librats 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,15 @@
27
27
 
28
28
  namespace librats {
29
29
 
30
+ /**
31
+ * Address family for socket creation
32
+ */
33
+ enum class AddressFamily {
34
+ IPv4, // IPv4 only
35
+ IPv6, // IPv6 only
36
+ DualStack // IPv6 socket with IPv4 support (default)
37
+ };
38
+
30
39
  /**
31
40
  * UDP peer information
32
41
  */
@@ -69,49 +78,15 @@ void cleanup_socket_library();
69
78
  socket_t create_tcp_client(const std::string& host, int port, int timeout_ms = 0);
70
79
 
71
80
  /**
72
- * Create a TCP client socket and connect to a server using IPv4 only
73
- * @param host The hostname or IP address to connect to
74
- * @param port The port number to connect to
75
- * @param timeout_ms Connection timeout in milliseconds (0 for blocking)
76
- * @return Socket handle, or INVALID_SOCKET_VALUE on error
77
- */
78
- socket_t create_tcp_client_v4(const std::string& host, int port, int timeout_ms = 0);
79
-
80
- /**
81
- * Create a TCP client socket and connect to a server using IPv6 only
82
- * @param host The hostname or IPv6 address to connect to
83
- * @param port The port number to connect to
84
- * @param timeout_ms Connection timeout in milliseconds (0 for blocking)
85
- * @return Socket handle, or INVALID_SOCKET_VALUE on error
86
- */
87
- socket_t create_tcp_client_v6(const std::string& host, int port, int timeout_ms = 0);
88
-
89
- /**
90
- * Create a TCP server socket and bind to a port using dual stack (IPv6 with IPv4 support)
81
+ * Create a TCP server socket and bind to a port
91
82
  * @param port The port number to bind to
92
83
  * @param backlog The maximum number of pending connections
93
84
  * @param bind_address The interface IP address to bind to (empty for all interfaces)
85
+ * @param af Address family (DualStack by default)
94
86
  * @return Socket handle, or INVALID_SOCKET_VALUE on error
95
87
  */
96
- socket_t create_tcp_server(int port, int backlog = 5, const std::string& bind_address = "");
97
-
98
- /**
99
- * Create a TCP server socket and bind to a port using IPv4 only
100
- * @param port The port number to bind to
101
- * @param backlog The maximum number of pending connections
102
- * @param bind_address The interface IP address to bind to (empty for all interfaces)
103
- * @return Socket handle, or INVALID_SOCKET_VALUE on error
104
- */
105
- socket_t create_tcp_server_v4(int port, int backlog = 5, const std::string& bind_address = "");
106
-
107
- /**
108
- * Create a TCP server socket and bind to a port using IPv6 only
109
- * @param port The port number to bind to
110
- * @param backlog The maximum number of pending connections
111
- * @param bind_address The interface IP address to bind to (empty for all interfaces)
112
- * @return Socket handle, or INVALID_SOCKET_VALUE on error
113
- */
114
- socket_t create_tcp_server_v6(int port, int backlog = 5, const std::string& bind_address = "");
88
+ socket_t create_tcp_server(int port, int backlog = 5, const std::string& bind_address = "",
89
+ AddressFamily af = AddressFamily::DualStack);
115
90
 
116
91
  /**
117
92
  * Accept a client connection on a server socket
@@ -144,29 +119,20 @@ int send_tcp_data(socket_t socket, const std::vector<uint8_t>& data);
144
119
  std::vector<uint8_t> receive_tcp_data(socket_t socket, size_t buffer_size = 1024);
145
120
 
146
121
  /**
147
- * Send a framed message with length prefix through a TCP socket
122
+ * Send a length-prefixed framed message through a TCP socket
148
123
  * @param socket The socket handle
149
124
  * @param message The binary message to send
150
125
  * @return Total bytes sent (including length prefix), or -1 on error
151
126
  */
152
- int send_tcp_message_framed(socket_t socket, const std::vector<uint8_t>& message);
153
-
154
- /**
155
- * Receive exact number of bytes from a TCP socket (blocking until complete)
156
- * @param socket The socket handle
157
- * @param num_bytes Number of bytes to receive
158
- * @return Received binary data, empty vector on error or connection close
159
- */
160
- std::vector<uint8_t> receive_exact_bytes(socket_t socket, size_t num_bytes);
127
+ int send_tcp_message(socket_t socket, const std::vector<uint8_t>& message);
161
128
 
162
129
  /**
163
- * Receive a complete framed message from a TCP socket
130
+ * Receive a complete length-prefixed framed message from a TCP socket
164
131
  * @param socket The socket handle
165
132
  * @return Complete binary message, empty vector on error or connection close
166
133
  */
167
- std::vector<uint8_t> receive_tcp_message_framed(socket_t socket);
134
+ std::vector<uint8_t> receive_tcp_message(socket_t socket);
168
135
 
169
- // Convenience functions for string compatibility
170
136
  /**
171
137
  * Send string data through a TCP socket (converts to binary)
172
138
  * @param socket The socket handle
@@ -175,98 +141,45 @@ std::vector<uint8_t> receive_tcp_message_framed(socket_t socket);
175
141
  */
176
142
  int send_tcp_string(socket_t socket, const std::string& data);
177
143
 
178
- /**
179
- * Receive data from a TCP socket as string
180
- * @param socket The socket handle
181
- * @param buffer_size Maximum number of bytes to receive
182
- * @return Received data as string, empty string on error
183
- */
184
- std::string receive_tcp_string(socket_t socket, size_t buffer_size = 1024);
185
-
186
- /**
187
- * Send a framed string message through a TCP socket
188
- * @param socket The socket handle
189
- * @param message The string message to send
190
- * @return Total bytes sent (including length prefix), or -1 on error
191
- */
192
- int send_tcp_string_framed(socket_t socket, const std::string& message);
193
-
194
- /**
195
- * Receive a complete framed string message from a TCP socket
196
- * @param socket The socket handle
197
- * @return Complete message as string, empty string on error or connection close
198
- */
199
- std::string receive_tcp_string_framed(socket_t socket);
200
-
201
144
  // UDP Socket Functions
202
145
  /**
203
- * Create a UDP socket with dual stack support (IPv6 with IPv4 support)
204
- * @param port The port to bind to (0 for any available port)
205
- * @param bind_address The interface IP address to bind to (empty for all interfaces)
206
- * @return UDP socket handle, or INVALID_SOCKET_VALUE on error
207
- */
208
- socket_t create_udp_socket(int port = 0, const std::string& bind_address = "");
209
-
210
- /**
211
- * Create a UDP socket with IPv4 support only
212
- * @param port The port to bind to (0 for any available port)
213
- * @param bind_address The interface IP address to bind to (empty for all interfaces)
214
- * @return UDP socket handle, or INVALID_SOCKET_VALUE on error
215
- */
216
- socket_t create_udp_socket_v4(int port = 0, const std::string& bind_address = "");
217
-
218
- /**
219
- * Create a UDP socket with IPv6 support only
146
+ * Create a UDP socket and bind to a port
220
147
  * @param port The port to bind to (0 for any available port)
221
148
  * @param bind_address The interface IP address to bind to (empty for all interfaces)
149
+ * @param af Address family (DualStack by default)
222
150
  * @return UDP socket handle, or INVALID_SOCKET_VALUE on error
223
151
  */
224
- socket_t create_udp_socket_v6(int port = 0, const std::string& bind_address = "");
225
-
226
- /**
227
- * Send UDP data to a peer
228
- * @param socket The UDP socket handle
229
- * @param data The data to send
230
- * @param peer The destination peer
231
- * @return Number of bytes sent, or -1 on error
232
- */
233
- int send_udp_data(socket_t socket, const std::vector<uint8_t>& data, const Peer& peer);
152
+ socket_t create_udp_socket(int port = 0, const std::string& bind_address = "",
153
+ AddressFamily af = AddressFamily::DualStack);
234
154
 
235
155
  /**
236
- * Send UDP data to a hostname and port directly
156
+ * Send UDP data to a destination host and port
237
157
  * @param socket The UDP socket handle
238
158
  * @param data The data to send
239
- * @param hostname The destination hostname or IP address
159
+ * @param host The destination hostname or IP address
240
160
  * @param port The destination port
161
+ * @param af Address family matching the socket (DualStack by default)
241
162
  * @return Number of bytes sent, or -1 on error
242
163
  */
243
- int send_udp_data_to(socket_t socket, const std::vector<uint8_t>& data, const std::string& hostname, int port);
164
+ int send_udp_data(socket_t socket, const std::vector<uint8_t>& data, const std::string& host, int port,
165
+ AddressFamily af = AddressFamily::DualStack);
244
166
 
245
167
  /**
246
- * Receive UDP data from a peer
168
+ * Receive UDP data with optional timeout
247
169
  * @param socket The UDP socket handle
248
170
  * @param buffer_size Maximum number of bytes to receive
249
171
  * @param sender_peer Output parameter for the sender's peer info
250
- * @return Received data, empty vector on error
251
- */
252
- std::vector<uint8_t> receive_udp_data(socket_t socket, size_t buffer_size, Peer& sender_peer);
253
-
254
- /**
255
- * Receive UDP data with timeout support
256
- * @param socket The UDP socket handle
257
- * @param buffer_size Maximum number of bytes to receive
258
- * @param timeout_ms Timeout in milliseconds (0 for non-blocking, -1 for blocking)
259
- * @param sender_ip Optional output parameter for sender IP address
260
- * @param sender_port Optional output parameter for sender port
172
+ * @param timeout_ms Timeout in milliseconds (-1 for blocking, 0 for non-blocking, >0 for timeout)
261
173
  * @return Received data, empty vector on timeout or error
262
174
  */
263
- std::vector<uint8_t> receive_udp_data_with_timeout(socket_t socket, size_t buffer_size, int timeout_ms,
264
- std::string* sender_ip = nullptr, int* sender_port = nullptr);
175
+ std::vector<uint8_t> receive_udp_data(socket_t socket, size_t buffer_size, Peer& sender_peer,
176
+ int timeout_ms = -1);
265
177
 
266
178
  // Common Socket Functions
267
179
  /**
268
180
  * Close a socket
269
181
  * @param socket The socket handle to close
182
+ * @param force If true, send RST instead of FIN (avoids TIME_WAIT)
270
183
  */
271
184
  void close_socket(socket_t socket, bool force = false);
272
185
 
@@ -291,13 +204,6 @@ bool set_socket_nonblocking(socket_t socket);
291
204
  */
292
205
  bool set_socket_blocking(socket_t socket);
293
206
 
294
- /**
295
- * Check if a socket is a TCP socket
296
- * @param socket The socket handle to check
297
- * @return true if TCP socket, false otherwise
298
- */
299
- bool is_tcp_socket(socket_t socket);
300
-
301
207
  /**
302
208
  * Connect to a socket address with timeout
303
209
  * @param socket The socket handle (should be non-blocking)
@@ -309,10 +215,10 @@ bool is_tcp_socket(socket_t socket);
309
215
  bool connect_with_timeout(socket_t socket, struct sockaddr* addr, socklen_t addr_len, int timeout_ms);
310
216
 
311
217
  /**
312
- * Get the ephemeral port that a socket is bound to
313
- * @param socket The socket handle to get the ephemeral port for
314
- * @return The ephemeral port, or 0 if the socket is not bound to an ephemeral port
218
+ * Get the port that a socket is bound to
219
+ * @param socket The socket handle
220
+ * @return The bound port, or 0 on error
315
221
  */
316
- int get_ephemeral_port(socket_t socket);
222
+ int get_bound_port(socket_t socket);
317
223
 
318
- } // namespace librats
224
+ } // namespace librats
@@ -1047,7 +1047,7 @@ std::optional<StunMessage> StunClient::send_request(socket_t socket,
1047
1047
  }
