diff --git a/include/uv.h b/include/uv.h index 4a8e5008..f4093ff7 100644 --- a/include/uv.h +++ b/include/uv.h @@ -658,6 +658,19 @@ UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl); */ UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on); +/* + * Set the time to live + * + * Arguments: + * handle UDP handle. Should have been initialized with + * `uv_udp_init`. + * ttl 1 through 255 + * + * Returns: + * 0 on success, -1 on error. + */ +UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl); + /* * Send data. If the socket has not previously been bound with `uv_udp_bind` * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address) diff --git a/src/unix/udp.c b/src/unix/udp.c index 0ebfe707..26e5f42d 100644 --- a/src/unix/udp.c +++ b/src/unix/udp.c @@ -528,6 +528,16 @@ int uv_udp_set_broadcast(uv_udp_t* handle, int on) { } +int uv_udp_set_ttl(uv_udp_t* handle, int ttl) { + if (setsockopt(handle->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof ttl)) { + uv__set_sys_error(handle->loop, errno); + return -1; + } + + return 0; +} + + int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name, int* namelen) { socklen_t socklen; diff --git a/src/win/udp.c b/src/win/udp.c index 8a36396b..359dce0a 100644 --- a/src/win/udp.c +++ b/src/win/udp.c @@ -599,3 +599,9 @@ int uv_udp_set_broadcast(uv_udp_t* handle, int on) { return 0; } + + +int uv_udp_set_ttl(uv_udp_t* handle, int ttl) { + uv__set_artificial_error(handle->loop, UV_ENOSYS); + return -1; +}