26
26
#include < sys/socket.h>
27
27
#include < netinet/in.h>
28
28
#include < arpa/inet.h>
29
+ #include < netdb.h>
29
30
#include < unistd.h>
30
31
#define closesocket close
31
32
typedef int fossil_io_socket_t ;
32
33
#define FOSSIL_IO_INVALID_SOCKET (-1 )
33
34
#endif
34
35
36
+ #define FOSSIL_IO_SOCKET_TYPE_TCP SOCK_STREAM
37
+ #define FOSSIL_IO_SOCKET_TYPE_UDP SOCK_DGRAM
38
+
35
39
#ifdef __cplusplus
36
40
extern " C" {
37
41
#endif
38
42
39
43
/* *
40
- * Initialize the network stack (needed for Windows).
41
- * Returns 0 on success, non-zero on failure.
44
+ * Initialize the network stack. This function is necessary for Windows
45
+ * platforms to set up the Winsock library. On other platforms, it may
46
+ * perform other necessary initializations.
47
+ *
48
+ * @return 0 on success, non-zero on failure.
42
49
*/
43
50
int fossil_io_network_create (void );
44
51
45
52
/* *
46
- * Clean up network stack (needed for Windows).
53
+ * Clean up the network stack. This function is necessary for Windows
54
+ * platforms to clean up the Winsock library. On other platforms, it may
55
+ * perform other necessary clean-up operations.
47
56
*/
48
57
void fossil_io_network_destroy (void );
49
58
50
59
/* *
51
- * Create a new TCP socket.
52
- * Returns a valid socket on success or FOSSIL_IO_INVALID_SOCKET on failure.
60
+ * Create a new socket of the specified type (TCP or UDP).
61
+ *
62
+ * @param type The type of socket to create (e.g., SOCK_STREAM for TCP, SOCK_DGRAM for UDP).
63
+ * @return A valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure.
53
64
*/
54
- fossil_io_socket_t fossil_io_network_create_socket (void );
65
+ fossil_io_socket_t fossil_io_network_create_socket (int type );
55
66
56
67
/* *
57
- * Bind a socket to a specific port (IPv4/IPv6).
58
- * Returns 0 on success, -1 on failure.
68
+ * Bind a socket to a specific IP address and port. This function associates
69
+ * the socket with a local address so that it can listen for incoming connections
70
+ * or send data.
71
+ *
72
+ * @param sock The socket to bind.
73
+ * @param ip The IP address to bind to (e.g., "127.0.0.1" for localhost).
74
+ * @param port The port number to bind to.
75
+ * @return 0 on success, -1 on failure.
59
76
*/
60
77
int fossil_io_network_bind (fossil_io_socket_t sock, const char *ip, uint16_t port);
61
78
62
79
/* *
63
- * Listen for incoming connections.
64
- * Returns 0 on success, -1 on failure.
80
+ * Listen for incoming connections on a bound socket. This function puts the
81
+ * socket into a state where it can accept incoming connection requests.
82
+ *
83
+ * @param sock The socket to listen on.
84
+ * @param backlog The maximum length of the queue of pending connections.
85
+ * @return 0 on success, -1 on failure.
65
86
*/
66
87
int fossil_io_network_listen (fossil_io_socket_t sock, int backlog);
67
88
68
89
/* *
69
- * Accept a new connection.
70
- * Returns a valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure.
90
+ * Accept a new incoming connection on a listening socket. This function
91
+ * extracts the first connection request on the queue of pending connections
92
+ * and creates a new socket for the connection.
93
+ *
94
+ * @param sock The listening socket.
95
+ * @param client_ip A buffer to store the IP address of the connecting client.
96
+ * @param client_port A pointer to store the port number of the connecting client.
97
+ * @return A valid socket for the new connection on success, or FOSSIL_IO_INVALID_SOCKET on failure.
71
98
*/
72
99
fossil_io_socket_t fossil_io_network_accept (fossil_io_socket_t sock, char *client_ip, uint16_t *client_port);
73
100
74
101
/* *
75
- * Connect to a remote server.
76
- * Returns 0 on success, -1 on failure.
102
+ * Connect to a remote server. This function establishes a connection to a
103
+ * specified IP address and port.
104
+ *
105
+ * @param sock The socket to use for the connection.
106
+ * @param ip The IP address of the remote server.
107
+ * @param port The port number of the remote server.
108
+ * @return 0 on success, -1 on failure.
77
109
*/
78
110
int fossil_io_network_connect (fossil_io_socket_t sock, const char *ip, uint16_t port);
79
111
80
112
/* *
81
- * Send data over a socket.
82
- * Returns the number of bytes sent, or -1 on failure.
113
+ * Send data over a connected socket. This function transmits data to the
114
+ * remote peer connected to the socket.
115
+ *
116
+ * @param sock The socket to use for sending data.
117
+ * @param data A pointer to the data to be sent.
118
+ * @param len The length of the data to be sent.
119
+ * @return The number of bytes sent, or -1 on failure.
83
120
*/
84
121
int fossil_io_network_send (fossil_io_socket_t sock, const void *data, size_t len);
85
122
86
123
/* *
87
- * Receive data from a socket.
88
- * Returns the number of bytes received, or -1 on failure.
124
+ * Receive data from a connected socket. This function reads data from the
125
+ * remote peer connected to the socket.
126
+ *
127
+ * @param sock The socket to use for receiving data.
128
+ * @param buffer A buffer to store the received data.
129
+ * @param len The maximum length of data to be received.
130
+ * @return The number of bytes received, or -1 on failure.
89
131
*/
90
132
int fossil_io_network_receive (fossil_io_socket_t sock, void *buffer, size_t len);
91
133
92
134
/* *
93
- * Close a socket.
135
+ * Close a socket. This function releases the resources associated with the
136
+ * socket and closes the connection.
137
+ *
138
+ * @param sock The socket to be closed.
94
139
*/
95
140
void fossil_io_network_close (fossil_io_socket_t sock);
96
141
142
+ /* *
143
+ * Send data to a specific IP address and port using a UDP socket. This function
144
+ * transmits data to the specified address and port without establishing a connection.
145
+ *
146
+ * @param sock The socket to use for sending data.
147
+ * @param data A pointer to the data to be sent.
148
+ * @param len The length of the data to be sent.
149
+ * @param ip The IP address of the destination.
150
+ * @param port The port number of the destination.
151
+ * @return The number of bytes sent, or -1 on failure.
152
+ */
153
+ int fossil_io_network_sendto (fossil_io_socket_t sock, const void *data, size_t len, const char *ip, uint16_t port);
154
+
155
+ /* *
156
+ * Receive data from a specific IP address and port using a UDP socket. This function
157
+ * reads data from the specified address and port without establishing a connection.
158
+ *
159
+ * @param sock The socket to use for receiving data.
160
+ * @param buffer A buffer to store the received data.
161
+ * @param len The maximum length of data to be received.
162
+ * @param ip A buffer to store the IP address of the sender.
163
+ * @param port A pointer to store the port number of the sender.
164
+ * @return The number of bytes received, or -1 on failure.
165
+ */
166
+ int fossil_io_network_recvfrom (fossil_io_socket_t sock, void *buffer, size_t len, char *ip, uint16_t *port);
167
+
168
+ /* *
169
+ * Bridge function for network operations. This function can be used to
170
+ * transfer data between two sockets, effectively bridging them.
171
+ *
172
+ * @param sock1 The first socket.
173
+ * @param sock2 The second socket.
174
+ * @return 0 on success, -1 on failure.
175
+ */
176
+ int fossil_io_network_bridge (fossil_io_socket_t sock1, fossil_io_socket_t sock2);
177
+
97
178
#ifdef __cplusplus
98
179
}
180
+
99
181
/* *
100
182
* C++ wrapper for the output functions.
101
183
*/
@@ -110,83 +192,166 @@ namespace fossil {
110
192
class Network {
111
193
public:
112
194
/* *
113
- * Initialize the network stack (needed for Windows).
114
- * Returns 0 on success, non-zero on failure.
195
+ * Initialize the network stack. This function is necessary for Windows
196
+ * platforms to set up the Winsock library. On other platforms, it may
197
+ * perform other necessary initializations.
198
+ *
199
+ * @return 0 on success, non-zero on failure.
115
200
*/
116
201
static int init (void ) {
117
202
return fossil_io_network_create ();
118
203
}
119
204
120
205
/* *
121
- * Clean up network stack (needed for Windows).
206
+ * Clean up the network stack. This function is necessary for Windows
207
+ * platforms to clean up the Winsock library. On other platforms, it may
208
+ * perform other necessary clean-up operations.
122
209
*/
123
210
static void cleanup (void ) {
124
211
fossil_io_network_destroy ();
125
212
}
126
213
127
214
/* *
128
- * Create a new TCP socket.
129
- * Returns a valid socket on success or FOSSIL_IO_INVALID_SOCKET on failure.
215
+ * Create a new socket of the specified type (TCP or UDP).
216
+ *
217
+ * @param type The type of socket to create (e.g., SOCK_STREAM for TCP, SOCK_DGRAM for UDP).
218
+ * @return A valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure.
130
219
*/
131
- static fossil_io_socket_t create_socket (void ) {
132
- return fossil_io_network_create_socket ();
220
+ static fossil_io_socket_t create_socket (int type ) {
221
+ return fossil_io_network_create_socket (type );
133
222
}
134
223
135
224
/* *
136
- * Bind a socket to a specific port (IPv4/IPv6).
137
- * Returns 0 on success, -1 on failure.
225
+ * Bind a socket to a specific IP address and port. This function associates
226
+ * the socket with a local address so that it can listen for incoming connections
227
+ * or send data.
228
+ *
229
+ * @param sock The socket to bind.
230
+ * @param ip The IP address to bind to (e.g., "127.0.0.1" for localhost).
231
+ * @param port The port number to bind to.
232
+ * @return 0 on success, -1 on failure.
138
233
*/
139
234
static int bind (fossil_io_socket_t sock, const char *ip, uint16_t port) {
140
235
return fossil_io_network_bind (sock, ip, port);
141
236
}
142
237
143
238
/* *
144
- * Listen for incoming connections.
145
- * Returns 0 on success, -1 on failure.
239
+ * Listen for incoming connections on a bound socket. This function puts the
240
+ * socket into a state where it can accept incoming connection requests.
241
+ *
242
+ * @param sock The socket to listen on.
243
+ * @param backlog The maximum length of the queue of pending connections.
244
+ * @return 0 on success, -1 on failure.
146
245
*/
147
246
static int listen (fossil_io_socket_t sock, int backlog) {
148
247
return fossil_io_network_listen (sock, backlog);
149
248
}
150
249
151
250
/* *
152
- * Accept a new connection.
153
- * Returns a valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure.
251
+ * Accept a new incoming connection on a listening socket. This function
252
+ * extracts the first connection request on the queue of pending connections
253
+ * and creates a new socket for the connection.
254
+ *
255
+ * @param sock The listening socket.
256
+ * @param client_ip A buffer to store the IP address of the connecting client.
257
+ * @param client_port A pointer to store the port number of the connecting client.
258
+ * @return A valid socket for the new connection on success, or FOSSIL_IO_INVALID_SOCKET on failure.
154
259
*/
155
260
static fossil_io_socket_t accept (fossil_io_socket_t sock, char *client_ip, uint16_t *client_port) {
156
261
return fossil_io_network_accept (sock, client_ip, client_port);
157
262
}
158
263
159
264
/* *
160
- * Connect to a remote server.
161
- * Returns 0 on success, -1 on failure.
265
+ * Connect to a remote server. This function establishes a connection to a
266
+ * specified IP address and port.
267
+ *
268
+ * @param sock The socket to use for the connection.
269
+ * @param ip The IP address of the remote server.
270
+ * @param port The port number of the remote server.
271
+ * @return 0 on success, -1 on failure.
162
272
*/
163
273
static int connect (fossil_io_socket_t sock, const char *ip, uint16_t port) {
164
274
return fossil_io_network_connect (sock, ip, port);
165
275
}
166
276
167
277
/* *
168
- * Send data over a socket.
169
- * Returns the number of bytes sent, or -1 on failure.
278
+ * Send data over a connected socket. This function transmits data to the
279
+ * remote peer connected to the socket.
280
+ *
281
+ * @param sock The socket to use for sending data.
282
+ * @param data A pointer to the data to be sent.
283
+ * @param len The length of the data to be sent.
284
+ * @return The number of bytes sent, or -1 on failure.
170
285
*/
171
286
static int send (fossil_io_socket_t sock, const void *data, size_t len) {
172
287
return fossil_io_network_send (sock, data, len);
173
288
}
174
289
175
290
/* *
176
- * Receive data from a socket.
177
- * Returns the number of bytes received, or -1 on failure.
291
+ * Receive data from a connected socket. This function reads data from the
292
+ * remote peer connected to the socket.
293
+ *
294
+ * @param sock The socket to use for receiving data.
295
+ * @param buffer A buffer to store the received data.
296
+ * @param len The maximum length of data to be received.
297
+ * @return The number of bytes received, or -1 on failure.
178
298
*/
179
299
static int receive (fossil_io_socket_t sock, void *buffer, size_t len) {
180
300
return fossil_io_network_receive (sock, buffer, len);
181
301
}
182
302
183
303
/* *
184
- * Close a socket.
304
+ * Close a socket. This function releases the resources associated with the
305
+ * socket and closes the connection.
306
+ *
307
+ * @param sock The socket to be closed.
185
308
*/
186
309
static void close (fossil_io_socket_t sock) {
187
310
fossil_io_network_close (sock);
188
311
}
189
312
313
+ /* *
314
+ * Send data to a specific IP address and port using a UDP socket. This function
315
+ * transmits data to the specified address and port without establishing a connection.
316
+ *
317
+ * @param sock The socket to use for sending data.
318
+ * @param data A pointer to the data to be sent.
319
+ * @param len The length of the data to be sent.
320
+ * @param ip The IP address of the destination.
321
+ * @param port The port number of the destination.
322
+ * @return The number of bytes sent, or -1 on failure.
323
+ */
324
+ static int sendto (fossil_io_socket_t sock, const void *data, size_t len, const char *ip, uint16_t port) {
325
+ return fossil_io_network_sendto (sock, data, len, ip, port);
326
+ }
327
+
328
+ /* *
329
+ * Receive data from a specific IP address and port using a UDP socket. This function
330
+ * reads data from the specified address and port without establishing a connection.
331
+ *
332
+ * @param sock The socket to use for receiving data.
333
+ * @param buffer A buffer to store the received data.
334
+ * @param len The maximum length of data to be received.
335
+ * @param ip A buffer to store the IP address of the sender.
336
+ * @param port A pointer to store the port number of the sender.
337
+ * @return The number of bytes received, or -1 on failure.
338
+ */
339
+ static int recvfrom (fossil_io_socket_t sock, void *buffer, size_t len, char *ip, uint16_t *port) {
340
+ return fossil_io_network_recvfrom (sock, buffer, len, ip, port);
341
+ }
342
+
343
+ /* *
344
+ * Bridge function for network operations. This function can be used to
345
+ * transfer data between two sockets, effectively bridging them.
346
+ *
347
+ * @param sock1 The first socket.
348
+ * @param sock2 The second socket.
349
+ * @return 0 on success, -1 on failure.
350
+ */
351
+ static int bridge (fossil_io_socket_t sock1, fossil_io_socket_t sock2) {
352
+ return fossil_io_network_bridge (sock1, sock2);
353
+ }
354
+
190
355
};
191
356
}
192
357
}
0 commit comments