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.h Lines: 10 28 35.7 %
Date: 2020-08-17 22:13:26 Branches: 0 0 - %

Line Branch Exec Source
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 "env.h"
7
#include "memory_tracker.h"
8
#include "base_object.h"
9
#include "node.h"
10
#include "uv.h"
11
#include "v8.h"
12
13
#include <memory>
14
#include <string>
15
#include <list>
16
#include <unordered_map>
17
18
namespace node {
19
20
class Environment;
21
22
148
class SocketAddress : public MemoryRetainer {
23
 public:
24
  enum class CompareResult {
25
    NOT_COMPARABLE = -2,
26
    LESS_THAN,
27
    SAME,
28
    GREATER_THAN
29
  };
30
31
9
  struct Hash {
32
    size_t operator()(const SocketAddress& addr) const;
33
  };
34
35
  inline bool operator==(const SocketAddress& other) const;
36
  inline bool operator!=(const SocketAddress& other) const;
37
38
  inline bool operator<(const SocketAddress& other) const;
39
  inline bool operator>(const SocketAddress& other) const;
40
  inline bool operator<=(const SocketAddress& other) const;
41
  inline bool operator>=(const SocketAddress& other) const;
42
43
  inline static bool is_numeric_host(const char* hostname);
44
  inline static bool is_numeric_host(const char* hostname, int family);
45
46
  // Returns true if converting {family, host, port} to *addr succeeded.
47
  static bool ToSockAddr(
48
      int32_t family,
49
      const char* host,
50
      uint32_t port,
51
      sockaddr_storage* addr);
52
53
  // Returns true if converting {family, host, port} to *addr succeeded.
54
  static bool New(
55
      int32_t family,
56
      const char* host,
57
      uint32_t port,
58
      SocketAddress* addr);
59
60
  static bool New(
61
      const char* host,
62
      uint32_t port,
63
      SocketAddress* addr);
64
65
  // Returns the port for an IPv4 or IPv6 address.
66
  inline static int GetPort(const sockaddr* addr);
67
  inline static int GetPort(const sockaddr_storage* addr);
68
69
  // Returns the numeric host as a string for an IPv4 or IPv6 address.
70
  inline static std::string GetAddress(const sockaddr* addr);
71
  inline static std::string GetAddress(const sockaddr_storage* addr);
72
73
  // Returns the struct length for an IPv4, IPv6 or UNIX domain.
74
  inline static size_t GetLength(const sockaddr* addr);
75
  inline static size_t GetLength(const sockaddr_storage* addr);
76
77
  SocketAddress() = default;
78
79
  inline explicit SocketAddress(const sockaddr* addr);
80
  inline SocketAddress(const SocketAddress& addr);
81
  inline SocketAddress& operator=(const sockaddr* other);
82
  inline SocketAddress& operator=(const SocketAddress& other);
83
84
  inline const sockaddr& operator*() const;
85
  inline const sockaddr* operator->() const;
86
87
  inline const sockaddr* data() const;
88
  inline const uint8_t* raw() const;
89
  inline sockaddr* storage();
90
  inline size_t length() const;
91
92
  inline int family() const;
93
  inline std::string address() const;
94
  inline int port() const;
95
96
  // Returns true if the given other SocketAddress is a match
97
  // for this one. The addresses are a match if:
98
  // 1. They are the same family and match identically
99
  // 2. They are different family but match semantically (
100
  //     for instance, an IPv4 addres in IPv6 notation)
101
  bool is_match(const SocketAddress& other) const;
102
103
  // Compares this SocketAddress to the given other SocketAddress.
104
  CompareResult compare(const SocketAddress& other) const;
105
106
  // Returns true if this SocketAddress is within the subnet
107
  // identified by the given network address and CIDR prefix.
108
  bool is_in_network(const SocketAddress& network, int prefix) const;
109
110
  // If the SocketAddress is an IPv6 address, returns the
111
  // current value of the IPv6 flow label, if set. Otherwise
112
  // returns 0.
113
  inline uint32_t flow_label() const;
114
115
  // If the SocketAddress is an IPv6 address, sets the
116
  // current value of the IPv6 flow label. If not an
117
  // IPv6 address, set_flow_label is a non-op. It
118
  // is important to note that the flow label,
119
  // while represented as an uint32_t, the flow
120
  // label is strictly limited to 20 bits, and
121
  // this will assert if any value larger than
122
  // 20-bits is specified.
123
  inline void set_flow_label(uint32_t label = 0);
124
125
  inline void Update(uint8_t* data, size_t len);
126
127
  static SocketAddress FromSockName(const uv_udp_t& handle);
128
  static SocketAddress FromSockName(const uv_tcp_t& handle);
129
  static SocketAddress FromPeerName(const uv_udp_t& handle);
130
  static SocketAddress FromPeerName(const uv_tcp_t& handle);
131
132
  inline v8::Local<v8::Object> ToJS(
133
      Environment* env,
134
      v8::Local<v8::Object> obj = v8::Local<v8::Object>()) const;
135
136
  inline std::string ToString() const;
137
138
  SET_NO_MEMORY_INFO()
139
  SET_MEMORY_INFO_NAME(SocketAddress)
140
  SET_SELF_SIZE(SocketAddress)
141
142
  template <typename T>
143
  using Map = std::unordered_map<SocketAddress, T, Hash>;
144
145
 private:
146
  sockaddr_storage address_;
147
};
148
149
template <typename T>
150
1
class SocketAddressLRU : public MemoryRetainer {
151
 public:
152
  using Type = typename T::Type;
153
154
  inline explicit SocketAddressLRU(size_t max_size);
155
156
  // If the item already exists, returns a reference to
157
  // the existing item, adjusting items position in the
158
  // LRU. If the item does not exist, emplaces the item
159
  // and returns the new item.
160
  Type* Upsert(const SocketAddress& address);
161
162
  // Returns a reference to the item if it exists, or
163
  // nullptr. The position in the LRU is not modified.
164
  Type* Peek(const SocketAddress& address) const;
165
166
3
  size_t size() const { return map_.size(); }
167
  size_t max_size() const { return max_size_; }
168
169
  void MemoryInfo(MemoryTracker* tracker) const override;
170
  SET_MEMORY_INFO_NAME(SocketAddressLRU)
171
  SET_SELF_SIZE(SocketAddressLRU)
172
173
 private:
174
  using Pair = std::pair<SocketAddress, Type>;
175
  using Iterator = typename std::list<Pair>::iterator;
176
177
  void CheckExpired();
178
179
  std::list<Pair> list_;
180
  SocketAddress::Map<Iterator> map_;
181
  size_t max_size_;
182
};
183
184
// A BlockList is used to evaluate whether a given
185
// SocketAddress should be accepted for inbound or
186
// outbound network activity.
187
class SocketAddressBlockList : public MemoryRetainer {
188
 public:
189
  explicit SocketAddressBlockList(
190
      std::shared_ptr<SocketAddressBlockList> parent = {});
191
7
  ~SocketAddressBlockList() = default;
192
193
  void AddSocketAddress(
194
      const SocketAddress& address);
195
196
  void RemoveSocketAddress(
197
      const SocketAddress& address);
198
199
  void AddSocketAddressRange(
200
      const SocketAddress& start,
201
      const SocketAddress& end);
202
203
  void AddSocketAddressMask(
204
      const SocketAddress& address,
205
      int prefix);
206
207
  bool Apply(const SocketAddress& address);
208
209
  size_t size() const { return rules_.size(); }
210
211
  v8::MaybeLocal<v8::Array> ListRules(Environment* env);
212
213
28
  struct Rule : public MemoryRetainer {
214
    virtual bool Apply(const SocketAddress& address) = 0;
215
    inline v8::MaybeLocal<v8::Value> ToV8String(Environment* env);
216
    virtual std::string ToString() = 0;
217
  };
218
219
12
  struct SocketAddressRule final : Rule {
220
    SocketAddress address;
221
222
    explicit SocketAddressRule(const SocketAddress& address);
223
224
    bool Apply(const SocketAddress& address) override;
225
    std::string ToString() override;
226
227
    void MemoryInfo(node::MemoryTracker* tracker) const override;
228
    SET_MEMORY_INFO_NAME(SocketAddressRule)
229
    SET_SELF_SIZE(SocketAddressRule)
230
  };
231
232
6
  struct SocketAddressRangeRule final : Rule {
233
    SocketAddress start;
234
    SocketAddress end;
235
236
    SocketAddressRangeRule(
237
        const SocketAddress& start,
238
        const SocketAddress& end);
239
240
    bool Apply(const SocketAddress& address) override;
241
    std::string ToString() override;
242
243
    void MemoryInfo(node::MemoryTracker* tracker) const override;
244
    SET_MEMORY_INFO_NAME(SocketAddressRangeRule)
245
    SET_SELF_SIZE(SocketAddressRangeRule)
246
  };
247
248
10
  struct SocketAddressMaskRule final : Rule {
249
    SocketAddress network;
250
    int prefix;
251
252
    SocketAddressMaskRule(
253
        const SocketAddress& address,
254
        int prefix);
255
256
    bool Apply(const SocketAddress& address) override;
257
    std::string ToString() override;
258
259
    void MemoryInfo(node::MemoryTracker* tracker) const override;
260
    SET_MEMORY_INFO_NAME(SocketAddressMaskRule)
261
    SET_SELF_SIZE(SocketAddressMaskRule)
262
  };
263
264
  void MemoryInfo(node::MemoryTracker* tracker) const override;
265
  SET_MEMORY_INFO_NAME(SocketAddressBlockList)
266
  SET_SELF_SIZE(SocketAddressBlockList)
267
268
 private:
269
  std::shared_ptr<SocketAddressBlockList> parent_;
270
  std::list<std::unique_ptr<Rule>> rules_;
271
  SocketAddress::Map<std::list<std::unique_ptr<Rule>>::iterator> address_rules_;
272
};
273
274
12
class SocketAddressBlockListWrap :
275
    public BaseObject,
276
    public SocketAddressBlockList {
277
 public:
278
  static void Initialize(v8::Local<v8::Object> target,
279
                         v8::Local<v8::Value> unused,
280
                         v8::Local<v8::Context> context,
281
                         void* priv);
282
283
  static BaseObjectPtr<SocketAddressBlockListWrap> New(Environment* env);
284
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
285
  static void AddAddress(const v8::FunctionCallbackInfo<v8::Value>& args);
286
  static void AddRange(const v8::FunctionCallbackInfo<v8::Value>& args);
287
  static void AddSubnet(const v8::FunctionCallbackInfo<v8::Value>& args);
288
  static void Check(const v8::FunctionCallbackInfo<v8::Value>& args);
289
  static void GetRules(const v8::FunctionCallbackInfo<v8::Value>& args);
290
291
  SocketAddressBlockListWrap(
292
      Environment* env,
293
      v8::Local<v8::Object> wrap);
294
295
  void MemoryInfo(node::MemoryTracker* tracker) const override {
296
    SocketAddressBlockList::MemoryInfo(tracker);
297
  }
298
  SET_MEMORY_INFO_NAME(SocketAddressBlockListWrap)
299
  SET_SELF_SIZE(SocketAddressBlockListWrap)
300
};
301
302
}  // namespace node
303
304
#endif  // NOE_WANT_INTERNALS
305
306
#endif  // SRC_NODE_SOCKADDR_H_