1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,58 @@ |
1 |
+/* |
|
2 |
+ * socklib.h |
|
3 |
+ * |
|
4 |
+ * Handy functions for IPv4 socket sonnections. |
|
5 |
+ * |
|
6 |
+ * Limitations: |
|
7 |
+ * These are for the simplest case, as the recommended approach |
|
8 |
+ * is to integrate the sock_connect() timeout inside the main |
|
9 |
+ * loop as to don't be blocking. |
|
10 |
+ * |
|
11 |
+ * Documentation used: |
|
12 |
+ * http://www.beej.us/guide/bgnet/output/html/multipage/getaddrinfoman.html |
|
13 |
+ * |
|
14 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
15 |
+ * This file is licensed under the terms of the GNU LGPL v2+ |
|
16 |
+ */ |
|
17 |
+ |
|
18 |
+#ifndef SOCKLIB_H |
|
19 |
+#define SOCKLIB_H |
|
20 |
+#include <sys/select.h> /* fd_set */ |
|
21 |
+ |
|
22 |
+typedef void sselect; |
|
23 |
+ |
|
24 |
+char *ipv4_genip(char *hostname, long *resulthostsize); |
|
25 |
+int ipv4_genport(char *portname, int fallback); |
|
26 |
+int ipv4_preconnect(char *host, long hostsize, int port); /* setup socket, set non-blocking, connect(2) call */ |
|
27 |
+int ipv4_connect(int fd, long timeoutmsec); /* tests writeability */ |
|
28 |
+int ipv4_postconnect(int fd); /* hopefully connect(2) suceeded, set blocking */ |
|
29 |
+int ipv4_server(int port); |
|
30 |
+int ipv4_serverbinded(char *host, long hostsize, int port); |
|
31 |
+int sock_accept(int fd); |
|
32 |
+int sock_getinfo(int fd, int *iplen, char *ip, int *port); /* ip must be at least 16 bytes to have room for an ipv6 address */ |
|
33 |
+int sock_queued(int fd); |
|
34 |
+int sock_setblocking(int fd, int block); |
|
35 |
+int sock_readable(int fd); |
|
36 |
+ |
|
37 |
+sselect *sselect_init(void); |
|
38 |
+void sselect_free(sselect *ssel); |
|
39 |
+int sselect_reset(sselect *ssel); |
|
40 |
+int sselect_addread(sselect *ssel, int fd, void *userptr); |
|
41 |
+int sselect_addwrite(sselect *ssel, int fd, void *userptr); |
|
42 |
+int sselect_delread(sselect *ssel, int fd); |
|
43 |
+int sselect_delwrite(sselect *ssel, int fd); |
|
44 |
+int sselect_wait(sselect *ssel, int ms); |
|
45 |
+int sselect_getread(sselect *ssel, int *fds, int sizefds); |
|
46 |
+int sselect_getwrite(sselect *ssel, int *fds, int sizefds); |
|
47 |
+void *sselect_getuserptr(sselect *ssel, int fd); |
|
48 |
+ |
|
49 |
+/* advanced sselect functions */ |
|
50 |
+int sselect_getreadfiltered(sselect *ssel, fd_set *filter, int *fds, int sizefds); |
|
51 |
+int sselect_getwritefiltered(sselect *ssel, fd_set *filter, int *fds, int sizefds); |
|
52 |
+ |
|
53 |
+/* socket configuration functions */ |
|
54 |
+void sock_setfast(int fd); /* mark as low-bandwidth interactive stream */ |
|
55 |
+void sock_setunsafe(int fd); /* for server sockets, configure reuseaddress, linger: only suitable for intranet usage */ |
|
56 |
+void sock_setsafe(int fd); /* for non-server sockets, configure reuseaddress, linger: make them suitable for internet usage */ |
|
57 |
+ |
|
58 |
+#endif |