Skip to content

Commit 3446949

Browse files
adding bridge
1 parent 39bac90 commit 3446949

File tree

2 files changed

+189
-44
lines changed

2 files changed

+189
-44
lines changed

code/logic/fossil/io/network.h

Lines changed: 176 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,75 +41,140 @@ extern "C" {
4141
#endif
4242

4343
/**
44-
* Initialize the network stack (needed for Windows).
45-
* 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.
4649
*/
4750
int fossil_io_network_create(void);
4851

4952
/**
50-
* 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.
5156
*/
5257
void fossil_io_network_destroy(void);
5358

5459
/**
55-
* Create a new TCP socket.
56-
* 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.
5764
*/
5865
fossil_io_socket_t fossil_io_network_create_socket(int type);
5966

6067
/**
61-
* Bind a socket to a specific port (IPv4/IPv6).
62-
* 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.
6376
*/
6477
int fossil_io_network_bind(fossil_io_socket_t sock, const char *ip, uint16_t port);
6578

6679
/**
67-
* Listen for incoming connections.
68-
* 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.
6986
*/
7087
int fossil_io_network_listen(fossil_io_socket_t sock, int backlog);
7188

7289
/**
73-
* Accept a new connection.
74-
* 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.
7598
*/
7699
fossil_io_socket_t fossil_io_network_accept(fossil_io_socket_t sock, char *client_ip, uint16_t *client_port);
77100

78101
/**
79-
* Connect to a remote server.
80-
* 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.
81109
*/
82110
int fossil_io_network_connect(fossil_io_socket_t sock, const char *ip, uint16_t port);
83111

84112
/**
85-
* Send data over a socket.
86-
* 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.
87120
*/
88121
int fossil_io_network_send(fossil_io_socket_t sock, const void *data, size_t len);
89122

90123
/**
91-
* Receive data from a socket.
92-
* 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.
93131
*/
94132
int fossil_io_network_receive(fossil_io_socket_t sock, void *buffer, size_t len);
95133

96134
/**
97-
* 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.
98139
*/
99140
void fossil_io_network_close(fossil_io_socket_t sock);
100141

101142
/**
102-
* Send data to a specific IP address and port.
103-
* Returns the number of bytes sent, or -1 on failure.
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.
104152
*/
105153
int fossil_io_network_sendto(fossil_io_socket_t sock, const void *data, size_t len, const char *ip, uint16_t port);
106154

107155
/**
108-
* Receive data from a specific IP address and port.
109-
* Returns the number of bytes received, or -1 on failure.
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.
110165
*/
111166
int fossil_io_network_recvfrom(fossil_io_socket_t sock, void *buffer, size_t len, char *ip, uint16_t *port);
112167

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+
113178
#ifdef __cplusplus
114179
}
115180

@@ -127,99 +192,166 @@ namespace fossil {
127192
class Network {
128193
public:
129194
/**
130-
* Initialize the network stack (needed for Windows).
131-
* 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.
132200
*/
133201
static int init(void) {
134202
return fossil_io_network_create();
135203
}
136204

137205
/**
138-
* 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.
139209
*/
140210
static void cleanup(void) {
141211
fossil_io_network_destroy();
142212
}
143213

144214
/**
145-
* Create a new TCP socket.
146-
* 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.
147219
*/
148220
static fossil_io_socket_t create_socket(int type) {
149221
return fossil_io_network_create_socket(type);
150222
}
151223

152224
/**
153-
* Bind a socket to a specific port (IPv4/IPv6).
154-
* 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.
155233
*/
156234
static int bind(fossil_io_socket_t sock, const char *ip, uint16_t port) {
157235
return fossil_io_network_bind(sock, ip, port);
158236
}
159237

160238
/**
161-
* Listen for incoming connections.
162-
* 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.
163245
*/
164246
static int listen(fossil_io_socket_t sock, int backlog) {
165247
return fossil_io_network_listen(sock, backlog);
166248
}
167249

168250
/**
169-
* Accept a new connection.
170-
* 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.
171259
*/
172260
static fossil_io_socket_t accept(fossil_io_socket_t sock, char *client_ip, uint16_t *client_port) {
173261
return fossil_io_network_accept(sock, client_ip, client_port);
174262
}
175263

176264
/**
177-
* Connect to a remote server.
178-
* 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.
179272
*/
180273
static int connect(fossil_io_socket_t sock, const char *ip, uint16_t port) {
181274
return fossil_io_network_connect(sock, ip, port);
182275
}
183276

184277
/**
185-
* Send data over a socket.
186-
* 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.
187285
*/
188286
static int send(fossil_io_socket_t sock, const void *data, size_t len) {
189287
return fossil_io_network_send(sock, data, len);
190288
}
191289

192290
/**
193-
* Receive data from a socket.
194-
* 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.
195298
*/
196299
static int receive(fossil_io_socket_t sock, void *buffer, size_t len) {
197300
return fossil_io_network_receive(sock, buffer, len);
198301
}
199302

200303
/**
201-
* 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.
202308
*/
203309
static void close(fossil_io_socket_t sock) {
204310
fossil_io_network_close(sock);
205311
}
206312

207313
/**
208-
* Send data to a specific IP address and port.
209-
* Returns the number of bytes sent, or -1 on failure.
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.
210323
*/
211324
static int sendto(fossil_io_socket_t sock, const void *data, size_t len, const char *ip, uint16_t port) {
212325
return fossil_io_network_sendto(sock, data, len, ip, port);
213326
}
214327

215328
/**
216-
* Receive data from a specific IP address and port.
217-
* Returns the number of bytes received, or -1 on failure.
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.
218338
*/
219339
static int recvfrom(fossil_io_socket_t sock, void *buffer, size_t len, char *ip, uint16_t *port) {
220340
return fossil_io_network_recvfrom(sock, buffer, len, ip, port);
221341
}
222342

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+
223355
};
224356
}
225357
}

code/logic/network.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,16 @@ void fossil_io_network_close(fossil_io_socket_t sock) {
186186
}
187187
#endif
188188
}
189+
190+
int fossil_io_network_bridge(fossil_io_socket_t sock1, fossil_io_socket_t sock2) {
191+
char buffer[1024];
192+
int bytes_received;
193+
194+
while ((bytes_received = fossil_io_network_receive(sock1, buffer, sizeof(buffer))) > 0) {
195+
if (fossil_io_network_send(sock2, buffer, bytes_received) == -1) {
196+
return -1;
197+
}
198+
}
199+
200+
return bytes_received;
201+
}

0 commit comments

Comments
 (0)