From f01e3800fb38dcbe4fdb43d39a87547ee7a1a427 Mon Sep 17 00:00:00 2001 From: david Date: Mon, 12 Nov 2012 20:44:37 +0000 Subject: [PATCH] Implementation of UNIX-domain sockets for Nsock. Implementation of UNIX-domain sockets functionality for Nsock. Also some minor necessary changes to existing Nsock functions, to work properly with UNIX-domain sockets. --- nsock/include/nsock.h | 28 ++++++++++ nsock/include/nsock_config.h.in | 3 + nsock/src/configure | 2 +- nsock/src/configure.ac | 2 +- nsock/src/netutils.c | 35 ++++++++++++ nsock/src/netutils.h | 25 +++++++++ nsock/src/nsock_connect.c | 98 ++++++++++++++++++++++++++++----- nsock/src/nsock_core.c | 31 ++++++----- nsock/src/nsock_internal.h | 8 ++- nsock/src/nsock_read.c | 15 +++-- nsock/src/nsock_write.c | 22 +++++--- 11 files changed, 221 insertions(+), 48 deletions(-) diff --git a/nsock/include/nsock.h b/nsock/include/nsock.h index 134810e35..330a8ba08 100644 --- a/nsock/include/nsock.h +++ b/nsock/include/nsock.h @@ -76,6 +76,16 @@ #include #endif +#if HAVE_SYS_UN_H +#include + +#ifndef SUN_LEN +#include +# define SUN_LEN(ptr) (sizeof(*(ptr)) - sizeof((ptr)->sun_path)) \ + + strlen ((ptr)->sun_path)) +#endif +#endif /* HAVE_SYS_UN_H */ + #ifdef __cplusplus extern "C" { #endif @@ -423,6 +433,24 @@ typedef void (*nsock_ev_handler)(nsock_pool, nsock_event, void *); /* Initialize an unconnected UDP socket. */ int nsock_setup_udp(nsock_pool nsp, nsock_iod ms_iod, int af); +#if HAVE_SYS_UN_H + +/* Request a UNIX domain sockets connection to the same system (by path to socket). + * This function connects to the socket of type SOCK_STREAM. ss should be a + * sockaddr_storage, sockaddr_un as appropriate (just like what you would pass to + * connect). sslen should be the sizeof the structure you are passing in. */ +nsock_event_id nsock_connect_unixsock_stream(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, + int timeout_msecs, void *userdata, struct sockaddr *ss, + size_t sslen); + +/* Request a UNIX domain sockets connection to the same system (by path to socket). + * This function connects to the socket of type SOCK_DGRAM. ss should be a + * sockaddr_storage, sockaddr_un as appropriate (just like what you would pass to + * connect). sslen should be the sizeof the structure you are passing in. */ +nsock_event_id nsock_connect_unixsock_datagram(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, + void *userdata, struct sockaddr *ss, size_t sslen); +#endif /* HAVE_SYS_UN_H */ + /* Request a TCP connection to another system (by IP address). The in_addr is * normal network byte order, but the port number should be given in HOST BYTE * ORDER. ss should be a sockaddr_storage, sockaddr_in6, or sockaddr_in as diff --git a/nsock/include/nsock_config.h.in b/nsock/include/nsock_config.h.in index 09ec194ee..1283f4a6b 100644 --- a/nsock/include/nsock_config.h.in +++ b/nsock/include/nsock_config.h.in @@ -75,6 +75,9 @@ #undef HAVE_NET_BPF_H #undef HAVE_SYS_IOCTL_H +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_UN_H + #undef HAVE_SSL_SET_TLSEXT_HOST_NAME #undef HAVE_EPOLL diff --git a/nsock/src/configure b/nsock/src/configure index 8d8016ce5..cfbd4f549 100755 --- a/nsock/src/configure +++ b/nsock/src/configure @@ -4533,7 +4533,7 @@ $as_echo "#define STDC_HEADERS 1" >>confdefs.h fi -for ac_header in net/bpf.h sys/ioctl.h +for ac_header in net/bpf.h sys/ioctl.h sys/un.h do : as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default" diff --git a/nsock/src/configure.ac b/nsock/src/configure.ac index 6cd72fce1..a883cadb9 100644 --- a/nsock/src/configure.ac +++ b/nsock/src/configure.ac @@ -140,7 +140,7 @@ AC_CHECK_FUNC(nanosleep, , AC_CHECK_LIB(posix4, nanosleep)) dnl Checks for header files. AC_HEADER_STDC -AC_CHECK_HEADERS(net/bpf.h sys/ioctl.h) +AC_CHECK_HEADERS(net/bpf.h sys/ioctl.h sys/un.h) # We test whether they specified openssl desires explicitly use_openssl="yes" diff --git a/nsock/src/netutils.c b/nsock/src/netutils.c index 3324fb35c..d94f06464 100644 --- a/nsock/src/netutils.c +++ b/nsock/src/netutils.c @@ -83,6 +83,9 @@ #if HAVE_FCNTL_H #include #endif +#if HAVE_SYS_UN_H +#include +#endif static int netutils_debugging = 0; @@ -134,3 +137,35 @@ int maximize_fdlimit(void) { return 0; } +#if HAVE_SYS_UN_H +/* Get the UNIX domain socket path or empty string if the address family != AF_UNIX. */ +const char *get_unixsock_path(struct sockaddr_storage *addr) +{ + if (!addr || addr->ss_family != AF_UNIX) + return ""; + + struct sockaddr_un *su = (struct sockaddr_un *)addr; + return (const char *)su->sun_path; +} +#endif + +/* Get the peer/host address string. + * In case we have support for UNIX domain sockets, function returns + * string containing path to UNIX socket if the address family is AF_UNIX, + * otherwise it returns string containing "
:". */ +char *get_hostaddr_string(struct sockaddr_storage *addr, size_t len, unsigned short port) +{ + static char buffer[PEER_STR_LEN]; + + if (!addr) + return ""; + +#if HAVE_SYS_UN_H + if (addr->ss_family == AF_UNIX) + sprintf(buffer, "%s", get_unixsock_path(addr)); + else +#endif + sprintf(buffer, "%s:%hu", inet_ntop_ez(addr, len), port); + + return buffer; +} diff --git a/nsock/src/netutils.h b/nsock/src/netutils.h index 87e1c7a5c..72a5adb56 100644 --- a/nsock/src/netutils.h +++ b/nsock/src/netutils.h @@ -63,17 +63,42 @@ #include "nbase_config.h" #endif +#if HAVE_NETINET_IN_H +#include +#endif + #ifdef WIN32 #include "nbase_winconfig.h" /* nbase_winunix.h somehow reason.h to get included */ #include "nbase_winunix.h" #endif +#if HAVE_SYS_UN_H +#include +#endif + +#if HAVE_SYS_UN_H + #define PEER_STR_LEN sizeof(((struct sockaddr_un *) 0)->sun_path) +#else + #define PEER_STR_LEN sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255:xxxxx") +#endif + /* Maximize the number of file descriptors (including sockets) allowed for this * process and return that maximum value (note -- you better not actually open * this many -- stdin, stdout, other files opened by libraries you use, etc. all * count toward this limit. Leave a little slack */ int maximize_fdlimit(void); +#if HAVE_SYS_UN_H +/* Get the UNIX domain socket path or NULL if the socket family != AF_UNIX. */ +const char *get_unixsock_path(struct sockaddr_storage *addr); +#endif + +/* Get the peer/host address string. + * In case we have support for UNIX domain sockets, function returns + * string containing path to UNIX socket if the address family is AF_UNIX, + * otherwise it returns string containing "
:". */ +char *get_hostaddr_string(struct sockaddr_storage *addr, size_t len, unsigned short port); + #endif /* NETUTILS_H */ diff --git a/nsock/src/nsock_connect.c b/nsock/src/nsock_connect.c index 3683cbccd..22ef529db 100644 --- a/nsock/src/nsock_connect.c +++ b/nsock/src/nsock_connect.c @@ -67,9 +67,9 @@ * broadcast flag. Trying to change these functions after making this call will * not have an effect. This function needs to be called before you try to read * or write on the iod. */ -static int nsock_make_socket(mspool *ms, msiod *iod, int family, int proto) { +static int nsock_make_socket(mspool *ms, msiod *iod, int family, int type, int proto) { /* inheritable_socket is from nbase */ - iod->sd = (int)inheritable_socket(family, (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM, proto); + iod->sd = (int)inheritable_socket(family, type, proto); if (iod->sd == -1) { perror("Socket troubles"); return -1; @@ -84,9 +84,17 @@ static int nsock_make_socket(mspool *ms, msiod *iod, int family, int proto) { setsockopt(iod->sd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one)); if (bind(iod->sd, (struct sockaddr *)&iod->local, (int) iod->locallen) == -1) { - if (ms->tracelevel > 0) - nsock_trace(ms, "Bind to %s failed (IOD #%li)", - inet_ntop_ez(&iod->local, iod->locallen), iod->id); + if (ms->tracelevel > 0) { + const char *addrstr = NULL; +#if HAVE_SYS_UN_H + if (iod->local.ss_family == AF_UNIX) + addrstr = get_unixsock_path(&iod->local); + else +#endif + addrstr = inet_ntop_ez(&iod->local, iod->locallen); + + nsock_trace(ms, "Bind to %s failed (IOD #%li)", addrstr, iod->id); + } } } if (iod->ipoptslen && family == AF_INET) { @@ -122,7 +130,7 @@ int nsock_setup_udp(nsock_pool nsp, nsock_iod ms_iod, int af) { if (ms->tracelevel > 0) nsock_trace(ms, "UDP unconnected socket (IOD #%li)", nsi->id); - if (nsock_make_socket(ms, nsi, af, IPPROTO_UDP) == -1) + if (nsock_make_socket(ms, nsi, af, SOCK_DGRAM, IPPROTO_UDP) == -1) return -1; return nsi->sd; @@ -130,7 +138,7 @@ int nsock_setup_udp(nsock_pool nsp, nsock_iod ms_iod, int af) { /* This does the actual logistics of requesting a TCP connection. It is shared * by nsock_connect_tcp and nsock_connect_ssl */ -void nsock_connect_internal(mspool *ms, msevent *nse, int proto, struct sockaddr_storage *ss, size_t sslen, +void nsock_connect_internal(mspool *ms, msevent *nse, int type, int proto, struct sockaddr_storage *ss, size_t sslen, unsigned short port) { struct sockaddr_in *sin = (struct sockaddr_in *)ss; @@ -140,15 +148,19 @@ void nsock_connect_internal(mspool *ms, msevent *nse, int proto, struct sockaddr msiod *iod = nse->iod; /* Now it is time to actually attempt the connection */ - if (nsock_make_socket(ms, iod, ss->ss_family, proto) == -1) { + if (nsock_make_socket(ms, iod, ss->ss_family, type, proto) == -1) { nse->event_done = 1; nse->status = NSE_STATUS_ERROR; nse->errnum = socket_errno(); } else { - if (sin->sin_family == AF_INET) { + if (ss->ss_family == AF_INET) { sin->sin_port = htons(port); +#if HAVE_SYS_UN_H + } else if (ss->ss_family == AF_INET6) { +#else } else { - assert(sin->sin_family == AF_INET6); +#endif + assert(ss->ss_family == AF_INET6); #if HAVE_IPV6 sin6->sin6_port = htons(port); #else @@ -174,6 +186,62 @@ void nsock_connect_internal(mspool *ms, msevent *nse, int proto, struct sockaddr } } +#if HAVE_SYS_UN_H + +/* Request a UNIX domain sockets connection to the same system (by path to socket). + * This function connects to the socket of type SOCK_STREAM. ss should be a + * sockaddr_storage, sockaddr_un as appropriate (just like what you would pass to + * connect). sslen should be the sizeof the structure you are passing in. */ +nsock_event_id nsock_connect_unixsock_stream(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, int timeout_msecs, + void *userdata, struct sockaddr *saddr, size_t sslen) { + msiod *nsi = (msiod *)nsiod; + mspool *ms = (mspool *)nsp; + msevent *nse; + struct sockaddr_storage *ss = (struct sockaddr_storage *)saddr; + + assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN); + + nse = msevent_new(ms, NSE_TYPE_CONNECT, nsi, timeout_msecs, handler, userdata); + assert(nse); + + if (ms->tracelevel > 0) + nsock_trace(ms, "UNIX domain socket (STREAM) connection requested to %s (IOD #%li) EID %li", + get_unixsock_path(ss), nsi->id, nse->id); + + nsock_connect_internal(ms, nse, SOCK_STREAM, 0, ss, sslen, 0); + nsp_add_event(ms, nse); + + return nse->id; + +} + +/* Request a UNIX domain sockets connection to the same system (by path to socket). + * This function connects to the socket of type SOCK_DGRAM. ss should be a + * sockaddr_storage, sockaddr_un as appropriate (just like what you would pass to + * connect). sslen should be the sizeof the structure you are passing in. */ +nsock_event_id nsock_connect_unixsock_datagram(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handler handler, + void *userdata, struct sockaddr *saddr, size_t sslen) { + msiod *nsi = (msiod *)nsiod; + mspool *ms = (mspool *)nsp; + msevent *nse; + struct sockaddr_storage *ss = (struct sockaddr_storage *)saddr; + + assert(nsi->state == NSIOD_STATE_INITIAL || nsi->state == NSIOD_STATE_UNKNOWN); + + nse = msevent_new(ms, NSE_TYPE_CONNECT, nsi, -1, handler, userdata); + assert(nse); + + if (ms->tracelevel > 0) + nsock_trace(ms, "UNIX domain socket (DGRAM) connection requested to %s (IOD #%li) EID %li", + get_unixsock_path(ss), nsi->id, nse->id); + + nsock_connect_internal(ms, nse, SOCK_DGRAM, 0, ss, sslen, 0); + nsp_add_event(ms, nse); + + return nse->id; +} + +#endif /* HAVE_SYS_UN_H */ /* Request a TCP connection to another system (by IP address). The in_addr is * normal network byte order, but the port number should be given in HOST BYTE @@ -198,7 +266,7 @@ nsock_event_id nsock_connect_tcp(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_hand inet_ntop_ez(ss, sslen), port, nsi->id, nse->id); /* Do the actual connect() */ - nsock_connect_internal(ms, nse, IPPROTO_TCP, ss, sslen, port); + nsock_connect_internal(ms, nse, SOCK_STREAM, IPPROTO_TCP, ss, sslen, port); nsp_add_event(ms, nse); return nse->id; @@ -227,7 +295,7 @@ nsock_event_id nsock_connect_sctp(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_han inet_ntop_ez(ss, sslen), port, nsi->id, nse->id); /* Do the actual connect() */ - nsock_connect_internal(ms, nse, IPPROTO_SCTP, ss, sslen, port); + nsock_connect_internal(ms, nse, SOCK_STREAM, IPPROTO_SCTP, ss, sslen, port); nsp_add_event(ms, nse); return nse->id; @@ -269,7 +337,7 @@ nsock_event_id nsock_connect_ssl(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handl inet_ntop_ez(ss, sslen), port, (proto == IPPROTO_TCP ? "tcp" : "sctp"), nsi->id, nse->id); /* Do the actual connect() */ - nsock_connect_internal(ms, nse, proto, ss, sslen, port); + nsock_connect_internal(ms, nse, SOCK_STREAM, proto, ss, sslen, port); nsp_add_event(ms, nse); return nse->id; @@ -342,7 +410,7 @@ nsock_event_id nsock_connect_udp(nsock_pool nsp, nsock_iod nsiod, nsock_ev_handl if (ms->tracelevel > 0) nsock_trace(ms, "UDP connection requested to %s:%hu (IOD #%li) EID %li", inet_ntop_ez(ss, sslen), port, nsi->id, nse->id); - nsock_connect_internal(ms, nse, IPPROTO_UDP, ss, sslen, port); + nsock_connect_internal(ms, nse, SOCK_DGRAM, IPPROTO_UDP, ss, sslen, port); nsp_add_event(ms, nse); return nse->id; @@ -378,7 +446,7 @@ int nsi_getlastcommunicationinfo(nsock_iod ms_iod, int *protocol, int *af, struc if (*protocol == -1) res = 0; } if (af) { - *af = ((struct sockaddr_in *)&nsi->peer)->sin_family; + *af = nsi->peer.ss_family; } if (local) { if (nsi->sd >= 0) { diff --git a/nsock/src/nsock_core.c b/nsock/src/nsock_core.c index 0c2cb915d..176864972 100644 --- a/nsock/src/nsock_core.c +++ b/nsock/src/nsock_core.c @@ -476,7 +476,7 @@ void handle_connect_result(mspool *ms, msevent *nse, enum nse_status status) { saved_ev = iod->watched_events; ms->engine->iod_unregister(ms, iod); close(iod->sd); - nsock_connect_internal(ms, nse, iod->lastproto, &iod->peer, iod->peerlen, nsi_peerport(iod)); + nsock_connect_internal(ms, nse, SOCK_STREAM, iod->lastproto, &iod->peer, iod->peerlen, nsi_peerport(iod)); ms->engine->iod_register(ms, iod, saved_ev); SSL_clear(iod->ssl); @@ -1272,18 +1272,19 @@ void nsock_trace_handler_callback(mspool *ms, msevent *nse) { switch(nse->type) { case NSE_TYPE_CONNECT: case NSE_TYPE_CONNECT_SSL: - nsock_trace(ms, "Callback: %s %s %sfor EID %li [%s:%d]", - nse_type2str(nse->type), nse_status2str(nse->status), errstr, - nse->id, inet_ntop_ez(&nsi->peer, nsi->peerlen), nsi_peerport(nsi)); + nsock_trace(ms, "Callback: %s %s %sfor EID %li [%s]", + nse_type2str(nse->type), nse_status2str(nse->status), + errstr, nse->id, get_hostaddr_string(&nsi->peer, + nsi->peerlen, (unsigned short)nsi_peerport(nsi))); break; case NSE_TYPE_READ: if (nse->status != NSE_STATUS_SUCCESS) { if (nsi->peerlen > 0) { - nsock_trace(ms, "Callback: %s %s %sfor EID %li [%s:%d]", - nse_type2str(nse->type), nse_status2str(nse->status), - errstr, nse->id, inet_ntop_ez(&nsi->peer, nsi->peerlen), - nsi_peerport(nsi)); + nsock_trace(ms, "Callback: %s %s %sfor EID %li [%s]", + nse_type2str(nse->type), nse_status2str(nse->status), + errstr, nse->id, get_hostaddr_string(&nsi->peer, + nsi->peerlen, (unsigned short)nsi_peerport(nsi))); } else { nsock_trace(ms, "Callback: %s %s %sfor EID %li (peer unspecified)", nse_type2str(nse->type), nse_status2str(nse->status), @@ -1301,11 +1302,11 @@ void nsock_trace_handler_callback(mspool *ms, msevent *nse) { } if (nsi->peerlen > 0) { - nsock_trace(ms, "Callback: %s %s for EID %li [%s:%d] %s(%d bytes)%s", + nsock_trace(ms, "Callback: %s %s for EID %li [%s] %s(%d bytes)%s", nse_type2str(nse->type), nse_status2str(nse->status), - nse->id, inet_ntop_ez(&nsi->peer, nsi->peerlen), - nsi_peerport(nsi), nse_eof(nse)? "[EOF]" : "", strlength, - displaystr); + nse->id, get_hostaddr_string(&nsi->peer, nsi->peerlen, + (unsigned short)nsi_peerport(nsi)), + nse_eof(nse)? "[EOF]" : "", strlength, displaystr); } else { nsock_trace(ms, "Callback %s %s for EID %li (peer unspecified) %s(%d bytes)%s", nse_type2str(nse->type), nse_status2str(nse->status), @@ -1315,10 +1316,10 @@ void nsock_trace_handler_callback(mspool *ms, msevent *nse) { break; case NSE_TYPE_WRITE: - nsock_trace(ms, "Callback: %s %s %sfor EID %li [%s:%d]", + nsock_trace(ms, "Callback: %s %s %sfor EID %li [%s]", nse_type2str(nse->type), nse_status2str(nse->status), errstr, - nse->id, inet_ntop_ez(&nsi->peer, nsi->peerlen), - nsi_peerport(nsi)); + nse->id, get_hostaddr_string(&nsi->peer, nsi->peerlen, + (unsigned short)nsi_peerport(nsi))); break; case NSE_TYPE_TIMER: diff --git a/nsock/src/nsock_internal.h b/nsock/src/nsock_internal.h index 24aa22dcb..0135bdac8 100644 --- a/nsock/src/nsock_internal.h +++ b/nsock/src/nsock_internal.h @@ -97,6 +97,9 @@ #if HAVE_STRINGS_H #include #endif +#if HAVE_SYS_UN_H +#include +#endif #ifndef IPPROTO_SCTP #define IPPROTO_SCTP 132 @@ -255,7 +258,8 @@ typedef struct { struct sockaddr_storage local; /* The length of peer/local actually used (sizeof(sockaddr_in) or - * sizeof(sockaddr_in6), or 0 if peer/local has not been filled in */ + * sizeof(sockaddr_in6), SUN_LEN(sockaddr_un), or 0 if peer/local + * has not been filled in */ size_t locallen; size_t peerlen; @@ -424,7 +428,7 @@ void msevent_delete(mspool *nsp, msevent *nse); * etc. */ void nsp_add_event(mspool *nsp, msevent *nse); -void nsock_connect_internal(mspool *ms, msevent *nse, int proto, struct sockaddr_storage *ss, size_t sslen, unsigned short port); +void nsock_connect_internal(mspool *ms, msevent *nse, int type, int proto, struct sockaddr_storage *ss, size_t sslen, unsigned short port); /* Comments on using the following handle_*_result functions are available in nsock_core.c */ diff --git a/nsock/src/nsock_read.c b/nsock/src/nsock_read.c index e24a3510a..3e1b441e6 100644 --- a/nsock/src/nsock_read.c +++ b/nsock/src/nsock_read.c @@ -74,8 +74,9 @@ nsock_event_id nsock_readlines(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_handle if (ms->tracelevel > 0) { if (nsi->peerlen > 0) - nsock_trace(ms, "Read request for %d lines from IOD #%li [%s:%d] EID %li", - nlines, nsi->id, inet_ntop_ez(&nsi->peer, nsi->peerlen), nsi_peerport(nsi), nse->id); + nsock_trace(ms, "Read request for %d lines from IOD #%li [%s] EID %li", + nlines, nsi->id, get_hostaddr_string(&nsi->peer, nsi->peerlen, (unsigned short)nsi_peerport(nsi)), + nse->id); else nsock_trace(ms, "Read request for %d lines from IOD #%li (peer unspecified) EID %li", nlines, nsi->id, nse->id); @@ -102,8 +103,9 @@ nsock_event_id nsock_readbytes(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_handle if (ms->tracelevel > 0) { if (nsi->peerlen > 0) - nsock_trace(ms, "Read request for %d bytes from IOD #%li [%s:%d] EID %li", - nbytes, nsi->id, inet_ntop_ez(&nsi->peer, nsi->peerlen), nsi_peerport(nsi), nse->id); + nsock_trace(ms, "Read request for %d bytes from IOD #%li [%s] EID %li", + nbytes, nsi->id, get_hostaddr_string(&nsi->peer, nsi->peerlen, (unsigned short)nsi_peerport(nsi)), + nse->id); else nsock_trace(ms, "Read request for %d bytes from IOD #%li (peer unspecified) EID %li", nbytes, nsi->id, nse->id); @@ -130,8 +132,9 @@ nsock_event_id nsock_read(nsock_pool nsp, nsock_iod ms_iod, nsock_ev_handler han if (ms->tracelevel > 0) { if (nsi->peerlen > 0) - nsock_trace(ms, "Read request from IOD #%li [%s:%d] (timeout: %dms) EID %li", - nsi->id, inet_ntop_ez(&nsi->peer, nsi->peerlen), nsi_peerport(nsi), timeout_msecs, nse->id); + nsock_trace(ms, "Read request from IOD #%li [%s] (timeout: %dms) EID %li", + nsi->id, get_hostaddr_string(&nsi->peer, nsi->peerlen, (unsigned short)nsi_peerport(nsi)), + timeout_msecs, nse->id); else nsock_trace(ms, "Read request from IOD #%li (peer unspecified) (timeout: %dms) EID %li", nsi->id, timeout_msecs, nse->id); diff --git a/nsock/src/nsock_write.c b/nsock/src/nsock_write.c index 4b1314c15..5995b24a8 100644 --- a/nsock/src/nsock_write.c +++ b/nsock/src/nsock_write.c @@ -76,10 +76,14 @@ nsock_event_id nsock_sendto(nsock_pool ms_pool, nsock_iod ms_iod, nsock_ev_handl nse = msevent_new(nsp, NSE_TYPE_WRITE, nsi, timeout_msecs, handler, userdata); assert(nse); - if (sin->sin_family == AF_INET) { + if (saddr->sa_family == AF_INET) { sin->sin_port = htons(port); +#if HAVE_SYS_UN_H + } else if (saddr->sa_family == AF_INET6) { +#else } else { - assert(sin->sin_family == AF_INET6); +#endif + assert(saddr->sa_family == AF_INET6); #if HAVE_IPV6 sin6->sin6_port = htons(port); #else @@ -107,9 +111,9 @@ nsock_event_id nsock_sendto(nsock_pool ms_pool, nsock_iod ms_iod, nsock_ev_handl } else { displaystr[0] = '\0'; } - nsock_trace(nsp, "Sendto request for %d bytes to IOD #%li EID %li [%s:%hu]%s", + nsock_trace(nsp, "Sendto request for %d bytes to IOD #%li EID %li [%s]%s", datalen, nsi->id, nse->id, - inet_ntop_ez(&nse->writeinfo.dest, nse->writeinfo.destlen), port, + get_hostaddr_string(&nse->writeinfo.dest, nse->writeinfo.destlen, port), displaystr); } @@ -147,8 +151,9 @@ nsock_event_id nsock_write(nsock_pool ms_pool, nsock_iod ms_iod, replacenonprintable(displaystr + 2, datalen, '.'); } else displaystr[0] = '\0'; if (nsi->peerlen > 0) - nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li [%s:%d]%s", datalen, nsi->id, - nse->id, inet_ntop_ez(&nsi->peer, nsi->peerlen), nsi_peerport(nsi), displaystr); + nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li [%s]%s", datalen, nsi->id, + nse->id, get_hostaddr_string(&nsi->peer, nsi->peerlen, (unsigned short)nsi_peerport(nsi)), + displaystr); else nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li (peer unspecified)%s", datalen, nsi->id, nse->id, displaystr); @@ -220,8 +225,9 @@ nsock_event_id nsock_printf(nsock_pool ms_pool, nsock_iod ms_iod, displaystr[0] = '\0'; } if (nsi->peerlen > 0) - nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li [%s:%d]%s", strlength, nsi->id, - nse->id, inet_ntop_ez(&nsi->peer, nsi->peerlen), nsi_peerport(nsi), displaystr); + nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li [%s]%s", strlength, nsi->id, + nse->id, get_hostaddr_string(&nsi->peer, nsi->peerlen, (unsigned short)nsi_peerport(nsi)), + displaystr); else nsock_trace(nsp, "Write request for %d bytes to IOD #%li EID %li (peer unspecified)%s", strlength, nsi->id, nse->id, displaystr);