1048
1048
 
1049
1049
  // Send request
1050
- int sent = send_udp_data_to(socket, data, server, port);
1050
+ int sent = send_udp_data(socket, data, server, port);
1051
1051
  if (sent <= 0) {
1052
1052
  LOG_STUN_ERROR("Failed to send STUN request");
1053
1053
  return std::nullopt;
@@ -1057,11 +1057,9 @@ std::optional<StunMessage> StunClient::send_request(socket_t socket,
1057
1057
 
1058
1058
  // Wait for response with current RTO
1059
1059
  int wait_time = (std::min)(rto, timeout_ms - total_time);
1060
- std::string sender_ip;
1061
- int sender_port;
1060
+ Peer sender;
1062
1061
 
1063
- auto response_data = receive_udp_data_with_timeout(socket, STUN_MAX_MESSAGE_SIZE,
1064
- wait_time, &sender_ip, &sender_port);
1062
+ auto response_data = receive_udp_data(socket, STUN_MAX_MESSAGE_SIZE, sender, wait_time);
1065
1063
 
1066
1064
  total_time += wait_time;
1067
1065
 
@@ -673,12 +673,13 @@ std::vector<uint8_t> UdpTrackerClient::send_request(const std::vector<uint8_t>&
673
673
  }
674
674
 
675
675
  // Send request
676
- if (send_udp_data_to(socket_, request, hostname_, port_) <= 0) {
676
+ if (send_udp_data(socket_, request, hostname_, port_) <= 0) {
677
677
  return std::vector<uint8_t>();
678
678
  }
679
679
 
680
680
  // Receive response with timeout
681
- std::vector<uint8_t> response = receive_udp_data_with_timeout(socket_, 2048, timeout_ms);
681
+ Peer sender;
682
+ std::vector<uint8_t> response = receive_udp_data(socket_, 2048, sender, timeout_ms);
682
683
 
683
684
  return response;
684
685
  }
@@ -600,7 +600,7 @@ bool TurnClient::send_indication(const StunMappedAddress& peer, const std::vecto
600
600
  indication.add_data(data);
601
601
 
602
602
  auto msg_data = indication.serialize();
603
- int sent = send_udp_data_to(socket_, msg_data, config_.server, config_.port);
603
+ int sent = send_udp_data(socket_, msg_data, config_.server, config_.port);
604
604
 
605
605
  return sent > 0;
606
606
  }
@@ -625,7 +625,7 @@ bool TurnClient::send_channel_data(uint16_t channel, const std::vector<uint8_t>&
625
625
  msg.push_back(0);
626
626
  }
627
627
 
628
- int sent = send_udp_data_to(socket_, msg, config_.server, config_.port);
628
+ int sent = send_udp_data(socket_, msg, config_.server, config_.port);
629
629
  return sent > 0;
630
630
  }
631
631
 
@@ -634,10 +634,8 @@ std::optional<std::pair<StunMappedAddress, std::vector<uint8_t>>> TurnClient::re
634
634
  return std::nullopt;
635
635
  }
