diff options
Diffstat (limited to 'other/reverb/network.h')
| -rw-r--r-- | other/reverb/network.h | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/other/reverb/network.h b/other/reverb/network.h new file mode 100644 index 0000000..8dc816d --- /dev/null +++ b/other/reverb/network.h | |||
| @@ -0,0 +1,140 @@ | |||
| 1 | /* scut's leet network library ;) | ||
| 2 | * 1999 (c) scut | ||
| 3 | * | ||
| 4 | * networking code | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <sys/socket.h> | ||
| 8 | #include <net/if.h> | ||
| 9 | #include <netinet/in.h> | ||
| 10 | #include <stdio.h> | ||
| 11 | |||
| 12 | #ifndef SCUT_NETWORK_H | ||
| 13 | #define SCUT_NETWORK_H | ||
| 14 | |||
| 15 | #define NET_READTIMEOUT 180 | ||
| 16 | #define NET_CONNTIMEOUT 60 | ||
| 17 | |||
| 18 | |||
| 19 | typedef struct bound { | ||
| 20 | int bs; /* bound socket */ | ||
| 21 | unsigned short port; /* port we bound to */ | ||
| 22 | struct sockaddr bsa; /* bs_in */ | ||
| 23 | } bound; | ||
| 24 | |||
| 25 | extern int net_readtimeout; | ||
| 26 | extern int net_conntimeout; | ||
| 27 | |||
| 28 | |||
| 29 | /* net_parseip | ||
| 30 | * | ||
| 31 | * reads an ip in the format "1.1.1.1:299" or "blabla:481" into | ||
| 32 | * the char pointer *ip and into the port *port | ||
| 33 | * | ||
| 34 | * returns 0 on failure | ||
| 35 | * returns 1 on success | ||
| 36 | */ | ||
| 37 | int net_parseip (char *inp, char **ip, unsigned short int *port); | ||
| 38 | |||
| 39 | |||
| 40 | /* net_accept | ||
| 41 | * accepts a connection from socket s, and stores the connection | ||
| 42 | * into cs. | ||
| 43 | * wait a maximum amount of maxsec seconds for connections | ||
| 44 | * maxsec can also be zero (infinite wait, until connection) | ||
| 45 | * | ||
| 46 | * returns 0 if no connection has been made within maxsec seconds | ||
| 47 | * returns -1 if an error appears | ||
| 48 | * returns the socket number if a connection has been made | ||
| 49 | */ | ||
| 50 | |||
| 51 | int net_accept (int s, struct sockaddr_in *cs, int maxsec); | ||
| 52 | |||
| 53 | |||
| 54 | /* net_bind | ||
| 55 | * binds a socket to an ip:port on the local machine, | ||
| 56 | * ip can be either NULL (bind to all IP's on the host), or a pointer | ||
| 57 | * to a virtual host name, or a real IP, or "*" for any. | ||
| 58 | * port can be either 0 (ephemeral port), or any free port. | ||
| 59 | * | ||
| 60 | * returns NULL on failure | ||
| 61 | * pointer to bound structure on success | ||
| 62 | */ | ||
| 63 | bound *net_bind (char *ip, unsigned short int port); | ||
| 64 | void net_boundfree (bound *bf); | ||
| 65 | |||
| 66 | |||
| 67 | /* net_resolve | ||
| 68 | * resolves host into s_addr | ||
| 69 | */ | ||
| 70 | unsigned long int net_resolve(char *host); | ||
| 71 | |||
| 72 | |||
| 73 | /* net_connect | ||
| 74 | * connects to the given server and port with a max timeout of sec | ||
| 75 | * initializes the sockaddr_in struct correctly (ipv4), accepts any | ||
| 76 | * ip "123.123.123.123" or hostname "localhost", "www.yahoo.de" as hostname | ||
| 77 | * creates a new socket and returns either -1 if failed or | ||
| 78 | * the socket if connection has been established within the timeout limit | ||
| 79 | * routine is still IPv4 biased :-/ | ||
| 80 | * with sourceip/sourceport you MAY specify the source IP and source port | ||
| 81 | * to use for the connection, but you can set the ip or port to NULL/0, | ||
| 82 | * to choose the default IP and an ephemeral port. this was added later in | ||
| 83 | * this library, so please update your sources. | ||
| 84 | * | ||
| 85 | * returns -1 on failure | ||
| 86 | * returns socket if success | ||
| 87 | */ | ||
| 88 | int net_connect (struct sockaddr_in *cs, char *server, unsigned short int port, int sec); | ||
| 89 | |||
| 90 | |||
| 91 | /* net_rtimeout | ||
| 92 | * waits max <sec> seconds for fd to become readable | ||
| 93 | * returns -1 on error (errno set) | ||
| 94 | * returns 1 on readability | ||
| 95 | */ | ||
| 96 | int net_rtimeout(int fd, int sec); | ||
| 97 | |||
| 98 | |||
| 99 | /* net_rbuf | ||
| 100 | * allocates memory and reads to *dst until connection close | ||
| 101 | * returns n if success (n = number of bytes read) | ||
| 102 | * returns -1 if failed | ||
| 103 | */ | ||
| 104 | long int net_rbuf(int fd, char **dst); | ||
| 105 | #define NET_BSIZE 4096 | ||
| 106 | |||
| 107 | |||
| 108 | /* net_rbuft | ||
| 109 | * reads dsize bytes into dst from fd, with timeout | ||
| 110 | * returns 1 on success | ||
| 111 | * returns -1 on failure | ||
| 112 | */ | ||
| 113 | int net_rbuft(int fd, char *dst, unsigned long int dsize); | ||
| 114 | |||
| 115 | |||
| 116 | /* net_rlinet | ||
| 117 | * reads a line from socket descriptor with timeout to buffer | ||
| 118 | * if sec = 0, then only a continuous stream of data is required, not | ||
| 119 | * an overall timeout. | ||
| 120 | * returns -1 on timeout | ||
| 121 | * returns 0 on connection close | ||
| 122 | * returns length of readen line (including '\n') | ||
| 123 | */ | ||
| 124 | int net_rlinet(int fd, char *buf, int bufsize, int sec); | ||
| 125 | |||
| 126 | |||
| 127 | /* net_tline | ||
| 128 | * returns length if string contains '\n' | ||
| 129 | * returns -1 if no '\n' in string | ||
| 130 | */ | ||
| 131 | int net_tline(char *buf, int bufsize); | ||
| 132 | |||
| 133 | |||
| 134 | /* net_write | ||
| 135 | * prints a formatted string to a socket | ||
| 136 | */ | ||
| 137 | void net_write(int fd, const char *str, ...); | ||
| 138 | |||
| 139 | #endif | ||
| 140 | |||
