1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef __NETWORK_UTIL_H
#define __NETWORK_UTIL_H
#include <netdb.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <sys/select.h>
typedef struct connection connection_t;
/**
* A callback function type used for handling network events.
*
* @param fd The file descriptor of the connection
* @param str The string data received from the connection
*
* This callback is typically used with the nu_multiplex function to handle
* data received from local or remote connections.
*/
typedef void nu_callback(int fd, char *str);
/**
* Listens on localhost:port for a single client connection.
* This function is blocking (i.e., it waits for a client to
* connect before returning).
**/
connection_t *nu_wait_client(int port);
/**
* Opens a client connection to the server pointed to by ip:port.
* This function assumes that the server has already called nu_wait_client.
**/
connection_t *nu_connect_server(const char *ip, int port);
/**
* Sends a string to the open connection represented by conn.
**/
int nu_send_str(connection_t *conn, const char *str);
/**
* Attempts to read a "\r\n\r\n" terminated header from the open connection
* represented by conn. This function will block if sender has sent unterminated
* bytes, but will return NULL immediately if the sender has sent nothing.
**/
char *nu_try_read_header(connection_t *conn);
/**
* Reads a "\r\n\r\n" terminated header from the open connection represented by
* conn.
* This function is blocking.
**/
char *nu_read_header(connection_t *conn);
/**
* Sends bytes to the open connection represented by conn.
*/
int nu_send_bytes(connection_t *conn, const char *bytes, size_t bytes_len);
/**
* Attempts to read a block of bytes from the connection represented by conn.
* This function will block if it receives some bytes but less than the requested
* amount, but will return NULL immediately if the sender has sent nothing.
*
* It returns either NULL or an array of bytes `amount` long.
**/
char *nu_try_read_bytes(connection_t *conn, size_t amount);
/**
* Reads a block of bytes from the connection represented by conn.
* This function will block until it reads `amount` bytes, so it always
* returns `amount` bytes.
*/
char *nu_read_bytes(connection_t *conn, size_t amount);
/**
* TODO: write docs here
* This function is blocking.
**/
int nu_multiplex(connection_t *local, connection_t *remote, nu_callback on_local_write, nu_callback on_remote_write);
/**
* Closes a connection opened by nu_wait_client or nu_connect_server.
**/
void nu_close_connection(connection_t *conn);
#endif /* __NETWORK_UTIL_H */