GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: udp_wrap.h Lines: 4 4 100.0 %
Date: 2022-08-28 04:20:35 Branches: 0 0 - %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_UDP_WRAP_H_
23
#define SRC_UDP_WRAP_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "handle_wrap.h"
28
#include "req_wrap.h"
29
#include "node_sockaddr.h"
30
#include "uv.h"
31
#include "v8.h"
32
33
namespace node {
34
35
class UDPWrapBase;
36
37
// A listener that can be attached to an `UDPWrapBase` object and generally
38
// manages its I/O activity. This is similar to `StreamListener`.
39
class UDPListener {
40
 public:
41
  virtual ~UDPListener();
42
43
  // Called right before data is received from the socket. Must return a
44
  // buffer suitable for reading data into, that is then passed to OnRecv.
45
  virtual uv_buf_t OnAlloc(size_t suggested_size) = 0;
46
47
  // Called right after data is received from the socket, and includes
48
  // information about the source address. If `nread` is negative, an error
49
  // has occurred, and it represents a libuv error code.
50
  virtual void OnRecv(ssize_t nread,
51
                      const uv_buf_t& buf,
52
                      const sockaddr* addr,
53
                      unsigned int flags) = 0;
54
55
  // Called when an asynchronous request for writing data is created.
56
  // The `msg_size` value contains the total size of the data to be sent,
57
  // but may be ignored by the implementation of this Method.
58
  // The return value is later passed to OnSendDone.
59
  virtual ReqWrap<uv_udp_send_t>* CreateSendWrap(size_t msg_size) = 0;
60
61
  // Called when an asynchronous request for writing data has finished.
62
  // If status is negative, an error has occurred, and it represents a libuv
63
  // error code.
64
  virtual void OnSendDone(ReqWrap<uv_udp_send_t>* wrap, int status) = 0;
65
66
  // Optional callback that is called after the socket has been bound.
67
140
  virtual void OnAfterBind() {}
68
69
  inline UDPWrapBase* udp() const { return wrap_; }
70
71
 protected:
72
  UDPWrapBase* wrap_ = nullptr;
73
74
  friend class UDPWrapBase;
75
};
76
77
class UDPWrapBase {
78
 public:
79
  // While UDPWrapBase itself does not extend from HandleWrap, classes
80
  // derived from it will (like UDPWrap)
81
  enum InternalFields {
82
    kUDPWrapBaseField = HandleWrap::kInternalFieldCount,
83
    kInternalFieldCount
84
  };
85
  virtual ~UDPWrapBase();
86
87
  // Start emitting OnAlloc() + OnRecv() events on the listener.
88
  virtual int RecvStart() = 0;
89
90
  // Stop emitting OnAlloc() + OnRecv() events on the listener.
91
  virtual int RecvStop() = 0;
92
93
  // Send a chunk of data over this socket. This may call CreateSendWrap()
94
  // on the listener if an async transmission is necessary.
95
  virtual ssize_t Send(uv_buf_t* bufs,
96
                       size_t nbufs,
97
                       const sockaddr* addr) = 0;
98
99
  virtual SocketAddress GetPeerName() = 0;
100
  virtual SocketAddress GetSockName() = 0;
101
102
  // Returns an AsyncWrap object with the same lifetime as this object.
103
  virtual AsyncWrap* GetAsyncWrap() = 0;
104
105
  void set_listener(UDPListener* listener);
106
  UDPListener* listener() const;
107
108
  static UDPWrapBase* FromObject(v8::Local<v8::Object> obj);
109
110
  static void RecvStart(const v8::FunctionCallbackInfo<v8::Value>& args);
111
  static void RecvStop(const v8::FunctionCallbackInfo<v8::Value>& args);
112
  static void AddMethods(Environment* env, v8::Local<v8::FunctionTemplate> t);
113
114
 private:
115
  UDPListener* listener_ = nullptr;
116
};
117
118
class UDPWrap final : public HandleWrap,
119
                      public UDPWrapBase,
120
                      public UDPListener {
121
 public:
122
  enum SocketType {
123
    SOCKET
124
  };
125
  static void Initialize(v8::Local<v8::Object> target,
126
                         v8::Local<v8::Value> unused,
127
                         v8::Local<v8::Context> context,
128
                         void* priv);
129
  static void GetFD(const v8::FunctionCallbackInfo<v8::Value>& args);
130
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
131
  static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
132
  static void Bind(const v8::FunctionCallbackInfo<v8::Value>& args);
133
  static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args);
134
  static void Send(const v8::FunctionCallbackInfo<v8::Value>& args);
135
  static void Bind6(const v8::FunctionCallbackInfo<v8::Value>& args);
136
  static void Connect6(const v8::FunctionCallbackInfo<v8::Value>& args);
137
  static void Send6(const v8::FunctionCallbackInfo<v8::Value>& args);
138
  static void Disconnect(const v8::FunctionCallbackInfo<v8::Value>& args);
139
  static void AddMembership(const v8::FunctionCallbackInfo<v8::Value>& args);
140
  static void DropMembership(const v8::FunctionCallbackInfo<v8::Value>& args);
141
  static void AddSourceSpecificMembership(
142
      const v8::FunctionCallbackInfo<v8::Value>& args);
143
  static void DropSourceSpecificMembership(
144
      const v8::FunctionCallbackInfo<v8::Value>& args);
145
  static void SetMulticastInterface(
146
      const v8::FunctionCallbackInfo<v8::Value>& args);
147
  static void SetMulticastTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
148
  static void SetMulticastLoopback(
149
      const v8::FunctionCallbackInfo<v8::Value>& args);
150
  static void SetBroadcast(const v8::FunctionCallbackInfo<v8::Value>& args);
151
  static void SetTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
152
  static void BufferSize(const v8::FunctionCallbackInfo<v8::Value>& args);
153
  static void GetSendQueueSize(const v8::FunctionCallbackInfo<v8::Value>& args);
154
  static void GetSendQueueCount(
155
      const v8::FunctionCallbackInfo<v8::Value>& args);
156
157
  // UDPListener implementation
158
  uv_buf_t OnAlloc(size_t suggested_size) override;
159
  void OnRecv(ssize_t nread,
160
              const uv_buf_t& buf,
161
              const sockaddr* addr,
162
              unsigned int flags) override;
163
  ReqWrap<uv_udp_send_t>* CreateSendWrap(size_t msg_size) override;
164
  void OnSendDone(ReqWrap<uv_udp_send_t>* wrap, int status) override;
165
166
  // UDPWrapBase implementation
167
  int RecvStart() override;
168
  int RecvStop() override;
169
  ssize_t Send(uv_buf_t* bufs,
170
               size_t nbufs,
171
               const sockaddr* addr) override;
172
173
  SocketAddress GetPeerName() override;
174
  SocketAddress GetSockName() override;
175
176
  AsyncWrap* GetAsyncWrap() override;
177
178
  static v8::MaybeLocal<v8::Object> Instantiate(Environment* env,
179
                                                AsyncWrap* parent,
180
                                                SocketType type);
181
1
  SET_NO_MEMORY_INFO()
182
17
  SET_MEMORY_INFO_NAME(UDPWrap)
183
1
  SET_SELF_SIZE(UDPWrap)
184
185
 private:
186
  typedef uv_udp_t HandleType;
187
188
  template <typename T,
189
            int (*F)(const typename T::HandleType*, sockaddr*, int*)>
190
  friend void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>&);
