2626 #include < sys/socket.h>
2727 #include < netinet/in.h>
2828 #include < arpa/inet.h>
29+ #include < netdb.h>
2930 #include < unistd.h>
3031 #define closesocket close
3132 typedef int fossil_io_socket_t ;
3233 #define FOSSIL_IO_INVALID_SOCKET (-1 )
3334#endif
3435
36+ #define FOSSIL_IO_SOCKET_TYPE_TCP SOCK_STREAM
37+ #define FOSSIL_IO_SOCKET_TYPE_UDP SOCK_DGRAM
38+
3539#ifdef __cplusplus
3640extern " C" {
3741#endif
3842
3943/* *
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.
4249 */
4350int fossil_io_network_create (void );
4451
4552/* *
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.
4756 */
4857void fossil_io_network_destroy (void );
4958
5059/* *
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.
5364 */
54- fossil_io_socket_t fossil_io_network_create_socket (void );
65+ fossil_io_socket_t fossil_io_network_create_socket (int type );
5566
5667/* *
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.
5976 */
6077int fossil_io_network_bind (fossil_io_socket_t sock, const char *ip, uint16_t port);
6178
6279/* *
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.
6586 */
6687int fossil_io_network_listen (fossil_io_socket_t sock, int backlog);
6788
6889/* *
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.
7198 */
7299fossil_io_socket_t fossil_io_network_accept (fossil_io_socket_t sock, char *client_ip, uint16_t *client_port);
73100
74101/* *
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.
77109 */
78110int fossil_io_network_connect (fossil_io_socket_t sock, const char *ip, uint16_t port);
79111
80112/* *
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.
83120 */
84121int fossil_io_network_send (fossil_io_socket_t sock, const void *data, size_t len);
85122
86123/* *
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.
89131 */
90132int fossil_io_network_receive (fossil_io_socket_t sock, void *buffer, size_t len);
91133
92134/* *
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.
94139 */
95140void fossil_io_network_close (fossil_io_socket_t sock);
96141
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+
97178#ifdef __cplusplus
98179}
180+
99181/* *
100182 * C++ wrapper for the output functions.
101183 */
@@ -110,83 +192,166 @@ namespace fossil {
110192 class Network {
111193 public:
112194 /* *
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.
115200 */
116201 static int init (void ) {
117202 return fossil_io_network_create ();
118203 }
119204
120205 /* *
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.
122209 */
123210 static void cleanup (void ) {
124211 fossil_io_network_destroy ();
125212 }
126213
127214 /* *
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.
130219 */
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 );
133222 }
134223
135224 /* *
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.
138233 */
139234 static int bind (fossil_io_socket_t sock, const char *ip, uint16_t port) {
140235 return fossil_io_network_bind (sock, ip, port);
141236 }
142237
143238 /* *
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.
146245 */
147246 static int listen (fossil_io_socket_t sock, int backlog) {
148247 return fossil_io_network_listen (sock, backlog);
149248 }
150249
151250 /* *
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.
154259 */
155260 static fossil_io_socket_t accept (fossil_io_socket_t sock, char *client_ip, uint16_t *client_port) {
156261 return fossil_io_network_accept (sock, client_ip, client_port);
157262 }
158263
159264 /* *
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.
162272 */
163273 static int connect (fossil_io_socket_t sock, const char *ip, uint16_t port) {
164274 return fossil_io_network_connect (sock, ip, port);
165275 }
166276
167277 /* *
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.
170285 */
171286 static int send (fossil_io_socket_t sock, const void *data, size_t len) {
172287 return fossil_io_network_send (sock, data, len);
173288 }
174289
175290 /* *
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.
178298 */
179299 static int receive (fossil_io_socket_t sock, void *buffer, size_t len) {
180300 return fossil_io_network_receive (sock, buffer, len);
181301 }
182302
183303 /* *
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.
185308 */
186309 static void close (fossil_io_socket_t sock) {
187310 fossil_io_network_close (sock);
188311 }
189312
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+
190355 };
191356 }
192357}
0 commit comments