librats 0.5.0 → 0.5.1

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.
Files changed (63) hide show
  1. package/binding.gyp +1 -0
  2. package/native-src/3rdparty/android/ifaddrs-android.c +600 -0
  3. package/native-src/3rdparty/android/ifaddrs-android.h +54 -0
  4. package/native-src/CMakeLists.txt +360 -0
  5. package/native-src/LICENSE +21 -0
  6. package/native-src/src/bencode.cpp +485 -0
  7. package/native-src/src/bencode.h +145 -0
  8. package/native-src/src/bittorrent.cpp +3682 -0
  9. package/native-src/src/bittorrent.h +731 -0
  10. package/native-src/src/dht.cpp +2342 -0
  11. package/native-src/src/dht.h +501 -0
  12. package/native-src/src/encrypted_socket.cpp +817 -0
  13. package/native-src/src/encrypted_socket.h +239 -0
  14. package/native-src/src/file_transfer.cpp +1808 -0
  15. package/native-src/src/file_transfer.h +567 -0
  16. package/native-src/src/fs.cpp +639 -0
  17. package/native-src/src/fs.h +108 -0
  18. package/native-src/src/gossipsub.cpp +1137 -0
  19. package/native-src/src/gossipsub.h +403 -0
  20. package/native-src/src/ice.cpp +1386 -0
  21. package/native-src/src/ice.h +328 -0
  22. package/native-src/src/json.hpp +25526 -0
  23. package/native-src/src/krpc.cpp +558 -0
  24. package/native-src/src/krpc.h +145 -0
  25. package/native-src/src/librats.cpp +2715 -0
  26. package/native-src/src/librats.h +1729 -0
  27. package/native-src/src/librats_bittorrent.cpp +167 -0
  28. package/native-src/src/librats_c.cpp +1317 -0
  29. package/native-src/src/librats_c.h +237 -0
  30. package/native-src/src/librats_encryption.cpp +123 -0
  31. package/native-src/src/librats_file_transfer.cpp +226 -0
  32. package/native-src/src/librats_gossipsub.cpp +293 -0
  33. package/native-src/src/librats_ice.cpp +515 -0
  34. package/native-src/src/librats_logging.cpp +158 -0
  35. package/native-src/src/librats_mdns.cpp +171 -0
  36. package/native-src/src/librats_nat.cpp +571 -0
  37. package/native-src/src/librats_persistence.cpp +815 -0
  38. package/native-src/src/logger.h +412 -0
  39. package/native-src/src/mdns.cpp +1178 -0
  40. package/native-src/src/mdns.h +253 -0
  41. package/native-src/src/network_utils.cpp +598 -0
  42. package/native-src/src/network_utils.h +162 -0
  43. package/native-src/src/noise.cpp +981 -0
  44. package/native-src/src/noise.h +227 -0
  45. package/native-src/src/os.cpp +371 -0
  46. package/native-src/src/os.h +40 -0
  47. package/native-src/src/rats_export.h +17 -0
  48. package/native-src/src/sha1.cpp +163 -0
  49. package/native-src/src/sha1.h +42 -0
  50. package/native-src/src/socket.cpp +1376 -0
  51. package/native-src/src/socket.h +309 -0
  52. package/native-src/src/stun.cpp +484 -0
  53. package/native-src/src/stun.h +349 -0
  54. package/native-src/src/threadmanager.cpp +105 -0
  55. package/native-src/src/threadmanager.h +53 -0
  56. package/native-src/src/tracker.cpp +1110 -0
  57. package/native-src/src/tracker.h +268 -0
  58. package/native-src/src/version.cpp +24 -0
  59. package/native-src/src/version.h.in +45 -0
  60. package/native-src/version.rc.in +31 -0
  61. package/package.json +2 -8
  62. package/scripts/build-librats.js +59 -12
  63. package/scripts/prepare-package.js +133 -37
