GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_sockaddr-inl.h Lines: 68 70 97.1 %
Date: 2020-08-07 22:13:19 Branches: 22 28 78.6 %

Line Branch Exec Source
1
#ifndef SRC_NODE_SOCKADDR_INL_H_
2
#define SRC_NODE_SOCKADDR_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "node.h"
7
#include "node_internals.h"
8
#include "node_sockaddr.h"
9
#include "util-inl.h"
10
11
#include <string>
12
13
namespace node {
14
15
static constexpr uint32_t kLabelMask = 0xFFFFF;
16
17
7
inline void hash_combine(size_t* seed) { }
18
19
template <typename T, typename... Args>
20
14
inline void hash_combine(size_t* seed, const T& value, Args... rest) {
21
14
    *seed ^= std::hash<T>{}(value) + 0x9e3779b9 + (*seed << 6) + (*seed >> 2);
22
14
    hash_combine(seed, rest...);
23
14
}
24
25
2
bool SocketAddress::is_numeric_host(const char* hostname) {
26

3
  return is_numeric_host(hostname, AF_INET) ||
27
3
         is_numeric_host(hostname, AF_INET6);
28
}
29
30
3
bool SocketAddress::is_numeric_host(const char* hostname, int family) {
31
  in6_addr dst;
32
3
  return inet_pton(family, hostname, &dst) == 1;
33
}
34
35
3
int SocketAddress::GetPort(const sockaddr* addr) {
36

3
  CHECK(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
37
6
  return ntohs(addr->sa_family == AF_INET ?
38
2
      reinterpret_cast<const sockaddr_in*>(addr)->sin_port :
39
4
      reinterpret_cast<const sockaddr_in6*>(addr)->sin6_port);
40
}
41
42
3
int SocketAddress::GetPort(const sockaddr_storage* addr) {
43
3
  return GetPort(reinterpret_cast<const sockaddr*>(addr));
44
}
45
46
3
std::string SocketAddress::GetAddress(const sockaddr* addr) {
47

3
  CHECK(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
48
  char host[INET6_ADDRSTRLEN];
49
3
  const void* src = addr->sa_family == AF_INET ?
50
      static_cast<const void*>(
51
          &(reinterpret_cast<const sockaddr_in*>(addr)->sin_addr)) :
52
      static_cast<const void*>(
53
3
          &(reinterpret_cast<const sockaddr_in6*>(addr)->sin6_addr));
54
3
  uv_inet_ntop(addr->sa_family, src, host, INET6_ADDRSTRLEN);
55
3
  return std::string(host);
56
}
57
58
3
std::string SocketAddress::GetAddress(const sockaddr_storage* addr) {
59
3
  return GetAddress(reinterpret_cast<const sockaddr*>(addr));
60
}
61
62
11
size_t SocketAddress::GetLength(const sockaddr* addr) {
63
11
  return addr->sa_family == AF_INET ?
64
11
      sizeof(sockaddr_in) : sizeof(sockaddr_in6);
65
}
66
67
8
size_t SocketAddress::GetLength(const sockaddr_storage* addr) {
68
8
  return GetLength(reinterpret_cast<const sockaddr*>(addr));
69
}
70
71
3
SocketAddress::SocketAddress(const sockaddr* addr) {
72
3
  memcpy(&address_, addr, GetLength(addr));
73
3
}
74
75
1
SocketAddress::SocketAddress(const SocketAddress& addr) {
76
1
  memcpy(&address_, &addr.address_, addr.length());
77
1
}
78
79
SocketAddress& SocketAddress::operator=(const sockaddr* addr) {
80
  memcpy(&address_, addr, GetLength(addr));
81
  return *this;
82
}
83
84
SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
85
  memcpy(&address_, &addr.address_, addr.length());
86
  return *this;
87
}
88
89
const sockaddr& SocketAddress::operator*() const {
90
  return *this->data();
91
}
92
93
const sockaddr* SocketAddress::operator->() const {
94
  return this->data();
95
}
96
97
8
size_t SocketAddress::length() const {
98
8
  return GetLength(&address_);
99
}
100
101
1
const sockaddr* SocketAddress::data() const {
102
1
  return reinterpret_cast<const sockaddr*>(&address_);
103
}
104
105
15
const uint8_t* SocketAddress::raw() const {
106
15
  return reinterpret_cast<const uint8_t*>(&address_);
107
}
108
109
sockaddr* SocketAddress::storage() {
110
  return reinterpret_cast<sockaddr*>(&address_);
111
}
112
113
22
int SocketAddress::family() const {
114
22
  return address_.ss_family;
115
}
116
117
3
std::string SocketAddress::address() const {
118
3
  return GetAddress(&address_);
119
}
120
121
3
int SocketAddress::port() const {
122
3
  return GetPort(&address_);
123
}
124
125
2
uint32_t SocketAddress::flow_label() const {
126
2
  if (family() != AF_INET6)
127
1
    return 0;
128
1
  const sockaddr_in6* in = reinterpret_cast<const sockaddr_in6*>(data());
129
1
  return in->sin6_flowinfo;
130
}
131
132
2
void SocketAddress::set_flow_label(uint32_t label) {
133
2
  if (family() != AF_INET6)
134
1
    return;
135
1
  CHECK_LE(label, kLabelMask);
136
1
  sockaddr_in6* in = reinterpret_cast<sockaddr_in6*>(&address_);
137
1
  in->sin6_flowinfo = label;
138
}
139
140
std::string SocketAddress::ToString() const {
141
  if (family() != AF_INET && family() != AF_INET6) return "";
142
  return (family() == AF_INET6 ?
143
              std::string("[") + address() + "]:" :
144
              address() + ":") +
145
      std::to_string(port());
146
}
147
148
1
void SocketAddress::Update(uint8_t* data, size_t len) {
149
1
  CHECK_LE(len, sizeof(address_));
150
1
  memcpy(&address_, data, len);
151
1
}
152
153
v8::Local<v8::Object> SocketAddress::ToJS(
154
    Environment* env,
155
    v8::Local<v8::Object> info) const {
156
  return AddressToJS(env, data(), info);
157
}
158
159
4
bool SocketAddress::operator==(const SocketAddress& other) const {
160
4
  if (family() != other.family()) return false;
161
4
  return memcmp(raw(), other.raw(), length()) == 0;
162
}
163
164
1
bool SocketAddress::operator!=(const SocketAddress& other) const {
165
1
  return !(*this == other);
166
}
167
}  // namespace node
168
169
#endif  // NODE_WANT_INTERNALS
170
#endif  // SRC_NODE_SOCKADDR_INL_H_