From 371de55728931fc253763328ae322ce9512afba1 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 7 Feb 2017 18:26:54 +0100 Subject: llmnrd: compare full hostname in query, fix off-by-one error When checking whether the hostname matches, llmnrd does not consider the last character of the hostnames, leading to the following incorrect behavior: $ ./llmnrd -H foobar Starting llmnrd on port 5355, hostname foobar ... $ ./llmnr-query foobaZ LLMNR query: foobaZ IN ANY LLMNR response: foobar IN A 10.42.0.42 (TTL 30) Fix it by considering the full hostname. Also do only convert alphabetic characters to lowercase using tolower(3), since its behavior is undefined for any other characters. Fixes #21 Signed-off-by: Tobias Klauser --- llmnr.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/llmnr.c b/llmnr.c index 64fcabd..0b35ae7 100644 --- a/llmnr.c +++ b/llmnr.c @@ -61,9 +61,15 @@ static bool llmnr_name_matches(const uint8_t *query) if (query[1 + n] != 0) return false; - for (i = 1; i < llmnr_hostname[0]; i++) - if (tolower(query[i]) != tolower(llmnr_hostname[i])) + for (i = 1; i <= n; i++) { + char a = query[i]; + char b = llmnr_hostname[i]; + a = isalpha(a) ? tolower(a) : a; + b = isalpha(b) ? tolower(b) : b; + if (a != b) { return false; + } + } return true; } -- cgit v1.2.3-54-g00ecf