socket.c revision edf8e79280a78350a13ad519ce035ec12c79be0f
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Copyright (C) 1998-2003 Internet Software Consortium.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Permission to use, copy, modify, and distribute this software for any
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * PERFORMANCE OF THIS SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley/* $Id: socket.c,v 1.267 2007/01/04 00:01:13 marka Exp $ */
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*! \file */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <config.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <sys/param.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <sys/types.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <sys/socket.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <sys/stat.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ISC_PLATFORM_HAVESYSUNH
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <sys/un.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley#include <sys/time.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <sys/uio.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <errno.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <fcntl.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <stddef.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <stdlib.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <string.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <unistd.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/buffer.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/bufferlist.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/condition.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/formatcheck.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/list.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/log.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/mem.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/msgs.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/mutex.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/net.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/platform.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/print.h>
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews#include <isc/region.h>
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews#include <isc/socket.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/strerror.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/task.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/thread.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/util.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/xml.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include "errno2result.h"
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#ifndef ISC_PLATFORM_USETHREADS
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include "socket_p.h"
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif /* ISC_PLATFORM_USETHREADS */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*%
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Some systems define the socket length argument as an int, some as size_t,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * some as socklen_t. This is here so it can be easily changed if needed.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifndef ISC_SOCKADDR_LEN_T
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define ISC_SOCKADDR_LEN_T unsigned int
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*%
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Define what the possible "soft" errors can be. These are non-fatal returns
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * of various network related functions, like recv() and so on.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * For some reason, BSDI (and perhaps others) will sometimes return <0
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * from recv() but will have errno==0. This is broken, but we have to
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * work around it here.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SOFT_ERROR(e) ((e) == EAGAIN || \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (e) == EWOULDBLOCK || \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (e) == EINTR || \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (e) == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*!<
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * DLVL(90) -- Function entry/exit and other tracing.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * DLVL(70) -- Socket "correctness" -- including returning of events, etc.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * DLVL(60) -- Socket data send/receive
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * DLVL(50) -- Event tracing, including receiving/sending completion events.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * DLVL(20) -- Socket creation/destruction.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define TRACE_LEVEL 90
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CORRECTNESS_LEVEL 70
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define IOEVENT_LEVEL 60
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define EVENT_LEVEL 50
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CREATION_LEVEL 20
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define TRACE DLVL(TRACE_LEVEL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CORRECTNESS DLVL(CORRECTNESS_LEVEL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define IOEVENT DLVL(IOEVENT_LEVEL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define EVENT DLVL(EVENT_LEVEL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CREATION DLVL(CREATION_LEVEL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef isc_event_t intev_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SOCKET_MAGIC ISC_MAGIC('I', 'O', 'i', 'o')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define VALID_SOCKET(t) ISC_MAGIC_VALID(t, SOCKET_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*!
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * IPv6 control information. If the socket is an IPv6 socket we want
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * to collect the destination address and interface so the client can
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * set them on outgoing packets.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifndef USE_CMSG
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define USE_CMSG 1
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley/*%
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley * NetBSD and FreeBSD can timestamp packets. XXXMLG Should we have
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * a setsockopt() like interface to request timestamps, and if the OS
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * doesn't do it for us, call gettimeofday() on every UDP receive?
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#ifdef SO_TIMESTAMP
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#ifndef USE_CMSG
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#define USE_CMSG 1
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#endif
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#endif
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley/*%
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * The size to raise the recieve buffer to (from BIND 8).
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#define RCVBUFSIZE (32*1024)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley/*%
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley * The number of times a send operation is repeated if the result is EINTR.
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley#define NRETRIES 10
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halleystruct isc_socket {
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley /* Not locked. */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley unsigned int magic;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_socketmgr_t *manager;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mutex_t lock;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_sockettype_t type;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley /* Locked by socket lock. */
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley ISC_LINK(isc_socket_t) link;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley unsigned int references;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley int fd;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley int pf;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley ISC_LIST(isc_socketevent_t) send_list;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley ISC_LIST(isc_socketevent_t) recv_list;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley ISC_LIST(isc_socket_newconnev_t) accept_list;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_socket_connev_t *connect_ev;
95f78a208ad6dfb8359320c77ab30c670c773922Mark Andrews
95f78a208ad6dfb8359320c77ab30c670c773922Mark Andrews /*
95f78a208ad6dfb8359320c77ab30c670c773922Mark Andrews * Internal events. Posted when a descriptor is readable or
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley * writable. These are statically allocated and never freed.
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley * They will be set to non-purgable before use.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley intev_t readable_ev;
3740b569ae76295b941d57a724a43beb75b533baBob Halley intev_t writable_ev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley isc_sockaddr_t peer_address; /* remote address */
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley unsigned int pending_recv : 1,
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley pending_send : 1,
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley pending_accept : 1,
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley listener : 1, /* listener socket */
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley connected : 1,
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley connecting : 1, /* connect pending */
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley bound : 1; /* bound to local addr */
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley#ifdef ISC_NET_RECVOVERFLOW
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley unsigned char overflow; /* used for MSG_TRUNC fake */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley#endif
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley char *recvcmsgbuf;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_SOCKADDR_LEN_T recvcmsgbuflen;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley char *sendcmsgbuf;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley ISC_SOCKADDR_LEN_T sendcmsgbuflen;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley void *fdwatcharg;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_sockfdwatch_t fdwatchcb;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley int fdwatchflags;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_task_t *fdwatchtask;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley};
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley#define SOCKET_MANAGER_MAGIC ISC_MAGIC('I', 'O', 'm', 'g')
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley#define VALID_MANAGER(m) ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleystruct isc_socketmgr {
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley /* Not locked. */
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned int magic;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_mem_t *mctx;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_mutex_t lock;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley /* Locked by manager lock. */
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley ISC_LIST(isc_socket_t) socklist;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley fd_set read_fds;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley fd_set write_fds;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_socket_t *fds[FD_SETSIZE];
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley int fdstate[FD_SETSIZE];
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley int maxfd;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley#ifdef ISC_PLATFORM_USETHREADS
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_thread_t watcher;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_condition_t shutdown_ok;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley int pipe_fds[2];
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#else /* ISC_PLATFORM_USETHREADS */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int refs;
3740b569ae76295b941d57a724a43beb75b533baBob Halley#endif /* ISC_PLATFORM_USETHREADS */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley};
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifndef ISC_PLATFORM_USETHREADS
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic isc_socketmgr_t *socketmgr = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif /* ISC_PLATFORM_USETHREADS */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLOSED 0 /* this one must be zero */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define MANAGED 1
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLOSE_PENDING 2
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define MANAGER_CLOSE_PENDING 3
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * send() and recv() iovec counts
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ISC_NET_RECVOVERFLOW
3740b569ae76295b941d57a724a43beb75b533baBob Halley# define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER + 1)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley# define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void send_recvdone_event(isc_socket_t *, isc_socketevent_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void send_senddone_event(isc_socket_t *, isc_socketevent_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void free_socket(isc_socket_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic isc_result_t allocate_socket(isc_socketmgr_t *, isc_sockettype_t,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void destroy(isc_socket_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void internal_accept(isc_task_t *, isc_event_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void internal_connect(isc_task_t *, isc_event_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void internal_recv(isc_task_t *, isc_event_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void internal_send(isc_task_t *, isc_event_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void internal_fdwatch_write(isc_task_t *, isc_event_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void internal_fdwatch_read(isc_task_t *, isc_event_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void process_cmsg(isc_socket_t *, struct msghdr *, isc_socketevent_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void build_msghdr_send(isc_socket_t *, isc_socketevent_t *,
3740b569ae76295b941d57a724a43beb75b533baBob Halley struct msghdr *, struct iovec *, size_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void build_msghdr_recv(isc_socket_t *, isc_socketevent_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley struct msghdr *, struct iovec *, size_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SELECT_POKE_SHUTDOWN (-1)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SELECT_POKE_NOTHING (-2)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SELECT_POKE_READ (-3)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SELECT_POKE_ACCEPT (-3) /*%< Same as _READ */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SELECT_POKE_WRITE (-4)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SELECT_POKE_CONNECT (-4) /*%< Same as _WRITE */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SELECT_POKE_CLOSE (-5)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SOCK_DEAD(s) ((s)->references == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleymanager_log(isc_socketmgr_t *sockmgr,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_logcategory_t *category, isc_logmodule_t *module, int level,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleymanager_log(isc_socketmgr_t *sockmgr,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_logcategory_t *category, isc_logmodule_t *module, int level,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley const char *fmt, ...)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley char msgbuf[2048];
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley va_list ap;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (! isc_log_wouldlog(isc_lctx, level))
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley va_start(ap, fmt);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley va_end(ap);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_log_write(isc_lctx, category, module, level,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley "sockmgr %p: %s", sockmgr, msgbuf);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic void
ce3761f64d3d734cc94605026985898900ecc474Bob Halleysocket_log(isc_socket_t *sock, isc_sockaddr_t *address,
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_logcategory_t *category, isc_logmodule_t *module, int level,
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley isc_msgcat_t *msgcat, int msgset, int message,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley const char *fmt, ...) ISC_FORMAT_PRINTF(9, 10);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleysocket_log(isc_socket_t *sock, isc_sockaddr_t *address,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_logcategory_t *category, isc_logmodule_t *module, int level,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_msgcat_t *msgcat, int msgset, int message,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley const char *fmt, ...)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley{
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley char msgbuf[2048];
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley char peerbuf[ISC_SOCKADDR_FORMATSIZE];
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley va_list ap;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (! isc_log_wouldlog(isc_lctx, level))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley va_start(ap, fmt);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley va_end(ap);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (address == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_log_iwrite(isc_lctx, category, module, level,
ce3761f64d3d734cc94605026985898900ecc474Bob Halley msgcat, msgset, message,
ce3761f64d3d734cc94605026985898900ecc474Bob Halley "socket %p: %s", sock, msgbuf);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley } else {
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_sockaddr_format(address, peerbuf, sizeof(peerbuf));
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_log_iwrite(isc_lctx, category, module, level,
ce3761f64d3d734cc94605026985898900ecc474Bob Halley msgcat, msgset, message,
ce3761f64d3d734cc94605026985898900ecc474Bob Halley "socket %p %s: %s", sock, peerbuf, msgbuf);
5fc1b54cc6134bd70f4e22df90a2e5631aaea77aBob Halley }
5fc1b54cc6134bd70f4e22df90a2e5631aaea77aBob Halley}
5fc1b54cc6134bd70f4e22df90a2e5631aaea77aBob Halley
5fc1b54cc6134bd70f4e22df90a2e5631aaea77aBob Halleystatic void
ce3761f64d3d734cc94605026985898900ecc474Bob Halleywakeup_socket(isc_socketmgr_t *manager, int fd, int msg) {
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_socket_t *sock;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley /*
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * This is a wakeup on a socket. If the socket is not in the
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * process of being closed, start watching it for either reads
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * or writes.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley */
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley INSIST(fd >= 0 && fd < (int)FD_SETSIZE);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley if (manager->fdstate[fd] == CLOSE_PENDING
ce3761f64d3d734cc94605026985898900ecc474Bob Halley || manager->fdstate[fd] == MANAGER_CLOSE_PENDING) {
ce3761f64d3d734cc94605026985898900ecc474Bob Halley FD_CLR(fd, &manager->read_fds);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley FD_CLR(fd, &manager->write_fds);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley if (manager->fdstate[fd] == CLOSE_PENDING)
ce3761f64d3d734cc94605026985898900ecc474Bob Halley (void)close(fd);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley manager->fdstate[fd] = CLOSED;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley return;
3740b569ae76295b941d57a724a43beb75b533baBob Halley }
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (manager->fdstate[fd] != MANAGED)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock = manager->fds[fd];
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Set requested bit.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (msg == SELECT_POKE_READ)
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley FD_SET(sock->fd, &manager->read_fds);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley if (msg == SELECT_POKE_WRITE)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley FD_SET(sock->fd, &manager->write_fds);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#ifdef ISC_PLATFORM_USETHREADS
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley/*
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley * Poke the select loop when there is something for us to do.
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley * The write is required (by POSIX) to complete. That is, we
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley * will not get partial writes.
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley */
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halleystatic void
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halleyselect_poke(isc_socketmgr_t *mgr, int fd, int msg) {
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley int cc;
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley int buf[2];
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley char strbuf[ISC_STRERRORSIZE];
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley buf[0] = fd;
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley buf[1] = msg;
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley do {
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley cc = write(mgr->pipe_fds[1], buf, sizeof(buf));
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#ifdef ENOSR
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley /*
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley * Treat ENOSR as EAGAIN but loop slowly as it is
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley * unlikely to clear fast.
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley */
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley if (cc < 0 && errno == ENOSR) {
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley sleep(1);
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley errno = EAGAIN;
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley }
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley#endif
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley } while (cc < 0 && SOFT_ERROR(errno));
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley if (cc < 0) {
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley FATAL_ERROR(__FILE__, __LINE__,
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley ISC_MSG_WRITEFAILED,
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley "write() failed "
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley "during watcher poke: %s"),
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley strbuf);
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley }
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley INSIST(cc == sizeof(buf));
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley}
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley/*
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley * Read a message on the internal fd.
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley */
f18f3c93e7fecf120302658f93addae573a6e874Bob Halleystatic void
f18f3c93e7fecf120302658f93addae573a6e874Bob Halleyselect_readmsg(isc_socketmgr_t *mgr, int *fd, int *msg) {
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley int buf[2];
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley int cc;
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley char strbuf[ISC_STRERRORSIZE];
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley
f18f3c93e7fecf120302658f93addae573a6e874Bob Halley cc = read(mgr->pipe_fds[0], buf, sizeof(buf));
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley if (cc < 0) {
1366b7833c86343de278480b9abd71754e418bfaBob Halley *msg = SELECT_POKE_NOTHING;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley *fd = -1; /* Silence compiler. */
f0bbac2c0f1afa74b88cab902daf11202ebe7cbdBob Halley if (SOFT_ERROR(errno))
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley return;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley
1366b7833c86343de278480b9abd71754e418bfaBob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley FATAL_ERROR(__FILE__, __LINE__,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley ISC_MSG_READFAILED,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley "read() failed "
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley "during watcher poke: %s"),
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley strbuf);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley return;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley }
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley INSIST(cc == sizeof(buf));
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley *fd = buf[0];
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley *msg = buf[1];
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley}
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley#else /* ISC_PLATFORM_USETHREADS */
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley/*
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley * Update the state of the socketmgr when something changes.
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley */
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void
1366b7833c86343de278480b9abd71754e418bfaBob Halleyselect_poke(isc_socketmgr_t *manager, int fd, int msg) {
1366b7833c86343de278480b9abd71754e418bfaBob Halley if (msg == SELECT_POKE_SHUTDOWN)
1366b7833c86343de278480b9abd71754e418bfaBob Halley return;
1366b7833c86343de278480b9abd71754e418bfaBob Halley else if (fd >= 0)
1366b7833c86343de278480b9abd71754e418bfaBob Halley wakeup_socket(manager, fd, msg);
1366b7833c86343de278480b9abd71754e418bfaBob Halley return;
1366b7833c86343de278480b9abd71754e418bfaBob Halley}
1366b7833c86343de278480b9abd71754e418bfaBob Halley#endif /* ISC_PLATFORM_USETHREADS */
1366b7833c86343de278480b9abd71754e418bfaBob Halley
1366b7833c86343de278480b9abd71754e418bfaBob Halley/*
1366b7833c86343de278480b9abd71754e418bfaBob Halley * Make a fd non-blocking.
1366b7833c86343de278480b9abd71754e418bfaBob Halley */
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic isc_result_t
1366b7833c86343de278480b9abd71754e418bfaBob Halleymake_nonblock(int fd) {
1366b7833c86343de278480b9abd71754e418bfaBob Halley int ret;
1366b7833c86343de278480b9abd71754e418bfaBob Halley int flags;
1366b7833c86343de278480b9abd71754e418bfaBob Halley char strbuf[ISC_STRERRORSIZE];
1366b7833c86343de278480b9abd71754e418bfaBob Halley#ifdef USE_FIONBIO_IOCTL
1366b7833c86343de278480b9abd71754e418bfaBob Halley int on = 1;
1366b7833c86343de278480b9abd71754e418bfaBob Halley
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley ret = ioctl(fd, FIONBIO, (char *)&on);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley#else
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley flags = fcntl(fd, F_GETFL, 0);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley flags |= PORT_NONBLOCK;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley ret = fcntl(fd, F_SETFL, flags);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley#endif
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (ret == -1) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#ifdef USE_FIONBIO_IOCTL
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "ioctl(%d, FIONBIO, &on): %s", fd,
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "fcntl(%d, F_SETFL, %d): %s", fd, flags,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley strbuf);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (ISC_R_UNEXPECTED);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ISC_R_SUCCESS);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#ifdef USE_CMSG
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley/*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * In order to ensure as much portability as possible, we provide wrapper
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * functions of these macros.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Note that cmsg_space() could run slow on OSes that do not have
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * CMSG_SPACE.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline ISC_SOCKADDR_LEN_T
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycmsg_len(ISC_SOCKADDR_LEN_T len) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef CMSG_LEN
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (CMSG_LEN(len));
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#else
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley ISC_SOCKADDR_LEN_T hdrlen;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley /*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * is correct.
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley */
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley hdrlen = (ISC_SOCKADDR_LEN_T)CMSG_DATA(((struct cmsghdr *)NULL));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (hdrlen + len);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#endif
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley}
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halleystatic inline ISC_SOCKADDR_LEN_T
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halleycmsg_space(ISC_SOCKADDR_LEN_T len) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef CMSG_SPACE
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (CMSG_SPACE(len));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley struct msghdr msg;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley struct cmsghdr *cmsgp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * XXX: The buffer length is an ad-hoc value, but should be enough
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * in a practical sense.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley char dummybuf[sizeof(struct cmsghdr) + 1024];
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley memset(&msg, 0, sizeof(msg));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley msg.msg_control = dummybuf;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley msg.msg_controllen = sizeof(dummybuf);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cmsgp = (struct cmsghdr *)dummybuf;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cmsgp->cmsg_len = cmsg_len(len);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley cmsgp = CMSG_NXTHDR(&msg, cmsgp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (cmsgp != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return ((char *)cmsgp - (char *)msg.msg_control);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif /* USE_CMSG */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Process control messages received on a socket.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyprocess_cmsg(isc_socket_t *sock, struct msghdr *msg, isc_socketevent_t *dev) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef USE_CMSG
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley struct cmsghdr *cmsgp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley struct in6_pktinfo *pktinfop;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#ifdef SO_TIMESTAMP
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley struct timeval *timevalp;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#endif
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#endif
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley /*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * msg and dev are used only when ISC_NET_BSD44MSGHDR is defined.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * They are all here, outside of the CPP tests, because it is
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * more consistent with the usual ISC coding style.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley UNUSED(sock);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley UNUSED(msg);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNUSED(dev);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#ifdef ISC_NET_BSD44MSGHDR
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#ifdef MSG_TRUNC
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if ((msg->msg_flags & MSG_TRUNC) == MSG_TRUNC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#ifdef MSG_CTRUNC
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if ((msg->msg_flags & MSG_CTRUNC) == MSG_CTRUNC)
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley dev->attributes |= ISC_SOCKEVENTATTR_CTRUNC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifndef USE_CMSG
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (msg->msg_controllen == 0U || msg->msg_control == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef SO_TIMESTAMP
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley timevalp = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley pktinfop = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cmsgp = CMSG_FIRSTHDR(msg);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (cmsgp != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley socket_log(sock, NULL, TRACE,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PROCESSCMSG,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "processing cmsg %p", cmsgp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (cmsgp->cmsg_level == IPPROTO_IPV6
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley && cmsgp->cmsg_type == IPV6_PKTINFO) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley memcpy(&dev->pktinfo, pktinfop,
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley sizeof(struct in6_pktinfo));
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley socket_log(sock, NULL, TRACE,
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley isc_msgcat, ISC_MSGSET_SOCKET,
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley ISC_MSG_IFRECEIVED,
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley "interface received on ifindex %u",
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley dev->pktinfo.ipi6_ifindex);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (IN6_IS_ADDR_MULTICAST(&pktinfop->ipi6_addr))
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley dev->attributes |= ISC_SOCKEVENTATTR_MULTICAST;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley goto next;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#endif
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#ifdef SO_TIMESTAMP
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (cmsgp->cmsg_level == SOL_SOCKET
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley && cmsgp->cmsg_type == SCM_TIMESTAMP) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley timevalp = (struct timeval *)CMSG_DATA(cmsgp);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley dev->timestamp.seconds = timevalp->tv_sec;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley dev->timestamp.nanoseconds = timevalp->tv_usec * 1000;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley dev->attributes |= ISC_SOCKEVENTATTR_TIMESTAMP;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley goto next;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#endif
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley next:
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley cmsgp = CMSG_NXTHDR(msg, cmsgp);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#endif /* USE_CMSG */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#endif /* ISC_NET_BSD44MSGHDR */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley}
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley/*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Construct an iov array and attach it to the msghdr passed in. This is
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * the SEND constructor, which will use the used region of the buffer
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * (if using a buffer list) or will use the internal region (if a single
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * buffer I/O is requested).
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley *
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Nothing can be NULL, and the done event must list at least one buffer
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * on the buffer linked list for this function to be meaningful.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley *
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * If write_countp != NULL, *write_countp will hold the number of bytes
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * this transaction can send.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halleystatic void
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halleybuild_msghdr_send(isc_socket_t *sock, isc_socketevent_t *dev,
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley struct msghdr *msg, struct iovec *iov, size_t *write_countp)
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley{
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley unsigned int iovcount;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley isc_buffer_t *buffer;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley isc_region_t used;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley size_t write_count;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley size_t skip_count;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley memset(msg, 0, sizeof(*msg));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->type == isc_sockettype_udp) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley msg->msg_name = (void *)&dev->address.type.sa;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley msg->msg_namelen = dev->address.length;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley } else {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley msg->msg_name = NULL;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley msg->msg_namelen = 0;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley buffer = ISC_LIST_HEAD(dev->bufferlist);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley write_count = 0;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley iovcount = 0;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley /*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Single buffer I/O? Skip what we've done so far in this region.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (buffer == NULL) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley write_count = dev->region.length - dev->n;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley iov[0].iov_base = (void *)(dev->region.base + dev->n);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley iov[0].iov_len = write_count;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley iovcount = 1;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley goto config;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley /*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Multibuffer I/O.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Skip the data in the buffer list that we have already written.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley skip_count = dev->n;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley while (buffer != NULL) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley REQUIRE(ISC_BUFFER_VALID(buffer));
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (skip_count < isc_buffer_usedlength(buffer))
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley break;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley skip_count -= isc_buffer_usedlength(buffer);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley buffer = ISC_LIST_NEXT(buffer, link);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley while (buffer != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(iovcount < MAXSCATTERGATHER_SEND);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley isc_buffer_usedregion(buffer, &used);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (used.length > 0) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley iov[iovcount].iov_base = (void *)(used.base
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley + skip_count);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley iov[iovcount].iov_len = used.length - skip_count;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley write_count += (used.length - skip_count);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley skip_count = 0;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley iovcount++;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley buffer = ISC_LIST_NEXT(buffer, link);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley INSIST(skip_count == 0U);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley config:
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley msg->msg_iov = iov;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley msg->msg_iovlen = iovcount;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley#ifdef ISC_NET_BSD44MSGHDR
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley msg->msg_control = NULL;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley msg->msg_controllen = 0;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley msg->msg_flags = 0;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley#if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if ((sock->type == isc_sockettype_udp)
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley && ((dev->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley struct cmsghdr *cmsgp;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley struct in6_pktinfo *pktinfop;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley socket_log(sock, NULL, TRACE,
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_SENDTODATA,
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley "sendto pktinfo data, ifindex %u",
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley dev->pktinfo.ipi6_ifindex);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley msg->msg_controllen = cmsg_space(sizeof(struct in6_pktinfo));
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley msg->msg_control = (void *)sock->sendcmsgbuf;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley cmsgp = (struct cmsghdr *)sock->sendcmsgbuf;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley cmsgp->cmsg_level = IPPROTO_IPV6;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley cmsgp->cmsg_type = IPV6_PKTINFO;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley cmsgp->cmsg_len = cmsg_len(sizeof(struct in6_pktinfo));
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley memcpy(pktinfop, &dev->pktinfo, sizeof(struct in6_pktinfo));
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley#endif /* USE_CMSG && ISC_PLATFORM_HAVEIPV6 */
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley#else /* ISC_NET_BSD44MSGHDR */
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley msg->msg_accrights = NULL;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley msg->msg_accrightslen = 0;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley#endif /* ISC_NET_BSD44MSGHDR */
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (write_countp != NULL)
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley *write_countp = write_count;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley}
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley/*
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Construct an iov array and attach it to the msghdr passed in. This is
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * the RECV constructor, which will use the avialable region of the buffer
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * (if using a buffer list) or will use the internal region (if a single
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * buffer I/O is requested).
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley *
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Nothing can be NULL, and the done event must list at least one buffer
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * on the buffer linked list for this function to be meaningful.
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley *
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * If read_countp != NULL, *read_countp will hold the number of bytes
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * this transaction can receive.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews */
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsstatic void
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsbuild_msghdr_recv(isc_socket_t *sock, isc_socketevent_t *dev,
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews struct msghdr *msg, struct iovec *iov, size_t *read_countp)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews{
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews unsigned int iovcount;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews isc_buffer_t *buffer;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews isc_region_t available;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews size_t read_count;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews memset(msg, 0, sizeof(struct msghdr));
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews if (sock->type == isc_sockettype_udp) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews memset(&dev->address, 0, sizeof(dev->address));
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews#ifdef BROKEN_RECVMSG
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews if (sock->pf == AF_INET) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_name = (void *)&dev->address.type.sin;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_namelen = sizeof(dev->address.type.sin6);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews } else if (sock->pf == AF_INET6) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_name = (void *)&dev->address.type.sin6;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_namelen = sizeof(dev->address.type.sin6);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews#ifdef ISC_PLATFORM_HAVESYSUNH
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews } else if (sock->pf == AF_UNIX) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_name = (void *)&dev->address.type.sunix;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_namelen = sizeof(dev->address.type.sunix);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews#endif
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews } else {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_name = (void *)&dev->address.type.sa;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_namelen = sizeof(dev->address.type);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews }
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews#else
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_name = (void *)&dev->address.type.sa;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_namelen = sizeof(dev->address.type);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews#endif
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews#ifdef ISC_NET_RECVOVERFLOW
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews /* If needed, steal one iovec for overflow detection. */
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews maxiov--;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews#endif
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews } else { /* TCP */
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_name = NULL;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews msg->msg_namelen = 0;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews dev->address = sock->peer_address;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews }
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews buffer = ISC_LIST_HEAD(dev->bufferlist);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews read_count = 0;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews /*
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Single buffer I/O? Skip what we've done so far in this region.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews */
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews if (buffer == NULL) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews read_count = dev->region.length - dev->n;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews iov[0].iov_base = (void *)(dev->region.base + dev->n);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews iov[0].iov_len = read_count;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews iovcount = 1;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews goto config;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews }
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley /*
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Multibuffer I/O.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Skip empty buffers.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews */
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews while (buffer != NULL) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews REQUIRE(ISC_BUFFER_VALID(buffer));
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews if (isc_buffer_availablelength(buffer) != 0)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews break;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews buffer = ISC_LIST_NEXT(buffer, link);
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson }
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson iovcount = 0;
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson while (buffer != NULL) {
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson INSIST(iovcount < MAXSCATTERGATHER_RECV);
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson isc_buffer_availableregion(buffer, &available);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews if (available.length > 0) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews iov[iovcount].iov_base = (void *)(available.base);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews iov[iovcount].iov_len = available.length;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews read_count += available.length;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley iovcount++;
3740b569ae76295b941d57a724a43beb75b533baBob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley buffer = ISC_LIST_NEXT(buffer, link);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley config:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * If needed, set up to receive that one extra byte. Note that
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * we know there is at least one iov left, since we stole it
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * at the top of this function.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ISC_NET_RECVOVERFLOW
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->type == isc_sockettype_udp) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iov[iovcount].iov_base = (void *)(&sock->overflow);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley iov[iovcount].iov_len = 1;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley iovcount++;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley#endif
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley msg->msg_iov = iov;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley msg->msg_iovlen = iovcount;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ISC_NET_BSD44MSGHDR
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington msg->msg_control = NULL;
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington msg->msg_controllen = 0;
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley msg->msg_flags = 0;
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley#if defined(USE_CMSG)
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington if (sock->type == isc_sockettype_udp) {
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington msg->msg_control = sock->recvcmsgbuf;
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington msg->msg_controllen = sock->recvcmsgbuflen;
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington }
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington#endif /* USE_CMSG */
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington#else /* ISC_NET_BSD44MSGHDR */
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington msg->msg_accrights = NULL;
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington msg->msg_accrightslen = 0;
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington#endif /* ISC_NET_BSD44MSGHDR */
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley if (read_countp != NULL)
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley *read_countp = read_count;
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley}
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellingtonstatic void
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellingtonset_dev_address(isc_sockaddr_t *address, isc_socket_t *sock,
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_socketevent_t *dev)
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley{
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley if (sock->type == isc_sockettype_udp) {
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley if (address != NULL)
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley dev->address = *address;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley else
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley dev->address = sock->peer_address;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley } else if (sock->type == isc_sockettype_tcp) {
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley INSIST(address == NULL);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley dev->address = sock->peer_address;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley }
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley}
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halleystatic void
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halleydestroy_socketevent(isc_event_t *event) {
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_socketevent_t *ev = (isc_socketevent_t *)event;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley INSIST(ISC_LIST_EMPTY(ev->bufferlist));
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley (ev->destroy)(event);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley}
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halleystatic isc_socketevent_t *
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halleyallocate_socketevent(isc_socket_t *sock, isc_eventtype_t eventtype,
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_taskaction_t action, const void *arg)
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley{
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_socketevent_t *ev;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ev = (isc_socketevent_t *)isc_event_allocate(sock->manager->mctx,
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley sock, eventtype,
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley action, arg,
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley sizeof(*ev));
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley if (ev == NULL)
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley return (NULL);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ev->result = ISC_R_UNEXPECTED;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ISC_LINK_INIT(ev, ev_link);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ISC_LIST_INIT(ev->bufferlist);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ev->region.base = NULL;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ev->n = 0;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ev->offset = 0;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ev->attributes = 0;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ev->destroy = ev->ev_destroy;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev->ev_destroy = destroy_socketevent;
3740b569ae76295b941d57a724a43beb75b533baBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ev);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#if defined(ISC_SOCKET_DEBUG)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydump_msg(struct msghdr *msg) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int i;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley printf("MSGHDR %p\n", msg);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley printf("\tname %p, namelen %d\n", msg->msg_name, msg->msg_namelen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley printf("\tiov %p, iovlen %d\n", msg->msg_iov, msg->msg_iovlen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
3740b569ae76295b941d57a724a43beb75b533baBob Halley printf("\t\t%d\tbase %p, len %d\n", i,
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley msg->msg_iov[i].iov_base,
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley msg->msg_iov[i].iov_len);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#ifdef ISC_NET_BSD44MSGHDR
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley printf("\tcontrol %p, controllen %d\n", msg->msg_control,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley msg->msg_controllen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DOIO_SUCCESS 0 /* i/o ok, event sent */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DOIO_HARD 2 /* i/o error, event sent */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#define DOIO_EOF 3 /* EOF, no event sent */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic int
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydoio_recv(isc_socket_t *sock, isc_socketevent_t *dev) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley int cc;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley struct iovec iov[MAXSCATTERGATHER_RECV];
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley size_t read_count;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley size_t actual_count;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley struct msghdr msghdr;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_buffer_t *buffer;
3740b569ae76295b941d57a724a43beb75b533baBob Halley int recv_errno;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley char strbuf[ISC_STRERRORSIZE];
3740b569ae76295b941d57a724a43beb75b533baBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley build_msghdr_recv(sock, dev, &msghdr, iov, &read_count);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#if defined(ISC_SOCKET_DEBUG)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dump_msg(&msghdr);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cc = recvmsg(sock->fd, &msghdr, 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley recv_errno = errno;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#if defined(ISC_SOCKET_DEBUG)
0370190a682a3933119784a1023b1b9a51c66fa3Bob Halley dump_msg(&msghdr);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (cc < 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (SOFT_ERROR(recv_errno))
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley return (DOIO_SOFT);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc__strerror(recv_errno, strbuf, sizeof(strbuf));
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley socket_log(sock, NULL, IOEVENT,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_msgcat, ISC_MSGSET_SOCKET,
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley ISC_MSG_DOIORECV,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley "doio_recv: recvmsg(%d) %d bytes, err %d/%s",
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley sock->fd, cc, recv_errno, strbuf);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley#define SOFT_OR_HARD(_system, _isc) \
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (recv_errno == _system) { \
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (sock->connected) { \
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley dev->result = _isc; \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (DOIO_HARD); \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } \
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (DOIO_SOFT); \
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley#define ALWAYS_HARD(_system, _isc) \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (recv_errno == _system) { \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev->result = _isc; \
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley return (DOIO_HARD); \
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley }
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley SOFT_OR_HARD(EHOSTDOWN, ISC_R_HOSTDOWN);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley /* HPUX 11.11 can return EADDRNOTAVAIL. */
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley SOFT_OR_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley#undef SOFT_OR_HARD
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley#undef ALWAYS_HARD
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
fff9a37a0355e4181b3132370a173d1dbb5a0f3cBob Halley dev->result = isc__errno2result(recv_errno);
fff9a37a0355e4181b3132370a173d1dbb5a0f3cBob Halley return (DOIO_HARD);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley }
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley /*
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley * On TCP, zero length reads indicate EOF, while on
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley * UDP, zero length reads are perfectly valid, although
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley * strange.
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley */
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley if ((sock->type == isc_sockettype_tcp) && (cc == 0))
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley return (DOIO_EOF);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->type == isc_sockettype_udp) {
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley dev->address.length = msghdr.msg_namelen;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (isc_sockaddr_getport(&dev->address) == 0) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews socket_log(sock, &dev->address, IOEVENT,
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews isc_msgcat, ISC_MSGSET_SOCKET,
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley ISC_MSG_ZEROPORT,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "dropping source port zero packet");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (DOIO_SOFT);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley socket_log(sock, &dev->address, IOEVENT,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PKTRECV,
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley "packet received correctly");
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews /*
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews * Overflow bit detection. If we received MORE bytes than we should,
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews * this indicates an overflow situation. Set the flag in the
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews * dev entry and adjust how much we read by one.
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews */
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews#ifdef ISC_NET_RECVOVERFLOW
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) {
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews cc--;
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews }
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews#endif
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * If there are control messages attached, run through them and pull
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * out the interesting bits.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews if (sock->type == isc_sockettype_udp)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley process_cmsg(sock, &msghdr, dev);
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews /*
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews * update the buffers (if any) and the i/o count
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews */
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews dev->n += cc;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley actual_count = cc;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley buffer = ISC_LIST_HEAD(dev->bufferlist);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (buffer != NULL && actual_count > 0U) {
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley REQUIRE(ISC_BUFFER_VALID(buffer));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (isc_buffer_availablelength(buffer) <= actual_count) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley actual_count -= isc_buffer_availablelength(buffer);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_buffer_add(buffer,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_buffer_availablelength(buffer));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_buffer_add(buffer, actual_count);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley actual_count = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley break;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley buffer = ISC_LIST_NEXT(buffer, link);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (buffer == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(actual_count == 0U);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * If we read less than we expected, update counters,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * and let the upper layer poke the descriptor.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (((size_t)cc != read_count) && (dev->n < dev->minimum))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (DOIO_SOFT);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley /*
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * Full reads are posted, or partials if partials are ok.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dev->result = ISC_R_SUCCESS;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (DOIO_SUCCESS);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Returns:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * DOIO_SUCCESS The operation succeeded. dev->result contains
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * ISC_R_SUCCESS.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * DOIO_HARD A hard or unexpected I/O error was encountered.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * dev->result contains the appropriate error.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * DOIO_SOFT A soft I/O error was encountered. No senddone
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * event was sent. The operation should be retried.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * No other return values are possible.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley */
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic int
ce3761f64d3d734cc94605026985898900ecc474Bob Halleydoio_send(isc_socket_t *sock, isc_socketevent_t *dev) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley int cc;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley struct iovec iov[MAXSCATTERGATHER_SEND];
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley size_t write_count;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley struct msghdr msghdr;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley char addrbuf[ISC_SOCKADDR_FORMATSIZE];
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley int attempts = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int send_errno;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley char strbuf[ISC_STRERRORSIZE];
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley build_msghdr_send(sock, dev, &msghdr, iov, &write_count);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley resend:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cc = sendmsg(sock->fd, &msghdr, 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley send_errno = errno;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Check for error or block condition.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (cc < 0) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (send_errno == EINTR && ++attempts < NRETRIES)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto resend;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley if (SOFT_ERROR(send_errno))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (DOIO_SOFT);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define SOFT_OR_HARD(_system, _isc) \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (send_errno == _system) { \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->connected) { \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev->result = _isc; \
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (DOIO_HARD); \
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley } \
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (DOIO_SOFT); \
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#define ALWAYS_HARD(_system, _isc) \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (send_errno == _system) { \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev->result = _isc; \
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley return (DOIO_HARD); \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ALWAYS_HARD(EACCES, ISC_R_NOPERM);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ALWAYS_HARD(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ALWAYS_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ALWAYS_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef EHOSTDOWN
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ALWAYS_HARD(EHOSTDOWN, ISC_R_HOSTUNREACH);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ALWAYS_HARD(ENETUNREACH, ISC_R_NETUNREACH);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ALWAYS_HARD(EPERM, ISC_R_HOSTUNREACH);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ALWAYS_HARD(EPIPE, ISC_R_NOTCONNECTED);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ALWAYS_HARD(ECONNRESET, ISC_R_CONNECTIONRESET);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#undef SOFT_OR_HARD
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#undef ALWAYS_HARD
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The other error types depend on whether or not the
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * socket is UDP or TCP. If it is UDP, some errors
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley * that we expect to be fatal under TCP are merely
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * annoying, and are really soft errors.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * However, these soft errors are still returned as
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * a status.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_sockaddr_format(&dev->address, addrbuf, sizeof(addrbuf));
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews isc__strerror(send_errno, strbuf, sizeof(strbuf));
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__, "internal_send: %s: %s",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley addrbuf, strbuf);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev->result = isc__errno2result(send_errno);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (DOIO_HARD);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley if (cc == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "internal_send: send() %s 0",
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSG_RETURNED, "returned"));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * If we write less than we expected, update counters, poke.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev->n += cc;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if ((size_t)cc != write_count)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (DOIO_SOFT);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Exactly what we wanted to write. We're done with this
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * entry. Post its completion event.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev->result = ISC_R_SUCCESS;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (DOIO_SUCCESS);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley/*
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * Kill.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Caller must ensure that the socket is not locked and no external
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * references exist.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydestroy(isc_socket_t **sockp) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_t *sock = *sockp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socketmgr_t *manager = sock->manager;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSG_DESTROYING, "destroying");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(ISC_LIST_EMPTY(sock->accept_list));
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley INSIST(ISC_LIST_EMPTY(sock->recv_list));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(ISC_LIST_EMPTY(sock->send_list));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(sock->connect_ev == NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley REQUIRE(sock->fd >= 0 && sock->fd < (int)FD_SETSIZE);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley LOCK(&manager->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * No one has this socket open, so the watcher doesn't have to be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * poked, and the socket doesn't have to be locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley manager->fds[sock->fd] = NULL;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley if (sock->type == isc_sockettype_fdwatch)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley manager->fdstate[sock->fd] = MANAGER_CLOSE_PENDING;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley else
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley manager->fdstate[sock->fd] = CLOSE_PENDING;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley select_poke(manager, sock->fd, SELECT_POKE_CLOSE);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_UNLINK(manager->socklist, sock, link);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
af3459443207253f197cd684567bfea0311ab76dMark Andrews#ifdef ISC_PLATFORM_USETHREADS
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (ISC_LIST_EMPTY(manager->socklist))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley SIGNAL(&manager->shutdown_ok);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif /* ISC_PLATFORM_USETHREADS */
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * XXX should reset manager->maxfd here
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley UNLOCK(&manager->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_socket(sockp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic isc_result_t
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyallocate_socket(isc_socketmgr_t *manager, isc_sockettype_t type,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_t **socketp)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley{
af3459443207253f197cd684567bfea0311ab76dMark Andrews isc_socket_t *sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_result_t result;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_SOCKADDR_LEN_T cmsgbuflen;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock = isc_mem_get(manager->mctx, sizeof(*sock));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ISC_R_NOMEMORY);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = ISC_R_UNEXPECTED;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->references = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->manager = manager;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley sock->type = type;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock->fd = -1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK_INIT(sock, link);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->recvcmsgbuf = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->sendcmsgbuf = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * set up cmsg buffers
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley cmsgbuflen = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#if defined(USE_CMSG) && defined(SO_TIMESTAMP)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley cmsgbuflen += cmsg_space(sizeof(struct timeval));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->recvcmsgbuflen = cmsgbuflen;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->recvcmsgbuflen != 0U) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->recvcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (sock->recvcmsgbuf == NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto error;
af3459443207253f197cd684567bfea0311ab76dMark Andrews }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cmsgbuflen = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock->sendcmsgbuflen = cmsgbuflen;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (sock->sendcmsgbuflen != 0U) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->sendcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->sendcmsgbuf == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto error;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * set up list of readers and writers to be initially empty
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(sock->recv_list);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(sock->send_list);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(sock->accept_list);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->connect_ev = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->pending_recv = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->pending_send = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->pending_accept = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->listener = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->connected = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->connecting = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->bound = 0;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * initialize the lock
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley result = isc_mutex_init(&sock->lock);
c7dd70fe1b0d3815ce267a575450662b8ee289b6Bob Halley if (result != ISC_R_SUCCESS) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto error;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Initialize readable and writable events
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence ISC_EVENT_INIT(&sock->readable_ev, sizeof(intev_t),
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTR,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence NULL, sock, sock, NULL, NULL);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence ISC_EVENT_INIT(&sock->writable_ev, sizeof(intev_t),
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTW,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence NULL, sock, sock, NULL, NULL);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence sock->magic = SOCKET_MAGIC;
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence *socketp = sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ISC_R_SUCCESS);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley error:
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley if (sock->recvcmsgbuf != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mem_put(manager->mctx, sock->recvcmsgbuf,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->recvcmsgbuflen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->sendcmsgbuf != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mem_put(manager->mctx, sock->sendcmsgbuf,
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley sock->sendcmsgbuflen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mem_put(manager->mctx, sock, sizeof(*sock));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This event requires that the various lists be empty, that the reference
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * count be 1, and that the magic number is valid. The other socket bits,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * like the lock, must be initialized as well. The fd associated must be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * marked as closed, by setting it to -1 on close, or this routine will
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * also close the socket.
af3459443207253f197cd684567bfea0311ab76dMark Andrews */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_socket(isc_socket_t **socketp) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_t *sock = *socketp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(sock->references == 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(VALID_SOCKET(sock));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!sock->connecting);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!sock->pending_recv);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!sock->pending_send);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!sock->pending_accept);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(ISC_LIST_EMPTY(sock->recv_list));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(ISC_LIST_EMPTY(sock->send_list));
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley INSIST(ISC_LIST_EMPTY(sock->accept_list));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!ISC_LINK_LINKED(sock, link));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->recvcmsgbuf != NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_mem_put(sock->manager->mctx, sock->recvcmsgbuf,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->recvcmsgbuflen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->sendcmsgbuf != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mem_put(sock->manager->mctx, sock->sendcmsgbuf,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->sendcmsgbuflen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DESTROYLOCK(&sock->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_mem_put(sock->manager->mctx, sock, sizeof(*sock));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *socketp = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Create a new 'type' socket managed by 'manager'. Events
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * will be posted to 'task' and when dispatched 'action' will be
af3459443207253f197cd684567bfea0311ab76dMark Andrews * called with 'arg' as the arg value. The new socket is returned
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * in 'socketp'.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyisc_result_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyisc_socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_t **socketp)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_t *sock = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_result_t result;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#if defined(USE_CMSG) || defined(SO_BSDCOMPAT)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int on = 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#if defined(SO_RCVBUF)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_SOCKADDR_LEN_T optlen;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int size;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley char strbuf[ISC_STRERRORSIZE];
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley const char *err = "socket";
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley REQUIRE(VALID_MANAGER(manager));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(socketp != NULL && *socketp == NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = allocate_socket(manager, type, &sock);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence if (result != ISC_R_SUCCESS)
c3e95f11e02c51fc786b9124c817ab72dda3084cBob Halley return (result);
c3e95f11e02c51fc786b9124c817ab72dda3084cBob Halley
c3e95f11e02c51fc786b9124c817ab72dda3084cBob Halley sock->pf = pf;
c3e95f11e02c51fc786b9124c817ab72dda3084cBob Halley switch (type) {
c3e95f11e02c51fc786b9124c817ab72dda3084cBob Halley case isc_sockettype_udp:
c3e95f11e02c51fc786b9124c817ab72dda3084cBob Halley sock->fd = socket(pf, SOCK_DGRAM, IPPROTO_UDP);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley break;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case isc_sockettype_tcp:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->fd = socket(pf, SOCK_STREAM, IPPROTO_TCP);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley break;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case isc_sockettype_unix:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->fd = socket(pf, SOCK_STREAM, 0);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley break;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case isc_sockettype_fdwatch:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(type != isc_sockettype_fdwatch);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley break;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef F_DUPFD
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Leave a space for stdio to work in.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->fd >= 0 && sock->fd < 20) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int new, tmp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley new = fcntl(sock->fd, F_DUPFD, 20);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley tmp = errno;
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence (void)close(sock->fd);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence errno = tmp;
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence sock->fd = new;
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence err = "isc_socket_create: fcntl";
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence }
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence#endif
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence if (sock->fd >= (int)FD_SETSIZE) {
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence (void)close(sock->fd);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence isc_msgcat, ISC_MSGSET_SOCKET,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence ISC_MSG_TOOMANYFDS,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence "%s: too many open file descriptors", "socket");
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence free_socket(&sock);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence return (ISC_R_NORESOURCES);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence }
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence if (sock->fd < 0) {
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence free_socket(&sock);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence switch (errno) {
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence case EMFILE:
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence case ENFILE:
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence case ENOBUFS:
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence return (ISC_R_NORESOURCES);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence case EPROTONOSUPPORT:
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence case EPFNOSUPPORT:
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley case EAFNOSUPPORT:
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence /*
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence * Linux 2.2 (and maybe others) return EINVAL instead of
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence * EAFNOSUPPORT.
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence */
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence case EINVAL:
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley return (ISC_R_FAMILYNOSUPPORT);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley default:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "%s() %s: %s", err,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_msgcat_get(isc_msgcat,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSGSET_GENERAL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSG_FAILED,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "failed"),
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley strbuf);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ISC_R_UNEXPECTED);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (make_nonblock(sock->fd) != ISC_R_SUCCESS) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void)close(sock->fd);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_socket(&sock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ISC_R_UNEXPECTED);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef SO_BSDCOMPAT
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (type != isc_sockettype_unix &&
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley setsockopt(sock->fd, SOL_SOCKET, SO_BSDCOMPAT,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void *)&on, sizeof(on)) < 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "setsockopt(%d, SO_BSDCOMPAT) %s: %s",
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence sock->fd,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence ISC_MSG_FAILED, "failed"),
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence strbuf);
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence /* Press on... */
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence }
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence#endif
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence#if defined(USE_CMSG) || defined(SO_RCVBUF)
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence if (type == isc_sockettype_udp) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#if defined(USE_CMSG)
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence#if defined(SO_TIMESTAMP)
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence if (setsockopt(sock->fd, SOL_SOCKET, SO_TIMESTAMP,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence (void *)&on, sizeof(on)) < 0
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence && errno != ENOPROTOOPT) {
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence isc__strerror(errno, strbuf, sizeof(strbuf));
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
213a9ec2addc5bcf3b168fe507312e7b25960e0bDavid Lawrence "setsockopt(%d, SO_TIMESTAMP) %s: %s",
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock->fd,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_msgcat_get(isc_msgcat,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSGSET_GENERAL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSG_FAILED,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "failed"),
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley strbuf);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /* Press on... */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif /* SO_TIMESTAMP */
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#if defined(ISC_PLATFORM_HAVEIPV6)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (pf == AF_INET6 && sock->recvcmsgbuflen == 0U) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Warn explicitly because this anomaly can be hidden
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * in usual operation (and unexpectedly appear later).
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley "No buffer available to receive "
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley "IPv6 destination");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#ifdef IPV6_RECVPKTINFO
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /* 2292bis */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if ((pf == AF_INET6)
af3459443207253f197cd684567bfea0311ab76dMark Andrews && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void *)&on, sizeof(on)) < 0)) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "setsockopt(%d, IPV6_RECVPKTINFO) "
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "%s: %s", sock->fd,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_msgcat_get(isc_msgcat,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSGSET_GENERAL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSG_FAILED,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "failed"),
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley strbuf);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /* 2292 */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if ((pf == AF_INET6)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void *)&on, sizeof(on)) < 0)) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley "setsockopt(%d, IPV6_PKTINFO) %s: %s",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->fd,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_msgcat_get(isc_msgcat,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSGSET_GENERAL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSG_FAILED,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley "failed"),
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley strbuf);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif /* IPV6_RECVPKTINFO */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#ifdef IPV6_USE_MIN_MTU /*2292bis, not too common yet*/
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /* use minimum MTU */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (pf == AF_INET6) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void)setsockopt(sock->fd, IPPROTO_IPV6,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley IPV6_USE_MIN_MTU,
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews (void *)&on, sizeof(on));
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#endif /* ISC_PLATFORM_HAVEIPV6 */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#endif /* defined(USE_CMSG) */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#if defined(SO_RCVBUF)
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley optlen = sizeof(size);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void *)&size, &optlen) >= 0 &&
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley size < RCVBUFSIZE) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley size = RCVBUFSIZE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void *)&size, sizeof(size)) == -1) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "setsockopt(%d, SO_RCVBUF, %d) %s: %s",
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley sock->fd, size,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_msgcat_get(isc_msgcat,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ISC_MSGSET_GENERAL,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ISC_MSG_FAILED,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley "failed"),
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley strbuf);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley#endif
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley#endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley sock->references = 1;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley *socketp = sock;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley LOCK(&manager->lock);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley /*
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * Note we don't have to lock the socket like we normally would because
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * there are no external references to it yet.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley manager->fds[sock->fd] = sock;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley manager->fdstate[sock->fd] = MANAGED;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ISC_LIST_APPEND(manager->socklist, sock, link);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (manager->maxfd < sock->fd)
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley manager->maxfd = sock->fd;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley UNLOCK(&manager->lock);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ISC_MSG_CREATED, "created");
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley return (ISC_R_SUCCESS);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Create a new 'type' socket managed by 'manager'. Events
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * will be posted to 'task' and when dispatched 'action' will be
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * called with 'arg' as the arg value. The new socket is returned
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * in 'socketp'.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyisc_result_t
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halleyisc_socket_fdwatchcreate(isc_socketmgr_t *manager, int fd, int flags,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_sockfdwatch_t callback, void *cbarg,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_t *task, isc_socket_t **socketp)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_t *sock = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_result_t result;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley REQUIRE(VALID_MANAGER(manager));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(socketp != NULL && *socketp == NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = allocate_socket(manager, isc_sockettype_fdwatch, &sock);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (result != ISC_R_SUCCESS)
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley return (result);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->fd = fd;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->fdwatcharg = cbarg;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->fdwatchcb = callback;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->fdwatchflags = flags;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->fdwatchtask = task;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->references = 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *socketp = sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley LOCK(&manager->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Note we don't have to lock the socket like we normally would because
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * there are no external references to it yet.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley manager->fds[sock->fd] = sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley manager->fdstate[sock->fd] = MANAGED;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_APPEND(manager->socklist, sock, link);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (manager->maxfd < sock->fd)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley manager->maxfd = sock->fd;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&manager->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (flags & ISC_SOCKFDWATCH_READ)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (flags & ISC_SOCKFDWATCH_WRITE)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley ISC_MSG_CREATED, "fdwatch-created");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ISC_R_SUCCESS);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Attach to a socket. Caller must explicitly detach when it is done.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyisc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(VALID_SOCKET(sock));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(socketp != NULL && *socketp == NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&sock->lock);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley sock->references++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&sock->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *socketp = sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Dereference a socket. If this is the last reference to it, clean things
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * up by destroying the socket.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyisc_socket_detach(isc_socket_t **socketp) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_t *sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_boolean_t kill_socket = ISC_FALSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(socketp != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock = *socketp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(VALID_SOCKET(sock));
b9a2c5ddf99250e851f45a606cdbe3e3fc4beccaBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&sock->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(sock->references > 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->references--;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->references == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley kill_socket = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&sock->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (kill_socket)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley destroy(&sock);
b9a2c5ddf99250e851f45a606cdbe3e3fc4beccaBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *socketp = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * I/O is possible on a given socket. Schedule an event to this task that
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * will call an internal function to do the I/O. This will charge the
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * task with the I/O operation and let our select loop handler get back
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * to doing something real as fast as possible.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The socket and manager must be locked before calling this function.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydispatch_recv(isc_socket_t *sock) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley intev_t *iev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socketevent_t *ev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_t *sender;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!sock->pending_recv);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->type != isc_sockettype_fdwatch) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev = ISC_LIST_HEAD(sock->recv_list);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (ev == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley socket_log(sock, NULL, EVENT, NULL, 0, 0,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "dispatch_recv: event %p -> task %p",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev, ev->ev_sender);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sender = ev->ev_sender;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sender = sock->fdwatchtask;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->pending_recv = 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev = &sock->readable_ev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->references++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev->ev_sender = sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->type == isc_sockettype_fdwatch)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev->ev_action = internal_fdwatch_read;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev->ev_action = internal_recv;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev->ev_arg = sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_send(sender, (isc_event_t **)&iev);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydispatch_send(isc_socket_t *sock) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley intev_t *iev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socketevent_t *ev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_t *sender;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!sock->pending_send);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->type != isc_sockettype_fdwatch) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev = ISC_LIST_HEAD(sock->send_list);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (ev == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley socket_log(sock, NULL, EVENT, NULL, 0, 0,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "dispatch_send: event %p -> task %p",
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ev, ev->ev_sender);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sender = ev->ev_sender;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley } else {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sender = sock->fdwatchtask;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->pending_send = 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev = &sock->writable_ev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->references++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev->ev_sender = sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (sock->type == isc_sockettype_fdwatch)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev->ev_action = internal_fdwatch_write;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev->ev_action = internal_send;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley iev->ev_arg = sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_send(sender, (isc_event_t **)&iev);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Dispatch an internal accept event.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleydispatch_accept(isc_socket_t *sock) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley intev_t *iev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_newconnev_t *ev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!sock->pending_accept);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley /*
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * Are there any done events left, or were they all canceled
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * before the manager got the socket lock?
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley */
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington ev = ISC_LIST_HEAD(sock->accept_list);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (ev == NULL)
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington return;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley sock->pending_accept = 1;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley iev = &sock->readable_ev;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley sock->references++; /* keep socket around for this internal event */
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington iev->ev_sender = sock;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley iev->ev_action = internal_accept;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley iev->ev_arg = sock;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley}
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleystatic void
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleydispatch_connect(isc_socket_t *sock) {
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley intev_t *iev;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_socket_connev_t *ev;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley iev = &sock->writable_ev;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley ev = sock->connect_ev;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley INSIST(ev != NULL); /* XXX */
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley INSIST(sock->connecting);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley sock->references++; /* keep socket around for this internal event */
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley iev->ev_sender = sock;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley iev->ev_action = internal_connect;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley iev->ev_arg = sock;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington}
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington/*
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * Dequeue an item off the given socket's read queue, set the result code
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * in the done event to the one provided, and send it to the task it was
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * destined for.
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington *
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * If the event to be sent is on a list, remove it before sending. If
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * asked to, send and detach from the socket as well.
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley *
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * Caller must have the socket locked if the event is attached to the socket.
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington */
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellingtonstatic void
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellingtonsend_recvdone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington isc_task_t *task;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley task = (*dev)->ev_sender;
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington (*dev)->ev_sender = sock;
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington if (ISC_LINK_LINKED(*dev, ev_link))
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington ISC_LIST_DEQUEUE(sock->recv_list, *dev, ev_link);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington == ISC_SOCKEVENTATTR_ATTACHED)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_task_sendanddetach(&task, (isc_event_t **)dev);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington else
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_task_send(task, (isc_event_t **)dev);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley}
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley/*
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * See comments for send_recvdone_event() above.
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington *
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * Caller must have the socket locked if the event is attached to the socket.
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington */
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellingtonstatic void
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellingtonsend_senddone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_task_t *task;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley INSIST(dev != NULL && *dev != NULL);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley task = (*dev)->ev_sender;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley (*dev)->ev_sender = sock;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (ISC_LINK_LINKED(*dev, ev_link))
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley ISC_LIST_DEQUEUE(sock->send_list, *dev, ev_link);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley == ISC_SOCKEVENTATTR_ATTACHED)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_task_sendanddetach(&task, (isc_event_t **)dev);
a0abd77baacdd97272356faab5efc79dd1483077Bob Halley else
a0abd77baacdd97272356faab5efc79dd1483077Bob Halley isc_task_send(task, (isc_event_t **)dev);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley}
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Call accept() on a socket, to get the new file descriptor. The listen
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * socket is used as a prototype to create a new isc_socket_t. The new
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * socket has one outstanding reference. The task receiving the event
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * will be detached from just after the event is delivered.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * On entry to this function, the event delivered is the internal
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * readable event, and the first item on the accept_list should be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * the done event we want to send. If the list is empty, this is a no-op,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * so just unlock and return.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyinternal_accept(isc_task_t *me, isc_event_t *ev) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socket_t *sock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_socketmgr_t *manager;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_socket_newconnev_t *dev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_t *task;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_SOCKADDR_LEN_T addrlen;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int fd;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_result_t result = ISC_R_SUCCESS;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley char strbuf[ISC_STRERRORSIZE];
566f721b3a0a4e63e7da775a0fbcc2c44645cae4Mark Andrews const char *err = "accept";
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNUSED(me);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock = ev->ev_sender;
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley INSIST(VALID_SOCKET(sock));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&sock->lock);
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley socket_log(sock, NULL, TRACE,
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley "internal_accept called, locked socket");
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley manager = sock->manager;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(VALID_MANAGER(manager));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(sock->listener);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(sock->pending_accept == 1);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->pending_accept = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(sock->references > 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->references--; /* the internal event is done with this socket */
566f721b3a0a4e63e7da775a0fbcc2c44645cae4Mark Andrews if (sock->references == 0) {
566f721b3a0a4e63e7da775a0fbcc2c44645cae4Mark Andrews UNLOCK(&sock->lock);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley destroy(&sock);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley return;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley /*
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * Get the first item off the accept list.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * If it is empty, unlock the socket and return.
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev = ISC_LIST_HEAD(sock->accept_list);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (dev == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&sock->lock);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Try to accept the new connection. If the accept fails with
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * EAGAIN or EINTR, simply poke the watcher to watch this socket
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * again. Also ignore ECONNRESET, which has been reported to
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * be spuriously returned on Linux 2.2.19 although it is not
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * a documented error for accept(). ECONNABORTED has been
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * reported for Solaris 8. The rest are thrown in not because
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * we have seen them but because they are ignored by other
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * deamons such as BIND 8 and Apache.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley addrlen = sizeof(dev->newsocket->peer_address.type);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley memset(&dev->newsocket->peer_address.type.sa, 0, addrlen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley fd = accept(sock->fd, &dev->newsocket->peer_address.type.sa,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void *)&addrlen);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#ifdef F_DUPFD
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Leave a space for stdio to work in.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (fd >= 0 && fd < 20) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int new, tmp;
ed7f35a9f73f9895be856d7861fe9752cb52e3b7Bob Halley new = fcntl(fd, F_DUPFD, 20);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley tmp = errno;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void)close(fd);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley errno = tmp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley fd = new;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley err = "fcntl";
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (fd < 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (SOFT_ERROR(errno))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto soft_error;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley switch (errno) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case ENOBUFS:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case ENFILE:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case ENOMEM:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case ECONNRESET:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case ECONNABORTED:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case EHOSTUNREACH:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case EHOSTDOWN:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case ENETUNREACH:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case ENETDOWN:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case ECONNREFUSED:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef EPROTO
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case EPROTO:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#ifdef ENONET
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case ENONET:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto soft_error;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley default:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley break;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc__strerror(errno, strbuf, sizeof(strbuf));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "internal_accept: %s() %s: %s", err,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_msgcat_get(isc_msgcat,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSGSET_GENERAL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSG_FAILED,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "failed"),
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley strbuf);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley fd = -1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = ISC_R_UNEXPECTED;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (addrlen == 0U) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "internal_accept(): "
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "accept() failed to return "
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "remote address");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void)close(fd);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto soft_error;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else if (dev->newsocket->peer_address.type.sa.sa_family !=
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->pf)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "internal_accept(): "
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "accept() returned peer address "
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "family %u (expected %u)",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev->newsocket->peer_address.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley type.sa.sa_family,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley sock->pf);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void)close(fd);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto soft_error;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else if (fd >= (int)FD_SETSIZE) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_msgcat, ISC_MSGSET_SOCKET,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_MSG_TOOMANYFDS,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "%s: too many open file descriptors",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "accept");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void)close(fd);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto soft_error;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (fd != -1) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev->newsocket->peer_address.length = addrlen;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev->newsocket->pf = sock->pf;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Pull off the done event.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_UNLINK(sock->accept_list, dev, ev_link);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Poke watcher if there are more pending accepts.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (!ISC_LIST_EMPTY(sock->accept_list))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&sock->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (fd != -1 && (make_nonblock(fd) != ISC_R_SUCCESS)) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (void)close(fd);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley fd = -1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = ISC_R_UNEXPECTED;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * -1 means the new socket didn't happen.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (fd != -1) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&manager->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_APPEND(manager->socklist, dev->newsocket, link);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
ed7f35a9f73f9895be856d7861fe9752cb52e3b7Bob Halley dev->newsocket->fd = fd;
ed7f35a9f73f9895be856d7861fe9752cb52e3b7Bob Halley dev->newsocket->bound = 1;
ed7f35a9f73f9895be856d7861fe9752cb52e3b7Bob Halley dev->newsocket->connected = 1;
ed7f35a9f73f9895be856d7861fe9752cb52e3b7Bob Halley
ed7f35a9f73f9895be856d7861fe9752cb52e3b7Bob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Save away the remote address
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dev->address = dev->newsocket->peer_address;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
566f721b3a0a4e63e7da775a0fbcc2c44645cae4Mark Andrews manager->fds[fd] = dev->newsocket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley manager->fdstate[fd] = MANAGED;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (manager->maxfd < fd)
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff manager->maxfd = fd;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley socket_log(sock, &dev->newsocket->peer_address, CREATION,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTEDCXN,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley "accepted connection, new socket %p",
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev->newsocket);
a0abd77baacdd97272356faab5efc79dd1483077Bob Halley
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence UNLOCK(&manager->lock);
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence } else {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev->newsocket->references--;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley free_socket(&dev->newsocket);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Fill in the done event details and send it off.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev->result = result;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley task = dev->ev_sender;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev->ev_sender = sock;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
ce3761f64d3d734cc94605026985898900ecc474Bob Halley return;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley soft_error:
ce3761f64d3d734cc94605026985898900ecc474Bob Halley select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&sock->lock);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley return;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic void
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleyinternal_recv(isc_task_t *me, isc_event_t *ev) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_socketevent_t *dev;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_socket_t *sock;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock = ev->ev_sender;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(VALID_SOCKET(sock));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley socket_log(sock, NULL, IOEVENT,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley "internal_recv: task %p got event %p", me, ev);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(sock->pending_recv == 1);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock->pending_recv = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(sock->references > 0);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock->references--; /* the internal event is done with this socket */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (sock->references == 0) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley destroy(&sock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Try to do as much I/O as possible on this socket. There are no
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * limits here, currently.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev = ISC_LIST_HEAD(sock->recv_list);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley while (dev != NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley switch (doio_recv(sock, dev)) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley case DOIO_SOFT:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto poke;
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley case DOIO_EOF:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * read of 0 means the remote end was closed.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Run through the event queue and dispatch all
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * the events with an EOF result code.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley do {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev->result = ISC_R_EOF;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley send_recvdone_event(sock, &dev);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev = ISC_LIST_HEAD(sock->recv_list);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley } while (dev != NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto poke;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley case DOIO_SUCCESS:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley case DOIO_HARD:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley send_recvdone_event(sock, &dev);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley break;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews dev = ISC_LIST_HEAD(sock->recv_list);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews }
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence poke:
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews if (!ISC_LIST_EMPTY(sock->recv_list))
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley UNLOCK(&sock->lock);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley}
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halleystatic void
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halleyinternal_send(isc_task_t *me, isc_event_t *ev) {
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley isc_socketevent_t *dev;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_socket_t *sock;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Find out what socket this is and lock it.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock = (isc_socket_t *)ev->ev_sender;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley INSIST(VALID_SOCKET(sock));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley socket_log(sock, NULL, IOEVENT,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley "internal_send: task %p got event %p", me, ev);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley INSIST(sock->pending_send == 1);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley sock->pending_send = 0;
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley INSIST(sock->references > 0);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews sock->references--; /* the internal event is done with this socket */
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews if (sock->references == 0) {
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews UNLOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley destroy(&sock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Try to do as much I/O as possible on this socket. There are no
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * limits here, currently.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dev = ISC_LIST_HEAD(sock->send_list);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley while (dev != NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley switch (doio_send(sock, dev)) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley case DOIO_SOFT:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto poke;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley case DOIO_HARD:
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews case DOIO_SUCCESS:
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews send_senddone_event(sock, &dev);
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews break;
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews dev = ISC_LIST_HEAD(sock->send_list);
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews }
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews poke:
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews if (!ISC_LIST_EMPTY(sock->send_list))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic void
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrewsinternal_fdwatch_write(isc_task_t *me, isc_event_t *ev) {
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews isc_socket_t *sock;
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews int more_data;
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews /*
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews * Find out what socket this is and lock it.
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews */
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews sock = (isc_socket_t *)ev->ev_sender;
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews INSIST(VALID_SOCKET(sock));
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley socket_log(sock, NULL, IOEVENT,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley "internal_fdwatch_write: task %p got event %p", me, ev);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(sock->pending_send == 1);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley more_data = (sock->fdwatchcb)(me, sock, sock->fdwatcharg);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock->pending_send = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley INSIST(sock->references > 0);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley sock->references--; /* the internal event is done with this socket */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (sock->references == 0) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley destroy(&sock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (more_data)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyinternal_fdwatch_read(isc_task_t *me, isc_event_t *ev) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_socket_t *sock;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley int more_data;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * Find out what socket this is and lock it.
571469b0a58b011966bbea91ca992928554d6ca1David Lawrence */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews sock = (isc_socket_t *)ev->ev_sender;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews INSIST(VALID_SOCKET(sock));
86921103ec656d7a0176d356b5adac10a362d2d3Bob Halley
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews LOCK(&sock->lock);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews socket_log(sock, NULL, IOEVENT,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley "internal_fdwatch_read: task %p got event %p", me, ev);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(sock->pending_recv == 1);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley more_data = (sock->fdwatchcb)(me, sock, sock->fdwatcharg);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&sock->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley sock->pending_recv = 0;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews INSIST(sock->references > 0);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews sock->references--; /* the internal event is done with this socket */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (sock->references == 0) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews UNLOCK(&sock->lock);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews destroy(&sock);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews return;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews }
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (more_data)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews UNLOCK(&sock->lock);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews}
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrewsstatic void
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrewsprocess_fds(isc_socketmgr_t *manager, int maxfd,
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews fd_set *readfds, fd_set *writefds)
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews{
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews int i;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_socket_t *sock;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_boolean_t unlock_sock;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews REQUIRE(maxfd <= (int)FD_SETSIZE);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews /*
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * Process read/writes on other fds here. Avoid locking
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * and unlocking twice if both reads and writes are possible.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews for (i = 0; i < maxfd; i++) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews#ifdef ISC_PLATFORM_USETHREADS
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (i == manager->pipe_fds[0] || i == manager->pipe_fds[1])
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews continue;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews#endif /* ISC_PLATFORM_USETHREADS */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews /*
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * If we need to close the socket, do it now.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (manager->fdstate[i] == CLOSE_PENDING
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence || manager->fdstate[i] == MANAGER_CLOSE_PENDING) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews FD_CLR(i, &manager->read_fds);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews FD_CLR(i, &manager->write_fds);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (manager->fdstate[i] == CLOSE_PENDING)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews (void)close(i);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews manager->fdstate[i] = CLOSED;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews continue;
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence }
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews sock = manager->fds[i];
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews unlock_sock = ISC_FALSE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (FD_ISSET(i, readfds)) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (sock == NULL) {
518be7faab2498c795e6dc9bb25ac10ca38b3a8dMark Andrews FD_CLR(i, &manager->read_fds);
518be7faab2498c795e6dc9bb25ac10ca38b3a8dMark Andrews goto check_write;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley unlock_sock = ISC_TRUE;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley LOCK(&sock->lock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (!SOCK_DEAD(sock)) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (sock->listener)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley dispatch_accept(sock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley else
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley dispatch_recv(sock);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews }
ce3761f64d3d734cc94605026985898900ecc474Bob Halley FD_CLR(i, &manager->read_fds);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley check_write:
ce3761f64d3d734cc94605026985898900ecc474Bob Halley if (FD_ISSET(i, writefds)) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (sock == NULL) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley FD_CLR(i, &manager->write_fds);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley continue;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (!unlock_sock) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley unlock_sock = ISC_TRUE;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley LOCK(&sock->lock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (!SOCK_DEAD(sock)) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (sock->connecting)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley dispatch_connect(sock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley else
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley dispatch_send(sock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley FD_CLR(i, &manager->write_fds);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (unlock_sock)
ce3761f64d3d734cc94605026985898900ecc474Bob Halley UNLOCK(&sock->lock);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley}
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews#ifdef ISC_PLATFORM_USETHREADS
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley/*
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * This is the thread that will loop forever, always in a select or poll
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * call.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley *
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * When select returns something to do, track down what thread gets to do
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * this I/O and post the event to it.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley */
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halleystatic isc_threadresult_t
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halleywatcher(void *uap) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_socketmgr_t *manager = uap;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_boolean_t done;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley int ctlfd;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley int cc;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley fd_set readfds;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley fd_set writefds;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley int msg, fd;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley int maxfd;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley char strbuf[ISC_STRERRORSIZE];
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley /*
518be7faab2498c795e6dc9bb25ac10ca38b3a8dMark Andrews * Get the control fd here. This will never change.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley */
ce3761f64d3d734cc94605026985898900ecc474Bob Halley LOCK(&manager->lock);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley ctlfd = manager->pipe_fds[0];
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews done = ISC_FALSE;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews while (!done) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews do {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley readfds = manager->read_fds;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley writefds = manager->write_fds;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley maxfd = manager->maxfd + 1;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley UNLOCK(&manager->lock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley cc = select(maxfd, &readfds, &writefds, NULL, NULL);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (cc < 0) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (!SOFT_ERROR(errno)) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc__strerror(errno, strbuf,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley sizeof(strbuf));
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley FATAL_ERROR(__FILE__, __LINE__,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley "select() %s: %s",
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_msgcat_get(isc_msgcat,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_MSGSET_GENERAL,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley ISC_MSG_FAILED,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley "failed"),
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley strbuf);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley LOCK(&manager->lock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley } while (cc < 0);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Process reads on internal, control fd.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley */
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (FD_ISSET(ctlfd, &readfds)) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley for (;;) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley select_readmsg(manager, &fd, &msg);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley manager_log(manager, IOEVENT,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_msgcat_get(isc_msgcat,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley ISC_MSGSET_SOCKET,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley ISC_MSG_WATCHERMSG,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley "watcher got message %d"
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley " for socket %d"),
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley msg, fd);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Nothing to read?
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley */
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (msg == SELECT_POKE_NOTHING)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley break;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Handle shutdown message. We really should
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * jump out of this loop right away, but
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * it doesn't matter if we have to do a little
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * more work first.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley */
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (msg == SELECT_POKE_SHUTDOWN) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley done = ISC_TRUE;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley break;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews }
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * This is a wakeup on a socket. Look
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * at the event queue for both read and write,
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * and decide if we need to watch on it now
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * or not.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley wakeup_socket(manager, fd, msg);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley process_fds(manager, maxfd, &readfds, &writefds);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley manager_log(manager, TRACE,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley ISC_MSG_EXITING, "watcher exiting"));
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley UNLOCK(&manager->lock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley return ((isc_threadresult_t)0);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley}
ce3761f64d3d734cc94605026985898900ecc474Bob Halley#endif /* ISC_PLATFORM_USETHREADS */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews/*
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * Create a new socket manager.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyisc_result_t
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrenceisc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_socketmgr_t *manager;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#ifdef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence char strbuf[ISC_STRERRORSIZE];
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_result_t result;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence REQUIRE(managerp != NULL && *managerp == NULL);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff#ifndef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (socketmgr != NULL) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence socketmgr->refs++;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence *managerp = socketmgr;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence return (ISC_R_SUCCESS);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager = isc_mem_get(mctx, sizeof(*manager));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (manager == NULL)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence return (ISC_R_NOMEMORY);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager->magic = SOCKET_MANAGER_MAGIC;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager->mctx = NULL;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley memset(manager->fds, 0, sizeof(manager->fds));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence ISC_LIST_INIT(manager->socklist);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence result = isc_mutex_init(&manager->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (result != ISC_R_SUCCESS) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_put(mctx, manager, sizeof(*manager));
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley return (result);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#ifdef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (isc_condition_init(&manager->shutdown_ok) != ISC_R_SUCCESS) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence DESTROYLOCK(&manager->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_put(mctx, manager, sizeof(*manager));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence "isc_condition_init() %s",
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence ISC_MSG_FAILED, "failed"));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence return (ISC_R_UNEXPECTED);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Create the special fds that will be used to wake up the
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * select/poll loop when something internal needs to be done.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (pipe(manager->pipe_fds) != 0) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence DESTROYLOCK(&manager->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_put(mctx, manager, sizeof(*manager));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc__strerror(errno, strbuf, sizeof(strbuf));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence "pipe() %s: %s",
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence ISC_MSG_FAILED, "failed"),
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence strbuf);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence return (ISC_R_UNEXPECTED);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence RUNTIME_CHECK(make_nonblock(manager->pipe_fds[0]) == ISC_R_SUCCESS);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#if 0
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence RUNTIME_CHECK(make_nonblock(manager->pipe_fds[1]) == ISC_R_SUCCESS);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#else /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager->refs = 1;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Set up initial state for the select loop
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence FD_ZERO(&manager->read_fds);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence FD_ZERO(&manager->write_fds);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#ifdef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence FD_SET(manager->pipe_fds[0], &manager->read_fds);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager->maxfd = manager->pipe_fds[0];
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#else /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager->maxfd = 0;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence memset(manager->fdstate, 0, sizeof(manager->fdstate));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#ifdef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Start up the select/poll thread.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (isc_thread_create(watcher, manager, &manager->watcher) !=
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence ISC_R_SUCCESS) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence (void)close(manager->pipe_fds[0]);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence (void)close(manager->pipe_fds[1]);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence DESTROYLOCK(&manager->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_put(mctx, manager, sizeof(*manager));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence "isc_thread_create() %s",
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence ISC_MSG_FAILED, "failed"));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence return (ISC_R_UNEXPECTED);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_attach(mctx, &manager->mctx);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#ifndef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence socketmgr = manager;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence *managerp = manager;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence return (ISC_R_SUCCESS);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence}
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrencevoid
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrenceisc_socketmgr_destroy(isc_socketmgr_t **managerp) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_socketmgr_t *manager;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence int i;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_t *mctx;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley * Destroy a socket manager.
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence REQUIRE(managerp != NULL);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager = *managerp;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence REQUIRE(VALID_MANAGER(manager));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#ifndef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (manager->refs > 1) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager->refs--;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence *managerp = NULL;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence return;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence LOCK(&manager->lock);
d92d2f722ad5a2063010a6720fe137ada7562e30David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#ifdef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Wait for all sockets to be destroyed.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence while (!ISC_LIST_EMPTY(manager->socklist)) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager_log(manager, CREATION,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence ISC_MSG_SOCKETSREMAIN,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence "sockets exist"));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence WAIT(&manager->shutdown_ok, &manager->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#else /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Hope all sockets have been destroyed.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (!ISC_LIST_EMPTY(manager->socklist)) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager_log(manager, CREATION,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence ISC_MSG_SOCKETSREMAIN,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence "sockets exist"));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence INSIST(0);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence UNLOCK(&manager->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Here, poke our select/poll thread. Do this by closing the write
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * half of the pipe, which will send EOF to the read half.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * This is currently a no-op in the non-threaded case.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence select_poke(manager, 0, SELECT_POKE_SHUTDOWN);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#ifdef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Wait for thread to exit.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (isc_thread_join(manager->watcher, NULL) != ISC_R_SUCCESS)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence "isc_thread_join() %s",
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence ISC_MSG_FAILED, "failed"));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Clean up.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#ifdef ISC_PLATFORM_USETHREADS
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence (void)close(manager->pipe_fds[0]);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence (void)close(manager->pipe_fds[1]);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence (void)isc_condition_destroy(&manager->shutdown_ok);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence#endif /* ISC_PLATFORM_USETHREADS */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence for (i = 0; i < (int)FD_SETSIZE; i++)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (manager->fdstate[i] == CLOSE_PENDING)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence (void)close(i);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence DESTROYLOCK(&manager->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence manager->magic = 0;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence mctx= manager->mctx;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_put(mctx, manager, sizeof(*manager));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_detach(&mctx);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence *managerp = NULL;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence}
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrencestatic isc_result_t
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrencesocket_recv(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence unsigned int flags)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence{
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence int io_state;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_boolean_t have_lock = ISC_FALSE;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_task_t *ntask = NULL;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_result_t result = ISC_R_SUCCESS;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence dev->ev_sender = task;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (sock->type == isc_sockettype_udp) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence io_state = doio_recv(sock, dev);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence } else {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence LOCK(&sock->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence have_lock = ISC_TRUE;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (ISC_LIST_EMPTY(sock->recv_list))
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence io_state = doio_recv(sock, dev);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence else
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence io_state = DOIO_SOFT;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence switch (io_state) {
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence case DOIO_SOFT:
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * We couldn't read all or part of the request right now, so
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * queue it.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence *
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews * Attach to socket and to task
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_task_attach(task, &ntask);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (!have_lock) {
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews LOCK(&sock->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence have_lock = ISC_TRUE;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Enqueue the request. If the socket was previously not being
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * watched, poke the watcher to start paying attention to it.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (ISC_LIST_EMPTY(sock->recv_list))
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence ISC_LIST_ENQUEUE(sock->recv_list, dev, ev_link);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence socket_log(sock, NULL, EVENT, NULL, 0, 0,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence "socket_recv: event %p -> task %p",
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence dev, ntask);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence result = ISC_R_INPROGRESS;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence break;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence case DOIO_EOF:
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence dev->result = ISC_R_EOF;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /* fallthrough */
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence case DOIO_HARD:
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence case DOIO_SUCCESS:
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence send_recvdone_event(sock, &dev);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence break;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence }
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (have_lock)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence UNLOCK(&sock->lock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence return (result);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence}
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halleyisc_result_t
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halleyisc_socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley unsigned int minimum, isc_task_t *task,
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_taskaction_t action, const void *arg)
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley{
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_socketevent_t *dev;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_socketmgr_t *manager;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley unsigned int iocount;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_buffer_t *buffer;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(VALID_SOCKET(sock));
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(buflist != NULL);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(!ISC_LIST_EMPTY(*buflist));
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(task != NULL);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(action != NULL);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley manager = sock->manager;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(VALID_MANAGER(manager));
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley iocount = isc_bufferlist_availablecount(buflist);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(iocount > 0);
3dbea10f98472c452cc2467634059f28a87feb29Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley INSIST(sock->bound);
3dbea10f98472c452cc2467634059f28a87feb29Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley if (dev == NULL) {
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley return (ISC_R_NOMEMORY);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley }
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley /*
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley * UDP sockets are always partial read
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley */
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley if (sock->type == isc_sockettype_udp)
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley dev->minimum = 1;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley else {
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley if (minimum == 0)
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley dev->minimum = iocount;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley else
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley dev->minimum = minimum;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley }
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley /*
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley * Move each buffer from the passed in list to our internal one.
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley */
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley buffer = ISC_LIST_HEAD(*buflist);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley while (buffer != NULL) {
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ISC_LIST_DEQUEUE(*buflist, buffer, link);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley buffer = ISC_LIST_HEAD(*buflist);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley }
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley return (socket_recv(sock, dev, task, 0));
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley}
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halleyisc_result_t
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halleyisc_socket_recv(isc_socket_t *sock, isc_region_t *region, unsigned int minimum,
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_task_t *task, isc_taskaction_t action, const void *arg)
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley{
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_socketevent_t *dev;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_socketmgr_t *manager;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(VALID_SOCKET(sock));
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(action != NULL);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley manager = sock->manager;
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley REQUIRE(VALID_MANAGER(manager));
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley INSIST(sock->bound);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley if (dev == NULL)
8326257468615966b10820260beb3ee96eee94b5Bob Halley return (ISC_R_NOMEMORY);
8326257468615966b10820260beb3ee96eee94b5Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halley return (isc_socket_recv2(sock, region, minimum, task, dev, 0));
8326257468615966b10820260beb3ee96eee94b5Bob Halley}
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halleyisc_result_t
402b05ddea16c6179fd251e4e740606dd973d593Bob Halleyisc_socket_recv2(isc_socket_t *sock, isc_region_t *region,
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley unsigned int minimum, isc_task_t *task,
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley isc_socketevent_t *event, unsigned int flags)
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley{
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley event->ev_sender = sock;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley event->result = ISC_R_UNEXPECTED;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley ISC_LIST_INIT(event->bufferlist);
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley event->region = *region;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley event->n = 0;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley event->offset = 0;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley event->attributes = 0;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley /*
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley * UDP sockets are always partial read.
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley */
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley if (sock->type == isc_sockettype_udp)
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley event->minimum = 1;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley else {
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley if (minimum == 0)
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley event->minimum = region->length;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley else
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley event->minimum = minimum;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley }
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley return (socket_recv(sock, event, task, flags));
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley}
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halleystatic isc_result_t
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halleysocket_send(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley unsigned int flags)
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley{
8326257468615966b10820260beb3ee96eee94b5Bob Halley int io_state;
8326257468615966b10820260beb3ee96eee94b5Bob Halley isc_boolean_t have_lock = ISC_FALSE;
8326257468615966b10820260beb3ee96eee94b5Bob Halley isc_task_t *ntask = NULL;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley isc_result_t result = ISC_R_SUCCESS;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley dev->ev_sender = task;
8326257468615966b10820260beb3ee96eee94b5Bob Halley
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff set_dev_address(address, sock, dev);
8326257468615966b10820260beb3ee96eee94b5Bob Halley if (pktinfo != NULL) {
8326257468615966b10820260beb3ee96eee94b5Bob Halley dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
8326257468615966b10820260beb3ee96eee94b5Bob Halley dev->pktinfo = *pktinfo;
8326257468615966b10820260beb3ee96eee94b5Bob Halley
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (!isc_sockaddr_issitelocal(&dev->address) &&
8326257468615966b10820260beb3ee96eee94b5Bob Halley !isc_sockaddr_islinklocal(&dev->address)) {
8326257468615966b10820260beb3ee96eee94b5Bob Halley socket_log(sock, NULL, TRACE, isc_msgcat,
8326257468615966b10820260beb3ee96eee94b5Bob Halley ISC_MSGSET_SOCKET, ISC_MSG_PKTINFOPROVIDED,
8326257468615966b10820260beb3ee96eee94b5Bob Halley "pktinfo structure provided, ifindex %u "
8326257468615966b10820260beb3ee96eee94b5Bob Halley "(set to 0)", pktinfo->ipi6_ifindex);
8326257468615966b10820260beb3ee96eee94b5Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halley /*
8326257468615966b10820260beb3ee96eee94b5Bob Halley * Set the pktinfo index to 0 here, to let the
8326257468615966b10820260beb3ee96eee94b5Bob Halley * kernel decide what interface it should send on.
8326257468615966b10820260beb3ee96eee94b5Bob Halley */
8326257468615966b10820260beb3ee96eee94b5Bob Halley dev->pktinfo.ipi6_ifindex = 0;
8326257468615966b10820260beb3ee96eee94b5Bob Halley }
8326257468615966b10820260beb3ee96eee94b5Bob Halley }
8326257468615966b10820260beb3ee96eee94b5Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halley if (sock->type == isc_sockettype_udp)
8326257468615966b10820260beb3ee96eee94b5Bob Halley io_state = doio_send(sock, dev);
8326257468615966b10820260beb3ee96eee94b5Bob Halley else {
8326257468615966b10820260beb3ee96eee94b5Bob Halley LOCK(&sock->lock);
8326257468615966b10820260beb3ee96eee94b5Bob Halley have_lock = ISC_TRUE;
8326257468615966b10820260beb3ee96eee94b5Bob Halley
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews if (ISC_LIST_EMPTY(sock->send_list))
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews io_state = doio_send(sock, dev);
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews else
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews io_state = DOIO_SOFT;
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews }
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley switch (io_state) {
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley case DOIO_SOFT:
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley /*
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews * We couldn't send all or part of the request right now, so
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews * queue it unless ISC_SOCKFLAG_NORETRY is set.
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews */
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley if ((flags & ISC_SOCKFLAG_NORETRY) == 0) {
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_task_attach(task, &ntask);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley if (!have_lock) {
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley LOCK(&sock->lock);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley have_lock = ISC_TRUE;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley }
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley /*
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley * Enqueue the request. If the socket was previously
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley * not being watched, poke the watcher to start
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley * paying attention to it.
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley */
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley if (ISC_LIST_EMPTY(sock->send_list))
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley select_poke(sock->manager, sock->fd,
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley SELECT_POKE_WRITE);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley ISC_LIST_ENQUEUE(sock->send_list, dev, ev_link);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley socket_log(sock, NULL, EVENT, NULL, 0, 0,
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley "socket_send: event %p -> task %p",
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley dev, ntask);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
result = ISC_R_INPROGRESS;
break;
}
case DOIO_HARD:
case DOIO_SUCCESS:
if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
send_senddone_event(sock, &dev);
break;
}
if (have_lock)
UNLOCK(&sock->lock);
return (result);
}
isc_result_t
isc_socket_send(isc_socket_t *sock, isc_region_t *region,
isc_task_t *task, isc_taskaction_t action, const void *arg)
{
/*
* REQUIRE() checking is performed in isc_socket_sendto().
*/
return (isc_socket_sendto(sock, region, task, action, arg, NULL,
NULL));
}
isc_result_t
isc_socket_sendto(isc_socket_t *sock, isc_region_t *region,
isc_task_t *task, isc_taskaction_t action, const void *arg,
isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
{
isc_socketevent_t *dev;
isc_socketmgr_t *manager;
REQUIRE(VALID_SOCKET(sock));
REQUIRE(region != NULL);
REQUIRE(task != NULL);
REQUIRE(action != NULL);
manager = sock->manager;
REQUIRE(VALID_MANAGER(manager));
INSIST(sock->bound);
dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
if (dev == NULL) {
return (ISC_R_NOMEMORY);
}
dev->region = *region;
return (socket_send(sock, dev, task, address, pktinfo, 0));
}
isc_result_t
isc_socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
isc_task_t *task, isc_taskaction_t action, const void *arg)
{
return (isc_socket_sendtov(sock, buflist, task, action, arg, NULL,
NULL));
}
isc_result_t
isc_socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
isc_task_t *task, isc_taskaction_t action, const void *arg,
isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
{
isc_socketevent_t *dev;
isc_socketmgr_t *manager;
unsigned int iocount;
isc_buffer_t *buffer;
REQUIRE(VALID_SOCKET(sock));
REQUIRE(buflist != NULL);
REQUIRE(!ISC_LIST_EMPTY(*buflist));
REQUIRE(task != NULL);
REQUIRE(action != NULL);
manager = sock->manager;
REQUIRE(VALID_MANAGER(manager));
iocount = isc_bufferlist_usedcount(buflist);
REQUIRE(iocount > 0);
dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
if (dev == NULL) {
return (ISC_R_NOMEMORY);
}
/*
* Move each buffer from the passed in list to our internal one.
*/
buffer = ISC_LIST_HEAD(*buflist);
while (buffer != NULL) {
ISC_LIST_DEQUEUE(*buflist, buffer, link);
ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
buffer = ISC_LIST_HEAD(*buflist);
}
return (socket_send(sock, dev, task, address, pktinfo, 0));
}
isc_result_t
isc_socket_sendto2(isc_socket_t *sock, isc_region_t *region,
isc_task_t *task,
isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
isc_socketevent_t *event, unsigned int flags)
{
REQUIRE((flags & ~(ISC_SOCKFLAG_IMMEDIATE|ISC_SOCKFLAG_NORETRY)) == 0);
if ((flags & ISC_SOCKFLAG_NORETRY) != 0)
REQUIRE(sock->type == isc_sockettype_udp);
event->ev_sender = sock;
event->result = ISC_R_UNEXPECTED;
ISC_LIST_INIT(event->bufferlist);
event->region = *region;
event->n = 0;
event->offset = 0;
event->attributes = 0;
return (socket_send(sock, event, task, address, pktinfo, flags));
}
void
isc_socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active) {
#ifdef ISC_PLATFORM_HAVESYSUNH
int s;
struct stat sb;
char strbuf[ISC_STRERRORSIZE];
if (sockaddr->type.sa.sa_family != AF_UNIX)
return;
#ifndef S_ISSOCK
#if defined(S_IFMT) && defined(S_IFSOCK)
#define S_ISSOCK(mode) ((mode & S_IFMT)==S_IFSOCK)
#elif defined(_S_IFMT) && defined(S_IFSOCK)
#define S_ISSOCK(mode) ((mode & _S_IFMT)==S_IFSOCK)
#endif
#endif
#ifndef S_ISFIFO
#if defined(S_IFMT) && defined(S_IFIFO)
#define S_ISFIFO(mode) ((mode & S_IFMT)==S_IFIFO)
#elif defined(_S_IFMT) && defined(S_IFIFO)
#define S_ISFIFO(mode) ((mode & _S_IFMT)==S_IFIFO)
#endif
#endif
#if !defined(S_ISFIFO) && !defined(S_ISSOCK)
#error You need to define S_ISFIFO and S_ISSOCK as appropriate for your platform. See <sys/stat.h>.
#endif
#ifndef S_ISFIFO
#define S_ISFIFO(mode) 0
#endif
#ifndef S_ISSOCK
#define S_ISSOCK(mode) 0
#endif
if (active) {
if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
"isc_socket_cleanunix: stat(%s): %s",
sockaddr->type.sunix.sun_path, strbuf);
return;
}
if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
"isc_socket_cleanunix: %s: not a socket",
sockaddr->type.sunix.sun_path);
return;
}
if (unlink(sockaddr->type.sunix.sun_path) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
"isc_socket_cleanunix: unlink(%s): %s",
sockaddr->type.sunix.sun_path, strbuf);
}
return;
}
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
"isc_socket_cleanunix: socket(%s): %s",
sockaddr->type.sunix.sun_path, strbuf);
return;
}
if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
switch (errno) {
case ENOENT: /* We exited cleanly last time */
break;
default:
isc__strerror(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
"isc_socket_cleanunix: stat(%s): %s",
sockaddr->type.sunix.sun_path, strbuf);
break;
}
goto cleanup;
}
if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
"isc_socket_cleanunix: %s: not a socket",
sockaddr->type.sunix.sun_path);
goto cleanup;
}
if (connect(s, (struct sockaddr *)&sockaddr->type.sunix,
sizeof(sockaddr->type.sunix)) < 0) {
switch (errno) {
case ECONNREFUSED:
case ECONNRESET:
if (unlink(sockaddr->type.sunix.sun_path) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET,
ISC_LOG_WARNING,
"isc_socket_cleanunix: "
"unlink(%s): %s",
sockaddr->type.sunix.sun_path,
strbuf);
}
break;
default:
isc__strerror(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
"isc_socket_cleanunix: connect(%s): %s",
sockaddr->type.sunix.sun_path, strbuf);
break;
}
}
cleanup:
close(s);
#else
UNUSED(sockaddr);
UNUSED(active);
#endif
}
isc_result_t
isc_socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
isc_uint32_t owner, isc_uint32_t group)
{
#ifdef ISC_PLATFORM_HAVESYSUNH
isc_result_t result = ISC_R_SUCCESS;
char strbuf[ISC_STRERRORSIZE];
char path[sizeof(sockaddr->type.sunix.sun_path)];
#ifdef NEED_SECURE_DIRECTORY
char *slash;
#endif
REQUIRE(sockaddr->type.sa.sa_family == AF_UNIX);
INSIST(strlen(sockaddr->type.sunix.sun_path) < sizeof(path));
strcpy(path, sockaddr->type.sunix.sun_path);
#ifdef NEED_SECURE_DIRECTORY
slash = strrchr(path, '/');
if (slash != NULL) {
if (slash != path)
*slash = '\0';
else
strcpy(path, "/");
} else
strcpy(path, ".");
#endif
if (chmod(path, perm) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
"isc_socket_permunix: chmod(%s, %d): %s",
path, perm, strbuf);
result = ISC_R_FAILURE;
}
if (chown(path, owner, group) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
"isc_socket_permunix: chown(%s, %d, %d): %s",
path, owner, group,
strbuf);
result = ISC_R_FAILURE;
}
return (result);
#else
UNUSED(sockaddr);
UNUSED(perm);
UNUSED(owner);
UNUSED(group);
return (ISC_R_NOTIMPLEMENTED);
#endif
}
isc_result_t
isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr) {
char strbuf[ISC_STRERRORSIZE];
int on = 1;
LOCK(&sock->lock);
INSIST(!sock->bound);
if (sock->pf != sockaddr->type.sa.sa_family) {
UNLOCK(&sock->lock);
return (ISC_R_FAMILYMISMATCH);
}
/*
* Only set SO_REUSEADDR when we want a specific port.
*/
#ifdef AF_UNIX
if (sock->pf == AF_UNIX)
goto bind_socket;
#endif
if (isc_sockaddr_getport(sockaddr) != (in_port_t)0 &&
setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
sizeof(on)) < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"setsockopt(%d) %s", sock->fd,
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_FAILED, "failed"));
/* Press on... */
}
#ifdef AF_UNIX
bind_socket:
#endif
if (bind(sock->fd, &sockaddr->type.sa, sockaddr->length) < 0) {
UNLOCK(&sock->lock);
switch (errno) {
case EACCES:
return (ISC_R_NOPERM);
case EADDRNOTAVAIL:
return (ISC_R_ADDRNOTAVAIL);
case EADDRINUSE:
return (ISC_R_ADDRINUSE);
case EINVAL:
return (ISC_R_BOUND);
default:
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__, "bind: %s",
strbuf);
return (ISC_R_UNEXPECTED);
}
}
socket_log(sock, sockaddr, TRACE,
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_BOUND, "bound");
sock->bound = 1;
UNLOCK(&sock->lock);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_socket_filter(isc_socket_t *sock, const char *filter) {
#ifdef SO_ACCEPTFILTER
char strbuf[ISC_STRERRORSIZE];
struct accept_filter_arg afa;
#else
UNUSED(sock);
UNUSED(filter);
#endif
REQUIRE(VALID_SOCKET(sock));
#ifdef SO_ACCEPTFILTER
bzero(&afa, sizeof(afa));
strncpy(afa.af_name, filter, sizeof(afa.af_name));
if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER,
&afa, sizeof(afa)) == -1) {
isc__strerror(errno, strbuf, sizeof(strbuf));
socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
ISC_MSG_FILTER, "setsockopt(SO_ACCEPTFILTER): %s",
strbuf);
return (ISC_R_FAILURE);
}
return (ISC_R_SUCCESS);
#else
return (ISC_R_NOTIMPLEMENTED);
#endif
}
/*
* Set up to listen on a given socket. We do this by creating an internal
* event that will be dispatched when the socket has read activity. The
* watcher will send the internal event to the task when there is a new
* connection.
*
* Unlike in read, we don't preallocate a done event here. Every time there
* is a new connection we'll have to allocate a new one anyway, so we might
* as well keep things simple rather than having to track them.
*/
isc_result_t
isc_socket_listen(isc_socket_t *sock, unsigned int backlog) {
char strbuf[ISC_STRERRORSIZE];
REQUIRE(VALID_SOCKET(sock));
LOCK(&sock->lock);
REQUIRE(!sock->listener);
REQUIRE(sock->bound);
REQUIRE(sock->type == isc_sockettype_tcp ||
sock->type == isc_sockettype_unix);
if (backlog == 0)
backlog = SOMAXCONN;
if (listen(sock->fd, (int)backlog) < 0) {
UNLOCK(&sock->lock);
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__, "listen: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
sock->listener = 1;
UNLOCK(&sock->lock);
return (ISC_R_SUCCESS);
}
/*
* This should try to do agressive accept() XXXMLG
*/
isc_result_t
isc_socket_accept(isc_socket_t *sock,
isc_task_t *task, isc_taskaction_t action, const void *arg)
{
isc_socket_newconnev_t *dev;
isc_socketmgr_t *manager;
isc_task_t *ntask = NULL;
isc_socket_t *nsock;
isc_result_t result;
isc_boolean_t do_poke = ISC_FALSE;
REQUIRE(VALID_SOCKET(sock));
manager = sock->manager;
REQUIRE(VALID_MANAGER(manager));
LOCK(&sock->lock);
REQUIRE(sock->listener);
/*
* Sender field is overloaded here with the task we will be sending
* this event to. Just before the actual event is delivered the
* actual ev_sender will be touched up to be the socket.
*/
dev = (isc_socket_newconnev_t *)
isc_event_allocate(manager->mctx, task, ISC_SOCKEVENT_NEWCONN,
action, arg, sizeof(*dev));
if (dev == NULL) {
UNLOCK(&sock->lock);
return (ISC_R_NOMEMORY);
}
ISC_LINK_INIT(dev, ev_link);
result = allocate_socket(manager, sock->type, &nsock);
if (result != ISC_R_SUCCESS) {
isc_event_free(ISC_EVENT_PTR(&dev));
UNLOCK(&sock->lock);
return (result);
}
/*
* Attach to socket and to task.
*/
isc_task_attach(task, &ntask);
nsock->references++;
dev->ev_sender = ntask;
dev->newsocket = nsock;
/*
* Poke watcher here. We still have the socket locked, so there
* is no race condition. We will keep the lock for such a short
* bit of time waking it up now or later won't matter all that much.
*/
if (ISC_LIST_EMPTY(sock->accept_list))
do_poke = ISC_TRUE;
ISC_LIST_ENQUEUE(sock->accept_list, dev, ev_link);
if (do_poke)
select_poke(manager, sock->fd, SELECT_POKE_ACCEPT);
UNLOCK(&sock->lock);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr,
isc_task_t *task, isc_taskaction_t action, const void *arg)
{
isc_socket_connev_t *dev;
isc_task_t *ntask = NULL;
isc_socketmgr_t *manager;
int cc;
char strbuf[ISC_STRERRORSIZE];
REQUIRE(VALID_SOCKET(sock));
REQUIRE(addr != NULL);
REQUIRE(task != NULL);
REQUIRE(action != NULL);
manager = sock->manager;
REQUIRE(VALID_MANAGER(manager));
REQUIRE(addr != NULL);
if (isc_sockaddr_ismulticast(addr))
return (ISC_R_MULTICAST);
LOCK(&sock->lock);
REQUIRE(!sock->connecting);
dev = (isc_socket_connev_t *)isc_event_allocate(manager->mctx, sock,
ISC_SOCKEVENT_CONNECT,
action, arg,
sizeof(*dev));
if (dev == NULL) {
UNLOCK(&sock->lock);
return (ISC_R_NOMEMORY);
}
ISC_LINK_INIT(dev, ev_link);
/*
* Try to do the connect right away, as there can be only one
* outstanding, and it might happen to complete.
*/
sock->peer_address = *addr;
cc = connect(sock->fd, &addr->type.sa, addr->length);
if (cc < 0) {
if (SOFT_ERROR(errno) || errno == EINPROGRESS)
goto queue;
switch (errno) {
#define ERROR_MATCH(a, b) case a: dev->result = b; goto err_exit;
ERROR_MATCH(EACCES, ISC_R_NOPERM);
ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
#ifdef EHOSTDOWN
ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
#endif
ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
#undef ERROR_MATCH
}
sock->connected = 0;
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__, "%d/%s", errno, strbuf);
UNLOCK(&sock->lock);
isc_event_free(ISC_EVENT_PTR(&dev));
return (ISC_R_UNEXPECTED);
err_exit:
sock->connected = 0;
isc_task_send(task, ISC_EVENT_PTR(&dev));
UNLOCK(&sock->lock);
return (ISC_R_SUCCESS);
}
/*
* If connect completed, fire off the done event.
*/
if (cc == 0) {
sock->connected = 1;
sock->bound = 1;
dev->result = ISC_R_SUCCESS;
isc_task_send(task, ISC_EVENT_PTR(&dev));
UNLOCK(&sock->lock);
return (ISC_R_SUCCESS);
}
queue:
/*
* Attach to task.
*/
isc_task_attach(task, &ntask);
sock->connecting = 1;
dev->ev_sender = ntask;
/*
* Poke watcher here. We still have the socket locked, so there
* is no race condition. We will keep the lock for such a short
* bit of time waking it up now or later won't matter all that much.
*/
if (sock->connect_ev == NULL)
select_poke(manager, sock->fd, SELECT_POKE_CONNECT);
sock->connect_ev = dev;
UNLOCK(&sock->lock);
return (ISC_R_SUCCESS);
}
/*
* Called when a socket with a pending connect() finishes.
*/
static void
internal_connect(isc_task_t *me, isc_event_t *ev) {
isc_socket_t *sock;
isc_socket_connev_t *dev;
isc_task_t *task;
int cc;
ISC_SOCKADDR_LEN_T optlen;
char strbuf[ISC_STRERRORSIZE];
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
UNUSED(me);
INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
sock = ev->ev_sender;
INSIST(VALID_SOCKET(sock));
LOCK(&sock->lock);
/*
* When the internal event was sent the reference count was bumped
* to keep the socket around for us. Decrement the count here.
*/
INSIST(sock->references > 0);
sock->references--;
if (sock->references == 0) {
UNLOCK(&sock->lock);
destroy(&sock);
return;
}
/*
* Has this event been canceled?
*/
dev = sock->connect_ev;
if (dev == NULL) {
INSIST(!sock->connecting);
UNLOCK(&sock->lock);
return;
}
INSIST(sock->connecting);
sock->connecting = 0;
/*
* Get any possible error status here.
*/
optlen = sizeof(cc);
if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR,
(void *)&cc, (void *)&optlen) < 0)
cc = errno;
else
errno = cc;
if (errno != 0) {
/*
* If the error is EAGAIN, just re-select on this
* fd and pretend nothing strange happened.
*/
if (SOFT_ERROR(errno) || errno == EINPROGRESS) {
sock->connecting = 1;
select_poke(sock->manager, sock->fd,
SELECT_POKE_CONNECT);
UNLOCK(&sock->lock);
return;
}
/*
* Translate other errors into ISC_R_* flavors.
*/
switch (errno) {
#define ERROR_MATCH(a, b) case a: dev->result = b; break;
ERROR_MATCH(EACCES, ISC_R_NOPERM);
ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
#ifdef EHOSTDOWN
ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
#endif
ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
ERROR_MATCH(ETIMEDOUT, ISC_R_TIMEDOUT);
ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
#undef ERROR_MATCH
default:
dev->result = ISC_R_UNEXPECTED;
isc_sockaddr_format(&sock->peer_address, peerbuf,
sizeof(peerbuf));
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__,
"internal_connect: connect(%s) %s",
peerbuf, strbuf);
}
} else {
dev->result = ISC_R_SUCCESS;
sock->connected = 1;
sock->bound = 1;
}
sock->connect_ev = NULL;
UNLOCK(&sock->lock);
task = dev->ev_sender;
dev->ev_sender = sock;
isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
}
isc_result_t
isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp) {
isc_result_t result;
REQUIRE(VALID_SOCKET(sock));
REQUIRE(addressp != NULL);
LOCK(&sock->lock);
if (sock->connected) {
*addressp = sock->peer_address;
result = ISC_R_SUCCESS;
} else {
result = ISC_R_NOTCONNECTED;
}
UNLOCK(&sock->lock);
return (result);
}
isc_result_t
isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp) {
ISC_SOCKADDR_LEN_T len;
isc_result_t result;
char strbuf[ISC_STRERRORSIZE];
REQUIRE(VALID_SOCKET(sock));
REQUIRE(addressp != NULL);
LOCK(&sock->lock);
if (!sock->bound) {
result = ISC_R_NOTBOUND;
goto out;
}
result = ISC_R_SUCCESS;
len = sizeof(addressp->type);
if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) {
isc__strerror(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR(__FILE__, __LINE__, "getsockname: %s",
strbuf);
result = ISC_R_UNEXPECTED;
goto out;
}
addressp->length = (unsigned int)len;
out:
UNLOCK(&sock->lock);
return (result);
}
/*
* Run through the list of events on this socket, and cancel the ones
* queued for task "task" of type "how". "how" is a bitmask.
*/
void
isc_socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how) {
REQUIRE(VALID_SOCKET(sock));
/*
* Quick exit if there is nothing to do. Don't even bother locking
* in this case.
*/
if (how == 0)
return;
LOCK(&sock->lock);
/*
* All of these do the same thing, more or less.
* Each will:
* o If the internal event is marked as "posted" try to
* remove it from the task's queue. If this fails, mark it
* as canceled instead, and let the task clean it up later.
* o For each I/O request for that task of that type, post
* its done event with status of "ISC_R_CANCELED".
* o Reset any state needed.
*/
if (((how & ISC_SOCKCANCEL_RECV) == ISC_SOCKCANCEL_RECV)
&& !ISC_LIST_EMPTY(sock->recv_list)) {
isc_socketevent_t *dev;
isc_socketevent_t *next;
isc_task_t *current_task;
dev = ISC_LIST_HEAD(sock->recv_list);
while (dev != NULL) {
current_task = dev->ev_sender;
next = ISC_LIST_NEXT(dev, ev_link);
if ((task == NULL) || (task == current_task)) {
dev->result = ISC_R_CANCELED;
send_recvdone_event(sock, &dev);
}
dev = next;
}
}
if (((how & ISC_SOCKCANCEL_SEND) == ISC_SOCKCANCEL_SEND)
&& !ISC_LIST_EMPTY(sock->send_list)) {
isc_socketevent_t *dev;
isc_socketevent_t *next;
isc_task_t *current_task;
dev = ISC_LIST_HEAD(sock->send_list);
while (dev != NULL) {
current_task = dev->ev_sender;
next = ISC_LIST_NEXT(dev, ev_link);
if ((task == NULL) || (task == current_task)) {
dev->result = ISC_R_CANCELED;
send_senddone_event(sock, &dev);
}
dev = next;
}
}
if (((how & ISC_SOCKCANCEL_ACCEPT) == ISC_SOCKCANCEL_ACCEPT)
&& !ISC_LIST_EMPTY(sock->accept_list)) {
isc_socket_newconnev_t *dev;
isc_socket_newconnev_t *next;
isc_task_t *current_task;
dev = ISC_LIST_HEAD(sock->accept_list);
while (dev != NULL) {
current_task = dev->ev_sender;
next = ISC_LIST_NEXT(dev, ev_link);
if ((task == NULL) || (task == current_task)) {
ISC_LIST_UNLINK(sock->accept_list, dev,
ev_link);
dev->newsocket->references--;
free_socket(&dev->newsocket);
dev->result = ISC_R_CANCELED;
dev->ev_sender = sock;
isc_task_sendanddetach(&current_task,
ISC_EVENT_PTR(&dev));
}
dev = next;
}
}
/*
* Connecting is not a list.
*/
if (((how & ISC_SOCKCANCEL_CONNECT) == ISC_SOCKCANCEL_CONNECT)
&& sock->connect_ev != NULL) {
isc_socket_connev_t *dev;
isc_task_t *current_task;
INSIST(sock->connecting);
sock->connecting = 0;
dev = sock->connect_ev;
current_task = dev->ev_sender;
if ((task == NULL) || (task == current_task)) {
sock->connect_ev = NULL;
dev->result = ISC_R_CANCELED;
dev->ev_sender = sock;
isc_task_sendanddetach(&current_task,
ISC_EVENT_PTR(&dev));
}
}
UNLOCK(&sock->lock);
}
isc_sockettype_t
isc_socket_gettype(isc_socket_t *sock) {
REQUIRE(VALID_SOCKET(sock));
return (sock->type);
}
isc_boolean_t
isc_socket_isbound(isc_socket_t *sock) {
isc_boolean_t val;
LOCK(&sock->lock);
val = ((sock->bound) ? ISC_TRUE : ISC_FALSE);
UNLOCK(&sock->lock);
return (val);
}
void
isc_socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes) {
#if defined(IPV6_V6ONLY)
int onoff = yes ? 1 : 0;
#else
UNUSED(yes);
UNUSED(sock);
#endif
REQUIRE(VALID_SOCKET(sock));
#ifdef IPV6_V6ONLY
if (sock->pf == AF_INET6) {
(void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
(void *)&onoff, sizeof(onoff));
}
#endif
}
#ifndef ISC_PLATFORM_USETHREADS
void
isc__socketmgr_getfdsets(fd_set *readset, fd_set *writeset, int *maxfd) {
if (socketmgr == NULL)
*maxfd = 0;
else {
*readset = socketmgr->read_fds;
*writeset = socketmgr->write_fds;
*maxfd = socketmgr->maxfd + 1;
}
}
isc_result_t
isc__socketmgr_dispatch(fd_set *readset, fd_set *writeset, int maxfd) {
isc_socketmgr_t *manager = socketmgr;
if (manager == NULL)
return (ISC_R_NOTFOUND);
process_fds(manager, maxfd, readset, writeset);
return (ISC_R_SUCCESS);
}
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef HAVE_LIBXML2
static const char *
_socktype(int type)
{
if (type == 1)
return ("udp");
else if (type == 2)
return ("tcp");
else if (type == 3)
return ("unix");
else if (type == 4)
return ("fdwatch");
else
return ("not-initialized");
}
void
isc_socketmgr_renderxml(isc_socketmgr_t *mgr, xmlTextWriterPtr writer)
{
isc_socket_t *sock;
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
isc_sockaddr_t addr;
ISC_SOCKADDR_LEN_T len;
LOCK(&mgr->lock);
#ifndef ISC_PLATFORM_USETHREADS
xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
xmlTextWriterWriteFormatString(writer, "%d", mgr->refs);
xmlTextWriterEndElement(writer);
#endif
xmlTextWriterStartElement(writer, ISC_XMLCHAR "sockets");
sock = ISC_LIST_HEAD(mgr->socklist);
while (sock != NULL) {
LOCK(&sock->lock);
xmlTextWriterStartElement(writer, ISC_XMLCHAR "socket");
xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
xmlTextWriterWriteFormatString(writer, "%d", sock->references);
xmlTextWriterEndElement(writer);
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "type",
ISC_XMLCHAR _socktype(sock->type));
if (sock->connected) {
isc_sockaddr_format(&sock->peer_address, peerbuf,
sizeof(peerbuf));
xmlTextWriterWriteElement(writer,
ISC_XMLCHAR "peer-address",
ISC_XMLCHAR peerbuf);
}
len = sizeof(addr);
if (getsockname(sock->fd, &addr.type.sa, (void *)&len) == 0) {
isc_sockaddr_format(&addr, peerbuf, sizeof(peerbuf));
xmlTextWriterWriteElement(writer,
ISC_XMLCHAR "local-address",
ISC_XMLCHAR peerbuf);
}
if (sock->pending_recv)
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
ISC_XMLCHAR "pending-receive");
if (sock->pending_send)
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
ISC_XMLCHAR "pending-send");
if (sock->pending_accept)
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
ISC_XMLCHAR "pending_accept");
if (sock->listener)
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
ISC_XMLCHAR "listener");
if (sock->connected)
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
ISC_XMLCHAR "connected");
if (sock->connecting)
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
ISC_XMLCHAR "connecting");
if (sock->bound)
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
ISC_XMLCHAR "bound");
xmlTextWriterEndElement(writer); /* socket */
UNLOCK(&sock->lock);
sock = ISC_LIST_NEXT(sock, link);
}
xmlTextWriterEndElement(writer); /* sockets */
UNLOCK(&mgr->lock);
}
#endif /* HAVE_LIBXML2 */