From f1c0c1bba381637caec073c41eb46061f8fe7562 Mon Sep 17 00:00:00 2001 From: Love Hornquist Astrand Date: Wed, 25 Nov 2009 05:29:18 +0100 Subject: [PATCH] prefix SOCKET symbols with rk_ --- kdc/connect.c | 71 +++++++++++++++--------------- kdc/hpropd.c | 2 +- lib/kadm5/ipropd_master.c | 41 ++++++++--------- lib/kadm5/private.h | 2 +- lib/krb5/auth_context.c | 8 ++-- lib/krb5/context.c | 8 +--- lib/krb5/net_read.c | 17 +------ lib/krb5/net_write.c | 24 +++------- lib/krb5/send_to_kdc.c | 4 +- lib/krb5/store_fd.c | 4 +- lib/roken/NTMakefile | 2 +- lib/roken/getifaddrs-test.c | 4 +- lib/roken/mini_inetd.c | 36 +++++++-------- lib/roken/net_read.c | 10 +++-- lib/roken/net_write.c | 8 ++-- lib/roken/roken-common.h | 17 +++---- lib/roken/roken.h.in | 64 +++++++++++---------------- lib/roken/sendmsg.c | 88 ++++++++++++++++++++++++++++++++++++- lib/roken/sendmsg_w32.c | 86 ------------------------------------ lib/roken/socket.c | 27 ++++++------ lib/roken/test-mini_inetd.c | 59 ++++++++++++++----------- 21 files changed, 272 insertions(+), 310 deletions(-) delete mode 100644 lib/roken/sendmsg_w32.c diff --git a/kdc/connect.c b/kdc/connect.c index d55415e41..58126ecef 100644 --- a/kdc/connect.c +++ b/kdc/connect.c @@ -217,7 +217,7 @@ parse_ports(krb5_context context, */ struct descr { - SOCKET s; + krb5_socket_t s; int type; int port; unsigned char *buf; @@ -235,7 +235,7 @@ init_descr(struct descr *d) { memset(d, 0, sizeof(*d)); d->sa = (struct sockaddr *)&d->__ss; - d->s = INVALID_SOCKET; + d->s = rk_INVALID_SOCKET; } /* @@ -271,7 +271,7 @@ init_socket(krb5_context context, if (ret) { krb5_warn(context, ret, "krb5_addr2sockaddr"); closesocket(d->s); - d->s = INVALID_SOCKET; + d->s = rk_INVALID_SOCKET; return; } @@ -279,9 +279,9 @@ init_socket(krb5_context context, return; d->s = socket(family, type, 0); - if(IS_BAD_SOCKET(d->s)){ + if(rk_IS_BAD_SOCKET(d->s)){ krb5_warn(context, errno, "socket(%d, %d, 0)", family, type); - d->s = INVALID_SOCKET; + d->s = rk_INVALID_SOCKET; return; } #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR) @@ -293,24 +293,24 @@ init_socket(krb5_context context, d->type = type; d->port = port; - if(IS_SOCKET_ERROR(bind(d->s, sa, sa_size))){ + if(rk_IS_SOCKET_ERROR(bind(d->s, sa, sa_size))){ char a_str[256]; size_t len; krb5_print_address (a, a_str, sizeof(a_str), &len); krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port)); closesocket(d->s); - d->s = INVALID_SOCKET; + d->s = rk_INVALID_SOCKET; return; } - if(type == SOCK_STREAM && IS_SOCKET_ERROR(listen(d->s, SOMAXCONN))){ + if(type == SOCK_STREAM && rk_IS_SOCKET_ERROR(listen(d->s, SOMAXCONN))){ char a_str[256]; size_t len; krb5_print_address (a, a_str, sizeof(a_str), &len); krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port)); closesocket(d->s); - d->s = INVALID_SOCKET; + d->s = rk_INVALID_SOCKET; return; } } @@ -348,7 +348,7 @@ init_sockets(krb5_context context, for (j = 0; j < addresses.len; ++j) { init_socket(context, config, &d[num], &addresses.val[j], ports[i].family, ports[i].type, ports[i].port); - if(d[num].s != INVALID_SOCKET){ + if(d[num].s != rk_INVALID_SOCKET){ char a_str[80]; size_t len; @@ -423,15 +423,16 @@ send_reply(krb5_context context, l[1] = (reply->length >> 16) & 0xff; l[2] = (reply->length >> 8) & 0xff; l[3] = reply->length & 0xff; - if(IS_SOCKET_ERROR(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len))) { + if(rk_IS_SOCKET_ERROR(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len))) { kdc_log (context, config, - 0, "sendto(%s): %s", d->addr_string, strerror(SOCK_ERRNO)); + 0, "sendto(%s): %s", d->addr_string, + strerror(rk_SOCK_ERRNO)); return; } } - if(IS_SOCKET_ERROR(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len))) { - kdc_log (context, config, - 0, "sendto(%s): %s", d->addr_string, strerror(SOCK_ERRNO)); + if(rk_IS_SOCKET_ERROR(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len))) { + kdc_log (context, config, 0, "sendto(%s): %s", d->addr_string, + strerror(rk_SOCK_ERRNO)); return; } } @@ -489,8 +490,8 @@ handle_udp(krb5_context context, d->sock_len = sizeof(d->__ss); n = recvfrom(d->s, buf, max_request, 0, d->sa, &d->sock_len); - if(IS_SOCKET_ERROR(n)) - krb5_warn(context, SOCK_ERRNO, "recvfrom"); + if(rk_IS_SOCKET_ERROR(n)) + krb5_warn(context, rk_SOCK_ERRNO, "recvfrom"); else { addr_to_string (context, d->sa, d->sock_len, d->addr_string, sizeof(d->addr_string)); @@ -523,9 +524,9 @@ clear_descr(struct descr *d) if(d->buf) memset(d->buf, 0, d->size); d->len = 0; - if(d->s != INVALID_SOCKET) + if(d->s != rk_INVALID_SOCKET) closesocket(d->s); - d->s = INVALID_SOCKET; + d->s = rk_INVALID_SOCKET; } @@ -559,15 +560,15 @@ add_new_tcp (krb5_context context, krb5_kdc_configuration *config, struct descr *d, int parent, int child) { - SOCKET s; + krb5_socket_t s; if (child == -1) return; d[child].sock_len = sizeof(d[child].__ss); s = accept(d[parent].s, d[child].sa, &d[child].sock_len); - if(IS_BAD_SOCKET(s)) { - krb5_warn(context, SOCK_ERRNO, "accept"); + if(rk_IS_BAD_SOCKET(s)) { + krb5_warn(context, rk_SOCK_ERRNO, "accept"); return; } @@ -720,14 +721,14 @@ handle_http_tcp (krb5_context context, kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string); kdc_log(context, config, 5, "HTTP request: %s", t); free(data); - if (IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) { + if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) { kdc_log(context, config, 0, "HTTP write failed: %s: %s", - d->addr_string, strerror(SOCK_ERRNO)); + d->addr_string, strerror(rk_SOCK_ERRNO)); return -1; } - if (IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) { + if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) { kdc_log(context, config, 0, "HTTP write failed: %s: %s", - d->addr_string, strerror(SOCK_ERRNO)); + d->addr_string, strerror(rk_SOCK_ERRNO)); return -1; } return -1; @@ -740,16 +741,16 @@ handle_http_tcp (krb5_context context, "Pragma: no-cache\r\n" "Content-type: application/octet-stream\r\n" "Content-transfer-encoding: binary\r\n\r\n"; - if (IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) { + if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) { free(data); kdc_log(context, config, 0, "HTTP write failed: %s: %s", - d->addr_string, strerror(SOCK_ERRNO)); + d->addr_string, strerror(rk_SOCK_ERRNO)); return -1; } - if (IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) { + if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) { free(data); kdc_log(context, config, 0, "HTTP write failed: %s: %s", - d->addr_string, strerror(SOCK_ERRNO)); + d->addr_string, strerror(rk_SOCK_ERRNO)); return -1; } } @@ -780,8 +781,8 @@ handle_tcp(krb5_context context, } n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL); - if(IS_SOCKET_ERROR(n)){ - krb5_warn(context, SOCK_ERRNO, "recvfrom failed from %s to %s/%d", + if(rk_IS_SOCKET_ERROR(n)){ + krb5_warn(context, rk_SOCK_ERRNO, "recvfrom failed from %s to %s/%d", d[idx].addr_string, descr_type(d + idx), ntohs(d[idx].port)); return; @@ -867,7 +868,7 @@ loop(krb5_context context, FD_ZERO(&fds); for(i = 0; i < ndescr; i++) { - if(!IS_BAD_SOCKET(d[i].s)){ + if(!rk_IS_BAD_SOCKET(d[i].s)){ if(d[i].type == SOCK_STREAM && d[i].timeout && d[i].timeout < time(NULL)) { kdc_log(context, config, 1, @@ -909,11 +910,11 @@ loop(krb5_context context, break; case -1: if (errno != EINTR) - krb5_warn(context, SOCK_ERRNO, "select"); + krb5_warn(context, rk_SOCK_ERRNO, "select"); break; default: for(i = 0; i < ndescr; i++) - if(!IS_BAD_SOCKET(d[i].s) && FD_ISSET(d[i].s, &fds)) { + if(!rk_IS_BAD_SOCKET(d[i].s) && FD_ISSET(d[i].s, &fds)) { if(d[i].type == SOCK_DGRAM) handle_udp(context, config, &d[i]); else if(d[i].type == SOCK_STREAM) diff --git a/kdc/hpropd.c b/kdc/hpropd.c index 1f86257db..2fbcbc4a4 100644 --- a/kdc/hpropd.c +++ b/kdc/hpropd.c @@ -78,7 +78,7 @@ main(int argc, char **argv) krb5_principal c1, c2; krb5_authenticator authent; krb5_keytab keytab; - SOCKET sock = INVALID_SOCKET; + krb5_socket_t sock = rk_INVALID_SOCKET; int close_socket = 0; HDB *db = NULL; int optidx = 0; diff --git a/lib/kadm5/ipropd_master.c b/lib/kadm5/ipropd_master.c index e3d574d0f..17d19a8f5 100644 --- a/lib/kadm5/ipropd_master.c +++ b/lib/kadm5/ipropd_master.c @@ -45,13 +45,13 @@ static int time_before_gone; const char *master_hostname; -static SOCKET +static krb5_socket_t make_signal_socket (krb5_context context) { #ifndef NO_UNIX_SOCKETS struct sockaddr_un addr; const char *fn; - SOCKET fd; + krb5_socket_t fd; fn = kadm5_log_signal_socket(context); @@ -67,30 +67,30 @@ make_signal_socket (krb5_context context) return fd; #else struct addrinfo *ai = NULL; - SOCKET fd; + krb5_socket_t fd; kadm5_log_signal_socket_info(context, 1, &ai); fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (IS_BAD_SOCKET(fd)) - krb5_err (context, 1, SOCK_ERRNO, "socket AF=%d", ai->ai_family); + if (rk_IS_BAD_SOCKET(fd)) + krb5_err (context, 1, rk_SOCK_ERRNO, "socket AF=%d", ai->ai_family); - if (IS_SOCKET_ERROR( bind (fd, ai->ai_addr, ai->ai_addrlen) )) - krb5_err (context, 1, SOCK_ERRNO, "bind"); + if (rk_IS_SOCKET_ERROR( bind (fd, ai->ai_addr, ai->ai_addrlen) )) + krb5_err (context, 1, rk_SOCK_ERRNO, "bind"); return fd; #endif } -static SOCKET +static krb5_socket_t make_listen_socket (krb5_context context, const char *port_str) { - SOCKET fd; + krb5_socket_t fd; int one = 1; struct sockaddr_in addr; fd = socket (AF_INET, SOCK_STREAM, 0); - if (IS_BAD_SOCKET(fd)) - krb5_err (context, 1, SOCK_ERRNO, "socket AF_INET"); + if (rk_IS_BAD_SOCKET(fd)) + krb5_err (context, 1, rk_SOCK_ERRNO, "socket AF_INET"); setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one)); memset (&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; @@ -120,7 +120,7 @@ make_listen_socket (krb5_context context, const char *port_str) } struct slave { - SOCKET fd; + krb5_socket_t fd; struct sockaddr_in addr; char *name; krb5_auth_context ac; @@ -195,9 +195,9 @@ slave_dead(krb5_context context, slave *s) { krb5_warnx(context, "slave %s dead", s->name); - if (!IS_BAD_SOCKET(s->fd)) { + if (!rk_IS_BAD_SOCKET(s->fd)) { closesocket (s->fd); - s->fd = INVALID_SOCKET; + s->fd = rk_INVALID_SOCKET; } s->flags |= SLAVE_F_DEAD; slave_seen(s); @@ -208,7 +208,7 @@ remove_slave (krb5_context context, slave *s, slave **root) { slave **p; - if (!IS_BAD_SOCKET(s->fd)) + if (!rk_IS_BAD_SOCKET(s->fd)) closesocket (s->fd); if (s->name) free (s->name); @@ -224,7 +224,8 @@ remove_slave (krb5_context context, slave *s, slave **root) } static void -add_slave (krb5_context context, krb5_keytab keytab, slave **root, SOCKET fd) +add_slave (krb5_context context, krb5_keytab keytab, slave **root, + krb5_socket_t fd) { krb5_principal server; krb5_error_code ret; @@ -243,8 +244,8 @@ add_slave (krb5_context context, krb5_keytab keytab, slave **root, SOCKET fd) addr_len = sizeof(s->addr); s->fd = accept (fd, (struct sockaddr *)&s->addr, &addr_len); - if (IS_BAD_SOCKET(s->fd)) { - krb5_warn (context, errno, "accept"); + if (rk_IS_BAD_SOCKET(s->fd)) { + krb5_warn (context, rk_SOCK_ERRNO, "accept"); goto error; } if (master_hostname) @@ -309,7 +310,7 @@ error: struct prop_context { krb5_auth_context auth_context; - SOCKET fd; + krb5_socket_t fd; }; static int @@ -759,7 +760,7 @@ main(int argc, char **argv) void *kadm_handle; kadm5_server_context *server_context; kadm5_config_params conf; - SOCKET signal_fd, listen_fd; + krb5_socket_t signal_fd, listen_fd; int log_fd; slave *slaves = NULL; uint32_t current_version = 0, old_version = 0; diff --git a/lib/kadm5/private.h b/lib/kadm5/private.h index c1fec6feb..7c5b27f17 100644 --- a/lib/kadm5/private.h +++ b/lib/kadm5/private.h @@ -79,7 +79,7 @@ typedef struct kadm5_log_context { #else struct addrinfo *socket_info; #endif - SOCKET socket_fd; + krb5_socket_t socket_fd; } kadm5_log_context; typedef struct kadm5_server_context { diff --git a/lib/krb5/auth_context.c b/lib/krb5/auth_context.c index db24e73a4..db1833b4b 100644 --- a/lib/krb5/auth_context.c +++ b/lib/krb5/auth_context.c @@ -170,9 +170,9 @@ krb5_auth_con_genaddrs(krb5_context context, if(flags & KRB5_AUTH_CONTEXT_GENERATE_LOCAL_ADDR) { if (auth_context->local_address == NULL) { len = sizeof(ss_local); - if(IS_SOCKET_ERROR(getsockname(fd, local, &len))) { + if(rk_IS_SOCKET_ERROR(getsockname(fd, local, &len))) { char buf[128]; - ret = SOCK_ERRNO; + ret = rk_SOCK_ERRNO; strerror_r(ret, buf, sizeof(buf)); krb5_set_error_message(context, ret, "getsockname: %s", buf); goto out; @@ -188,9 +188,9 @@ krb5_auth_con_genaddrs(krb5_context context, } if(flags & KRB5_AUTH_CONTEXT_GENERATE_REMOTE_ADDR) { len = sizeof(ss_remote); - if(IS_SOCKET_ERROR(getpeername(fd, remote, &len))) { + if(rk_IS_SOCKET_ERROR(getpeername(fd, remote, &len))) { char buf[128]; - ret = SOCK_ERRNO; + ret = rk_SOCK_ERRNO; strerror_r(ret, buf, sizeof(buf)); krb5_set_error_message(context, ret, "getpeername: %s", buf); goto out; diff --git a/lib/krb5/context.c b/lib/krb5/context.c index 529247e76..d3dcb3064 100644 --- a/lib/krb5/context.c +++ b/lib/krb5/context.c @@ -363,10 +363,8 @@ krb5_init_context(krb5_context *context) if (ret) goto out; #endif -#ifdef NEED_SOCK_INIT - if (SOCK_INIT) + if (SOCK_INIT()) p->flags |= KRB5_CTX_F_SOCKETS_INITIALIZED; -#endif out: if(ret) { @@ -535,11 +533,9 @@ krb5_free_context(krb5_context context) HEIMDAL_MUTEX_destroy(context->mutex); free(context->mutex); -#ifdef NEED_SOCK_INIT if (context->flags & KRB5_CTX_F_SOCKETS_INITIALIZED) { - SOCK_EXIT; + SOCK_EXIT(); } -#endif memset(context, 0, sizeof(*context)); free(context); diff --git a/lib/krb5/net_read.c b/lib/krb5/net_read.c index 6e3023335..f6d781c27 100644 --- a/lib/krb5/net_read.c +++ b/lib/krb5/net_read.c @@ -40,20 +40,5 @@ krb5_net_read (krb5_context context, size_t len) { krb5_socket_t fd = *((krb5_socket_t *)p_fd); - -#ifdef SOCKET_IS_NOT_AN_FD -#ifdef _MSC_VER - { - HANDLE h = _get_osfhandle(fd); - - if (h != INVALID_HANDLE_VALUE) { - return net_read (fd, buf, len); - } - } -#else -#error Dont know how to handle socket that may be an fd -#endif -#endif - - return net_read_s (fd, buf, len); + return net_read(fd, buf, len); } diff --git a/lib/krb5/net_write.c b/lib/krb5/net_write.c index 19d2a005d..82f699374 100644 --- a/lib/krb5/net_write.c +++ b/lib/krb5/net_write.c @@ -40,24 +40,10 @@ krb5_net_write (krb5_context context, size_t len) { krb5_socket_t fd = *((krb5_socket_t *)p_fd); - -#ifdef SOCKET_IS_NOT_AN_FD -#ifdef _MSC_VER - { - HANDLE h = _get_osfhandle(fd); - - if (h != INVALID_HANDLE_VALUE) { - return net_write (fd, buf, len); - } - } -#else -#error Dont know how to handle SOCKET that may be an fd -#endif -#endif - - return net_write_s (fd, buf, len); + return net_write(fd, buf, len); } +KRB5_DEPRECATED KRB5_LIB_FUNCTION krb5_ssize_t KRB5_LIB_CALL krb5_net_write_block(krb5_context context, void *p_fd, @@ -85,8 +71,8 @@ krb5_net_write_block(krb5_context context, tvp = NULL; ret = select(fd + 1, NULL, &wfds, NULL, tvp); - if (IS_SOCKET_ERROR(ret)) { - if (SOCK_ERRNO == EINTR) + if (rk_IS_SOCKET_ERROR(ret)) { + if (rk_SOCK_ERRNO == EINTR) continue; return -1; } @@ -99,7 +85,7 @@ krb5_net_write_block(krb5_context context, count = send (fd, cbuf, rem, 0); - if (IS_SOCKET_ERROR(count)) { + if (rk_IS_SOCKET_ERROR(count)) { return -1; } diff --git a/lib/krb5/send_to_kdc.c b/lib/krb5/send_to_kdc.c index 3723d98a6..c539babb8 100644 --- a/lib/krb5/send_to_kdc.c +++ b/lib/krb5/send_to_kdc.c @@ -266,7 +266,7 @@ send_via_proxy (krb5_context context, struct addrinfo hints; struct addrinfo *ai, *a; int ret; - krb5_socket_t s = INVALID_SOCKET; + krb5_socket_t s = rk_INVALID_SOCKET; char portstr[NI_MAXSERV]; if (proxy == NULL) @@ -416,7 +416,7 @@ krb5_sendto (krb5_context context, for (a = ai; a != NULL; a = a->ai_next) { fd = socket (a->ai_family, a->ai_socktype | SOCK_CLOEXEC, a->ai_protocol); - if (IS_BAD_SOCKET(fd)) + if (rk_IS_BAD_SOCKET(fd)) continue; rk_cloexec(fd); if (connect (fd, a->ai_addr, a->ai_addrlen) < 0) { diff --git a/lib/krb5/store_fd.c b/lib/krb5/store_fd.c index bd357dbe3..bb261be2d 100644 --- a/lib/krb5/store_fd.c +++ b/lib/krb5/store_fd.c @@ -91,7 +91,7 @@ krb5_storage_from_fd(krb5_socket_t fd_in) krb5_storage *sp; int fd; -#ifdef SOCKET_IS_NOT_AN_FD +#ifdef _WIN32 #ifdef _MSC_VER if (_get_osfhandle(fd_in) != -1) { fd = dup(fd_in); @@ -101,7 +101,7 @@ krb5_storage_from_fd(krb5_socket_t fd_in) #else #error Dont know how to deal with fd that may or may not be a socket. #endif -#else /* SOCKET_IS_NOT_AN_FD */ +#else fd = dup(fd_in); #endif diff --git a/lib/roken/NTMakefile b/lib/roken/NTMakefile index f6d2e30dc..05f01382e 100644 --- a/lib/roken/NTMakefile +++ b/lib/roken/NTMakefile @@ -79,7 +79,7 @@ libroken_la_OBJS = \ $(OBJ)\resolve.obj \ $(OBJ)\roken_gethostby.obj \ $(OBJ)\rtbl.obj \ - $(OBJ)\sendmsg_w32.obj \ + $(OBJ)\sendmsg.obj \ $(OBJ)\setenv.obj \ $(OBJ)\setprogname.obj \ $(OBJ)\simple_exec_w32.obj \ diff --git a/lib/roken/getifaddrs-test.c b/lib/roken/getifaddrs-test.c index 2762e4552..34141a2d3 100644 --- a/lib/roken/getifaddrs-test.c +++ b/lib/roken/getifaddrs-test.c @@ -80,7 +80,7 @@ main(int argc, char **argv) int ret; if (SOCK_INIT) - errx(1, "Couldn't initialize sockets. Err=%d\n", SOCK_ERRNO); + errx(1, "Couldn't initialize sockets. Err=%d\n", rk_SOCK_ERRNO); ret = getifaddrs(&addrs); if (ret != 0) @@ -95,7 +95,7 @@ main(int argc, char **argv) freeifaddrs(addrs); if (SOCK_EXIT) - errx(1, "Couldn't uninitialize sockets. Err=%d\n", SOCK_ERRNO); + errx(1, "Couldn't uninitialize sockets. Err=%d\n", rk_SOCK_ERRNO); return 0; } diff --git a/lib/roken/mini_inetd.c b/lib/roken/mini_inetd.c index 7f9b61681..1cf3815d1 100644 --- a/lib/roken/mini_inetd.c +++ b/lib/roken/mini_inetd.c @@ -41,12 +41,12 @@ */ static void -accept_it (SOCKET s, SOCKET *ret_socket) +accept_it (rk_socket_t s, rk_socket_t *ret_socket) { - SOCKET as; + rk_socket_t as; as = accept(s, NULL, NULL); - if(IS_BAD_SOCKET(as)) + if(rk_IS_BAD_SOCKET(as)) err (1, "accept"); if (ret_socket) { @@ -54,20 +54,16 @@ accept_it (SOCKET s, SOCKET *ret_socket) *ret_socket = as; } else { - int fd = fd_from_socket(as, 0); + int fd = socket_to_fd(as, 0); - /* We would use _O_RDONLY for the fd_from_socket() call for + /* We would use _O_RDONLY for the socket_to_fd() call for STDIN, but there are instances where we assume that STDIN is a r/w socket. */ dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); -#ifdef SOCKET_IS_NOT_AN_FD - close(fd); -#else - closesocket(as); -#endif + closesocket(fd); } } @@ -90,14 +86,14 @@ accept_it (SOCKET s, SOCKET *ret_socket) * @see mini_inetd() */ ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -mini_inetd_addrinfo (struct addrinfo *ai, SOCKET *ret_socket) +mini_inetd_addrinfo (struct addrinfo *ai, rk_socket_t *ret_socket) { int ret; struct addrinfo *a; int n, nalloc, i; - SOCKET *fds; + rk_socket_t *fds; fd_set orig_read_set, read_set; - SOCKET max_fd = (SOCKET)-1; + rk_socket_t max_fd = (rk_socket_t)-1; for (nalloc = 0, a = ai; a != NULL; a = a->ai_next) ++nalloc; @@ -112,20 +108,20 @@ mini_inetd_addrinfo (struct addrinfo *ai, SOCKET *ret_socket) for (i = 0, a = ai; a != NULL; a = a->ai_next) { fds[i] = socket (a->ai_family, a->ai_socktype, a->ai_protocol); - if (IS_BAD_SOCKET(fds[i])) + if (rk_IS_BAD_SOCKET(fds[i])) continue; socket_set_reuseaddr (fds[i], 1); socket_set_ipv6only(fds[i], 1); - if (IS_SOCKET_ERROR(bind (fds[i], a->ai_addr, a->ai_addrlen))) { + if (rk_IS_SOCKET_ERROR(bind (fds[i], a->ai_addr, a->ai_addrlen))) { warn ("bind af = %d", a->ai_family); closesocket(fds[i]); - fds[i] = INVALID_SOCKET; + fds[i] = rk_INVALID_SOCKET; continue; } - if (IS_SOCKET_ERROR(listen (fds[i], SOMAXCONN))) { + if (rk_IS_SOCKET_ERROR(listen (fds[i], SOMAXCONN))) { warn ("listen af = %d", a->ai_family); closesocket(fds[i]); - fds[i] = INVALID_SOCKET; + fds[i] = rk_INVALID_SOCKET; continue; } #ifndef NO_LIMIT_FD_SETSIZE @@ -144,7 +140,7 @@ mini_inetd_addrinfo (struct addrinfo *ai, SOCKET *ret_socket) read_set = orig_read_set; ret = select (max_fd + 1, &read_set, NULL, NULL, NULL); - if (IS_SOCKET_ERROR(ret) && SOCK_ERRNO != EINTR) + if (rk_IS_SOCKET_ERROR(ret) && rk_SOCK_ERRNO != EINTR) err (1, "select"); } while (ret <= 0); @@ -177,7 +173,7 @@ mini_inetd_addrinfo (struct addrinfo *ai, SOCKET *ret_socket) * @see mini_inetd_addrinfo() */ ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -mini_inetd (int port, SOCKET * ret_socket) +mini_inetd (int port, rk_socket_t * ret_socket) { int error; struct addrinfo *ai, hints; diff --git a/lib/roken/net_read.c b/lib/roken/net_read.c index 1010d8b8e..b57dda3dd 100644 --- a/lib/roken/net_read.c +++ b/lib/roken/net_read.c @@ -39,8 +39,10 @@ * Like read but never return partial data. */ +#ifndef _WIN32 + ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL -net_read (int fd, void *buf, size_t nbytes) +net_read (rk_socket_t fd, void *buf, size_t nbytes) { char *cbuf = (char *)buf; ssize_t count; @@ -62,10 +64,10 @@ net_read (int fd, void *buf, size_t nbytes) return nbytes; } -#ifdef SOCKET_IS_NOT_AN_FD +#else ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL -net_read_s (SOCKET sock, void *buf, size_t nbytes) +net_read(rk_socket_t sock, void *buf, size_t nbytes) { char *cbuf = (char *)buf; ssize_t count; @@ -80,7 +82,7 @@ net_read_s (SOCKET sock, void *buf, size_t nbytes) WSACancelBlockingCall(). */ #ifndef HAVE_WINSOCK - if (SOCK_ERRNO == EINTR) + if (rk_SOCK_ERRNO == EINTR) continue; #endif return count; diff --git a/lib/roken/net_write.c b/lib/roken/net_write.c index a12c06a49..94c9df1c3 100644 --- a/lib/roken/net_write.c +++ b/lib/roken/net_write.c @@ -39,8 +39,10 @@ * Like write but never return partial data. */ +#ifndef _WIN32 + ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL -net_write (int fd, const void *buf, size_t nbytes) +net_write (rk_socket_t fd, const void *buf, size_t nbytes) { const char *cbuf = (const char *)buf; ssize_t count; @@ -60,10 +62,10 @@ net_write (int fd, const void *buf, size_t nbytes) return nbytes; } -#ifdef SOCKET_IS_NOT_AN_FD +#else ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL -net_write_s (SOCKET sock, const void *buf, size_t nbytes) +net_write(rk_socket_t sock, const void *buf, size_t nbytes) { const char *cbuf = (const char *)buf; ssize_t count; diff --git a/lib/roken/roken-common.h b/lib/roken/roken-common.h index 918254158..bdaedfa84 100644 --- a/lib/roken/roken-common.h +++ b/lib/roken/roken-common.h @@ -392,30 +392,27 @@ socket_set_port (struct sockaddr *, int); #define socket_set_portrange rk_socket_set_portrange ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_portrange (SOCKET, int, int); +socket_set_portrange (rk_socket_t, int, int); #define socket_set_debug rk_socket_set_debug ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_debug (SOCKET); +socket_set_debug (rk_socket_t); #define socket_set_tos rk_socket_set_tos ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_tos (SOCKET, int); +socket_set_tos (rk_socket_t, int); #define socket_set_reuseaddr rk_socket_set_reuseaddr ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_reuseaddr (SOCKET, int); +socket_set_reuseaddr (rk_socket_t, int); #define socket_set_ipv6only rk_socket_set_ipv6only ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_ipv6only (SOCKET, int); +socket_set_ipv6only (rk_socket_t, int); -#ifdef SOCKET_IS_NOT_AN_FD +#define socket_to_fd rk_socket_to_fd ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL -fd_from_socket(SOCKET, int); -#else -#define fd_from_socket(s,f) (s) -#endif +socket_to_fd(rk_socket_t, int); #define vstrcollect rk_vstrcollect ROKEN_LIB_FUNCTION char ** ROKEN_LIB_CALL diff --git a/lib/roken/roken.h.in b/lib/roken/roken.h.in index 5dd6652ac..9b7773b3f 100644 --- a/lib/roken/roken.h.in +++ b/lib/roken/roken.h.in @@ -56,36 +56,36 @@ #include -#define IS_BAD_SOCKET(s) ((s) == INVALID_SOCKET) -#define IS_SOCKET_ERROR(rv) ((rv) == SOCKET_ERROR) -#define SOCK_ERRNO WSAGetLastError() -#define SOCK_IOCTL(s,c,a) ioctlsocket((s),(c),(a)) +typedef SOCKET rk_socket_t; + +#define rk_IS_BAD_SOCKET(s) ((s) == INVALID_SOCKET) +#define rk_IS_SOCKET_ERROR(rv) ((rv) == SOCKET_ERROR) +#define rk_SOCK_ERRNO WSAGetLastError() +#define rk_SOCK_IOCTL(s,c,a) ioctlsocket((s),(c),(a)) #define ETIMEDOUT WSAETIMEDOUT #define EWOULDBLOCK WSAEWOULDBLOCK #define ENOTSOCK WSAENOTSOCK -#define SOCK_INIT rk_WSAStartup() -#define SOCK_EXIT rk_WSACleanup() -#define NEED_SOCK_INIT 1 +#define rk_SOCK_INIT rk_WSAStartup() +#define rk_SOCK_EXIT rk_WSACleanup() ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL rk_WSAStartup(void); ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL rk_WSACleanup(void); #else /* not WinSock */ -typedef int SOCKET; +typedef int rk_socket_t; -#define closesocket(x) close(x) -#define SOCK_IOCTL(s,c,a) ioctl((s),(c),(a)) -#define IS_BAD_SOCKET(s) ((s) < 0) -#define IS_SOCKET_ERROR(rv) ((rv) < 0) -#define SOCK_ERRNO errno -#define INVALID_SOCKET (-1) +#define rk_closesocket(x) close(x) +#define rk_SOCK_IOCTL(s,c,a) ioctl((s),(c),(a)) +#define rk_IS_BAD_SOCKET(s) ((s) < 0) +#define rk_IS_SOCKET_ERROR(rv) ((rv) < 0) +#define rk_SOCK_ERRNO errno +#define rk_INVALID_SOCKET (-1) -#define SOCK_INIT (0) -#define SOCK_EXIT (0) -#undef NEED_SOCK_INIT +#define rk_SOCK_INIT 0 +#define rk_SOCK_EXIT 0 #endif @@ -654,24 +654,14 @@ ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL roken_vconcat (char *, size_t, va_list); ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL roken_vmconcat (char **, size_t, va_list); -ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL net_write (int, const void *, size_t); +ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL + net_write (rk_socket_t, const void *, size_t); -ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL net_read (int, void *, size_t); +ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL + net_read (rk_socket_t, void *, size_t); -#ifdef SOCKET_IS_NOT_AN_FD - -ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL net_write_s (SOCKET, const void *, size_t); - -ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL net_read_s (SOCKET, void *, size_t); - -#else - -#define net_read_s net_read -#define net_write_s net_write - -#endif - -ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL issuid(void); +ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL + issuid(void); #ifndef HAVE_STRUCT_WINSIZE struct winsize { @@ -833,9 +823,7 @@ struct msghdr { }; ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL -sendmsg_w32(SOCKET s, const struct msghdr * msg, int flags); - -#define sendmsg(s,m,f) sendmsg_w32((s),(m),(f)) +sendmsg(rk_socket_t s, const struct msghdr * msg, int flags); #endif @@ -935,10 +923,10 @@ extern const char *__progname; #endif ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -mini_inetd_addrinfo (struct addrinfo*, SOCKET *); +mini_inetd_addrinfo (struct addrinfo*, rk_socket *); ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -mini_inetd (int, SOCKET *); +mini_inetd (int, rk_socket_t *); #ifndef HAVE_LOCALTIME_R #define localtime_r rk_localtime_r diff --git a/lib/roken/sendmsg.c b/lib/roken/sendmsg.c index d2e8b00ff..bbe5c303f 100644 --- a/lib/roken/sendmsg.c +++ b/lib/roken/sendmsg.c @@ -35,8 +35,10 @@ #include "roken.h" +#ifndef _WIN32 + ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL -sendmsg(SOCKET s, const struct msghdr *msg, int flags) +sendmsg(rk_socket_t s, const struct msghdr *msg, int flags) { ssize_t ret; size_t tot = 0; @@ -60,3 +62,87 @@ sendmsg(SOCKET s, const struct msghdr *msg, int flags) free (buf); return ret; } + +#else /* _WIN32 */ + +/*********************************************************************** + * Copyright (c) 2009, Secure Endpoints Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * - Neither the name of Secure Endpoints Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + **********************************************************************/ + +/* + * Implementation of sendmsg() for WIN32 + * + * We are using a contrived definition of msghdr which actually uses + * an array of ::_WSABUF structures instead of ::iovec . This allows + * us to call WSASend directly using the given ::msghdr instead of + * having to allocate another array of ::_WSABUF and copying data for + * each call. + * + * Limitations: + * + * - msg->msg_name is ignored. So is msg->control. + * - WSASend() only supports ::MSG_DONTROUTE, ::MSG_OOB and + * ::MSG_PARTIAL. + * + * @param[in] s The socket to use. + * @param[in] msg The message + * @param[in] flags Flags. A combination of ::MSG_DONTROUTE, + * ::MSG_OOB and ::MSG_PARTIAL + * + * @return The number of bytes sent, on success. Or -1 on error. + */ +ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL +sendmsg_w32(rk_socket_t s, const struct msghdr * msg, int flags) +{ + int srv; + DWORD num_bytes_sent = 0; + + /* TODO: For _WIN32_WINNT >= 0x0600 we can use WSASendMsg using + WSAMSG which is a much more direct analogue to sendmsg(). */ + + srv = WSASend(s, msg->msg_iov, msg->msg_iovlen, + &num_bytes_sent, flags, NULL, NULL); + + if (srv == 0) + return (int) num_bytes_sent; + + /* srv == SOCKET_ERROR and WSAGetLastError() == WSA_IO_PENDING + indicates that a non-blocking transfer has been scheduled. + We'll have to check for that if we ever support non-blocking + I/O. */ + + return -1; +} + +#endif /* !_WIN32 */ diff --git a/lib/roken/sendmsg_w32.c b/lib/roken/sendmsg_w32.c deleted file mode 100644 index 19edf6931..000000000 --- a/lib/roken/sendmsg_w32.c +++ /dev/null @@ -1,86 +0,0 @@ -/*********************************************************************** - * Copyright (c) 2009, Secure Endpoints Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * - Neither the name of Secure Endpoints Inc. nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * - **********************************************************************/ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include - -/** - * Implementation of sendmsg() for WIN32 - * - * We are using a contrived definition of msghdr which actually uses - * an array of ::_WSABUF structures instead of ::iovec . This allows - * us to call WSASend directly using the given ::msghdr instead of - * having to allocate another array of ::_WSABUF and copying data for - * each call. - * - * Limitations: - * - * - msg->msg_name is ignored. So is msg->control. - * - WSASend() only supports ::MSG_DONTROUTE, ::MSG_OOB and - * ::MSG_PARTIAL. - * - * @param[in] s The socket to use. - * @param[in] msg The message - * @param[in] flags Flags. A combination of ::MSG_DONTROUTE, - * ::MSG_OOB and ::MSG_PARTIAL - * - * @return The number of bytes sent, on success. Or -1 on error. - */ -ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL -sendmsg_w32(SOCKET s, const struct msghdr * msg, int flags) -{ - int srv; - DWORD num_bytes_sent = 0; - - /* TODO: For _WIN32_WINNT >= 0x0600 we can use WSASendMsg using - WSAMSG which is a much more direct analogue to sendmsg(). */ - - srv = WSASend(s, msg->msg_iov, msg->msg_iovlen, - &num_bytes_sent, flags, NULL, NULL); - - if (srv == 0) - return (int) num_bytes_sent; - - /* srv == SOCKET_ERROR and WSAGetLastError() == WSA_IO_PENDING - indicates that a non-blocking transfer has been scheduled. - We'll have to check for that if we ever support non-blocking - I/O. */ - - return -1; -} - diff --git a/lib/roken/socket.c b/lib/roken/socket.c index aa7e76ca3..24b5242ba 100644 --- a/lib/roken/socket.c +++ b/lib/roken/socket.c @@ -222,7 +222,7 @@ socket_set_port (struct sockaddr *sa, int port) * Set the range of ports to use when binding with port = 0. */ ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_portrange (SOCKET sock, int restr, int af) +socket_set_portrange (rk_socket_t sock, int restr, int af) { #if defined(IP_PORTRANGE) if (af == AF_INET) { @@ -248,12 +248,12 @@ socket_set_portrange (SOCKET sock, int restr, int af) */ ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_debug (SOCKET sock) +socket_set_debug (rk_socket_t sock) { #if defined(SO_DEBUG) && defined(HAVE_SETSOCKOPT) int on = 1; - if (setsockopt (sock, SOL_SOCKET, SO_DEBUG, (void *) &on, sizeof (on)) < 0) + if (setsockopt (sock, SOL_rk_socket_t, SO_DEBUG, (void *) &on, sizeof (on)) < 0) warn ("setsockopt SO_DEBUG (ignored)"); #endif } @@ -263,7 +263,7 @@ socket_set_debug (SOCKET sock) */ ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_tos (SOCKET sock, int tos) +socket_set_tos (rk_socket_t sock, int tos) { #if defined(IP_TOS) && defined(HAVE_SETSOCKOPT) if (setsockopt (sock, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof (int)) < 0) @@ -277,10 +277,10 @@ socket_set_tos (SOCKET sock, int tos) */ ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_reuseaddr (SOCKET sock, int val) +socket_set_reuseaddr (rk_socket_t sock, int val) { #if defined(SO_REUSEADDR) && defined(HAVE_SETSOCKOPT) - if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&val, + if(setsockopt(sock, SOL_rk_socket_t, SO_REUSEADDR, (void *)&val, sizeof(val)) < 0) err (1, "setsockopt SO_REUSEADDR"); #endif @@ -291,28 +291,27 @@ socket_set_reuseaddr (SOCKET sock, int val) */ ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL -socket_set_ipv6only (SOCKET sock, int val) +socket_set_ipv6only (rk_socket_t sock, int val) { #if defined(IPV6_V6ONLY) && defined(HAVE_SETSOCKOPT) setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&val, sizeof(val)); #endif } - -#ifdef SOCKET_IS_NOT_AN_FD - /** * Create a file descriptor from a socket * * While the socket handle in \a sock can be used with WinSock - * functions after calling fd_from_socket(), it should not be closed + * functions after calling socket_to_fd(), it should not be closed * with closesocket(). The socket will be closed when the associated * file descriptor is closed. */ ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL -fd_from_socket(SOCKET sock, int flags) +socket_to_fd(rk_socket_t sock, int flags) { +#ifndef _WIN32 + return sock; +#else return _open_osfhandle((intptr_t) sock, flags); -} - #endif +} diff --git a/lib/roken/test-mini_inetd.c b/lib/roken/test-mini_inetd.c index 0aee4933a..fa486834b 100644 --- a/lib/roken/test-mini_inetd.c +++ b/lib/roken/test-mini_inetd.c @@ -55,31 +55,30 @@ get_address(int flags, struct addrinfo ** ret) ai.ai_protocol = PF_UNSPEC; rv = getaddrinfo("127.0.0.1", PORT_S, &ai, ret); - if (rv) { - fprintf(stderr, "[%s] getaddrinfo: %s", prog, gai_strerror(rv)); - } + if (rv) + warnx("getaddrinfo: %s", gai_strerror(rv)); return rv; } static int -get_connected_socket(SOCKET * s_ret) +get_connected_socket(rk_socket_t * s_ret) { struct addrinfo * ai = NULL; int rv = 0; - SOCKET s = INVALID_SOCKET; + rk_socket_t s = rk_INVALID_SOCKET; rv = get_address(0, &ai); if (rv) return rv; s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (IS_BAD_SOCKET(s)) { + if (rk_IS_BAD_SOCKET(s)) { rv = 1; goto done; } rv = connect(s, ai->ai_addr, ai->ai_addrlen); - if (IS_SOCKET_ERROR(rv)) + if (rk_IS_SOCKET_ERROR(rv)) goto done; *s_ret = s; @@ -87,13 +86,13 @@ get_connected_socket(SOCKET * s_ret) rv = 0; done: - if (!IS_BAD_SOCKET(s)) + if (!rk_IS_BAD_SOCKET(s)) closesocket(s); if (ai) freeaddrinfo(ai); - return (rv)?SOCK_ERRNO: 0; + return (rv) ? rk_SOCK_ERRNO : 0; } const char * test_strings[] = { @@ -106,31 +105,34 @@ const char * test_strings[] = { static int test_simple_echo_client(void) { - SOCKET s = INVALID_SOCKET; + rk_socket_t s = INVALID_SOCKET; int rv; char buf[81]; int i; - fprintf (stderr, "[%s] Getting connected socket...", prog); + fprintf(stderr, "[%s] Getting connected socket...", getprogname()); rv = get_connected_socket(&s); if (rv) { - fprintf(stderr, "\n[%s] get_connected_socket() failed (%s)\n", prog, strerror(SOCK_ERRNO)); + fprintf(stderr, "\n[%s] get_connected_socket() failed (%s)\n", + getprogname(), strerror(rk_SOCK_ERRNO)); return 1; } - fprintf (stderr, "[%s] done\n", prog); + fprintf(stderr, "[%s] done\n", getprogname()); for (i=0; i < sizeof(test_strings)/sizeof(test_strings[0]); i++) { rv = send(s, test_strings[i], strlen(test_strings[i]), 0); - if (IS_SOCKET_ERROR(rv)) { - fprintf (stderr, "[%s] send() failure (%s)\n", prog, strerror(SOCK_ERRNO)); + if (rk_IS_SOCKET_ERROR(rv)) { + fprintf(stderr, "[%s] send() failure (%s)\n", + getprogname(), strerror(rk_SOCK_ERRNO)); closesocket(s); return 1; } rv = recv(s, buf, sizeof(buf), 0); - if (IS_SOCKET_ERROR(rv)) { - fprintf (stderr, "[%s] recv() failure (%s)\n", prog, strerror(SOCK_ERRNO)); + if (rk_IS_SOCKET_ERROR(rv)) { + fprintf (stderr, "[%s] recv() failure (%s)\n", + getprogname(), strerror(rk_SOCK_ERRNO)); closesocket(s); return 1; } @@ -163,12 +165,13 @@ test_simple_echo_socket(void) return test_simple_echo_client(); } else { - SOCKET s = INVALID_SOCKET; + rk_socket_t s = INVALID_SOCKET; fprintf (stderr, "[%s] Listening for connections...\n", prog); mini_inetd(htons(PORT), &s); - if (IS_BAD_SOCKET(s)) { - fprintf (stderr, "[%s] Connect failed (%s)\n", prog, strerror(SOCK_ERRNO)); + if (rk_IS_BAD_SOCKET(s)) { + fprintf (stderr, "[%s] Connect failed (%s)\n", + getprogname(), strerror(rk_SOCK_ERRNO)); } else { fprintf (stderr, "[%s] Connected\n", prog); } @@ -177,17 +180,19 @@ test_simple_echo_socket(void) char buf[81]; int rv, srv; - while ((rv = recv(s, buf, sizeof(buf), 0)) != 0 && !IS_SOCKET_ERROR(rv)) { + while ((rv = recv(s, buf, sizeof(buf), 0)) != 0 && !rk_IS_SOCKET_ERROR(rv)) { buf[rv] = 0; fprintf(stderr, "[%s] Received [%s]\n", prog, buf); /* simple echo */ srv = send(s, buf, rv, 0); if (srv != rv) { - if (IS_SOCKET_ERROR(srv)) - fprintf(stderr, "[%s] send() error [%s]\n", prog, strerror(SOCK_ERRNO)); + if (rk_IS_SOCKET_ERROR(srv)) + fprintf(stderr, "[%s] send() error [%s]\n", + getprogname(), strerror(rk_SOCK_ERRNO)); else - fprintf(stderr, "[%s] send() size mismatch %d != %d", prog, srv, rv); + fprintf(stderr, "[%s] send() size mismatch %d != %d", + getprogname(), srv, rv); } if (!strcmp(buf, "exit")) { @@ -198,7 +203,9 @@ test_simple_echo_socket(void) } } - fprintf(stderr, "[%s] recv() failed (%s)\n", prog, strerror(SOCK_ERRNO)); + fprintf(stderr, "[%s] recv() failed (%s)\n", + getprogname(), + strerror(rk_SOCK_ERRNO)); } closesocket(s); @@ -346,6 +353,8 @@ do_test(char * path) int main(int argc, char ** argv) { + setprogname(argv[0]); + if (argc == 2 && strcmp(argv[1], "--client") == 0) return do_client(); else if (argc == 2 && strcmp(argv[1], "--server") == 0)