@@ -0,0 +1,309 @@
1
+ #pragma once
2
+
3
+ #include <string>
4
+ #include <functional>
5
+ #include <vector>
6
+ #include <cstdint>
7
+
8
+ #ifdef _WIN32
9
+ #include <winsock2.h>
10
+ #include <ws2tcpip.h>
11
+ #pragma comment(lib, "ws2_32.lib")
12
+ typedef SOCKET socket_t;
13
+ #define INVALID_SOCKET_VALUE INVALID_SOCKET
14
+ #define SOCKET_ERROR_VALUE SOCKET_ERROR
15
+ #else
16
+ #include <sys/socket.h>
17
+ #include <arpa/inet.h>
18
+ #include <netinet/in.h>
19
+ #include <unistd.h>
20
+ typedef int socket_t;
21
+ #define INVALID_SOCKET_VALUE -1
22
+ #define SOCKET_ERROR_VALUE -1
23
+ #define closesocket close
24
+ #endif
25
+
26
+ namespace librats {
27
+
28
+ /**
29
+ * UDP peer information
30
+ */
31
+ struct Peer {
32
+ std::string ip;
33
+ uint16_t port;
34
+
35
+ Peer() : port(0) {}
36
+ Peer(const std::string& ip, uint16_t port) : ip(ip), port(port) {}
37
+
38
+ bool operator==(const Peer& other) const {
39
+ return ip == other.ip && port == other.port;
40
+ }
41
+
42
+ bool operator!=(const Peer& other) const {
43
+ return !(*this == other);
44
+ }
45
+ };
46
+
47
+ // Socket Library Initialization
48
+ /**
49
+ * Initialize the socket library
50
+ * @return true if successful, false otherwise
51
+ */
52
+ bool init_socket_library();
53
+
54
+ /**
55
+ * Cleanup the socket library
56
+ */
57
+ void cleanup_socket_library();
58
+
59
+ // TCP Socket Functions
60
+ /**
61
+ * Create a TCP client socket and connect to a server using dual stack (IPv6 with IPv4 fallback)
62
+ * @param host The hostname or IP address to connect to
63
+ * @param port The port number to connect to
64
+ * @param timeout_ms Connection timeout in milliseconds (0 for blocking)
65
+ * @return Socket handle, or INVALID_SOCKET_VALUE on error
66
+ */
67
+ socket_t create_tcp_client(const std::string& host, int port, int timeout_ms = 0);
68
+
69
+ /**
70
+ * Create a TCP client socket and connect to a server using IPv4 only
71
+ * @param host The hostname or IP address to connect to
72
+ * @param port The port number to connect to
73
+ * @param timeout_ms Connection timeout in milliseconds (0 for blocking)
74
+ * @return Socket handle, or INVALID_SOCKET_VALUE on error
75
+ */
76
+ socket_t create_tcp_client_v4(const std::string& host, int port, int timeout_ms = 0);
77
+
78
+ /**
79
+ * Create a TCP client socket and connect to a server using IPv6 only
80
+ * @param host The hostname or IPv6 address to connect to
81
+ * @param port The port number to connect to
82
+ * @param timeout_ms Connection timeout in milliseconds (0 for blocking)
83
+ * @return Socket handle, or INVALID_SOCKET_VALUE on error
84
+ */
85
+ socket_t create_tcp_client_v6(const std::string& host, int port, int timeout_ms = 0);
86
+
87
+ /**
88
+ * Create a TCP server socket and bind to a port using dual stack (IPv6 with IPv4 support)
89
+ * @param port The port number to bind to
90
+ * @param backlog The maximum number of pending connections
91
+ * @param bind_address The interface IP address to bind to (empty for all interfaces)
92
+ * @return Socket handle, or INVALID_SOCKET_VALUE on error
93
+ */
94
+ socket_t create_tcp_server(int port, int backlog = 5, const std::string& bind_address = "");
95
+
96
+ /**
97
+ * Create a TCP server socket and bind to a port using IPv4 only
98
+ * @param port The port number to bind to
99
+ * @param backlog The maximum number of pending connections
100
+ * @param bind_address The interface IP address to bind to (empty for all interfaces)
101
+ * @return Socket handle, or INVALID_SOCKET_VALUE on error
102
+ */
103
+ socket_t create_tcp_server_v4(int port, int backlog = 5, const std::string& bind_address = "");
104
+
105
+ /**
106
+ * Create a TCP server socket and bind to a port using IPv6 only
107
+ * @param port The port number to bind to
108
+ * @param backlog The maximum number of pending connections
109
+ * @param bind_address The interface IP address to bind to (empty for all interfaces)
110
+ * @return Socket handle, or INVALID_SOCKET_VALUE on error
111
+ */
112
+ socket_t create_tcp_server_v6(int port, int backlog = 5, const std::string& bind_address = "");
113
+
114
+ /**
115
+ * Accept a client connection on a server socket
116
+ * @param server_socket The server socket handle
117
+ * @return Client socket handle, or INVALID_SOCKET_VALUE on error
118
+ */
119
+ socket_t accept_client(socket_t server_socket);
120
+
121
+ /**
122
+ * Get the peer address (IP:port) from a connected socket
123
+ * @param socket The connected socket handle
124
+ * @return Peer address string in format "IP:port", or empty string on error
125
+ */
126
+ std::string get_peer_address(socket_t socket);
127
+
128
+ /**
129
+ * Send data through a TCP socket
130
+ * @param socket The socket handle
131
+ * @param data The binary data to send
132
+ * @return Number of bytes sent, or -1 on error
133
+ */
134
+ int send_tcp_data(socket_t socket, const std::vector<uint8_t>& data);
135
+
136
+ /**
137
+ * Receive data from a TCP socket
138
+ * @param socket The socket handle
139
+ * @param buffer_size Maximum number of bytes to receive
140
+ * @return Received binary data, empty vector on error
141
+ */
142
+ std::vector<uint8_t> receive_tcp_data(socket_t socket, size_t buffer_size = 1024);
143
+
144
+ /**
145
+ * Send a framed message with length prefix through a TCP socket
146
+ * @param socket The socket handle
147
+ * @param message The binary message to send
148
+ * @return Total bytes sent (including length prefix), or -1 on error
149
+ */
150
+ int send_tcp_message_framed(socket_t socket, const std::vector<uint8_t>& message);
151
+
152
+ /**
153
+ * Receive exact number of bytes from a TCP socket (blocking until complete)
154
+ * @param socket The socket handle
155
+ * @param num_bytes Number of bytes to receive
156
+ * @return Received binary data, empty vector on error or connection close
157
+ */
158
+ std::vector<uint8_t> receive_exact_bytes(socket_t socket, size_t num_bytes);
159
+
160
+ /**
161
+ * Receive a complete framed message from a TCP socket
162
+ * @param socket The socket handle
163
+ * @return Complete binary message, empty vector on error or connection close
164
+ */
165
+ std::vector<uint8_t> receive_tcp_message_framed(socket_t socket);
166
+
167
+ // Convenience functions for string compatibility
168
+ /**
169
+ * Send string data through a TCP socket (converts to binary)
170
+ * @param socket The socket handle
171
+ * @param data The string data to send
172
+ * @return Number of bytes sent, or -1 on error
173
+ */
174
+ int send_tcp_string(socket_t socket, const std::string& data);
175
+
176
+ /**
177
+ * Receive data from a TCP socket as string
178
+ * @param socket The socket handle
179
+ * @param buffer_size Maximum number of bytes to receive
180
+ * @return Received data as string, empty string on error
181
+ */
182
+ std::string receive_tcp_string(socket_t socket, size_t buffer_size = 1024);
183
+
184
+ /**
185
+ * Send a framed string message through a TCP socket
186
+ * @param socket The socket handle
187
+ * @param message The string message to send
188
+ * @return Total bytes sent (including length prefix), or -1 on error
189
+ */
190
+ int send_tcp_string_framed(socket_t socket, const std::string& message);
191
+
192
+ /**
193
+ * Receive a complete framed string message from a TCP socket
194
+ * @param socket The socket handle
195
+ * @return Complete message as string, empty string on error or connection close
196
+ */
197
+ std::string receive_tcp_string_framed(socket_t socket);
198
+
199
+ // UDP Socket Functions
200
+ /**
201
+ * Create a UDP socket with dual stack support (IPv6 with IPv4 support)
202
+ * @param port The port to bind to (0 for any available port)
203
+ * @param bind_address The interface IP address to bind to (empty for all interfaces)
204
+ * @return UDP socket handle, or INVALID_SOCKET_VALUE on error
205
+ */
206
+ socket_t create_udp_socket(int port = 0, const std::string& bind_address = "");
207
+
208
+ /**
209
+ * Create a UDP socket with IPv4 support only
210
+ * @param port The port to bind to (0 for any available port)
211
+ * @param bind_address The interface IP address to bind to (empty for all interfaces)
212
+ * @return UDP socket handle, or INVALID_SOCKET_VALUE on error
213
+ */
214
+ socket_t create_udp_socket_v4(int port = 0, const std::string& bind_address = "");
215
+
216
+ /**
217
+ * Create a UDP socket with IPv6 support only
218
+ * @param port The port to bind to (0 for any available port)
219
+ * @param bind_address The interface IP address to bind to (empty for all interfaces)
220
+ * @return UDP socket handle, or INVALID_SOCKET_VALUE on error
221
+ */
222
+ socket_t create_udp_socket_v6(int port = 0, const std::string& bind_address = "");
223
+
224
+ /**
225
+ * Send UDP data to a peer
226
+ * @param socket The UDP socket handle
227
+ * @param data The data to send
228
+ * @param peer The destination peer
229
+ * @return Number of bytes sent, or -1 on error
230
+ */
231
+ int send_udp_data(socket_t socket, const std::vector<uint8_t>& data, const Peer& peer);
232
+
233
+ /**
234
+ * Send UDP data to a hostname and port directly
235
+ * @param socket The UDP socket handle
236
+ * @param data The data to send
237
+ * @param hostname The destination hostname or IP address
238
+ * @param port The destination port
239
+ * @return Number of bytes sent, or -1 on error
240
+ */
241
+ int send_udp_data_to(socket_t socket, const std::vector<uint8_t>& data, const std::string& hostname, int port);
242
+
243
+ /**
244
+ * Receive UDP data from a peer
245
+ * @param socket The UDP socket handle
246
+ * @param buffer_size Maximum number of bytes to receive
247
+ * @param sender_peer Output parameter for the sender's peer info
248
+ * @return Received data, empty vector on error
249
+ */
250
+ std::vector<uint8_t> receive_udp_data(socket_t socket, size_t buffer_size, Peer& sender_peer);
251
+
252
+ /**
253
+ * Receive UDP data with timeout support
254
+ * @param socket The UDP socket handle
255
+ * @param buffer_size Maximum number of bytes to receive
256
+ * @param timeout_ms Timeout in milliseconds (0 for non-blocking, -1 for blocking)
257
+ * @param sender_ip Optional output parameter for sender IP address
258
+ * @param sender_port Optional output parameter for sender port
259
+ * @return Received data, empty vector on timeout or error
260
+ */
261
+ std::vector<uint8_t> receive_udp_data_with_timeout(socket_t socket, size_t buffer_size, int timeout_ms,
262
+ std::string* sender_ip = nullptr, int* sender_port = nullptr);
263
+
264
+ // Common Socket Functions
265
+ /**
266
+ * Close a socket
267
+ * @param socket The socket handle to close
268
+ */
269
+ void close_socket(socket_t socket, bool force = false);
270
+
271
+ /**
272
+ * Check if a socket is valid
273
+ * @param socket The socket handle to check
274
+ * @return true if valid, false otherwise
275
+ */
276
+ bool is_valid_socket(socket_t socket);
277
+
278
+ /**
279
+ * Set socket to non-blocking mode
280
+ * @param socket The socket handle
281
+ * @return true if successful, false otherwise
282
+ */
283
+ bool set_socket_nonblocking(socket_t socket);
284
+
285
+ /**
286
+ * Check if a socket is a TCP socket
287
+ * @param socket The socket handle to check
288
+ * @return true if TCP socket, false otherwise
289
+ */
290
+ bool is_tcp_socket(socket_t socket);
291
+
292
+ /**
293
+ * Connect to a socket address with timeout
294
+ * @param socket The socket handle (should be non-blocking)
295
+ * @param addr The socket address structure
296
+ * @param addr_len Length of the address structure
297
+ * @param timeout_ms Connection timeout in milliseconds
298
+ * @return true if connected successfully, false on timeout or error
299
+ */
300
+ bool connect_with_timeout(socket_t socket, struct sockaddr* addr, socklen_t addr_len, int timeout_ms);
301
+
302
+ /**
303
+ * Get the ephemeral port that a socket is bound to
304
+ * @param socket The socket handle to get the ephemeral port for
305
+ * @return The ephemeral port, or 0 if the socket is not bound to an ephemeral port
306
+ */
307
+ int get_ephemeral_port(socket_t socket);
308
+
309
+ } // namespace librats