Browse code

API rework to accomodate ipv6 and epoll in the future

Dario Rodriguez authored on 27/05/2014 11:33:45
Showing 2 changed files
... ...
@@ -5,6 +5,7 @@
5 5
  *
6 6
  * History:
7 7
  *      28/01/2014 Creation
8
+ *      27/05/2014 New API.
8 9
  *
9 10
  * Author: Dario Rodriguez dario@softhome.net
10 11
  * This file is licensed under the terms of the GNU LGPL v2+
... ...
@@ -16,54 +17,33 @@
16 17
 #include <netinet/in.h>
17 18
 #include <sys/types.h>
18 19
 #include <sys/socket.h>
20
+#include <sys/select.h>
19 21
 
20 22
 #include "socklib.h"
21 23
 
22
-struct sockaddr *
23
-sock_genip(char *hostname, long *resulthostsize)
24
-{
25
-        struct addrinfo hints,*ai;
26
-        struct sockaddr_in *in;
27
-        char *host;
28
-        memset(&hints,0,sizeof(hints));
29
-        hints.ai_family=AF_UNSPEC;
30
-        if(getaddrinfo(hostname,NULL,&hints,&ai)!=0)
31
-                return(NULL);
32
-        in=(struct sockaddr_in *)ai->addr;
33
-        if((host=malloc(ai->ai_addrlen))==NULL) {
34
-                freeaddrinfo(ai),ai=NULL;
35
-                return(NULL);
36
-        }
24
+typedef struct _sselect {
25
+        int dummy
26
+} _sselect;
37 27
 
38
-        memcpy(host,ai->ai_addr);
39
-        *resulthostsize=ai->ai_addrlen;
40
-        freeaddrinfo(ai),ai=NULL;
41
-        return(host);
42
-}
43
-
44
-int
45
-sock_genport(char *portname, int fallback)
46
-{
47
-        struct addrinfo *ai;
48
-        int port;
49
-        if(getaddrinfo(NULL,portname,NULL,&ai)!=0) {
50
-                return(fallback);
51
-        }
52
-        port=ai->
53
-        *resulthostsize=ai->ai_addrlen;
54
-        freeaddrinfo(ai),ai=NULL;
55
-        return(host);
56
-}
57
-
58
-int
59
-sock_connect(char *host, long hostsize, int port)
60
-{
61
-}
62
-
63
-int sock_server(int port);
64
-int sock_serverbinded(char *host, long hostsize, int port);
65
-int sock_getinfo(int socket, char *ip, int *port);
66
-int sock_select(long timoutms, ...); /* int numfds, int *resfdreadgroup1, int *fdreadgroup1,...,-1,int numfds, int *resfdwritegroup1, int *fdwritegroup1,...,-1); */
28
+char *ipv4_genip(char *hostname, long *resulthostsize);
29
+int ipv4_genport(char *portname, int fallback);
30
+int ipv4_preconnect(char *host, long hostsize, int port); /* setup socket, set non-blocking, connect(2) call */
31
+int ipv4_connect(int fd); /* tests writeability */
32
+int ipv4_postconnect(int fd); /* hopefully connect(2) suceeded, set blocking */
33
+int ipv4_server(int port);
34
+int ipv4_serverbinded(char *host, long hostsize, int port);
35
+int sock_accept(int fd);
36
+int sock_getinfo(int socket, int *iplen, char *ip, int *port); /* ip must be at least 16 bytes to have room for an ipv6 address */
67 37
 int sock_queued(int socket);
68 38
 int sock_setblocking(int socket, int block);
69
-#endif
39
+
40
+sselect *sselect_init(void);
41
+int sselect_reset(sselect *ssel);
42
+int sselect_addread(sselect *ssel, int fd);
43
+int sselect_addwrite(sselect *ssel, int fd);
44
+int sselect_delread(sselect *ssel, int fd);
45
+int sselect_delwrite(sselect *ssel, int fd);
46
+int sselect_wait(sslect *ssel, int ms);
47
+int sselect_getread(sselect *ssel, int *fds, int sizefds);
48
+int sselect_getwrite(sselect *ssel, int *fds, int sizefds);
49
+
... ...
@@ -10,6 +10,12 @@
10 10
  *
11 11
  * History:
12 12
  *      27/01/2014 Creation
13
+ *       3/04/2014 Add async-connect functions, add fd_set to select
14
+ *      27/05/2014 Rethought the API; separate ipv4 from general functions,
15
+ *                 rework select so that epoll usage is possible.
16
+ *
17
+ * Documentation used:
18
+ *   http://www.beej.us/guide/bgnet/output/html/multipage/getaddrinfoman.html
13 19
  *
14 20
  * Author: Dario Rodriguez dario@softhome.net
15 21
  * This file is licensed under the terms of the GNU LGPL v2+
... ...
@@ -18,13 +24,27 @@
18 24
 #ifndef SOCKLIB_H
19 25
 #define SOCKLIB_H
20 26
 
21
-char *sock_genip(char *hostname, long *resulthostsize);
22
-int sock_genport(char *portname, int fallback);
23
-int sock_connect(char *host, long hostsize, int port);
24
-int sock_server(int port);
25
-int sock_serverbinded(char *host, long hostsize, int port);
26
-int sock_getinfo(int socket, char *ip, int *port);
27
-int sock_select(long timoutms, ...); /* int numfds, int *resfdreadgroup1, int *fdreadgroup1,...,-1,int numfds, int *resfdwritegroup1, int *fdwritegroup1,...,-1); */
27
+typedef sselect void;
28
+
29
+char *ipv4_genip(char *hostname, long *resulthostsize);
30
+int ipv4_genport(char *portname, int fallback);
31
+int ipv4_preconnect(char *host, long hostsize, int port); /* setup socket, set non-blocking, connect(2) call */
32
+int ipv4_connect(int fd); /* tests writeability */
33
+int ipv4_postconnect(int fd); /* hopefully connect(2) suceeded, set blocking */
34
+int ipv4_server(int port);
35
+int ipv4_serverbinded(char *host, long hostsize, int port);
36
+int sock_accept(int fd);
37
+int sock_getinfo(int socket, int *iplen, char *ip, int *port); /* ip must be at least 16 bytes to have room for an ipv6 address */
28 38
 int sock_queued(int socket);
29 39
 int sock_setblocking(int socket, int block);
40
+
41
+sselect *sselect_init(void);
42
+int sselect_reset(sselect *ssel);
43
+int sselect_addread(sselect *ssel, int fd);
44
+int sselect_addwrite(sselect *ssel, int fd);
45
+int sselect_delread(sselect *ssel, int fd);
46
+int sselect_delwrite(sselect *ssel, int fd);
47
+int sselect_wait(sslect *ssel, int ms);
48
+int sselect_getread(sselect *ssel, int *fds, int sizefds);
49
+int sselect_getwrite(sselect *ssel, int *fds, int sizefds);
30 50
 #endif