2019-11-05 13:55:54 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <uv.h>
|
|
|
|
|
|
|
|
#include <isc/atomic.h>
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/condition.h>
|
|
|
|
#include <isc/magic.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/netmgr.h>
|
|
|
|
#include <isc/random.h>
|
|
|
|
#include <isc/refcount.h>
|
|
|
|
#include <isc/region.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/sockaddr.h>
|
|
|
|
#include <isc/thread.h>
|
|
|
|
#include <isc/util.h>
|
2019-12-02 11:19:55 +01:00
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
#include "netmgr-int.h"
|
2020-02-12 13:59:18 +01:00
|
|
|
#include "uv-compat.h"
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
static isc_result_t udp_send_direct(isc_nmsocket_t *sock, isc__nm_uvreq_t *req,
|
|
|
|
isc_sockaddr_t *peer);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
static void udp_recv_cb(uv_udp_t *handle, ssize_t nrecv, const uv_buf_t *buf,
|
|
|
|
const struct sockaddr *addr, unsigned flags);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
static void udp_send_cb(uv_udp_send_t *req, int status);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_nm_listenudp(isc_nm_t *mgr, isc_nmiface_t *iface, isc_nm_recv_cb_t cb,
|
2020-02-13 14:44:37 -08:00
|
|
|
void *cbarg, size_t extrahandlesize, isc_nmsocket_t **sockp) {
|
2019-11-05 13:55:54 -08:00
|
|
|
isc_nmsocket_t *nsock = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_NM(mgr));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are creating mgr->nworkers duplicated sockets, one
|
|
|
|
* socket for each worker thread.
|
|
|
|
*/
|
|
|
|
nsock = isc_mem_get(mgr->mctx, sizeof(isc_nmsocket_t));
|
2020-01-05 01:02:12 -08:00
|
|
|
isc__nmsocket_init(nsock, mgr, isc_nm_udplistener, iface);
|
2019-11-05 13:55:54 -08:00
|
|
|
nsock->nchildren = mgr->nworkers;
|
|
|
|
atomic_init(&nsock->rchildren, mgr->nworkers);
|
2020-02-13 14:44:37 -08:00
|
|
|
nsock->children = isc_mem_get(mgr->mctx,
|
|
|
|
mgr->nworkers * sizeof(*nsock));
|
2019-11-05 13:55:54 -08:00
|
|
|
memset(nsock->children, 0, mgr->nworkers * sizeof(*nsock));
|
|
|
|
|
|
|
|
INSIST(nsock->rcb.recv == NULL && nsock->rcbarg == NULL);
|
|
|
|
nsock->rcb.recv = cb;
|
|
|
|
nsock->rcbarg = cbarg;
|
|
|
|
nsock->extrahandlesize = extrahandlesize;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mgr->nworkers; i++) {
|
|
|
|
uint16_t family = iface->addr.type.sa.sa_family;
|
2020-02-13 14:44:37 -08:00
|
|
|
int res;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
isc__netievent_udplisten_t *ievent = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nmsocket_t *csock = &nsock->children[i];
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-01-05 01:02:12 -08:00
|
|
|
isc__nmsocket_init(csock, mgr, isc_nm_udpsocket, iface);
|
2019-11-05 13:55:54 -08:00
|
|
|
csock->parent = nsock;
|
|
|
|
csock->tid = i;
|
|
|
|
csock->extrahandlesize = extrahandlesize;
|
|
|
|
|
|
|
|
INSIST(csock->rcb.recv == NULL && csock->rcbarg == NULL);
|
|
|
|
csock->rcb.recv = cb;
|
|
|
|
csock->rcbarg = cbarg;
|
|
|
|
csock->fd = socket(family, SOCK_DGRAM, 0);
|
|
|
|
INSIST(csock->fd >= 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is SO_REUSE**** hell:
|
|
|
|
* On Linux SO_REUSEPORT allows multiple sockets to bind to
|
|
|
|
* the same host:port pair.
|
|
|
|
* On Windows the same thing is achieved with SO_REUSEADDR
|
|
|
|
*/
|
|
|
|
#ifdef WIN32
|
|
|
|
res = setsockopt(csock->fd, SOL_SOCKET, SO_REUSEADDR,
|
2020-02-12 13:59:18 +01:00
|
|
|
&(int){ 1 }, sizeof(int));
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef WIN32 */
|
2019-11-05 13:55:54 -08:00
|
|
|
res = setsockopt(csock->fd, SOL_SOCKET, SO_REUSEPORT,
|
2020-02-12 13:59:18 +01:00
|
|
|
&(int){ 1 }, sizeof(int));
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef WIN32 */
|
2019-11-05 13:55:54 -08:00
|
|
|
RUNTIME_CHECK(res == 0);
|
|
|
|
|
|
|
|
ievent = isc__nm_get_ievent(mgr, netievent_udplisten);
|
|
|
|
ievent->sock = csock;
|
|
|
|
isc__nm_enqueue_ievent(&mgr->workers[i],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)ievent);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
*sockp = nsock;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle 'udplisten' async call - start listening on a socket.
|
|
|
|
*/
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__nm_async_udplisten(isc__networker_t *worker, isc__netievent_t *ev0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__netievent_udplisten_t *ievent = (isc__netievent_udplisten_t *)ev0;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nmsocket_t *sock = ievent->sock;
|
|
|
|
int r, flags = 0;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
REQUIRE(sock->iface != NULL);
|
|
|
|
REQUIRE(sock->parent != NULL);
|
2020-01-16 12:13:28 +01:00
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
uv_udp_init(&worker->loop, &sock->uv_handle.udp);
|
2019-12-02 11:19:55 +01:00
|
|
|
uv_handle_set_data(&sock->uv_handle.handle, NULL);
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_nmsocket_attach(sock, (isc_nmsocket_t **)&sock->uv_handle.udp.data);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-01-06 20:26:47 -08:00
|
|
|
r = uv_udp_open(&sock->uv_handle.udp, sock->fd);
|
|
|
|
if (r == 0) {
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_OPEN]);
|
|
|
|
} else {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_OPENFAIL]);
|
2020-01-06 20:26:47 -08:00
|
|
|
}
|
|
|
|
|
2020-01-10 14:25:30 -08:00
|
|
|
if (sock->iface->addr.type.sa.sa_family == AF_INET6) {
|
|
|
|
flags = UV_UDP_IPV6ONLY;
|
|
|
|
}
|
2020-01-06 20:26:47 -08:00
|
|
|
|
|
|
|
r = uv_udp_bind(&sock->uv_handle.udp,
|
|
|
|
&sock->parent->iface->addr.type.sa, flags);
|
|
|
|
if (r < 0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_BINDFAIL]);
|
2020-01-06 20:26:47 -08:00
|
|
|
}
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
uv_recv_buffer_size(&sock->uv_handle.handle,
|
2020-02-12 13:59:18 +01:00
|
|
|
&(int){ 16 * 1024 * 1024 });
|
2019-11-05 13:55:54 -08:00
|
|
|
uv_send_buffer_size(&sock->uv_handle.handle,
|
2020-02-12 13:59:18 +01:00
|
|
|
&(int){ 16 * 1024 * 1024 });
|
2020-01-06 20:26:47 -08:00
|
|
|
uv_udp_recv_start(&sock->uv_handle.udp, isc__nm_alloc_cb, udp_recv_cb);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
udp_close_cb(uv_handle_t *handle) {
|
2019-12-02 11:19:55 +01:00
|
|
|
isc_nmsocket_t *sock = uv_handle_get_data(handle);
|
2019-11-05 13:55:54 -08:00
|
|
|
atomic_store(&sock->closed, true);
|
|
|
|
|
|
|
|
isc_nmsocket_detach((isc_nmsocket_t **)&sock->uv_handle.udp.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
stop_udp_child(isc_nmsocket_t *sock) {
|
2020-01-16 12:13:28 +01:00
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
uv_udp_recv_stop(&sock->uv_handle.udp);
|
2020-02-12 13:59:18 +01:00
|
|
|
uv_close((uv_handle_t *)&sock->uv_handle.udp, udp_close_cb);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-01-06 20:26:47 -08:00
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_CLOSE]);
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
LOCK(&sock->parent->lock);
|
|
|
|
atomic_fetch_sub(&sock->parent->rchildren, 1);
|
|
|
|
UNLOCK(&sock->parent->lock);
|
|
|
|
BROADCAST(&sock->parent->cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
stoplistening(isc_nmsocket_t *sock) {
|
2020-01-16 12:13:28 +01:00
|
|
|
REQUIRE(sock->type == isc_nm_udplistener);
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
/*
|
|
|
|
* Socket is already closing; there's nothing to do.
|
|
|
|
*/
|
2020-01-16 11:52:58 +01:00
|
|
|
if (!isc__nmsocket_active(sock)) {
|
2019-11-05 13:55:54 -08:00
|
|
|
return;
|
|
|
|
}
|
2020-01-16 12:13:28 +01:00
|
|
|
|
2020-01-16 11:52:58 +01:00
|
|
|
/*
|
|
|
|
* Mark it inactive now so that all sends will be ignored
|
|
|
|
* and we won't try to stop listening again.
|
|
|
|
*/
|
|
|
|
atomic_store(&sock->active, false);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
for (int i = 0; i < sock->nchildren; i++) {
|
2019-12-09 12:24:46 -08:00
|
|
|
isc__netievent_udpstop_t *event = NULL;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-01-16 12:13:28 +01:00
|
|
|
if (isc_nm_tid() == sock->children[i].tid) {
|
2019-11-05 13:55:54 -08:00
|
|
|
stop_udp_child(&sock->children[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-09 12:24:46 -08:00
|
|
|
event = isc__nm_get_ievent(sock->mgr, netievent_udpstop);
|
2019-11-05 13:55:54 -08:00
|
|
|
event->sock = &sock->children[i];
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[i],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)event);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
while (atomic_load_relaxed(&sock->rchildren) > 0) {
|
|
|
|
WAIT(&sock->cond, &sock->lock);
|
|
|
|
}
|
|
|
|
atomic_store(&sock->closed, true);
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
isc__nmsocket_prep_destroy(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nm_udp_stoplistening(isc_nmsocket_t *sock) {
|
2019-12-09 12:24:46 -08:00
|
|
|
isc__netievent_udpstop_t *ievent = NULL;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
/* We can't be launched from network thread, we'd deadlock */
|
|
|
|
REQUIRE(!isc__nm_in_netthread());
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
REQUIRE(sock->type == isc_nm_udplistener);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the manager is interlocked, re-enqueue this as an asynchronous
|
|
|
|
* event. Otherwise, go ahead and stop listening right away.
|
|
|
|
*/
|
|
|
|
if (!isc__nm_acquire_interlocked(sock->mgr)) {
|
2019-12-09 12:24:46 -08:00
|
|
|
ievent = isc__nm_get_ievent(sock->mgr, netievent_udpstop);
|
2019-11-05 13:55:54 -08:00
|
|
|
ievent->sock = sock;
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)ievent);
|
2019-11-05 13:55:54 -08:00
|
|
|
} else {
|
|
|
|
stoplistening(sock);
|
|
|
|
isc__nm_drop_interlocked(sock->mgr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-12-09 12:24:46 -08:00
|
|
|
* handle 'udpstop' async call - stop listening on a socket.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__nm_async_udpstop(isc__networker_t *worker, isc__netievent_t *ev0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__netievent_udpstop_t *ievent = (isc__netievent_udpstop_t *)ev0;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nmsocket_t *sock = ievent->sock;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
REQUIRE(sock->iface != NULL);
|
|
|
|
UNUSED(worker);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is a child socket, stop listening and return.
|
|
|
|
*/
|
|
|
|
if (sock->parent != NULL) {
|
|
|
|
stop_udp_child(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If network manager is paused, re-enqueue the event for later.
|
|
|
|
*/
|
|
|
|
if (!isc__nm_acquire_interlocked(sock->mgr)) {
|
|
|
|
isc__netievent_udplisten_t *event = NULL;
|
|
|
|
|
2019-12-09 12:24:46 -08:00
|
|
|
event = isc__nm_get_ievent(sock->mgr, netievent_udpstop);
|
2019-11-05 13:55:54 -08:00
|
|
|
event->sock = sock;
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)event);
|
2019-11-05 13:55:54 -08:00
|
|
|
} else {
|
|
|
|
stoplistening(sock);
|
|
|
|
isc__nm_drop_interlocked(sock->mgr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-01-06 20:26:47 -08:00
|
|
|
* udp_recv_cb handles incoming UDP packet from uv. The buffer here is
|
|
|
|
* reused for a series of packets, so we need to allocate a new one. This
|
|
|
|
* new one can be reused to send the response then.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
udp_recv_cb(uv_udp_t *handle, ssize_t nrecv, const uv_buf_t *buf,
|
2020-02-13 14:44:37 -08:00
|
|
|
const struct sockaddr *addr, unsigned flags) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_nmhandle_t *nmhandle = NULL;
|
|
|
|
isc_sockaddr_t sockaddr;
|
|
|
|
isc_sockaddr_t localaddr;
|
2019-11-05 13:55:54 -08:00
|
|
|
struct sockaddr_storage laddr;
|
2019-12-02 11:19:55 +01:00
|
|
|
isc_nmsocket_t *sock = uv_handle_get_data((uv_handle_t *)handle);
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_region_t region;
|
|
|
|
uint32_t maxudp;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
|
2019-11-15 13:22:13 -08:00
|
|
|
/*
|
|
|
|
* We can ignore the flags; currently the only one in use by libuv
|
|
|
|
* is UV_UDP_PARTIAL, which only occurs if the receive buffer is
|
|
|
|
* too small, which can't happen here.
|
|
|
|
*/
|
2019-11-05 13:55:54 -08:00
|
|
|
UNUSED(flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If addr == NULL that's the end of stream - we can
|
|
|
|
* free the buffer and bail.
|
|
|
|
*/
|
|
|
|
if (addr == NULL) {
|
|
|
|
isc__nm_free_uvbuf(sock, buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simulate a firewall blocking UDP packets bigger than
|
|
|
|
* 'maxudp' bytes.
|
|
|
|
*/
|
|
|
|
maxudp = atomic_load(&sock->mgr->maxudp);
|
|
|
|
if (maxudp != 0 && (uint32_t)nrecv > maxudp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = isc_sockaddr_fromsockaddr(&sockaddr, addr);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
2020-02-12 13:59:18 +01:00
|
|
|
uv_udp_getsockname(handle, (struct sockaddr *)&laddr,
|
|
|
|
&(int){ sizeof(struct sockaddr_storage) });
|
2019-11-05 13:55:54 -08:00
|
|
|
result = isc_sockaddr_fromsockaddr(&localaddr,
|
2020-02-12 13:59:18 +01:00
|
|
|
(struct sockaddr *)&laddr);
|
2019-11-05 13:55:54 -08:00
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
nmhandle = isc__nmhandle_get(sock, &sockaddr, &localaddr);
|
2020-02-12 13:59:18 +01:00
|
|
|
region.base = (unsigned char *)buf->base;
|
2019-11-05 13:55:54 -08:00
|
|
|
region.length = nrecv;
|
|
|
|
|
|
|
|
INSIST(sock->rcb.recv != NULL);
|
|
|
|
sock->rcb.recv(nmhandle, ®ion, sock->rcbarg);
|
|
|
|
isc__nm_free_uvbuf(sock, buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the recv callback wants to hold on to the handle,
|
|
|
|
* it needs to attach to it.
|
|
|
|
*/
|
|
|
|
isc_nmhandle_unref(nmhandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* isc__nm_udp_send sends buf to a peer on a socket.
|
|
|
|
* It tries to find a proper sibling/child socket so that we won't have
|
|
|
|
* to jump to other thread.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__nm_udp_send(isc_nmhandle_t *handle, isc_region_t *region, isc_nm_cb_t cb,
|
2020-02-13 14:44:37 -08:00
|
|
|
void *cbarg) {
|
|
|
|
isc_nmsocket_t *psock = NULL, *rsock = NULL;
|
|
|
|
isc_nmsocket_t *sock = handle->sock;
|
|
|
|
isc_sockaddr_t *peer = &handle->peer;
|
2019-12-03 00:07:59 -08:00
|
|
|
isc__netievent_udpsend_t *ievent = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__nm_uvreq_t *uvreq = NULL;
|
|
|
|
int ntid;
|
|
|
|
uint32_t maxudp = atomic_load(&sock->mgr->maxudp);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Simulate a firewall blocking UDP packets bigger than
|
2020-01-17 11:42:35 +01:00
|
|
|
* 'maxudp' bytes, for testing purposes.
|
|
|
|
*
|
|
|
|
* The client would ordinarily have unreferenced the handle
|
|
|
|
* in the callback, but that won't happen in this case, so
|
|
|
|
* we need to do so here.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
|
|
|
if (maxudp != 0 && region->length > maxudp) {
|
|
|
|
isc_nmhandle_unref(handle);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->type == isc_nm_udpsocket) {
|
|
|
|
INSIST(sock->parent != NULL);
|
|
|
|
psock = sock->parent;
|
|
|
|
} else if (sock->type == isc_nm_udplistener) {
|
|
|
|
psock = sock;
|
|
|
|
} else {
|
2020-01-17 12:07:34 +01:00
|
|
|
INSIST(0);
|
|
|
|
ISC_UNREACHABLE();
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
2020-01-16 11:52:58 +01:00
|
|
|
if (!isc__nmsocket_active(sock)) {
|
|
|
|
return (ISC_R_CANCELED);
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
if (isc__nm_in_netthread()) {
|
|
|
|
ntid = isc_nm_tid();
|
|
|
|
} else {
|
2020-02-12 13:59:18 +01:00
|
|
|
ntid = (int)isc_random_uniform(sock->nchildren);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
rsock = &psock->children[ntid];
|
|
|
|
|
|
|
|
uvreq = isc__nm_uvreq_get(sock->mgr, sock);
|
2020-02-12 13:59:18 +01:00
|
|
|
uvreq->uvbuf.base = (char *)region->base;
|
2019-11-05 13:55:54 -08:00
|
|
|
uvreq->uvbuf.len = region->length;
|
|
|
|
|
|
|
|
uvreq->handle = handle;
|
|
|
|
isc_nmhandle_ref(uvreq->handle);
|
|
|
|
|
|
|
|
uvreq->cb.send = cb;
|
|
|
|
uvreq->cbarg = cbarg;
|
|
|
|
|
|
|
|
if (isc_nm_tid() == rsock->tid) {
|
|
|
|
/*
|
|
|
|
* If we're in the same thread as the socket we can send the
|
|
|
|
* data directly
|
|
|
|
*/
|
|
|
|
return (udp_send_direct(rsock, uvreq, peer));
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We need to create an event and pass it using async channel
|
|
|
|
*/
|
|
|
|
ievent = isc__nm_get_ievent(sock->mgr, netievent_udpsend);
|
|
|
|
ievent->sock = rsock;
|
|
|
|
ievent->peer = *peer;
|
|
|
|
ievent->req = uvreq;
|
|
|
|
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[rsock->tid],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)ievent);
|
2019-11-05 13:55:54 -08:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle 'udpsend' async event - send a packet on the socket
|
|
|
|
*/
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__nm_async_udpsend(isc__networker_t *worker, isc__netievent_t *ev0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__netievent_udpsend_t *ievent = (isc__netievent_udpsend_t *)ev0;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
REQUIRE(worker->id == ievent->sock->tid);
|
|
|
|
|
2020-01-16 11:52:58 +01:00
|
|
|
if (isc__nmsocket_active(ievent->sock)) {
|
2019-11-05 13:55:54 -08:00
|
|
|
udp_send_direct(ievent->sock, ievent->req, &ievent->peer);
|
|
|
|
} else {
|
2020-02-12 13:59:18 +01:00
|
|
|
ievent->req->cb.send(ievent->req->handle, ISC_R_CANCELED,
|
|
|
|
ievent->req->cbarg);
|
2019-11-05 13:55:54 -08:00
|
|
|
isc__nm_uvreq_put(&ievent->req, ievent->req->sock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* udp_send_cb - callback
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
udp_send_cb(uv_udp_send_t *req, int status) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2019-11-05 13:55:54 -08:00
|
|
|
isc__nm_uvreq_t *uvreq = (isc__nm_uvreq_t *)req->data;
|
|
|
|
|
|
|
|
REQUIRE(VALID_UVREQ(uvreq));
|
|
|
|
REQUIRE(VALID_NMHANDLE(uvreq->handle));
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
result = isc__nm_uverr2result(status);
|
2020-01-06 20:26:47 -08:00
|
|
|
isc__nm_incstats(uvreq->sock->mgr,
|
|
|
|
uvreq->sock->statsindex[STATID_SENDFAIL]);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
uvreq->cb.send(uvreq->handle, result, uvreq->cbarg);
|
|
|
|
isc_nmhandle_unref(uvreq->handle);
|
|
|
|
isc__nm_uvreq_put(&uvreq, uvreq->sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* udp_send_direct sends buf to a peer on a socket. Sock has to be in
|
|
|
|
* the same thread as the callee.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
udp_send_direct(isc_nmsocket_t *sock, isc__nm_uvreq_t *req,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_sockaddr_t *peer) {
|
2019-11-05 13:55:54 -08:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
|
|
|
|
isc_nmhandle_ref(req->handle);
|
2020-02-12 13:59:18 +01:00
|
|
|
rv = uv_udp_send(&req->uv_req.udp_send, &sock->uv_handle.udp,
|
|
|
|
&req->uvbuf, 1, &peer->type.sa, udp_send_cb);
|
2019-11-05 13:55:54 -08:00
|
|
|
if (rv < 0) {
|
2020-01-06 20:26:47 -08:00
|
|
|
isc__nm_incstats(req->sock->mgr,
|
|
|
|
req->sock->statsindex[STATID_SENDFAIL]);
|
2019-11-05 13:55:54 -08:00
|
|
|
return (isc__nm_uverr2result(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|