# HG changeset patch # User David Demelier # Date 1377548586 -7200 # Node ID 29531c2f8213284f62cc362ebc01aa5fb8d86cfe # Parent a89ff602b30fa58109c0ad08d7537d738e86c405 Update nsock diff -r a89ff602b30f -r 29531c2f8213 nsock.c --- a/nsock.c Thu Aug 22 17:40:54 2013 +0200 +++ b/nsock.c Mon Aug 26 22:23:06 2013 +0200 @@ -19,9 +19,28 @@ # include # include -#if !defined(_WIN32) +#if defined(_WIN32) + + typedef SOCKET nsock_socket_t; + typedef const char * nsock_carg_t; + typedef char * nsock_arg_t; + +#else # include # include + +# define ioctlsocket(s) ioctl(s) +# define closesocket(s) close(s) + +# define gai_strerrorA gai_strerror + +# define INVALID_SOCKET -1 +# define SOCKET_ERROR -1 + + typedef int nsock_socket_t; + typedef const void * nsock_carg_t; + typedef void * nsock_arg_t; + #endif #include "nsock.h" @@ -95,7 +114,7 @@ char error[128]; }; -struct nsock_ep { +struct nsock_address { struct sockaddr_storage addr; socklen_t addrlen; char error[128]; @@ -174,19 +193,25 @@ } struct nsock * -nsock_create(int domain, int type, int protocol) +nsock_new(void) { struct nsock *ns; if ((ns = malloc(sizeof (struct nsock))) == NULL) return NULL; + return ns; +} + +int +nsock_create(struct nsock *ns, int domain, int type, int protocol) +{ ns->fd = socket(domain, type, protocol); if (ns->fd == INVALID_SOCKET) nsock_set_error(ns->error, sizeof (ns->error)); - return ns; + return ns->fd == INVALID_SOCKET ? -1 : 0; } const char * @@ -199,7 +224,7 @@ } int -nsock_bind(struct nsock *ns, const struct nsock_ep *ep) +nsock_bind(struct nsock *ns, const struct nsock_address *ep) { int ret; @@ -225,7 +250,7 @@ } int -nsock_accept(struct nsock *ns, struct nsock **client, struct nsock_ep **clientinfo) +nsock_accept(struct nsock *ns, struct nsock **client, struct nsock_address **clientinfo) { struct sockaddr_storage *st = NULL; socklen_t *len = NULL; @@ -237,7 +262,7 @@ } if (clientinfo != NULL) { - if ((*clientinfo = malloc(sizeof (struct nsock_ep))) == NULL) { + if ((*clientinfo = malloc(sizeof (struct nsock_address))) == NULL) { free(client); nsock_set_errno(ns->error, sizeof (ns->error), ENOMEM); return -1; @@ -274,7 +299,7 @@ } int -nsock_connect(struct nsock *ns, const struct nsock_ep *ep) +nsock_connect(struct nsock *ns, const struct nsock_address *ep) { int ret; @@ -313,7 +338,7 @@ } long -nsock_recvfrom(struct nsock *ns, struct nsock_ep *ep, void *data, size_t datasz, int flags) +nsock_recvfrom(struct nsock *ns, struct nsock_address *ep, void *data, size_t datasz, int flags) { struct sockaddr_storage *st = NULL; socklen_t *len = NULL; @@ -350,7 +375,7 @@ } long -nsock_sendto(struct nsock *ns, const struct nsock_ep *ep, const void *data, size_t datasz, int flags) +nsock_sendto(struct nsock *ns, const struct nsock_address *ep, const void *data, size_t datasz, int flags) { long nbsent; @@ -387,19 +412,19 @@ * End point functions * -------------------------------------------------------- */ -struct nsock_ep * -nsock_ep_create(void) +struct nsock_address * +nsock_addr_new(void) { - struct nsock_ep *ep; + struct nsock_address *ep; - if ((ep = calloc(1, sizeof (struct nsock_ep))) == NULL) + if ((ep = calloc(1, sizeof (struct nsock_address))) == NULL) return NULL; return ep; } const char * -nsock_ep_error(struct nsock_ep *ep) +nsock_addr_error(struct nsock_address *ep) { if (ep == NULL) return strerror(ENOMEM); @@ -408,7 +433,7 @@ } int -nsock_ep_bind_ip(struct nsock_ep *ep, const char *iface, unsigned port, int family) +nsock_addr_bind_ip(struct nsock_address *ep, const char *iface, unsigned port, int family) { if (family == AF_INET6) { struct sockaddr_in6 *ptr = (struct sockaddr_in6 *)&ep->addr; @@ -446,7 +471,7 @@ } int -nsock_ep_connect_ip(struct nsock_ep *ep, const char *host, unsigned port, int family) +nsock_addr_connect_ip(struct nsock_address *ep, const char *host, unsigned port, int family) { struct addrinfo hints, *res; char portstr[32]; @@ -477,7 +502,7 @@ #if !defined(_WIN32) void -nsock_ep_unix(struct nsock_ep *ep, const char *path) +nsock_addr_unix(struct nsock_address *ep, const char *path) { struct sockaddr_un *ptr= (struct sockaddr_un *)&ep->addr; @@ -493,19 +518,19 @@ #endif struct sockaddr * -nsock_ep_getaddr(struct nsock_ep *ep) +nsock_addr_getaddr(struct nsock_address *ep) { return (struct sockaddr *)&ep->addr; } socklen_t -nsock_ep_getaddrlen(const struct nsock_ep *ep) +nsock_addr_getaddrlen(const struct nsock_address *ep) { return ep->addrlen; } void -nsock_ep_free(struct nsock_ep *ep) +nsock_addr_free(struct nsock_address *ep) { free(ep); } @@ -515,7 +540,7 @@ * -------------------------------------------------------- */ struct nsock_listener * -nsock_lst_create(const struct nsock *ns) +nsock_lst_new(const struct nsock *ns) { struct nsock_listener *ls; @@ -616,6 +641,15 @@ } void +nsock_lst_map(struct nsock_listener *ls, nsock_lst_map_t map, void *data) +{ + const struct nsock_clt *clt; + + TAILQ_FOREACH(clt, &ls->head, link) + map(clt->sock, data); +} + +void nsock_lst_free(struct nsock_listener *ls) { struct nsock_clt *clt, *tmp; diff -r a89ff602b30f -r 29531c2f8213 nsock.h --- a/nsock.h Thu Aug 22 17:40:54 2013 +0200 +++ b/nsock.h Mon Aug 26 22:23:06 2013 +0200 @@ -19,42 +19,26 @@ #ifndef _NSOCK_H_ #define _NSOCK_H_ -#if defined(_WIN32) -# include -# include - -typedef SOCKET nsock_socket_t; -typedef const char * nsock_carg_t; -typedef char * nsock_arg_t; - -#else -# include -# include -# include - -# include +#if !defined(NSOCK_NOINCLUDES) +# if defined(_WIN32) +# include +# include +# else +# include +# include +# include -# include +# include -# include -# include - -# define ioctlsocket(s) ioctl(s) -# define closesocket(s) close(s) +# include -# define gai_strerrorA gai_strerror - -# define INVALID_SOCKET -1 -# define SOCKET_ERROR -1 - -typedef int nsock_socket_t; -typedef const void * nsock_carg_t; -typedef void * nsock_arg_t; - +# include +# include +# endif #endif struct nsock; -struct nsock_ep; +struct nsock_address; struct nsock_listener; /* -------------------------------------------------------- @@ -65,22 +49,25 @@ nsock_init(void); struct nsock * -nsock_create(int, int, int); +nsock_new(void); + +int +nsock_create(struct nsock *, int, int, int); const char * nsock_error(struct nsock *); int -nsock_bind(struct nsock *, const struct nsock_ep *); +nsock_bind(struct nsock *, const struct nsock_address *); int nsock_listen(struct nsock *, int); int -nsock_accept(struct nsock *, struct nsock **, struct nsock_ep **); +nsock_accept(struct nsock *, struct nsock **, struct nsock_address **); int -nsock_connect(struct nsock *, const struct nsock_ep *); +nsock_connect(struct nsock *, const struct nsock_address *); int nsock_set(struct nsock *, int, int, const void *, unsigned); @@ -89,13 +76,13 @@ nsock_recv(struct nsock *, void *, size_t, int); long -nsock_recvfrom(struct nsock *, struct nsock_ep *, void *, size_t, int); +nsock_recvfrom(struct nsock *, struct nsock_address *, void *, size_t, int); long nsock_send(struct nsock *, const void *, size_t, int); long -nsock_sendto(struct nsock *, const struct nsock_ep *, const void *, size_t, int); +nsock_sendto(struct nsock *, const struct nsock_address *, const void *, size_t, int); void nsock_close(struct nsock *); @@ -110,40 +97,42 @@ * End point functions * -------------------------------------------------------- */ -struct nsock_ep * -nsock_ep_create(void); +struct nsock_address * +nsock_addr_new(void); const char * -nsock_ep_error(struct nsock_ep *); +nsock_addr_error(struct nsock_address *); int -nsock_ep_bind_ip(struct nsock_ep *, const char *, unsigned, int); +nsock_addr_bind_ip(struct nsock_address *, const char *, unsigned, int); int -nsock_ep_connect_ip(struct nsock_ep *, const char *, unsigned, int); +nsock_addr_connect_ip(struct nsock_address *, const char *, unsigned, int); #if !defined(_WIN32) void -nsock_ep_unix(struct nsock_ep *, const char *); +nsock_addr_unix(struct nsock_address *, const char *); #endif struct sockaddr * -nsock_ep_getaddr(struct nsock_ep *); +nsock_addr_getaddr(struct nsock_address *); socklen_t -nsock_ep_getaddrlen(const struct nsock_ep *); +nsock_addr_getaddrlen(const struct nsock_address *); void -nsock_ep_free(struct nsock_ep *); +nsock_addr_free(struct nsock_address *); /* -------------------------------------------------------- * listener functions * -------------------------------------------------------- */ +typedef void (*nsock_lst_map_t)(const struct nsock *, void *); + struct nsock_listener * -nsock_lst_create(const struct nsock *); +nsock_lst_new(const struct nsock *); const char * nsock_lst_error(struct nsock_listener *); @@ -164,6 +153,9 @@ nsock_lst_select(struct nsock_listener *, long, long); void +nsock_lst_map(struct nsock_listener *, nsock_lst_map_t, void *); + +void nsock_lst_free(struct nsock_listener *); #endif /* !_NSOCK_H_ */