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.cc Lines: 13 38 34.2 %
Date: 2020-05-27 22:15:15 Branches: 3 18 16.7 %

Line Branch Exec Source
1
#include "node_sockaddr-inl.h"  // NOLINT(build/include)
2
#include "uv.h"
3
4
namespace node {
5
6
namespace {
7
template <typename T, typename F>
8
SocketAddress FromUVHandle(F fn, const T& handle) {
9
  SocketAddress addr;
10
  int len = sizeof(sockaddr_storage);
11
  if (fn(&handle, addr.storage(), &len) == 0)
12
    CHECK_EQ(static_cast<size_t>(len), addr.length());
13
  else
14
    addr.storage()->sa_family = 0;
15
  return addr;
16
}
17
}  // namespace
18
19
3
bool SocketAddress::ToSockAddr(
20
    int32_t family,
21
    const char* host,
22
    uint32_t port,
23
    sockaddr_storage* addr) {
24
3
  switch (family) {
25
    case AF_INET:
26
2
      return uv_ip4_addr(
27
          host,
28
          port,
29
2
          reinterpret_cast<sockaddr_in*>(addr)) == 0;
30
    case AF_INET6:
31
1
      return uv_ip6_addr(
32
          host,
33
          port,
34
1
          reinterpret_cast<sockaddr_in6*>(addr)) == 0;
35
    default:
36
      UNREACHABLE();
37
  }
38
}
39
40
bool SocketAddress::New(
41
    const char* host,
42
    uint32_t port,
43
    SocketAddress* addr) {
44
  return New(AF_INET, host, port, addr) || New(AF_INET6, host, port, addr);
45
}
46
47
bool SocketAddress::New(
48
    int32_t family,
49
    const char* host,
50
    uint32_t port,
51
    SocketAddress* addr) {
52
  return ToSockAddr(family, host, port,
53
                    reinterpret_cast<sockaddr_storage*>(addr->storage()));
54
}
55
56
7
size_t SocketAddress::Hash::operator()(const SocketAddress& addr) const {
57
7
  size_t hash = 0;
58
7
  switch (addr.family()) {
59
    case AF_INET: {
60
      const sockaddr_in* ipv4 =
61
7
          reinterpret_cast<const sockaddr_in*>(addr.raw());
62
7
      hash_combine(&hash, ipv4->sin_port, ipv4->sin_addr.s_addr);
63
7
      break;
64
    }
65
    case AF_INET6: {
66
      const sockaddr_in6* ipv6 =
67
          reinterpret_cast<const sockaddr_in6*>(addr.raw());
68
      const uint64_t* a =
69
          reinterpret_cast<const uint64_t*>(&ipv6->sin6_addr);
70
      hash_combine(&hash, ipv6->sin6_port, a[0], a[1]);
71
      break;
72
    }
73
    default:
74
      UNREACHABLE();
75
  }
76
7
  return hash;
77
}
78
79
SocketAddress SocketAddress::FromSockName(const uv_tcp_t& handle) {
80
  return FromUVHandle(uv_tcp_getsockname, handle);
81
}
82
83
SocketAddress SocketAddress::FromSockName(const uv_udp_t& handle) {
84
  return FromUVHandle(uv_udp_getsockname, handle);
85
}
86
87
SocketAddress SocketAddress::FromPeerName(const uv_tcp_t& handle) {
88
  return FromUVHandle(uv_tcp_getpeername, handle);
89
}
90
91
SocketAddress SocketAddress::FromPeerName(const uv_udp_t& handle) {
92
  return FromUVHandle(uv_udp_getpeername, handle);
93
}
94
95
}  // namespace node