diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2016-08-02 10:55:40 +0200 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2016-08-02 10:55:40 +0200 |
commit | 64644ddece9e6f5f08469ab4ad1277ab31983dad (patch) | |
tree | af107e7f323ebee27d7cecc385b2311ab3497eae | |
parent | 741f912bc2b8da74ce7d60325dff662bdb4b2bfe (diff) |
socket: Set TTL values as suggested in RFC 4795, section 2.5
Set the TTL of the IP header to 255 for UDP unicast and multicast
packets, as recommended in RFC 4795, section 2.5.
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r-- | socket.c | 25 |
1 files changed, 24 insertions, 1 deletions
@@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2015 Tobias Klauser <tklauser@distanz.ch> + * Copyright (C) 2014-2016 Tobias Klauser <tklauser@distanz.ch> * * This file is part of llmnrd. * @@ -34,6 +34,7 @@ #include "socket.h" static const int YES = 1; +static const int TTL = 255; int socket_open_ipv4(uint16_t port) { @@ -52,6 +53,17 @@ int socket_open_ipv4(uint16_t port) goto err; } + /* RFC 4795, section 2.5 recommends to set TTL to 255 for UDP */ + if (setsockopt(sock, IPPROTO_IP, IP_TTL, &TTL, sizeof(TTL)) < 0) { + log_err("Failed to set IPv4 unicast TTL socket option: %s\n", strerror(errno)); + goto err; + } + + if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &TTL, sizeof(TTL)) < 0) { + log_err("Failed to set IPv4 multicast TTL socket option: %s\n", strerror(errno)); + goto err; + } + /* bind the socket */ memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; @@ -91,6 +103,17 @@ int socket_open_ipv6(uint16_t port) goto err; } + /* RFC 4795, section 2.5 recommends to set TTL to 255 for UDP */ + if (setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &TTL, sizeof(TTL)) < 0) { + log_err("Failed to set IPv6 unicast hops socket option: %s\n", strerror(errno)); + goto err; + } + + if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &TTL, sizeof(TTL)) < 0) { + log_err("Failed to set IPv6 multicast hops socket option: %s\n", strerror(errno)); + goto err; + } + /* IPv6 only socket */ if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &YES, sizeof(YES)) < 0) { log_err("Failed to set IPv6 only socket option: %s\n", strerror(errno)); |