summaryrefslogtreecommitdiff
path: root/other/zylyx/src/network.h
diff options
context:
space:
mode:
Diffstat (limited to 'other/zylyx/src/network.h')
-rw-r--r--other/zylyx/src/network.h342
1 files changed, 342 insertions, 0 deletions
diff --git a/other/zylyx/src/network.h b/other/zylyx/src/network.h
new file mode 100644
index 0000000..46a8b54
--- /dev/null
+++ b/other/zylyx/src/network.h
@@ -0,0 +1,342 @@
1/* scut's leet network library ;)
2 * 1999 (c) scut
3 *
4 * networking code
5 */
6
7#ifndef SCUT_NETWORK_H
8#define SCUT_NETWORK_H
9
10#include <sys/socket.h>
11#include <net/if.h>
12#include <netinet/in.h>
13#include <stdio.h>
14
15#define NET_READTIMEOUT 180
16#define NET_CONNTIMEOUT 60
17#define NET_IDENTTIMEOUT 15
18
19#define IFI_NAME 16
20#define IFI_HADDR 8
21
22/* struct ifi_info
23 *
24 * a linked list giving information about all the network interfaces available
25 * a pointer to this struct list is returned by net_get_ifi.
26 */
27
28struct ifi_info {
29 char ifi_name[IFI_NAME];
30 u_char ifi_haddr[IFI_HADDR];
31 u_short ifi_hlen;
32 short ifi_flags;
33 short ifi_myflags;
34 struct sockaddr *ifi_addr;
35 struct in_addr ifi_saddr;
36 struct ifi_info *ifi_next;
37};
38
39#define IFI_ALIAS 1
40
41typedef struct bound {
42 int bs; /* bound socket */
43 unsigned short port; /* port we bound to */
44 struct sockaddr bsa; /* bs_in */
45} bound;
46
47extern int net_readtimeout;
48extern int net_conntimeout;
49extern int net_identtimeout;
50
51
52/* net_socks_connect
53 *
54 * relays through an open socks 5 server (NO AUTH type)
55 * returns a socket descriptor which is already connected
56 */
57
58int net_socks_connect (char *socks, unsigned short int sport,
59 char *server, unsigned short int port, int sec);
60
61
62/* net_socks_put_s5info
63 *
64 * insert socks 5 compatible relay information into socket s5s,
65 * used to relay over more then just one socks server
66 */
67
68int net_socks_put_s5info (int s5s, char *server,
69 unsigned short int port, int sec);
70
71
72/* net_parseip
73 *
74 * read an ip in the format "1.1.1.1:299" or "blabla:481" into
75 * the char pointer *ip and into the port *port
76 *
77 * return 0 on failure
78 * return 1 on success
79 */
80
81int net_parseip (char *inp, char **ip, unsigned short int *port);
82
83
84/* net_getlocalip
85 *
86 * give back the main IP of the local machine
87 *
88 * return the local IP address as string on success
89 * return NULL on failure
90 */
91
92char *net_getlocalip (void);
93
94
95/* net_peername
96 *
97 * return a pointer that points to an alloced character array that contains
98 * the fully qualified hostname for the remote host that is connected using
99 * the socket descriptor `socket'.
100 +
101 * return NULL on failure
102 * return pointer to hostname or quad-dotted IP address
103 */
104
105char *net_peername (int socket);
106
107
108/* net_descriptify
109 *
110 * descriptify a socket `socket' ;)
111 *
112 * return -1 on failure
113 * return file descriptor on success
114 */
115
116FILE *net_descriptify (int socket);
117
118
119/* net_ident
120 *
121 * ident a connection identified by the host:port pairs on both sides,
122 * returning the ident in *ident
123 *
124 * return 1 on success
125 * return -1 on failure
126 */
127
128int net_ident (char **ident, struct sockaddr_in *locals, unsigned short int localport,
129 struct sockaddr_in *remotes, unsigned short int remoteport);
130
131
132/* net_accept
133 *
134 * accept a connection from socket s, and stores the connection
135 * into cs.
136 * wait a maximum amount of maxsec seconds for connections
137 * maxsec can also be zero (infinite wait, until connection)
138 *
139 * return 0 if no connection has been made within maxsec seconds
140 * return -1 if an error appears
141 * return the socket number if a connection has been made
142 */
143
144int net_accept (int s, struct sockaddr_in *cs, int maxsec);
145
146
147/* net_get_ifi
148 *
149 * get network interface information
150 *
151 * return NULL on failure
152 * return a pointer to a linked list structure ifi_info (see above)
153 */
154
155struct ifi_info *net_ifi_get (int family, int doaliases);
156
157
158/* net_ifi_free
159 *
160 * free the linked list associated with `tf'.
161 *
162 * return in any case
163 */
164
165void net_ifi_free (struct ifi_info *tf);
166
167
168/* net_testvip
169 *
170 * test if virtual ip/hostname is available for use on the local machine,
171 *
172 * return 1 if the ip can be used
173 * return 0 if the ip/host is not available
174 */
175
176int net_testvip (char *ip);
177
178
179/* net_bind
180 *
181 * bind a socket to an ip:port on the local machine,
182 * `ip' can be either NULL (bind to all IP's on the host), or a pointer
183 * to a virtual host name, or a real IP, or "*" for any.
184 * `port' can be either 0 (ephemeral port), or any free port.
185 *
186 * return NULL on failure
187 * return pointer to bound structure on success
188 */
189
190bound *net_bind (char *ip, unsigned short int port);
191
192
193/* net_boundfree
194 *
195 * free the bound structure pointed to by `bf'
196 *
197 * return in any case
198 */
199
200void net_boundfree (bound *bf);
201
202
203/* net_resolve
204 *
205 * resolve a hostname pointed to by `host' into a s_addr return value
206 *
207 * return the correct formatted `s_addr' for this host on success
208 * return 0 on failure
209 */
210
211unsigned long int net_resolve (char *host);
212
213
214/* net_assignaddr
215 *
216 * assign an IP address and port to a socket
217 * sourceip can be an IP or hostname that is available on the local host,
218 * or NULL/"*" to let the kernel choose one, same applies to sourceport,
219 * it can either be zero (ephemeral port choosen by the kernel) or a
220 * valid port number
221 *
222 * return 1 on success
223 * return 0 on failure
224 */
225
226int net_assignaddr (int sd, char *sourceip, unsigned short int sourceport);
227
228
229/* net_connect
230 *
231 * connect to the given `server' and `port' with a max timeout of `sec'.
232 * initialize the sockaddr_in struct `cs' correctly (ipv4), accept any
233 * ip "123.123.123.123" or hostname "localhost", "www.yahoo.de" as hostname.
234 * create a new socket and return either -1 if failed or
235 * the connected socket if connection has been established within the
236 * timeout limit.
237 *
238 * the routine is still IPv4 biased :-/
239 * with `sourceip'/`sourceportÄ you MAY specify the source IP and source port
240 * to use for the connection, but you can set the ip or port to NULL/0,
241 * to choose the default IP and an ephemeral port. this was added later in
242 * this library, so please update your sources.
243 *
244 * return -1 on failure
245 * return socket if success
246 */
247
248int net_connect (struct sockaddr_in *cs, char *server, unsigned short int port, char *sourceip,
249 unsigned short int sourceport, int sec);
250
251
252/* net_rtimeout
253 *
254 * waits max `sec' seconds for fd to become readable
255 *
256 * return -1 on error (errno set)
257 * return 1 on readability
258 */
259
260int net_rtimeout (int fd, int sec);
261
262
263/* net_rbuf
264 *
265 * allocate memory and read socket data to `dst' until the connection
266 * gets closed.
267 *
268 * return n if success (n = number of bytes read)
269 * return -1 if failed
270 */
271
272long int net_rbuf (int fd, char **dst);
273#define NET_BSIZE 4096 /* blocksize for pre-allocation */
274
275
276/* net_rbuft
277 *
278 * read `dsize' bytes into `dst' from `fd', with timeout
279 *
280 * return 1 on success
281 * return -1 on failure
282 */
283int net_rbuft (int fd, char *dst, unsigned long int dsize);
284
285
286/* net_rlinet
287 *
288 * read a line from socket descriptor with timeout to buffer
289 * if sec = 0, then only a continuous stream of data is required, not
290 * an overall timeout.
291 *
292 * return -1 on timeout
293 * return 0 on connection close
294 * return length of readen line (including '\n')
295 *
296 * net_rlineta
297 * same as net_rlinet, but allocs the memory itself
298 */
299
300int net_rlineta (int fd, char **buf, int sec);
301int net_rlinet (int fd, char *buf, int bufsize, int sec);
302
303
304/* net_tline
305 *
306 * return length if string `buf' with a maximum length of `bufsize'
307 * contains '\n'
308 *
309 * return -1 if no '\n' in string
310 */
311
312int net_tline (char *buf, int bufsize);
313
314
315/* net_write
316 *
317 * print a formatted string to a socket, see syntax of printf
318 *
319 * return in any case
320 */
321
322void net_write (int fd, const char *str, ...);
323
324
325/* net_printip
326 *
327 * print an IP address stored in the struct in_addr pointed to by `ia' to a
328 * string `str' with a maximum length of `len'.
329 *
330 * return 0 on success
331 *Üreturn 1 on failure
332 *
333 * net_printipa behaves the same way, except it allocates memory and let
334 * `*str' point to the string
335 */
336
337int net_printip (struct in_addr *ia, char *str, size_t len);
338int net_printipa (struct in_addr *ia, char **str);
339
340
341#endif
342