• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_NODE_SOCKADDR_H_
2 #define SRC_NODE_SOCKADDR_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "memory_tracker.h"
7 #include "node.h"
8 #include "uv.h"
9 #include "v8.h"
10 
11 #include <string>
12 #include <unordered_map>
13 
14 namespace node {
15 
16 class Environment;
17 
18 class SocketAddress : public MemoryRetainer {
19  public:
20   struct Hash {
21     size_t operator()(const SocketAddress& addr) const;
22   };
23 
24   inline bool operator==(const SocketAddress& other) const;
25   inline bool operator!=(const SocketAddress& other) const;
26 
27   inline static bool is_numeric_host(const char* hostname);
28   inline static bool is_numeric_host(const char* hostname, int family);
29 
30   // Returns true if converting {family, host, port} to *addr succeeded.
31   static bool ToSockAddr(
32       int32_t family,
33       const char* host,
34       uint32_t port,
35       sockaddr_storage* addr);
36 
37   // Returns true if converting {family, host, port} to *addr succeeded.
38   static bool New(
39       int32_t family,
40       const char* host,
41       uint32_t port,
42       SocketAddress* addr);
43 
44   static bool New(
45       const char* host,
46       uint32_t port,
47       SocketAddress* addr);
48 
49   // Returns the port for an IPv4 or IPv6 address.
50   inline static int GetPort(const sockaddr* addr);
51   inline static int GetPort(const sockaddr_storage* addr);
52 
53   // Returns the numeric host as a string for an IPv4 or IPv6 address.
54   inline static std::string GetAddress(const sockaddr* addr);
55   inline static std::string GetAddress(const sockaddr_storage* addr);
56 
57   // Returns the struct length for an IPv4, IPv6 or UNIX domain.
58   inline static size_t GetLength(const sockaddr* addr);
59   inline static size_t GetLength(const sockaddr_storage* addr);
60 
61   SocketAddress() = default;
62 
63   inline explicit SocketAddress(const sockaddr* addr);
64   inline SocketAddress(const SocketAddress& addr);
65   inline SocketAddress& operator=(const sockaddr* other);
66   inline SocketAddress& operator=(const SocketAddress& other);
67 
68   inline const sockaddr& operator*() const;
69   inline const sockaddr* operator->() const;
70 
71   inline const sockaddr* data() const;
72   inline const uint8_t* raw() const;
73   inline sockaddr* storage();
74   inline size_t length() const;
75 
76   inline int family() const;
77   inline std::string address() const;
78   inline int port() const;
79 
80   // If the SocketAddress is an IPv6 address, returns the
81   // current value of the IPv6 flow label, if set. Otherwise
82   // returns 0.
83   inline uint32_t flow_label() const;
84 
85   // If the SocketAddress is an IPv6 address, sets the
86   // current value of the IPv6 flow label. If not an
87   // IPv6 address, set_flow_label is a non-op. It
88   // is important to note that the flow label,
89   // while represented as an uint32_t, the flow
90   // label is strictly limited to 20 bits, and
91   // this will assert if any value larger than
92   // 20-bits is specified.
93   inline void set_flow_label(uint32_t label = 0);
94 
95   inline void Update(uint8_t* data, size_t len);
96 
97   static SocketAddress FromSockName(const uv_udp_t& handle);
98   static SocketAddress FromSockName(const uv_tcp_t& handle);
99   static SocketAddress FromPeerName(const uv_udp_t& handle);
100   static SocketAddress FromPeerName(const uv_tcp_t& handle);
101 
102   inline v8::Local<v8::Object> ToJS(
103       Environment* env,
104       v8::Local<v8::Object> obj = v8::Local<v8::Object>()) const;
105 
106   inline std::string ToString() const;
107 
108   SET_NO_MEMORY_INFO()
109   SET_MEMORY_INFO_NAME(SocketAddress)
110   SET_SELF_SIZE(SocketAddress)
111 
112   template <typename T>
113   using Map = std::unordered_map<SocketAddress, T, Hash>;
114 
115  private:
116   sockaddr_storage address_;
117 };
118 
119 }  // namespace node
120 
121 #endif  // NOE_WANT_INTERNALS
122 
123 #endif  // SRC_NODE_SOCKADDR_H_
124