636
636
 
637
- std::string sender_ip;
638
- int sender_port;
639
- auto data = receive_udp_data_with_timeout(socket_, STUN_MAX_MESSAGE_SIZE, timeout_ms,
640
- &sender_ip, &sender_port);
637
+ Peer sender;
638
+ auto data = receive_udp_data(socket_, STUN_MAX_MESSAGE_SIZE, sender, timeout_ms);
641
639
 
642
640
  if (data.empty()) {
643
641
  return std::nullopt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "librats",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "Node.js bindings for librats - A high-performance peer-to-peer networking library",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -1330,6 +1330,25 @@ Napi::Value GetVersion(const Napi::CallbackInfo& info) {
1330
1330
  return version;
1331
1331
  }
1332
1332
 
1333
+ // Console logging control functions
1334
+ Napi::Value SetConsoleLoggingEnabled(const Napi::CallbackInfo& info) {
1335
+ Napi::Env env = info.Env();
1336
+
1337
+ if (info.Length() < 1 || !info[0].IsBoolean()) {
1338
+ Napi::TypeError::New(env, "Boolean expected").ThrowAsJavaScriptException();
1339
+ return env.Undefined();
1340
+ }
1341
+
1342
+ bool enabled = info[0].As<Napi::Boolean>().Value();
1343
+ rats_set_console_logging_enabled(enabled ? 1 : 0);
1344
+ return env.Undefined();
1345
+ }
1346
+
1347
+ Napi::Value IsConsoleLoggingEnabled(const Napi::CallbackInfo& info) {
1348
+ Napi::Env env = info.Env();
1349
+ return Napi::Boolean::New(env, rats_is_console_logging_enabled() != 0);
1350
+ }
1351
+
1333
1352
  Napi::Value GetGitDescribe(const Napi::CallbackInfo& info) {
1334
1353
  Napi::Env env = info.Env();
1335
1354
  const char* git_describe = rats_get_git_describe();
@@ -1399,6 +1418,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
1399
1418
  exports.Set("getGitDescribe", Napi::Function::New(env, GetGitDescribe));
1400
1419
  exports.Set("getAbi", Napi::Function::New(env, GetAbi));
1401
1420
 
1421
+ // Export logging control functions
1422
+ exports.Set("setConsoleLoggingEnabled", Napi::Function::New(env, SetConsoleLoggingEnabled));
1423
+ exports.Set("isConsoleLoggingEnabled", Napi::Function::New(env, IsConsoleLoggingEnabled));
1424
+
1402
1425
  // Export constants
1403
1426
  exports.Set("constants", InitConstants(env));
1404
1427