diff --git a/lib/isc/include/isc/socket.h b/lib/isc/include/isc/socket.h index a4eed16dfd..73c75fa82e 100644 --- a/lib/isc/include/isc/socket.h +++ b/lib/isc/include/isc/socket.h @@ -78,7 +78,8 @@ ISC_LANG_BEGINDECLS ***/ /* - * Maximum number of buffers in a scatter/gather read/write + * Maximum number of buffers in a scatter/gather read/write. The operating + * system in use must support at least this number (plus one on some.) */ #define ISC_SOCKET_MAXSCATTERGATHER 8 diff --git a/lib/isc/unix/include/isc/net.h.in b/lib/isc/unix/include/isc/net.h.in index fad5a41ded..078e38fbe7 100644 --- a/lib/isc/unix/include/isc/net.h.in +++ b/lib/isc/unix/include/isc/net.h.in @@ -120,6 +120,15 @@ */ @ISC_NET_MSGHDRFLAVOR@ +/* + * If this system does not have MSG_TRUNC (as returned from recvmsg()) + * ISC_NET_RECVOVERFLOW will be defined. This will enable the MSG_TRUNC + * faking code in socket.c. + */ +#ifndef MSG_TRUNC +#define ISC_NET_RECVOVERFLOW +#endif + /*** *** Functions. ***/ diff --git a/lib/isc/unix/socket.c b/lib/isc/unix/socket.c index 11e25bfb37..1cd7629882 100644 --- a/lib/isc/unix/socket.c +++ b/lib/isc/unix/socket.c @@ -146,6 +146,9 @@ struct isc_socket { connected : 1, connecting : 1; /* connect pending */ +#ifdef ISC_NET_RECVOVERFLOW + unsigned char overflow; /* used for MSG_TRUNC fake */ +#endif #ifdef notyet unsigned char cmsg[1024]; /* XXX size? */ #endif @@ -175,6 +178,16 @@ struct isc_socketmgr { #define MANAGED 1 #define CLOSE_PENDING 2 +/* + * send() and recv() iovec counts + */ +#define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER) +#ifdef ISC_NET_RECVOVERFLOW +# define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER + 1) +#else +# define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER) +#endif + static void send_recvdone_event(isc_socket_t *, isc_socketevent_t **, isc_result_t); static void send_senddone_event(isc_socket_t *, isc_socketevent_t **, @@ -420,7 +433,11 @@ build_msghdr_recv(isc_socket_t *sock, isc_socketevent_t *dev, memset(&dev->address, 0, sizeof(dev->address)); msg->msg_name = (void *)&dev->address.type.sa; msg->msg_namelen = sizeof(dev->address.type.sa); - } else { +#ifdef ISC_NET_RECVOVERFLOW + /* If needed, steal one iovec for overflow detection. */ + maxiov--; +#endif + } else { /* TCP */ msg->msg_name = NULL; msg->msg_namelen = 0; dev->address = sock->address; @@ -436,8 +453,7 @@ build_msghdr_recv(isc_socket_t *sock, isc_socketevent_t *dev, read_count = dev->region.length - dev->n; iov[0].iov_base = (void *)(dev->region.base + dev->n); iov[0].iov_len = read_count; - msg->msg_iov = iov; - msg->msg_iovlen = 1; + iovcount = 1; goto config; } @@ -468,10 +484,24 @@ build_msghdr_recv(isc_socket_t *sock, isc_socketevent_t *dev, buffer = ISC_LIST_NEXT(buffer, link); } + config: + + /* + * If needed, set up to receive that one extra byte. Note that + * we know there is at least one iov left, since we stole it + * at the top of this function. + */ +#ifdef ISC_NET_RECVOVERFLOW + if (sock->type == isc_sockettype_udp) { + iov[iovcount].iov_base = (void *)(&sock->overflow); + iov[iovcount].iov_len = 1; + iovcount++; + } +#endif + msg->msg_iov = iov; msg->msg_iovlen = iovcount; - config: #ifdef ISC_NET_BSD44MSGHDR msg->msg_control = NULL; msg->msg_controllen = 0; @@ -524,6 +554,22 @@ allocate_socketevent(isc_socket_t *sock, isc_eventtype_t eventtype, return (ev); } +#if defined(ISC_SOCKET_DEBUG) +static void +dump_msg(struct msghdr *msg) +{ + unsigned int i; + + printf("MSGHDR %p\n", msg); + printf("\tname %p, namelen %d\n", msg->msg_name, msg->msg_namelen); + printf("\tiov %p, iovlen %d\n", msg->msg_iov, msg->msg_iovlen); + for (i = 0 ; i < msg->msg_iovlen ; i++) + printf("\t\t%d\tbase %p, len %d\n", i, + msg->msg_iov[i].iov_base, + msg->msg_iov[i].iov_len); +} +#endif + #define DOIO_SUCCESS 0 /* i/o ok, event sent */ #define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */ #define DOIO_HARD 2 /* i/o error, event sent */ @@ -534,14 +580,18 @@ static int doio_recv(isc_socket_t *sock, isc_socketevent_t *dev) { int cc; - struct iovec iov[ISC_SOCKET_MAXSCATTERGATHER]; + struct iovec iov[MAXSCATTERGATHER_RECV]; size_t read_count; size_t actual_count; struct msghdr msghdr; isc_buffer_t *buffer; build_msghdr_recv(sock, dev, &msghdr, iov, - ISC_SOCKET_MAXSCATTERGATHER, &read_count); + MAXSCATTERGATHER_RECV, &read_count); + +#if defined(ISC_SOCKET_DEBUG) + dump_msg(&msghdr); +#endif cc = recvmsg(sock->fd, &msghdr, 0); if (sock->type == isc_sockettype_udp) @@ -595,6 +645,18 @@ doio_recv(isc_socket_t *sock, isc_socketevent_t *dev) return (DOIO_EOF); } + /* + * Overflow bit detection. If we received MORE bytes than we should, + * this indicates an overflow situation. Set the flag in the + * dev entry and adjust how much we read by one. + */ +#ifdef ISC_NET_RECVOVERFLOW + if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) { + dev->attributes |= ISC_SOCKEVENTATTR_TRUNC; + cc--; + } +#endif + /* * If there are control messages attached, run through them and pull * out the interesting bits. @@ -646,12 +708,12 @@ static int doio_send(isc_socket_t *sock, isc_socketevent_t *dev) { int cc; - struct iovec iov[ISC_SOCKET_MAXSCATTERGATHER]; + struct iovec iov[MAXSCATTERGATHER_SEND]; size_t write_count; struct msghdr msghdr; build_msghdr_send(sock, dev, &msghdr, iov, - ISC_SOCKET_MAXSCATTERGATHER, &write_count); + MAXSCATTERGATHER_SEND, &write_count); cc = sendmsg(sock->fd, &msghdr, 0);