Lines Matching defs:sock

558 socket_log(isc_socket_t *sock, isc_sockaddr_t *address,
563 socket_log(isc_socket_t *sock, isc_sockaddr_t *address,
582 "socket %p: %s", sock, msgbuf);
587 "socket %p %s: %s", sock, peerbuf, msgbuf);
598 FIX_IPV6_RECVPKTINFO(isc_socket_t *sock)
603 if (sock->pf != AF_INET6 || sock->type != isc_sockettype_udp)
606 if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
612 "%s: %s", sock->fd,
621 #define FIX_IPV6_RECVPKTINFO(sock) (void)0
1030 process_cmsg(isc_socket_t *sock, struct msghdr *msg, isc_socketevent_t *dev) {
1042 * sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
1047 UNUSED(sock);
1078 socket_log(sock, NULL, TRACE,
1090 socket_log(sock, NULL, TRACE,
1135 build_msghdr_send(isc_socket_t *sock, isc_socketevent_t *dev,
1146 if (!sock->connected) {
1210 if ((sock->type == isc_sockettype_udp)
1218 socket_log(sock, NULL, TRACE,
1224 INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
1225 msg->msg_control = (void *)sock->sendcmsgbuf;
1227 cmsgp = (struct cmsghdr *)sock->sendcmsgbuf;
1239 cmsgp = (struct cmsghdr *)(sock->sendcmsgbuf +
1242 INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
1273 build_msghdr_recv(isc_socket_t *sock, isc_socketevent_t *dev,
1283 if (sock->type == isc_sockettype_udp) {
1286 if (sock->pf == AF_INET) {
1289 } else if (sock->pf == AF_INET6) {
1293 } else if (sock->pf == AF_UNIX) {
1312 dev->address = sock->peer_address;
1364 if (sock->type == isc_sockettype_udp) {
1365 iov[iovcount].iov_base = (void *)(&sock->overflow);
1379 if (sock->type == isc_sockettype_udp) {
1380 msg->msg_control = sock->recvcmsgbuf;
1381 msg->msg_controllen = sock->recvcmsgbuflen;
1394 set_dev_address(isc_sockaddr_t *address, isc_socket_t *sock,
1397 if (sock->type == isc_sockettype_udp) {
1401 dev->address = sock->peer_address;
1402 } else if (sock->type == isc_sockettype_tcp) {
1404 dev->address = sock->peer_address;
1418 allocate_socketevent(isc_socket_t *sock, isc_eventtype_t eventtype,
1423 ev = (isc_socketevent_t *)isc_event_allocate(sock->manager->mctx,
1424 sock, eventtype,
1471 doio_recv(isc_socket_t *sock, isc_socketevent_t *dev) {
1481 build_msghdr_recv(sock, dev, &msghdr, iov, &read_count);
1487 cc = recvmsg(sock->fd, &msghdr, 0);
1500 socket_log(sock, NULL, IOEVENT,
1504 sock->fd, cc, recv_errno, strbuf);
1509 if (sock->connected) { \
1511 inc_stats(sock->manager->stats, \
1512 sock->statsindex[STATID_RECVFAIL]); \
1520 inc_stats(sock->manager->stats, \
1521 sock->statsindex[STATID_RECVFAIL]); \
1549 inc_stats(sock->manager->stats,
1550 sock->statsindex[STATID_RECVFAIL]);
1559 switch (sock->type) {
1572 if (sock->type == isc_sockettype_udp) {
1576 socket_log(sock, &dev->address, IOEVENT,
1585 socket_log(sock, &dev->address, IOEVENT,
1595 if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) {
1605 if (sock->type == isc_sockettype_udp)
1606 process_cmsg(sock, &msghdr, dev);
1660 doio_send(isc_socket_t *sock, isc_socketevent_t *dev) {
1670 build_msghdr_send(sock, dev, &msghdr, iov, &write_count);
1673 cc = sendmsg(sock->fd, &msghdr, 0);
1688 if (sock->connected) { \
1690 inc_stats(sock->manager->stats, \
1691 sock->statsindex[STATID_SENDFAIL]); \
1699 inc_stats(sock->manager->stats, \
1700 sock->statsindex[STATID_SENDFAIL]); \
1735 inc_stats(sock->manager->stats,
1736 sock->statsindex[STATID_SENDFAIL]);
1741 inc_stats(sock->manager->stats,
1742 sock->statsindex[STATID_SENDFAIL]);
1771 closesocket(isc_socketmgr_t *manager, isc_socket_t *sock, int fd) {
1772 isc_sockettype_t type = sock->type;
1801 inc_stats(manager->stats, sock->statsindex[STATID_CLOSE]);
1836 isc_socket_t *sock = *sockp;
1837 isc_socketmgr_t *manager = sock->manager;
1839 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
1842 INSIST(ISC_LIST_EMPTY(sock->accept_list));
1843 INSIST(ISC_LIST_EMPTY(sock->recv_list));
1844 INSIST(ISC_LIST_EMPTY(sock->send_list));
1845 INSIST(sock->connect_ev == NULL);
1846 REQUIRE(sock->fd == -1 || sock->fd < (int)manager->maxsocks);
1848 if (sock->fd >= 0) {
1849 fd = sock->fd;
1850 sock->fd = -1;
1851 closesocket(manager, sock, fd);
1856 ISC_LIST_UNLINK(manager->socklist, sock, link);
1873 isc_socket_t *sock;
1877 sock = isc_mem_get(manager->mctx, sizeof(*sock));
1879 if (sock == NULL)
1882 sock->magic = 0;
1883 sock->references = 0;
1885 sock->manager = manager;
1886 sock->type = type;
1887 sock->fd = -1;
1888 sock->statsindex = NULL;
1890 ISC_LINK_INIT(sock, link);
1892 sock->recvcmsgbuf = NULL;
1893 sock->sendcmsgbuf = NULL;
1905 sock->recvcmsgbuflen = cmsgbuflen;
1906 if (sock->recvcmsgbuflen != 0U) {
1907 sock->recvcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
1908 if (sock->recvcmsgbuf == NULL) {
1925 sock->sendcmsgbuflen = cmsgbuflen;
1926 if (sock->sendcmsgbuflen != 0U) {
1927 sock->sendcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
1928 if (sock->sendcmsgbuf == NULL) {
1934 memset(sock->name, 0, sizeof(sock->name));
1935 sock->tag = NULL;
1940 ISC_LIST_INIT(sock->recv_list);
1941 ISC_LIST_INIT(sock->send_list);
1942 ISC_LIST_INIT(sock->accept_list);
1943 sock->connect_ev = NULL;
1944 sock->pending_recv = 0;
1945 sock->pending_send = 0;
1946 sock->pending_accept = 0;
1947 sock->listener = 0;
1948 sock->connected = 0;
1949 sock->connecting = 0;
1950 sock->bound = 0;
1955 result = isc_mutex_init(&sock->lock);
1957 sock->magic = 0;
1964 ISC_EVENT_INIT(&sock->readable_ev, sizeof(intev_t),
1966 NULL, sock, sock, NULL, NULL);
1967 ISC_EVENT_INIT(&sock->writable_ev, sizeof(intev_t),
1969 NULL, sock, sock, NULL, NULL);
1971 sock->magic = SOCKET_MAGIC;
1972 *socketp = sock;
1977 if (sock->recvcmsgbuf != NULL)
1978 isc_mem_put(manager->mctx, sock->recvcmsgbuf,
1979 sock->recvcmsgbuflen);
1980 if (sock->sendcmsgbuf != NULL)
1981 isc_mem_put(manager->mctx, sock->sendcmsgbuf,
1982 sock->sendcmsgbuflen);
1983 isc_mem_put(manager->mctx, sock, sizeof(*sock));
1997 isc_socket_t *sock = *socketp;
1999 INSIST(sock->references == 0);
2000 INSIST(VALID_SOCKET(sock));
2001 INSIST(!sock->connecting);
2002 INSIST(!sock->pending_recv);
2003 INSIST(!sock->pending_send);
2004 INSIST(!sock->pending_accept);
2005 INSIST(ISC_LIST_EMPTY(sock->recv_list));
2006 INSIST(ISC_LIST_EMPTY(sock->send_list));
2007 INSIST(ISC_LIST_EMPTY(sock->accept_list));
2008 INSIST(!ISC_LINK_LINKED(sock, link));
2010 if (sock->recvcmsgbuf != NULL)
2011 isc_mem_put(sock->manager->mctx, sock->recvcmsgbuf,
2012 sock->recvcmsgbuflen);
2013 if (sock->sendcmsgbuf != NULL)
2014 isc_mem_put(sock->manager->mctx, sock->sendcmsgbuf,
2015 sock->sendcmsgbuflen);
2017 sock->magic = 0;
2019 DESTROYLOCK(&sock->lock);
2021 isc_mem_put(sock->manager->mctx, sock, sizeof(*sock));
2065 use_min_mtu(isc_socket_t *sock) {
2067 UNUSED(sock);
2071 if (sock->pf == AF_INET6) {
2073 (void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
2081 if (sock->pf == AF_INET6) {
2083 (void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MTU,
2090 opensocket(isc_socketmgr_t *manager, isc_socket_t *sock) {
2104 switch (sock->type) {
2106 sock->fd = socket(sock->pf, SOCK_DGRAM, IPPROTO_UDP);
2109 sock->fd = socket(sock->pf, SOCK_STREAM, IPPROTO_TCP);
2112 sock->fd = socket(sock->pf, SOCK_STREAM, 0);
2121 if (sock->fd == -1 && errno == EINTR && tries++ < 42)
2128 if (manager->reserved != 0 && sock->type == isc_sockettype_udp &&
2129 sock->fd >= 0 && sock->fd < manager->reserved) {
2131 new = fcntl(sock->fd, F_DUPFD, manager->reserved);
2133 (void)close(sock->fd);
2135 sock->fd = new;
2137 } else if (sock->fd >= 0 && sock->fd < 20) {
2139 new = fcntl(sock->fd, F_DUPFD, 20);
2141 (void)close(sock->fd);
2143 sock->fd = new;
2148 if (sock->fd >= (int)manager->maxsocks) {
2149 (void)close(sock->fd);
2155 sock->fd, manager->maxsocks);
2159 if (sock->fd < 0) {
2196 result = make_nonblock(sock->fd);
2198 (void)close(sock->fd);
2205 if (sock->type != isc_sockettype_unix && bsdcompat &&
2206 setsockopt(sock->fd, SOL_SOCKET, SO_BSDCOMPAT,
2211 sock->fd,
2220 if (setsockopt(sock->fd, SOL_SOCKET, SO_NOSIGPIPE,
2225 sock->fd,
2236 use_min_mtu(sock);
2239 if (sock->type == isc_sockettype_udp) {
2243 if (setsockopt(sock->fd, SOL_SOCKET, SO_TIMESTAMP,
2249 sock->fd,
2260 if (sock->pf == AF_INET6 && sock->recvcmsgbuflen == 0U) {
2272 if ((sock->pf == AF_INET6)
2273 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
2278 "%s: %s", sock->fd,
2287 if ((sock->pf == AF_INET6)
2288 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO,
2293 sock->fd,
2306 if (sock->pf == AF_INET6) {
2308 (void)setsockopt(sock->fd, IPPROTO_IPV6,
2320 if (sock->pf == AF_INET) {
2322 (void)setsockopt(sock->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2330 if (sock->pf == AF_INET) {
2332 (void)setsockopt(sock->fd, IPPROTO_IP, IP_DONTFRAG,
2339 if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
2343 if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
2348 sock->fd, size,
2360 inc_stats(manager->stats, sock->statsindex[STATID_OPEN]);
2375 isc_socket_t *sock = NULL;
2383 result = allocate_socket(manager, type, &sock);
2387 switch (sock->type) {
2389 sock->statsindex =
2393 sock->statsindex =
2397 sock->statsindex = unixstatsindex;
2403 sock->pf = pf;
2404 result = opensocket(manager, sock);
2406 inc_stats(manager->stats, sock->statsindex[STATID_OPENFAIL]);
2407 free_socket(&sock);
2411 sock->references = 1;
2412 *socketp = sock;
2419 lockid = FDLOCK_ID(sock->fd);
2421 manager->fds[sock->fd] = sock;
2422 manager->fdstate[sock->fd] = MANAGED;
2424 INSIST(sock->manager->fdpollinfo[sock->fd].want_read == 0 &&
2425 sock->manager->fdpollinfo[sock->fd].want_write == 0);
2430 ISC_LIST_APPEND(manager->socklist, sock, link);
2432 if (manager->maxfd < sock->fd)
2433 manager->maxfd = sock->fd;
2437 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
2444 isc_socket_open(isc_socket_t *sock) {
2447 REQUIRE(VALID_SOCKET(sock));
2449 LOCK(&sock->lock);
2450 REQUIRE(sock->references == 1);
2451 REQUIRE(sock->type != isc_sockettype_fdwatch);
2452 UNLOCK(&sock->lock);
2457 REQUIRE(sock->fd == -1);
2459 result = opensocket(sock->manager, sock);
2461 sock->fd = -1;
2464 int lockid = FDLOCK_ID(sock->fd);
2466 LOCK(&sock->manager->fdlock[lockid]);
2467 sock->manager->fds[sock->fd] = sock;
2468 sock->manager->fdstate[sock->fd] = MANAGED;
2470 INSIST(sock->manager->fdpollinfo[sock->fd].want_read == 0 &&
2471 sock->manager->fdpollinfo[sock->fd].want_write == 0);
2473 UNLOCK(&sock->manager->fdlock[lockid]);
2476 LOCK(&sock->manager->lock);
2477 if (sock->manager->maxfd < sock->fd)
2478 sock->manager->maxfd = sock->fd;
2479 UNLOCK(&sock->manager->lock);
2497 isc_socket_t *sock = NULL;
2504 result = allocate_socket(manager, isc_sockettype_fdwatch, &sock);
2508 sock->fd = fd;
2509 sock->fdwatcharg = cbarg;
2510 sock->fdwatchcb = callback;
2511 sock->fdwatchflags = flags;
2512 sock->fdwatchtask = task;
2513 sock->statsindex = fdwatchstatsindex;
2515 sock->references = 1;
2516 *socketp = sock;
2523 lockid = FDLOCK_ID(sock->fd);
2525 manager->fds[sock->fd] = sock;
2526 manager->fdstate[sock->fd] = MANAGED;
2530 ISC_LIST_APPEND(manager->socklist, sock, link);
2532 if (manager->maxfd < sock->fd)
2533 manager->maxfd = sock->fd;
2538 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
2540 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
2542 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
2552 isc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp) {
2553 REQUIRE(VALID_SOCKET(sock));
2556 LOCK(&sock->lock);
2557 sock->references++;
2558 UNLOCK(&sock->lock);
2560 *socketp = sock;
2569 isc_socket_t *sock;
2573 sock = *socketp;
2574 REQUIRE(VALID_SOCKET(sock));
2576 LOCK(&sock->lock);
2577 REQUIRE(sock->references > 0);
2578 sock->references--;
2579 if (sock->references == 0)
2581 UNLOCK(&sock->lock);
2584 destroy(&sock);
2590 isc_socket_close(isc_socket_t *sock) {
2594 REQUIRE(VALID_SOCKET(sock));
2596 LOCK(&sock->lock);
2598 REQUIRE(sock->references == 1);
2599 REQUIRE(sock->type != isc_sockettype_fdwatch);
2600 REQUIRE(sock->fd >= 0 && sock->fd < (int)sock->manager->maxsocks);
2602 INSIST(!sock->connecting);
2603 INSIST(!sock->pending_recv);
2604 INSIST(!sock->pending_send);
2605 INSIST(!sock->pending_accept);
2606 INSIST(ISC_LIST_EMPTY(sock->recv_list));
2607 INSIST(ISC_LIST_EMPTY(sock->send_list));
2608 INSIST(ISC_LIST_EMPTY(sock->accept_list));
2609 INSIST(sock->connect_ev == NULL);
2611 manager = sock->manager;
2612 fd = sock->fd;
2613 sock->fd = -1;
2614 memset(sock->name, 0, sizeof(sock->name));
2615 sock->tag = NULL;
2616 sock->listener = 0;
2617 sock->connected = 0;
2618 sock->connecting = 0;
2619 sock->bound = 0;
2620 isc_sockaddr_any(&sock->peer_address);
2622 UNLOCK(&sock->lock);
2624 closesocket(manager, sock, fd);
2638 dispatch_recv(isc_socket_t *sock) {
2643 INSIST(!sock->pending_recv);
2645 if (sock->type != isc_sockettype_fdwatch) {
2646 ev = ISC_LIST_HEAD(sock->recv_list);
2649 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2654 sender = sock->fdwatchtask;
2657 sock->pending_recv = 1;
2658 iev = &sock->readable_ev;
2660 sock->references++;
2661 iev->ev_sender = sock;
2662 if (sock->type == isc_sockettype_fdwatch)
2666 iev->ev_arg = sock;
2672 dispatch_send(isc_socket_t *sock) {
2677 INSIST(!sock->pending_send);
2679 if (sock->type != isc_sockettype_fdwatch) {
2680 ev = ISC_LIST_HEAD(sock->send_list);
2683 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2688 sender = sock->fdwatchtask;
2691 sock->pending_send = 1;
2692 iev = &sock->writable_ev;
2694 sock->references++;
2695 iev->ev_sender = sock;
2696 if (sock->type == isc_sockettype_fdwatch)
2700 iev->ev_arg = sock;
2709 dispatch_accept(isc_socket_t *sock) {
2713 INSIST(!sock->pending_accept);
2719 ev = ISC_LIST_HEAD(sock->accept_list);
2723 sock->pending_accept = 1;
2724 iev = &sock->readable_ev;
2726 sock->references++; /* keep socket around for this internal event */
2727 iev->ev_sender = sock;
2729 iev->ev_arg = sock;
2735 dispatch_connect(isc_socket_t *sock) {
2739 iev = &sock->writable_ev;
2741 ev = sock->connect_ev;
2744 INSIST(sock->connecting);
2746 sock->references++; /* keep socket around for this internal event */
2747 iev->ev_sender = sock;
2749 iev->ev_arg = sock;
2765 send_recvdone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
2770 (*dev)->ev_sender = sock;
2773 ISC_LIST_DEQUEUE(sock->recv_list, *dev, ev_link);
2788 send_senddone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
2794 (*dev)->ev_sender = sock;
2797 ISC_LIST_DEQUEUE(sock->send_list, *dev, ev_link);
2819 isc_socket_t *sock;
2831 sock = ev->ev_sender;
2832 INSIST(VALID_SOCKET(sock));
2834 LOCK(&sock->lock);
2835 socket_log(sock, NULL, TRACE,
2839 manager = sock->manager;
2842 INSIST(sock->listener);
2843 INSIST(sock->pending_accept == 1);
2844 sock->pending_accept = 0;
2846 INSIST(sock->references > 0);
2847 sock->references--; /* the internal event is done with this socket */
2848 if (sock->references == 0) {
2849 UNLOCK(&sock->lock);
2850 destroy(&sock);
2858 dev = ISC_LIST_HEAD(sock->accept_list);
2860 UNLOCK(&sock->lock);
2877 fd = accept(sock->fd, &dev->newsocket->peer_address.type.sa,
2948 sock->pf)
2956 sock->pf);
2974 dev->newsocket->pf = sock->pf;
2980 ISC_LIST_UNLINK(sock->accept_list, dev, ev_link);
2985 if (!ISC_LIST_EMPTY(sock->accept_list))
2986 select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
2988 UNLOCK(&sock->lock);
3030 socket_log(sock, &dev->newsocket->peer_address, CREATION,
3039 inc_stats(manager->stats, sock->statsindex[STATID_ACCEPT]);
3041 inc_stats(manager->stats, sock->statsindex[STATID_ACCEPTFAIL]);
3051 dev->ev_sender = sock;
3057 select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
3058 UNLOCK(&sock->lock);
3060 inc_stats(manager->stats, sock->statsindex[STATID_ACCEPTFAIL]);
3067 isc_socket_t *sock;
3071 sock = ev->ev_sender;
3072 INSIST(VALID_SOCKET(sock));
3074 LOCK(&sock->lock);
3075 socket_log(sock, NULL, IOEVENT,
3079 INSIST(sock->pending_recv == 1);
3080 sock->pending_recv = 0;
3082 INSIST(sock->references > 0);
3083 sock->references--; /* the internal event is done with this socket */
3084 if (sock->references == 0) {
3085 UNLOCK(&sock->lock);
3086 destroy(&sock);
3094 dev = ISC_LIST_HEAD(sock->recv_list);
3096 switch (doio_recv(sock, dev)) {
3108 send_recvdone_event(sock, &dev);
3109 dev = ISC_LIST_HEAD(sock->recv_list);
3115 send_recvdone_event(sock, &dev);
3119 dev = ISC_LIST_HEAD(sock->recv_list);
3123 if (!ISC_LIST_EMPTY(sock->recv_list))
3124 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
3126 UNLOCK(&sock->lock);
3132 isc_socket_t *sock;
3139 sock = (isc_socket_t *)ev->ev_sender;
3140 INSIST(VALID_SOCKET(sock));
3142 LOCK(&sock->lock);
3143 socket_log(sock, NULL, IOEVENT,
3147 INSIST(sock->pending_send == 1);
3148 sock->pending_send = 0;
3150 INSIST(sock->references > 0);
3151 sock->references--; /* the internal event is done with this socket */
3152 if (sock->references == 0) {
3153 UNLOCK(&sock->lock);
3154 destroy(&sock);
3162 dev = ISC_LIST_HEAD(sock->send_list);
3164 switch (doio_send(sock, dev)) {
3170 send_senddone_event(sock, &dev);
3174 dev = ISC_LIST_HEAD(sock->send_list);
3178 if (!ISC_LIST_EMPTY(sock->send_list))
3179 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
3181 UNLOCK(&sock->lock);
3186 isc_socket_t *sock;
3194 sock = (isc_socket_t *)ev->ev_sender;
3195 INSIST(VALID_SOCKET(sock));
3197 LOCK(&sock->lock);
3198 socket_log(sock, NULL, IOEVENT,
3202 INSIST(sock->pending_send == 1);
3204 UNLOCK(&sock->lock);
3205 more_data = (sock->fdwatchcb)(me, sock, sock->fdwatcharg);
3206 LOCK(&sock->lock);
3208 sock->pending_send = 0;
3210 INSIST(sock->references > 0);
3211 sock->references--; /* the internal event is done with this socket */
3212 if (sock->references == 0) {
3213 UNLOCK(&sock->lock);
3214 destroy(&sock);
3219 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
3221 UNLOCK(&sock->lock);
3226 isc_socket_t *sock;
3234 sock = (isc_socket_t *)ev->ev_sender;
3235 INSIST(VALID_SOCKET(sock));
3237 LOCK(&sock->lock);
3238 socket_log(sock, NULL, IOEVENT,
3242 INSIST(sock->pending_recv == 1);
3244 UNLOCK(&sock->lock);
3245 more_data = (sock->fdwatchcb)(me, sock, sock->fdwatcharg);
3246 LOCK(&sock->lock);
3248 sock->pending_recv = 0;
3250 INSIST(sock->references > 0);
3251 sock->references--; /* the internal event is done with this socket */
3252 if (sock->references == 0) {
3253 UNLOCK(&sock->lock);
3254 destroy(&sock);
3259 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
3261 UNLOCK(&sock->lock);
3272 isc_socket_t *sock;
3289 sock = manager->fds[fd];
3292 if (sock == NULL) {
3297 LOCK(&sock->lock);
3298 if (!SOCK_DEAD(sock)) {
3299 if (sock->listener)
3300 dispatch_accept(sock);
3302 dispatch_recv(sock);
3308 if (sock == NULL) {
3314 LOCK(&sock->lock);
3316 if (!SOCK_DEAD(sock)) {
3317 if (sock->connecting)
3318 dispatch_connect(sock);
3320 dispatch_send(sock);
3325 UNLOCK(&sock->lock);
4179 socket_recv(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
4189 if (sock->type == isc_sockettype_udp) {
4190 io_state = doio_recv(sock, dev);
4192 LOCK(&sock->lock);
4195 if (ISC_LIST_EMPTY(sock->recv_list))
4196 io_state = doio_recv(sock, dev);
4213 LOCK(&sock->lock);
4221 if (ISC_LIST_EMPTY(sock->recv_list) && !sock->pending_recv)
4222 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
4223 ISC_LIST_ENQUEUE(sock->recv_list, dev, ev_link);
4225 socket_log(sock, NULL, EVENT, NULL, 0, 0,
4240 send_recvdone_event(sock, &dev);
4245 UNLOCK(&sock->lock);
4251 isc_socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
4260 REQUIRE(VALID_SOCKET(sock));
4266 manager = sock->manager;
4272 INSIST(sock->bound);
4274 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
4281 if (sock->type == isc_sockettype_udp)
4300 return (socket_recv(sock, dev, task, 0));
4304 isc_socket_recv(isc_socket_t *sock, isc_region_t *region, unsigned int minimum,
4310 REQUIRE(VALID_SOCKET(sock));
4313 manager = sock->manager;
4316 INSIST(sock->bound);
4318 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
4322 return (isc_socket_recv2(sock, region, minimum, task, dev, 0));
4326 isc_socket_recv2(isc_socket_t *sock, isc_region_t *region,
4330 event->ev_sender = sock;
4341 if (sock->type == isc_sockettype_udp)
4350 return (socket_recv(sock, event, task, flags));
4354 socket_send(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
4365 set_dev_address(address, sock, dev);
4372 socket_log(sock, NULL, TRACE, isc_msgcat,
4385 if (sock->type == isc_sockettype_udp)
4386 io_state = doio_send(sock, dev);
4388 LOCK(&sock->lock);
4391 if (ISC_LIST_EMPTY(sock->send_list))
4392 io_state = doio_send(sock, dev);
4408 LOCK(&sock->lock);
4417 if (ISC_LIST_EMPTY(sock->send_list) &&
4418 !sock->pending_send)
4419 select_poke(sock->manager, sock->fd,
4421 ISC_LIST_ENQUEUE(sock->send_list, dev, ev_link);
4423 socket_log(sock, NULL, EVENT, NULL, 0, 0,
4435 send_senddone_event(sock, &dev);
4440 UNLOCK(&sock->lock);
4446 isc_socket_send(isc_socket_t *sock, isc_region_t *region,
4452 return (isc_socket_sendto(sock, region, task, action, arg, NULL,
4457 isc_socket_sendto(isc_socket_t *sock, isc_region_t *region,
4464 REQUIRE(VALID_SOCKET(sock));
4469 manager = sock->manager;
4472 INSIST(sock->bound);
4474 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
4480 return (socket_send(sock, dev, task, address, pktinfo, 0));
4484 isc_socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
4487 return (isc_socket_sendtov2(sock, buflist, task, action, arg, NULL,
4492 isc_socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
4496 return (isc_socket_sendtov2(sock, buflist, task, action, arg, address,
4501 isc_socket_sendtov2(isc_socket_t *sock, isc_bufferlist_t *buflist,
4511 REQUIRE(VALID_SOCKET(sock));
4517 manager = sock->manager;
4523 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
4537 return (socket_send(sock, dev, task, address, pktinfo, flags));
4541 isc_socket_sendto2(isc_socket_t *sock, isc_region_t *region,
4548 REQUIRE(sock->type == isc_sockettype_udp);
4549 event->ev_sender = sock;
4557 return (socket_send(sock, event, task, address, pktinfo, flags));
4745 isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr,
4750 LOCK(&sock->lock);
4752 INSIST(!sock->bound);
4754 if (sock->pf != sockaddr->type.sa.sa_family) {
4755 UNLOCK(&sock->lock);
4762 if (sock->pf == AF_UNIX)
4767 setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
4770 "setsockopt(%d) %s", sock->fd,
4778 if (bind(sock->fd, &sockaddr->type.sa, sockaddr->length) < 0) {
4779 inc_stats(sock->manager->stats,
4780 sock->statsindex[STATID_BINDFAIL]);
4782 UNLOCK(&sock->lock);
4800 socket_log(sock, sockaddr, TRACE,
4802 sock->bound = 1;
4804 UNLOCK(&sock->lock);
4816 isc_socket_filter(isc_socket_t *sock, const char *filter) {
4821 UNUSED(sock);
4825 REQUIRE(VALID_SOCKET(sock));
4830 if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER,
4833 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
4855 isc_socket_listen(isc_socket_t *sock, unsigned int backlog) {
4858 REQUIRE(VALID_SOCKET(sock));
4860 LOCK(&sock->lock);
4862 REQUIRE(!sock->listener);
4863 REQUIRE(sock->bound);
4864 REQUIRE(sock->type == isc_sockettype_tcp ||
4865 sock->type == isc_sockettype_unix);
4870 if (listen(sock->fd, (int)backlog) < 0) {
4871 UNLOCK(&sock->lock);
4879 sock->listener = 1;
4881 UNLOCK(&sock->lock);
4889 isc_socket_accept(isc_socket_t *sock,
4899 REQUIRE(VALID_SOCKET(sock));
4900 manager = sock->manager;
4903 LOCK(&sock->lock);
4905 REQUIRE(sock->listener);
4916 UNLOCK(&sock->lock);
4921 result = allocate_socket(manager, sock->type, &nsock);
4924 UNLOCK(&sock->lock);
4936 UNLOCK(&sock->lock);
4940 nsock->statsindex = sock->statsindex;
4950 if (ISC_LIST_EMPTY(sock->accept_list))
4953 ISC_LIST_ENQUEUE(sock->accept_list, dev, ev_link);
4956 select_poke(manager, sock->fd, SELECT_POKE_ACCEPT);
4958 UNLOCK(&sock->lock);
4963 isc_socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr,
4973 REQUIRE(VALID_SOCKET(sock));
4978 manager = sock->manager;
4985 LOCK(&sock->lock);
4987 REQUIRE(!sock->connecting);
4989 dev = (isc_socket_connev_t *)isc_event_allocate(manager->mctx, sock,
4994 UNLOCK(&sock->lock);
5003 sock->peer_address = *addr;
5004 cc = connect(sock->fd, &addr->type.sa, addr->length);
5012 if (sock->type == isc_sockettype_udp && errno == EINPROGRESS) {
5037 sock->connected = 0;
5044 UNLOCK(&sock->lock);
5045 inc_stats(sock->manager->stats,
5046 sock->statsindex[STATID_CONNECTFAIL]);
5051 sock->connected = 0;
5054 UNLOCK(&sock->lock);
5055 inc_stats(sock->manager->stats,
5056 sock->statsindex[STATID_CONNECTFAIL]);
5065 sock->connected = 1;
5066 sock->bound = 1;
5070 UNLOCK(&sock->lock);
5072 inc_stats(sock->manager->stats,
5073 sock->statsindex[STATID_CONNECT]);
5085 sock->connecting = 1;
5094 if (sock->connect_ev == NULL)
5095 select_poke(manager, sock->fd, SELECT_POKE_CONNECT);
5097 sock->connect_ev = dev;
5099 UNLOCK(&sock->lock);
5108 isc_socket_t *sock;
5119 sock = ev->ev_sender;
5120 INSIST(VALID_SOCKET(sock));
5122 LOCK(&sock->lock);
5128 INSIST(sock->references > 0);
5129 sock->references--;
5130 if (sock->references == 0) {
5131 UNLOCK(&sock->lock);
5132 destroy(&sock);
5139 dev = sock->connect_ev;
5141 INSIST(!sock->connecting);
5142 UNLOCK(&sock->lock);
5146 INSIST(sock->connecting);
5147 sock->connecting = 0;
5153 if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR,
5165 sock->connecting = 1;
5166 select_poke(sock->manager, sock->fd,
5168 UNLOCK(&sock->lock);
5173 inc_stats(sock->manager->stats,
5174 sock->statsindex[STATID_CONNECTFAIL]);
5198 isc_sockaddr_format(&sock->peer_address, peerbuf,
5206 inc_stats(sock->manager->stats,
5207 sock->statsindex[STATID_CONNECT]);
5209 sock->connected = 1;
5210 sock->bound = 1;
5213 sock->connect_ev = NULL;
5215 UNLOCK(&sock->lock);
5218 dev->ev_sender = sock;
5223 isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp) {
5226 REQUIRE(VALID_SOCKET(sock));
5229 LOCK(&sock->lock);
5231 if (sock->connected) {
5232 *addressp = sock->peer_address;
5238 UNLOCK(&sock->lock);
5244 isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp) {
5249 REQUIRE(VALID_SOCKET(sock));
5252 LOCK(&sock->lock);
5254 if (!sock->bound) {
5262 if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) {
5272 UNLOCK(&sock->lock);
5282 isc_socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how) {
5284 REQUIRE(VALID_SOCKET(sock));
5293 LOCK(&sock->lock);
5306 && !ISC_LIST_EMPTY(sock->recv_list)) {
5311 dev = ISC_LIST_HEAD(sock->recv_list);
5319 send_recvdone_event(sock, &dev);
5326 && !ISC_LIST_EMPTY(sock->send_list)) {
5331 dev = ISC_LIST_HEAD(sock->send_list);
5339 send_senddone_event(sock, &dev);
5346 && !ISC_LIST_EMPTY(sock->accept_list)) {
5351 dev = ISC_LIST_HEAD(sock->accept_list);
5358 ISC_LIST_UNLINK(sock->accept_list, dev,
5365 dev->ev_sender = sock;
5378 && sock->connect_ev != NULL) {
5382 INSIST(sock->connecting);
5383 sock->connecting = 0;
5385 dev = sock->connect_ev;
5389 sock->connect_ev = NULL;
5392 dev->ev_sender = sock;
5398 UNLOCK(&sock->lock);
5402 isc_socket_gettype(isc_socket_t *sock) {
5403 REQUIRE(VALID_SOCKET(sock));
5405 return (sock->type);
5409 isc_socket_isbound(isc_socket_t *sock) {
5412 LOCK(&sock->lock);
5413 val = ((sock->bound) ? ISC_TRUE : ISC_FALSE);
5414 UNLOCK(&sock->lock);
5420 isc_socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes) {
5425 UNUSED(sock);
5428 REQUIRE(VALID_SOCKET(sock));
5431 if (sock->pf == AF_INET6) {
5432 if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
5438 "%s: %s", sock->fd,
5446 FIX_IPV6_RECVPKTINFO(sock); /* AIX */
5584 isc_socket_t *sock = NULL;
5599 sock = ISC_LIST_HEAD(mgr->socklist);
5600 while (sock != NULL) {
5601 LOCK(&sock->lock);
5605 TRY0(xmlTextWriterWriteFormatString(writer, "%p", sock));
5608 if (sock->name[0] != 0) {
5612 sock->name));
5619 sock->references));
5623 ISC_XMLCHAR _socktype(sock->type)));
5625 if (sock->connected) {
5626 isc_sockaddr_format(&sock->peer_address, peerbuf,
5634 if (getsockname(sock->fd, &addr.type.sa, (void *)&len) == 0) {
5642 if (sock->pending_recv)
5646 if (sock->pending_send)
5650 if (sock->pending_accept)
5654 if (sock->listener)
5658 if (sock->connected)
5662 if (sock->connecting)
5666 if (sock->bound)
5675 UNLOCK(&sock->lock);
5676 sock = ISC_LIST_NEXT(sock, link);
5681 if (sock != NULL)
5682 UNLOCK(&sock->lock);