From 00093ae71662ad597ac7db916ff08fe2f18e901d Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 2 Aug 2016 11:05:52 +0200 Subject: llmnr-query: Set TTL values as suggested in RFC 4795, section 2.5 Follow commit 64644dde ("socket: Set TTL values as suggested in RFC 4795, section 2.5") and 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 --- llmnr-query.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'llmnr-query.c') diff --git a/llmnr-query.c b/llmnr-query.c index 303961d..de3df26 100644 --- a/llmnr-query.c +++ b/llmnr-query.c @@ -1,7 +1,7 @@ /* * Simple LLMNR query command. * - * Copyright (C) 2015 Tobias Klauser + * Copyright (C) 2015-2016 Tobias Klauser * * This file is part of llmnrd. * @@ -67,7 +67,7 @@ static void __noreturn usage_and_exit(int status) static void __noreturn version_and_exit(void) { fprintf(stdout, "llmnr-query %s %s\n" - "Copyright (C) 2015 Tobias Klauser \n" + "Copyright (C) 2015-2016 Tobias Klauser \n" "Licensed under the GNU General Public License, version 2\n", VERSION_STRING, GIT_VERSION); exit(EXIT_SUCCESS); @@ -96,6 +96,7 @@ int main(int argc, char **argv) uint16_t qtype = LLMNR_QTYPE_A; bool ipv6 = false; struct pkt *p; + static const int TTL = 255; while ((c = getopt_long(argc, argv, short_ops, long_opts, NULL)) != -1) { switch (c) { @@ -151,6 +152,30 @@ int main(int argc, char **argv) return -1; } + if (ipv6) { + /* 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; + } + } else { + /* 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; + } + } + if (iface != NULL) { unsigned int ifindex = if_nametoindex(iface); -- cgit v1.2.3-54-g00ecf