/contrib/bind9/lib/isc/unix/socket.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 5944 lines · 4372 code · 781 blank · 791 comment · 886 complexity · 2bc52ee0f9ebc7e39a20f5b2ce900a03 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1998-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <sys/param.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <sys/stat.h>
  24. #include <sys/time.h>
  25. #include <sys/uio.h>
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <stddef.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <isc/buffer.h>
  33. #include <isc/bufferlist.h>
  34. #include <isc/condition.h>
  35. #include <isc/formatcheck.h>
  36. #include <isc/list.h>
  37. #include <isc/log.h>
  38. #include <isc/mem.h>
  39. #include <isc/msgs.h>
  40. #include <isc/mutex.h>
  41. #include <isc/net.h>
  42. #include <isc/once.h>
  43. #include <isc/platform.h>
  44. #include <isc/print.h>
  45. #include <isc/region.h>
  46. #include <isc/socket.h>
  47. #include <isc/stats.h>
  48. #include <isc/strerror.h>
  49. #include <isc/task.h>
  50. #include <isc/thread.h>
  51. #include <isc/util.h>
  52. #include <isc/xml.h>
  53. #ifdef ISC_PLATFORM_HAVESYSUNH
  54. #include <sys/un.h>
  55. #endif
  56. #ifdef ISC_PLATFORM_HAVEKQUEUE
  57. #include <sys/event.h>
  58. #endif
  59. #ifdef ISC_PLATFORM_HAVEEPOLL
  60. #include <sys/epoll.h>
  61. #endif
  62. #ifdef ISC_PLATFORM_HAVEDEVPOLL
  63. #if defined(HAVE_SYS_DEVPOLL_H)
  64. #include <sys/devpoll.h>
  65. #elif defined(HAVE_DEVPOLL_H)
  66. #include <devpoll.h>
  67. #endif
  68. #endif
  69. #include "errno2result.h"
  70. /* See task.c about the following definition: */
  71. #ifdef BIND9
  72. #ifdef ISC_PLATFORM_USETHREADS
  73. #define USE_WATCHER_THREAD
  74. #else
  75. #define USE_SHARED_MANAGER
  76. #endif /* ISC_PLATFORM_USETHREADS */
  77. #endif /* BIND9 */
  78. #ifndef USE_WATCHER_THREAD
  79. #include "socket_p.h"
  80. #include "../task_p.h"
  81. #endif /* USE_WATCHER_THREAD */
  82. #if defined(SO_BSDCOMPAT) && defined(__linux__)
  83. #include <sys/utsname.h>
  84. #endif
  85. /*%
  86. * Choose the most preferable multiplex method.
  87. */
  88. #ifdef ISC_PLATFORM_HAVEKQUEUE
  89. #define USE_KQUEUE
  90. #elif defined (ISC_PLATFORM_HAVEEPOLL)
  91. #define USE_EPOLL
  92. #elif defined (ISC_PLATFORM_HAVEDEVPOLL)
  93. #define USE_DEVPOLL
  94. typedef struct {
  95. unsigned int want_read : 1,
  96. want_write : 1;
  97. } pollinfo_t;
  98. #else
  99. #define USE_SELECT
  100. #endif /* ISC_PLATFORM_HAVEKQUEUE */
  101. #ifndef USE_WATCHER_THREAD
  102. #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
  103. struct isc_socketwait {
  104. int nevents;
  105. };
  106. #elif defined (USE_SELECT)
  107. struct isc_socketwait {
  108. fd_set *readset;
  109. fd_set *writeset;
  110. int nfds;
  111. int maxfd;
  112. };
  113. #endif /* USE_KQUEUE */
  114. #endif /* !USE_WATCHER_THREAD */
  115. /*%
  116. * Maximum number of allowable open sockets. This is also the maximum
  117. * allowable socket file descriptor.
  118. *
  119. * Care should be taken before modifying this value for select():
  120. * The API standard doesn't ensure select() accept more than (the system default
  121. * of) FD_SETSIZE descriptors, and the default size should in fact be fine in
  122. * the vast majority of cases. This constant should therefore be increased only
  123. * when absolutely necessary and possible, i.e., the server is exhausting all
  124. * available file descriptors (up to FD_SETSIZE) and the select() function
  125. * and FD_xxx macros support larger values than FD_SETSIZE (which may not
  126. * always by true, but we keep using some of them to ensure as much
  127. * portability as possible). Note also that overall server performance
  128. * may be rather worsened with a larger value of this constant due to
  129. * inherent scalability problems of select().
  130. *
  131. * As a special note, this value shouldn't have to be touched if
  132. * this is a build for an authoritative only DNS server.
  133. */
  134. #ifndef ISC_SOCKET_MAXSOCKETS
  135. #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
  136. #define ISC_SOCKET_MAXSOCKETS 4096
  137. #elif defined(USE_SELECT)
  138. #define ISC_SOCKET_MAXSOCKETS FD_SETSIZE
  139. #endif /* USE_KQUEUE... */
  140. #endif /* ISC_SOCKET_MAXSOCKETS */
  141. #ifdef USE_SELECT
  142. /*%
  143. * Mac OS X needs a special definition to support larger values in select().
  144. * We always define this because a larger value can be specified run-time.
  145. */
  146. #ifdef __APPLE__
  147. #define _DARWIN_UNLIMITED_SELECT
  148. #endif /* __APPLE__ */
  149. #endif /* USE_SELECT */
  150. #ifdef ISC_SOCKET_USE_POLLWATCH
  151. /*%
  152. * If this macro is defined, enable workaround for a Solaris /dev/poll kernel
  153. * bug: DP_POLL ioctl could keep sleeping even if socket I/O is possible for
  154. * some of the specified FD. The idea is based on the observation that it's
  155. * likely for a busy server to keep receiving packets. It specifically works
  156. * as follows: the socket watcher is first initialized with the state of
  157. * "poll_idle". While it's in the idle state it keeps sleeping until a socket
  158. * event occurs. When it wakes up for a socket I/O event, it moves to the
  159. * poll_active state, and sets the poll timeout to a short period
  160. * (ISC_SOCKET_POLLWATCH_TIMEOUT msec). If timeout occurs in this state, the
  161. * watcher goes to the poll_checking state with the same timeout period.
  162. * In this state, the watcher tries to detect whether this is a break
  163. * during intermittent events or the kernel bug is triggered. If the next
  164. * polling reports an event within the short period, the previous timeout is
  165. * likely to be a kernel bug, and so the watcher goes back to the active state.
  166. * Otherwise, it moves to the idle state again.
  167. *
  168. * It's not clear whether this is a thread-related bug, but since we've only
  169. * seen this with threads, this workaround is used only when enabling threads.
  170. */
  171. typedef enum { poll_idle, poll_active, poll_checking } pollstate_t;
  172. #ifndef ISC_SOCKET_POLLWATCH_TIMEOUT
  173. #define ISC_SOCKET_POLLWATCH_TIMEOUT 10
  174. #endif /* ISC_SOCKET_POLLWATCH_TIMEOUT */
  175. #endif /* ISC_SOCKET_USE_POLLWATCH */
  176. /*%
  177. * Size of per-FD lock buckets.
  178. */
  179. #ifdef ISC_PLATFORM_USETHREADS
  180. #define FDLOCK_COUNT 1024
  181. #define FDLOCK_ID(fd) ((fd) % FDLOCK_COUNT)
  182. #else
  183. #define FDLOCK_COUNT 1
  184. #define FDLOCK_ID(fd) 0
  185. #endif /* ISC_PLATFORM_USETHREADS */
  186. /*%
  187. * Maximum number of events communicated with the kernel. There should normally
  188. * be no need for having a large number.
  189. */
  190. #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
  191. #ifndef ISC_SOCKET_MAXEVENTS
  192. #define ISC_SOCKET_MAXEVENTS 64
  193. #endif
  194. #endif
  195. /*%
  196. * Some systems define the socket length argument as an int, some as size_t,
  197. * some as socklen_t. This is here so it can be easily changed if needed.
  198. */
  199. #ifndef ISC_SOCKADDR_LEN_T
  200. #define ISC_SOCKADDR_LEN_T unsigned int
  201. #endif
  202. /*%
  203. * Define what the possible "soft" errors can be. These are non-fatal returns
  204. * of various network related functions, like recv() and so on.
  205. *
  206. * For some reason, BSDI (and perhaps others) will sometimes return <0
  207. * from recv() but will have errno==0. This is broken, but we have to
  208. * work around it here.
  209. */
  210. #define SOFT_ERROR(e) ((e) == EAGAIN || \
  211. (e) == EWOULDBLOCK || \
  212. (e) == EINTR || \
  213. (e) == 0)
  214. #define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
  215. /*!<
  216. * DLVL(90) -- Function entry/exit and other tracing.
  217. * DLVL(70) -- Socket "correctness" -- including returning of events, etc.
  218. * DLVL(60) -- Socket data send/receive
  219. * DLVL(50) -- Event tracing, including receiving/sending completion events.
  220. * DLVL(20) -- Socket creation/destruction.
  221. */
  222. #define TRACE_LEVEL 90
  223. #define CORRECTNESS_LEVEL 70
  224. #define IOEVENT_LEVEL 60
  225. #define EVENT_LEVEL 50
  226. #define CREATION_LEVEL 20
  227. #define TRACE DLVL(TRACE_LEVEL)
  228. #define CORRECTNESS DLVL(CORRECTNESS_LEVEL)
  229. #define IOEVENT DLVL(IOEVENT_LEVEL)
  230. #define EVENT DLVL(EVENT_LEVEL)
  231. #define CREATION DLVL(CREATION_LEVEL)
  232. typedef isc_event_t intev_t;
  233. #define SOCKET_MAGIC ISC_MAGIC('I', 'O', 'i', 'o')
  234. #define VALID_SOCKET(s) ISC_MAGIC_VALID(s, SOCKET_MAGIC)
  235. /*!
  236. * IPv6 control information. If the socket is an IPv6 socket we want
  237. * to collect the destination address and interface so the client can
  238. * set them on outgoing packets.
  239. */
  240. #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
  241. #ifndef USE_CMSG
  242. #define USE_CMSG 1
  243. #endif
  244. #endif
  245. /*%
  246. * NetBSD and FreeBSD can timestamp packets. XXXMLG Should we have
  247. * a setsockopt() like interface to request timestamps, and if the OS
  248. * doesn't do it for us, call gettimeofday() on every UDP receive?
  249. */
  250. #ifdef SO_TIMESTAMP
  251. #ifndef USE_CMSG
  252. #define USE_CMSG 1
  253. #endif
  254. #endif
  255. /*%
  256. * The size to raise the receive buffer to (from BIND 8).
  257. */
  258. #define RCVBUFSIZE (32*1024)
  259. /*%
  260. * The number of times a send operation is repeated if the result is EINTR.
  261. */
  262. #define NRETRIES 10
  263. typedef struct isc__socket isc__socket_t;
  264. typedef struct isc__socketmgr isc__socketmgr_t;
  265. #define NEWCONNSOCK(ev) ((isc__socket_t *)(ev)->newsocket)
  266. struct isc__socket {
  267. /* Not locked. */
  268. isc_socket_t common;
  269. isc__socketmgr_t *manager;
  270. isc_mutex_t lock;
  271. isc_sockettype_t type;
  272. const isc_statscounter_t *statsindex;
  273. /* Locked by socket lock. */
  274. ISC_LINK(isc__socket_t) link;
  275. unsigned int references;
  276. int fd;
  277. int pf;
  278. char name[16];
  279. void * tag;
  280. ISC_LIST(isc_socketevent_t) send_list;
  281. ISC_LIST(isc_socketevent_t) recv_list;
  282. ISC_LIST(isc_socket_newconnev_t) accept_list;
  283. isc_socket_connev_t *connect_ev;
  284. /*
  285. * Internal events. Posted when a descriptor is readable or
  286. * writable. These are statically allocated and never freed.
  287. * They will be set to non-purgable before use.
  288. */
  289. intev_t readable_ev;
  290. intev_t writable_ev;
  291. isc_sockaddr_t peer_address; /* remote address */
  292. unsigned int pending_recv : 1,
  293. pending_send : 1,
  294. pending_accept : 1,
  295. listener : 1, /* listener socket */
  296. connected : 1,
  297. connecting : 1, /* connect pending */
  298. bound : 1; /* bound to local addr */
  299. #ifdef ISC_NET_RECVOVERFLOW
  300. unsigned char overflow; /* used for MSG_TRUNC fake */
  301. #endif
  302. char *recvcmsgbuf;
  303. ISC_SOCKADDR_LEN_T recvcmsgbuflen;
  304. char *sendcmsgbuf;
  305. ISC_SOCKADDR_LEN_T sendcmsgbuflen;
  306. void *fdwatcharg;
  307. isc_sockfdwatch_t fdwatchcb;
  308. int fdwatchflags;
  309. isc_task_t *fdwatchtask;
  310. };
  311. #define SOCKET_MANAGER_MAGIC ISC_MAGIC('I', 'O', 'm', 'g')
  312. #define VALID_MANAGER(m) ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
  313. struct isc__socketmgr {
  314. /* Not locked. */
  315. isc_socketmgr_t common;
  316. isc_mem_t *mctx;
  317. isc_mutex_t lock;
  318. isc_mutex_t *fdlock;
  319. isc_stats_t *stats;
  320. #ifdef USE_KQUEUE
  321. int kqueue_fd;
  322. int nevents;
  323. struct kevent *events;
  324. #endif /* USE_KQUEUE */
  325. #ifdef USE_EPOLL
  326. int epoll_fd;
  327. int nevents;
  328. struct epoll_event *events;
  329. #endif /* USE_EPOLL */
  330. #ifdef USE_DEVPOLL
  331. int devpoll_fd;
  332. int nevents;
  333. struct pollfd *events;
  334. #endif /* USE_DEVPOLL */
  335. #ifdef USE_SELECT
  336. int fd_bufsize;
  337. #endif /* USE_SELECT */
  338. unsigned int maxsocks;
  339. #ifdef ISC_PLATFORM_USETHREADS
  340. int pipe_fds[2];
  341. #endif
  342. /* Locked by fdlock. */
  343. isc__socket_t **fds;
  344. int *fdstate;
  345. #ifdef USE_DEVPOLL
  346. pollinfo_t *fdpollinfo;
  347. #endif
  348. /* Locked by manager lock. */
  349. ISC_LIST(isc__socket_t) socklist;
  350. #ifdef USE_SELECT
  351. fd_set *read_fds;
  352. fd_set *read_fds_copy;
  353. fd_set *write_fds;
  354. fd_set *write_fds_copy;
  355. int maxfd;
  356. #endif /* USE_SELECT */
  357. int reserved; /* unlocked */
  358. #ifdef USE_WATCHER_THREAD
  359. isc_thread_t watcher;
  360. isc_condition_t shutdown_ok;
  361. #else /* USE_WATCHER_THREAD */
  362. unsigned int refs;
  363. #endif /* USE_WATCHER_THREAD */
  364. int maxudp;
  365. };
  366. #ifdef USE_SHARED_MANAGER
  367. static isc__socketmgr_t *socketmgr = NULL;
  368. #endif /* USE_SHARED_MANAGER */
  369. #define CLOSED 0 /* this one must be zero */
  370. #define MANAGED 1
  371. #define CLOSE_PENDING 2
  372. /*
  373. * send() and recv() iovec counts
  374. */
  375. #define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER)
  376. #ifdef ISC_NET_RECVOVERFLOW
  377. # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER + 1)
  378. #else
  379. # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
  380. #endif
  381. static void send_recvdone_event(isc__socket_t *, isc_socketevent_t **);
  382. static void send_senddone_event(isc__socket_t *, isc_socketevent_t **);
  383. static void free_socket(isc__socket_t **);
  384. static isc_result_t allocate_socket(isc__socketmgr_t *, isc_sockettype_t,
  385. isc__socket_t **);
  386. static void destroy(isc__socket_t **);
  387. static void internal_accept(isc_task_t *, isc_event_t *);
  388. static void internal_connect(isc_task_t *, isc_event_t *);
  389. static void internal_recv(isc_task_t *, isc_event_t *);
  390. static void internal_send(isc_task_t *, isc_event_t *);
  391. static void internal_fdwatch_write(isc_task_t *, isc_event_t *);
  392. static void internal_fdwatch_read(isc_task_t *, isc_event_t *);
  393. static void process_cmsg(isc__socket_t *, struct msghdr *, isc_socketevent_t *);
  394. static void build_msghdr_send(isc__socket_t *, isc_socketevent_t *,
  395. struct msghdr *, struct iovec *, size_t *);
  396. static void build_msghdr_recv(isc__socket_t *, isc_socketevent_t *,
  397. struct msghdr *, struct iovec *, size_t *);
  398. #ifdef USE_WATCHER_THREAD
  399. static isc_boolean_t process_ctlfd(isc__socketmgr_t *manager);
  400. #endif
  401. /*%
  402. * The following can be either static or public, depending on build environment.
  403. */
  404. #ifdef BIND9
  405. #define ISC_SOCKETFUNC_SCOPE
  406. #else
  407. #define ISC_SOCKETFUNC_SCOPE static
  408. #endif
  409. ISC_SOCKETFUNC_SCOPE isc_result_t
  410. isc__socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
  411. isc_socket_t **socketp);
  412. ISC_SOCKETFUNC_SCOPE void
  413. isc__socket_attach(isc_socket_t *sock, isc_socket_t **socketp);
  414. ISC_SOCKETFUNC_SCOPE void
  415. isc__socket_detach(isc_socket_t **socketp);
  416. ISC_SOCKETFUNC_SCOPE isc_result_t
  417. isc__socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp);
  418. ISC_SOCKETFUNC_SCOPE isc_result_t
  419. isc__socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
  420. unsigned int maxsocks);
  421. ISC_SOCKETFUNC_SCOPE void
  422. isc__socketmgr_destroy(isc_socketmgr_t **managerp);
  423. ISC_SOCKETFUNC_SCOPE isc_result_t
  424. isc__socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
  425. unsigned int minimum, isc_task_t *task,
  426. isc_taskaction_t action, const void *arg);
  427. ISC_SOCKETFUNC_SCOPE isc_result_t
  428. isc__socket_recv(isc_socket_t *sock, isc_region_t *region,
  429. unsigned int minimum, isc_task_t *task,
  430. isc_taskaction_t action, const void *arg);
  431. ISC_SOCKETFUNC_SCOPE isc_result_t
  432. isc__socket_recv2(isc_socket_t *sock, isc_region_t *region,
  433. unsigned int minimum, isc_task_t *task,
  434. isc_socketevent_t *event, unsigned int flags);
  435. ISC_SOCKETFUNC_SCOPE isc_result_t
  436. isc__socket_send(isc_socket_t *sock, isc_region_t *region,
  437. isc_task_t *task, isc_taskaction_t action, const void *arg);
  438. ISC_SOCKETFUNC_SCOPE isc_result_t
  439. isc__socket_sendto(isc_socket_t *sock, isc_region_t *region,
  440. isc_task_t *task, isc_taskaction_t action, const void *arg,
  441. isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
  442. ISC_SOCKETFUNC_SCOPE isc_result_t
  443. isc__socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
  444. isc_task_t *task, isc_taskaction_t action, const void *arg);
  445. ISC_SOCKETFUNC_SCOPE isc_result_t
  446. isc__socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
  447. isc_task_t *task, isc_taskaction_t action, const void *arg,
  448. isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
  449. ISC_SOCKETFUNC_SCOPE isc_result_t
  450. isc__socket_sendto2(isc_socket_t *sock, isc_region_t *region,
  451. isc_task_t *task,
  452. isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
  453. isc_socketevent_t *event, unsigned int flags);
  454. ISC_SOCKETFUNC_SCOPE void
  455. isc__socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active);
  456. ISC_SOCKETFUNC_SCOPE isc_result_t
  457. isc__socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
  458. isc_uint32_t owner, isc_uint32_t group);
  459. ISC_SOCKETFUNC_SCOPE isc_result_t
  460. isc__socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr,
  461. unsigned int options);
  462. ISC_SOCKETFUNC_SCOPE isc_result_t
  463. isc__socket_filter(isc_socket_t *sock, const char *filter);
  464. ISC_SOCKETFUNC_SCOPE isc_result_t
  465. isc__socket_listen(isc_socket_t *sock, unsigned int backlog);
  466. ISC_SOCKETFUNC_SCOPE isc_result_t
  467. isc__socket_accept(isc_socket_t *sock,
  468. isc_task_t *task, isc_taskaction_t action, const void *arg);
  469. ISC_SOCKETFUNC_SCOPE isc_result_t
  470. isc__socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr,
  471. isc_task_t *task, isc_taskaction_t action,
  472. const void *arg);
  473. ISC_SOCKETFUNC_SCOPE isc_result_t
  474. isc__socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp);
  475. ISC_SOCKETFUNC_SCOPE isc_result_t
  476. isc__socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp);
  477. ISC_SOCKETFUNC_SCOPE void
  478. isc__socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how);
  479. ISC_SOCKETFUNC_SCOPE isc_sockettype_t
  480. isc__socket_gettype(isc_socket_t *sock);
  481. ISC_SOCKETFUNC_SCOPE isc_boolean_t
  482. isc__socket_isbound(isc_socket_t *sock);
  483. ISC_SOCKETFUNC_SCOPE void
  484. isc__socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes);
  485. #if defined(HAVE_LIBXML2) && defined(BIND9)
  486. ISC_SOCKETFUNC_SCOPE void
  487. isc__socketmgr_renderxml(isc_socketmgr_t *mgr0, xmlTextWriterPtr writer);
  488. #endif
  489. ISC_SOCKETFUNC_SCOPE isc_result_t
  490. isc__socket_fdwatchcreate(isc_socketmgr_t *manager, int fd, int flags,
  491. isc_sockfdwatch_t callback, void *cbarg,
  492. isc_task_t *task, isc_socket_t **socketp);
  493. ISC_SOCKETFUNC_SCOPE isc_result_t
  494. isc__socket_fdwatchpoke(isc_socket_t *sock, int flags);
  495. static struct {
  496. isc_socketmethods_t methods;
  497. /*%
  498. * The following are defined just for avoiding unused static functions.
  499. */
  500. #ifndef BIND9
  501. void *recvv, *send, *sendv, *sendto2, *cleanunix, *permunix, *filter,
  502. *listen, *accept, *getpeername, *isbound;
  503. #endif
  504. } socketmethods = {
  505. {
  506. isc__socket_attach,
  507. isc__socket_detach,
  508. isc__socket_bind,
  509. isc__socket_sendto,
  510. isc__socket_connect,
  511. isc__socket_recv,
  512. isc__socket_cancel,
  513. isc__socket_getsockname,
  514. isc__socket_gettype,
  515. isc__socket_ipv6only,
  516. isc__socket_fdwatchpoke
  517. }
  518. #ifndef BIND9
  519. ,
  520. (void *)isc__socket_recvv, (void *)isc__socket_send,
  521. (void *)isc__socket_sendv, (void *)isc__socket_sendto2,
  522. (void *)isc__socket_cleanunix, (void *)isc__socket_permunix,
  523. (void *)isc__socket_filter, (void *)isc__socket_listen,
  524. (void *)isc__socket_accept, (void *)isc__socket_getpeername,
  525. (void *)isc__socket_isbound
  526. #endif
  527. };
  528. static isc_socketmgrmethods_t socketmgrmethods = {
  529. isc__socketmgr_destroy,
  530. isc__socket_create,
  531. isc__socket_fdwatchcreate
  532. };
  533. #define SELECT_POKE_SHUTDOWN (-1)
  534. #define SELECT_POKE_NOTHING (-2)
  535. #define SELECT_POKE_READ (-3)
  536. #define SELECT_POKE_ACCEPT (-3) /*%< Same as _READ */
  537. #define SELECT_POKE_WRITE (-4)
  538. #define SELECT_POKE_CONNECT (-4) /*%< Same as _WRITE */
  539. #define SELECT_POKE_CLOSE (-5)
  540. #define SOCK_DEAD(s) ((s)->references == 0)
  541. /*%
  542. * Shortcut index arrays to get access to statistics counters.
  543. */
  544. enum {
  545. STATID_OPEN = 0,
  546. STATID_OPENFAIL = 1,
  547. STATID_CLOSE = 2,
  548. STATID_BINDFAIL = 3,
  549. STATID_CONNECTFAIL = 4,
  550. STATID_CONNECT = 5,
  551. STATID_ACCEPTFAIL = 6,
  552. STATID_ACCEPT = 7,
  553. STATID_SENDFAIL = 8,
  554. STATID_RECVFAIL = 9
  555. };
  556. static const isc_statscounter_t upd4statsindex[] = {
  557. isc_sockstatscounter_udp4open,
  558. isc_sockstatscounter_udp4openfail,
  559. isc_sockstatscounter_udp4close,
  560. isc_sockstatscounter_udp4bindfail,
  561. isc_sockstatscounter_udp4connectfail,
  562. isc_sockstatscounter_udp4connect,
  563. -1,
  564. -1,
  565. isc_sockstatscounter_udp4sendfail,
  566. isc_sockstatscounter_udp4recvfail
  567. };
  568. static const isc_statscounter_t upd6statsindex[] = {
  569. isc_sockstatscounter_udp6open,
  570. isc_sockstatscounter_udp6openfail,
  571. isc_sockstatscounter_udp6close,
  572. isc_sockstatscounter_udp6bindfail,
  573. isc_sockstatscounter_udp6connectfail,
  574. isc_sockstatscounter_udp6connect,
  575. -1,
  576. -1,
  577. isc_sockstatscounter_udp6sendfail,
  578. isc_sockstatscounter_udp6recvfail
  579. };
  580. static const isc_statscounter_t tcp4statsindex[] = {
  581. isc_sockstatscounter_tcp4open,
  582. isc_sockstatscounter_tcp4openfail,
  583. isc_sockstatscounter_tcp4close,
  584. isc_sockstatscounter_tcp4bindfail,
  585. isc_sockstatscounter_tcp4connectfail,
  586. isc_sockstatscounter_tcp4connect,
  587. isc_sockstatscounter_tcp4acceptfail,
  588. isc_sockstatscounter_tcp4accept,
  589. isc_sockstatscounter_tcp4sendfail,
  590. isc_sockstatscounter_tcp4recvfail
  591. };
  592. static const isc_statscounter_t tcp6statsindex[] = {
  593. isc_sockstatscounter_tcp6open,
  594. isc_sockstatscounter_tcp6openfail,
  595. isc_sockstatscounter_tcp6close,
  596. isc_sockstatscounter_tcp6bindfail,
  597. isc_sockstatscounter_tcp6connectfail,
  598. isc_sockstatscounter_tcp6connect,
  599. isc_sockstatscounter_tcp6acceptfail,
  600. isc_sockstatscounter_tcp6accept,
  601. isc_sockstatscounter_tcp6sendfail,
  602. isc_sockstatscounter_tcp6recvfail
  603. };
  604. static const isc_statscounter_t unixstatsindex[] = {
  605. isc_sockstatscounter_unixopen,
  606. isc_sockstatscounter_unixopenfail,
  607. isc_sockstatscounter_unixclose,
  608. isc_sockstatscounter_unixbindfail,
  609. isc_sockstatscounter_unixconnectfail,
  610. isc_sockstatscounter_unixconnect,
  611. isc_sockstatscounter_unixacceptfail,
  612. isc_sockstatscounter_unixaccept,
  613. isc_sockstatscounter_unixsendfail,
  614. isc_sockstatscounter_unixrecvfail
  615. };
  616. static const isc_statscounter_t fdwatchstatsindex[] = {
  617. -1,
  618. -1,
  619. isc_sockstatscounter_fdwatchclose,
  620. isc_sockstatscounter_fdwatchbindfail,
  621. isc_sockstatscounter_fdwatchconnectfail,
  622. isc_sockstatscounter_fdwatchconnect,
  623. -1,
  624. -1,
  625. isc_sockstatscounter_fdwatchsendfail,
  626. isc_sockstatscounter_fdwatchrecvfail
  627. };
  628. #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL) || \
  629. defined(USE_WATCHER_THREAD)
  630. static void
  631. manager_log(isc__socketmgr_t *sockmgr,
  632. isc_logcategory_t *category, isc_logmodule_t *module, int level,
  633. const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
  634. static void
  635. manager_log(isc__socketmgr_t *sockmgr,
  636. isc_logcategory_t *category, isc_logmodule_t *module, int level,
  637. const char *fmt, ...)
  638. {
  639. char msgbuf[2048];
  640. va_list ap;
  641. if (! isc_log_wouldlog(isc_lctx, level))
  642. return;
  643. va_start(ap, fmt);
  644. vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
  645. va_end(ap);
  646. isc_log_write(isc_lctx, category, module, level,
  647. "sockmgr %p: %s", sockmgr, msgbuf);
  648. }
  649. #endif
  650. static void
  651. socket_log(isc__socket_t *sock, isc_sockaddr_t *address,
  652. isc_logcategory_t *category, isc_logmodule_t *module, int level,
  653. isc_msgcat_t *msgcat, int msgset, int message,
  654. const char *fmt, ...) ISC_FORMAT_PRINTF(9, 10);
  655. static void
  656. socket_log(isc__socket_t *sock, isc_sockaddr_t *address,
  657. isc_logcategory_t *category, isc_logmodule_t *module, int level,
  658. isc_msgcat_t *msgcat, int msgset, int message,
  659. const char *fmt, ...)
  660. {
  661. char msgbuf[2048];
  662. char peerbuf[ISC_SOCKADDR_FORMATSIZE];
  663. va_list ap;
  664. if (! isc_log_wouldlog(isc_lctx, level))
  665. return;
  666. va_start(ap, fmt);
  667. vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
  668. va_end(ap);
  669. if (address == NULL) {
  670. isc_log_iwrite(isc_lctx, category, module, level,
  671. msgcat, msgset, message,
  672. "socket %p: %s", sock, msgbuf);
  673. } else {
  674. isc_sockaddr_format(address, peerbuf, sizeof(peerbuf));
  675. isc_log_iwrite(isc_lctx, category, module, level,
  676. msgcat, msgset, message,
  677. "socket %p %s: %s", sock, peerbuf, msgbuf);
  678. }
  679. }
  680. #if defined(_AIX) && defined(ISC_NET_BSD44MSGHDR) && \
  681. defined(USE_CMSG) && defined(IPV6_RECVPKTINFO)
  682. /*
  683. * AIX has a kernel bug where IPV6_RECVPKTINFO gets cleared by
  684. * setting IPV6_V6ONLY.
  685. */
  686. static void
  687. FIX_IPV6_RECVPKTINFO(isc__socket_t *sock)
  688. {
  689. char strbuf[ISC_STRERRORSIZE];
  690. int on = 1;
  691. if (sock->pf != AF_INET6 || sock->type != isc_sockettype_udp)
  692. return;
  693. if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
  694. (void *)&on, sizeof(on)) < 0) {
  695. isc__strerror(errno, strbuf, sizeof(strbuf));
  696. UNEXPECTED_ERROR(__FILE__, __LINE__,
  697. "setsockopt(%d, IPV6_RECVPKTINFO) "
  698. "%s: %s", sock->fd,
  699. isc_msgcat_get(isc_msgcat,
  700. ISC_MSGSET_GENERAL,
  701. ISC_MSG_FAILED,
  702. "failed"),
  703. strbuf);
  704. }
  705. }
  706. #else
  707. #define FIX_IPV6_RECVPKTINFO(sock) (void)0
  708. #endif
  709. /*%
  710. * Increment socket-related statistics counters.
  711. */
  712. static inline void
  713. inc_stats(isc_stats_t *stats, isc_statscounter_t counterid) {
  714. REQUIRE(counterid != -1);
  715. if (stats != NULL)
  716. isc_stats_increment(stats, counterid);
  717. }
  718. static inline isc_result_t
  719. watch_fd(isc__socketmgr_t *manager, int fd, int msg) {
  720. isc_result_t result = ISC_R_SUCCESS;
  721. #ifdef USE_KQUEUE
  722. struct kevent evchange;
  723. memset(&evchange, 0, sizeof(evchange));
  724. if (msg == SELECT_POKE_READ)
  725. evchange.filter = EVFILT_READ;
  726. else
  727. evchange.filter = EVFILT_WRITE;
  728. evchange.flags = EV_ADD;
  729. evchange.ident = fd;
  730. if (kevent(manager->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0)
  731. result = isc__errno2result(errno);
  732. return (result);
  733. #elif defined(USE_EPOLL)
  734. struct epoll_event event;
  735. if (msg == SELECT_POKE_READ)
  736. event.events = EPOLLIN;
  737. else
  738. event.events = EPOLLOUT;
  739. memset(&event.data, 0, sizeof(event.data));
  740. event.data.fd = fd;
  741. if (epoll_ctl(manager->epoll_fd, EPOLL_CTL_ADD, fd, &event) == -1 &&
  742. errno != EEXIST) {
  743. result = isc__errno2result(errno);
  744. }
  745. return (result);
  746. #elif defined(USE_DEVPOLL)
  747. struct pollfd pfd;
  748. int lockid = FDLOCK_ID(fd);
  749. memset(&pfd, 0, sizeof(pfd));
  750. if (msg == SELECT_POKE_READ)
  751. pfd.events = POLLIN;
  752. else
  753. pfd.events = POLLOUT;
  754. pfd.fd = fd;
  755. pfd.revents = 0;
  756. LOCK(&manager->fdlock[lockid]);
  757. if (write(manager->devpoll_fd, &pfd, sizeof(pfd)) == -1)
  758. result = isc__errno2result(errno);
  759. else {
  760. if (msg == SELECT_POKE_READ)
  761. manager->fdpollinfo[fd].want_read = 1;
  762. else
  763. manager->fdpollinfo[fd].want_write = 1;
  764. }
  765. UNLOCK(&manager->fdlock[lockid]);
  766. return (result);
  767. #elif defined(USE_SELECT)
  768. LOCK(&manager->lock);
  769. if (msg == SELECT_POKE_READ)
  770. FD_SET(fd, manager->read_fds);
  771. if (msg == SELECT_POKE_WRITE)
  772. FD_SET(fd, manager->write_fds);
  773. UNLOCK(&manager->lock);
  774. return (result);
  775. #endif
  776. }
  777. static inline isc_result_t
  778. unwatch_fd(isc__socketmgr_t *manager, int fd, int msg) {
  779. isc_result_t result = ISC_R_SUCCESS;
  780. #ifdef USE_KQUEUE
  781. struct kevent evchange;
  782. memset(&evchange, 0, sizeof(evchange));
  783. if (msg == SELECT_POKE_READ)
  784. evchange.filter = EVFILT_READ;
  785. else
  786. evchange.filter = EVFILT_WRITE;
  787. evchange.flags = EV_DELETE;
  788. evchange.ident = fd;
  789. if (kevent(manager->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0)
  790. result = isc__errno2result(errno);
  791. return (result);
  792. #elif defined(USE_EPOLL)
  793. struct epoll_event event;
  794. if (msg == SELECT_POKE_READ)
  795. event.events = EPOLLIN;
  796. else
  797. event.events = EPOLLOUT;
  798. memset(&event.data, 0, sizeof(event.data));
  799. event.data.fd = fd;
  800. if (epoll_ctl(manager->epoll_fd, EPOLL_CTL_DEL, fd, &event) == -1 &&
  801. errno != ENOENT) {
  802. char strbuf[ISC_STRERRORSIZE];
  803. isc__strerror(errno, strbuf, sizeof(strbuf));
  804. UNEXPECTED_ERROR(__FILE__, __LINE__,
  805. "epoll_ctl(DEL), %d: %s", fd, strbuf);
  806. result = ISC_R_UNEXPECTED;
  807. }
  808. return (result);
  809. #elif defined(USE_DEVPOLL)
  810. struct pollfd pfds[2];
  811. size_t writelen = sizeof(pfds[0]);
  812. int lockid = FDLOCK_ID(fd);
  813. memset(pfds, 0, sizeof(pfds));
  814. pfds[0].events = POLLREMOVE;
  815. pfds[0].fd = fd;
  816. /*
  817. * Canceling read or write polling via /dev/poll is tricky. Since it
  818. * only provides a way of canceling per FD, we may need to re-poll the
  819. * socket for the other operation.
  820. */
  821. LOCK(&manager->fdlock[lockid]);
  822. if (msg == SELECT_POKE_READ &&
  823. manager->fdpollinfo[fd].want_write == 1) {
  824. pfds[1].events = POLLOUT;
  825. pfds[1].fd = fd;
  826. writelen += sizeof(pfds[1]);
  827. }
  828. if (msg == SELECT_POKE_WRITE &&
  829. manager->fdpollinfo[fd].want_read == 1) {
  830. pfds[1].events = POLLIN;
  831. pfds[1].fd = fd;
  832. writelen += sizeof(pfds[1]);
  833. }
  834. if (write(manager->devpoll_fd, pfds, writelen) == -1)
  835. result = isc__errno2result(errno);
  836. else {
  837. if (msg == SELECT_POKE_READ)
  838. manager->fdpollinfo[fd].want_read = 0;
  839. else
  840. manager->fdpollinfo[fd].want_write = 0;
  841. }
  842. UNLOCK(&manager->fdlock[lockid]);
  843. return (result);
  844. #elif defined(USE_SELECT)
  845. LOCK(&manager->lock);
  846. if (msg == SELECT_POKE_READ)
  847. FD_CLR(fd, manager->read_fds);
  848. else if (msg == SELECT_POKE_WRITE)
  849. FD_CLR(fd, manager->write_fds);
  850. UNLOCK(&manager->lock);
  851. return (result);
  852. #endif
  853. }
  854. static void
  855. wakeup_socket(isc__socketmgr_t *manager, int fd, int msg) {
  856. isc_result_t result;
  857. int lockid = FDLOCK_ID(fd);
  858. /*
  859. * This is a wakeup on a socket. If the socket is not in the
  860. * process of being closed, start watching it for either reads
  861. * or writes.
  862. */
  863. INSIST(fd >= 0 && fd < (int)manager->maxsocks);
  864. if (msg == SELECT_POKE_CLOSE) {
  865. /* No one should be updating fdstate, so no need to lock it */
  866. INSIST(manager->fdstate[fd] == CLOSE_PENDING);
  867. manager->fdstate[fd] = CLOSED;
  868. (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
  869. (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
  870. (void)close(fd);
  871. return;
  872. }
  873. LOCK(&manager->fdlock[lockid]);
  874. if (manager->fdstate[fd] == CLOSE_PENDING) {
  875. UNLOCK(&manager->fdlock[lockid]);
  876. /*
  877. * We accept (and ignore) any error from unwatch_fd() as we are
  878. * closing the socket, hoping it doesn't leave dangling state in
  879. * the kernel.
  880. * Note that unwatch_fd() must be called after releasing the
  881. * fdlock; otherwise it could cause deadlock due to a lock order
  882. * reversal.
  883. */
  884. (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
  885. (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
  886. return;
  887. }
  888. if (manager->fdstate[fd] != MANAGED) {
  889. UNLOCK(&manager->fdlock[lockid]);
  890. return;
  891. }
  892. UNLOCK(&manager->fdlock[lockid]);
  893. /*
  894. * Set requested bit.
  895. */
  896. result = watch_fd(manager, fd, msg);
  897. if (result != ISC_R_SUCCESS) {
  898. /*
  899. * XXXJT: what should we do? Ignoring the failure of watching
  900. * a socket will make the application dysfunctional, but there
  901. * seems to be no reasonable recovery process.
  902. */
  903. isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
  904. ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
  905. "failed to start watching FD (%d): %s",
  906. fd, isc_result_totext(result));
  907. }
  908. }
  909. #ifdef USE_WATCHER_THREAD
  910. /*
  911. * Poke the select loop when there is something for us to do.
  912. * The write is required (by POSIX) to complete. That is, we
  913. * will not get partial writes.
  914. */
  915. static void
  916. select_poke(isc__socketmgr_t *mgr, int fd, int msg) {
  917. int cc;
  918. int buf[2];
  919. char strbuf[ISC_STRERRORSIZE];
  920. buf[0] = fd;
  921. buf[1] = msg;
  922. do {
  923. cc = write(mgr->pipe_fds[1], buf, sizeof(buf));
  924. #ifdef ENOSR
  925. /*
  926. * Treat ENOSR as EAGAIN but loop slowly as it is
  927. * unlikely to clear fast.
  928. */
  929. if (cc < 0 && errno == ENOSR) {
  930. sleep(1);
  931. errno = EAGAIN;
  932. }
  933. #endif
  934. } while (cc < 0 && SOFT_ERROR(errno));
  935. if (cc < 0) {
  936. isc__strerror(errno, strbuf, sizeof(strbuf));
  937. FATAL_ERROR(__FILE__, __LINE__,
  938. isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
  939. ISC_MSG_WRITEFAILED,
  940. "write() failed "
  941. "during watcher poke: %s"),
  942. strbuf);
  943. }
  944. INSIST(cc == sizeof(buf));
  945. }
  946. /*
  947. * Read a message on the internal fd.
  948. */
  949. static void
  950. select_readmsg(isc__socketmgr_t *mgr, int *fd, int *msg) {
  951. int buf[2];
  952. int cc;
  953. char strbuf[ISC_STRERRORSIZE];
  954. cc = read(mgr->pipe_fds[0], buf, sizeof(buf));
  955. if (cc < 0) {
  956. *msg = SELECT_POKE_NOTHING;
  957. *fd = -1; /* Silence compiler. */
  958. if (SOFT_ERROR(errno))
  959. return;
  960. isc__strerror(errno, strbuf, sizeof(strbuf));
  961. FATAL_ERROR(__FILE__, __LINE__,
  962. isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
  963. ISC_MSG_READFAILED,
  964. "read() failed "
  965. "during watcher poke: %s"),
  966. strbuf);
  967. return;
  968. }
  969. INSIST(cc == sizeof(buf));
  970. *fd = buf[0];
  971. *msg = buf[1];
  972. }
  973. #else /* USE_WATCHER_THREAD */
  974. /*
  975. * Update the state of the socketmgr when something changes.
  976. */
  977. static void
  978. select_poke(isc__socketmgr_t *manager, int fd, int msg) {
  979. if (msg == SELECT_POKE_SHUTDOWN)
  980. return;
  981. else if (fd >= 0)
  982. wakeup_socket(manager, fd, msg);
  983. return;
  984. }
  985. #endif /* USE_WATCHER_THREAD */
  986. /*
  987. * Make a fd non-blocking.
  988. */
  989. static isc_result_t
  990. make_nonblock(int fd) {
  991. int ret;
  992. int flags;
  993. char strbuf[ISC_STRERRORSIZE];
  994. #ifdef USE_FIONBIO_IOCTL
  995. int on = 1;
  996. ret = ioctl(fd, FIONBIO, (char *)&on);
  997. #else
  998. flags = fcntl(fd, F_GETFL, 0);
  999. flags |= PORT_NONBLOCK;
  1000. ret = fcntl(fd, F_SETFL, flags);
  1001. #endif
  1002. if (ret == -1) {
  1003. isc__strerror(errno, strbuf, sizeof(strbuf));
  1004. UNEXPECTED_ERROR(__FILE__, __LINE__,
  1005. #ifdef USE_FIONBIO_IOCTL
  1006. "ioctl(%d, FIONBIO, &on): %s", fd,
  1007. #else
  1008. "fcntl(%d, F_SETFL, %d): %s", fd, flags,
  1009. #endif
  1010. strbuf);
  1011. return (ISC_R_UNEXPECTED);
  1012. }
  1013. return (ISC_R_SUCCESS);
  1014. }
  1015. #ifdef USE_CMSG
  1016. /*
  1017. * Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
  1018. * In order to ensure as much portability as possible, we provide wrapper
  1019. * functions of these macros.
  1020. * Note that cmsg_space() could run slow on OSes that do not have
  1021. * CMSG_SPACE.
  1022. */
  1023. static inline ISC_SOCKADDR_LEN_T
  1024. cmsg_len(ISC_SOCKADDR_LEN_T len) {
  1025. #ifdef CMSG_LEN
  1026. return (CMSG_LEN(len));
  1027. #else
  1028. ISC_SOCKADDR_LEN_T hdrlen;
  1029. /*
  1030. * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
  1031. * is correct.
  1032. */
  1033. hdrlen = (ISC_SOCKADDR_LEN_T)CMSG_DATA(((struct cmsghdr *)NULL));
  1034. return (hdrlen + len);
  1035. #endif
  1036. }
  1037. static inline ISC_SOCKADDR_LEN_T
  1038. cmsg_space(ISC_SOCKADDR_LEN_T len) {
  1039. #ifdef CMSG_SPACE
  1040. return (CMSG_SPACE(len));
  1041. #else
  1042. struct msghdr msg;
  1043. struct cmsghdr *cmsgp;
  1044. /*
  1045. * XXX: The buffer length is an ad-hoc value, but should be enough
  1046. * in a practical sense.
  1047. */
  1048. char dummybuf[sizeof(struct cmsghdr) + 1024];
  1049. memset(&msg, 0, sizeof(msg));
  1050. msg.msg_control = dummybuf;
  1051. msg.msg_controllen = sizeof(dummybuf);
  1052. cmsgp = (struct cmsghdr *)dummybuf;
  1053. cmsgp->cmsg_len = cmsg_len(len);
  1054. cmsgp = CMSG_NXTHDR(&msg, cmsgp);
  1055. if (cmsgp != NULL)
  1056. return ((char *)cmsgp - (char *)msg.msg_control);
  1057. else
  1058. return (0);
  1059. #endif
  1060. }
  1061. #endif /* USE_CMSG */
  1062. /*
  1063. * Process control messages received on a socket.
  1064. */
  1065. static void
  1066. process_cmsg(isc__socket_t *sock, struct msghdr *msg, isc_socketevent_t *dev) {
  1067. #ifdef USE_CMSG
  1068. struct cmsghdr *cmsgp;
  1069. #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
  1070. struct in6_pktinfo *pktinfop;
  1071. #endif
  1072. #ifdef SO_TIMESTAMP
  1073. struct timeval *timevalp;
  1074. #endif
  1075. #endif
  1076. /*
  1077. * sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
  1078. * msg and dev are used only when ISC_NET_BSD44MSGHDR is defined.
  1079. * They are all here, outside of the CPP tests, because it is
  1080. * more consistent with the usual ISC coding style.
  1081. */
  1082. UNUSED(sock);
  1083. UNUSED(msg);
  1084. UNUSED(dev);
  1085. #ifdef ISC_NET_BSD44MSGHDR
  1086. #ifdef MSG_TRUNC
  1087. if ((msg->msg_flags & MSG_TRUNC) == MSG_TRUNC)
  1088. dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
  1089. #endif
  1090. #ifdef MSG_CTRUNC
  1091. if ((msg->msg_flags & MSG_CTRUNC) == MSG_CTRUNC)
  1092. dev->attributes |= ISC_SOCKEVENTATTR_CTRUNC;
  1093. #endif
  1094. #ifndef USE_CMSG
  1095. return;
  1096. #else
  1097. if (msg->msg_controllen == 0U || msg->msg_control == NULL)
  1098. return;
  1099. #ifdef SO_TIMESTAMP
  1100. timevalp = NULL;
  1101. #endif
  1102. #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
  1103. pktinfop = NULL;
  1104. #endif
  1105. cmsgp = CMSG_FIRSTHDR(msg);
  1106. while (cmsgp != NULL) {
  1107. socket_log(sock, NULL, TRACE,
  1108. isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PROCESSCMSG,
  1109. "processing cmsg %p", cmsgp);
  1110. #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
  1111. if (cmsgp->cmsg_level == IPPROTO_IPV6
  1112. && cmsgp->cmsg_type == IPV6_PKTINFO) {
  1113. pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
  1114. memcpy(&dev->pktinfo, pktinfop,
  1115. sizeof(struct in6_pktinfo));
  1116. dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
  1117. socket_log(sock, NULL, TRACE,
  1118. isc_msgcat, ISC_MSGSET_SOCKET,
  1119. ISC_MSG_IFRECEIVED,
  1120. "interface received on ifindex %u",
  1121. dev->pktinfo.ipi6_ifindex);
  1122. if (IN6_IS_ADDR_MULTICAST(&pktinfop->ipi6_addr))
  1123. dev->attributes |= ISC_SOCKEVENTATTR_MULTICAST;
  1124. goto next;
  1125. }
  1126. #endif
  1127. #ifdef SO_TIMESTAMP
  1128. if (cmsgp->cmsg_level == SOL_SOCKET
  1129. && cmsgp->cmsg_type == SCM_TIMESTAMP) {
  1130. timevalp = (struct timeval *)CMSG_DATA(cmsgp);
  1131. dev->timestamp.seconds = timevalp->tv_sec;
  1132. dev->timestamp.nanoseconds = timevalp->tv_usec * 1000;
  1133. dev->attributes |= ISC_SOCKEVENTATTR_TIMESTAMP;
  1134. goto next;
  1135. }
  1136. #endif
  1137. next:
  1138. cmsgp = CMSG_NXTHDR(msg, cmsgp);
  1139. }
  1140. #endif /* USE_CMSG */
  1141. #endif /* ISC_NET_BSD44MSGHDR */
  1142. }
  1143. /*
  1144. * Construct an iov array and attach it to the msghdr passed in. This is
  1145. * the SEND constructor, which will use the used region of the buffer
  1146. * (if using a buffer list) or will use the internal region (if a single
  1147. * buffer I/O is requested).
  1148. *
  1149. * Nothing can be NULL, and the done event must list at least one buffer
  1150. * on the buffer linked list for this function to be meaningful.
  1151. *
  1152. * If write_countp != NULL, *write_countp will hold the number of bytes
  1153. * this transaction can send.
  1154. */
  1155. static void
  1156. build_msghdr_send(isc__socket_t *sock, isc_socketevent_t *dev,
  1157. struct msghdr *msg, struct iovec *iov, size_t *write_countp)
  1158. {
  1159. unsigned int iovcount;
  1160. isc_buffer_t *buffer;
  1161. isc_region_t used;
  1162. size_t write_count;
  1163. size_t skip_count;
  1164. memset(msg, 0, sizeof(*msg));
  1165. if (!sock->connected) {
  1166. msg->msg_name = (void *)&dev->address.type.sa;
  1167. msg->msg_namelen = dev->address.length;
  1168. } else {
  1169. msg->msg_name = NULL;
  1170. msg->msg_namelen = 0;
  1171. }
  1172. buffer = ISC_LIST_HEAD(dev->bufferlist);
  1173. write_count = 0;
  1174. iovcount = 0;
  1175. /*
  1176. * Single buffer I/O? Skip what we've done so far in this region.
  1177. */
  1178. if (buffer == NULL) {
  1179. write_count = dev->region.length - dev->n;
  1180. iov[0].iov_base = (void *)(dev->region.base + dev->n);
  1181. iov[0].iov_len = write_count;
  1182. iovcount = 1;
  1183. goto config;
  1184. }
  1185. /*
  1186. * Multibuffer I/O.
  1187. * Skip the data in the buffer list that we have already written.
  1188. */
  1189. skip_count = dev->n;
  1190. while (buffer != NULL) {
  1191. REQUIRE(ISC_BUFFER_VALID(buffer));
  1192. if (skip_count < isc_buffer_usedlength(buffer))
  1193. break;
  1194. skip_count -= isc_buffer_usedlength(buffer);
  1195. buffer = ISC_LIST_NEXT(buffer, link);
  1196. }
  1197. while (buffer != NULL) {
  1198. INSIST(iovcount < MAXSCATTERGATHER_SEND);
  1199. isc_buffer_usedregion(buffer, &used);
  1200. if (used.length > 0) {
  1201. iov[iovcount].iov_base = (void *)(used.base
  1202. + skip_count);
  1203. iov[iovcount].iov_len = used.length - skip_count;
  1204. write_count += (used.length - skip_count);
  1205. skip_count = 0;
  1206. iovcount++;
  1207. }
  1208. buffer = ISC_LIST_NEXT(buffer, link);
  1209. }
  1210. INSIST(skip_count == 0U);
  1211. config:
  1212. msg->msg_iov = iov;
  1213. msg->msg_iovlen = iovcount;
  1214. #ifdef ISC_NET_BSD44MSGHDR
  1215. msg->msg_control = NULL;
  1216. msg->msg_controllen = 0;
  1217. msg->msg_flags = 0;
  1218. #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
  1219. if ((sock->type == isc_sockettype_udp)
  1220. && ((dev->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0)) {
  1221. #if defined(IPV6_USE_MIN_MTU)
  1222. int use_min_mtu = 1; /* -1, 0, 1 */
  1223. #endif
  1224. struct cmsghdr *cmsgp;
  1225. struct in6_pktinfo *pktinfop;
  1226. socket_log(sock, NULL, TRACE,
  1227. isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_SENDTODATA,
  1228. "sendto pktinfo data, ifindex %u",
  1229. dev->pktinfo.ipi6_ifindex);
  1230. msg->msg_controllen = cmsg_space(sizeof(struct in6_pktinfo));
  1231. INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
  1232. msg->msg_control = (void *)sock->sendcmsgbuf;
  1233. cmsgp = (struct cmsghdr *)sock->sendcmsgbuf;
  1234. cmsgp->cmsg_level = IPPROTO_IPV6;
  1235. cmsgp->cmsg_type = IPV6_PKTINFO;
  1236. cmsgp->cmsg_len = cmsg_len(sizeof(struct in6_pktinfo));
  1237. pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
  1238. memcpy(pktinfop, &dev->pktinfo, sizeof(struct in6_pktinfo));
  1239. #if defined(IPV6_USE_MIN_MTU)
  1240. /*
  1241. * Set IPV6_USE_MIN_MTU as a per packet option as FreeBSD
  1242. * ignores setsockopt(IPV6_USE_MIN_MTU) when IPV6_PKTINFO
  1243. * is used.
  1244. */
  1245. cmsgp = (struct cmsghdr *)(sock->sendcmsgbuf +
  1246. msg->msg_controllen);
  1247. msg->msg_controllen += cmsg_space(sizeof(use_min_mtu));
  1248. INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
  1249. cmsgp->cmsg_level = IPPROTO_IPV6;
  1250. cmsgp->cmsg_type = IPV6_USE_MIN_MTU;
  1251. cmsgp->cmsg_len = cmsg_len(sizeof(use_min_mtu));
  1252. memcpy(CMSG_DATA(cmsgp), &use_min_mtu, sizeof(use_min_mtu));
  1253. #endif
  1254. }
  1255. #endif /* USE_CMSG && ISC_PLATFORM_HAVEIPV6 */
  1256. #else /* ISC_NET_BSD44MSGHDR */
  1257. msg->msg_accrights = NULL;
  1258. msg->msg_accrightslen = 0;
  1259. #endif /* ISC_NET_BSD44MSGHDR */
  1260. if (write_countp != NULL)
  1261. *write_countp = write_count;
  1262. }
  1263. /*
  1264. * Construct an iov array and attach it to the msghdr passed in. This is
  1265. * the RECV constructor, which will use the available region of the buffer
  1266. * (if using a buffer list) or will use the internal region (if a single
  1267. * buffer I/O is requested).
  1268. *
  1269. * Nothing can be NULL, and the done event must list at least one buffer
  1270. * on the buffer linked list for this function to be meaningful.
  1271. *
  1272. * If read_countp != NULL, *read_countp will hold the number of bytes
  1273. * this transaction can receive.
  1274. */
  1275. static void
  1276. build_msghdr_recv(isc__socket_t *sock, isc_socketevent_t *dev,
  1277. struct msghdr *msg, struct iovec *iov, size_t *read_countp)
  1278. {
  1279. unsigned int iovcount;
  1280. isc_buffer_t *buffer;
  1281. isc_region_t available;
  1282. size_t read_count;
  1283. memset(msg, 0, sizeof(struct msghdr));
  1284. if (sock->type == isc_sockettype_udp) {
  1285. memset(&dev->address, 0, sizeof(dev->address));
  1286. #ifdef BROKEN_RECVMSG
  1287. if (sock->pf == AF_INET) {
  1288. msg->msg_name = (void *)&dev->address.type.sin;
  1289. msg->msg_namelen = sizeof(dev->address.type.sin6);
  1290. } else if (sock->pf == AF_INET6) {
  1291. msg->msg_name = (void *)&dev->address.type.sin6;
  1292. msg->msg_namelen = sizeof(dev->address.type.sin6);
  1293. #ifdef ISC_PLATFORM_HAVESYSUNH
  1294. } else if (sock->pf == AF_UNIX) {
  1295. msg->msg_name = (void *)&dev->address.type.sunix;
  1296. msg->msg_namelen = sizeof(dev->address.type.sunix);
  1297. #endif
  1298. } else {
  1299. msg->msg_name = (void *)&dev->address.type.sa;
  1300. msg->msg_namelen = sizeof(dev->address.type);
  1301. }
  1302. #else
  1303. msg->msg_name = (void *)&dev->address.type.sa;
  1304. msg->msg_namelen = sizeof(dev->address.type);
  1305. #endif
  1306. #ifdef ISC_NET_RECVOVERFLOW
  1307. /* If needed, steal one iovec for overflow detection. */
  1308. maxiov--;
  1309. #endif
  1310. } else { /* TCP */
  1311. msg->msg_name = NULL;
  1312. msg->msg_namelen = 0;
  1313. dev->address = sock->peer_address;
  1314. }
  1315. buffer = ISC_LIST_HEAD(dev->bufferlist);
  1316. read_count = 0;
  1317. /*
  1318. * Single buffer I/O? Skip what we've done so far in this region.
  1319. */
  1320. if (buffer == NULL) {
  1321. read_count = dev->region.length - dev->n;
  1322. iov[0].iov_base = (void *)(dev->region.base + dev->n);
  1323. iov[0].iov_len = read_count;
  1324. iovcount = 1;
  1325. goto config;
  1326. }
  1327. /*
  1328. * Multibuffer I/O.
  1329. * Skip empty buffers.
  1330. */
  1331. while (buffer != NULL) {
  1332. REQUIRE(ISC_BUFFER_VALID(buffer));
  1333. if (isc_buffer_availablelength(buffer) != 0)
  1334. break;
  1335. buffer = ISC_LIST_NEXT(buffer, link);
  1336. }
  1337. iovcount = 0;
  1338. while (buffer != NULL) {
  1339. INSIST(iovcount < MAXSCATTERGATHER_RECV);
  1340. isc_buffer_availableregion(buffer, &available);
  1341. if (available.length > 0) {
  1342. iov[iovcount].iov_base = (void *)(available.base);
  1343. iov[iovcount].iov_len = available.length;
  1344. read_count += available.length;
  1345. iovcount++;
  1346. }
  1347. buffer = ISC_LIST_NEXT(buffer, link);
  1348. }
  1349. config:
  1350. /*
  1351. * If needed, set up to receive that one extra byte. Note that
  1352. * we know there is at least one iov left, since we stole it
  1353. * at the top of this function.
  1354. */
  1355. #ifdef ISC_NET_RECVOVERFLOW
  1356. if (sock->type == isc_sockettype_udp) {
  1357. iov[iovcount].iov_base = (void *)(&sock->overflow);
  1358. iov[iovcount].iov_len = 1;
  1359. iovcount++;
  1360. }
  1361. #endif
  1362. msg->msg_iov = iov;
  1363. msg->msg_iovlen = iovcount;
  1364. #ifdef ISC_NET_BSD44MSGHDR
  1365. msg->msg_control = NULL;
  1366. msg->msg_controllen = 0;
  1367. msg->msg_flags = 0;
  1368. #if defined(USE_CMSG)
  1369. if (sock->type == isc_sockettype_udp) {
  1370. msg->msg_control = sock->recvcmsgbuf;
  1371. msg->msg_controllen = sock->recvcmsgbuflen;
  1372. }
  1373. #endif /* USE_CMSG */
  1374. #else /* ISC_NET_BSD44MSGHDR */
  1375. msg->msg_accrights = NULL;
  1376. msg->msg_accrightslen = 0;
  1377. #endif /* ISC_NET_BSD44MSGHDR */
  1378. if (read_countp != NULL)
  1379. *read_countp = read_count;
  1380. }
  1381. static void
  1382. set_dev_address(isc_sockaddr_t *address, isc__socket_t *sock,
  1383. isc_socketevent_t *dev)
  1384. {
  1385. if (sock->type == isc_sockettype_udp) {
  1386. if (address != NULL)
  1387. dev->address = *address;
  1388. else
  1389. dev->address = sock->peer_address;
  1390. } else if (sock->type == isc_sockettype_tcp) {
  1391. INSIST(address == NULL);
  1392. dev->address = sock->peer_address;
  1393. }
  1394. }
  1395. static void
  1396. destroy_socketevent(isc_event_t *event) {
  1397. isc_socketevent_t *ev = (isc_socketevent_t *)event;
  1398. INSIST(ISC_LIST_EMPTY(ev->bufferlist));
  1399. (ev->destroy)(event);
  1400. }
  1401. static isc_socketevent_t *
  1402. allocate_socketevent(isc__socket_t *sock, isc_eventtype_t eventtype,
  1403. isc_taskaction_t action, const void *arg)
  1404. {
  1405. isc_socketevent_t *ev;
  1406. ev = (isc_socketevent_t *)isc_event_allocate(sock->manager->mctx,
  1407. sock, eventtype,
  1408. action, arg,
  1409. sizeof(*ev));
  1410. if (ev == NULL)
  1411. return (NULL);
  1412. ev->result = ISC_R_UNSET;
  1413. ISC_LINK_INIT(ev, ev_link);
  1414. ISC_LIST_INIT(ev->bufferlist);
  1415. ev->region.base = NULL;
  1416. ev->n = 0;
  1417. ev->offset = 0;
  1418. ev->attributes = 0;
  1419. ev->destroy = ev->ev_destroy;
  1420. ev->ev_destroy = destroy_socketevent;
  1421. return (ev);
  1422. }
  1423. #if defined(ISC_SOCKET_DEBUG)
  1424. static void
  1425. dump_msg(struct msghdr *msg) {
  1426. unsigned int i;
  1427. printf("MSGHDR %p\n", msg);
  1428. printf("\tname %p, namelen %ld\n", msg->msg_name,
  1429. (long) msg->msg_namelen);
  1430. printf("\tiov %p, iovlen %ld\n", msg->msg_iov,
  1431. (long) msg->msg_iovlen);
  1432. for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
  1433. printf("\t\t%d\tbase %p, len %ld\n", i,
  1434. msg->msg_iov[i].iov_base,
  1435. (long) msg->msg_iov[i].iov_len);
  1436. #ifdef ISC_NET_BSD44MSGHDR
  1437. printf("\tcontrol %p, controllen %ld\n", msg->msg_control,
  1438. (long) msg->msg_controllen);
  1439. #endif
  1440. }
  1441. #endif
  1442. #define DOIO_SUCCESS 0 /* i/o ok, event sent */
  1443. #define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */
  1444. #define DOIO_HARD 2 /* i/o error, event sent */
  1445. #define DOIO_EOF 3 /* EOF, no event sent */
  1446. static int
  1447. doio_recv(isc__socket_t *sock, isc_socketevent_t *dev) {
  1448. int cc;
  1449. struct iovec iov[MAXSCATTERGATHER_RECV];
  1450. size_t read_count;
  1451. size_t actual_count;
  1452. struct msghdr msghdr;
  1453. isc_buffer_t *buffer;
  1454. int recv_errno;
  1455. char strbuf[ISC_STRERRORSIZE];
  1456. build_msghdr_recv(sock, dev, &msghdr, iov, &read_count);
  1457. #if defined(ISC_SOCKET_DEBUG)
  1458. dump_msg(&msghdr);
  1459. #endif
  1460. cc = recvmsg(sock->fd, &msghdr, 0);
  1461. recv_errno = errno;
  1462. #if defined(ISC_SOCKET_DEBUG)
  1463. dump_msg(&msghdr);
  1464. #endif
  1465. if (cc < 0) {
  1466. if (SOFT_ERROR(recv_errno))
  1467. return (DOIO_SOFT);
  1468. if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
  1469. isc__strerror(recv_errno, strbuf, sizeof(strbuf));
  1470. socket_log(sock, NULL, IOEVENT,
  1471. isc_msgcat, ISC_MSGSET_SOCKET,
  1472. ISC_MSG_DOIORECV,
  1473. "doio_recv: recvmsg(%d) %d bytes, err %d/%s",
  1474. sock->fd, cc, recv_errno, strbuf);
  1475. }
  1476. #define SOFT_OR_HARD(_system, _isc) \
  1477. if (recv_errno == _system) { \
  1478. if (sock->connected) { \
  1479. dev->result = _isc; \
  1480. inc_stats(sock->manager->stats, \
  1481. sock->statsindex[STATID_RECVFAIL]); \
  1482. return (DOIO_HARD); \
  1483. } \
  1484. return (DOIO_SOFT); \
  1485. }
  1486. #define ALWAYS_HARD(_system, _isc) \
  1487. if (recv_errno == _system) { \
  1488. dev->result = _isc; \
  1489. inc_stats(sock->manager->stats, \
  1490. sock->statsindex[STATID_RECVFAIL]); \
  1491. return (DOIO_HARD); \
  1492. }
  1493. SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
  1494. SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
  1495. SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
  1496. SOFT_OR_HARD(EHOSTDOWN, ISC_R_HOSTDOWN);
  1497. /* HPUX 11.11 can return EADDRNOTAVAIL. */
  1498. SOFT_OR_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
  1499. ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
  1500. /*
  1501. * HPUX returns EPROTO and EINVAL on receiving some ICMP/ICMPv6
  1502. * errors.
  1503. */
  1504. #ifdef EPROTO
  1505. SOFT_OR_HARD(EPROTO, ISC_R_HOSTUNREACH);
  1506. #endif
  1507. SOFT_OR_HARD(EINVAL, ISC_R_HOSTUNREACH);
  1508. #undef SOFT_OR_HARD
  1509. #undef ALWAYS_HARD
  1510. dev->result = isc__errno2result(recv_errno);
  1511. inc_stats(sock->manager->stats,
  1512. sock->statsindex[STATID_RECVFAIL]);
  1513. return (DOIO_HARD);
  1514. }
  1515. /*
  1516. * On TCP and UNIX sockets, zero length reads indicate EOF,
  1517. * while on UDP sockets, zero length reads are perfectly valid,
  1518. * although strange.
  1519. */
  1520. switch (sock->type) {
  1521. case isc_sockettype_tcp:
  1522. case isc_sockettype_unix:
  1523. if (cc == 0)
  1524. return (DOIO_EOF);
  1525. break;
  1526. case isc_sockettype_udp:
  1527. break;
  1528. case isc_sockettype_fdwatch:
  1529. default:
  1530. INSIST(0);
  1531. }
  1532. if (sock->type == isc_sockettype_udp) {
  1533. dev->address.length = msghdr.msg_namelen;
  1534. if (isc_sockaddr_getport(&dev->address) == 0) {
  1535. if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
  1536. socket_log(sock, &dev->address, IOEVENT,
  1537. isc_msgcat, ISC_MSGSET_SOCKET,
  1538. ISC_MSG_ZEROPORT,
  1539. "dropping source port zero packet");
  1540. }
  1541. return (DOIO_SOFT);
  1542. }
  1543. /*
  1544. * Simulate a firewall blocking UDP responses bigger than
  1545. * 512 bytes.
  1546. */
  1547. if (sock->manager->maxudp != 0 && cc > sock->manager->maxudp)
  1548. return (DOIO_SOFT);
  1549. }
  1550. socket_log(sock, &dev->address, IOEVENT,
  1551. isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PKTRECV,
  1552. "packet received correctly");
  1553. /*
  1554. * Overflow bit detection. If we received MORE bytes than we should,
  1555. * this indicates an overflow situation. Set the flag in the
  1556. * dev entry and adjust how much we read by one.
  1557. */
  1558. #ifdef ISC_NET_RECVOVERFLOW
  1559. if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) {
  1560. dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
  1561. cc--;
  1562. }
  1563. #endif
  1564. /*
  1565. * If there are control messages attached, run through them and pull
  1566. * out the interesting bits.
  1567. */
  1568. if (sock->type == isc_sockettype_udp)
  1569. process_cmsg(sock, &msghdr, dev);
  1570. /*
  1571. * update the buffers (if any) and the i/o count
  1572. */
  1573. dev->n += cc;
  1574. actual_count = cc;
  1575. buffer = ISC_LIST_HEAD(dev->bufferlist);
  1576. while (buffer != NULL && actual_count > 0U) {
  1577. REQUIRE(ISC_BUFFER_VALID(buffer));
  1578. if (isc_buffer_availablelength(buffer) <= actual_count) {
  1579. actual_count -= isc_buffer_availablelength(buffer);
  1580. isc_buffer_add(buffer,
  1581. isc_buffer_availablelength(buffer));
  1582. } else {
  1583. isc_buffer_add(buffer, actual_count);
  1584. actual_count = 0;
  1585. POST(actual_count);
  1586. break;
  1587. }
  1588. buffer = ISC_LIST_NEXT(buffer, link);
  1589. if (buffer == NULL) {
  1590. INSIST(actual_count == 0U);
  1591. }
  1592. }
  1593. /*
  1594. * If we read less than we expected, update counters,
  1595. * and let the upper layer poke the descriptor.
  1596. */
  1597. if (((size_t)cc != read_count) && (dev->n < dev->minimum))
  1598. return (DOIO_SOFT);
  1599. /*
  1600. * Full reads are posted, or partials if partials are ok.
  1601. */
  1602. dev->result = ISC_R_SUCCESS;
  1603. return (DOIO_SUCCESS);
  1604. }
  1605. /*
  1606. * Returns:
  1607. * DOIO_SUCCESS The operation succeeded. dev->result contai…