191
192
  UDPWrap(Environment* env, v8::Local<v8::Object> object);
193
194
  static void DoBind(const v8::FunctionCallbackInfo<v8::Value>& args,
195
                     int family);
196
  static void DoConnect(const v8::FunctionCallbackInfo<v8::Value>& args,
197
                     int family);
198
  static void DoSend(const v8::FunctionCallbackInfo<v8::Value>& args,
199
                     int family);
200
  static void SetMembership(const v8::FunctionCallbackInfo<v8::Value>& args,
201
                            uv_membership membership);
202
  static void SetSourceMembership(
203
      const v8::FunctionCallbackInfo<v8::Value>& args,
204
      uv_membership membership);
205
206
  static void OnAlloc(uv_handle_t* handle,
207
                      size_t suggested_size,
208
                      uv_buf_t* buf);
209
  static void OnRecv(uv_udp_t* handle,
210
                     ssize_t nread,
211
                     const uv_buf_t* buf,
212
                     const struct sockaddr* addr,
213
                     unsigned int flags);
214
215
  uv_udp_t handle_;
216
217
  bool current_send_has_callback_;
218
  v8::Local<v8::Object> current_send_req_wrap_;
219
};
220
221
int sockaddr_for_family(int address_family,
222
                        const char* address,
223
                        const unsigned short port,
224
                        sockaddr_storage* addr);
225
226
}  // namespace node
227
228
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
229
230
#endif  // SRC_UDP_WRAP_H_