1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1998-12-12 20:48:14 +00:00
|
|
|
*/
|
1998-12-12 19:25:20 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdbool.h>
|
2000-04-13 08:10:18 +00:00
|
|
|
#include <sys/param.h>
|
1999-12-04 01:27:44 +00:00
|
|
|
#include <sys/socket.h>
|
2005-02-23 01:09:23 +00:00
|
|
|
#include <sys/stat.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <sys/types.h>
|
2019-11-06 15:31:47 +01:00
|
|
|
#if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__)
|
2017-09-18 08:34:41 +02:00
|
|
|
#include <sys/sysctl.h>
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__) */
|
1999-12-04 01:27:44 +00:00
|
|
|
#include <sys/time.h>
|
1999-08-25 23:50:48 +00:00
|
|
|
#include <sys/uio.h>
|
|
|
|
|
2014-02-10 09:46:54 +11:00
|
|
|
#if defined(HAVE_LINUX_NETLINK_H) && defined(HAVE_LINUX_RTNETLINK_H)
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(HAVE_LINUX_NETLINK_H) && defined(HAVE_LINUX_RTNETLINK_H) \
|
|
|
|
*/
|
2014-02-10 09:46:54 +11:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
#include <errno.h>
|
2001-04-24 18:40:07 +00:00
|
|
|
#include <fcntl.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
2018-08-07 16:46:53 +02:00
|
|
|
#include <sys/un.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-08-06 13:00:55 +02:00
|
|
|
#include <isc/app.h>
|
1999-09-02 02:10:44 +00:00
|
|
|
#include <isc/buffer.h>
|
2000-12-26 21:45:08 +00:00
|
|
|
#include <isc/condition.h>
|
2001-08-08 22:54:55 +00:00
|
|
|
#include <isc/formatcheck.h>
|
1999-09-02 02:10:44 +00:00
|
|
|
#include <isc/list.h>
|
2000-05-03 21:09:49 +00:00
|
|
|
#include <isc/log.h>
|
2000-04-28 03:53:48 +00:00
|
|
|
#include <isc/mem.h>
|
2000-12-26 21:45:08 +00:00
|
|
|
#include <isc/mutex.h>
|
1999-08-31 20:57:46 +00:00
|
|
|
#include <isc/net.h>
|
2007-02-26 01:37:37 +00:00
|
|
|
#include <isc/once.h>
|
2000-08-29 23:58:17 +00:00
|
|
|
#include <isc/platform.h>
|
2000-05-03 22:04:49 +00:00
|
|
|
#include <isc/print.h>
|
2018-10-04 12:30:04 +00:00
|
|
|
#include <isc/refcount.h>
|
1999-09-02 02:10:44 +00:00
|
|
|
#include <isc/region.h>
|
2014-05-23 13:05:23 +10:00
|
|
|
#include <isc/resource.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
#include <isc/socket.h>
|
2009-01-27 22:30:00 +00:00
|
|
|
#include <isc/stats.h>
|
2018-08-28 15:43:44 -07:00
|
|
|
#include <isc/strerr.h>
|
2017-09-13 00:14:37 -07:00
|
|
|
#include <isc/string.h>
|
2000-04-28 03:53:48 +00:00
|
|
|
#include <isc/task.h>
|
2000-12-26 21:45:08 +00:00
|
|
|
#include <isc/thread.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1998-12-13 02:04:22 +00:00
|
|
|
|
2018-09-05 12:05:32 +02:00
|
|
|
#ifdef HAVE_KQUEUE
|
2008-06-23 19:41:20 +00:00
|
|
|
#include <sys/event.h>
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef HAVE_KQUEUE */
|
2018-09-05 12:14:27 +02:00
|
|
|
#ifdef HAVE_EPOLL_CREATE1
|
2008-06-23 19:41:20 +00:00
|
|
|
#include <sys/epoll.h>
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef HAVE_EPOLL_CREATE1 */
|
2010-12-03 00:57:57 +00:00
|
|
|
#if defined(HAVE_SYS_DEVPOLL_H)
|
2008-06-23 19:41:20 +00:00
|
|
|
#include <sys/devpoll.h>
|
2010-12-03 00:57:57 +00:00
|
|
|
#elif defined(HAVE_DEVPOLL_H)
|
|
|
|
#include <devpoll.h>
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(HAVE_SYS_DEVPOLL_H) */
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
2020-03-09 16:17:26 +01:00
|
|
|
#include "errno2result.h"
|
2021-04-27 00:07:43 +02:00
|
|
|
#include "socket_p.h"
|
2020-03-09 16:17:26 +01:00
|
|
|
|
2018-09-05 12:34:10 +02:00
|
|
|
#ifdef ENABLE_TCP_FASTOPEN
|
2016-08-12 15:31:33 +10:00
|
|
|
#include <netinet/tcp.h>
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef ENABLE_TCP_FASTOPEN */
|
2016-08-12 15:31:33 +10:00
|
|
|
|
2019-06-24 12:21:47 +02:00
|
|
|
#ifdef HAVE_JSON_C
|
|
|
|
#include <json_object.h>
|
|
|
|
#endif /* HAVE_JSON_C */
|
|
|
|
|
2019-06-24 14:25:55 +02:00
|
|
|
#ifdef HAVE_LIBXML2
|
|
|
|
#include <libxml/xmlwriter.h>
|
|
|
|
#define ISC_XMLCHAR (const xmlChar *)
|
|
|
|
#endif /* HAVE_LIBXML2 */
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/*%
|
2008-06-23 23:47:11 +00:00
|
|
|
* Choose the most preferable multiplex method.
|
2008-06-23 19:41:20 +00:00
|
|
|
*/
|
2018-09-05 12:05:32 +02:00
|
|
|
#if defined(HAVE_KQUEUE)
|
2008-06-23 19:41:20 +00:00
|
|
|
#define USE_KQUEUE
|
2018-09-05 12:14:27 +02:00
|
|
|
#elif defined(HAVE_EPOLL_CREATE1)
|
2008-06-23 19:41:20 +00:00
|
|
|
#define USE_EPOLL
|
2018-09-05 12:26:40 +02:00
|
|
|
#elif defined(HAVE_SYS_DEVPOLL_H) || defined(HAVE_DEVPOLL_H)
|
2008-06-23 19:41:20 +00:00
|
|
|
#define USE_DEVPOLL
|
|
|
|
typedef struct {
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int want_read : 1, want_write : 1;
|
2008-06-23 19:41:20 +00:00
|
|
|
} pollinfo_t;
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(HAVE_KQUEUE) */
|
2008-06-23 19:41:20 +00:00
|
|
|
#define USE_SELECT
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* HAVE_KQUEUE */
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
/*
|
|
|
|
* Set by the -T dscp option on the command line. If set to a value
|
|
|
|
* other than -1, we check to make sure DSCP values match it, and
|
|
|
|
* assert if not.
|
|
|
|
*/
|
|
|
|
int isc_dscp_check_value = -1;
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/*%
|
|
|
|
* Maximum number of allowable open sockets. This is also the maximum
|
2008-08-07 21:28:13 +00:00
|
|
|
* allowable socket file descriptor.
|
|
|
|
*
|
|
|
|
* Care should be taken before modifying this value for select():
|
|
|
|
* The API standard doesn't ensure select() accept more than (the system default
|
|
|
|
* of) FD_SETSIZE descriptors, and the default size should in fact be fine in
|
|
|
|
* the vast majority of cases. This constant should therefore be increased only
|
2008-08-07 23:47:34 +00:00
|
|
|
* when absolutely necessary and possible, i.e., the server is exhausting all
|
2008-08-07 21:28:13 +00:00
|
|
|
* available file descriptors (up to FD_SETSIZE) and the select() function
|
|
|
|
* and FD_xxx macros support larger values than FD_SETSIZE (which may not
|
|
|
|
* always by true, but we keep using some of them to ensure as much
|
|
|
|
* portability as possible). Note also that overall server performance
|
|
|
|
* may be rather worsened with a larger value of this constant due to
|
|
|
|
* inherent scalability problems of select().
|
|
|
|
*
|
|
|
|
* As a special note, this value shouldn't have to be touched if
|
|
|
|
* this is a build for an authoritative only DNS server.
|
2008-06-23 19:41:20 +00:00
|
|
|
*/
|
|
|
|
#ifndef ISC_SOCKET_MAXSOCKETS
|
2008-08-07 21:28:13 +00:00
|
|
|
#if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
|
2014-02-18 22:36:14 -08:00
|
|
|
#ifdef TUNE_LARGE
|
|
|
|
#define ISC_SOCKET_MAXSOCKETS 21000
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef TUNE_LARGE */
|
2008-06-23 19:41:20 +00:00
|
|
|
#define ISC_SOCKET_MAXSOCKETS 4096
|
2014-02-18 22:36:14 -08:00
|
|
|
#endif /* TUNE_LARGE */
|
2008-08-07 21:28:13 +00:00
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
#define ISC_SOCKET_MAXSOCKETS FD_SETSIZE
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* USE_KQUEUE... */
|
|
|
|
#endif /* ISC_SOCKET_MAXSOCKETS */
|
2008-08-07 21:28:13 +00:00
|
|
|
|
|
|
|
#ifdef USE_SELECT
|
|
|
|
/*%
|
2008-08-20 23:57:59 +00:00
|
|
|
* Mac OS X needs a special definition to support larger values in select().
|
|
|
|
* We always define this because a larger value can be specified run-time.
|
2008-08-07 21:28:13 +00:00
|
|
|
*/
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define _DARWIN_UNLIMITED_SELECT
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* __APPLE__ */
|
|
|
|
#endif /* USE_SELECT */
|
2008-08-07 21:28:13 +00:00
|
|
|
|
2008-11-08 22:35:12 +00:00
|
|
|
#ifdef ISC_SOCKET_USE_POLLWATCH
|
|
|
|
/*%
|
|
|
|
* If this macro is defined, enable workaround for a Solaris /dev/poll kernel
|
|
|
|
* bug: DP_POLL ioctl could keep sleeping even if socket I/O is possible for
|
|
|
|
* some of the specified FD. The idea is based on the observation that it's
|
|
|
|
* likely for a busy server to keep receiving packets. It specifically works
|
|
|
|
* as follows: the socket watcher is first initialized with the state of
|
|
|
|
* "poll_idle". While it's in the idle state it keeps sleeping until a socket
|
|
|
|
* event occurs. When it wakes up for a socket I/O event, it moves to the
|
|
|
|
* poll_active state, and sets the poll timeout to a short period
|
|
|
|
* (ISC_SOCKET_POLLWATCH_TIMEOUT msec). If timeout occurs in this state, the
|
|
|
|
* watcher goes to the poll_checking state with the same timeout period.
|
|
|
|
* In this state, the watcher tries to detect whether this is a break
|
|
|
|
* during intermittent events or the kernel bug is triggered. If the next
|
|
|
|
* polling reports an event within the short period, the previous timeout is
|
|
|
|
* likely to be a kernel bug, and so the watcher goes back to the active state.
|
|
|
|
* Otherwise, it moves to the idle state again.
|
|
|
|
*
|
|
|
|
* It's not clear whether this is a thread-related bug, but since we've only
|
|
|
|
* seen this with threads, this workaround is used only when enabling threads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef enum { poll_idle, poll_active, poll_checking } pollstate_t;
|
|
|
|
|
|
|
|
#ifndef ISC_SOCKET_POLLWATCH_TIMEOUT
|
|
|
|
#define ISC_SOCKET_POLLWATCH_TIMEOUT 10
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* ISC_SOCKET_POLLWATCH_TIMEOUT */
|
|
|
|
#endif /* ISC_SOCKET_USE_POLLWATCH */
|
2008-11-08 22:35:12 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/*%
|
2018-10-03 22:11:52 +00:00
|
|
|
* Per-FD lock buckets, we shuffle them around a bit as FDs come in herds.
|
2008-06-23 19:41:20 +00:00
|
|
|
*/
|
2020-02-13 14:44:37 -08:00
|
|
|
#define FDLOCK_BITS 10
|
2020-02-12 13:59:18 +01:00
|
|
|
#define FDLOCK_COUNT (1 << FDLOCK_BITS)
|
|
|
|
#define FDLOCK_ID(fd) \
|
|
|
|
(((fd) % (FDLOCK_COUNT) >> (FDLOCK_BITS / 2)) | \
|
|
|
|
(((fd) << (FDLOCK_BITS / 2)) % (FDLOCK_COUNT)))
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
/*%
|
|
|
|
* Maximum number of events communicated with the kernel. There should normally
|
|
|
|
* be no need for having a large number.
|
|
|
|
*/
|
|
|
|
#if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
|
|
|
|
#ifndef ISC_SOCKET_MAXEVENTS
|
2014-02-18 22:36:14 -08:00
|
|
|
#ifdef TUNE_LARGE
|
2020-02-12 13:59:18 +01:00
|
|
|
#define ISC_SOCKET_MAXEVENTS 2048
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef TUNE_LARGE */
|
2020-02-12 13:59:18 +01:00
|
|
|
#define ISC_SOCKET_MAXEVENTS 64
|
2014-02-18 22:36:14 -08:00
|
|
|
#endif /* TUNE_LARGE */
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef ISC_SOCKET_MAXEVENTS */
|
|
|
|
#endif /* if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL) \
|
|
|
|
* */
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
1999-02-11 06:38:12 +00:00
|
|
|
* Some systems define the socket length argument as an int, some as size_t,
|
1999-08-28 05:34:41 +00:00
|
|
|
* some as socklen_t. This is here so it can be easily changed if needed.
|
1999-02-11 06:38:12 +00:00
|
|
|
*/
|
2018-09-05 14:03:10 +02:00
|
|
|
#ifndef socklen_t
|
|
|
|
#define socklen_t unsigned int
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef socklen_t */
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
1999-02-11 06:38:12 +00:00
|
|
|
* Define what the possible "soft" errors can be. These are non-fatal returns
|
|
|
|
* of various network related functions, like recv() and so on.
|
1999-03-29 23:56:06 +00:00
|
|
|
*
|
|
|
|
* For some reason, BSDI (and perhaps others) will sometimes return <0
|
|
|
|
* from recv() but will have errno==0. This is broken, but we have to
|
|
|
|
* work around it here.
|
1999-02-11 06:38:12 +00:00
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
#define SOFT_ERROR(e) \
|
|
|
|
((e) == EAGAIN || (e) == EWOULDBLOCK || (e) == ENOBUFS || \
|
|
|
|
(e) == EINTR || (e) == 0)
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2000-05-03 21:09:49 +00:00
|
|
|
#define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!<
|
2000-05-03 21:09:49 +00:00
|
|
|
* DLVL(90) -- Function entry/exit and other tracing.
|
|
|
|
* DLVL(70) -- Socket "correctness" -- including returning of events, etc.
|
|
|
|
* DLVL(60) -- Socket data send/receive
|
|
|
|
* DLVL(50) -- Event tracing, including receiving/sending completion events.
|
|
|
|
* DLVL(20) -- Socket creation/destruction.
|
|
|
|
*/
|
2020-02-13 14:44:37 -08:00
|
|
|
#define TRACE_LEVEL 90
|
2020-02-12 13:59:18 +01:00
|
|
|
#define CORRECTNESS_LEVEL 70
|
2020-02-13 14:44:37 -08:00
|
|
|
#define IOEVENT_LEVEL 60
|
|
|
|
#define EVENT_LEVEL 50
|
|
|
|
#define CREATION_LEVEL 20
|
2020-02-12 13:59:18 +01:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define TRACE DLVL(TRACE_LEVEL)
|
2020-02-12 13:59:18 +01:00
|
|
|
#define CORRECTNESS DLVL(CORRECTNESS_LEVEL)
|
2020-02-13 14:44:37 -08:00
|
|
|
#define IOEVENT DLVL(IOEVENT_LEVEL)
|
|
|
|
#define EVENT DLVL(EVENT_LEVEL)
|
|
|
|
#define CREATION DLVL(CREATION_LEVEL)
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
typedef isc_event_t intev_t;
|
1998-12-04 11:21:11 +00:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define SOCKET_MAGIC ISC_MAGIC('I', 'O', 'i', 'o')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_SOCKET(s) ISC_MAGIC_VALID(s, SOCKET_MAGIC)
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-12-04 01:27:44 +00:00
|
|
|
* IPv6 control information. If the socket is an IPv6 socket we want
|
|
|
|
* to collect the destination address and interface so the client can
|
|
|
|
* set them on outgoing packets.
|
|
|
|
*/
|
|
|
|
#ifndef USE_CMSG
|
2020-02-12 13:59:18 +01:00
|
|
|
#define USE_CMSG 1
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef USE_CMSG */
|
1999-12-04 01:27:44 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
1999-12-15 20:47:38 +00:00
|
|
|
* NetBSD and FreeBSD can timestamp packets. XXXMLG Should we have
|
1999-12-04 01:27:44 +00:00
|
|
|
* a setsockopt() like interface to request timestamps, and if the OS
|
|
|
|
* doesn't do it for us, call gettimeofday() on every UDP receive?
|
|
|
|
*/
|
|
|
|
#ifdef SO_TIMESTAMP
|
|
|
|
#ifndef USE_CMSG
|
2020-02-12 13:59:18 +01:00
|
|
|
#define USE_CMSG 1
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef USE_CMSG */
|
|
|
|
#endif /* ifdef SO_TIMESTAMP */
|
1999-12-04 01:27:44 +00:00
|
|
|
|
2020-04-30 00:25:09 +02:00
|
|
|
#if defined(SO_RCVBUF) && defined(ISC_RECV_BUFFER_SIZE)
|
|
|
|
#define SET_RCVBUF
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SO_SNDBUF) && defined(ISC_SEND_BUFFER_SIZE)
|
|
|
|
#define SET_SNDBUF
|
|
|
|
#endif
|
2005-07-12 00:41:55 +00:00
|
|
|
|
2018-06-26 15:11:28 +02:00
|
|
|
/*%
|
|
|
|
* Instead of calculating the cmsgbuf lengths every time we take
|
|
|
|
* a rule of thumb approach - sizes are taken from x86_64 linux,
|
|
|
|
* multiplied by 2, everything should fit. Those sizes are not
|
|
|
|
* large enough to cause any concern.
|
|
|
|
*/
|
2018-08-21 13:22:40 +02:00
|
|
|
#if defined(USE_CMSG)
|
2018-06-26 15:11:28 +02:00
|
|
|
#define CMSG_SP_IN6PKT 40
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(USE_CMSG) */
|
2018-06-26 15:11:28 +02:00
|
|
|
#define CMSG_SP_IN6PKT 0
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_CMSG) */
|
2018-06-26 15:11:28 +02:00
|
|
|
|
|
|
|
#if defined(USE_CMSG) && defined(SO_TIMESTAMP)
|
|
|
|
#define CMSG_SP_TIMESTAMP 32
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(USE_CMSG) && defined(SO_TIMESTAMP) */
|
2018-06-26 15:11:28 +02:00
|
|
|
#define CMSG_SP_TIMESTAMP 0
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_CMSG) && defined(SO_TIMESTAMP) */
|
2018-06-26 15:11:28 +02:00
|
|
|
|
|
|
|
#if defined(USE_CMSG) && (defined(IPV6_TCLASS) || defined(IP_TOS))
|
|
|
|
#define CMSG_SP_TCTOS 24
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(USE_CMSG) && (defined(IPV6_TCLASS) || defined(IP_TOS)) */
|
2018-06-26 15:11:28 +02:00
|
|
|
#define CMSG_SP_TCTOS 0
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_CMSG) && (defined(IPV6_TCLASS) || defined(IP_TOS)) */
|
2018-06-26 15:11:28 +02:00
|
|
|
|
|
|
|
#define CMSG_SP_INT 24
|
|
|
|
|
2018-09-07 09:34:32 +02:00
|
|
|
/* Align cmsg buffers to be safe on SPARC etc. */
|
2020-02-12 13:59:18 +01:00
|
|
|
#define RECVCMSGBUFLEN \
|
|
|
|
ISC_ALIGN(2 * (CMSG_SP_IN6PKT + CMSG_SP_TIMESTAMP + CMSG_SP_TCTOS) + \
|
|
|
|
1, \
|
|
|
|
sizeof(void *))
|
|
|
|
#define SENDCMSGBUFLEN \
|
|
|
|
ISC_ALIGN(2 * (CMSG_SP_IN6PKT + CMSG_SP_INT + CMSG_SP_TCTOS) + 1, \
|
|
|
|
sizeof(void *))
|
2018-06-26 15:11:28 +02:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2001-03-06 01:23:03 +00:00
|
|
|
* The number of times a send operation is repeated if the result is EINTR.
|
|
|
|
*/
|
|
|
|
#define NRETRIES 10
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
typedef struct isc__socketthread isc__socketthread_t;
|
2009-09-01 00:22:28 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
#define NEWCONNSOCK(ev) ((ev)->newsocket)
|
2009-09-01 00:22:28 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
struct isc_socket {
|
1998-11-03 00:54:47 +00:00
|
|
|
/* Not locked. */
|
2021-04-19 11:12:58 +02:00
|
|
|
unsigned int magic;
|
|
|
|
isc_socketmgr_t *manager;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mutex_t lock;
|
|
|
|
isc_sockettype_t type;
|
2020-02-12 13:59:18 +01:00
|
|
|
const isc_statscounter_t *statsindex;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_refcount_t references;
|
1998-12-04 11:21:11 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/* Locked by socket lock. */
|
2021-04-19 11:12:58 +02:00
|
|
|
ISC_LINK(isc_socket_t) link;
|
2020-02-13 14:44:37 -08:00
|
|
|
int fd;
|
|
|
|
int pf;
|
|
|
|
int threadid;
|
|
|
|
char name[16];
|
2020-02-12 13:59:18 +01:00
|
|
|
void *tag;
|
|
|
|
|
|
|
|
ISC_LIST(isc_socketevent_t) send_list;
|
|
|
|
ISC_LIST(isc_socketevent_t) recv_list;
|
|
|
|
ISC_LIST(isc_socket_newconnev_t) accept_list;
|
|
|
|
ISC_LIST(isc_socket_connev_t) connect_list;
|
|
|
|
|
|
|
|
isc_sockaddr_t peer_address; /* remote address */
|
|
|
|
|
|
|
|
unsigned int listener : 1, /* listener socket */
|
2020-02-13 21:48:23 +01:00
|
|
|
connected : 1, connecting : 1, /* connect pending
|
|
|
|
* */
|
2020-08-24 09:23:19 +02:00
|
|
|
bound : 1, /* bound to local addr */
|
2020-02-12 13:59:18 +01:00
|
|
|
dupped : 1, active : 1, /* currently active */
|
|
|
|
pktdscp : 1; /* per packet dscp */
|
1999-08-28 04:26:49 +00:00
|
|
|
|
2018-05-01 16:52:01 +10:00
|
|
|
#ifdef ISC_PLATFORM_RECVOVERFLOW
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned char overflow; /* used for MSG_TRUNC fake */
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef ISC_PLATFORM_RECVOVERFLOW */
|
2004-01-29 04:39:19 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int dscp;
|
1998-11-03 00:54:47 +00:00
|
|
|
};
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define SOCKET_MANAGER_MAGIC ISC_MAGIC('I', 'O', 'm', 'g')
|
2020-02-13 14:44:37 -08:00
|
|
|
#define VALID_MANAGER(m) ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
|
2001-06-04 19:33:39 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
struct isc_socketmgr {
|
1998-11-03 00:54:47 +00:00
|
|
|
/* Not locked. */
|
2021-04-19 11:12:58 +02:00
|
|
|
unsigned int magic;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx;
|
|
|
|
isc_mutex_t lock;
|
|
|
|
isc_stats_t *stats;
|
|
|
|
int nthreads;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__socketthread_t *threads;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int maxsocks;
|
2018-10-03 22:46:23 +02:00
|
|
|
/* Locked by manager lock. */
|
2021-04-19 11:12:58 +02:00
|
|
|
ISC_LIST(isc_socket_t) socklist;
|
2020-02-13 14:44:37 -08:00
|
|
|
int reserved; /* unlocked */
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_condition_t shutdown_ok;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t maxudp;
|
2018-10-03 22:46:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct isc__socketthread {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_t *manager;
|
2020-02-13 14:44:37 -08:00
|
|
|
int threadid;
|
|
|
|
isc_thread_t thread;
|
|
|
|
int pipe_fds[2];
|
|
|
|
isc_mutex_t *fdlock;
|
2018-10-03 22:46:23 +02:00
|
|
|
/* Locked by fdlock. */
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_t **fds;
|
2020-02-13 14:44:37 -08:00
|
|
|
int *fdstate;
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_KQUEUE
|
2020-02-13 14:44:37 -08:00
|
|
|
int kqueue_fd;
|
|
|
|
int nevents;
|
2020-02-12 13:59:18 +01:00
|
|
|
struct kevent *events;
|
|
|
|
#endif /* USE_KQUEUE */
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_EPOLL
|
2020-02-13 14:44:37 -08:00
|
|
|
int epoll_fd;
|
|
|
|
int nevents;
|
2020-02-12 13:59:18 +01:00
|
|
|
struct epoll_event *events;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t *epoll_events;
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* USE_EPOLL */
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_DEVPOLL
|
2020-02-13 14:44:37 -08:00
|
|
|
int devpoll_fd;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_resourcevalue_t open_max;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int calls;
|
|
|
|
int nevents;
|
|
|
|
struct pollfd *events;
|
|
|
|
pollinfo_t *fdpollinfo;
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* USE_DEVPOLL */
|
2008-08-07 21:28:13 +00:00
|
|
|
#ifdef USE_SELECT
|
2020-02-13 14:44:37 -08:00
|
|
|
int fd_bufsize;
|
2020-02-12 13:59:18 +01:00
|
|
|
fd_set *read_fds;
|
|
|
|
fd_set *read_fds_copy;
|
|
|
|
fd_set *write_fds;
|
|
|
|
fd_set *write_fds_copy;
|
2020-02-13 14:44:37 -08:00
|
|
|
int maxfd;
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* USE_SELECT */
|
1998-11-03 00:54:47 +00:00
|
|
|
};
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define CLOSED 0 /* this one must be zero */
|
|
|
|
#define MANAGED 1
|
2020-02-12 13:59:18 +01:00
|
|
|
#define CLOSE_PENDING 2
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-09-13 22:00:28 +00:00
|
|
|
/*
|
|
|
|
* send() and recv() iovec counts
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
#define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER)
|
2018-05-01 16:52:01 +10:00
|
|
|
#ifdef ISC_PLATFORM_RECVOVERFLOW
|
2020-02-12 13:59:18 +01:00
|
|
|
#define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER + 1)
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef ISC_PLATFORM_RECVOVERFLOW */
|
2020-02-12 13:59:18 +01:00
|
|
|
#define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef ISC_PLATFORM_RECVOVERFLOW */
|
1999-09-13 22:00:28 +00:00
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static isc_result_t
|
|
|
|
socket_create(isc_socketmgr_t *manager0, int pf, isc_sockettype_t type,
|
|
|
|
isc_socket_t **socketp, isc_socket_t *dup_socket);
|
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
send_recvdone_event(isc_socket_t *, isc_socketevent_t **);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
send_senddone_event(isc_socket_t *, isc_socketevent_t **);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
send_connectdone_event(isc_socket_t *, isc_socket_connev_t **);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
free_socket(isc_socket_t **);
|
2020-02-14 08:14:03 +01:00
|
|
|
static isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
allocate_socket(isc_socketmgr_t *, isc_sockettype_t, isc_socket_t **);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
destroy(isc_socket_t **);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
internal_accept(isc_socket_t *);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
internal_connect(isc_socket_t *);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
internal_recv(isc_socket_t *);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
internal_send(isc_socket_t *);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
process_cmsg(isc_socket_t *, struct msghdr *, isc_socketevent_t *);
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
build_msghdr_send(isc_socket_t *, char *, isc_socketevent_t *, struct msghdr *,
|
2020-02-14 08:14:03 +01:00
|
|
|
struct iovec *, size_t *);
|
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
build_msghdr_recv(isc_socket_t *, char *, isc_socketevent_t *, struct msghdr *,
|
2020-02-14 08:14:03 +01:00
|
|
|
struct iovec *, size_t *);
|
|
|
|
static bool
|
|
|
|
process_ctlfd(isc__socketthread_t *thread);
|
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
setdscp(isc_socket_t *sock, isc_dscp_t dscp);
|
2020-02-12 13:59:18 +01:00
|
|
|
|
|
|
|
#define SELECT_POKE_SHUTDOWN (-1)
|
2020-02-13 14:44:37 -08:00
|
|
|
#define SELECT_POKE_NOTHING (-2)
|
|
|
|
#define SELECT_POKE_READ (-3)
|
|
|
|
#define SELECT_POKE_ACCEPT (-3) /*%< Same as _READ */
|
|
|
|
#define SELECT_POKE_WRITE (-4)
|
|
|
|
#define SELECT_POKE_CONNECT (-4) /*%< Same as _WRITE */
|
|
|
|
#define SELECT_POKE_CLOSE (-5)
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2009-01-27 22:30:00 +00:00
|
|
|
/*%
|
|
|
|
* Shortcut index arrays to get access to statistics counters.
|
|
|
|
*/
|
2020-08-24 09:23:19 +02:00
|
|
|
enum {
|
|
|
|
STATID_OPEN = 0,
|
|
|
|
STATID_OPENFAIL = 1,
|
|
|
|
STATID_CLOSE = 2,
|
|
|
|
STATID_BINDFAIL = 3,
|
|
|
|
STATID_CONNECTFAIL = 4,
|
|
|
|
STATID_CONNECT = 5,
|
|
|
|
STATID_ACCEPTFAIL = 6,
|
|
|
|
STATID_ACCEPT = 7,
|
|
|
|
STATID_SENDFAIL = 8,
|
|
|
|
STATID_RECVFAIL = 9,
|
|
|
|
STATID_ACTIVE = 10
|
|
|
|
};
|
2012-10-10 13:10:07 +11:00
|
|
|
static const isc_statscounter_t udp4statsindex[] = {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_sockstatscounter_udp4open,
|
|
|
|
isc_sockstatscounter_udp4openfail,
|
|
|
|
isc_sockstatscounter_udp4close,
|
|
|
|
isc_sockstatscounter_udp4bindfail,
|
|
|
|
isc_sockstatscounter_udp4connectfail,
|
|
|
|
isc_sockstatscounter_udp4connect,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
isc_sockstatscounter_udp4sendfail,
|
2012-05-14 10:06:05 -07:00
|
|
|
isc_sockstatscounter_udp4recvfail,
|
|
|
|
isc_sockstatscounter_udp4active
|
2009-01-27 22:30:00 +00:00
|
|
|
};
|
2012-10-10 13:10:07 +11:00
|
|
|
static const isc_statscounter_t udp6statsindex[] = {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_sockstatscounter_udp6open,
|
|
|
|
isc_sockstatscounter_udp6openfail,
|
|
|
|
isc_sockstatscounter_udp6close,
|
|
|
|
isc_sockstatscounter_udp6bindfail,
|
|
|
|
isc_sockstatscounter_udp6connectfail,
|
|
|
|
isc_sockstatscounter_udp6connect,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
isc_sockstatscounter_udp6sendfail,
|
2012-05-14 10:06:05 -07:00
|
|
|
isc_sockstatscounter_udp6recvfail,
|
|
|
|
isc_sockstatscounter_udp6active
|
2009-01-27 22:30:00 +00:00
|
|
|
};
|
|
|
|
static const isc_statscounter_t tcp4statsindex[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_sockstatscounter_tcp4open, isc_sockstatscounter_tcp4openfail,
|
|
|
|
isc_sockstatscounter_tcp4close, isc_sockstatscounter_tcp4bindfail,
|
|
|
|
isc_sockstatscounter_tcp4connectfail, isc_sockstatscounter_tcp4connect,
|
|
|
|
isc_sockstatscounter_tcp4acceptfail, isc_sockstatscounter_tcp4accept,
|
|
|
|
isc_sockstatscounter_tcp4sendfail, isc_sockstatscounter_tcp4recvfail,
|
2012-05-14 10:06:05 -07:00
|
|
|
isc_sockstatscounter_tcp4active
|
2009-01-27 22:30:00 +00:00
|
|
|
};
|
|
|
|
static const isc_statscounter_t tcp6statsindex[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_sockstatscounter_tcp6open, isc_sockstatscounter_tcp6openfail,
|
|
|
|
isc_sockstatscounter_tcp6close, isc_sockstatscounter_tcp6bindfail,
|
|
|
|
isc_sockstatscounter_tcp6connectfail, isc_sockstatscounter_tcp6connect,
|
|
|
|
isc_sockstatscounter_tcp6acceptfail, isc_sockstatscounter_tcp6accept,
|
|
|
|
isc_sockstatscounter_tcp6sendfail, isc_sockstatscounter_tcp6recvfail,
|
2012-05-14 10:06:05 -07:00
|
|
|
isc_sockstatscounter_tcp6active
|
2009-01-27 22:30:00 +00:00
|
|
|
};
|
|
|
|
static const isc_statscounter_t unixstatsindex[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_sockstatscounter_unixopen, isc_sockstatscounter_unixopenfail,
|
|
|
|
isc_sockstatscounter_unixclose, isc_sockstatscounter_unixbindfail,
|
|
|
|
isc_sockstatscounter_unixconnectfail, isc_sockstatscounter_unixconnect,
|
|
|
|
isc_sockstatscounter_unixacceptfail, isc_sockstatscounter_unixaccept,
|
|
|
|
isc_sockstatscounter_unixsendfail, isc_sockstatscounter_unixrecvfail,
|
2012-05-14 10:06:05 -07:00
|
|
|
isc_sockstatscounter_unixactive
|
2009-01-27 22:30:00 +00:00
|
|
|
};
|
2014-02-07 17:16:37 +11:00
|
|
|
static const isc_statscounter_t rawstatsindex[] = {
|
|
|
|
isc_sockstatscounter_rawopen,
|
|
|
|
isc_sockstatscounter_rawopenfail,
|
|
|
|
isc_sockstatscounter_rawclose,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
isc_sockstatscounter_rawrecvfail,
|
|
|
|
isc_sockstatscounter_rawactive
|
|
|
|
};
|
2009-01-27 22:30:00 +00:00
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static int
|
2021-04-19 11:12:58 +02:00
|
|
|
gen_threadid(isc_socket_t *sock);
|
2018-10-04 16:11:24 +02:00
|
|
|
|
|
|
|
static int
|
2021-04-19 11:12:58 +02:00
|
|
|
gen_threadid(isc_socket_t *sock) {
|
2019-07-01 15:19:53 +02:00
|
|
|
return (sock->fd % sock->manager->nthreads);
|
2018-10-04 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
manager_log(isc_socketmgr_t *sockmgr, isc_logcategory_t *category,
|
2020-02-14 08:14:03 +01:00
|
|
|
isc_logmodule_t *module, int level, const char *fmt, ...)
|
|
|
|
ISC_FORMAT_PRINTF(5, 6);
|
2001-08-08 22:54:55 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
manager_log(isc_socketmgr_t *sockmgr, isc_logcategory_t *category,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_logmodule_t *module, int level, const char *fmt, ...) {
|
|
|
|
char msgbuf[2048];
|
2000-05-03 21:09:49 +00:00
|
|
|
va_list ap;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (!isc_log_wouldlog(isc_lctx, level)) {
|
2000-07-13 01:38:40 +00:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-07-13 01:38:40 +00:00
|
|
|
|
2000-05-03 21:09:49 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_log_write(isc_lctx, category, module, level, "sockmgr %p: %s",
|
|
|
|
sockmgr, msgbuf);
|
2000-05-03 21:09:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
|
|
|
thread_log(isc__socketthread_t *thread, isc_logcategory_t *category,
|
|
|
|
isc_logmodule_t *module, int level, const char *fmt, ...)
|
2020-02-12 13:59:18 +01:00
|
|
|
ISC_FORMAT_PRINTF(5, 6);
|
2018-10-03 22:46:23 +02:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
thread_log(isc__socketthread_t *thread, isc_logcategory_t *category,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_logmodule_t *module, int level, const char *fmt, ...) {
|
|
|
|
char msgbuf[2048];
|
2018-10-03 22:46:23 +02:00
|
|
|
va_list ap;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
if (!isc_log_wouldlog(isc_lctx, level)) {
|
2018-10-03 22:46:23 +02:00
|
|
|
return;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
isc_log_write(isc_lctx, category, module, level,
|
2020-02-12 13:59:18 +01:00
|
|
|
"sockmgr %p thread %d: %s", thread->manager,
|
|
|
|
thread->threadid, msgbuf);
|
2018-10-03 22:46:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
socket_log(isc_socket_t *sock, const isc_sockaddr_t *address,
|
2020-02-14 08:14:03 +01:00
|
|
|
isc_logcategory_t *category, isc_logmodule_t *module, int level,
|
|
|
|
const char *fmt, ...) ISC_FORMAT_PRINTF(6, 7);
|
2001-08-08 22:54:55 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
socket_log(isc_socket_t *sock, const isc_sockaddr_t *address,
|
2001-08-08 22:54:55 +00:00
|
|
|
isc_logcategory_t *category, isc_logmodule_t *module, int level,
|
2020-02-13 14:44:37 -08:00
|
|
|
const char *fmt, ...) {
|
|
|
|
char msgbuf[2048];
|
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
2000-05-03 21:09:49 +00:00
|
|
|
va_list ap;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (!isc_log_wouldlog(isc_lctx, level)) {
|
2000-07-13 00:25:38 +00:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-07-13 00:25:38 +00:00
|
|
|
|
2000-05-03 21:09:49 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (address == NULL) {
|
2018-11-23 21:35:01 +01:00
|
|
|
isc_log_write(isc_lctx, category, module, level,
|
|
|
|
"socket %p: %s", sock, msgbuf);
|
2000-05-03 21:09:49 +00:00
|
|
|
} else {
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_sockaddr_format(address, peerbuf, sizeof(peerbuf));
|
2018-11-23 21:35:01 +01:00
|
|
|
isc_log_write(isc_lctx, category, module, level,
|
|
|
|
"socket %p %s: %s", sock, peerbuf, msgbuf);
|
2000-05-03 21:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-27 22:30:00 +00:00
|
|
|
/*%
|
|
|
|
* Increment socket-related statistics counters.
|
|
|
|
*/
|
|
|
|
static inline void
|
2020-02-13 14:44:37 -08:00
|
|
|
inc_stats(isc_stats_t *stats, isc_statscounter_t counterid) {
|
2009-01-27 22:30:00 +00:00
|
|
|
REQUIRE(counterid != -1);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (stats != NULL) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_increment(stats, counterid);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-01-27 22:30:00 +00:00
|
|
|
}
|
|
|
|
|
2012-05-14 10:06:05 -07:00
|
|
|
/*%
|
|
|
|
* Decrement socket-related statistics counters.
|
|
|
|
*/
|
|
|
|
static inline void
|
2020-02-13 14:44:37 -08:00
|
|
|
dec_stats(isc_stats_t *stats, isc_statscounter_t counterid) {
|
2012-05-14 10:06:05 -07:00
|
|
|
REQUIRE(counterid != -1);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (stats != NULL) {
|
2012-05-14 10:06:05 -07:00
|
|
|
isc_stats_decrement(stats, counterid);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-05-14 10:06:05 -07:00
|
|
|
}
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
static inline isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
watch_fd(isc__socketthread_t *thread, int fd, int msg) {
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
struct kevent evchange;
|
|
|
|
|
|
|
|
memset(&evchange, 0, sizeof(evchange));
|
2018-11-08 19:34:51 -08:00
|
|
|
if (msg == SELECT_POKE_READ) {
|
2008-06-23 19:41:20 +00:00
|
|
|
evchange.filter = EVFILT_READ;
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2008-06-23 19:41:20 +00:00
|
|
|
evchange.filter = EVFILT_WRITE;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
evchange.flags = EV_ADD;
|
|
|
|
evchange.ident = fd;
|
2018-11-08 19:34:51 -08:00
|
|
|
if (kevent(thread->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0) {
|
2008-06-23 19:41:20 +00:00
|
|
|
result = isc__errno2result(errno);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_EPOLL)
|
|
|
|
struct epoll_event event;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t oldevents;
|
|
|
|
int ret;
|
|
|
|
int op;
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
oldevents = thread->epoll_events[fd];
|
2018-11-08 19:34:51 -08:00
|
|
|
if (msg == SELECT_POKE_READ) {
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->epoll_events[fd] |= EPOLLIN;
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->epoll_events[fd] |= EPOLLOUT;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2015-11-19 11:01:45 +05:30
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
event.events = thread->epoll_events[fd];
|
2010-03-12 03:22:57 +00:00
|
|
|
memset(&event.data, 0, sizeof(event.data));
|
2008-06-23 19:41:20 +00:00
|
|
|
event.data.fd = fd;
|
2015-11-19 11:01:45 +05:30
|
|
|
|
|
|
|
op = (oldevents == 0U) ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
|
2020-09-23 13:54:06 +10:00
|
|
|
if (thread->fds[fd] != NULL) {
|
|
|
|
LOCK(&thread->fds[fd]->lock);
|
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
ret = epoll_ctl(thread->epoll_fd, op, fd, &event);
|
2020-09-23 13:54:06 +10:00
|
|
|
if (thread->fds[fd] != NULL) {
|
|
|
|
UNLOCK(&thread->fds[fd]->lock);
|
|
|
|
}
|
2015-11-19 11:01:45 +05:30
|
|
|
if (ret == -1) {
|
2018-11-08 19:34:51 -08:00
|
|
|
if (errno == EEXIST) {
|
2015-11-19 11:01:45 +05:30
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"epoll_ctl(ADD/MOD) returned "
|
2020-02-12 13:59:18 +01:00
|
|
|
"EEXIST for fd %d",
|
|
|
|
fd);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
result = isc__errno2result(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
struct pollfd pfd;
|
|
|
|
|
|
|
|
memset(&pfd, 0, sizeof(pfd));
|
2018-11-08 19:34:51 -08:00
|
|
|
if (msg == SELECT_POKE_READ) {
|
2008-06-23 19:41:20 +00:00
|
|
|
pfd.events = POLLIN;
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2008-06-23 19:41:20 +00:00
|
|
|
pfd.events = POLLOUT;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.revents = 0;
|
2018-11-08 19:34:51 -08:00
|
|
|
if (write(thread->devpoll_fd, &pfd, sizeof(pfd)) == -1) {
|
2008-06-23 19:41:20 +00:00
|
|
|
result = isc__errno2result(errno);
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
|
|
|
if (msg == SELECT_POKE_READ) {
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->fdpollinfo[fd].want_read = 1;
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->fdpollinfo[fd].want_write = 1;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_SELECT)
|
2018-10-03 22:46:23 +02:00
|
|
|
LOCK(&thread->manager->lock);
|
2018-11-08 19:34:51 -08:00
|
|
|
if (msg == SELECT_POKE_READ) {
|
2018-10-03 22:46:23 +02:00
|
|
|
FD_SET(fd, thread->read_fds);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
|
|
|
if (msg == SELECT_POKE_WRITE) {
|
2018-10-03 22:46:23 +02:00
|
|
|
FD_SET(fd, thread->write_fds);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
UNLOCK(&thread->manager->lock);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_KQUEUE */
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
unwatch_fd(isc__socketthread_t *thread, int fd, int msg) {
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
struct kevent evchange;
|
|
|
|
|
|
|
|
memset(&evchange, 0, sizeof(evchange));
|
2018-11-08 19:34:51 -08:00
|
|
|
if (msg == SELECT_POKE_READ) {
|
2008-06-23 19:41:20 +00:00
|
|
|
evchange.filter = EVFILT_READ;
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2008-06-23 19:41:20 +00:00
|
|
|
evchange.filter = EVFILT_WRITE;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
evchange.flags = EV_DELETE;
|
|
|
|
evchange.ident = fd;
|
2018-11-08 19:34:51 -08:00
|
|
|
if (kevent(thread->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0) {
|
2008-06-23 19:41:20 +00:00
|
|
|
result = isc__errno2result(errno);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_EPOLL)
|
|
|
|
struct epoll_event event;
|
2020-02-13 14:44:37 -08:00
|
|
|
int ret;
|
|
|
|
int op;
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
if (msg == SELECT_POKE_READ) {
|
|
|
|
thread->epoll_events[fd] &= ~(EPOLLIN);
|
|
|
|
} else {
|
|
|
|
thread->epoll_events[fd] &= ~(EPOLLOUT);
|
|
|
|
}
|
2015-11-19 11:01:45 +05:30
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
event.events = thread->epoll_events[fd];
|
2010-03-12 03:22:57 +00:00
|
|
|
memset(&event.data, 0, sizeof(event.data));
|
2008-06-23 19:41:20 +00:00
|
|
|
event.data.fd = fd;
|
2015-11-19 11:01:45 +05:30
|
|
|
|
|
|
|
op = (event.events == 0U) ? EPOLL_CTL_DEL : EPOLL_CTL_MOD;
|
2018-10-03 22:46:23 +02:00
|
|
|
ret = epoll_ctl(thread->epoll_fd, op, fd, &event);
|
2015-11-19 11:01:45 +05:30
|
|
|
if (ret == -1 && errno != ENOENT) {
|
2008-06-23 19:41:20 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2020-02-12 13:59:18 +01:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "epoll_ctl(DEL), %d: %s",
|
|
|
|
fd, strbuf);
|
2008-06-23 19:41:20 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
struct pollfd pfds[2];
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t writelen = sizeof(pfds[0]);
|
|
|
|
int lockid = FDLOCK_ID(fd);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
memset(pfds, 0, sizeof(pfds));
|
|
|
|
pfds[0].events = POLLREMOVE;
|
|
|
|
pfds[0].fd = fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Canceling read or write polling via /dev/poll is tricky. Since it
|
|
|
|
* only provides a way of canceling per FD, we may need to re-poll the
|
|
|
|
* socket for the other operation.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
if (msg == SELECT_POKE_READ && thread->fdpollinfo[fd].want_write == 1) {
|
2008-06-23 19:41:20 +00:00
|
|
|
pfds[1].events = POLLOUT;
|
|
|
|
pfds[1].fd = fd;
|
|
|
|
writelen += sizeof(pfds[1]);
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
if (msg == SELECT_POKE_WRITE && thread->fdpollinfo[fd].want_read == 1) {
|
2008-06-23 19:41:20 +00:00
|
|
|
pfds[1].events = POLLIN;
|
|
|
|
pfds[1].fd = fd;
|
|
|
|
writelen += sizeof(pfds[1]);
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if (write(thread->devpoll_fd, pfds, writelen) == -1) {
|
2008-06-23 19:41:20 +00:00
|
|
|
result = isc__errno2result(errno);
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
|
|
|
if (msg == SELECT_POKE_READ) {
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->fdpollinfo[fd].want_read = 0;
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->fdpollinfo[fd].want_write = 0;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_SELECT)
|
2018-10-03 22:46:23 +02:00
|
|
|
LOCK(&thread->manager->lock);
|
2018-11-08 19:34:51 -08:00
|
|
|
if (msg == SELECT_POKE_READ) {
|
2018-10-03 22:46:23 +02:00
|
|
|
FD_CLR(fd, thread->read_fds);
|
2018-11-08 19:34:51 -08:00
|
|
|
} else if (msg == SELECT_POKE_WRITE) {
|
2018-10-03 22:46:23 +02:00
|
|
|
FD_CLR(fd, thread->write_fds);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
UNLOCK(&thread->manager->lock);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_KQUEUE */
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 10:20:29 +00:00
|
|
|
/*
|
|
|
|
* A poke message was received, perform a proper watch/unwatch
|
|
|
|
* on a fd provided
|
|
|
|
*/
|
2000-08-29 23:58:17 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
wakeup_socket(isc__socketthread_t *thread, int fd, int msg) {
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
int lockid = FDLOCK_ID(fd);
|
2000-08-29 23:58:17 +00:00
|
|
|
|
|
|
|
/*
|
2001-01-25 22:25:10 +00:00
|
|
|
* This is a wakeup on a socket. If the socket is not in the
|
|
|
|
* process of being closed, start watching it for either reads
|
|
|
|
* or writes.
|
2000-08-29 23:58:17 +00:00
|
|
|
*/
|
2001-04-26 23:45:55 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
INSIST(fd >= 0 && fd < (int)thread->manager->maxsocks);
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2008-08-01 19:04:02 +00:00
|
|
|
if (msg == SELECT_POKE_CLOSE) {
|
2019-07-01 15:19:53 +02:00
|
|
|
LOCK(&thread->fdlock[lockid]);
|
2018-10-03 22:46:23 +02:00
|
|
|
INSIST(thread->fdstate[fd] == CLOSE_PENDING);
|
|
|
|
thread->fdstate[fd] = CLOSED;
|
|
|
|
(void)unwatch_fd(thread, fd, SELECT_POKE_READ);
|
|
|
|
(void)unwatch_fd(thread, fd, SELECT_POKE_WRITE);
|
2008-08-01 19:04:02 +00:00
|
|
|
(void)close(fd);
|
2019-07-20 16:10:49 -04:00
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2008-08-01 19:04:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
LOCK(&thread->fdlock[lockid]);
|
|
|
|
if (thread->fdstate[fd] == CLOSE_PENDING) {
|
2008-06-23 19:41:20 +00:00
|
|
|
/*
|
|
|
|
* We accept (and ignore) any error from unwatch_fd() as we are
|
|
|
|
* closing the socket, hoping it doesn't leave dangling state in
|
|
|
|
* the kernel.
|
|
|
|
* Note that unwatch_fd() must be called after releasing the
|
|
|
|
* fdlock; otherwise it could cause deadlock due to a lock order
|
|
|
|
* reversal.
|
|
|
|
*/
|
2018-10-03 22:46:23 +02:00
|
|
|
(void)unwatch_fd(thread, fd, SELECT_POKE_READ);
|
|
|
|
(void)unwatch_fd(thread, fd, SELECT_POKE_WRITE);
|
2019-07-20 16:10:49 -04:00
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2000-08-29 23:58:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
if (thread->fdstate[fd] != MANAGED) {
|
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2000-08-29 23:58:17 +00:00
|
|
|
return;
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
2000-08-29 23:58:17 +00:00
|
|
|
|
|
|
|
/*
|
2001-12-19 05:51:34 +00:00
|
|
|
* Set requested bit.
|
2000-08-29 23:58:17 +00:00
|
|
|
*/
|
2018-10-03 22:46:23 +02:00
|
|
|
result = watch_fd(thread, fd, msg);
|
2008-06-23 19:41:20 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
/*
|
|
|
|
* XXXJT: what should we do? Ignoring the failure of watching
|
|
|
|
* a socket will make the application dysfunctional, but there
|
|
|
|
* seems to be no reasonable recovery process.
|
|
|
|
*/
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
2020-02-12 13:59:18 +01:00
|
|
|
"failed to start watching FD (%d): %s", fd,
|
|
|
|
isc_result_totext(result));
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
2020-09-02 14:42:47 +10:00
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
1998-12-04 11:21:11 +00:00
|
|
|
* Poke the select loop when there is something for us to do.
|
2001-01-25 22:25:10 +00:00
|
|
|
* The write is required (by POSIX) to complete. That is, we
|
|
|
|
* will not get partial writes.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
select_poke(isc_socketmgr_t *mgr, int threadid, int fd, int msg) {
|
2020-02-13 14:44:37 -08:00
|
|
|
int cc;
|
|
|
|
int buf[2];
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2001-01-25 22:25:10 +00:00
|
|
|
|
|
|
|
buf[0] = fd;
|
|
|
|
buf[1] = msg;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-02-11 06:38:12 +00:00
|
|
|
do {
|
2020-02-12 13:59:18 +01:00
|
|
|
cc = write(mgr->threads[threadid].pipe_fds[1], buf,
|
|
|
|
sizeof(buf));
|
2002-01-22 02:38:34 +00:00
|
|
|
#ifdef ENOSR
|
|
|
|
/*
|
|
|
|
* Treat ENOSR as EAGAIN but loop slowly as it is
|
|
|
|
* unlikely to clear fast.
|
|
|
|
*/
|
|
|
|
if (cc < 0 && errno == ENOSR) {
|
|
|
|
sleep(1);
|
|
|
|
errno = EAGAIN;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef ENOSR */
|
1999-02-11 06:38:12 +00:00
|
|
|
} while (cc < 0 && SOFT_ERROR(errno));
|
2005-05-19 02:42:42 +00:00
|
|
|
|
2001-08-31 05:57:58 +00:00
|
|
|
if (cc < 0) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
1998-12-04 11:21:11 +00:00
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"write() failed during watcher poke: %s", strbuf);
|
2001-08-31 05:57:58 +00:00
|
|
|
}
|
1999-02-11 06:38:12 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
INSIST(cc == sizeof(buf));
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Read a message on the internal fd.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
2001-01-25 22:25:10 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
select_readmsg(isc__socketthread_t *thread, int *fd, int *msg) {
|
|
|
|
int buf[2];
|
|
|
|
int cc;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
cc = read(thread->pipe_fds[0], buf, sizeof(buf));
|
1998-11-06 01:45:35 +00:00
|
|
|
if (cc < 0) {
|
2001-01-25 22:25:10 +00:00
|
|
|
*msg = SELECT_POKE_NOTHING;
|
2020-02-12 13:59:18 +01:00
|
|
|
*fd = -1; /* Silence compiler. */
|
2020-02-13 21:48:23 +01:00
|
|
|
if (SOFT_ERROR(errno)) {
|
2001-01-25 22:25:10 +00:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
1998-12-04 11:21:11 +00:00
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"read() failed during watcher poke: %s", strbuf);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
INSIST(cc == sizeof(buf));
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
*fd = buf[0];
|
|
|
|
*msg = buf[1];
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Make a fd non-blocking.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
1998-11-06 01:45:35 +00:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
make_nonblock(int fd) {
|
|
|
|
int ret;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2005-07-08 04:30:22 +00:00
|
|
|
#ifdef USE_FIONBIO_IOCTL
|
|
|
|
int on = 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef USE_FIONBIO_IOCTL */
|
2018-02-20 13:08:08 +11:00
|
|
|
int flags;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_FIONBIO_IOCTL */
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2018-02-20 13:08:08 +11:00
|
|
|
#ifdef USE_FIONBIO_IOCTL
|
2005-07-08 04:30:22 +00:00
|
|
|
ret = ioctl(fd, FIONBIO, (char *)&on);
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef USE_FIONBIO_IOCTL */
|
1998-11-06 01:45:35 +00:00
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
2018-08-07 16:46:53 +02:00
|
|
|
flags |= O_NONBLOCK;
|
1998-11-06 01:45:35 +00:00
|
|
|
ret = fcntl(fd, F_SETFL, flags);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_FIONBIO_IOCTL */
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
if (ret == -1) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
1998-11-06 01:45:35 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2005-07-08 04:30:22 +00:00
|
|
|
#ifdef USE_FIONBIO_IOCTL
|
|
|
|
"ioctl(%d, FIONBIO, &on): %s", fd,
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef USE_FIONBIO_IOCTL */
|
2005-07-08 04:30:22 +00:00
|
|
|
"fcntl(%d, F_SETFL, %d): %s", fd, flags,
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_FIONBIO_IOCTL */
|
2005-07-08 04:30:22 +00:00
|
|
|
strbuf);
|
1998-12-04 11:21:11 +00:00
|
|
|
|
1998-12-01 21:39:00 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
1998-12-01 21:39:00 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
2004-01-29 04:39:19 +00:00
|
|
|
#ifdef USE_CMSG
|
|
|
|
/*
|
|
|
|
* Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
|
|
|
|
* In order to ensure as much portability as possible, we provide wrapper
|
|
|
|
* functions of these macros.
|
|
|
|
* Note that cmsg_space() could run slow on OSes that do not have
|
|
|
|
* CMSG_SPACE.
|
|
|
|
*/
|
2018-09-05 14:03:10 +02:00
|
|
|
static inline socklen_t
|
2020-02-13 14:44:37 -08:00
|
|
|
cmsg_len(socklen_t len) {
|
2004-01-29 04:39:19 +00:00
|
|
|
#ifdef CMSG_LEN
|
|
|
|
return (CMSG_LEN(len));
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef CMSG_LEN */
|
2018-09-05 14:03:10 +02:00
|
|
|
socklen_t hdrlen;
|
2004-01-29 04:39:19 +00:00
|
|
|
|
2005-03-30 05:37:08 +00:00
|
|
|
/*
|
|
|
|
* Cast NULL so that any pointer arithmetic performed by CMSG_DATA
|
|
|
|
* is correct.
|
|
|
|
*/
|
2018-09-05 14:03:10 +02:00
|
|
|
hdrlen = (socklen_t)CMSG_DATA(((struct cmsghdr *)NULL));
|
2004-01-29 04:39:19 +00:00
|
|
|
return (hdrlen + len);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef CMSG_LEN */
|
2004-01-29 04:39:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:03:10 +02:00
|
|
|
static inline socklen_t
|
2020-02-13 14:44:37 -08:00
|
|
|
cmsg_space(socklen_t len) {
|
2004-01-29 04:39:19 +00:00
|
|
|
#ifdef CMSG_SPACE
|
|
|
|
return (CMSG_SPACE(len));
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef CMSG_SPACE */
|
2020-02-13 14:44:37 -08:00
|
|
|
struct msghdr msg;
|
2004-01-29 04:39:19 +00:00
|
|
|
struct cmsghdr *cmsgp;
|
|
|
|
/*
|
|
|
|
* XXX: The buffer length is an ad-hoc value, but should be enough
|
|
|
|
* in a practical sense.
|
|
|
|
*/
|
|
|
|
char dummybuf[sizeof(struct cmsghdr) + 1024];
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.msg_control = dummybuf;
|
|
|
|
msg.msg_controllen = sizeof(dummybuf);
|
|
|
|
|
|
|
|
cmsgp = (struct cmsghdr *)dummybuf;
|
|
|
|
cmsgp->cmsg_len = cmsg_len(len);
|
|
|
|
|
|
|
|
cmsgp = CMSG_NXTHDR(&msg, cmsgp);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (cmsgp != NULL) {
|
2004-03-18 02:58:08 +00:00
|
|
|
return ((char *)cmsgp - (char *)msg.msg_control);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2004-01-29 04:39:19 +00:00
|
|
|
return (0);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
#endif /* ifdef CMSG_SPACE */
|
2004-01-29 04:39:19 +00:00
|
|
|
}
|
|
|
|
#endif /* USE_CMSG */
|
|
|
|
|
1999-08-28 04:26:49 +00:00
|
|
|
/*
|
|
|
|
* Process control messages received on a socket.
|
|
|
|
*/
|
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
process_cmsg(isc_socket_t *sock, struct msghdr *msg, isc_socketevent_t *dev) {
|
1999-12-04 01:27:44 +00:00
|
|
|
#ifdef USE_CMSG
|
2020-02-13 14:44:37 -08:00
|
|
|
struct cmsghdr *cmsgp;
|
1999-12-04 01:27:44 +00:00
|
|
|
struct in6_pktinfo *pktinfop;
|
|
|
|
#ifdef SO_TIMESTAMP
|
2012-11-01 11:16:59 +11:00
|
|
|
void *timevalp;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef SO_TIMESTAMP */
|
|
|
|
#endif /* ifdef USE_CMSG */
|
1999-12-04 01:27:44 +00:00
|
|
|
|
2000-05-14 22:35:45 +00:00
|
|
|
/*
|
|
|
|
* sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
|
|
|
|
* msg and dev are used only when ISC_NET_BSD44MSGHDR is defined.
|
|
|
|
* They are all here, outside of the CPP tests, because it is
|
|
|
|
* more consistent with the usual ISC coding style.
|
|
|
|
*/
|
2000-04-17 19:22:44 +00:00
|
|
|
UNUSED(sock);
|
2000-05-14 22:35:45 +00:00
|
|
|
UNUSED(msg);
|
|
|
|
UNUSED(dev);
|
|
|
|
|
1999-08-28 19:30:23 +00:00
|
|
|
#ifdef MSG_TRUNC
|
2018-10-11 11:57:57 +02:00
|
|
|
if ((msg->msg_flags & MSG_TRUNC) != 0) {
|
1999-08-28 04:26:49 +00:00
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
|
2018-10-11 11:57:57 +02:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef MSG_TRUNC */
|
1999-08-28 04:26:49 +00:00
|
|
|
|
1999-08-28 19:30:23 +00:00
|
|
|
#ifdef MSG_CTRUNC
|
2018-10-11 11:57:57 +02:00
|
|
|
if ((msg->msg_flags & MSG_CTRUNC) != 0) {
|
1999-08-28 04:26:49 +00:00
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_CTRUNC;
|
2018-10-11 11:57:57 +02:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef MSG_CTRUNC */
|
1999-08-28 04:26:49 +00:00
|
|
|
|
1999-12-04 01:27:44 +00:00
|
|
|
#ifndef USE_CMSG
|
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifndef USE_CMSG */
|
|
|
|
if (msg->msg_controllen == 0U || msg->msg_control == NULL) {
|
1999-08-28 04:26:49 +00:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-12-04 01:27:44 +00:00
|
|
|
|
|
|
|
#ifdef SO_TIMESTAMP
|
|
|
|
timevalp = NULL;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef SO_TIMESTAMP */
|
1999-12-04 01:27:44 +00:00
|
|
|
pktinfop = NULL;
|
|
|
|
|
|
|
|
cmsgp = CMSG_FIRSTHDR(msg);
|
|
|
|
while (cmsgp != NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
socket_log(sock, NULL, TRACE, "processing cmsg %p", cmsgp);
|
2000-05-23 02:20:23 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
if (cmsgp->cmsg_level == IPPROTO_IPV6 &&
|
|
|
|
cmsgp->cmsg_type == IPV6_PKTINFO) {
|
1999-12-04 01:27:44 +00:00
|
|
|
pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(&dev->pktinfo, pktinfop,
|
|
|
|
sizeof(struct in6_pktinfo));
|
1999-12-04 01:27:44 +00:00
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
|
2000-05-31 23:03:01 +00:00
|
|
|
socket_log(sock, NULL, TRACE,
|
2000-12-19 20:35:37 +00:00
|
|
|
"interface received on ifindex %u",
|
2000-05-24 05:10:00 +00:00
|
|
|
dev->pktinfo.ipi6_ifindex);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (IN6_IS_ADDR_MULTICAST(&pktinfop->ipi6_addr)) {
|
2008-01-18 23:46:58 +00:00
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_MULTICAST;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-12-04 01:27:44 +00:00
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SO_TIMESTAMP
|
2020-02-12 13:59:18 +01:00
|
|
|
if (cmsgp->cmsg_level == SOL_SOCKET &&
|
|
|
|
cmsgp->cmsg_type == SCM_TIMESTAMP) {
|
2012-11-01 11:16:59 +11:00
|
|
|
struct timeval tv;
|
|
|
|
timevalp = CMSG_DATA(cmsgp);
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(&tv, timevalp, sizeof(tv));
|
2012-11-01 11:16:59 +11:00
|
|
|
dev->timestamp.seconds = tv.tv_sec;
|
|
|
|
dev->timestamp.nanoseconds = tv.tv_usec * 1000;
|
1999-12-04 01:27:44 +00:00
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_TIMESTAMP;
|
|
|
|
goto next;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef SO_TIMESTAMP */
|
1999-12-04 01:27:44 +00:00
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
#ifdef IPV6_TCLASS
|
2020-02-12 13:59:18 +01:00
|
|
|
if (cmsgp->cmsg_level == IPPROTO_IPV6 &&
|
|
|
|
cmsgp->cmsg_type == IPV6_TCLASS) {
|
2013-03-22 12:27:54 -07:00
|
|
|
dev->dscp = *(int *)CMSG_DATA(cmsgp);
|
|
|
|
dev->dscp >>= 2;
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_DSCP;
|
|
|
|
goto next;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IPV6_TCLASS */
|
2013-03-22 12:27:54 -07:00
|
|
|
|
|
|
|
#ifdef IP_TOS
|
2020-02-12 13:59:18 +01:00
|
|
|
if (cmsgp->cmsg_level == IPPROTO_IP &&
|
|
|
|
(cmsgp->cmsg_type == IP_TOS
|
2013-04-12 09:32:41 +10:00
|
|
|
#ifdef IP_RECVTOS
|
2020-02-12 13:59:18 +01:00
|
|
|
|| cmsgp->cmsg_type == IP_RECVTOS
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IP_RECVTOS */
|
2020-02-13 14:44:37 -08:00
|
|
|
))
|
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
dev->dscp = (int)*(unsigned char *)CMSG_DATA(cmsgp);
|
2013-03-22 12:27:54 -07:00
|
|
|
dev->dscp >>= 2;
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_DSCP;
|
|
|
|
goto next;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IP_TOS */
|
1999-12-04 01:27:44 +00:00
|
|
|
next:
|
|
|
|
cmsgp = CMSG_NXTHDR(msg, cmsgp);
|
|
|
|
}
|
|
|
|
#endif /* USE_CMSG */
|
1999-08-28 04:26:49 +00:00
|
|
|
}
|
|
|
|
|
1999-09-02 02:10:44 +00:00
|
|
|
/*
|
2001-01-25 22:25:10 +00:00
|
|
|
* Construct an iov array and attach it to the msghdr passed in. This is
|
|
|
|
* the SEND constructor, which will use the used region of the buffer
|
|
|
|
* (if using a buffer list) or will use the internal region (if a single
|
|
|
|
* buffer I/O is requested).
|
1999-09-02 02:10:44 +00:00
|
|
|
*
|
|
|
|
* Nothing can be NULL, and the done event must list at least one buffer
|
|
|
|
* on the buffer linked list for this function to be meaningful.
|
1999-09-02 23:34:09 +00:00
|
|
|
*
|
|
|
|
* If write_countp != NULL, *write_countp will hold the number of bytes
|
|
|
|
* this transaction can send.
|
1999-09-02 02:10:44 +00:00
|
|
|
*/
|
1999-09-03 01:18:45 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
build_msghdr_send(isc_socket_t *sock, char *cmsgbuf, isc_socketevent_t *dev,
|
2020-02-13 14:44:37 -08:00
|
|
|
struct msghdr *msg, struct iovec *iov, size_t *write_countp) {
|
|
|
|
unsigned int iovcount;
|
|
|
|
size_t write_count;
|
2013-03-22 12:27:54 -07:00
|
|
|
struct cmsghdr *cmsgp;
|
1999-09-02 20:06:55 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
memset(msg, 0, sizeof(*msg));
|
2018-06-26 11:18:43 +02:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
if (!sock->connected) {
|
1999-09-02 20:06:55 +00:00
|
|
|
msg->msg_name = (void *)&dev->address.type.sa;
|
|
|
|
msg->msg_namelen = dev->address.length;
|
1999-09-02 23:34:09 +00:00
|
|
|
} else {
|
|
|
|
msg->msg_name = NULL;
|
|
|
|
msg->msg_namelen = 0;
|
1999-09-02 20:06:55 +00:00
|
|
|
}
|
1999-09-02 02:10:44 +00:00
|
|
|
|
2018-10-26 19:07:24 +00:00
|
|
|
write_count = dev->region.length - dev->n;
|
|
|
|
iov[0].iov_base = (void *)(dev->region.base + dev->n);
|
|
|
|
iov[0].iov_len = write_count;
|
|
|
|
iovcount = 1;
|
1999-09-02 02:10:44 +00:00
|
|
|
|
|
|
|
msg->msg_iov = iov;
|
|
|
|
msg->msg_iovlen = iovcount;
|
1999-09-02 20:06:55 +00:00
|
|
|
msg->msg_control = NULL;
|
|
|
|
msg->msg_controllen = 0;
|
|
|
|
msg->msg_flags = 0;
|
2016-08-12 09:41:59 +10:00
|
|
|
#if defined(USE_CMSG)
|
|
|
|
|
|
|
|
if ((sock->type == isc_sockettype_udp) &&
|
2020-02-13 14:44:37 -08:00
|
|
|
((dev->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0))
|
|
|
|
{
|
2000-05-23 02:20:23 +00:00
|
|
|
struct in6_pktinfo *pktinfop;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
socket_log(sock, NULL, TRACE, "sendto pktinfo data, ifindex %u",
|
2000-05-24 05:10:00 +00:00
|
|
|
dev->pktinfo.ipi6_ifindex);
|
2000-05-23 02:20:23 +00:00
|
|
|
|
2018-06-26 11:18:43 +02:00
|
|
|
msg->msg_control = (void *)cmsgbuf;
|
2004-01-29 04:39:19 +00:00
|
|
|
msg->msg_controllen = cmsg_space(sizeof(struct in6_pktinfo));
|
2018-06-26 15:11:28 +02:00
|
|
|
INSIST(msg->msg_controllen <= SENDCMSGBUFLEN);
|
1999-12-15 20:47:38 +00:00
|
|
|
|
2018-06-26 11:18:43 +02:00
|
|
|
cmsgp = (struct cmsghdr *)cmsgbuf;
|
1999-12-15 20:47:38 +00:00
|
|
|
cmsgp->cmsg_level = IPPROTO_IPV6;
|
|
|
|
cmsgp->cmsg_type = IPV6_PKTINFO;
|
2004-01-29 04:39:19 +00:00
|
|
|
cmsgp->cmsg_len = cmsg_len(sizeof(struct in6_pktinfo));
|
1999-12-15 20:47:38 +00:00
|
|
|
pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(pktinfop, &dev->pktinfo, sizeof(struct in6_pktinfo));
|
2016-08-11 23:45:29 +00:00
|
|
|
}
|
2016-08-12 09:41:59 +10:00
|
|
|
|
2011-07-21 01:40:50 +00:00
|
|
|
#if defined(IPV6_USE_MIN_MTU)
|
2018-10-29 16:49:33 +11:00
|
|
|
if ((sock->type == isc_sockettype_udp) && (sock->pf == AF_INET6) &&
|
2020-02-13 14:44:37 -08:00
|
|
|
((dev->attributes & ISC_SOCKEVENTATTR_USEMINMTU) != 0))
|
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
int use_min_mtu = 1; /* -1, 0, 1 */
|
2016-08-12 09:41:59 +10:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cmsgp = (struct cmsghdr *)(cmsgbuf + msg->msg_controllen);
|
2018-06-26 15:11:28 +02:00
|
|
|
msg->msg_control = (void *)cmsgbuf;
|
2011-07-21 01:40:50 +00:00
|
|
|
msg->msg_controllen += cmsg_space(sizeof(use_min_mtu));
|
2018-06-26 15:11:28 +02:00
|
|
|
INSIST(msg->msg_controllen <= SENDCMSGBUFLEN);
|
2011-07-21 01:40:50 +00:00
|
|
|
|
|
|
|
cmsgp->cmsg_level = IPPROTO_IPV6;
|
|
|
|
cmsgp->cmsg_type = IPV6_USE_MIN_MTU;
|
|
|
|
cmsgp->cmsg_len = cmsg_len(sizeof(use_min_mtu));
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(CMSG_DATA(cmsgp), &use_min_mtu, sizeof(use_min_mtu));
|
1999-12-15 20:47:38 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(IPV6_USE_MIN_MTU) */
|
2014-07-18 11:40:44 +10:00
|
|
|
|
|
|
|
if (isc_dscp_check_value > -1) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
2013-03-22 12:27:54 -07:00
|
|
|
INSIST((int)dev->dscp == isc_dscp_check_value);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (sock->type == isc_sockettype_tcp) {
|
2014-07-18 11:40:44 +10:00
|
|
|
INSIST((int)sock->dscp == isc_dscp_check_value);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2013-03-22 12:27:54 -07:00
|
|
|
}
|
|
|
|
|
2018-02-20 13:08:08 +11:00
|
|
|
#if defined(IP_TOS) || (defined(IPPROTO_IPV6) && defined(IPV6_TCLASS))
|
2013-04-10 21:59:14 -07:00
|
|
|
if ((sock->type == isc_sockettype_udp) &&
|
2020-02-13 14:44:37 -08:00
|
|
|
((dev->attributes & ISC_SOCKEVENTATTR_DSCP) != 0))
|
|
|
|
{
|
2013-03-22 12:27:54 -07:00
|
|
|
int dscp = (dev->dscp << 2) & 0xff;
|
|
|
|
|
2014-07-19 11:56:23 +10:00
|
|
|
INSIST(dev->dscp < 0x40);
|
2014-07-18 11:40:44 +10:00
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
#ifdef IP_TOS
|
2014-07-18 11:40:44 +10:00
|
|
|
if (sock->pf == AF_INET && sock->pktdscp) {
|
2018-06-26 11:18:43 +02:00
|
|
|
cmsgp = (struct cmsghdr *)(cmsgbuf +
|
2013-03-22 12:27:54 -07:00
|
|
|
msg->msg_controllen);
|
2018-06-26 11:18:43 +02:00
|
|
|
msg->msg_control = (void *)cmsgbuf;
|
2013-03-22 12:27:54 -07:00
|
|
|
msg->msg_controllen += cmsg_space(sizeof(dscp));
|
2018-06-26 15:11:28 +02:00
|
|
|
INSIST(msg->msg_controllen <= SENDCMSGBUFLEN);
|
2013-03-22 12:27:54 -07:00
|
|
|
|
|
|
|
cmsgp->cmsg_level = IPPROTO_IP;
|
|
|
|
cmsgp->cmsg_type = IP_TOS;
|
|
|
|
cmsgp->cmsg_len = cmsg_len(sizeof(char));
|
2020-02-12 13:59:18 +01:00
|
|
|
*(unsigned char *)CMSG_DATA(cmsgp) = dscp;
|
2014-07-18 11:40:44 +10:00
|
|
|
} else if (sock->pf == AF_INET && sock->dscp != dev->dscp) {
|
2013-03-22 12:27:54 -07:00
|
|
|
if (setsockopt(sock->fd, IPPROTO_IP, IP_TOS,
|
2020-02-12 13:59:18 +01:00
|
|
|
(void *)&dscp, sizeof(int)) < 0) {
|
2013-03-22 12:27:54 -07:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2013-03-22 12:27:54 -07:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d, IP_TOS, %.02x)"
|
2018-11-23 21:35:01 +01:00
|
|
|
" failed: %s",
|
2020-02-12 13:59:18 +01:00
|
|
|
sock->fd, dscp >> 2, strbuf);
|
2019-07-01 15:19:53 +02:00
|
|
|
} else {
|
2014-07-18 11:40:44 +10:00
|
|
|
sock->dscp = dscp;
|
2019-07-01 15:19:53 +02:00
|
|
|
}
|
2013-03-22 12:27:54 -07:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IP_TOS */
|
2013-03-23 19:04:41 +11:00
|
|
|
#if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
|
2014-07-18 11:40:44 +10:00
|
|
|
if (sock->pf == AF_INET6 && sock->pktdscp) {
|
2018-06-26 11:18:43 +02:00
|
|
|
cmsgp = (struct cmsghdr *)(cmsgbuf +
|
2013-03-22 12:27:54 -07:00
|
|
|
msg->msg_controllen);
|
2018-06-26 11:18:43 +02:00
|
|
|
msg->msg_control = (void *)cmsgbuf;
|
2013-03-22 12:27:54 -07:00
|
|
|
msg->msg_controllen += cmsg_space(sizeof(dscp));
|
2018-06-26 15:11:28 +02:00
|
|
|
INSIST(msg->msg_controllen <= SENDCMSGBUFLEN);
|
2013-03-22 12:27:54 -07:00
|
|
|
|
|
|
|
cmsgp->cmsg_level = IPPROTO_IPV6;
|
|
|
|
cmsgp->cmsg_type = IPV6_TCLASS;
|
|
|
|
cmsgp->cmsg_len = cmsg_len(sizeof(dscp));
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(CMSG_DATA(cmsgp), &dscp, sizeof(dscp));
|
2014-07-18 11:40:44 +10:00
|
|
|
} else if (sock->pf == AF_INET6 && sock->dscp != dev->dscp) {
|
2013-03-22 12:27:54 -07:00
|
|
|
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_TCLASS,
|
2020-02-13 14:44:37 -08:00
|
|
|
(void *)&dscp, sizeof(int)) < 0)
|
|
|
|
{
|
2013-03-22 12:27:54 -07:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2013-03-22 12:27:54 -07:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d, IPV6_TCLASS, "
|
2018-11-23 21:35:01 +01:00
|
|
|
"%.02x) failed: %s",
|
2020-02-12 13:59:18 +01:00
|
|
|
sock->fd, dscp >> 2, strbuf);
|
2019-07-01 15:19:53 +02:00
|
|
|
} else {
|
2014-07-18 11:40:44 +10:00
|
|
|
sock->dscp = dscp;
|
2019-07-01 15:19:53 +02:00
|
|
|
}
|
2013-03-22 12:27:54 -07:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS) */
|
2018-01-11 16:19:00 +11:00
|
|
|
if (msg->msg_controllen != 0 &&
|
2020-02-12 13:59:18 +01:00
|
|
|
msg->msg_controllen < SENDCMSGBUFLEN) {
|
2018-06-26 11:18:43 +02:00
|
|
|
memset(cmsgbuf + msg->msg_controllen, 0,
|
2018-06-26 15:11:28 +02:00
|
|
|
SENDCMSGBUFLEN - msg->msg_controllen);
|
2018-01-11 16:19:00 +11:00
|
|
|
}
|
2013-04-10 21:59:14 -07:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(IP_TOS) || (defined(IPPROTO_IPV6) && \
|
|
|
|
* defined(IPV6_TCLASS)) \
|
|
|
|
* */
|
2016-08-12 09:41:59 +10:00
|
|
|
#endif /* USE_CMSG */
|
1999-09-02 20:06:55 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (write_countp != NULL) {
|
1999-09-02 20:06:55 +00:00
|
|
|
*write_countp = write_count;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-02 02:10:44 +00:00
|
|
|
}
|
|
|
|
|
1999-09-02 23:34:09 +00:00
|
|
|
/*
|
2001-01-25 22:25:10 +00:00
|
|
|
* Construct an iov array and attach it to the msghdr passed in. This is
|
2009-01-18 00:06:06 +00:00
|
|
|
* the RECV constructor, which will use the available region of the buffer
|
2001-01-25 22:25:10 +00:00
|
|
|
* (if using a buffer list) or will use the internal region (if a single
|
|
|
|
* buffer I/O is requested).
|
1999-09-02 23:34:09 +00:00
|
|
|
*
|
|
|
|
* Nothing can be NULL, and the done event must list at least one buffer
|
|
|
|
* on the buffer linked list for this function to be meaningful.
|
|
|
|
*
|
|
|
|
* If read_countp != NULL, *read_countp will hold the number of bytes
|
|
|
|
* this transaction can receive.
|
|
|
|
*/
|
1999-09-03 01:18:45 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
build_msghdr_recv(isc_socket_t *sock, char *cmsgbuf, isc_socketevent_t *dev,
|
2020-02-13 14:44:37 -08:00
|
|
|
struct msghdr *msg, struct iovec *iov, size_t *read_countp) {
|
1999-09-02 23:34:09 +00:00
|
|
|
unsigned int iovcount;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t read_count;
|
1999-09-02 23:34:09 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
memset(msg, 0, sizeof(struct msghdr));
|
1999-09-02 23:34:09 +00:00
|
|
|
|
|
|
|
if (sock->type == isc_sockettype_udp) {
|
|
|
|
memset(&dev->address, 0, sizeof(dev->address));
|
|
|
|
msg->msg_name = (void *)&dev->address.type.sa;
|
1999-11-01 22:51:19 +00:00
|
|
|
msg->msg_namelen = sizeof(dev->address.type);
|
1999-09-13 22:00:28 +00:00
|
|
|
} else { /* TCP */
|
1999-09-02 23:34:09 +00:00
|
|
|
msg->msg_name = NULL;
|
|
|
|
msg->msg_namelen = 0;
|
2007-01-03 20:57:07 +00:00
|
|
|
dev->address = sock->peer_address;
|
1999-09-02 23:34:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 19:07:24 +00:00
|
|
|
read_count = dev->region.length - dev->n;
|
|
|
|
iov[0].iov_base = (void *)(dev->region.base + dev->n);
|
|
|
|
iov[0].iov_len = read_count;
|
|
|
|
iovcount = 1;
|
1999-09-13 22:00:28 +00:00
|
|
|
|
|
|
|
/*
|
2018-05-01 16:52:01 +10:00
|
|
|
* If needed, set up to receive that one extra byte.
|
1999-09-13 22:00:28 +00:00
|
|
|
*/
|
2018-05-01 16:52:01 +10:00
|
|
|
#ifdef ISC_PLATFORM_RECVOVERFLOW
|
1999-09-13 22:00:28 +00:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
2018-05-01 16:52:01 +10:00
|
|
|
INSIST(iovcount < MAXSCATTERGATHER_RECV);
|
1999-09-13 22:00:28 +00:00
|
|
|
iov[iovcount].iov_base = (void *)(&sock->overflow);
|
|
|
|
iov[iovcount].iov_len = 1;
|
|
|
|
iovcount++;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef ISC_PLATFORM_RECVOVERFLOW */
|
1999-09-13 22:00:28 +00:00
|
|
|
|
1999-09-02 23:34:09 +00:00
|
|
|
msg->msg_iov = iov;
|
|
|
|
msg->msg_iovlen = iovcount;
|
|
|
|
|
1999-12-15 20:47:38 +00:00
|
|
|
#if defined(USE_CMSG)
|
2018-06-26 11:18:43 +02:00
|
|
|
msg->msg_control = cmsgbuf;
|
2018-06-26 15:11:28 +02:00
|
|
|
msg->msg_controllen = RECVCMSGBUFLEN;
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(USE_CMSG) */
|
2015-09-18 23:30:01 +10:00
|
|
|
msg->msg_control = NULL;
|
|
|
|
msg->msg_controllen = 0;
|
1999-12-15 20:47:38 +00:00
|
|
|
#endif /* USE_CMSG */
|
2015-09-18 23:30:01 +10:00
|
|
|
msg->msg_flags = 0;
|
1999-09-02 23:34:09 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (read_countp != NULL) {
|
1999-09-02 23:34:09 +00:00
|
|
|
*read_countp = read_count;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-02 23:34:09 +00:00
|
|
|
}
|
|
|
|
|
1999-09-02 20:06:55 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
set_dev_address(const isc_sockaddr_t *address, isc_socket_t *sock,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_socketevent_t *dev) {
|
1999-09-02 20:06:55 +00:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (address != NULL) {
|
1999-09-02 20:06:55 +00:00
|
|
|
dev->address = *address;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2007-01-03 20:57:07 +00:00
|
|
|
dev->address = sock->peer_address;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-02 20:06:55 +00:00
|
|
|
} else if (sock->type == isc_sockettype_tcp) {
|
|
|
|
INSIST(address == NULL);
|
2007-01-03 20:57:07 +00:00
|
|
|
dev->address = sock->peer_address;
|
1999-09-02 20:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-06 00:53:36 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
destroy_socketevent(isc_event_t *event) {
|
2006-06-06 00:53:36 +00:00
|
|
|
isc_socketevent_t *ev = (isc_socketevent_t *)event;
|
|
|
|
|
|
|
|
(ev->destroy)(event);
|
|
|
|
}
|
|
|
|
|
1999-09-02 18:56:11 +00:00
|
|
|
static isc_socketevent_t *
|
2020-02-12 13:59:18 +01:00
|
|
|
allocate_socketevent(isc_mem_t *mctx, void *sender, isc_eventtype_t eventtype,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_taskaction_t action, void *arg) {
|
1999-09-02 18:56:11 +00:00
|
|
|
isc_socketevent_t *ev;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
ev = (isc_socketevent_t *)isc_event_allocate(mctx, sender, eventtype,
|
|
|
|
action, arg, sizeof(*ev));
|
1999-09-02 18:56:11 +00:00
|
|
|
|
2012-01-27 01:21:41 +00:00
|
|
|
ev->result = ISC_R_UNSET;
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LINK_INIT(ev, ev_link);
|
1999-09-02 18:56:11 +00:00
|
|
|
ev->region.base = NULL;
|
1999-09-02 23:34:09 +00:00
|
|
|
ev->n = 0;
|
|
|
|
ev->offset = 0;
|
2000-04-17 19:22:44 +00:00
|
|
|
ev->attributes = 0;
|
2006-06-06 00:53:36 +00:00
|
|
|
ev->destroy = ev->ev_destroy;
|
|
|
|
ev->ev_destroy = destroy_socketevent;
|
2013-03-22 12:27:54 -07:00
|
|
|
ev->dscp = 0;
|
1999-09-02 18:56:11 +00:00
|
|
|
|
|
|
|
return (ev);
|
|
|
|
}
|
|
|
|
|
1999-09-13 22:00:28 +00:00
|
|
|
#if defined(ISC_SOCKET_DEBUG)
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
dump_msg(struct msghdr *msg) {
|
1999-09-13 22:00:28 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
printf("MSGHDR %p\n", msg);
|
2008-11-12 03:52:32 +00:00
|
|
|
printf("\tname %p, namelen %ld\n", msg->msg_name,
|
2020-02-12 13:59:18 +01:00
|
|
|
(long)msg->msg_namelen);
|
|
|
|
printf("\tiov %p, iovlen %ld\n", msg->msg_iov, (long)msg->msg_iovlen);
|
2001-11-27 00:56:32 +00:00
|
|
|
for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
|
2018-02-20 13:09:58 +11:00
|
|
|
printf("\t\t%u\tbase %p, len %ld\n", i,
|
2020-02-12 13:59:18 +01:00
|
|
|
msg->msg_iov[i].iov_base, (long)msg->msg_iov[i].iov_len);
|
2008-11-12 03:52:32 +00:00
|
|
|
printf("\tcontrol %p, controllen %ld\n", msg->msg_control,
|
2020-02-12 13:59:18 +01:00
|
|
|
(long)msg->msg_controllen);
|
1999-09-13 22:00:28 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(ISC_SOCKET_DEBUG) */
|
1999-09-13 22:00:28 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define DOIO_SUCCESS 0 /* i/o ok, event sent */
|
2020-02-13 14:44:37 -08:00
|
|
|
#define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */
|
|
|
|
#define DOIO_HARD 2 /* i/o error, event sent */
|
|
|
|
#define DOIO_EOF 3 /* EOF, no event sent */
|
1999-09-03 01:18:45 +00:00
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
static int
|
2021-04-19 11:12:58 +02:00
|
|
|
doio_recv(isc_socket_t *sock, isc_socketevent_t *dev) {
|
2020-02-13 14:44:37 -08:00
|
|
|
int cc;
|
|
|
|
struct iovec iov[MAXSCATTERGATHER_RECV];
|
|
|
|
size_t read_count;
|
1999-09-03 00:22:28 +00:00
|
|
|
struct msghdr msghdr;
|
2020-02-13 14:44:37 -08:00
|
|
|
int recv_errno;
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
char cmsgbuf[RECVCMSGBUFLEN] = { 0 };
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2018-06-26 11:18:43 +02:00
|
|
|
build_msghdr_recv(sock, cmsgbuf, dev, &msghdr, iov, &read_count);
|
1999-09-13 22:00:28 +00:00
|
|
|
|
|
|
|
#if defined(ISC_SOCKET_DEBUG)
|
|
|
|
dump_msg(&msghdr);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(ISC_SOCKET_DEBUG) */
|
1999-09-03 00:22:28 +00:00
|
|
|
|
|
|
|
cc = recvmsg(sock->fd, &msghdr, 0);
|
2001-05-14 18:20:03 +00:00
|
|
|
recv_errno = errno;
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2006-02-02 23:07:53 +00:00
|
|
|
#if defined(ISC_SOCKET_DEBUG)
|
|
|
|
dump_msg(&msghdr);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(ISC_SOCKET_DEBUG) */
|
2006-02-02 23:07:53 +00:00
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
if (cc < 0) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (SOFT_ERROR(recv_errno)) {
|
1999-09-03 00:22:28 +00:00
|
|
|
return (DOIO_SOFT);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2001-10-22 20:57:41 +00:00
|
|
|
if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(recv_errno, strbuf, sizeof(strbuf));
|
2000-07-13 01:13:53 +00:00
|
|
|
socket_log(sock, NULL, IOEVENT,
|
2019-07-01 15:19:53 +02:00
|
|
|
"doio_recv: recvmsg(%d) %d bytes, err %d/%s",
|
2001-08-31 05:57:58 +00:00
|
|
|
sock->fd, cc, recv_errno, strbuf);
|
2001-10-22 20:57:41 +00:00
|
|
|
}
|
1999-12-06 23:07:05 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define SOFT_OR_HARD(_system, _isc) \
|
|
|
|
if (recv_errno == _system) { \
|
|
|
|
if (sock->connected) { \
|
|
|
|
dev->result = _isc; \
|
|
|
|
inc_stats(sock->manager->stats, \
|
2009-01-27 22:30:00 +00:00
|
|
|
sock->statsindex[STATID_RECVFAIL]); \
|
2020-02-12 13:59:18 +01:00
|
|
|
return (DOIO_HARD); \
|
|
|
|
} \
|
|
|
|
return (DOIO_SOFT); \
|
|
|
|
}
|
|
|
|
#define ALWAYS_HARD(_system, _isc) \
|
|
|
|
if (recv_errno == _system) { \
|
|
|
|
dev->result = _isc; \
|
|
|
|
inc_stats(sock->manager->stats, \
|
2009-01-27 22:30:00 +00:00
|
|
|
sock->statsindex[STATID_RECVFAIL]); \
|
2020-02-12 13:59:18 +01:00
|
|
|
return (DOIO_HARD); \
|
2000-04-14 15:38:28 +00:00
|
|
|
}
|
1999-09-03 00:22:28 +00:00
|
|
|
|
|
|
|
SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
|
2000-09-06 22:55:33 +00:00
|
|
|
SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
|
|
|
|
SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
2001-09-18 22:54:02 +00:00
|
|
|
SOFT_OR_HARD(EHOSTDOWN, ISC_R_HOSTDOWN);
|
2018-08-13 09:35:42 +02:00
|
|
|
SOFT_OR_HARD(ENOBUFS, ISC_R_NORESOURCES);
|
2020-08-26 16:31:13 +02:00
|
|
|
/*
|
|
|
|
* Older operating systems may still return EPROTO in some
|
|
|
|
* situations, for example when receiving ICMP/ICMPv6 errors.
|
|
|
|
* A real life scenario is when ICMPv6 returns code 5 or 6.
|
|
|
|
* These codes are introduced in RFC 4443 from March 2006,
|
|
|
|
* and the document obsoletes RFC 1885. But unfortunately not
|
|
|
|
* all operating systems have caught up with the new standard
|
|
|
|
* (in 2020) and thus a generic protocol error is returned.
|
|
|
|
*/
|
|
|
|
SOFT_OR_HARD(EPROTO, ISC_R_HOSTUNREACH);
|
2013-08-28 15:56:49 -05:00
|
|
|
/* Should never get this one but it was seen. */
|
|
|
|
#ifdef ENOPROTOOPT
|
|
|
|
SOFT_OR_HARD(ENOPROTOOPT, ISC_R_HOSTUNREACH);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef ENOPROTOOPT */
|
2008-07-18 02:43:31 +00:00
|
|
|
SOFT_OR_HARD(EINVAL, ISC_R_HOSTUNREACH);
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2000-04-14 15:38:28 +00:00
|
|
|
#undef SOFT_OR_HARD
|
|
|
|
#undef ALWAYS_HARD
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2001-05-14 23:58:33 +00:00
|
|
|
dev->result = isc__errno2result(recv_errno);
|
2009-01-27 22:30:00 +00:00
|
|
|
inc_stats(sock->manager->stats,
|
|
|
|
sock->statsindex[STATID_RECVFAIL]);
|
2001-02-12 21:43:17 +00:00
|
|
|
return (DOIO_HARD);
|
1999-09-03 00:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-01-31 23:18:03 +00:00
|
|
|
* On TCP and UNIX sockets, zero length reads indicate EOF,
|
|
|
|
* while on UDP sockets, zero length reads are perfectly valid,
|
|
|
|
* although strange.
|
1999-09-03 00:22:28 +00:00
|
|
|
*/
|
2010-01-31 23:18:03 +00:00
|
|
|
switch (sock->type) {
|
|
|
|
case isc_sockettype_tcp:
|
|
|
|
case isc_sockettype_unix:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (cc == 0) {
|
2010-01-31 23:18:03 +00:00
|
|
|
return (DOIO_EOF);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2010-01-31 23:18:03 +00:00
|
|
|
break;
|
|
|
|
case isc_sockettype_udp:
|
2014-02-07 17:16:37 +11:00
|
|
|
case isc_sockettype_raw:
|
2010-01-31 23:18:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
2018-11-07 15:00:07 +07:00
|
|
|
ISC_UNREACHABLE();
|
2010-01-31 23:18:03 +00:00
|
|
|
}
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2002-04-02 04:36:52 +00:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
1999-12-04 01:27:44 +00:00
|
|
|
dev->address.length = msghdr.msg_namelen;
|
2002-04-02 04:36:52 +00:00
|
|
|
if (isc_sockaddr_getport(&dev->address) == 0) {
|
|
|
|
if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
|
|
|
|
socket_log(sock, &dev->address, IOEVENT,
|
|
|
|
"dropping source port zero packet");
|
|
|
|
}
|
|
|
|
return (DOIO_SOFT);
|
|
|
|
}
|
2009-03-05 03:13:55 +00:00
|
|
|
/*
|
|
|
|
* Simulate a firewall blocking UDP responses bigger than
|
2012-10-16 10:23:08 +11:00
|
|
|
* 'maxudp' bytes.
|
2009-03-05 03:13:55 +00:00
|
|
|
*/
|
2019-08-08 18:31:20 +10:00
|
|
|
if (sock->manager->maxudp != 0 &&
|
2020-02-12 13:59:18 +01:00
|
|
|
cc > (int)sock->manager->maxudp) {
|
2009-03-05 03:13:55 +00:00
|
|
|
return (DOIO_SOFT);
|
2019-08-08 18:31:20 +10:00
|
|
|
}
|
2002-04-02 04:36:52 +00:00
|
|
|
}
|
1999-12-04 01:27:44 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
socket_log(sock, &dev->address, IOEVENT, "packet received correctly");
|
2000-05-03 21:09:49 +00:00
|
|
|
|
1999-09-13 22:00:28 +00:00
|
|
|
/*
|
|
|
|
* Overflow bit detection. If we received MORE bytes than we should,
|
|
|
|
* this indicates an overflow situation. Set the flag in the
|
|
|
|
* dev entry and adjust how much we read by one.
|
|
|
|
*/
|
2018-05-01 16:52:01 +10:00
|
|
|
#ifdef ISC_PLATFORM_RECVOVERFLOW
|
1999-09-13 22:00:28 +00:00
|
|
|
if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) {
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
|
|
|
|
cc--;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef ISC_PLATFORM_RECVOVERFLOW */
|
1999-09-13 22:00:28 +00:00
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
/*
|
|
|
|
* If there are control messages attached, run through them and pull
|
|
|
|
* out the interesting bits.
|
|
|
|
*/
|
2013-03-22 12:27:54 -07:00
|
|
|
process_cmsg(sock, &msghdr, dev);
|
1999-09-03 00:22:28 +00:00
|
|
|
|
1999-09-03 16:38:08 +00:00
|
|
|
/*
|
|
|
|
* update the buffers (if any) and the i/o count
|
|
|
|
*/
|
|
|
|
dev->n += cc;
|
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
/*
|
|
|
|
* If we read less than we expected, update counters,
|
|
|
|
* and let the upper layer poke the descriptor.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (((size_t)cc != read_count) && (dev->n < dev->minimum)) {
|
1999-09-03 00:22:28 +00:00
|
|
|
return (DOIO_SOFT);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-03 00:22:28 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Full reads are posted, or partials if partials are ok.
|
1999-09-03 00:22:28 +00:00
|
|
|
*/
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = ISC_R_SUCCESS;
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2000-06-26 22:59:22 +00:00
|
|
|
/*
|
|
|
|
* Returns:
|
2001-02-12 21:43:17 +00:00
|
|
|
* DOIO_SUCCESS The operation succeeded. dev->result contains
|
|
|
|
* ISC_R_SUCCESS.
|
2000-06-26 22:59:22 +00:00
|
|
|
*
|
|
|
|
* DOIO_HARD A hard or unexpected I/O error was encountered.
|
2001-02-12 21:43:17 +00:00
|
|
|
* dev->result contains the appropriate error.
|
2000-06-26 22:59:22 +00:00
|
|
|
*
|
|
|
|
* DOIO_SOFT A soft I/O error was encountered. No senddone
|
|
|
|
* event was sent. The operation should be retried.
|
|
|
|
*
|
|
|
|
* No other return values are possible.
|
|
|
|
*/
|
1999-09-03 01:18:45 +00:00
|
|
|
static int
|
2021-04-19 11:12:58 +02:00
|
|
|
doio_send(isc_socket_t *sock, isc_socketevent_t *dev) {
|
2020-02-13 14:44:37 -08:00
|
|
|
int cc;
|
|
|
|
struct iovec iov[MAXSCATTERGATHER_SEND];
|
|
|
|
size_t write_count;
|
1999-09-03 01:18:45 +00:00
|
|
|
struct msghdr msghdr;
|
2020-02-13 14:44:37 -08:00
|
|
|
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
|
|
|
|
int attempts = 0;
|
|
|
|
int send_errno;
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
char cmsgbuf[SENDCMSGBUFLEN] = { 0 };
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2018-06-26 11:18:43 +02:00
|
|
|
build_msghdr_send(sock, cmsgbuf, dev, &msghdr, iov, &write_count);
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
resend:
|
|
|
|
if (sock->type == isc_sockettype_udp && sock->manager->maxudp != 0 &&
|
2020-02-13 14:44:37 -08:00
|
|
|
write_count > sock->manager->maxudp)
|
|
|
|
{
|
2012-10-16 10:23:08 +11:00
|
|
|
cc = write_count;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2012-10-16 10:23:08 +11:00
|
|
|
cc = sendmsg(sock->fd, &msghdr, 0);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-06-07 00:21:53 +00:00
|
|
|
send_errno = errno;
|
1999-09-03 01:18:45 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Check for error or block condition.
|
1999-09-03 01:18:45 +00:00
|
|
|
*/
|
|
|
|
if (cc < 0) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (send_errno == EINTR && ++attempts < NRETRIES) {
|
2001-03-06 01:23:03 +00:00
|
|
|
goto resend;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-03-06 01:23:03 +00:00
|
|
|
|
2015-09-21 17:22:53 +10:00
|
|
|
if (SOFT_ERROR(send_errno)) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
2015-09-21 17:22:53 +10:00
|
|
|
dev->result = ISC_R_WOULDBLOCK;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_SOFT);
|
2015-09-21 17:22:53 +10:00
|
|
|
}
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define SOFT_OR_HARD(_system, _isc) \
|
|
|
|
if (send_errno == _system) { \
|
|
|
|
if (sock->connected) { \
|
|
|
|
dev->result = _isc; \
|
|
|
|
inc_stats(sock->manager->stats, \
|
2009-01-27 22:30:00 +00:00
|
|
|
sock->statsindex[STATID_SENDFAIL]); \
|
2020-02-12 13:59:18 +01:00
|
|
|
return (DOIO_HARD); \
|
|
|
|
} \
|
|
|
|
return (DOIO_SOFT); \
|
|
|
|
}
|
|
|
|
#define ALWAYS_HARD(_system, _isc) \
|
|
|
|
if (send_errno == _system) { \
|
|
|
|
dev->result = _isc; \
|
|
|
|
inc_stats(sock->manager->stats, \
|
2009-01-27 22:30:00 +00:00
|
|
|
sock->statsindex[STATID_SENDFAIL]); \
|
2020-02-12 13:59:18 +01:00
|
|
|
return (DOIO_HARD); \
|
2000-04-14 15:38:28 +00:00
|
|
|
}
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2000-09-21 21:30:27 +00:00
|
|
|
SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
|
2001-02-24 23:51:09 +00:00
|
|
|
ALWAYS_HARD(EACCES, ISC_R_NOPERM);
|
|
|
|
ALWAYS_HARD(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
|
2000-09-21 21:30:27 +00:00
|
|
|
ALWAYS_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
|
2000-06-26 20:16:01 +00:00
|
|
|
ALWAYS_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
2000-11-17 21:04:12 +00:00
|
|
|
#ifdef EHOSTDOWN
|
|
|
|
ALWAYS_HARD(EHOSTDOWN, ISC_R_HOSTUNREACH);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef EHOSTDOWN */
|
2000-09-21 21:30:27 +00:00
|
|
|
ALWAYS_HARD(ENETUNREACH, ISC_R_NETUNREACH);
|
2018-08-13 09:35:42 +02:00
|
|
|
SOFT_OR_HARD(ENOBUFS, ISC_R_NORESOURCES);
|
2000-08-10 21:47:46 +00:00
|
|
|
ALWAYS_HARD(EPERM, ISC_R_HOSTUNREACH);
|
2000-09-07 01:59:57 +00:00
|
|
|
ALWAYS_HARD(EPIPE, ISC_R_NOTCONNECTED);
|
2002-06-26 00:12:54 +00:00
|
|
|
ALWAYS_HARD(ECONNRESET, ISC_R_CONNECTIONRESET);
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2000-04-14 15:38:28 +00:00
|
|
|
#undef SOFT_OR_HARD
|
|
|
|
#undef ALWAYS_HARD
|
1999-09-03 01:18:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The other error types depend on whether or not the
|
|
|
|
* socket is UDP or TCP. If it is UDP, some errors
|
|
|
|
* that we expect to be fatal under TCP are merely
|
|
|
|
* annoying, and are really soft errors.
|
|
|
|
*
|
|
|
|
* However, these soft errors are still returned as
|
|
|
|
* a status.
|
|
|
|
*/
|
2000-08-31 13:38:55 +00:00
|
|
|
isc_sockaddr_format(&dev->address, addrbuf, sizeof(addrbuf));
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(send_errno, strbuf, sizeof(strbuf));
|
2001-08-31 05:57:58 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "internal_send: %s: %s",
|
|
|
|
addrbuf, strbuf);
|
2001-06-07 00:21:53 +00:00
|
|
|
dev->result = isc__errno2result(send_errno);
|
2009-01-27 22:30:00 +00:00
|
|
|
inc_stats(sock->manager->stats,
|
|
|
|
sock->statsindex[STATID_SENDFAIL]);
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_HARD);
|
|
|
|
}
|
|
|
|
|
2009-01-27 22:30:00 +00:00
|
|
|
if (cc == 0) {
|
|
|
|
inc_stats(sock->manager->stats,
|
|
|
|
sock->statsindex[STATID_SENDFAIL]);
|
1999-09-03 01:18:45 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2018-11-23 21:35:01 +01:00
|
|
|
"doio_send: send() returned 0");
|
2009-01-27 22:30:00 +00:00
|
|
|
}
|
1999-09-03 01:18:45 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* If we write less than we expected, update counters, poke.
|
1999-09-03 01:18:45 +00:00
|
|
|
*/
|
|
|
|
dev->n += cc;
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((size_t)cc != write_count) {
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_SOFT);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-03 00:22:28 +00:00
|
|
|
|
1999-09-03 01:18:45 +00:00
|
|
|
/*
|
|
|
|
* Exactly what we wanted to write. We're done with this
|
|
|
|
* entry. Post its completion event.
|
|
|
|
*/
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = ISC_R_SUCCESS;
|
1999-09-03 00:22:28 +00:00
|
|
|
return (DOIO_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Kill.
|
|
|
|
*
|
1999-07-28 01:07:21 +00:00
|
|
|
* Caller must ensure that the socket is not locked and no external
|
|
|
|
* references exist.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
socketclose(isc__socketthread_t *thread, isc_socket_t *sock, int fd) {
|
2008-06-23 19:41:20 +00:00
|
|
|
int lockid = FDLOCK_ID(fd);
|
|
|
|
/*
|
|
|
|
* No one has this socket open, so the watcher doesn't have to be
|
|
|
|
* poked, and the socket doesn't have to be locked.
|
|
|
|
*/
|
2018-10-03 22:46:23 +02:00
|
|
|
LOCK(&thread->fdlock[lockid]);
|
|
|
|
thread->fds[fd] = NULL;
|
|
|
|
thread->fdstate[fd] = CLOSE_PENDING;
|
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2018-10-04 16:11:24 +02:00
|
|
|
select_poke(thread->manager, thread->threadid, fd, SELECT_POKE_CLOSE);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2018-10-04 16:11:24 +02:00
|
|
|
inc_stats(thread->manager->stats, sock->statsindex[STATID_CLOSE]);
|
2019-07-01 15:19:53 +02:00
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
2013-01-23 14:28:23 -08:00
|
|
|
if (sock->active == 1) {
|
2018-11-08 19:34:51 -08:00
|
|
|
dec_stats(thread->manager->stats,
|
|
|
|
sock->statsindex[STATID_ACTIVE]);
|
2013-01-23 14:28:23 -08:00
|
|
|
sock->active = 0;
|
|
|
|
}
|
2019-07-01 15:19:53 +02:00
|
|
|
UNLOCK(&sock->lock);
|
2009-01-27 22:30:00 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/*
|
|
|
|
* update manager->maxfd here (XXX: this should be implemented more
|
|
|
|
* efficiently)
|
|
|
|
*/
|
|
|
|
#ifdef USE_SELECT
|
2018-10-04 16:11:24 +02:00
|
|
|
LOCK(&thread->manager->lock);
|
2018-10-03 22:46:23 +02:00
|
|
|
if (thread->maxfd == fd) {
|
2008-06-23 19:41:20 +00:00
|
|
|
int i;
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->maxfd = 0;
|
2008-06-23 19:41:20 +00:00
|
|
|
for (i = fd - 1; i >= 0; i--) {
|
|
|
|
lockid = FDLOCK_ID(i);
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
LOCK(&thread->fdlock[lockid]);
|
|
|
|
if (thread->fdstate[i] == MANAGED) {
|
|
|
|
thread->maxfd = i;
|
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2008-06-23 19:41:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
2018-11-08 19:34:51 -08:00
|
|
|
if (thread->maxfd < thread->pipe_fds[0]) {
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->maxfd = thread->pipe_fds[0];
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
2012-05-14 10:06:05 -07:00
|
|
|
|
2018-10-04 16:11:24 +02:00
|
|
|
UNLOCK(&thread->manager->lock);
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* USE_SELECT */
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
destroy(isc_socket_t **sockp) {
|
2020-02-13 14:44:37 -08:00
|
|
|
int fd = 0;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_t *sock = *sockp;
|
|
|
|
isc_socketmgr_t *manager = sock->manager;
|
2019-07-01 15:19:53 +02:00
|
|
|
isc__socketthread_t *thread = NULL;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
socket_log(sock, NULL, CREATION, "destroying");
|
1998-11-11 00:43:14 +00:00
|
|
|
|
2019-12-12 13:08:34 +01:00
|
|
|
isc_refcount_destroy(&sock->references);
|
|
|
|
|
2019-12-12 12:59:39 +01:00
|
|
|
LOCK(&sock->lock);
|
2015-01-20 17:22:31 -08:00
|
|
|
INSIST(ISC_LIST_EMPTY(sock->connect_list));
|
1999-07-28 01:07:21 +00:00
|
|
|
INSIST(ISC_LIST_EMPTY(sock->accept_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->recv_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->send_list));
|
2016-03-22 18:05:32 -07:00
|
|
|
INSIST(sock->fd >= -1 && sock->fd < (int)manager->maxsocks);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
if (sock->fd >= 0) {
|
|
|
|
fd = sock->fd;
|
2018-10-04 16:11:24 +02:00
|
|
|
thread = &manager->threads[sock->threadid];
|
2008-06-23 19:41:20 +00:00
|
|
|
sock->fd = -1;
|
2018-10-04 16:11:24 +02:00
|
|
|
sock->threadid = -1;
|
2019-07-01 15:19:53 +02:00
|
|
|
}
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
if (fd > 0) {
|
2018-10-04 16:11:24 +02:00
|
|
|
socketclose(thread, sock, fd);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
1999-07-28 01:07:21 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LIST_UNLINK(manager->socklist, sock, link);
|
2000-05-03 21:09:49 +00:00
|
|
|
|
2019-07-01 15:19:53 +02:00
|
|
|
if (ISC_LIST_EMPTY(manager->socklist)) {
|
1999-05-17 22:31:26 +00:00
|
|
|
SIGNAL(&manager->shutdown_ok);
|
2019-07-01 15:19:53 +02:00
|
|
|
}
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2011-02-28 12:48:30 +00:00
|
|
|
/* can't unlock manager as its memory context is still used */
|
1998-11-11 00:43:14 +00:00
|
|
|
free_socket(sockp);
|
2011-02-28 12:48:30 +00:00
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
allocate_socket(isc_socketmgr_t *manager, isc_sockettype_t type,
|
|
|
|
isc_socket_t **socketp) {
|
|
|
|
isc_socket_t *sock;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
sock = isc_mem_get(manager->mctx, sizeof(*sock));
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
sock->magic = 0;
|
2018-10-04 12:30:04 +00:00
|
|
|
isc_refcount_init(&sock->references, 0);
|
1998-11-10 01:56:44 +00:00
|
|
|
|
|
|
|
sock->manager = manager;
|
|
|
|
sock->type = type;
|
1998-12-10 16:14:05 +00:00
|
|
|
sock->fd = -1;
|
2018-10-04 16:11:24 +02:00
|
|
|
sock->threadid = -1;
|
2020-02-12 13:59:18 +01:00
|
|
|
sock->dscp = 0; /* TOS/TCLASS is zero until set. */
|
2011-07-28 04:04:37 +00:00
|
|
|
sock->dupped = 0;
|
2009-01-27 22:30:00 +00:00
|
|
|
sock->statsindex = NULL;
|
2015-04-17 14:04:47 +10:00
|
|
|
sock->active = 0;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LINK_INIT(sock, link);
|
|
|
|
|
2009-04-18 01:28:17 +00:00
|
|
|
memset(sock->name, 0, sizeof(sock->name));
|
|
|
|
sock->tag = NULL;
|
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
2013-03-22 09:21:30 +11:00
|
|
|
* Set up list of readers and writers to be initially empty.
|
1998-11-10 01:56:44 +00:00
|
|
|
*/
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_INIT(sock->recv_list);
|
|
|
|
ISC_LIST_INIT(sock->send_list);
|
1999-07-28 06:50:10 +00:00
|
|
|
ISC_LIST_INIT(sock->accept_list);
|
2015-01-20 17:22:31 -08:00
|
|
|
ISC_LIST_INIT(sock->connect_list);
|
2019-07-01 15:19:53 +02:00
|
|
|
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->listener = 0;
|
|
|
|
sock->connected = 0;
|
|
|
|
sock->connecting = 0;
|
2000-05-10 21:17:49 +00:00
|
|
|
sock->bound = 0;
|
2014-07-18 11:40:44 +10:00
|
|
|
sock->pktdscp = 0;
|
1999-06-10 00:45:30 +00:00
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
2013-03-22 09:21:30 +11:00
|
|
|
* Initialize the lock.
|
1998-11-10 01:56:44 +00:00
|
|
|
*/
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&sock->lock);
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
sock->magic = SOCKET_MAGIC;
|
1998-11-10 01:56:44 +00:00
|
|
|
*socketp = sock;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This event requires that the various lists be empty, that the reference
|
|
|
|
* count be 1, and that the magic number is valid. The other socket bits,
|
|
|
|
* like the lock, must be initialized as well. The fd associated must be
|
|
|
|
* marked as closed, by setting it to -1 on close, or this routine will
|
|
|
|
* also close the socket.
|
|
|
|
*/
|
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
free_socket(isc_socket_t **socketp) {
|
|
|
|
isc_socket_t *sock = *socketp;
|
2020-02-08 04:37:54 -08:00
|
|
|
*socketp = NULL;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_SOCKET(sock));
|
2019-07-15 11:19:03 +02:00
|
|
|
isc_refcount_destroy(&sock->references);
|
2019-12-12 13:08:34 +01:00
|
|
|
LOCK(&sock->lock);
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(!sock->connecting);
|
2000-06-02 01:27:27 +00:00
|
|
|
INSIST(ISC_LIST_EMPTY(sock->recv_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->send_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->accept_list));
|
2015-01-20 17:22:31 -08:00
|
|
|
INSIST(ISC_LIST_EMPTY(sock->connect_list));
|
2000-06-02 20:09:01 +00:00
|
|
|
INSIST(!ISC_LINK_LINKED(sock, link));
|
2019-12-12 13:08:34 +01:00
|
|
|
UNLOCK(&sock->lock);
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
sock->magic = 0;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&sock->lock);
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_mem_put(sock->manager->mctx, sock, sizeof(*sock));
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 00:25:09 +02:00
|
|
|
#if defined(SET_RCVBUF)
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_once_t rcvbuf_once = ISC_ONCE_INIT;
|
2020-04-30 00:25:09 +02:00
|
|
|
static int rcvbuf = ISC_RECV_BUFFER_SIZE;
|
2014-10-18 12:40:13 +11:00
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
set_rcvbuf(void) {
|
|
|
|
int fd;
|
|
|
|
int max = rcvbuf, min;
|
2018-09-05 14:03:10 +02:00
|
|
|
socklen_t len;
|
2014-10-18 12:40:13 +11:00
|
|
|
|
|
|
|
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (fd == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case EPROTONOSUPPORT:
|
|
|
|
case EPFNOSUPPORT:
|
|
|
|
case EAFNOSUPPORT:
|
|
|
|
/*
|
|
|
|
* Linux 2.2 (and maybe others) return EINVAL instead of
|
|
|
|
* EAFNOSUPPORT.
|
|
|
|
*/
|
|
|
|
case EINVAL:
|
|
|
|
fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
if (fd == -1) {
|
2014-10-18 12:40:13 +11:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-10-18 12:40:13 +11:00
|
|
|
|
|
|
|
len = sizeof(min);
|
2015-02-03 11:43:34 +05:30
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&min, &len) == 0 &&
|
2020-02-13 14:44:37 -08:00
|
|
|
min < rcvbuf)
|
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
again:
|
2014-10-18 12:40:13 +11:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf,
|
2020-02-13 14:44:37 -08:00
|
|
|
sizeof(rcvbuf)) == -1)
|
|
|
|
{
|
2014-10-18 12:40:13 +11:00
|
|
|
if (errno == ENOBUFS && rcvbuf > min) {
|
|
|
|
max = rcvbuf - 1;
|
|
|
|
rcvbuf = (rcvbuf + min) / 2;
|
|
|
|
goto again;
|
|
|
|
} else {
|
|
|
|
rcvbuf = min;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2014-10-18 12:40:13 +11:00
|
|
|
min = rcvbuf;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-10-18 12:40:13 +11:00
|
|
|
if (min != max) {
|
|
|
|
rcvbuf = max;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
|
|
|
close(fd);
|
2014-10-18 12:40:13 +11:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef SO_RCVBUF */
|
2014-10-18 12:40:13 +11:00
|
|
|
|
2020-04-30 00:25:09 +02:00
|
|
|
#if defined(SET_SNDBUF)
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_once_t sndbuf_once = ISC_ONCE_INIT;
|
2020-04-30 00:25:09 +02:00
|
|
|
static int sndbuf = ISC_SEND_BUFFER_SIZE;
|
2018-01-26 15:45:59 +05:30
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
set_sndbuf(void) {
|
|
|
|
int fd;
|
|
|
|
int max = sndbuf, min;
|
2018-09-05 14:03:10 +02:00
|
|
|
socklen_t len;
|
2018-01-26 15:45:59 +05:30
|
|
|
|
|
|
|
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (fd == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case EPROTONOSUPPORT:
|
|
|
|
case EPFNOSUPPORT:
|
|
|
|
case EAFNOSUPPORT:
|
|
|
|
/*
|
|
|
|
* Linux 2.2 (and maybe others) return EINVAL instead of
|
|
|
|
* EAFNOSUPPORT.
|
|
|
|
*/
|
|
|
|
case EINVAL:
|
|
|
|
fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 15:21:49 -08:00
|
|
|
if (fd == -1) {
|
2018-01-26 15:45:59 +05:30
|
|
|
return;
|
2018-02-25 15:21:49 -08:00
|
|
|
}
|
2018-01-26 15:45:59 +05:30
|
|
|
|
|
|
|
len = sizeof(min);
|
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *)&min, &len) == 0 &&
|
2020-02-13 14:44:37 -08:00
|
|
|
min < sndbuf)
|
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
again:
|
2018-01-26 15:45:59 +05:30
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *)&sndbuf,
|
2020-02-13 14:44:37 -08:00
|
|
|
sizeof(sndbuf)) == -1)
|
|
|
|
{
|
2018-01-26 15:45:59 +05:30
|
|
|
if (errno == ENOBUFS && sndbuf > min) {
|
|
|
|
max = sndbuf - 1;
|
|
|
|
sndbuf = (sndbuf + min) / 2;
|
|
|
|
goto again;
|
|
|
|
} else {
|
|
|
|
sndbuf = min;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-02-25 15:21:49 -08:00
|
|
|
} else {
|
2018-01-26 15:45:59 +05:30
|
|
|
min = sndbuf;
|
2018-02-25 15:21:49 -08:00
|
|
|
}
|
2018-01-26 15:45:59 +05:30
|
|
|
if (min != max) {
|
|
|
|
sndbuf = max;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
|
|
|
close(fd);
|
2018-01-26 15:45:59 +05:30
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef SO_SNDBUF */
|
2018-01-26 15:45:59 +05:30
|
|
|
|
2012-11-01 08:42:56 +11:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
use_min_mtu(isc_socket_t *sock) {
|
2012-11-01 15:27:56 +11:00
|
|
|
#if !defined(IPV6_USE_MIN_MTU) && !defined(IPV6_MTU)
|
|
|
|
UNUSED(sock);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if !defined(IPV6_USE_MIN_MTU) && !defined(IPV6_MTU) */
|
2012-11-01 08:42:56 +11:00
|
|
|
#ifdef IPV6_USE_MIN_MTU
|
|
|
|
/* use minimum MTU */
|
|
|
|
if (sock->pf == AF_INET6) {
|
|
|
|
int on = 1;
|
|
|
|
(void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
|
2020-02-12 13:59:18 +01:00
|
|
|
(void *)&on, sizeof(on));
|
2012-11-01 08:42:56 +11:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IPV6_USE_MIN_MTU */
|
2012-11-01 08:42:56 +11:00
|
|
|
#if defined(IPV6_MTU)
|
|
|
|
/*
|
|
|
|
* Use minimum MTU on IPv6 sockets.
|
|
|
|
*/
|
|
|
|
if (sock->pf == AF_INET6) {
|
|
|
|
int mtu = 1280;
|
2020-02-12 13:59:18 +01:00
|
|
|
(void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MTU, &mtu,
|
|
|
|
sizeof(mtu));
|
2012-11-01 08:42:56 +11:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(IPV6_MTU) */
|
2012-11-01 08:42:56 +11:00
|
|
|
}
|
|
|
|
|
2016-08-16 07:42:25 +10:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
set_tcp_maxseg(isc_socket_t *sock, int size) {
|
2016-08-16 07:42:25 +10:00
|
|
|
#ifdef TCP_MAXSEG
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sock->type == isc_sockettype_tcp) {
|
2016-08-16 07:42:25 +10:00
|
|
|
(void)setsockopt(sock->fd, IPPROTO_TCP, TCP_MAXSEG,
|
2020-02-12 13:59:18 +01:00
|
|
|
(void *)&size, sizeof(size));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
#endif /* ifdef TCP_MAXSEG */
|
2016-08-16 07:42:25 +10:00
|
|
|
}
|
|
|
|
|
2020-09-23 14:47:26 +02:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
set_ip_dontfrag(isc_socket_t *sock) {
|
2020-09-23 14:47:26 +02:00
|
|
|
/*
|
|
|
|
* Set the Don't Fragment flag on IP packets
|
|
|
|
*/
|
|
|
|
if (sock->pf == AF_INET6) {
|
|
|
|
#if defined(IPV6_DONTFRAG)
|
|
|
|
(void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_DONTFRAG,
|
|
|
|
&(int){ 1 }, sizeof(int));
|
|
|
|
#endif
|
|
|
|
#if defined(IPV6_MTU_DISCOVER)
|
|
|
|
(void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
|
|
|
|
&(int){ IP_PMTUDISC_DO }, sizeof(int));
|
|
|
|
#endif
|
|
|
|
} else if (sock->pf == AF_INET) {
|
|
|
|
#if defined(IP_DONTFRAG)
|
|
|
|
(void)setsockopt(sock->fd, IPPROTO_IP, IP_DONTFRAG, &(int){ 1 },
|
|
|
|
sizeof(int));
|
|
|
|
#endif
|
|
|
|
#if defined(IP_MTU_DISCOVER)
|
|
|
|
(void)setsockopt(sock->fd, IPPROTO_IP, IP_MTU_DISCOVER,
|
|
|
|
&(int){ IP_PMTUDISC_DO }, sizeof(int));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
static isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
opensocket(isc_socketmgr_t *manager, isc_socket_t *sock,
|
|
|
|
isc_socket_t *dup_socket) {
|
2012-01-27 01:21:41 +00:00
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
const char *err = "socket";
|
|
|
|
int tries = 0;
|
2019-07-01 16:16:40 +02:00
|
|
|
#if defined(USE_CMSG) || defined(SO_NOSIGPIPE)
|
1999-12-09 00:19:31 +00:00
|
|
|
int on = 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_CMSG) || defined(SO_NOSIGPIPE) */
|
2020-04-30 00:25:09 +02:00
|
|
|
#if defined(SET_RCVBUF) || defined(SET_SNDBUF)
|
2018-09-05 14:03:10 +02:00
|
|
|
socklen_t optlen;
|
2020-02-13 14:44:37 -08:00
|
|
|
int size = 0;
|
2020-04-30 00:25:09 +02:00
|
|
|
#endif
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
again:
|
2011-07-28 04:04:37 +00:00
|
|
|
if (dup_socket == NULL) {
|
|
|
|
switch (sock->type) {
|
|
|
|
case isc_sockettype_udp:
|
|
|
|
sock->fd = socket(sock->pf, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
break;
|
|
|
|
case isc_sockettype_tcp:
|
|
|
|
sock->fd = socket(sock->pf, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
break;
|
|
|
|
case isc_sockettype_unix:
|
|
|
|
sock->fd = socket(sock->pf, SOCK_STREAM, 0);
|
|
|
|
break;
|
2014-02-07 17:16:37 +11:00
|
|
|
case isc_sockettype_raw:
|
2014-02-10 09:46:54 +11:00
|
|
|
errno = EPFNOSUPPORT;
|
|
|
|
/*
|
|
|
|
* PF_ROUTE is a alias for PF_NETLINK on linux.
|
|
|
|
*/
|
|
|
|
#if defined(PF_ROUTE)
|
|
|
|
if (sock->fd == -1 && sock->pf == PF_ROUTE) {
|
|
|
|
#ifdef NETLINK_ROUTE
|
|
|
|
sock->fd = socket(sock->pf, SOCK_RAW,
|
|
|
|
NETLINK_ROUTE);
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef NETLINK_ROUTE */
|
2014-02-10 09:46:54 +11:00
|
|
|
sock->fd = socket(sock->pf, SOCK_RAW, 0);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef NETLINK_ROUTE */
|
2014-02-10 09:46:54 +11:00
|
|
|
if (sock->fd != -1) {
|
|
|
|
#ifdef NETLINK_ROUTE
|
|
|
|
struct sockaddr_nl sa;
|
2020-02-13 14:44:37 -08:00
|
|
|
int n;
|
2014-02-10 09:46:54 +11:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do an implicit bind.
|
|
|
|
*/
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.nl_family = AF_NETLINK;
|
|
|
|
sa.nl_groups = RTMGRP_IPV4_IFADDR |
|
|
|
|
RTMGRP_IPV6_IFADDR;
|
|
|
|
n = bind(sock->fd,
|
2020-02-12 13:59:18 +01:00
|
|
|
(struct sockaddr *)&sa,
|
2014-02-10 09:46:54 +11:00
|
|
|
sizeof(sa));
|
|
|
|
if (n < 0) {
|
|
|
|
close(sock->fd);
|
|
|
|
sock->fd = -1;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef NETLINK_ROUTE */
|
2014-02-10 09:46:54 +11:00
|
|
|
sock->bound = 1;
|
|
|
|
}
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(PF_ROUTE) */
|
2014-02-07 17:16:37 +11:00
|
|
|
break;
|
2011-07-28 04:04:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sock->fd = dup(dup_socket->fd);
|
|
|
|
sock->dupped = 1;
|
2011-11-29 01:03:47 +00:00
|
|
|
sock->bound = dup_socket->bound;
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
2019-07-01 15:19:53 +02:00
|
|
|
if (sock->fd == -1 && errno == EINTR && tries++ < 42) {
|
2007-05-21 01:55:10 +00:00
|
|
|
goto again;
|
2019-07-01 15:19:53 +02:00
|
|
|
}
|
2001-04-26 23:45:55 +00:00
|
|
|
|
2003-02-26 04:00:20 +00:00
|
|
|
#ifdef F_DUPFD
|
2005-05-19 02:42:42 +00:00
|
|
|
/*
|
2008-09-04 05:56:43 +00:00
|
|
|
* Leave a space for stdio and TCP to work in.
|
2005-05-19 02:42:42 +00:00
|
|
|
*/
|
2008-09-04 05:56:43 +00:00
|
|
|
if (manager->reserved != 0 && sock->type == isc_sockettype_udp &&
|
2020-02-13 14:44:37 -08:00
|
|
|
sock->fd >= 0 && sock->fd < manager->reserved)
|
|
|
|
{
|
2017-07-21 11:52:24 +10:00
|
|
|
int newfd, tmp;
|
|
|
|
newfd = fcntl(sock->fd, F_DUPFD, manager->reserved);
|
2008-09-04 05:56:43 +00:00
|
|
|
tmp = errno;
|
|
|
|
(void)close(sock->fd);
|
|
|
|
errno = tmp;
|
2017-07-21 11:52:24 +10:00
|
|
|
sock->fd = newfd;
|
2008-09-04 05:56:43 +00:00
|
|
|
err = "isc_socket_create: fcntl/reserved";
|
|
|
|
} else if (sock->fd >= 0 && sock->fd < 20) {
|
2017-07-21 11:52:24 +10:00
|
|
|
int newfd, tmp;
|
|
|
|
newfd = fcntl(sock->fd, F_DUPFD, 20);
|
2005-05-19 02:42:42 +00:00
|
|
|
tmp = errno;
|
|
|
|
(void)close(sock->fd);
|
|
|
|
errno = tmp;
|
2017-07-21 11:52:24 +10:00
|
|
|
sock->fd = newfd;
|
2005-05-19 02:42:42 +00:00
|
|
|
err = "isc_socket_create: fcntl";
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef F_DUPFD */
|
2003-02-26 04:00:20 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
if (sock->fd >= (int)manager->maxsocks) {
|
2001-04-26 23:45:55 +00:00
|
|
|
(void)close(sock->fd);
|
2018-11-23 21:35:01 +01:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"socket: file descriptor exceeds limit (%d/%u)",
|
|
|
|
sock->fd, manager->maxsocks);
|
2012-05-14 10:06:05 -07:00
|
|
|
inc_stats(manager->stats, sock->statsindex[STATID_OPENFAIL]);
|
2001-04-26 23:45:55 +00:00
|
|
|
return (ISC_R_NORESOURCES);
|
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
if (sock->fd < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case EMFILE:
|
|
|
|
case ENFILE:
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2018-11-23 21:35:01 +01:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"%s: %s", err, strbuf);
|
2020-02-13 21:48:23 +01:00
|
|
|
/* fallthrough */
|
1998-11-03 00:54:47 +00:00
|
|
|
case ENOBUFS:
|
2012-05-14 10:06:05 -07:00
|
|
|
inc_stats(manager->stats,
|
|
|
|
sock->statsindex[STATID_OPENFAIL]);
|
1998-11-03 00:54:47 +00:00
|
|
|
return (ISC_R_NORESOURCES);
|
2000-08-15 01:43:38 +00:00
|
|
|
|
|
|
|
case EPROTONOSUPPORT:
|
|
|
|
case EPFNOSUPPORT:
|
|
|
|
case EAFNOSUPPORT:
|
2000-12-06 01:53:38 +00:00
|
|
|
/*
|
|
|
|
* Linux 2.2 (and maybe others) return EINVAL instead of
|
|
|
|
* EAFNOSUPPORT.
|
|
|
|
*/
|
|
|
|
case EINVAL:
|
2012-05-14 10:06:05 -07:00
|
|
|
inc_stats(manager->stats,
|
|
|
|
sock->statsindex[STATID_OPENFAIL]);
|
2000-08-15 01:43:38 +00:00
|
|
|
return (ISC_R_FAMILYNOSUPPORT);
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
default:
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2020-02-12 13:59:18 +01:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s() failed: %s",
|
|
|
|
err, strbuf);
|
2012-05-14 10:06:05 -07:00
|
|
|
inc_stats(manager->stats,
|
|
|
|
sock->statsindex[STATID_OPENFAIL]);
|
1998-11-03 00:54:47 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dup_socket != NULL) {
|
2011-07-28 04:04:37 +00:00
|
|
|
goto setup_done;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-07-28 04:04:37 +00:00
|
|
|
|
2012-01-27 01:21:41 +00:00
|
|
|
result = make_nonblock(sock->fd);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2001-11-09 00:37:10 +00:00
|
|
|
(void)close(sock->fd);
|
2012-05-14 10:06:05 -07:00
|
|
|
inc_stats(manager->stats, sock->statsindex[STATID_OPENFAIL]);
|
2012-01-27 01:21:41 +00:00
|
|
|
return (result);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
2007-12-14 03:52:40 +00:00
|
|
|
#ifdef SO_NOSIGPIPE
|
2020-02-12 13:59:18 +01:00
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on,
|
|
|
|
sizeof(on)) < 0) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2007-12-14 03:52:40 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2018-11-23 21:35:01 +01:00
|
|
|
"setsockopt(%d, SO_NOSIGPIPE) failed: %s",
|
2020-02-12 13:59:18 +01:00
|
|
|
sock->fd, strbuf);
|
2007-12-14 03:52:40 +00:00
|
|
|
/* Press on... */
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef SO_NOSIGPIPE */
|
2007-12-14 03:52:40 +00:00
|
|
|
|
2012-11-01 08:42:56 +11:00
|
|
|
/*
|
|
|
|
* Use minimum mtu if possible.
|
|
|
|
*/
|
2016-08-16 07:42:25 +10:00
|
|
|
if (sock->type == isc_sockettype_tcp && sock->pf == AF_INET6) {
|
2016-08-12 09:41:59 +10:00
|
|
|
use_min_mtu(sock);
|
2016-08-16 07:42:25 +10:00
|
|
|
set_tcp_maxseg(sock, 1280 - 20 - 40); /* 1280 - TCP - IPV6 */
|
|
|
|
}
|
2012-11-01 08:42:56 +11:00
|
|
|
|
2020-04-30 00:25:09 +02:00
|
|
|
#if defined(USE_CMSG) || defined(SET_RCVBUF) || defined(SET_SNDBUF)
|
2008-06-23 19:41:20 +00:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
2005-07-12 00:41:55 +00:00
|
|
|
#if defined(USE_CMSG)
|
1999-12-09 00:19:31 +00:00
|
|
|
#if defined(SO_TIMESTAMP)
|
2020-02-12 13:59:18 +01:00
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_TIMESTAMP, (void *)&on,
|
|
|
|
sizeof(on)) < 0 &&
|
2020-02-13 14:44:37 -08:00
|
|
|
errno != ENOPROTOOPT)
|
|
|
|
{
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
1999-12-09 00:19:31 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"setsockopt(%d, SO_TIMESTAMP) failed: "
|
|
|
|
"%s",
|
|
|
|
sock->fd, strbuf);
|
1999-12-09 00:19:31 +00:00
|
|
|
/* Press on... */
|
|
|
|
}
|
|
|
|
#endif /* SO_TIMESTAMP */
|
|
|
|
|
2000-08-10 00:05:45 +00:00
|
|
|
#ifdef IPV6_RECVPKTINFO
|
2007-04-02 02:03:55 +00:00
|
|
|
/* RFC 3542 */
|
2020-02-12 13:59:18 +01:00
|
|
|
if ((sock->pf == AF_INET6) &&
|
|
|
|
(setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
|
2020-02-13 14:44:37 -08:00
|
|
|
(void *)&on, sizeof(on)) < 0))
|
|
|
|
{
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2000-08-10 00:05:45 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d, IPV6_RECVPKTINFO) "
|
2020-02-12 13:59:18 +01:00
|
|
|
"failed: %s",
|
|
|
|
sock->fd, strbuf);
|
2000-08-10 00:05:45 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef IPV6_RECVPKTINFO */
|
2007-04-02 02:03:55 +00:00
|
|
|
/* RFC 2292 */
|
2020-02-12 13:59:18 +01:00
|
|
|
if ((sock->pf == AF_INET6) &&
|
|
|
|
(setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO,
|
2020-02-13 14:44:37 -08:00
|
|
|
(void *)&on, sizeof(on)) < 0))
|
|
|
|
{
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
1999-12-09 00:19:31 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"setsockopt(%d, IPV6_PKTINFO) failed: "
|
|
|
|
"%s",
|
|
|
|
sock->fd, strbuf);
|
1999-12-09 00:19:31 +00:00
|
|
|
}
|
2000-08-10 00:05:45 +00:00
|
|
|
#endif /* IPV6_RECVPKTINFO */
|
2005-07-12 00:41:55 +00:00
|
|
|
#endif /* defined(USE_CMSG) */
|
|
|
|
|
2020-04-30 00:25:09 +02:00
|
|
|
#if defined(SET_RCVBUF)
|
2005-07-12 00:41:55 +00:00
|
|
|
optlen = sizeof(size);
|
2020-02-12 13:59:18 +01:00
|
|
|
if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, (void *)&size,
|
|
|
|
&optlen) == 0 &&
|
2020-02-13 14:44:37 -08:00
|
|
|
size < rcvbuf)
|
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
RUNTIME_CHECK(isc_once_do(&rcvbuf_once, set_rcvbuf) ==
|
|
|
|
ISC_R_SUCCESS);
|
2005-07-12 00:41:55 +00:00
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
|
2020-02-13 14:44:37 -08:00
|
|
|
(void *)&rcvbuf, sizeof(rcvbuf)) == -1)
|
|
|
|
{
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2005-07-12 00:41:55 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"setsockopt(%d, SO_RCVBUF, "
|
|
|
|
"%d) failed: %s",
|
|
|
|
sock->fd, rcvbuf, strbuf);
|
2005-07-12 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-30 00:25:09 +02:00
|
|
|
#endif /* if defined(SET_RCVBUF) */
|
2018-01-26 15:45:59 +05:30
|
|
|
|
2020-04-30 00:25:09 +02:00
|
|
|
#if defined(SET_SNDBUF)
|
2018-01-26 15:45:59 +05:30
|
|
|
optlen = sizeof(size);
|
2020-02-12 13:59:18 +01:00
|
|
|
if (getsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, (void *)&size,
|
|
|
|
&optlen) == 0 &&
|
2020-02-13 14:44:37 -08:00
|
|
|
size < sndbuf)
|
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
RUNTIME_CHECK(isc_once_do(&sndbuf_once, set_sndbuf) ==
|
|
|
|
ISC_R_SUCCESS);
|
2018-01-26 15:45:59 +05:30
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF,
|
2020-02-13 14:44:37 -08:00
|
|
|
(void *)&sndbuf, sizeof(sndbuf)) == -1)
|
|
|
|
{
|
2018-08-28 20:25:36 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2018-01-26 15:45:59 +05:30
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"setsockopt(%d, SO_SNDBUF, "
|
|
|
|
"%d) failed: %s",
|
|
|
|
sock->fd, sndbuf, strbuf);
|
2018-01-26 15:45:59 +05:30
|
|
|
}
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(SO_SNDBUF) */
|
1999-08-28 04:26:49 +00:00
|
|
|
}
|
2013-03-22 12:27:54 -07:00
|
|
|
#ifdef IPV6_RECVTCLASS
|
2020-02-12 13:59:18 +01:00
|
|
|
if ((sock->pf == AF_INET6) &&
|
|
|
|
(setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVTCLASS, (void *)&on,
|
2020-02-13 14:44:37 -08:00
|
|
|
sizeof(on)) < 0))
|
|
|
|
{
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2013-03-22 12:27:54 -07:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d, IPV6_RECVTCLASS) "
|
2020-02-12 13:59:18 +01:00
|
|
|
"failed: %s",
|
|
|
|
sock->fd, strbuf);
|
2013-03-22 12:27:54 -07:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IPV6_RECVTCLASS */
|
2013-03-22 12:27:54 -07:00
|
|
|
#ifdef IP_RECVTOS
|
2020-02-12 13:59:18 +01:00
|
|
|
if ((sock->pf == AF_INET) &&
|
|
|
|
(setsockopt(sock->fd, IPPROTO_IP, IP_RECVTOS, (void *)&on,
|
2020-02-13 14:44:37 -08:00
|
|
|
sizeof(on)) < 0))
|
|
|
|
{
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2013-03-22 12:27:54 -07:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d, IP_RECVTOS) "
|
2020-02-12 13:59:18 +01:00
|
|
|
"failed: %s",
|
|
|
|
sock->fd, strbuf);
|
2013-03-22 12:27:54 -07:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IP_RECVTOS */
|
2020-04-30 00:25:09 +02:00
|
|
|
#endif /* defined(USE_CMSG) || defined(SET_RCVBUF) || defined(SET_SNDBUF) */
|
1999-08-28 04:26:49 +00:00
|
|
|
|
2020-10-08 08:26:28 +02:00
|
|
|
set_ip_dontfrag(sock);
|
|
|
|
|
2011-07-28 04:04:37 +00:00
|
|
|
setup_done:
|
2009-01-27 22:30:00 +00:00
|
|
|
inc_stats(manager->stats, sock->statsindex[STATID_OPEN]);
|
2013-01-23 14:28:23 -08:00
|
|
|
if (sock->active == 0) {
|
|
|
|
inc_stats(manager->stats, sock->statsindex[STATID_ACTIVE]);
|
|
|
|
sock->active = 1;
|
|
|
|
}
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2011-07-28 04:04:37 +00:00
|
|
|
/*
|
|
|
|
* Create a 'type' socket or duplicate an existing socket, managed
|
|
|
|
* by 'manager'. Events will be posted to 'task' and when dispatched
|
|
|
|
* 'action' will be called with 'arg' as the arg value. The new
|
|
|
|
* socket is returned in 'socketp'.
|
2008-06-23 19:41:20 +00:00
|
|
|
*/
|
2011-07-28 04:04:37 +00:00
|
|
|
static isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_socket_t **socketp, isc_socket_t *dup_socket) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_t *sock = NULL;
|
2018-10-03 22:46:23 +02:00
|
|
|
isc__socketthread_t *thread;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
|
|
|
int lockid;
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(socketp != NULL && *socketp == NULL);
|
|
|
|
|
|
|
|
result = allocate_socket(manager, type, &sock);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2008-06-23 19:41:20 +00:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2009-01-27 22:30:00 +00:00
|
|
|
switch (sock->type) {
|
|
|
|
case isc_sockettype_udp:
|
2020-02-12 13:59:18 +01:00
|
|
|
sock->statsindex = (pf == AF_INET) ? udp4statsindex
|
|
|
|
: udp6statsindex;
|
2014-07-18 11:40:44 +10:00
|
|
|
#define DCSPPKT(pf) ((pf == AF_INET) ? ISC_NET_DSCPPKTV4 : ISC_NET_DSCPPKTV6)
|
|
|
|
sock->pktdscp = (isc_net_probedscp() & DCSPPKT(pf)) != 0;
|
2009-01-27 22:30:00 +00:00
|
|
|
break;
|
|
|
|
case isc_sockettype_tcp:
|
2020-02-12 13:59:18 +01:00
|
|
|
sock->statsindex = (pf == AF_INET) ? tcp4statsindex
|
|
|
|
: tcp6statsindex;
|
2009-01-27 22:30:00 +00:00
|
|
|
break;
|
|
|
|
case isc_sockettype_unix:
|
|
|
|
sock->statsindex = unixstatsindex;
|
|
|
|
break;
|
2014-02-07 17:16:37 +11:00
|
|
|
case isc_sockettype_raw:
|
|
|
|
sock->statsindex = rawstatsindex;
|
|
|
|
break;
|
2009-01-27 22:30:00 +00:00
|
|
|
default:
|
|
|
|
INSIST(0);
|
2018-11-07 15:00:07 +07:00
|
|
|
ISC_UNREACHABLE();
|
2009-01-27 22:30:00 +00:00
|
|
|
}
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
sock->pf = pf;
|
2011-07-28 04:04:37 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
result = opensocket(manager, sock, dup_socket);
|
2008-06-23 19:41:20 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
free_socket(&sock);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2018-10-04 16:11:24 +02:00
|
|
|
if (sock->fd == -1) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
sock->threadid = gen_threadid(sock);
|
2019-07-23 10:24:50 -04:00
|
|
|
isc_refcount_increment0(&sock->references);
|
2018-10-03 22:46:23 +02:00
|
|
|
thread = &manager->threads[sock->threadid];
|
2021-04-19 11:12:58 +02:00
|
|
|
*socketp = sock;
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Note we don't have to lock the socket like we normally would because
|
|
|
|
* there are no external references to it yet.
|
|
|
|
*/
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
lockid = FDLOCK_ID(sock->fd);
|
2018-10-03 22:46:23 +02:00
|
|
|
LOCK(&thread->fdlock[lockid]);
|
|
|
|
thread->fds[sock->fd] = sock;
|
|
|
|
thread->fdstate[sock->fd] = MANAGED;
|
2015-11-19 11:01:45 +05:30
|
|
|
#if defined(USE_EPOLL)
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->epoll_events[sock->fd] = 0;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_EPOLL) */
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_DEVPOLL
|
2018-10-03 22:46:23 +02:00
|
|
|
INSIST(thread->fdpollinfo[sock->fd].want_read == 0 &&
|
|
|
|
thread->fdpollinfo[sock->fd].want_write == 0);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_DEVPOLL */
|
2018-10-03 22:46:23 +02:00
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LIST_APPEND(manager->socklist, sock, link);
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_SELECT
|
2018-10-03 22:46:23 +02:00
|
|
|
if (thread->maxfd < sock->fd) {
|
|
|
|
thread->maxfd = sock->fd;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_SELECT */
|
1998-11-03 00:54:47 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
socket_log(sock, NULL, CREATION,
|
|
|
|
dup_socket != NULL ? "dupped" : "created");
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
2011-07-28 04:04:37 +00:00
|
|
|
/*%
|
|
|
|
* Create a new 'type' socket managed by 'manager'. Events
|
|
|
|
* will be posted to 'task' and when dispatched 'action' will be
|
|
|
|
* called with 'arg' as the arg value. The new socket is returned
|
|
|
|
* in 'socketp'.
|
|
|
|
*/
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2018-08-06 13:00:55 +02:00
|
|
|
isc_socket_create(isc_socketmgr_t *manager0, int pf, isc_sockettype_t type,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_socket_t **socketp) {
|
2011-07-28 04:04:37 +00:00
|
|
|
return (socket_create(manager0, pf, type, socketp, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Duplicate an existing socket. The new socket is returned
|
|
|
|
* in 'socketp'.
|
|
|
|
*/
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_dup(isc_socket_t *sock, isc_socket_t **socketp) {
|
2011-07-28 04:04:37 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(socketp != NULL && *socketp == NULL);
|
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
return (socket_create(sock->manager, sock->pf, sock->type, socketp,
|
|
|
|
sock));
|
2011-07-28 04:04:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_open(isc_socket_t *sock) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2018-10-03 22:46:23 +02:00
|
|
|
isc__socketthread_t *thread;
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
2019-05-31 10:40:52 +02:00
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
REQUIRE(isc_refcount_current(&sock->references) >= 1);
|
2008-06-23 19:41:20 +00:00
|
|
|
REQUIRE(sock->fd == -1);
|
2018-10-03 22:46:23 +02:00
|
|
|
REQUIRE(sock->threadid == -1);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2011-07-28 04:04:37 +00:00
|
|
|
result = opensocket(sock->manager, sock, NULL);
|
2019-05-31 10:40:52 +02:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2008-06-23 19:41:20 +00:00
|
|
|
sock->fd = -1;
|
2018-10-03 22:46:23 +02:00
|
|
|
} else {
|
2018-10-04 16:11:24 +02:00
|
|
|
sock->threadid = gen_threadid(sock);
|
2018-10-03 22:46:23 +02:00
|
|
|
thread = &sock->manager->threads[sock->threadid];
|
2008-06-23 19:41:20 +00:00
|
|
|
int lockid = FDLOCK_ID(sock->fd);
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
LOCK(&thread->fdlock[lockid]);
|
|
|
|
thread->fds[sock->fd] = sock;
|
|
|
|
thread->fdstate[sock->fd] = MANAGED;
|
2015-11-19 11:01:45 +05:30
|
|
|
#if defined(USE_EPOLL)
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->epoll_events[sock->fd] = 0;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_EPOLL) */
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_DEVPOLL
|
2018-10-03 22:46:23 +02:00
|
|
|
INSIST(thread->fdpollinfo[sock->fd].want_read == 0 &&
|
|
|
|
thread->fdpollinfo[sock->fd].want_write == 0);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_DEVPOLL */
|
2018-10-03 22:46:23 +02:00
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
#ifdef USE_SELECT
|
|
|
|
LOCK(&sock->manager->lock);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (thread->maxfd < sock->fd) {
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->maxfd = sock->fd;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
UNLOCK(&sock->manager->lock);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_SELECT */
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Attach to a socket. Caller must explicitly detach when it is done.
|
|
|
|
*/
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp) {
|
1998-11-03 00:54:47 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(socketp != NULL && *socketp == NULL);
|
|
|
|
|
2018-10-04 12:30:04 +00:00
|
|
|
int old_refs = isc_refcount_increment(&sock->references);
|
|
|
|
REQUIRE(old_refs > 0);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
*socketp = sock;
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dereference a socket. If this is the last reference to it, clean things
|
|
|
|
* up by destroying the socket.
|
|
|
|
*/
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_socket_detach(isc_socket_t **socketp) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_t *sock;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
REQUIRE(socketp != NULL);
|
2021-04-19 11:12:58 +02:00
|
|
|
sock = *socketp;
|
1998-11-03 00:54:47 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
2018-10-04 12:30:04 +00:00
|
|
|
if (isc_refcount_decrement(&sock->references) == 1) {
|
1998-11-11 00:43:14 +00:00
|
|
|
destroy(&sock);
|
2018-10-04 12:30:04 +00:00
|
|
|
}
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
*socketp = NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_close(isc_socket_t *sock) {
|
2020-02-13 14:44:37 -08:00
|
|
|
int fd;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_t *manager;
|
2018-10-04 16:11:24 +02:00
|
|
|
isc__socketthread_t *thread;
|
2011-07-28 04:04:37 +00:00
|
|
|
fflush(stdout);
|
2008-06-23 19:41:20 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
2008-12-03 02:03:47 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
REQUIRE(sock->fd >= 0 && sock->fd < (int)sock->manager->maxsocks);
|
|
|
|
|
|
|
|
INSIST(!sock->connecting);
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->recv_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->send_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->accept_list));
|
2015-01-20 17:22:31 -08:00
|
|
|
INSIST(ISC_LIST_EMPTY(sock->connect_list));
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2008-12-03 02:03:47 +00:00
|
|
|
manager = sock->manager;
|
2018-10-04 16:11:24 +02:00
|
|
|
thread = &manager->threads[sock->threadid];
|
2008-06-23 19:41:20 +00:00
|
|
|
fd = sock->fd;
|
|
|
|
sock->fd = -1;
|
2018-10-04 16:11:24 +02:00
|
|
|
sock->threadid = -1;
|
|
|
|
|
2011-07-28 04:04:37 +00:00
|
|
|
sock->dupped = 0;
|
2009-04-18 01:28:17 +00:00
|
|
|
memset(sock->name, 0, sizeof(sock->name));
|
|
|
|
sock->tag = NULL;
|
2008-06-23 19:41:20 +00:00
|
|
|
sock->listener = 0;
|
|
|
|
sock->connected = 0;
|
|
|
|
sock->connecting = 0;
|
|
|
|
sock->bound = 0;
|
|
|
|
isc_sockaddr_any(&sock->peer_address);
|
2009-04-18 01:28:17 +00:00
|
|
|
|
2008-12-03 02:03:47 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2018-10-04 16:11:24 +02:00
|
|
|
socketclose(thread, sock, fd);
|
2008-07-03 00:13:25 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
1998-11-07 02:31:04 +00:00
|
|
|
/*
|
|
|
|
* Dequeue an item off the given socket's read queue, set the result code
|
|
|
|
* in the done event to the one provided, and send it to the task it was
|
|
|
|
* destined for.
|
|
|
|
*
|
1999-07-28 06:50:10 +00:00
|
|
|
* If the event to be sent is on a list, remove it before sending. If
|
|
|
|
* asked to, send and detach from the socket as well.
|
1999-03-29 23:56:06 +00:00
|
|
|
*
|
2001-01-25 22:25:10 +00:00
|
|
|
* Caller must have the socket locked if the event is attached to the socket.
|
1998-11-07 02:31:04 +00:00
|
|
|
*/
|
1998-11-06 01:45:35 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
send_recvdone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
|
1999-09-03 00:22:28 +00:00
|
|
|
isc_task_t *task;
|
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
task = (*dev)->ev_sender;
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
(*dev)->ev_sender = sock;
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2018-10-18 22:00:15 +00:00
|
|
|
if (ISC_LINK_LINKED(*dev, ev_link)) {
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->recv_list, *dev, ev_link);
|
2018-10-18 22:00:15 +00:00
|
|
|
}
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2018-10-11 11:57:57 +02:00
|
|
|
if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED) != 0) {
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendtoanddetach(&task, (isc_event_t **)dev,
|
|
|
|
sock->threadid);
|
2018-10-11 11:57:57 +02:00
|
|
|
} else {
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendto(task, (isc_event_t **)dev, sock->threadid);
|
2018-10-11 11:57:57 +02:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
1999-03-29 23:56:06 +00:00
|
|
|
|
|
|
|
/*
|
1999-03-30 06:22:28 +00:00
|
|
|
* See comments for send_recvdone_event() above.
|
1999-03-29 23:56:06 +00:00
|
|
|
*
|
2001-01-25 22:25:10 +00:00
|
|
|
* Caller must have the socket locked if the event is attached to the socket.
|
1999-03-29 23:56:06 +00:00
|
|
|
*/
|
1998-11-11 02:05:36 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
send_senddone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
|
1999-09-03 01:18:45 +00:00
|
|
|
isc_task_t *task;
|
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
INSIST(dev != NULL && *dev != NULL);
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
task = (*dev)->ev_sender;
|
|
|
|
(*dev)->ev_sender = sock;
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (ISC_LINK_LINKED(*dev, ev_link)) {
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->send_list, *dev, ev_link);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2018-10-11 11:57:57 +02:00
|
|
|
if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED) != 0) {
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendtoanddetach(&task, (isc_event_t **)dev,
|
|
|
|
sock->threadid);
|
2018-10-11 11:57:57 +02:00
|
|
|
} else {
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendto(task, (isc_event_t **)dev, sock->threadid);
|
2018-10-11 11:57:57 +02:00
|
|
|
}
|
1998-11-11 02:05:36 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2015-01-20 17:22:31 -08:00
|
|
|
/*
|
|
|
|
* See comments for send_recvdone_event() above.
|
|
|
|
*
|
|
|
|
* Caller must have the socket locked if the event is attached to the socket.
|
|
|
|
*/
|
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
send_connectdone_event(isc_socket_t *sock, isc_socket_connev_t **dev) {
|
2015-01-20 17:22:31 -08:00
|
|
|
isc_task_t *task;
|
|
|
|
|
|
|
|
INSIST(dev != NULL && *dev != NULL);
|
|
|
|
|
|
|
|
task = (*dev)->ev_sender;
|
|
|
|
(*dev)->ev_sender = sock;
|
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if (ISC_LINK_LINKED(*dev, ev_link)) {
|
2015-01-20 17:22:31 -08:00
|
|
|
ISC_LIST_DEQUEUE(sock->connect_list, *dev, ev_link);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2015-01-20 17:22:31 -08:00
|
|
|
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendtoanddetach(&task, (isc_event_t **)dev, sock->threadid);
|
2015-01-20 17:22:31 -08:00
|
|
|
}
|
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
|
|
|
* Call accept() on a socket, to get the new file descriptor. The listen
|
|
|
|
* socket is used as a prototype to create a new isc_socket_t. The new
|
1999-06-10 00:45:30 +00:00
|
|
|
* socket has one outstanding reference. The task receiving the event
|
|
|
|
* will be detached from just after the event is delivered.
|
|
|
|
*
|
|
|
|
* On entry to this function, the event delivered is the internal
|
|
|
|
* readable event, and the first item on the accept_list should be
|
|
|
|
* the done event we want to send. If the list is empty, this is a no-op,
|
|
|
|
* so just unlock and return.
|
1998-11-10 01:56:44 +00:00
|
|
|
*/
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
internal_accept(isc_socket_t *sock) {
|
|
|
|
isc_socketmgr_t *manager;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__socketthread_t *thread, *nthread;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_newconnev_t *dev;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_task_t *task;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int fd;
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
const char *err = "accept";
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_SOCKET(sock));
|
2020-02-05 12:35:54 +01:00
|
|
|
REQUIRE(sock->fd >= 0);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
socket_log(sock, NULL, TRACE, "internal_accept called, locked socket");
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
manager = sock->manager;
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_MANAGER(manager));
|
2018-10-03 22:46:23 +02:00
|
|
|
thread = &manager->threads[sock->threadid];
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
INSIST(sock->listener);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the first item off the accept list.
|
|
|
|
* If it is empty, unlock the socket and return.
|
|
|
|
*/
|
|
|
|
dev = ISC_LIST_HEAD(sock->accept_list);
|
|
|
|
if (dev == NULL) {
|
2018-10-04 14:52:54 +00:00
|
|
|
unwatch_fd(thread, sock->fd, SELECT_POKE_ACCEPT);
|
2020-05-19 10:08:25 +02:00
|
|
|
UNLOCK(&sock->lock);
|
1998-12-16 02:05:38 +00:00
|
|
|
return;
|
1998-11-10 11:37:54 +00:00
|
|
|
}
|
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
|
|
|
* Try to accept the new connection. If the accept fails with
|
1998-12-04 11:21:11 +00:00
|
|
|
* EAGAIN or EINTR, simply poke the watcher to watch this socket
|
2001-07-15 22:50:24 +00:00
|
|
|
* again. Also ignore ECONNRESET, which has been reported to
|
|
|
|
* be spuriously returned on Linux 2.2.19 although it is not
|
2001-11-08 20:24:25 +00:00
|
|
|
* a documented error for accept(). ECONNABORTED has been
|
|
|
|
* reported for Solaris 8. The rest are thrown in not because
|
|
|
|
* we have seen them but because they are ignored by other
|
2009-01-18 00:06:06 +00:00
|
|
|
* daemons such as BIND 8 and Apache.
|
1998-11-10 01:56:44 +00:00
|
|
|
*/
|
2001-11-08 20:24:25 +00:00
|
|
|
|
2009-09-01 00:22:28 +00:00
|
|
|
addrlen = sizeof(NEWCONNSOCK(dev)->peer_address.type);
|
|
|
|
memset(&NEWCONNSOCK(dev)->peer_address.type, 0, addrlen);
|
|
|
|
fd = accept(sock->fd, &NEWCONNSOCK(dev)->peer_address.type.sa,
|
2000-05-14 22:35:45 +00:00
|
|
|
(void *)&addrlen);
|
2003-02-26 04:00:20 +00:00
|
|
|
|
|
|
|
#ifdef F_DUPFD
|
2005-05-19 02:42:42 +00:00
|
|
|
/*
|
|
|
|
* Leave a space for stdio to work in.
|
|
|
|
*/
|
|
|
|
if (fd >= 0 && fd < 20) {
|
2017-07-21 11:52:24 +10:00
|
|
|
int newfd, tmp;
|
|
|
|
newfd = fcntl(fd, F_DUPFD, 20);
|
2005-05-19 02:42:42 +00:00
|
|
|
tmp = errno;
|
|
|
|
(void)close(fd);
|
|
|
|
errno = tmp;
|
2017-07-21 11:52:24 +10:00
|
|
|
fd = newfd;
|
2008-07-24 09:50:21 +00:00
|
|
|
err = "accept/fcntl";
|
2005-05-19 02:42:42 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef F_DUPFD */
|
2003-02-26 04:00:20 +00:00
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
if (fd < 0) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (SOFT_ERROR(errno)) {
|
2001-04-10 21:48:27 +00:00
|
|
|
goto soft_error;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-11-08 20:24:25 +00:00
|
|
|
switch (errno) {
|
2002-04-03 05:41:20 +00:00
|
|
|
case ENFILE:
|
2008-07-24 09:50:21 +00:00
|
|
|
case EMFILE:
|
2018-11-23 21:35:01 +01:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"%s: too many open file descriptors",
|
|
|
|
err);
|
2008-07-24 09:50:21 +00:00
|
|
|
goto soft_error;
|
|
|
|
|
|
|
|
case ENOBUFS:
|
2002-04-03 05:41:20 +00:00
|
|
|
case ENOMEM:
|
2001-11-08 20:24:25 +00:00
|
|
|
case ECONNRESET:
|
|
|
|
case ECONNABORTED:
|
|
|
|
case EHOSTUNREACH:
|
|
|
|
case EHOSTDOWN:
|
|
|
|
case ENETUNREACH:
|
|
|
|
case ENETDOWN:
|
|
|
|
case ECONNREFUSED:
|
|
|
|
#ifdef EPROTO
|
|
|
|
case EPROTO:
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef EPROTO */
|
2001-11-08 20:24:25 +00:00
|
|
|
#ifdef ENONET
|
|
|
|
case ENONET:
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef ENONET */
|
2001-11-08 20:24:25 +00:00
|
|
|
goto soft_error;
|
|
|
|
default:
|
|
|
|
break;
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2001-11-08 20:24:25 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2018-11-23 21:35:01 +01:00
|
|
|
"internal_accept: %s() failed: %s", err,
|
2001-11-08 20:24:25 +00:00
|
|
|
strbuf);
|
|
|
|
fd = -1;
|
|
|
|
result = ISC_R_UNEXPECTED;
|
2001-01-23 21:07:12 +00:00
|
|
|
} else {
|
2005-08-25 03:26:54 +00:00
|
|
|
if (addrlen == 0U) {
|
2001-02-07 23:40:28 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"internal_accept(): "
|
|
|
|
"accept() failed to return "
|
|
|
|
"remote address");
|
2001-02-08 00:04:11 +00:00
|
|
|
|
2001-02-07 23:40:28 +00:00
|
|
|
(void)close(fd);
|
2001-04-10 21:48:27 +00:00
|
|
|
goto soft_error;
|
2009-09-01 00:22:28 +00:00
|
|
|
} else if (NEWCONNSOCK(dev)->peer_address.type.sa.sa_family !=
|
2020-02-12 13:59:18 +01:00
|
|
|
sock->pf) {
|
|
|
|
UNEXPECTED_ERROR(
|
|
|
|
__FILE__, __LINE__,
|
|
|
|
"internal_accept(): "
|
|
|
|
"accept() returned peer address "
|
|
|
|
"family %u (expected %u)",
|
|
|
|
NEWCONNSOCK(dev)->peer_address.type.sa.sa_family,
|
|
|
|
sock->pf);
|
2001-02-06 23:04:02 +00:00
|
|
|
(void)close(fd);
|
2001-04-10 21:48:27 +00:00
|
|
|
goto soft_error;
|
2008-06-23 19:41:20 +00:00
|
|
|
} else if (fd >= (int)manager->maxsocks) {
|
2018-11-23 21:35:01 +01:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
2020-02-12 13:59:18 +01:00
|
|
|
"accept: file descriptor exceeds limit "
|
|
|
|
"(%d/%u)",
|
2018-11-23 21:35:01 +01:00
|
|
|
fd, manager->maxsocks);
|
2001-04-26 23:45:55 +00:00
|
|
|
(void)close(fd);
|
|
|
|
goto soft_error;
|
2001-02-06 23:04:02 +00:00
|
|
|
}
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2001-04-10 21:38:33 +00:00
|
|
|
if (fd != -1) {
|
2009-09-01 00:22:28 +00:00
|
|
|
NEWCONNSOCK(dev)->peer_address.length = addrlen;
|
|
|
|
NEWCONNSOCK(dev)->pf = sock->pf;
|
2001-02-08 00:04:11 +00:00
|
|
|
}
|
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
/*
|
|
|
|
* Pull off the done event.
|
|
|
|
*/
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LIST_UNLINK(sock->accept_list, dev, ev_link);
|
1999-06-10 00:45:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Poke watcher if there are more pending accepts.
|
|
|
|
*/
|
2019-07-04 16:10:19 +02:00
|
|
|
if (ISC_LIST_EMPTY(sock->accept_list)) {
|
2020-02-12 13:59:18 +01:00
|
|
|
unwatch_fd(thread, sock->fd, SELECT_POKE_ACCEPT);
|
2019-07-04 16:10:19 +02:00
|
|
|
}
|
1999-06-10 00:45:30 +00:00
|
|
|
|
2012-01-27 01:21:41 +00:00
|
|
|
if (fd != -1) {
|
|
|
|
result = make_nonblock(fd);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
(void)close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
1998-12-05 01:44:38 +00:00
|
|
|
}
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2020-05-19 10:08:25 +02:00
|
|
|
/*
|
|
|
|
* We need to unlock sock->lock now to be able to lock manager->lock
|
|
|
|
* without risking a deadlock with xmlstats.
|
|
|
|
*/
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
1998-11-10 11:37:54 +00:00
|
|
|
/*
|
1998-12-10 16:14:05 +00:00
|
|
|
* -1 means the new socket didn't happen.
|
1998-11-10 11:37:54 +00:00
|
|
|
*/
|
1998-12-10 16:14:05 +00:00
|
|
|
if (fd != -1) {
|
2008-06-23 19:41:20 +00:00
|
|
|
int lockid = FDLOCK_ID(fd);
|
|
|
|
|
2009-09-01 00:22:28 +00:00
|
|
|
NEWCONNSOCK(dev)->fd = fd;
|
2018-10-04 16:11:24 +02:00
|
|
|
NEWCONNSOCK(dev)->threadid = gen_threadid(NEWCONNSOCK(dev));
|
2009-09-01 00:22:28 +00:00
|
|
|
NEWCONNSOCK(dev)->bound = 1;
|
|
|
|
NEWCONNSOCK(dev)->connected = 1;
|
2018-10-03 22:46:23 +02:00
|
|
|
nthread = &manager->threads[NEWCONNSOCK(dev)->threadid];
|
1998-11-10 11:37:54 +00:00
|
|
|
|
2019-05-22 12:37:03 +02:00
|
|
|
/*
|
|
|
|
* We already hold a lock on one fdlock in accepting thread,
|
|
|
|
* we need to make sure that we don't double lock.
|
|
|
|
*/
|
2020-02-13 14:44:37 -08:00
|
|
|
bool same_bucket = (sock->threadid ==
|
|
|
|
NEWCONNSOCK(dev)->threadid) &&
|
|
|
|
(FDLOCK_ID(sock->fd) == lockid);
|
2019-05-22 12:37:03 +02:00
|
|
|
|
2012-11-01 08:42:56 +11:00
|
|
|
/*
|
|
|
|
* Use minimum mtu if possible.
|
|
|
|
*/
|
|
|
|
use_min_mtu(NEWCONNSOCK(dev));
|
2016-08-16 07:42:25 +10:00
|
|
|
set_tcp_maxseg(NEWCONNSOCK(dev), 1280 - 20 - 40);
|
2012-11-01 08:42:56 +11:00
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
/*
|
|
|
|
* Ensure DSCP settings are inherited across accept.
|
|
|
|
*/
|
2014-07-18 11:40:44 +10:00
|
|
|
setdscp(NEWCONNSOCK(dev), sock->dscp);
|
2013-03-22 12:27:54 -07:00
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
/*
|
|
|
|
* Save away the remote address
|
|
|
|
*/
|
2009-09-01 00:22:28 +00:00
|
|
|
dev->address = NEWCONNSOCK(dev)->peer_address;
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2017-11-02 10:42:42 +11:00
|
|
|
if (NEWCONNSOCK(dev)->active == 0) {
|
|
|
|
inc_stats(manager->stats,
|
|
|
|
NEWCONNSOCK(dev)->statsindex[STATID_ACTIVE]);
|
|
|
|
NEWCONNSOCK(dev)->active = 1;
|
|
|
|
}
|
|
|
|
|
2019-05-22 12:37:03 +02:00
|
|
|
if (!same_bucket) {
|
|
|
|
LOCK(&nthread->fdlock[lockid]);
|
|
|
|
}
|
2018-10-04 14:32:28 +00:00
|
|
|
nthread->fds[fd] = NEWCONNSOCK(dev);
|
|
|
|
nthread->fdstate[fd] = MANAGED;
|
2015-11-19 11:01:45 +05:30
|
|
|
#if defined(USE_EPOLL)
|
2018-10-04 14:32:28 +00:00
|
|
|
nthread->epoll_events[fd] = 0;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_EPOLL) */
|
2019-05-22 12:37:03 +02:00
|
|
|
if (!same_bucket) {
|
|
|
|
UNLOCK(&nthread->fdlock[lockid]);
|
|
|
|
}
|
2013-04-03 17:42:42 +11:00
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_SELECT
|
2020-02-13 21:48:23 +01:00
|
|
|
if (nthread->maxfd < fd) {
|
2018-10-03 22:46:23 +02:00
|
|
|
nthread->maxfd = fd;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
#endif /* ifdef USE_SELECT */
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2009-09-01 00:22:28 +00:00
|
|
|
socket_log(sock, &NEWCONNSOCK(dev)->peer_address, CREATION,
|
2000-12-19 20:35:37 +00:00
|
|
|
"accepted connection, new socket %p",
|
2000-05-03 21:09:49 +00:00
|
|
|
dev->newsocket);
|
2000-06-02 20:09:01 +00:00
|
|
|
|
2013-04-03 17:42:42 +11:00
|
|
|
ISC_LIST_APPEND(manager->socklist, NEWCONNSOCK(dev), link);
|
|
|
|
|
2001-04-26 23:45:55 +00:00
|
|
|
UNLOCK(&manager->lock);
|
2009-01-27 22:30:00 +00:00
|
|
|
|
|
|
|
inc_stats(manager->stats, sock->statsindex[STATID_ACCEPT]);
|
2001-04-26 23:45:55 +00:00
|
|
|
} else {
|
2009-01-27 22:30:00 +00:00
|
|
|
inc_stats(manager->stats, sock->statsindex[STATID_ACCEPTFAIL]);
|
2019-12-05 13:29:45 +11:00
|
|
|
isc_refcount_decrementz(&NEWCONNSOCK(dev)->references);
|
2021-04-19 11:12:58 +02:00
|
|
|
free_socket((isc_socket_t **)&dev->newsocket);
|
2001-04-26 23:45:55 +00:00
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
/*
|
|
|
|
* Fill in the done event details and send it off.
|
|
|
|
*/
|
|
|
|
dev->result = result;
|
2000-04-17 19:22:44 +00:00
|
|
|
task = dev->ev_sender;
|
|
|
|
dev->ev_sender = sock;
|
1999-06-10 00:45:30 +00:00
|
|
|
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendtoanddetach(&task, ISC_EVENT_PTR(&dev), sock->threadid);
|
2001-04-10 21:48:27 +00:00
|
|
|
return;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
soft_error:
|
2018-10-03 22:46:23 +02:00
|
|
|
watch_fd(thread, sock->fd, SELECT_POKE_ACCEPT);
|
2020-05-19 10:08:25 +02:00
|
|
|
UNLOCK(&sock->lock);
|
2009-01-27 22:30:00 +00:00
|
|
|
|
|
|
|
inc_stats(manager->stats, sock->statsindex[STATID_ACCEPTFAIL]);
|
2001-04-10 21:48:27 +00:00
|
|
|
return;
|
1998-11-07 02:31:04 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
internal_recv(isc_socket_t *sock) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t *dev;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_SOCKET(sock));
|
2020-02-05 12:35:54 +01:00
|
|
|
REQUIRE(sock->fd >= 0);
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2018-08-21 14:04:06 +02:00
|
|
|
dev = ISC_LIST_HEAD(sock->recv_list);
|
|
|
|
if (dev == NULL) {
|
2018-10-04 14:52:54 +00:00
|
|
|
goto finish;
|
1999-07-28 01:07:21 +00:00
|
|
|
}
|
1998-11-07 02:31:04 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
socket_log(sock, NULL, IOEVENT, "internal_recv: event %p -> task %p",
|
|
|
|
dev, dev->ev_sender);
|
2018-08-21 14:04:06 +02:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
|
|
|
* Try to do as much I/O as possible on this socket. There are no
|
2001-01-26 23:17:26 +00:00
|
|
|
* limits here, currently.
|
1998-11-06 01:45:35 +00:00
|
|
|
*/
|
1999-07-28 06:50:10 +00:00
|
|
|
while (dev != NULL) {
|
1999-09-03 00:22:28 +00:00
|
|
|
switch (doio_recv(sock, dev)) {
|
|
|
|
case DOIO_SOFT:
|
2018-10-04 14:52:54 +00:00
|
|
|
goto finish;
|
1998-12-18 01:48:43 +00:00
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
case DOIO_EOF:
|
1998-12-18 01:48:43 +00:00
|
|
|
/*
|
1999-09-03 00:22:28 +00:00
|
|
|
* read of 0 means the remote end was closed.
|
|
|
|
* Run through the event queue and dispatch all
|
2001-01-26 23:17:26 +00:00
|
|
|
* the events with an EOF result code.
|
1998-12-18 01:48:43 +00:00
|
|
|
*/
|
1998-11-06 01:45:35 +00:00
|
|
|
do {
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = ISC_R_EOF;
|
|
|
|
send_recvdone_event(sock, &dev);
|
1999-07-28 01:07:21 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->recv_list);
|
|
|
|
} while (dev != NULL);
|
2018-10-04 14:52:54 +00:00
|
|
|
goto finish;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
case DOIO_SUCCESS:
|
1999-09-03 01:18:45 +00:00
|
|
|
case DOIO_HARD:
|
2001-02-12 21:43:17 +00:00
|
|
|
send_recvdone_event(sock, &dev);
|
1999-09-03 00:22:28 +00:00
|
|
|
break;
|
1998-11-10 11:37:54 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->recv_list);
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
finish:
|
2018-11-08 19:34:51 -08:00
|
|
|
if (ISC_LIST_EMPTY(sock->recv_list)) {
|
2018-10-04 14:52:54 +00:00
|
|
|
unwatch_fd(&sock->manager->threads[sock->threadid], sock->fd,
|
|
|
|
SELECT_POKE_READ);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
internal_send(isc_socket_t *sock) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t *dev;
|
1999-07-28 21:30:37 +00:00
|
|
|
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_SOCKET(sock));
|
2020-02-05 12:35:54 +01:00
|
|
|
REQUIRE(sock->fd >= 0);
|
1999-07-28 06:50:10 +00:00
|
|
|
|
2018-08-21 14:04:06 +02:00
|
|
|
dev = ISC_LIST_HEAD(sock->send_list);
|
|
|
|
if (dev == NULL) {
|
2018-10-04 14:52:54 +00:00
|
|
|
goto finish;
|
1999-07-28 06:50:10 +00:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
socket_log(sock, NULL, EVENT, "internal_send: event %p -> task %p", dev,
|
|
|
|
dev->ev_sender);
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to do as much I/O as possible on this socket. There are no
|
2001-01-26 23:17:26 +00:00
|
|
|
* limits here, currently.
|
1998-11-11 00:43:14 +00:00
|
|
|
*/
|
1999-07-28 06:50:10 +00:00
|
|
|
while (dev != NULL) {
|
1999-09-03 01:18:45 +00:00
|
|
|
switch (doio_send(sock, dev)) {
|
|
|
|
case DOIO_SOFT:
|
2018-10-04 14:52:54 +00:00
|
|
|
goto finish;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1999-09-03 01:18:45 +00:00
|
|
|
case DOIO_HARD:
|
|
|
|
case DOIO_SUCCESS:
|
2001-02-12 21:43:17 +00:00
|
|
|
send_senddone_event(sock, &dev);
|
1999-09-03 01:18:45 +00:00
|
|
|
break;
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->send_list);
|
|
|
|
}
|
1998-11-11 00:43:14 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
finish:
|
2018-11-08 19:34:51 -08:00
|
|
|
if (ISC_LIST_EMPTY(sock->send_list)) {
|
2020-02-12 13:59:18 +01:00
|
|
|
unwatch_fd(&sock->manager->threads[sock->threadid], sock->fd,
|
|
|
|
SELECT_POKE_WRITE);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/*
|
|
|
|
* Process read/writes on each fd here. Avoid locking
|
|
|
|
* and unlocking twice if both reads and writes are possible.
|
|
|
|
*/
|
2000-08-29 23:58:17 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
process_fd(isc__socketthread_t *thread, int fd, bool readable, bool writeable) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_t *sock;
|
2020-02-13 14:44:37 -08:00
|
|
|
int lockid = FDLOCK_ID(fd);
|
2001-04-26 23:45:55 +00:00
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
/*
|
2008-08-01 19:04:02 +00:00
|
|
|
* If the socket is going to be closed, don't do more I/O.
|
2000-08-29 23:58:17 +00:00
|
|
|
*/
|
2018-10-03 22:46:23 +02:00
|
|
|
LOCK(&thread->fdlock[lockid]);
|
|
|
|
if (thread->fdstate[fd] == CLOSE_PENDING) {
|
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
(void)unwatch_fd(thread, fd, SELECT_POKE_READ);
|
|
|
|
(void)unwatch_fd(thread, fd, SELECT_POKE_WRITE);
|
2008-06-23 19:41:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
sock = thread->fds[fd];
|
|
|
|
if (sock == NULL) {
|
2018-10-04 14:52:54 +00:00
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
|
|
|
return;
|
2018-10-03 22:46:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 12:35:54 +01:00
|
|
|
LOCK(&sock->lock);
|
2020-05-19 10:08:25 +02:00
|
|
|
|
2020-02-05 12:35:54 +01:00
|
|
|
if (sock->fd < 0) {
|
2018-11-28 09:55:15 +00:00
|
|
|
/*
|
2020-02-05 12:35:54 +01:00
|
|
|
* Sock is being closed - the final external reference
|
|
|
|
* is gone but it was not yet removed from event loop
|
|
|
|
* and fdstate[]/fds[] as destroy() is waiting on
|
|
|
|
* thread->fdlock[lockid] or sock->lock that we're holding.
|
|
|
|
* Just release the locks and bail.
|
2018-11-28 09:55:15 +00:00
|
|
|
*/
|
2020-05-19 10:08:25 +02:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
|
|
|
return;
|
2018-11-28 09:55:15 +00:00
|
|
|
}
|
2018-08-21 14:04:06 +02:00
|
|
|
|
2020-05-19 10:08:25 +02:00
|
|
|
REQUIRE(readable || writeable);
|
2008-06-23 19:41:20 +00:00
|
|
|
if (writeable) {
|
2018-11-08 19:34:51 -08:00
|
|
|
if (sock->connecting) {
|
2018-10-03 20:39:39 +00:00
|
|
|
internal_connect(sock);
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2018-10-03 20:39:39 +00:00
|
|
|
internal_send(sock);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
2008-12-03 02:03:47 +00:00
|
|
|
|
2020-07-07 11:11:15 +10:00
|
|
|
if (readable) {
|
|
|
|
if (sock->listener) {
|
|
|
|
internal_accept(sock); /* unlocks sock */
|
|
|
|
} else {
|
|
|
|
internal_recv(sock);
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
UNLOCK(&thread->fdlock[lockid]);
|
2020-05-19 10:08:25 +02:00
|
|
|
|
2020-02-05 12:35:54 +01:00
|
|
|
/*
|
|
|
|
* Socket destruction might be pending, it will resume
|
|
|
|
* after releasing fdlock and sock->lock.
|
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 10:20:29 +00:00
|
|
|
/*
|
|
|
|
* process_fds is different for different event loops
|
|
|
|
* it takes the events from event loops and for each FD
|
|
|
|
* launches process_fd
|
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_KQUEUE
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
process_fds(isc__socketthread_t *thread, struct kevent *events, int nevents) {
|
|
|
|
int i;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool readable, writable;
|
|
|
|
bool done = false;
|
|
|
|
bool have_ctlevent = false;
|
2018-10-03 22:46:23 +02:00
|
|
|
if (nevents == thread->nevents) {
|
2005-12-06 16:54:49 +00:00
|
|
|
/*
|
2008-06-23 19:41:20 +00:00
|
|
|
* This is not an error, but something unexpected. If this
|
|
|
|
* happens, it may indicate the need for increasing
|
|
|
|
* ISC_SOCKET_MAXEVENTS.
|
2005-12-06 16:54:49 +00:00
|
|
|
*/
|
2018-10-03 22:46:23 +02:00
|
|
|
thread_log(thread, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
|
|
|
|
"maximum number of FD events (%d) received",
|
|
|
|
nevents);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nevents; i++) {
|
2018-10-03 22:46:23 +02:00
|
|
|
REQUIRE(events[i].ident < thread->manager->maxsocks);
|
|
|
|
if (events[i].ident == (uintptr_t)thread->pipe_fds[0]) {
|
2018-04-17 08:29:14 -07:00
|
|
|
have_ctlevent = true;
|
2000-08-29 23:58:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-04-17 08:29:14 -07:00
|
|
|
readable = (events[i].filter == EVFILT_READ);
|
|
|
|
writable = (events[i].filter == EVFILT_WRITE);
|
2018-10-03 22:46:23 +02:00
|
|
|
process_fd(thread, events[i].ident, readable, writable);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (have_ctlevent) {
|
2018-10-03 22:46:23 +02:00
|
|
|
done = process_ctlfd(thread);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-08-01 19:04:02 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
return (done);
|
|
|
|
}
|
|
|
|
#elif defined(USE_EPOLL)
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2018-10-03 22:46:23 +02:00
|
|
|
process_fds(isc__socketthread_t *thread, struct epoll_event *events,
|
2020-02-13 14:44:37 -08:00
|
|
|
int nevents) {
|
2008-06-23 19:41:20 +00:00
|
|
|
int i;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool done = false;
|
|
|
|
bool have_ctlevent = false;
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
if (nevents == thread->nevents) {
|
|
|
|
thread_log(thread, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
|
|
|
|
"maximum number of FD events (%d) received",
|
|
|
|
nevents);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nevents; i++) {
|
2018-10-03 22:46:23 +02:00
|
|
|
REQUIRE(events[i].data.fd < (int)thread->manager->maxsocks);
|
|
|
|
if (events[i].data.fd == thread->pipe_fds[0]) {
|
2018-04-17 08:29:14 -07:00
|
|
|
have_ctlevent = true;
|
2008-06-23 19:41:20 +00:00
|
|
|
continue;
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
if ((events[i].events & EPOLLERR) != 0 ||
|
|
|
|
(events[i].events & EPOLLHUP) != 0) {
|
|
|
|
/*
|
|
|
|
* epoll does not set IN/OUT bits on an erroneous
|
|
|
|
* condition, so we need to try both anyway. This is a
|
|
|
|
* bit inefficient, but should be okay for such rare
|
|
|
|
* events. Note also that the read or write attempt
|
|
|
|
* won't block because we use non-blocking sockets.
|
|
|
|
*/
|
2016-12-08 16:59:46 +11:00
|
|
|
int fd = events[i].data.fd;
|
2018-10-03 22:46:23 +02:00
|
|
|
events[i].events |= thread->epoll_events[fd];
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
process_fd(thread, events[i].data.fd,
|
2008-06-23 19:41:20 +00:00
|
|
|
(events[i].events & EPOLLIN) != 0,
|
|
|
|
(events[i].events & EPOLLOUT) != 0);
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (have_ctlevent) {
|
2018-10-03 22:46:23 +02:00
|
|
|
done = process_ctlfd(thread);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-08-01 19:04:02 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
return (done);
|
|
|
|
}
|
|
|
|
#elif defined(USE_DEVPOLL)
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
process_fds(isc__socketthread_t *thread, struct pollfd *events, int nevents) {
|
|
|
|
int i;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool done = false;
|
|
|
|
bool have_ctlevent = false;
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
if (nevents == thread->nevents) {
|
2018-10-04 16:11:24 +02:00
|
|
|
thread_log(thread, ISC_LOGCATEGORY_GENERAL,
|
2018-10-03 22:46:23 +02:00
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
|
|
|
|
"maximum number of FD events (%d) received",
|
|
|
|
nevents);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nevents; i++) {
|
2018-10-04 16:11:24 +02:00
|
|
|
REQUIRE(events[i].fd < (int)thread->manager->maxsocks);
|
2018-10-03 22:46:23 +02:00
|
|
|
if (events[i].fd == thread->pipe_fds[0]) {
|
2018-04-17 08:29:14 -07:00
|
|
|
have_ctlevent = true;
|
2008-06-23 19:41:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
process_fd(thread, events[i].fd,
|
2008-06-23 19:41:20 +00:00
|
|
|
(events[i].events & POLLIN) != 0,
|
|
|
|
(events[i].events & POLLOUT) != 0);
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (have_ctlevent) {
|
2018-10-03 22:46:23 +02:00
|
|
|
done = process_ctlfd(thread);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-08-01 19:04:02 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
return (done);
|
|
|
|
}
|
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
static void
|
2018-10-03 22:46:23 +02:00
|
|
|
process_fds(isc__socketthread_t *thread, int maxfd, fd_set *readfds,
|
2020-02-13 14:44:37 -08:00
|
|
|
fd_set *writefds) {
|
2008-06-23 19:41:20 +00:00
|
|
|
int i;
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
REQUIRE(maxfd <= (int)thread->manager->maxsocks);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
for (i = 0; i < maxfd; i++) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (i == thread->pipe_fds[0] || i == thread->pipe_fds[1]) {
|
2008-06-23 19:41:20 +00:00
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
process_fd(thread, i, FD_ISSET(i, readfds),
|
2008-06-23 19:41:20 +00:00
|
|
|
FD_ISSET(i, writefds));
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_KQUEUE */
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
process_ctlfd(isc__socketthread_t *thread) {
|
2008-06-23 19:41:20 +00:00
|
|
|
int msg, fd;
|
|
|
|
|
|
|
|
for (;;) {
|
2018-10-03 22:46:23 +02:00
|
|
|
select_readmsg(thread, &fd, &msg);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
thread_log(thread, IOEVENT,
|
2018-11-23 21:35:01 +01:00
|
|
|
"watcher got message %d for socket %d", msg, fd);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Nothing to read?
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (msg == SELECT_POKE_NOTHING) {
|
2008-08-09 19:02:10 +00:00
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle shutdown message. We really should
|
|
|
|
* jump out of this loop right away, but
|
|
|
|
* it doesn't matter if we have to do a little
|
|
|
|
* more work first.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (msg == SELECT_POKE_SHUTDOWN) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a wakeup on a socket. Look
|
|
|
|
* at the event queue for both read and write,
|
|
|
|
* and decide if we need to watch on it now
|
|
|
|
* or not.
|
|
|
|
*/
|
2018-10-03 22:46:23 +02:00
|
|
|
wakeup_socket(thread, fd, msg);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
1998-11-06 01:45:35 +00:00
|
|
|
* This is the thread that will loop forever, always in a select or poll
|
|
|
|
* call.
|
|
|
|
*
|
2018-10-03 22:46:23 +02:00
|
|
|
* When select returns something to do, do whatever's necessary and post
|
|
|
|
* an event to the task that was requesting the action.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
|
|
|
static isc_threadresult_t
|
2020-02-13 14:44:37 -08:00
|
|
|
netthread(void *uap) {
|
2018-10-03 22:46:23 +02:00
|
|
|
isc__socketthread_t *thread = uap;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_t *manager = thread->manager;
|
2018-10-03 22:46:23 +02:00
|
|
|
(void)manager;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool done;
|
2020-02-13 14:44:37 -08:00
|
|
|
int cc;
|
2018-10-18 20:47:23 +00:00
|
|
|
if (manager->nthreads > 1) {
|
|
|
|
isc_thread_setaffinity(thread->threadid);
|
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
const char *fnname = "kevent()";
|
2020-02-12 13:59:18 +01:00
|
|
|
#elif defined(USE_EPOLL)
|
2008-06-23 19:41:20 +00:00
|
|
|
const char *fnname = "epoll_wait()";
|
|
|
|
#elif defined(USE_DEVPOLL)
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
|
|
|
const char *fnname = "ioctl(DP_POLL)";
|
2008-06-23 19:41:20 +00:00
|
|
|
struct dvpoll dvp;
|
2020-02-13 14:44:37 -08:00
|
|
|
int pass;
|
2016-08-30 14:25:41 +10:00
|
|
|
#if defined(ISC_SOCKET_USE_POLLWATCH)
|
2020-02-13 14:44:37 -08:00
|
|
|
pollstate_t pollstate = poll_idle;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(ISC_SOCKET_USE_POLLWATCH) */
|
2020-02-12 13:59:18 +01:00
|
|
|
#elif defined(USE_SELECT)
|
2008-06-23 19:41:20 +00:00
|
|
|
const char *fnname = "select()";
|
1998-11-11 01:44:08 +00:00
|
|
|
int maxfd;
|
2011-08-25 08:17:54 +00:00
|
|
|
int ctlfd;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef USE_KQUEUE */
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#if defined(USE_SELECT)
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Get the control fd here. This will never change.
|
|
|
|
*/
|
2018-10-03 22:46:23 +02:00
|
|
|
ctlfd = thread->pipe_fds[0];
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_SELECT) */
|
2018-04-17 08:29:14 -07:00
|
|
|
done = false;
|
1998-11-03 00:54:47 +00:00
|
|
|
while (!done) {
|
1998-11-11 00:43:14 +00:00
|
|
|
do {
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_KQUEUE
|
2020-02-12 13:59:18 +01:00
|
|
|
cc = kevent(thread->kqueue_fd, NULL, 0, thread->events,
|
|
|
|
thread->nevents, NULL);
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_EPOLL)
|
2020-02-12 13:59:18 +01:00
|
|
|
cc = epoll_wait(thread->epoll_fd, thread->events,
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->nevents, -1);
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_DEVPOLL)
|
2020-02-13 14:44:37 -08:00
|
|
|
/*
|
|
|
|
* Re-probe every thousand calls.
|
|
|
|
*/
|
|
|
|
if (thread->calls++ > 1000U) {
|
|
|
|
result = isc_resource_getcurlimit(
|
|
|
|
isc_resource_openfiles,
|
|
|
|
&thread->open_max);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
thread->open_max = 64;
|
|
|
|
}
|
|
|
|
thread->calls = 0;
|
|
|
|
}
|
|
|
|
for (pass = 0; pass < 2; pass++) {
|
|
|
|
dvp.dp_fds = thread->events;
|
|
|
|
dvp.dp_nfds = thread->nevents;
|
|
|
|
if (dvp.dp_nfds >= thread->open_max) {
|
|
|
|
dvp.dp_nfds = thread->open_max - 1;
|
|
|
|
}
|
2008-11-08 22:35:12 +00:00
|
|
|
#ifndef ISC_SOCKET_USE_POLLWATCH
|
2020-02-13 14:44:37 -08:00
|
|
|
dvp.dp_timeout = -1;
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifndef ISC_SOCKET_USE_POLLWATCH */
|
2018-11-08 19:34:51 -08:00
|
|
|
if (pollstate == poll_idle) {
|
2014-05-23 13:05:23 +10:00
|
|
|
dvp.dp_timeout = -1;
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2014-05-23 13:05:23 +10:00
|
|
|
dvp.dp_timeout =
|
2020-02-12 13:59:18 +01:00
|
|
|
ISC_SOCKET_POLLWATCH_TIMEOUT;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* ISC_SOCKET_USE_POLLWATCH */
|
2020-02-13 14:44:37 -08:00
|
|
|
cc = ioctl(thread->devpoll_fd, DP_POLL, &dvp);
|
|
|
|
if (cc == -1 && errno == EINVAL) {
|
|
|
|
/*
|
|
|
|
* {OPEN_MAX} may have dropped. Look
|
|
|
|
* up the current value and try again.
|
|
|
|
*/
|
|
|
|
result = isc_resource_getcurlimit(
|
|
|
|
isc_resource_openfiles,
|
|
|
|
&thread->open_max);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
thread->open_max = 64;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_SELECT)
|
2018-10-03 22:46:23 +02:00
|
|
|
/*
|
|
|
|
* We will have only one thread anyway, we can lock
|
|
|
|
* manager lock and don't care
|
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
LOCK(&manager->lock);
|
2018-10-03 22:46:23 +02:00
|
|
|
memmove(thread->read_fds_copy, thread->read_fds,
|
|
|
|
thread->fd_bufsize);
|
|
|
|
memmove(thread->write_fds_copy, thread->write_fds,
|
|
|
|
thread->fd_bufsize);
|
|
|
|
maxfd = thread->maxfd + 1;
|
1998-11-11 00:43:14 +00:00
|
|
|
UNLOCK(&manager->lock);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
cc = select(maxfd, thread->read_fds_copy,
|
|
|
|
thread->write_fds_copy, NULL, NULL);
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* USE_KQUEUE */
|
1998-11-11 00:43:14 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
if (cc < 0 && !SOFT_ERROR(errno)) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2020-02-12 13:59:18 +01:00
|
|
|
FATAL_ERROR(__FILE__, __LINE__, "%s failed: %s",
|
|
|
|
fnname, strbuf);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
2008-11-08 22:35:12 +00:00
|
|
|
|
|
|
|
#if defined(USE_DEVPOLL) && defined(ISC_SOCKET_USE_POLLWATCH)
|
|
|
|
if (cc == 0) {
|
2018-11-08 19:34:51 -08:00
|
|
|
if (pollstate == poll_active) {
|
2008-11-08 22:35:12 +00:00
|
|
|
pollstate = poll_checking;
|
2018-11-08 19:34:51 -08:00
|
|
|
} else if (pollstate == poll_checking) {
|
2008-11-08 22:35:12 +00:00
|
|
|
pollstate = poll_idle;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2008-11-08 22:35:12 +00:00
|
|
|
} else if (cc > 0) {
|
|
|
|
if (pollstate == poll_checking) {
|
|
|
|
/*
|
|
|
|
* XXX: We'd like to use a more
|
|
|
|
* verbose log level as it's actually an
|
|
|
|
* unexpected event, but the kernel bug
|
|
|
|
* reportedly happens pretty frequently
|
|
|
|
* (and it can also be a false positive)
|
|
|
|
* so it would be just too noisy.
|
|
|
|
*/
|
2018-10-03 22:46:23 +02:00
|
|
|
thread_log(thread,
|
|
|
|
ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET,
|
|
|
|
ISC_LOG_DEBUG(1),
|
|
|
|
"unexpected POLL timeout");
|
2008-11-08 22:35:12 +00:00
|
|
|
}
|
|
|
|
pollstate = poll_active;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_DEVPOLL) && defined(ISC_SOCKET_USE_POLLWATCH) */
|
1998-11-06 01:45:35 +00:00
|
|
|
} while (cc < 0);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
|
2018-10-03 22:46:23 +02:00
|
|
|
done = process_fds(thread, thread->events, cc);
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_SELECT)
|
2018-10-03 22:46:23 +02:00
|
|
|
process_fds(thread, maxfd, thread->read_fds_copy,
|
|
|
|
thread->write_fds_copy);
|
2008-08-01 19:04:02 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Process reads on internal, control fd.
|
|
|
|
*/
|
2018-11-08 19:34:51 -08:00
|
|
|
if (FD_ISSET(ctlfd, thread->read_fds_copy)) {
|
2018-10-03 22:46:23 +02:00
|
|
|
done = process_ctlfd(thread);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL) \
|
|
|
|
* */
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
thread_log(thread, TRACE, "watcher exiting");
|
1998-11-03 00:54:47 +00:00
|
|
|
return ((isc_threadresult_t)0);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_setreserved(isc_socketmgr_t *manager, uint32_t reserved) {
|
2008-09-04 05:56:43 +00:00
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
manager->reserved = reserved;
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_maxudp(isc_socketmgr_t *manager, unsigned int maxudp) {
|
2009-03-05 03:13:55 +00:00
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
manager->maxudp = maxudp;
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
2018-10-03 22:46:23 +02:00
|
|
|
* Setup socket thread, thread->manager and thread->threadid must be filled.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
setup_thread(isc__socketthread_t *thread) {
|
2018-10-03 22:46:23 +02:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2020-02-13 14:44:37 -08:00
|
|
|
int i;
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2018-11-08 19:34:51 -08:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
REQUIRE(thread != NULL);
|
|
|
|
REQUIRE(VALID_MANAGER(thread->manager));
|
2018-11-08 19:34:51 -08:00
|
|
|
REQUIRE(thread->threadid >= 0 &&
|
|
|
|
thread->threadid < thread->manager->nthreads);
|
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
thread->fds =
|
|
|
|
isc_mem_get(thread->manager->mctx,
|
|
|
|
thread->manager->maxsocks * sizeof(isc_socket_t *));
|
2018-10-03 22:46:23 +02:00
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
memset(thread->fds, 0,
|
|
|
|
thread->manager->maxsocks * sizeof(isc_socket_t *));
|
|
|
|
|
|
|
|
thread->fdstate = isc_mem_get(thread->manager->mctx,
|
|
|
|
thread->manager->maxsocks * sizeof(int));
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
memset(thread->fdstate, 0, thread->manager->maxsocks * sizeof(int));
|
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
thread->fdlock = isc_mem_get(thread->manager->mctx,
|
|
|
|
FDLOCK_COUNT * sizeof(isc_mutex_t));
|
2018-10-03 22:46:23 +02:00
|
|
|
|
|
|
|
for (i = 0; i < FDLOCK_COUNT; i++) {
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&thread->fdlock[i]);
|
2018-10-03 22:46:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pipe(thread->pipe_fds) != 0) {
|
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2020-02-12 13:59:18 +01:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "pipe() failed: %s",
|
2018-10-03 22:46:23 +02:00
|
|
|
strbuf);
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
RUNTIME_CHECK(make_nonblock(thread->pipe_fds[0]) == ISC_R_SUCCESS);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->nevents = ISC_SOCKET_MAXEVENTS;
|
2018-11-08 19:34:51 -08:00
|
|
|
thread->events = isc_mem_get(thread->manager->mctx,
|
|
|
|
sizeof(struct kevent) * thread->nevents);
|
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->kqueue_fd = kqueue();
|
|
|
|
if (thread->kqueue_fd == -1) {
|
2008-06-23 19:41:20 +00:00
|
|
|
result = isc__errno2result(errno);
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2020-02-12 13:59:18 +01:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "kqueue failed: %s",
|
2008-07-24 05:19:15 +00:00
|
|
|
strbuf);
|
2018-10-03 22:46:23 +02:00
|
|
|
isc_mem_put(thread->manager->mctx, thread->events,
|
|
|
|
sizeof(struct kevent) * thread->nevents);
|
2008-06-23 19:41:20 +00:00
|
|
|
return (result);
|
|
|
|
}
|
2008-06-23 23:47:11 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
result = watch_fd(thread, thread->pipe_fds[0], SELECT_POKE_READ);
|
2008-06-23 19:41:20 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2018-10-03 22:46:23 +02:00
|
|
|
close(thread->kqueue_fd);
|
|
|
|
isc_mem_put(thread->manager->mctx, thread->events,
|
|
|
|
sizeof(struct kevent) * thread->nevents);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
return (result);
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_EPOLL)
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->nevents = ISC_SOCKET_MAXEVENTS;
|
2020-02-12 13:59:18 +01:00
|
|
|
thread->epoll_events =
|
|
|
|
isc_mem_get(thread->manager->mctx,
|
|
|
|
(thread->manager->maxsocks * sizeof(uint32_t)));
|
2018-11-08 19:34:51 -08:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
memset(thread->epoll_events, 0,
|
|
|
|
thread->manager->maxsocks * sizeof(uint32_t));
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
thread->events =
|
|
|
|
isc_mem_get(thread->manager->mctx,
|
|
|
|
sizeof(struct epoll_event) * thread->nevents);
|
2018-10-03 22:46:23 +02:00
|
|
|
|
|
|
|
thread->epoll_fd = epoll_create(thread->nevents);
|
|
|
|
if (thread->epoll_fd == -1) {
|
2008-06-23 19:41:20 +00:00
|
|
|
result = isc__errno2result(errno);
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2020-02-12 13:59:18 +01:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "epoll_create failed: %s",
|
|
|
|
strbuf);
|
2008-06-23 19:41:20 +00:00
|
|
|
return (result);
|
|
|
|
}
|
2018-11-08 19:34:51 -08:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
result = watch_fd(thread, thread->pipe_fds[0], SELECT_POKE_READ);
|
|
|
|
return (result);
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_DEVPOLL)
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->nevents = ISC_SOCKET_MAXEVENTS;
|
2014-05-23 13:05:23 +10:00
|
|
|
result = isc_resource_getcurlimit(isc_resource_openfiles,
|
2018-10-04 16:11:24 +02:00
|
|
|
&thread->open_max);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2018-10-04 16:11:24 +02:00
|
|
|
thread->open_max = 64;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2018-10-04 16:11:24 +02:00
|
|
|
thread->calls = 0;
|
|
|
|
thread->events = isc_mem_get(thread->manager->mctx,
|
2018-11-08 19:34:51 -08:00
|
|
|
sizeof(struct pollfd) * thread->nevents);
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/*
|
|
|
|
* Note: fdpollinfo should be able to support all possible FDs, so
|
|
|
|
* it must have maxsocks entries (not nevents).
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
thread->fdpollinfo =
|
|
|
|
isc_mem_get(thread->manager->mctx,
|
|
|
|
sizeof(pollinfo_t) * thread->manager->maxsocks);
|
|
|
|
memset(thread->fdpollinfo, 0,
|
|
|
|
sizeof(pollinfo_t) * thread->manager->maxsocks);
|
2018-10-04 16:11:24 +02:00
|
|
|
thread->devpoll_fd = open("/dev/poll", O_RDWR);
|
|
|
|
if (thread->devpoll_fd == -1) {
|
2008-06-23 19:41:20 +00:00
|
|
|
result = isc__errno2result(errno);
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2008-07-24 05:19:15 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"open(/dev/poll) failed: %s", strbuf);
|
2018-10-04 16:11:24 +02:00
|
|
|
isc_mem_put(thread->manager->mctx, thread->events,
|
|
|
|
sizeof(struct pollfd) * thread->nevents);
|
|
|
|
isc_mem_put(thread->manager->mctx, thread->fdpollinfo,
|
|
|
|
sizeof(pollinfo_t) * thread->manager->maxsocks);
|
2008-06-23 19:41:20 +00:00
|
|
|
return (result);
|
|
|
|
}
|
2018-10-04 16:11:24 +02:00
|
|
|
result = watch_fd(thread, thread->pipe_fds[0], SELECT_POKE_READ);
|
2008-06-23 19:41:20 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2018-10-04 16:11:24 +02:00
|
|
|
close(thread->devpoll_fd);
|
|
|
|
isc_mem_put(thread->manager->mctx, thread->events,
|
|
|
|
sizeof(struct pollfd) * thread->nevents);
|
|
|
|
isc_mem_put(thread->manager->mctx, thread->fdpollinfo,
|
|
|
|
sizeof(pollinfo_t) * thread->manager->maxsocks);
|
2008-06-23 19:41:20 +00:00
|
|
|
return (result);
|
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
UNUSED(result);
|
|
|
|
|
2008-08-07 21:28:13 +00:00
|
|
|
#if ISC_SOCKET_MAXSOCKETS > FD_SETSIZE
|
|
|
|
/*
|
|
|
|
* Note: this code should also cover the case of MAXSOCKETS <=
|
|
|
|
* FD_SETSIZE, but we separate the cases to avoid possible portability
|
|
|
|
* issues regarding howmany() and the actual representation of fd_set.
|
|
|
|
*/
|
2020-02-13 14:44:37 -08:00
|
|
|
thread->fd_bufsize = howmany(manager->maxsocks, NFDBITS) *
|
|
|
|
sizeof(fd_mask);
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if ISC_SOCKET_MAXSOCKETS > FD_SETSIZE */
|
2018-10-03 22:46:23 +02:00
|
|
|
thread->fd_bufsize = sizeof(fd_set);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if ISC_SOCKET_MAXSOCKETS > FD_SETSIZE */
|
2008-08-07 21:28:13 +00:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
thread->read_fds = isc_mem_get(thread->manager->mctx,
|
|
|
|
thread->fd_bufsize);
|
|
|
|
thread->read_fds_copy = isc_mem_get(thread->manager->mctx,
|
|
|
|
thread->fd_bufsize);
|
|
|
|
thread->write_fds = isc_mem_get(thread->manager->mctx,
|
|
|
|
thread->fd_bufsize);
|
|
|
|
thread->write_fds_copy = isc_mem_get(thread->manager->mctx,
|
|
|
|
thread->fd_bufsize);
|
2018-10-03 22:46:23 +02:00
|
|
|
memset(thread->read_fds, 0, thread->fd_bufsize);
|
|
|
|
memset(thread->write_fds, 0, thread->fd_bufsize);
|
2008-08-07 21:28:13 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
(void)watch_fd(thread, thread->pipe_fds[0], SELECT_POKE_READ);
|
|
|
|
thread->maxfd = thread->pipe_fds[0];
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* USE_KQUEUE */
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
cleanup_thread(isc_mem_t *mctx, isc__socketthread_t *thread) {
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
int i;
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2018-10-03 22:46:23 +02:00
|
|
|
result = unwatch_fd(thread, thread->pipe_fds[0], SELECT_POKE_READ);
|
2008-06-23 19:41:20 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-02-12 13:59:18 +01:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "epoll_ctl(DEL) failed");
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
#ifdef USE_KQUEUE
|
2018-10-03 22:46:23 +02:00
|
|
|
close(thread->kqueue_fd);
|
|
|
|
isc_mem_put(mctx, thread->events,
|
|
|
|
sizeof(struct kevent) * thread->nevents);
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_EPOLL)
|
2018-10-03 22:46:23 +02:00
|
|
|
close(thread->epoll_fd);
|
|
|
|
|
|
|
|
isc_mem_put(mctx, thread->events,
|
|
|
|
sizeof(struct epoll_event) * thread->nevents);
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_DEVPOLL)
|
2018-10-03 22:46:23 +02:00
|
|
|
close(thread->devpoll_fd);
|
|
|
|
isc_mem_put(mctx, thread->events,
|
|
|
|
sizeof(struct pollfd) * thread->nevents);
|
|
|
|
isc_mem_put(mctx, thread->fdpollinfo,
|
2018-10-04 16:11:24 +02:00
|
|
|
sizeof(pollinfo_t) * thread->manager->maxsocks);
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_SELECT)
|
2018-11-08 19:34:51 -08:00
|
|
|
if (thread->read_fds != NULL) {
|
2018-10-03 22:46:23 +02:00
|
|
|
isc_mem_put(mctx, thread->read_fds, thread->fd_bufsize);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
|
|
|
if (thread->read_fds_copy != NULL) {
|
2018-10-03 22:46:23 +02:00
|
|
|
isc_mem_put(mctx, thread->read_fds_copy, thread->fd_bufsize);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
|
|
|
if (thread->write_fds != NULL) {
|
2018-10-03 22:46:23 +02:00
|
|
|
isc_mem_put(mctx, thread->write_fds, thread->fd_bufsize);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
|
|
|
if (thread->write_fds_copy != NULL) {
|
2018-10-03 22:46:23 +02:00
|
|
|
isc_mem_put(mctx, thread->write_fds_copy, thread->fd_bufsize);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* USE_KQUEUE */
|
2018-11-08 19:34:51 -08:00
|
|
|
for (i = 0; i < (int)thread->manager->maxsocks; i++) {
|
|
|
|
if (thread->fdstate[i] == CLOSE_PENDING) {
|
|
|
|
/* no need to lock */
|
2018-10-03 22:46:23 +02:00
|
|
|
(void)close(i);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
|
|
|
}
|
2018-10-03 22:46:23 +02:00
|
|
|
|
|
|
|
#if defined(USE_EPOLL)
|
|
|
|
isc_mem_put(thread->manager->mctx, thread->epoll_events,
|
|
|
|
thread->manager->maxsocks * sizeof(uint32_t));
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(USE_EPOLL) */
|
2018-10-03 22:46:23 +02:00
|
|
|
isc_mem_put(thread->manager->mctx, thread->fds,
|
2021-04-19 11:12:58 +02:00
|
|
|
thread->manager->maxsocks * sizeof(isc_socket_t *));
|
2018-10-03 22:46:23 +02:00
|
|
|
isc_mem_put(thread->manager->mctx, thread->fdstate,
|
|
|
|
thread->manager->maxsocks * sizeof(int));
|
|
|
|
|
2020-09-09 10:40:14 +10:00
|
|
|
for (i = 0; i < FDLOCK_COUNT; i++) {
|
|
|
|
isc_mutex_destroy(&thread->fdlock[i]);
|
2018-10-03 22:46:23 +02:00
|
|
|
}
|
2020-09-09 10:40:14 +10:00
|
|
|
isc_mem_put(thread->manager->mctx, thread->fdlock,
|
|
|
|
FDLOCK_COUNT * sizeof(isc_mutex_t));
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-27 00:07:43 +02:00
|
|
|
isc__socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp,
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int maxsocks, int nthreads) {
|
|
|
|
int i;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_t *manager;
|
2008-06-24 01:58:16 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
REQUIRE(managerp != NULL && *managerp == NULL);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (maxsocks == 0) {
|
2008-08-20 23:57:59 +00:00
|
|
|
maxsocks = ISC_SOCKET_MAXSOCKETS;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2008-08-20 23:57:59 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
manager = isc_mem_get(mctx, sizeof(*manager));
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/* zero-clear so that necessary cleanup on failure will be easy */
|
|
|
|
memset(manager, 0, sizeof(*manager));
|
2008-08-20 23:57:59 +00:00
|
|
|
manager->maxsocks = maxsocks;
|
2008-09-04 05:56:43 +00:00
|
|
|
manager->reserved = 0;
|
2009-03-05 03:13:55 +00:00
|
|
|
manager->maxudp = 0;
|
2018-10-03 22:46:23 +02:00
|
|
|
manager->nthreads = nthreads;
|
2009-01-27 22:30:00 +00:00
|
|
|
manager->stats = NULL;
|
2008-06-23 19:41:20 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
manager->magic = SOCKET_MANAGER_MAGIC;
|
2000-06-21 00:01:52 +00:00
|
|
|
manager->mctx = NULL;
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LIST_INIT(manager->socklist);
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&manager->lock);
|
2018-11-15 17:20:36 +01:00
|
|
|
isc_condition_init(&manager->shutdown_ok);
|
2015-11-19 11:01:45 +05:30
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Start up the select/poll thread.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
manager->threads = isc_mem_get(mctx, sizeof(isc__socketthread_t) *
|
|
|
|
manager->nthreads);
|
2000-06-21 00:01:52 +00:00
|
|
|
isc_mem_attach(mctx, &manager->mctx);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
for (i = 0; i < manager->nthreads; i++) {
|
2018-10-03 22:46:23 +02:00
|
|
|
manager->threads[i].manager = manager;
|
|
|
|
manager->threads[i].threadid = i;
|
|
|
|
setup_thread(&manager->threads[i]);
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_thread_create(netthread, &manager->threads[i],
|
2019-07-18 17:24:05 +02:00
|
|
|
&manager->threads[i].thread);
|
2018-10-03 22:46:23 +02:00
|
|
|
char tname[1024];
|
|
|
|
sprintf(tname, "isc-socket-%d", i);
|
|
|
|
isc_thread_setname(manager->threads[i].thread, tname);
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
*managerp = manager;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2008-07-11 23:05:46 +00:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_getmaxsockets(isc_socketmgr_t *manager, unsigned int *nsockp) {
|
2008-07-11 23:05:46 +00:00
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(nsockp != NULL);
|
|
|
|
|
|
|
|
*nsockp = manager->maxsocks;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2009-01-27 22:30:00 +00:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_setstats(isc_socketmgr_t *manager, isc_stats_t *stats) {
|
2009-01-27 22:30:00 +00:00
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(ISC_LIST_EMPTY(manager->socklist));
|
|
|
|
REQUIRE(manager->stats == NULL);
|
|
|
|
REQUIRE(isc_stats_ncounters(stats) == isc_sockstatscounter_max);
|
|
|
|
|
|
|
|
isc_stats_attach(stats, &manager->stats);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-27 00:07:43 +02:00
|
|
|
isc__socketmgr_destroy(isc_socketmgr_t **managerp) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_t *manager;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a socket manager.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(managerp != NULL);
|
2021-04-19 11:12:58 +02:00
|
|
|
manager = *managerp;
|
1998-11-03 00:54:47 +00:00
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
1998-12-18 04:03:11 +00:00
|
|
|
|
|
|
|
/*
|
1999-05-17 22:31:26 +00:00
|
|
|
* Wait for all sockets to be destroyed.
|
1998-12-18 04:03:11 +00:00
|
|
|
*/
|
2000-06-02 01:27:27 +00:00
|
|
|
while (!ISC_LIST_EMPTY(manager->socklist)) {
|
2018-11-23 21:35:01 +01:00
|
|
|
manager_log(manager, CREATION, "sockets exist");
|
1999-05-17 22:31:26 +00:00
|
|
|
WAIT(&manager->shutdown_ok, &manager->lock);
|
2011-02-03 05:41:55 +00:00
|
|
|
}
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here, poke our select/poll thread. Do this by closing the write
|
|
|
|
* half of the pipe, which will send EOF to the read half.
|
2000-08-29 23:58:17 +00:00
|
|
|
* This is currently a no-op in the non-threaded case.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
2019-07-23 17:16:57 -04:00
|
|
|
for (int i = 0; i < manager->nthreads; i++) {
|
2018-10-03 22:46:23 +02:00
|
|
|
select_poke(manager, i, 0, SELECT_POKE_SHUTDOWN);
|
|
|
|
}
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for thread to exit.
|
|
|
|
*/
|
2019-07-23 17:16:57 -04:00
|
|
|
for (int i = 0; i < manager->nthreads; i++) {
|
2019-07-18 17:47:40 +02:00
|
|
|
isc_thread_join(manager->threads[i].thread, NULL);
|
2018-10-03 22:46:23 +02:00
|
|
|
cleanup_thread(manager->mctx, &manager->threads[i]);
|
|
|
|
}
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Clean up.
|
|
|
|
*/
|
2018-11-08 19:34:51 -08:00
|
|
|
isc_mem_put(manager->mctx, manager->threads,
|
|
|
|
sizeof(isc__socketthread_t) * manager->nthreads);
|
2000-08-29 23:58:17 +00:00
|
|
|
(void)isc_condition_destroy(&manager->shutdown_ok);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if (manager->stats != NULL) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_detach(&manager->stats);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&manager->lock);
|
2021-04-19 11:12:58 +02:00
|
|
|
manager->magic = 0;
|
2019-07-23 17:16:57 -04:00
|
|
|
isc_mem_putanddetach(&manager->mctx, manager, sizeof(*manager));
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
*managerp = NULL;
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
static isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
socket_recv(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int flags) {
|
|
|
|
int io_state;
|
|
|
|
bool have_lock = false;
|
|
|
|
isc_task_t *ntask = NULL;
|
2001-02-12 21:43:17 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
1999-09-03 16:38:08 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = task;
|
1999-09-03 16:38:08 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
|
|
|
io_state = doio_recv(sock, dev);
|
|
|
|
} else {
|
|
|
|
LOCK(&sock->lock);
|
2018-04-17 08:29:14 -07:00
|
|
|
have_lock = true;
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if (ISC_LIST_EMPTY(sock->recv_list)) {
|
2001-01-25 22:25:10 +00:00
|
|
|
io_state = doio_recv(sock, dev);
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2001-01-25 22:25:10 +00:00
|
|
|
io_state = DOIO_SOFT;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
}
|
1999-09-03 16:38:08 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
switch (io_state) {
|
1999-09-03 16:38:08 +00:00
|
|
|
case DOIO_SOFT:
|
2001-01-25 22:25:10 +00:00
|
|
|
/*
|
|
|
|
* We couldn't read all or part of the request right now, so
|
|
|
|
* queue it.
|
|
|
|
*
|
|
|
|
* Attach to socket and to task
|
|
|
|
*/
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
|
|
|
|
|
|
|
|
if (!have_lock) {
|
|
|
|
LOCK(&sock->lock);
|
2018-04-17 08:29:14 -07:00
|
|
|
have_lock = true;
|
2001-01-25 22:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enqueue the request. If the socket was previously not being
|
|
|
|
* watched, poke the watcher to start paying attention to it.
|
|
|
|
*/
|
2018-10-04 14:52:54 +00:00
|
|
|
bool do_poke = ISC_LIST_EMPTY(sock->recv_list);
|
|
|
|
ISC_LIST_ENQUEUE(sock->recv_list, dev, ev_link);
|
|
|
|
if (do_poke) {
|
2018-10-03 22:46:23 +02:00
|
|
|
select_poke(sock->manager, sock->threadid, sock->fd,
|
|
|
|
SELECT_POKE_READ);
|
2018-10-04 14:52:54 +00:00
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
socket_log(sock, NULL, EVENT,
|
2020-02-12 13:59:18 +01:00
|
|
|
"socket_recv: event %p -> task %p", dev, ntask);
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0) {
|
2001-02-12 21:43:17 +00:00
|
|
|
result = ISC_R_INPROGRESS;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
break;
|
1999-09-03 16:38:08 +00:00
|
|
|
|
|
|
|
case DOIO_EOF:
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = ISC_R_EOF;
|
|
|
|
/* fallthrough */
|
1999-09-03 16:38:08 +00:00
|
|
|
|
|
|
|
case DOIO_HARD:
|
|
|
|
case DOIO_SUCCESS:
|
2018-11-08 19:34:51 -08:00
|
|
|
if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0) {
|
2001-02-12 21:43:17 +00:00
|
|
|
send_recvdone_event(sock, &dev);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
break;
|
1999-09-03 16:38:08 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if (have_lock) {
|
2001-01-25 22:25:10 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
1999-09-03 16:38:08 +00:00
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
return (result);
|
1999-09-02 02:10:44 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_recv(isc_socket_t *sock, isc_region_t *region, unsigned int minimum,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_task_t *task, isc_taskaction_t action, void *arg) {
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_socketevent_t *dev;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_t *manager;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1998-12-18 02:28:27 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
1999-07-28 23:04:33 +00:00
|
|
|
REQUIRE(action != NULL);
|
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
manager = sock->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
2000-05-10 21:17:49 +00:00
|
|
|
INSIST(sock->bound);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
dev = allocate_socketevent(manager->mctx, sock, ISC_SOCKEVENT_RECVDONE,
|
|
|
|
action, arg);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dev == NULL) {
|
1999-09-06 04:44:40 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-02-12 21:43:17 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
return (isc_socket_recv2(sock, region, minimum, task, dev, 0));
|
2001-02-12 21:43:17 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_recv2(isc_socket_t *sock, isc_region_t *region, unsigned int minimum,
|
|
|
|
isc_task_t *task, isc_socketevent_t *event,
|
|
|
|
unsigned int flags) {
|
2001-02-12 21:43:17 +00:00
|
|
|
event->ev_sender = sock;
|
2012-01-27 01:21:41 +00:00
|
|
|
event->result = ISC_R_UNSET;
|
2001-02-12 21:43:17 +00:00
|
|
|
event->region = *region;
|
|
|
|
event->n = 0;
|
|
|
|
event->offset = 0;
|
|
|
|
event->attributes = 0;
|
1999-09-06 04:44:40 +00:00
|
|
|
|
2001-01-27 06:41:36 +00:00
|
|
|
/*
|
|
|
|
* UDP sockets are always partial read.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
2001-02-12 21:43:17 +00:00
|
|
|
event->minimum = 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
|
|
|
if (minimum == 0) {
|
2001-02-12 21:43:17 +00:00
|
|
|
event->minimum = region->length;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2001-02-12 21:43:17 +00:00
|
|
|
event->minimum = minimum;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-01-27 06:41:36 +00:00
|
|
|
}
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
return (socket_recv(sock, event, task, flags));
|
2001-01-27 06:41:36 +00:00
|
|
|
}
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
static isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
socket_send(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
|
2016-12-30 15:45:08 +11:00
|
|
|
const isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int flags) {
|
|
|
|
int io_state;
|
|
|
|
bool have_lock = false;
|
|
|
|
isc_task_t *ntask = NULL;
|
2001-02-12 21:43:17 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2001-01-27 06:41:36 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = task;
|
1999-09-06 04:44:40 +00:00
|
|
|
|
|
|
|
set_dev_address(address, sock, dev);
|
1999-12-15 20:47:38 +00:00
|
|
|
if (pktinfo != NULL) {
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
|
|
|
|
dev->pktinfo = *pktinfo;
|
2002-04-03 06:44:11 +00:00
|
|
|
|
2005-11-30 03:33:49 +00:00
|
|
|
if (!isc_sockaddr_issitelocal(&dev->address) &&
|
2020-02-13 14:44:37 -08:00
|
|
|
!isc_sockaddr_islinklocal(&dev->address))
|
|
|
|
{
|
2018-11-23 21:35:01 +01:00
|
|
|
socket_log(sock, NULL, TRACE,
|
2020-02-12 13:59:18 +01:00
|
|
|
"pktinfo structure provided, ifindex %u "
|
|
|
|
"(set to 0)",
|
2018-11-23 21:35:01 +01:00
|
|
|
pktinfo->ipi6_ifindex);
|
2002-04-03 06:44:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the pktinfo index to 0 here, to let the
|
|
|
|
* kernel decide what interface it should send on.
|
|
|
|
*/
|
|
|
|
dev->pktinfo.ipi6_ifindex = 0;
|
|
|
|
}
|
1999-12-15 20:47:38 +00:00
|
|
|
}
|
1999-09-06 04:44:40 +00:00
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
2001-01-25 22:25:10 +00:00
|
|
|
io_state = doio_send(sock, dev);
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2001-01-25 22:25:10 +00:00
|
|
|
LOCK(&sock->lock);
|
2018-04-17 08:29:14 -07:00
|
|
|
have_lock = true;
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if (ISC_LIST_EMPTY(sock->send_list)) {
|
2001-01-25 22:25:10 +00:00
|
|
|
io_state = doio_send(sock, dev);
|
2018-11-08 19:34:51 -08:00
|
|
|
} else {
|
2001-01-25 22:25:10 +00:00
|
|
|
io_state = DOIO_SOFT;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
}
|
1999-09-06 04:44:40 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
switch (io_state) {
|
1999-09-06 04:44:40 +00:00
|
|
|
case DOIO_SOFT:
|
2001-01-25 22:25:10 +00:00
|
|
|
/*
|
|
|
|
* We couldn't send all or part of the request right now, so
|
2001-03-06 01:23:03 +00:00
|
|
|
* queue it unless ISC_SOCKFLAG_NORETRY is set.
|
2001-01-25 22:25:10 +00:00
|
|
|
*/
|
2001-03-06 01:23:03 +00:00
|
|
|
if ((flags & ISC_SOCKFLAG_NORETRY) == 0) {
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2001-03-06 01:23:03 +00:00
|
|
|
if (!have_lock) {
|
|
|
|
LOCK(&sock->lock);
|
2018-04-17 08:29:14 -07:00
|
|
|
have_lock = true;
|
2001-03-06 01:23:03 +00:00
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2001-03-06 01:23:03 +00:00
|
|
|
/*
|
|
|
|
* Enqueue the request. If the socket was previously
|
|
|
|
* not being watched, poke the watcher to start
|
|
|
|
* paying attention to it.
|
|
|
|
*/
|
2018-10-04 14:52:54 +00:00
|
|
|
bool do_poke = ISC_LIST_EMPTY(sock->send_list);
|
|
|
|
ISC_LIST_ENQUEUE(sock->send_list, dev, ev_link);
|
|
|
|
if (do_poke) {
|
2018-10-03 22:46:23 +02:00
|
|
|
select_poke(sock->manager, sock->threadid,
|
2020-02-12 13:59:18 +01:00
|
|
|
sock->fd, SELECT_POKE_WRITE);
|
2018-10-04 14:52:54 +00:00
|
|
|
}
|
2018-11-23 21:35:01 +01:00
|
|
|
socket_log(sock, NULL, EVENT,
|
2020-02-12 13:59:18 +01:00
|
|
|
"socket_send: event %p -> task %p", dev,
|
|
|
|
ntask);
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0) {
|
2001-03-06 01:23:03 +00:00
|
|
|
result = ISC_R_INPROGRESS;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2001-03-06 01:23:03 +00:00
|
|
|
break;
|
|
|
|
}
|
1999-09-06 04:44:40 +00:00
|
|
|
|
2017-08-09 00:17:44 -07:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
1999-09-06 04:44:40 +00:00
|
|
|
case DOIO_HARD:
|
|
|
|
case DOIO_SUCCESS:
|
2019-07-01 15:19:53 +02:00
|
|
|
if (!have_lock) {
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
have_lock = true;
|
|
|
|
}
|
2018-11-08 19:34:51 -08:00
|
|
|
if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0) {
|
2001-02-12 21:43:17 +00:00
|
|
|
send_senddone_event(sock, &dev);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
break;
|
1999-09-06 04:44:40 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
if (have_lock) {
|
2001-01-25 22:25:10 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2001-02-12 21:43:17 +00:00
|
|
|
|
|
|
|
return (result);
|
2001-01-27 06:41:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_socket_send(isc_socket_t *sock, isc_region_t *region, isc_task_t *task,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_taskaction_t action, void *arg) {
|
2001-01-27 06:41:36 +00:00
|
|
|
/*
|
|
|
|
* REQUIRE() checking is performed in isc_socket_sendto().
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
return (isc_socket_sendto(sock, region, task, action, arg, NULL, NULL));
|
2001-01-27 06:41:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_sendto(isc_socket_t *sock, isc_region_t *region, isc_task_t *task,
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_taskaction_t action, void *arg,
|
2020-02-13 14:44:37 -08:00
|
|
|
const isc_sockaddr_t *address, struct in6_pktinfo *pktinfo) {
|
2001-01-27 06:41:36 +00:00
|
|
|
isc_socketevent_t *dev;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_t *manager;
|
2001-01-27 06:41:36 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(region != NULL);
|
|
|
|
REQUIRE(task != NULL);
|
|
|
|
REQUIRE(action != NULL);
|
|
|
|
|
|
|
|
manager = sock->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
INSIST(sock->bound);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
dev = allocate_socketevent(manager->mctx, sock, ISC_SOCKEVENT_SENDDONE,
|
|
|
|
action, arg);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dev == NULL) {
|
2001-01-27 06:41:36 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-01-27 06:41:36 +00:00
|
|
|
|
|
|
|
dev->region = *region;
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
return (socket_send(sock, dev, task, address, pktinfo, 0));
|
1999-09-06 04:44:40 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_sendto2(isc_socket_t *sock, isc_region_t *region, isc_task_t *task,
|
2020-02-12 13:59:18 +01:00
|
|
|
const isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_socketevent_t *event, unsigned int flags) {
|
2009-09-01 00:22:28 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
2020-02-12 13:59:18 +01:00
|
|
|
REQUIRE((flags & ~(ISC_SOCKFLAG_IMMEDIATE | ISC_SOCKFLAG_NORETRY)) ==
|
|
|
|
0);
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((flags & ISC_SOCKFLAG_NORETRY) != 0) {
|
2001-03-06 01:23:03 +00:00
|
|
|
REQUIRE(sock->type == isc_sockettype_udp);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-02-12 21:43:17 +00:00
|
|
|
event->ev_sender = sock;
|
2012-01-27 01:21:41 +00:00
|
|
|
event->result = ISC_R_UNSET;
|
2001-02-12 21:43:17 +00:00
|
|
|
event->region = *region;
|
|
|
|
event->n = 0;
|
|
|
|
event->offset = 0;
|
2013-03-22 12:27:54 -07:00
|
|
|
event->attributes &= ~ISC_SOCKEVENTATTR_ATTACHED;
|
2001-02-12 21:43:17 +00:00
|
|
|
|
|
|
|
return (socket_send(sock, event, task, address, pktinfo, flags));
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_socket_cleanunix(const isc_sockaddr_t *sockaddr, bool active) {
|
2018-08-07 16:46:53 +02:00
|
|
|
#ifndef _WIN32
|
2020-02-13 14:44:37 -08:00
|
|
|
int s;
|
2005-02-23 01:09:23 +00:00
|
|
|
struct stat sb;
|
2020-02-13 14:44:37 -08:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2005-02-23 01:09:23 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sockaddr->type.sa.sa_family != AF_UNIX) {
|
2005-02-23 01:09:23 +00:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2005-02-23 01:09:23 +00:00
|
|
|
|
|
|
|
#ifndef S_ISSOCK
|
|
|
|
#if defined(S_IFMT) && defined(S_IFSOCK)
|
2020-02-12 13:59:18 +01:00
|
|
|
#define S_ISSOCK(mode) ((mode & S_IFMT) == S_IFSOCK)
|
2005-02-23 01:09:23 +00:00
|
|
|
#elif defined(_S_IFMT) && defined(S_IFSOCK)
|
2020-02-12 13:59:18 +01:00
|
|
|
#define S_ISSOCK(mode) ((mode & _S_IFMT) == S_IFSOCK)
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(S_IFMT) && defined(S_IFSOCK) */
|
|
|
|
#endif /* ifndef S_ISSOCK */
|
2005-02-23 01:09:23 +00:00
|
|
|
|
|
|
|
#ifndef S_ISFIFO
|
|
|
|
#if defined(S_IFMT) && defined(S_IFIFO)
|
2020-02-12 13:59:18 +01:00
|
|
|
#define S_ISFIFO(mode) ((mode & S_IFMT) == S_IFIFO)
|
2005-02-23 01:09:23 +00:00
|
|
|
#elif defined(_S_IFMT) && defined(S_IFIFO)
|
2020-02-12 13:59:18 +01:00
|
|
|
#define S_ISFIFO(mode) ((mode & _S_IFMT) == S_IFIFO)
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(S_IFMT) && defined(S_IFIFO) */
|
|
|
|
#endif /* ifndef S_ISFIFO */
|
2005-02-23 01:09:23 +00:00
|
|
|
|
|
|
|
#if !defined(S_ISFIFO) && !defined(S_ISSOCK)
|
2019-09-27 12:04:35 +02:00
|
|
|
/* cppcheck-suppress preprocessorErrorDirective */
|
2020-02-13 21:48:23 +01:00
|
|
|
#error \
|
|
|
|
You need to define S_ISFIFO and S_ISSOCK as appropriate for your platform. See <sys/stat.h>.
|
|
|
|
#endif /* if !defined(S_ISFIFO) && !defined(S_ISSOCK) */
|
2005-02-23 01:09:23 +00:00
|
|
|
|
|
|
|
#ifndef S_ISFIFO
|
|
|
|
#define S_ISFIFO(mode) 0
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef S_ISFIFO */
|
2005-02-23 01:09:23 +00:00
|
|
|
|
|
|
|
#ifndef S_ISSOCK
|
|
|
|
#define S_ISSOCK(mode) 0
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef S_ISSOCK */
|
2005-02-23 01:09:23 +00:00
|
|
|
|
2019-03-12 13:20:11 +01:00
|
|
|
if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2020-07-24 13:49:56 +10:00
|
|
|
if (active) { /* We exited cleanly last time */
|
2019-03-12 13:20:11 +01:00
|
|
|
break;
|
2020-07-24 13:49:56 +10:00
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2019-03-12 13:20:11 +01:00
|
|
|
default:
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2005-02-23 01:09:23 +00:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
2019-03-12 13:20:11 +01:00
|
|
|
ISC_LOGMODULE_SOCKET,
|
|
|
|
active ? ISC_LOG_ERROR : ISC_LOG_WARNING,
|
2005-02-23 01:09:23 +00:00
|
|
|
"isc_socket_cleanunix: stat(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-12 13:20:11 +01:00
|
|
|
} else {
|
2005-02-23 01:09:23 +00:00
|
|
|
if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
2019-03-12 13:20:11 +01:00
|
|
|
ISC_LOGMODULE_SOCKET,
|
|
|
|
active ? ISC_LOG_ERROR : ISC_LOG_WARNING,
|
2005-02-23 01:09:23 +00:00
|
|
|
"isc_socket_cleanunix: %s: not a socket",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path);
|
2005-02-23 01:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-12 13:20:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (active) {
|
2005-03-16 23:39:06 +00:00
|
|
|
if (unlink(sockaddr->type.sunix.sun_path) < 0) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2005-02-23 01:09:23 +00:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"isc_socket_cleanunix: unlink(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (s < 0) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2005-02-23 01:09:23 +00:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
|
|
|
|
"isc_socket_cleanunix: socket(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-30 15:45:08 +11:00
|
|
|
if (connect(s, (const struct sockaddr *)&sockaddr->type.sunix,
|
2020-02-13 14:44:37 -08:00
|
|
|
sizeof(sockaddr->type.sunix)) < 0)
|
|
|
|
{
|
2005-02-23 01:09:23 +00:00
|
|
|
switch (errno) {
|
|
|
|
case ECONNREFUSED:
|
|
|
|
case ECONNRESET:
|
2005-03-16 23:39:06 +00:00
|
|
|
if (unlink(sockaddr->type.sunix.sun_path) < 0) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2020-02-12 13:59:18 +01:00
|
|
|
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);
|
2005-02-23 01:09:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2005-02-23 01:09:23 +00:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
|
|
|
|
"isc_socket_cleanunix: connect(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(s);
|
2018-08-07 16:46:53 +02:00
|
|
|
#else /* ifndef _WIN32 */
|
2005-02-23 01:09:23 +00:00
|
|
|
UNUSED(sockaddr);
|
|
|
|
UNUSED(active);
|
2018-08-07 16:46:53 +02:00
|
|
|
#endif /* ifndef _WIN32 */
|
2005-02-23 01:09:23 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2018-08-06 13:00:55 +02:00
|
|
|
isc_socket_permunix(const isc_sockaddr_t *sockaddr, uint32_t perm,
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t owner, uint32_t group) {
|
2018-08-07 16:46:53 +02:00
|
|
|
#ifndef _WIN32
|
2005-02-23 01:09:23 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2020-02-13 14:44:37 -08:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
char path[sizeof(sockaddr->type.sunix.sun_path)];
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef NEED_SECURE_DIRECTORY
|
|
|
|
char *slash;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef NEED_SECURE_DIRECTORY */
|
2005-02-23 01:09:23 +00:00
|
|
|
|
|
|
|
REQUIRE(sockaddr->type.sa.sa_family == AF_UNIX);
|
2005-03-16 23:39:06 +00:00
|
|
|
INSIST(strlen(sockaddr->type.sunix.sun_path) < sizeof(path));
|
2017-09-13 00:14:37 -07:00
|
|
|
strlcpy(path, sockaddr->type.sunix.sun_path, sizeof(path));
|
2005-02-23 01:09:23 +00:00
|
|
|
|
|
|
|
#ifdef NEED_SECURE_DIRECTORY
|
|
|
|
slash = strrchr(path, '/');
|
|
|
|
if (slash != NULL) {
|
2017-09-13 00:14:37 -07:00
|
|
|
if (slash != path) {
|
2005-02-23 01:09:23 +00:00
|
|
|
*slash = '\0';
|
2017-09-13 00:14:37 -07:00
|
|
|
} else {
|
|
|
|
strlcpy(path, "/", sizeof(path));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strlcpy(path, ".", sizeof(path));
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef NEED_SECURE_DIRECTORY */
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2005-02-23 01:09:23 +00:00
|
|
|
if (chmod(path, perm) < 0) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2005-02-23 01:09:23 +00:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
2020-02-12 13:59:18 +01:00
|
|
|
"isc_socket_permunix: chmod(%s, %d): %s", path,
|
|
|
|
perm, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
result = ISC_R_FAILURE;
|
|
|
|
}
|
|
|
|
if (chown(path, owner, group) < 0) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2005-02-23 01:09:23 +00:00
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"isc_socket_permunix: chown(%s, %d, %d): %s",
|
2020-02-12 13:59:18 +01:00
|
|
|
path, owner, group, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
result = ISC_R_FAILURE;
|
|
|
|
}
|
|
|
|
return (result);
|
2018-08-07 16:46:53 +02:00
|
|
|
#else /* ifndef _WIN32 */
|
2005-02-23 01:09:23 +00:00
|
|
|
UNUSED(sockaddr);
|
|
|
|
UNUSED(perm);
|
|
|
|
UNUSED(owner);
|
|
|
|
UNUSED(group);
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
2018-08-07 16:46:53 +02:00
|
|
|
#endif /* ifndef _WIN32 */
|
2005-02-23 01:09:23 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_bind(isc_socket_t *sock, const isc_sockaddr_t *sockaddr,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_socket_options_t options) {
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
int on = 1;
|
1998-12-05 01:44:38 +00:00
|
|
|
|
2009-09-01 00:22:28 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
2000-05-10 21:17:49 +00:00
|
|
|
INSIST(!sock->bound);
|
2011-11-29 01:03:47 +00:00
|
|
|
INSIST(!sock->dupped);
|
2000-05-10 21:17:49 +00:00
|
|
|
|
2000-08-10 23:11:12 +00:00
|
|
|
if (sock->pf != sockaddr->type.sa.sa_family) {
|
|
|
|
UNLOCK(&sock->lock);
|
2000-08-15 01:43:38 +00:00
|
|
|
return (ISC_R_FAMILYMISMATCH);
|
2000-08-10 23:11:12 +00:00
|
|
|
}
|
2011-11-29 01:03:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only set SO_REUSEADDR when we want a specific port.
|
|
|
|
*/
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef AF_UNIX
|
2018-11-08 19:34:51 -08:00
|
|
|
if (sock->pf == AF_UNIX) {
|
2011-11-29 01:03:47 +00:00
|
|
|
goto bind_socket;
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef AF_UNIX */
|
2011-11-29 01:03:47 +00:00
|
|
|
if ((options & ISC_SOCKET_REUSEADDRESS) != 0 &&
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_sockaddr_getport(sockaddr) != (in_port_t)0)
|
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
|
|
|
|
sizeof(on)) < 0) {
|
2018-08-23 09:42:30 +02:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2018-11-23 21:35:01 +01:00
|
|
|
"setsockopt(%d) failed", sock->fd);
|
2018-08-23 09:42:30 +02:00
|
|
|
}
|
2019-04-29 19:29:09 +02:00
|
|
|
#if defined(__FreeBSD_kernel__) && defined(SO_REUSEPORT_LB)
|
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEPORT_LB,
|
2020-02-13 14:44:37 -08:00
|
|
|
(void *)&on, sizeof(on)) < 0)
|
|
|
|
{
|
2019-04-29 19:29:09 +02:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d) failed", sock->fd);
|
|
|
|
}
|
2019-04-29 19:28:24 +02:00
|
|
|
#elif defined(__linux__) && defined(SO_REUSEPORT)
|
2020-02-12 13:59:18 +01:00
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on,
|
|
|
|
sizeof(on)) < 0) {
|
2018-08-23 09:42:30 +02:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2018-11-23 21:35:01 +01:00
|
|
|
"setsockopt(%d) failed", sock->fd);
|
2018-08-23 09:42:30 +02:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(__FreeBSD_kernel__) && defined(SO_REUSEPORT_LB) */
|
2011-11-29 01:03:47 +00:00
|
|
|
/* Press on... */
|
|
|
|
}
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef AF_UNIX
|
2020-02-12 13:59:18 +01:00
|
|
|
bind_socket:
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef AF_UNIX */
|
2011-11-29 01:03:47 +00:00
|
|
|
if (bind(sock->fd, &sockaddr->type.sa, sockaddr->length) < 0) {
|
|
|
|
inc_stats(sock->manager->stats,
|
|
|
|
sock->statsindex[STATID_BINDFAIL]);
|
2009-01-27 22:30:00 +00:00
|
|
|
|
2011-11-29 01:03:47 +00:00
|
|
|
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:
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2011-11-29 01:03:47 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "bind: %s",
|
|
|
|
strbuf);
|
|
|
|
return (ISC_R_UNEXPECTED);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-29 01:03:47 +00:00
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
socket_log(sock, sockaddr, TRACE, "bound");
|
2000-05-10 21:17:49 +00:00
|
|
|
sock->bound = 1;
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2010-12-22 03:08:36 +00:00
|
|
|
/*
|
|
|
|
* Enable this only for specific OS versions, and only when they have repaired
|
|
|
|
* their problems with it. Until then, this is is broken and needs to be
|
2020-02-20 14:49:36 -08:00
|
|
|
* disabled by default. See RT22589 for details.
|
2010-12-22 03:08:36 +00:00
|
|
|
*/
|
|
|
|
#undef ENABLE_ACCEPTFILTER
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_filter(isc_socket_t *sock, const char *filter) {
|
2010-12-22 03:08:36 +00:00
|
|
|
#if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER)
|
2020-02-13 14:44:37 -08:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2002-05-27 00:40:23 +00:00
|
|
|
struct accept_filter_arg afa;
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER) */
|
2002-05-27 00:40:23 +00:00
|
|
|
UNUSED(sock);
|
|
|
|
UNUSED(filter);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER) */
|
2002-05-27 00:40:23 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
2010-12-22 03:08:36 +00:00
|
|
|
#if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER)
|
2002-05-27 00:40:23 +00:00
|
|
|
bzero(&afa, sizeof(afa));
|
2017-09-14 18:11:56 +10:00
|
|
|
strlcpy(afa.af_name, filter, sizeof(afa.af_name));
|
2020-02-12 13:59:18 +01:00
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa,
|
|
|
|
sizeof(afa)) == -1) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2018-11-23 21:35:01 +01:00
|
|
|
socket_log(sock, NULL, CREATION,
|
|
|
|
"setsockopt(SO_ACCEPTFILTER): %s", strbuf);
|
2002-05-27 00:40:23 +00:00
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER) */
|
2002-05-27 00:40:23 +00:00
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER) */
|
2002-05-27 00:40:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 08:34:41 +02:00
|
|
|
/*
|
|
|
|
* Try enabling TCP Fast Open for a given socket if the OS supports it.
|
|
|
|
*/
|
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
set_tcp_fastopen(isc_socket_t *sock, unsigned int backlog) {
|
2018-09-05 12:34:10 +02:00
|
|
|
#if defined(ENABLE_TCP_FASTOPEN) && defined(TCP_FASTOPEN)
|
2017-09-18 08:34:41 +02:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FreeBSD, as of versions 10.3 and 11.0, defines TCP_FASTOPEN while also
|
|
|
|
* shipping a default kernel without TFO support, so we special-case it by
|
|
|
|
* performing an additional runtime check for TFO support using sysctl to
|
|
|
|
* prevent setsockopt() errors from being logged.
|
|
|
|
*/
|
|
|
|
#if defined(__FreeBSD__) && defined(HAVE_SYSCTLBYNAME)
|
|
|
|
#define SYSCTL_TFO "net.inet.tcp.fastopen.enabled"
|
|
|
|
unsigned int enabled;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t enabledlen = sizeof(enabled);
|
|
|
|
static bool tfo_notice_logged = false;
|
2017-09-18 08:34:41 +02:00
|
|
|
|
|
|
|
if (sysctlbyname(SYSCTL_TFO, &enabled, &enabledlen, NULL, 0) < 0) {
|
|
|
|
/*
|
|
|
|
* This kernel does not support TCP Fast Open. There is
|
|
|
|
* nothing more we can do.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
} else if (enabled == 0) {
|
|
|
|
/*
|
|
|
|
* This kernel does support TCP Fast Open, but it is disabled
|
|
|
|
* by sysctl. Notify the user, but do not nag.
|
|
|
|
*/
|
|
|
|
if (!tfo_notice_logged) {
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_NOTICE,
|
|
|
|
"TCP_FASTOPEN support is disabled by "
|
|
|
|
"sysctl (" SYSCTL_TFO " = 0)");
|
2018-04-17 08:29:14 -07:00
|
|
|
tfo_notice_logged = true;
|
2017-09-18 08:34:41 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(__FreeBSD__) && defined(HAVE_SYSCTLBYNAME) */
|
2017-09-18 08:34:41 +02:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
backlog = 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef __APPLE__ */
|
2017-09-18 08:34:41 +02:00
|
|
|
backlog = backlog / 2;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (backlog == 0) {
|
2017-09-18 08:34:41 +02:00
|
|
|
backlog = 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
#endif /* ifdef __APPLE__ */
|
2020-02-12 13:59:18 +01:00
|
|
|
if (setsockopt(sock->fd, IPPROTO_TCP, TCP_FASTOPEN, (void *)&backlog,
|
2020-02-13 14:44:37 -08:00
|
|
|
sizeof(backlog)) < 0)
|
|
|
|
{
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2017-09-18 08:34:41 +02:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d, TCP_FASTOPEN) failed with %s",
|
|
|
|
sock->fd, strbuf);
|
|
|
|
/* TCP_FASTOPEN is experimental so ignore failures */
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(ENABLE_TCP_FASTOPEN) && defined(TCP_FASTOPEN) */
|
2017-09-18 08:34:41 +02:00
|
|
|
UNUSED(sock);
|
|
|
|
UNUSED(backlog);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(ENABLE_TCP_FASTOPEN) && defined(TCP_FASTOPEN) */
|
2017-09-18 08:34:41 +02:00
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
2000-05-10 21:17:49 +00:00
|
|
|
* Set up to listen on a given socket. We do this by creating an internal
|
1998-11-06 01:45:35 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_listen(isc_socket_t *sock, unsigned int backlog) {
|
2020-02-13 14:44:37 -08:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2001-08-31 05:57:58 +00:00
|
|
|
|
1998-11-10 11:37:54 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
REQUIRE(!sock->listener);
|
2000-05-10 21:17:49 +00:00
|
|
|
REQUIRE(sock->bound);
|
2005-02-23 01:09:23 +00:00
|
|
|
REQUIRE(sock->type == isc_sockettype_tcp ||
|
|
|
|
sock->type == isc_sockettype_unix);
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (backlog == 0) {
|
1998-11-11 00:43:14 +00:00
|
|
|
backlog = SOMAXCONN;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
if (listen(sock->fd, (int)backlog) < 0) {
|
1998-11-06 01:45:35 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2001-08-31 05:57:58 +00:00
|
|
|
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "listen: %s", strbuf);
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
2017-09-18 08:34:41 +02:00
|
|
|
set_tcp_fastopen(sock, backlog);
|
2016-08-12 15:31:33 +10:00
|
|
|
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->listener = 1;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1999-07-28 07:02:56 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-11-10 11:37:54 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
2008-06-25 00:09:50 +00:00
|
|
|
* This should try to do aggressive accept() XXXMLG
|
1998-11-26 00:10:33 +00:00
|
|
|
*/
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_accept(isc_socket_t *sock, isc_task_t *task, isc_taskaction_t action,
|
|
|
|
void *arg) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_newconnev_t *dev;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_t *manager;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_task_t *ntask = NULL;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_t *nsock;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
|
|
|
bool do_poke = false;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
manager = sock->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
REQUIRE(sock->listener);
|
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
/*
|
|
|
|
* Sender field is overloaded here with the task we will be sending
|
|
|
|
* this event to. Just before the actual event is delivered the
|
2000-04-17 19:22:44 +00:00
|
|
|
* actual ev_sender will be touched up to be the socket.
|
1999-06-10 00:45:30 +00:00
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
dev = (isc_socket_newconnev_t *)isc_event_allocate(
|
|
|
|
manager->mctx, task, ISC_SOCKEVENT_NEWCONN, action, arg,
|
|
|
|
sizeof(*dev));
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LINK_INIT(dev, ev_link);
|
1998-11-10 11:37:54 +00:00
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
result = allocate_socket(manager, sock->type, &nsock);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2004-04-15 01:58:25 +00:00
|
|
|
isc_event_free(ISC_EVENT_PTR(&dev));
|
1999-07-28 07:02:56 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2005-07-12 01:00:20 +00:00
|
|
|
return (result);
|
1998-11-10 11:37:54 +00:00
|
|
|
}
|
|
|
|
|
1998-11-07 02:31:04 +00:00
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Attach to socket and to task.
|
1998-11-07 02:31:04 +00:00
|
|
|
*/
|
|
|
|
isc_task_attach(task, &ntask);
|
2010-12-03 22:05:19 +00:00
|
|
|
if (isc_task_exiting(ntask)) {
|
2011-07-28 04:27:27 +00:00
|
|
|
free_socket(&nsock);
|
2010-12-03 22:05:19 +00:00
|
|
|
isc_task_detach(&ntask);
|
|
|
|
isc_event_free(ISC_EVENT_PTR(&dev));
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SHUTTINGDOWN);
|
|
|
|
}
|
2019-07-23 10:24:50 -04:00
|
|
|
isc_refcount_increment0(&nsock->references);
|
2009-01-27 22:30:00 +00:00
|
|
|
nsock->statsindex = sock->statsindex;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = ntask;
|
2021-04-19 11:12:58 +02:00
|
|
|
dev->newsocket = nsock;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Poke watcher here. We still have the socket locked, so there
|
1998-11-10 01:56:44 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2018-10-04 14:52:54 +00:00
|
|
|
do_poke = ISC_LIST_EMPTY(sock->accept_list);
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LIST_ENQUEUE(sock->accept_list, dev, ev_link);
|
2018-10-04 14:52:54 +00:00
|
|
|
if (do_poke) {
|
2018-10-03 22:46:23 +02:00
|
|
|
select_poke(manager, sock->threadid, sock->fd,
|
|
|
|
SELECT_POKE_ACCEPT);
|
2018-10-04 14:52:54 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_connect(isc_socket_t *sock, const isc_sockaddr_t *addr,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_task_t *task, isc_taskaction_t action, void *arg) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_connev_t *dev;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_task_t *ntask = NULL;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_t *manager;
|
2020-02-13 14:44:37 -08:00
|
|
|
int cc;
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
1999-07-28 23:04:33 +00:00
|
|
|
REQUIRE(addr != NULL);
|
|
|
|
REQUIRE(task != NULL);
|
|
|
|
REQUIRE(action != NULL);
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
manager = sock->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
1998-12-01 17:58:34 +00:00
|
|
|
REQUIRE(addr != NULL);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (isc_sockaddr_ismulticast(addr)) {
|
2000-06-15 18:25:49 +00:00
|
|
|
return (ISC_R_MULTICAST);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-06-15 18:25:49 +00:00
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
dev = (isc_socket_connev_t *)isc_event_allocate(
|
|
|
|
manager->mctx, sock, ISC_SOCKEVENT_CONNECT, action, arg,
|
|
|
|
sizeof(*dev));
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LINK_INIT(dev, ev_link);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2015-01-20 17:22:31 -08:00
|
|
|
if (sock->connecting) {
|
|
|
|
INSIST(isc_sockaddr_equal(&sock->peer_address, addr));
|
|
|
|
goto queue;
|
|
|
|
}
|
|
|
|
|
2015-02-18 23:32:31 +11:00
|
|
|
if (sock->connected) {
|
|
|
|
INSIST(isc_sockaddr_equal(&sock->peer_address, addr));
|
|
|
|
dev->result = ISC_R_SUCCESS;
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendto(task, ISC_EVENT_PTR(&dev), sock->threadid);
|
2015-02-18 23:32:31 +11:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
|
|
|
* Try to do the connect right away, as there can be only one
|
|
|
|
* outstanding, and it might happen to complete.
|
|
|
|
*/
|
2007-01-03 20:57:07 +00:00
|
|
|
sock->peer_address = *addr;
|
1999-07-07 02:05:08 +00:00
|
|
|
cc = connect(sock->fd, &addr->type.sa, addr->length);
|
1998-11-26 00:10:33 +00:00
|
|
|
if (cc < 0) {
|
2008-06-25 22:56:33 +00:00
|
|
|
/*
|
2018-08-21 11:09:34 +02:00
|
|
|
* The socket is nonblocking and the connection cannot be
|
|
|
|
* completed immediately. It is possible to select(2) or
|
|
|
|
* poll(2) for completion by selecting the socket for writing.
|
|
|
|
* After select(2) indicates writability, use getsockopt(2) to
|
|
|
|
* read the SO_ERROR option at level SOL_SOCKET to determine
|
|
|
|
* whether connect() completed successfully (SO_ERROR is zero)
|
|
|
|
* or unsuccessfully (SO_ERROR is one of the usual error codes
|
|
|
|
* listed here, explaining the reason for the failure).
|
2008-06-25 22:56:33 +00:00
|
|
|
*/
|
|
|
|
if (sock->type == isc_sockettype_udp && errno == EINPROGRESS) {
|
|
|
|
cc = 0;
|
|
|
|
goto success;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
if (SOFT_ERROR(errno) || errno == EINPROGRESS) {
|
1998-11-26 00:10:33 +00:00
|
|
|
goto queue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1999-05-21 07:16:13 +00:00
|
|
|
switch (errno) {
|
2020-02-12 13:59:18 +01:00
|
|
|
#define ERROR_MATCH(a, b) \
|
|
|
|
case a: \
|
|
|
|
dev->result = b; \
|
|
|
|
goto err_exit;
|
2000-09-21 21:30:27 +00:00
|
|
|
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);
|
2000-11-17 21:04:12 +00:00
|
|
|
#ifdef EHOSTDOWN
|
|
|
|
ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef EHOSTDOWN */
|
2000-09-21 21:30:27 +00:00
|
|
|
ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
|
|
|
|
ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
|
|
|
|
ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
|
|
|
|
ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
|
2019-07-08 11:32:20 +10:00
|
|
|
ERROR_MATCH(ETIMEDOUT, ISC_R_TIMEDOUT);
|
2005-03-15 01:41:28 +00:00
|
|
|
ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
|
2000-09-21 21:30:27 +00:00
|
|
|
#undef ERROR_MATCH
|
1999-05-21 07:16:13 +00:00
|
|
|
}
|
|
|
|
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->connected = 0;
|
1998-12-18 01:48:43 +00:00
|
|
|
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2009-09-07 02:08:51 +00:00
|
|
|
isc_sockaddr_format(addr, addrbuf, sizeof(addrbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "connect(%s) %d/%s",
|
|
|
|
addrbuf, errno, strbuf);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2009-01-27 22:30:00 +00:00
|
|
|
inc_stats(sock->manager->stats,
|
|
|
|
sock->statsindex[STATID_CONNECTFAIL]);
|
2004-04-15 01:58:25 +00:00
|
|
|
isc_event_free(ISC_EVENT_PTR(&dev));
|
1998-11-26 00:10:33 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
|
1999-05-21 07:16:13 +00:00
|
|
|
err_exit:
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->connected = 0;
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendto(task, ISC_EVENT_PTR(&dev), sock->threadid);
|
1999-05-21 07:16:13 +00:00
|
|
|
|
1999-07-28 07:02:56 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2009-01-27 22:30:00 +00:00
|
|
|
inc_stats(sock->manager->stats,
|
|
|
|
sock->statsindex[STATID_CONNECTFAIL]);
|
1999-05-21 07:16:13 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1998-12-18 01:48:43 +00:00
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* If connect completed, fire off the done event.
|
1998-11-26 00:10:33 +00:00
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
success:
|
1998-11-26 00:10:33 +00:00
|
|
|
if (cc == 0) {
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->connected = 1;
|
2000-05-10 21:17:49 +00:00
|
|
|
sock->bound = 1;
|
1998-11-26 00:10:33 +00:00
|
|
|
dev->result = ISC_R_SUCCESS;
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendto(task, ISC_EVENT_PTR(&dev), sock->threadid);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-07-28 07:02:56 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2009-01-27 22:30:00 +00:00
|
|
|
|
|
|
|
inc_stats(sock->manager->stats,
|
|
|
|
sock->statsindex[STATID_CONNECT]);
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
queue:
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Attach to task.
|
1998-11-26 00:10:33 +00:00
|
|
|
*/
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = ntask;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Poke watcher here. We still have the socket locked, so there
|
1998-11-26 00:10:33 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2018-10-04 14:52:54 +00:00
|
|
|
bool do_poke = ISC_LIST_EMPTY(sock->connect_list);
|
|
|
|
ISC_LIST_ENQUEUE(sock->connect_list, dev, ev_link);
|
|
|
|
if (do_poke && !sock->connecting) {
|
|
|
|
sock->connecting = 1;
|
2018-10-03 22:46:23 +02:00
|
|
|
select_poke(manager, sock->threadid, sock->fd,
|
|
|
|
SELECT_POKE_CONNECT);
|
2018-10-04 14:52:54 +00:00
|
|
|
}
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when a socket with a pending connect() finishes.
|
|
|
|
*/
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
internal_connect(isc_socket_t *sock) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_connev_t *dev;
|
2020-02-13 14:44:37 -08:00
|
|
|
int cc;
|
|
|
|
isc_result_t result;
|
|
|
|
socklen_t optlen;
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_SOCKET(sock));
|
2020-02-05 12:35:54 +01:00
|
|
|
REQUIRE(sock->fd >= 0);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
/*
|
2015-01-20 17:22:31 -08:00
|
|
|
* Get the first item off the connect list.
|
|
|
|
* If it is empty, unlock the socket and return.
|
1999-07-28 01:07:21 +00:00
|
|
|
*/
|
2015-01-20 17:22:31 -08:00
|
|
|
dev = ISC_LIST_HEAD(sock->connect_list);
|
1999-07-28 01:07:21 +00:00
|
|
|
if (dev == NULL) {
|
2000-01-28 01:31:21 +00:00
|
|
|
INSIST(!sock->connecting);
|
2018-10-04 14:52:54 +00:00
|
|
|
goto finish;
|
1999-07-28 01:07:21 +00:00
|
|
|
}
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2000-01-28 01:31:21 +00:00
|
|
|
INSIST(sock->connecting);
|
|
|
|
sock->connecting = 0;
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
|
|
|
* Get any possible error status here.
|
|
|
|
*/
|
|
|
|
optlen = sizeof(cc);
|
2020-02-12 13:59:18 +01:00
|
|
|
if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, (void *)&cc,
|
2020-02-13 14:44:37 -08:00
|
|
|
(void *)&optlen) != 0)
|
|
|
|
{
|
1998-11-26 00:10:33 +00:00
|
|
|
cc = errno;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
1998-11-26 00:10:33 +00:00
|
|
|
errno = cc;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
if (errno != 0) {
|
|
|
|
/*
|
|
|
|
* If the error is EAGAIN, just re-select on this
|
|
|
|
* fd and pretend nothing strange happened.
|
|
|
|
*/
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno) || errno == EINPROGRESS) {
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->connecting = 1;
|
1998-12-16 02:05:38 +00:00
|
|
|
return;
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
2009-01-27 22:30:00 +00:00
|
|
|
inc_stats(sock->manager->stats,
|
|
|
|
sock->statsindex[STATID_CONNECTFAIL]);
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
|
|
|
* Translate other errors into ISC_R_* flavors.
|
|
|
|
*/
|
|
|
|
switch (errno) {
|
2020-02-12 13:59:18 +01:00
|
|
|
#define ERROR_MATCH(a, b) \
|
|
|
|
case a: \
|
|
|
|
result = b; \
|
|
|
|
break;
|
2000-09-21 21:30:27 +00:00
|
|
|
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);
|
2000-11-17 21:04:12 +00:00
|
|
|
#ifdef EHOSTDOWN
|
|
|
|
ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef EHOSTDOWN */
|
2000-09-21 21:30:27 +00:00
|
|
|
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);
|
2005-05-19 02:42:42 +00:00
|
|
|
ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
|
2000-09-21 21:30:27 +00:00
|
|
|
#undef ERROR_MATCH
|
1998-11-26 00:10:33 +00:00
|
|
|
default:
|
2015-01-20 17:22:31 -08:00
|
|
|
result = ISC_R_UNEXPECTED;
|
2007-01-03 20:57:07 +00:00
|
|
|
isc_sockaddr_format(&sock->peer_address, peerbuf,
|
2005-03-15 01:41:28 +00:00
|
|
|
sizeof(peerbuf));
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
1998-11-26 00:10:33 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2005-03-15 01:41:28 +00:00
|
|
|
"internal_connect: connect(%s) %s",
|
|
|
|
peerbuf, strbuf);
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
2000-07-10 19:40:06 +00:00
|
|
|
} else {
|
2009-01-27 22:30:00 +00:00
|
|
|
inc_stats(sock->manager->stats,
|
|
|
|
sock->statsindex[STATID_CONNECT]);
|
2015-01-20 17:22:31 -08:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-07-10 19:40:06 +00:00
|
|
|
sock->connected = 1;
|
|
|
|
sock->bound = 1;
|
|
|
|
}
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2015-01-20 17:22:31 -08:00
|
|
|
do {
|
|
|
|
dev->result = result;
|
|
|
|
send_connectdone_event(sock, &dev);
|
|
|
|
dev = ISC_LIST_HEAD(sock->connect_list);
|
|
|
|
} while (dev != NULL);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
finish:
|
2018-10-04 14:52:54 +00:00
|
|
|
unwatch_fd(&sock->manager->threads[sock->threadid], sock->fd,
|
|
|
|
SELECT_POKE_CONNECT);
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
1998-12-01 23:59:39 +00:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2000-05-11 05:43:53 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(addressp != NULL);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
2000-05-11 05:43:53 +00:00
|
|
|
if (sock->connected) {
|
2007-01-03 20:57:07 +00:00
|
|
|
*addressp = sock->peer_address;
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-05-11 05:43:53 +00:00
|
|
|
} else {
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_NOTCONNECTED;
|
2000-05-11 05:43:53 +00:00
|
|
|
}
|
2009-09-01 23:47:45 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
return (result);
|
1998-12-01 23:59:39 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp) {
|
2020-02-13 14:44:37 -08:00
|
|
|
socklen_t len;
|
|
|
|
isc_result_t result;
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1998-12-01 23:59:39 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(addressp != NULL);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
2000-05-11 05:43:53 +00:00
|
|
|
if (!sock->bound) {
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_NOTBOUND;
|
2000-05-11 05:43:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-05-11 05:43:53 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
len = sizeof(addressp->type);
|
1999-08-31 20:57:46 +00:00
|
|
|
if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2020-02-12 13:59:18 +01:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "getsockname: %s", strbuf);
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
2000-05-11 05:43:53 +00:00
|
|
|
goto out;
|
1998-12-01 23:59:39 +00:00
|
|
|
}
|
1999-07-07 02:05:08 +00:00
|
|
|
addressp->length = (unsigned int)len;
|
1998-12-01 23:59:39 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
out:
|
1998-12-05 00:28:13 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2000-05-11 05:43:53 +00:00
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
return (result);
|
1998-12-01 23:59:39 +00:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Run through the list of events on this socket, and cancel the ones
|
|
|
|
* queued for task "task" of type "how". "how" is a bitmask.
|
|
|
|
*/
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how) {
|
1998-12-05 00:28:13 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Quick exit if there is nothing to do. Don't even bother locking
|
|
|
|
* in this case.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (how == 0) {
|
1998-12-05 00:28:13 +00:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All of these do the same thing, more or less.
|
|
|
|
* Each will:
|
1998-12-10 16:14:05 +00:00
|
|
|
* o If the internal event is marked as "posted" try to
|
1998-12-05 00:28:13 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
if (((how & ISC_SOCKCANCEL_RECV) != 0) &&
|
|
|
|
!ISC_LIST_EMPTY(sock->recv_list)) {
|
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_socketevent_t *next;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_task_t *current_task;
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->recv_list);
|
1999-01-27 04:17:05 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
while (dev != NULL) {
|
2000-04-17 19:22:44 +00:00
|
|
|
current_task = dev->ev_sender;
|
|
|
|
next = ISC_LIST_NEXT(dev, ev_link);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
if ((task == NULL) || (task == current_task)) {
|
|
|
|
dev->result = ISC_R_CANCELED;
|
|
|
|
send_recvdone_event(sock, &dev);
|
|
|
|
}
|
1999-07-28 01:07:21 +00:00
|
|
|
dev = next;
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
if (((how & ISC_SOCKCANCEL_SEND) != 0) &&
|
|
|
|
!ISC_LIST_EMPTY(sock->send_list)) {
|
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_socketevent_t *next;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_task_t *current_task;
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->send_list);
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
while (dev != NULL) {
|
2000-04-17 19:22:44 +00:00
|
|
|
current_task = dev->ev_sender;
|
|
|
|
next = ISC_LIST_NEXT(dev, ev_link);
|
1998-12-18 04:03:11 +00:00
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
if ((task == NULL) || (task == current_task)) {
|
|
|
|
dev->result = ISC_R_CANCELED;
|
|
|
|
send_senddone_event(sock, &dev);
|
|
|
|
}
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = next;
|
1998-12-18 04:03:11 +00:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
if (((how & ISC_SOCKCANCEL_ACCEPT) != 0) &&
|
|
|
|
!ISC_LIST_EMPTY(sock->accept_list)) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_newconnev_t *dev;
|
1999-06-10 00:45:30 +00:00
|
|
|
isc_socket_newconnev_t *next;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_task_t *current_task;
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->accept_list);
|
|
|
|
while (dev != NULL) {
|
2000-04-17 19:22:44 +00:00
|
|
|
current_task = dev->ev_sender;
|
|
|
|
next = ISC_LIST_NEXT(dev, ev_link);
|
1999-01-27 04:17:05 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
if ((task == NULL) || (task == current_task)) {
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LIST_UNLINK(sock->accept_list, dev,
|
|
|
|
ev_link);
|
1999-01-27 04:17:05 +00:00
|
|
|
|
2019-12-05 13:29:45 +11:00
|
|
|
isc_refcount_decrementz(
|
2020-02-12 13:59:18 +01:00
|
|
|
&NEWCONNSOCK(dev)->references);
|
2021-04-19 11:12:58 +02:00
|
|
|
free_socket((isc_socket_t **)&dev->newsocket);
|
1999-06-10 00:45:30 +00:00
|
|
|
|
|
|
|
dev->result = ISC_R_CANCELED;
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = sock;
|
2018-10-18 22:00:15 +00:00
|
|
|
isc_task_sendtoanddetach(¤t_task,
|
2019-04-18 16:51:52 +10:00
|
|
|
ISC_EVENT_PTR(&dev),
|
|
|
|
sock->threadid);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
dev = next;
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
if (((how & ISC_SOCKCANCEL_CONNECT) != 0) &&
|
2020-02-13 14:44:37 -08:00
|
|
|
!ISC_LIST_EMPTY(sock->connect_list))
|
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_socket_connev_t *dev;
|
|
|
|
isc_socket_connev_t *next;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_task_t *current_task;
|
1999-01-27 04:17:05 +00:00
|
|
|
|
2000-01-28 01:31:21 +00:00
|
|
|
INSIST(sock->connecting);
|
|
|
|
sock->connecting = 0;
|
|
|
|
|
2015-01-20 17:22:31 -08:00
|
|
|
dev = ISC_LIST_HEAD(sock->connect_list);
|
1999-01-27 04:17:05 +00:00
|
|
|
|
2015-01-20 17:22:31 -08:00
|
|
|
while (dev != NULL) {
|
|
|
|
current_task = dev->ev_sender;
|
|
|
|
next = ISC_LIST_NEXT(dev, ev_link);
|
1999-01-27 04:17:05 +00:00
|
|
|
|
2015-01-20 17:22:31 -08:00
|
|
|
if ((task == NULL) || (task == current_task)) {
|
|
|
|
dev->result = ISC_R_CANCELED;
|
|
|
|
send_connectdone_event(sock, &dev);
|
|
|
|
}
|
|
|
|
dev = next;
|
1999-01-27 04:17:05 +00:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_sockettype_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_gettype(isc_socket_t *sock) {
|
1999-06-25 01:36:52 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
return (sock->type);
|
|
|
|
}
|
2000-05-11 07:39:17 +00:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_ipv6only(isc_socket_t *sock, bool yes) {
|
2001-11-29 07:31:25 +00:00
|
|
|
#if defined(IPV6_V6ONLY)
|
|
|
|
int onoff = yes ? 1 : 0;
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(IPV6_V6ONLY) */
|
2001-11-29 07:31:25 +00:00
|
|
|
UNUSED(yes);
|
|
|
|
UNUSED(sock);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(IPV6_V6ONLY) */
|
2001-11-29 07:31:25 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
2011-11-29 01:03:47 +00:00
|
|
|
INSIST(!sock->dupped);
|
2001-11-29 07:31:25 +00:00
|
|
|
|
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
if (sock->pf == AF_INET6) {
|
2008-11-12 03:52:32 +00:00
|
|
|
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
|
2020-02-13 14:44:37 -08:00
|
|
|
(void *)&onoff, sizeof(int)) < 0)
|
|
|
|
{
|
2008-11-12 03:52:32 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2008-11-12 03:52:32 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"setsockopt(%d, IPV6_V6ONLY) failed: "
|
|
|
|
"%s",
|
2018-11-23 21:35:01 +01:00
|
|
|
sock->fd, strbuf);
|
2008-11-12 03:52:32 +00:00
|
|
|
}
|
2001-11-29 07:31:25 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IPV6_V6ONLY */
|
2001-11-29 07:31:25 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
setdscp(isc_socket_t *sock, isc_dscp_t dscp) {
|
2013-03-22 12:27:54 -07:00
|
|
|
#if defined(IP_TOS) || defined(IPV6_TCLASS)
|
2013-03-27 10:59:34 +11:00
|
|
|
int value = dscp << 2;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(IP_TOS) || defined(IPV6_TCLASS) */
|
2013-03-22 12:27:54 -07:00
|
|
|
|
2013-03-27 10:59:34 +11:00
|
|
|
sock->dscp = dscp;
|
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
#ifdef IP_TOS
|
|
|
|
if (sock->pf == AF_INET) {
|
2020-02-12 13:59:18 +01:00
|
|
|
if (setsockopt(sock->fd, IPPROTO_IP, IP_TOS, (void *)&value,
|
|
|
|
sizeof(value)) < 0) {
|
2013-03-22 12:27:54 -07:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2013-03-22 12:27:54 -07:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"setsockopt(%d, IP_TOS, %.02x) "
|
|
|
|
"failed: %s",
|
|
|
|
sock->fd, value >> 2, strbuf);
|
2013-03-22 12:27:54 -07:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IP_TOS */
|
2013-03-22 12:27:54 -07:00
|
|
|
#ifdef IPV6_TCLASS
|
|
|
|
if (sock->pf == AF_INET6) {
|
|
|
|
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_TCLASS,
|
2020-02-13 14:44:37 -08:00
|
|
|
(void *)&value, sizeof(value)) < 0)
|
|
|
|
{
|
2013-03-22 12:27:54 -07:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2013-03-22 12:27:54 -07:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"setsockopt(%d, IPV6_TCLASS, %.02x) "
|
|
|
|
"failed: %s",
|
2018-11-23 21:35:01 +01:00
|
|
|
sock->fd, dscp >> 2, strbuf);
|
2013-03-22 12:27:54 -07:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef IPV6_TCLASS */
|
2013-03-22 12:27:54 -07:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_dscp(isc_socket_t *sock, isc_dscp_t dscp) {
|
2013-03-22 12:27:54 -07:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(dscp < 0x40);
|
|
|
|
|
|
|
|
#if !defined(IP_TOS) && !defined(IPV6_TCLASS)
|
|
|
|
UNUSED(dscp);
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if !defined(IP_TOS) && !defined(IPV6_TCLASS) */
|
|
|
|
if (dscp < 0) {
|
2013-03-22 12:27:54 -07:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2013-03-22 12:27:54 -07:00
|
|
|
|
2014-07-18 11:40:44 +10:00
|
|
|
/* The DSCP value must not be changed once it has been set. */
|
2020-02-13 21:48:23 +01:00
|
|
|
if (isc_dscp_check_value != -1) {
|
2013-03-22 12:27:54 -07:00
|
|
|
INSIST(dscp == isc_dscp_check_value);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
#endif /* if !defined(IP_TOS) && !defined(IPV6_TCLASS) */
|
2013-03-22 12:27:54 -07:00
|
|
|
|
|
|
|
#ifdef notyet
|
|
|
|
REQUIRE(!sock->dupped);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef notyet */
|
2013-03-22 12:27:54 -07:00
|
|
|
|
|
|
|
setdscp(sock, dscp);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_socketevent_t *
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_socket_socketevent(isc_mem_t *mctx, void *sender, isc_eventtype_t eventtype,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_taskaction_t action, void *arg) {
|
2013-03-22 12:27:54 -07:00
|
|
|
return (allocate_socketevent(mctx, sender, eventtype, action, arg));
|
|
|
|
}
|
|
|
|
|
2007-02-13 02:49:08 +00:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_setname(isc_socket_t *sock, const char *name, void *tag) {
|
2007-02-13 02:49:08 +00:00
|
|
|
/*
|
2015-02-27 10:55:55 +11:00
|
|
|
* Name 'sock'.
|
2007-02-13 02:49:08 +00:00
|
|
|
*/
|
|
|
|
|
2015-02-27 10:55:55 +11:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2015-02-27 10:55:55 +11:00
|
|
|
LOCK(&sock->lock);
|
2017-09-14 18:11:56 +10:00
|
|
|
strlcpy(sock->name, name, sizeof(sock->name));
|
2015-02-27 10:55:55 +11:00
|
|
|
sock->tag = tag;
|
|
|
|
UNLOCK(&sock->lock);
|
2007-02-13 02:49:08 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
const char *
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_getname(isc_socket_t *sock) {
|
2015-02-27 10:55:55 +11:00
|
|
|
return (sock->name);
|
2007-02-13 02:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_gettag(isc_socket_t *sock) {
|
2015-02-27 10:55:55 +11:00
|
|
|
return (sock->tag);
|
2007-02-13 02:49:08 +00:00
|
|
|
}
|
2009-09-01 00:22:28 +00:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
int
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_getfd(isc_socket_t *sock) {
|
2020-02-12 13:59:18 +01:00
|
|
|
return ((short)sock->fd);
|
2011-07-28 04:04:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_once_t hasreuseport_once = ISC_ONCE_INIT;
|
2020-02-13 14:44:37 -08:00
|
|
|
static bool hasreuseport = false;
|
2018-08-23 09:42:30 +02:00
|
|
|
|
|
|
|
static void
|
2018-08-07 16:46:53 +02:00
|
|
|
init_hasreuseport(void) {
|
2018-10-26 14:41:48 +02:00
|
|
|
/*
|
|
|
|
* SO_REUSEPORT works very differently on *BSD and on Linux (because why not).
|
|
|
|
* We only want to use it on Linux, if it's available. On BSD we want to dup()
|
|
|
|
* sockets instead of re-binding them.
|
|
|
|
*/
|
2019-04-29 19:29:09 +02:00
|
|
|
#if (defined(SO_REUSEPORT) && defined(__linux__)) || \
|
2020-02-12 13:59:18 +01:00
|
|
|
(defined(SO_REUSEPORT_LB) && defined(__FreeBSD_kernel__))
|
2018-08-23 09:42:30 +02:00
|
|
|
int sock, yes = 1;
|
|
|
|
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sock < 0) {
|
2018-11-19 12:27:56 +11:00
|
|
|
sock = socket(AF_INET6, SOCK_DGRAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&yes,
|
|
|
|
sizeof(yes)) < 0) {
|
2018-08-23 09:42:30 +02:00
|
|
|
close(sock);
|
|
|
|
return;
|
2019-04-29 19:29:09 +02:00
|
|
|
#if defined(__FreeBSD_kernel__)
|
2020-02-12 13:59:18 +01:00
|
|
|
} else if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT_LB, (void *)&yes,
|
|
|
|
sizeof(yes)) < 0)
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(__FreeBSD_kernel__) */
|
2020-02-12 13:59:18 +01:00
|
|
|
} else if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void *)&yes,
|
|
|
|
sizeof(yes)) < 0)
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(__FreeBSD_kernel__) */
|
2018-11-08 19:34:51 -08:00
|
|
|
{
|
2018-08-23 09:42:30 +02:00
|
|
|
close(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hasreuseport = true;
|
2018-11-19 12:27:56 +11:00
|
|
|
close(sock);
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if (defined(SO_REUSEPORT) && defined(__linux__)) || \
|
|
|
|
* (defined(SO_REUSEPORT_LB) && defined(__FreeBSD_kernel__)) */
|
2018-08-23 09:42:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-08-07 16:46:53 +02:00
|
|
|
isc_socket_hasreuseport(void) {
|
2020-02-12 13:59:18 +01:00
|
|
|
RUNTIME_CHECK(isc_once_do(&hasreuseport_once, init_hasreuseport) ==
|
|
|
|
ISC_R_SUCCESS);
|
2018-08-23 09:42:30 +02:00
|
|
|
return (hasreuseport);
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:56:42 +01:00
|
|
|
#if defined(HAVE_LIBXML2) || defined(HAVE_JSON_C)
|
2007-02-13 02:49:08 +00:00
|
|
|
static const char *
|
2020-02-13 14:44:37 -08:00
|
|
|
_socktype(isc_sockettype_t type) {
|
2018-11-08 19:34:51 -08:00
|
|
|
switch (type) {
|
|
|
|
case isc_sockettype_udp:
|
2007-02-13 02:49:08 +00:00
|
|
|
return ("udp");
|
2018-11-08 19:34:51 -08:00
|
|
|
case isc_sockettype_tcp:
|
2007-02-13 02:49:08 +00:00
|
|
|
return ("tcp");
|
2018-11-08 19:34:51 -08:00
|
|
|
case isc_sockettype_unix:
|
2007-02-13 02:49:08 +00:00
|
|
|
return ("unix");
|
2018-11-08 19:34:51 -08:00
|
|
|
default:
|
2007-02-13 02:49:08 +00:00
|
|
|
return ("not-initialized");
|
2018-11-08 19:34:51 -08:00
|
|
|
}
|
2007-02-13 02:49:08 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(HAVE_LIBXML2) || defined(HAVE_JSON_C) */
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2013-03-13 14:24:50 -07:00
|
|
|
#ifdef HAVE_LIBXML2
|
2020-02-12 13:59:18 +01:00
|
|
|
#define TRY0(a) \
|
|
|
|
do { \
|
|
|
|
xmlrc = (a); \
|
|
|
|
if (xmlrc < 0) \
|
|
|
|
goto error; \
|
|
|
|
} while (0)
|
2013-04-10 13:49:57 -07:00
|
|
|
int
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_renderxml(isc_socketmgr_t *mgr, void *writer0) {
|
|
|
|
isc_socket_t *sock = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
|
|
|
isc_sockaddr_t addr;
|
|
|
|
socklen_t len;
|
|
|
|
int xmlrc;
|
|
|
|
xmlTextWriterPtr writer = (xmlTextWriterPtr)writer0;
|
2007-02-13 02:49:08 +00:00
|
|
|
|
|
|
|
LOCK(&mgr->lock);
|
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "sockets"));
|
2007-02-13 02:49:08 +00:00
|
|
|
sock = ISC_LIST_HEAD(mgr->socklist);
|
|
|
|
while (sock != NULL) {
|
|
|
|
LOCK(&sock->lock);
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "socket"));
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "id"));
|
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%p", sock));
|
|
|
|
TRY0(xmlTextWriterEndElement(writer));
|
2007-02-13 02:49:08 +00:00
|
|
|
|
|
|
|
if (sock->name[0] != 0) {
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer,
|
|
|
|
ISC_XMLCHAR "name"));
|
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%s",
|
2012-10-31 23:45:49 +00:00
|
|
|
sock->name));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* name */
|
2007-02-13 02:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 12:05:39 -08:00
|
|
|
TRY0(xmlTextWriterStartElement(writer,
|
|
|
|
ISC_XMLCHAR "references"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteFormatString(
|
|
|
|
writer, "%d",
|
|
|
|
(int)isc_refcount_current(&sock->references)));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer));
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteElement(
|
|
|
|
writer, ISC_XMLCHAR "type",
|
|
|
|
ISC_XMLCHAR _socktype(sock->type)));
|
2007-02-13 02:49:08 +00:00
|
|
|
|
|
|
|
if (sock->connected) {
|
|
|
|
isc_sockaddr_format(&sock->peer_address, peerbuf,
|
|
|
|
sizeof(peerbuf));
|
2020-02-17 12:05:39 -08:00
|
|
|
TRY0(xmlTextWriterWriteElement(
|
|
|
|
writer, ISC_XMLCHAR "peer-address",
|
|
|
|
ISC_XMLCHAR peerbuf));
|
2007-02-13 02:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len = sizeof(addr);
|
|
|
|
if (getsockname(sock->fd, &addr.type.sa, (void *)&len) == 0) {
|
|
|
|
isc_sockaddr_format(&addr, peerbuf, sizeof(peerbuf));
|
2020-02-17 12:05:39 -08:00
|
|
|
TRY0(xmlTextWriterWriteElement(
|
|
|
|
writer, ISC_XMLCHAR "local-address",
|
|
|
|
ISC_XMLCHAR peerbuf));
|
2007-02-13 02:49:08 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "states"));
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sock->listener) {
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterWriteElement(writer,
|
|
|
|
ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "listener"));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (sock->connected) {
|
2020-02-17 12:05:39 -08:00
|
|
|
TRY0(xmlTextWriterWriteElement(
|
|
|
|
writer, ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "connected"));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (sock->connecting) {
|
2020-02-17 12:05:39 -08:00
|
|
|
TRY0(xmlTextWriterWriteElement(
|
|
|
|
writer, ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "connecting"));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (sock->bound) {
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterWriteElement(writer,
|
|
|
|
ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "bound"));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* states */
|
2007-02-13 02:49:08 +00:00
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* socket */
|
2007-02-13 02:49:08 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
sock = ISC_LIST_NEXT(sock, link);
|
|
|
|
}
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* sockets */
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
error:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sock != NULL) {
|
2012-11-01 10:22:11 +11:00
|
|
|
UNLOCK(&sock->lock);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2007-02-13 02:49:08 +00:00
|
|
|
|
|
|
|
UNLOCK(&mgr->lock);
|
2012-11-01 10:22:11 +11:00
|
|
|
|
|
|
|
return (xmlrc);
|
2007-02-13 02:49:08 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_LIBXML2 */
|
2013-03-13 14:24:50 -07:00
|
|
|
|
2019-02-06 11:56:42 +01:00
|
|
|
#ifdef HAVE_JSON_C
|
2020-02-12 13:59:18 +01:00
|
|
|
#define CHECKMEM(m) \
|
|
|
|
do { \
|
|
|
|
if (m == NULL) { \
|
|
|
|
result = ISC_R_NOMEMORY; \
|
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2013-03-13 14:24:50 -07:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socketmgr_renderjson(isc_socketmgr_t *mgr, void *stats0) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_socket_t *sock = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
|
|
|
isc_sockaddr_t addr;
|
|
|
|
socklen_t len;
|
|
|
|
json_object *obj, *array = json_object_new_array();
|
|
|
|
json_object *stats = (json_object *)stats0;
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
CHECKMEM(array);
|
|
|
|
|
|
|
|
LOCK(&mgr->lock);
|
|
|
|
|
|
|
|
sock = ISC_LIST_HEAD(mgr->socklist);
|
|
|
|
while (sock != NULL) {
|
|
|
|
json_object *states, *entry = json_object_new_object();
|
2020-02-13 14:44:37 -08:00
|
|
|
char buf[255];
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
CHECKMEM(entry);
|
|
|
|
json_object_array_add(array, entry);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
2017-10-03 14:54:19 +11:00
|
|
|
snprintf(buf, sizeof(buf), "%p", sock);
|
2013-03-13 14:24:50 -07:00
|
|
|
obj = json_object_new_string(buf);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(entry, "id", obj);
|
|
|
|
|
|
|
|
if (sock->name[0] != 0) {
|
|
|
|
obj = json_object_new_string(sock->name);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(entry, "name", obj);
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:34:51 -08:00
|
|
|
obj = json_object_new_int(
|
2020-02-12 13:59:18 +01:00
|
|
|
(int)isc_refcount_current(&sock->references));
|
2013-03-13 14:24:50 -07:00
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(entry, "references", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_string(_socktype(sock->type));
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(entry, "type", obj);
|
|
|
|
|
|
|
|
if (sock->connected) {
|
|
|
|
isc_sockaddr_format(&sock->peer_address, peerbuf,
|
|
|
|
sizeof(peerbuf));
|
|
|
|
obj = json_object_new_string(peerbuf);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(entry, "peer-address", obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = sizeof(addr);
|
|
|
|
if (getsockname(sock->fd, &addr.type.sa, (void *)&len) == 0) {
|
|
|
|
isc_sockaddr_format(&addr, peerbuf, sizeof(peerbuf));
|
|
|
|
obj = json_object_new_string(peerbuf);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(entry, "local-address", obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
states = json_object_new_array();
|
|
|
|
CHECKMEM(states);
|
|
|
|
json_object_object_add(entry, "states", states);
|
|
|
|
|
|
|
|
if (sock->listener) {
|
|
|
|
obj = json_object_new_string("listener");
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_array_add(states, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->connected) {
|
|
|
|
obj = json_object_new_string("connected");
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_array_add(states, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->connecting) {
|
|
|
|
obj = json_object_new_string("connecting");
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_array_add(states, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->bound) {
|
|
|
|
obj = json_object_new_string("bound");
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_array_add(states, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
sock = ISC_LIST_NEXT(sock, link);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_object_add(stats, "sockets", array);
|
|
|
|
array = NULL;
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
error:
|
2020-02-13 21:48:23 +01:00
|
|
|
if (array != NULL) {
|
2013-03-13 14:24:50 -07:00
|
|
|
json_object_put(array);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2013-03-13 14:24:50 -07:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sock != NULL) {
|
2013-03-13 14:24:50 -07:00
|
|
|
UNLOCK(&sock->lock);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
UNLOCK(&mgr->lock);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
2019-02-06 11:56:42 +01:00
|
|
|
#endif /* HAVE_JSON_C */
|