summaryrefslogtreecommitdiff
path: root/socket.c
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2016-08-02 10:55:40 +0200
committerTobias Klauser <tklauser@distanz.ch>2016-08-02 10:55:40 +0200
commit64644ddece9e6f5f08469ab4ad1277ab31983dad (patch)
treeaf107e7f323ebee27d7cecc385b2311ab3497eae /socket.c
parent741f912bc2b8da74ce7d60325dff662bdb4b2bfe (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>
Diffstat (limited to 'socket.c')
-rw-r--r--socket.c25
1 files changed, 24 insertions, 1 deletions
diff --git a/socket.c b/socket.c
index 0b0be78..872796d 100644
--- a/socket.c
+++ b/socket.c
@@ -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));