Add isc_uv_udp_connect() shim

The uv_udp_connect() function is available from libuv 1.27+, move the code to
connect to udp socket to a (limited) shim function called isc_uv_udp_connect().
This commit is contained in:
Ondřej Surý
2020-09-18 09:13:14 +02:00
committed by Evan Hunt
parent 5aee9f642b
commit 44a431cee6
3 changed files with 48 additions and 22 deletions

View File

@@ -621,27 +621,8 @@ isc__nm_async_udpconnect(isc__networker_t *worker, isc__netievent_t *ev0) {
goto done;
}
#if UV_VERSION_MAJOR == 1 && UV_VERSION_MINOR < 27
do {
int addrlen = (ievent->peer.type.sa.sa_family == AF_INET)
? sizeof(struct sockaddr_in)
: sizeof(struct sockaddr_in6);
errno = 0;
r = connect(sock->fd, &ievent->peer.type.sa, addrlen);
} while (r == -1 && errno == EINTR);
if (r == -1) {
isc__nm_incstats(sock->mgr,
sock->statsindex[STATID_CONNECTFAIL]);
atomic_store(&sock->connect_error, true);
sock->result = isc_errno_toresult(errno);
goto done;
}
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_CONNECT]);
#else
r = uv_udp_connect(&sock->uv_handle.udp, &ievent->peer.type.sa);
if (r != 0) {
r = isc_uv_udp_connect(&sock->uv_handle.udp, &ievent->peer.type.sa);
if (r < 0) {
isc__nm_incstats(sock->mgr,
sock->statsindex[STATID_CONNECTFAIL]);
atomic_store(&sock->connect_error, true);
@@ -649,7 +630,6 @@ isc__nm_async_udpconnect(isc__networker_t *worker, isc__netievent_t *ev0) {
goto done;
}
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_CONNECT]);
#endif
#ifdef ISC_RECV_BUFFER_SIZE
uv_recv_buffer_size(&sock->uv_handle.handle,

View File

@@ -187,3 +187,32 @@ isc_uv_import(uv_stream_t *stream, isc_uv_stream_info_t *info) {
#endif /* ifdef WIN32 */
#endif /* ifndef HAVE_UV_IMPORT */
#ifndef HAVE_UV_UDP_SEND
#else
int
isc_uv_udp_connect(uv_udp_t *handle, const struct sockaddr *addr) {
int err = 0;
do {
int addrlen = (ievent->peer.type.sa.sa_family == AF_INET)
? sizeof(struct sockaddr_in)
: sizeof(struct sockaddr_in6);
err = connect(sock->fd, &ievent->peer.type.sa, addrlen);
} while (err == -1 && errno == EINTR);
if (err) {
#ifdef WIN32
return (uv_translate_sys_error(err));
#else /* WIN32 */
return (uv_translate_sys_error(errno));
#endif /* WIN32 */
}
return (0);
}
#endif /* ifndef HAVE_UV_UDP_SEND */

View File

@@ -79,3 +79,20 @@ isc_uv_import(uv_stream_t *stream, isc_uv_stream_info_t *info);
*/
#endif
#ifndef HAVE_UV_UDP_SEND
#define isc_uv_udp_connect uv_udp_connect
#else
int
isc_uv_udp_connect(uv_udp_t *handle, const struct sockaddr *addr);
/*%<
* Associate the UDP handle to a remote address and port, so every message sent
* by this handle is automatically sent to that destination.
*
* NOTE: This is just a limited shim for uv_udp_connect() as it requires the
* handle to be bound.
*/
#endif