1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/*
* llmnrd -- LLMNR (RFC 4705) responder daemon.
*
* Copyright (C) 2014-2017 Tobias Klauser <tklauser@distanz.ch>
*
* This file is part of llmnrd.
*
* llmnrd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* llmnrd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with llmnrd. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include "compiler.h"
#include "log.h"
#include "util.h"
#include "iface.h"
#include "llmnr.h"
#include "llmnr-packet.h"
#include "socket.h"
static bool llmnrd_running = true;
static int llmnrd_sock_ipv4 = -1;
static int llmnrd_sock_ipv6 = -1;
static int llmnrd_fd_hostname = -1;
static const char *short_opts = "H:i:p:6dhV";
static const struct option long_opts[] = {
{ "hostname", required_argument, NULL, 'H' },
{ "interface", required_argument, NULL, 'i' },
{ "port", required_argument, NULL, 'p' },
{ "ipv6", no_argument, NULL, '6' },
{ "daemonize", no_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 },
};
static void __noreturn usage_and_exit(int status)
{
fprintf(stdout, "Usage: llmnrd [OPTIONS]\n"
"Options:\n"
" -H, --hostname NAME set hostname to respond with (default: system hostname)\n"
" -i, --interface DEV bind socket to a specific interface, e.g. eth0\n"
" -p, --port NUM set port number to listen on (default: %d)\n"
" -6, --ipv6 enable LLMNR name resolution over IPv6\n"
" -d, --daemonize run as daemon in the background\n"
" -h, --help show this help and exit\n"
" -V, --version show version information and exit\n",
LLMNR_UDP_PORT);
exit(status);
}
static void __noreturn version_and_exit(void)
{
fprintf(stdout, "llmnrd %s %s\n"
"Copyright (C) 2014-2017 Tobias Klauser <tklauser@distanz.ch>\n"
"Licensed under the GNU General Public License, version 2\n",
VERSION_STRING, GIT_VERSION);
exit(EXIT_SUCCESS);
}
static void signal_handler(int sig)
{
switch (sig) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
log_info("Interrupt received. Stopping llmnrd.\n");
llmnrd_running = false;
break;
case SIGHUP:
default:
/* ignore */
break;
}
}
static void register_signal(int sig, void (*handler)(int))
{
sigset_t block_mask;
struct sigaction saction;
sigfillset(&block_mask);
saction.sa_handler = handler;
saction.sa_mask = block_mask;
saction.sa_flags = SA_RESTART;
if (sigaction(sig, &saction, NULL) != 0) {
log_err("Failed to register signal handler for %s (%d)\n",
strsignal(sig), sig);
}
}
static void iface_event_handle(enum iface_event_type type, unsigned char af,
unsigned int ifindex)
{
switch (af) {
case AF_INET:
socket_mcast_group_ipv4(llmnrd_sock_ipv4, ifindex, type == IFACE_ADD);
break;
case AF_INET6:
socket_mcast_group_ipv6(llmnrd_sock_ipv6, ifindex, type == IFACE_ADD);
break;
default:
/* ignore */
break;
}
}
static void hostname_change_handle(char *hostname, size_t maxlen)
{
char *newname;
newname = xzalloc(maxlen);
if (gethostname(newname, maxlen) == 0) {
newname[maxlen - 1] = '\0';
if (strncmp(hostname, newname, maxlen) != 0) {
log_info("Hostname changed to %s\n", newname);
strncpy(hostname, newname, maxlen);
llmnr_set_hostname(hostname);
}
}
free(newname);
}
int main(int argc, char **argv)
{
int c, ret = -1;
long num_arg;
bool daemonize = false, ipv6 = false;
char *hostname = NULL;
char *iface = NULL;
uint16_t port = LLMNR_UDP_PORT;
int llmnrd_sock_rtnl = -1;
int nfds;
setlinebuf(stdout);
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
switch (c) {
case 'd':
daemonize = true;
break;
case 'H':
hostname = xstrdup(optarg);
break;
case 'i':
iface = xstrdup(optarg);
break;
case 'p':
num_arg = strtol(optarg, NULL, 0);
if (num_arg < 0 || num_arg > UINT16_MAX) {
log_err("Invalid port number: %ld\n", num_arg);
return EXIT_FAILURE;
}
port = num_arg;
break;
case '6':
ipv6 = true;
break;
case 'V':
version_and_exit();
case 'h':
usage_and_exit(EXIT_SUCCESS);
default:
usage_and_exit(EXIT_FAILURE);
}
}
register_signal(SIGINT, signal_handler);
register_signal(SIGQUIT, signal_handler);
register_signal(SIGTERM, signal_handler);
register_signal(SIGHUP, signal_handler);
if (!hostname) {
hostname = xzalloc(MAXHOSTNAMELEN);
if (gethostname(hostname, MAXHOSTNAMELEN) != 0) {
log_err("Failed to get hostname");
return EXIT_FAILURE;
}
hostname[MAXHOSTNAMELEN - 1] = '\0';
llmnrd_fd_hostname = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NDELAY);
}
if (daemonize) {
if (daemon(0, 0) != 0) {
log_err("Failed to daemonize process: %s\n", strerror(errno));
goto out;
}
}
log_info("Starting llmnrd on port %u, hostname %s\n", port, hostname);
if (iface)
log_info("Binding to interface %s\n", iface);
llmnrd_sock_ipv4 = socket_open_ipv4(port, iface);
if (llmnrd_sock_ipv4 < 0)
goto out;
if (ipv6) {
llmnrd_sock_ipv6 = socket_open_ipv6(port, iface);
if (llmnrd_sock_ipv6 < 0)
goto out;
}
llmnrd_sock_rtnl = socket_open_rtnl(ipv6);
if (llmnrd_sock_rtnl < 0)
goto out;
llmnr_init(hostname, ipv6);
iface_init(llmnrd_sock_rtnl, iface, ipv6, &iface_event_handle);
nfds = max(llmnrd_sock_ipv4, llmnrd_sock_rtnl);
if (llmnrd_sock_ipv6 >= 0)
nfds = max(nfds, llmnrd_sock_ipv6);
if (llmnrd_fd_hostname >= 0)
nfds = max(nfds, llmnrd_fd_hostname);
nfds += 1;
while (llmnrd_running) {
fd_set rfds, efds;
FD_ZERO(&rfds);
FD_SET(llmnrd_sock_ipv4, &rfds);
FD_SET(llmnrd_sock_rtnl, &rfds);
if (llmnrd_sock_ipv6 >= 0)
FD_SET(llmnrd_sock_ipv6, &rfds);
FD_ZERO(&efds);
if (llmnrd_fd_hostname >= 0)
FD_SET(llmnrd_fd_hostname, &efds);
ret = select(nfds, &rfds, NULL, &efds, NULL);
if (ret < 0) {
if (errno != EINTR)
log_err("Failed to select() on socket: %s\n", strerror(errno));
goto out;
} else if (ret) {
/* handle RTNL messages first so we can respond with
* up-to-date information.
*/
if (FD_ISSET(llmnrd_sock_rtnl, &rfds))
iface_recv(llmnrd_sock_rtnl);
if (FD_ISSET(llmnrd_sock_ipv4, &rfds))
llmnr_recv(llmnrd_sock_ipv4);
if (llmnrd_sock_ipv6 >= 0 && FD_ISSET(llmnrd_sock_ipv6, &rfds))
llmnr_recv(llmnrd_sock_ipv6);
if (llmnrd_fd_hostname >= 0 && FD_ISSET(llmnrd_fd_hostname, &efds))
hostname_change_handle(hostname, MAXHOSTNAMELEN);
}
}
ret = 0;
out:
if (llmnrd_sock_rtnl >= 0)
close(llmnrd_sock_rtnl);
if (llmnrd_sock_ipv6 >= 0)
close(llmnrd_sock_ipv6);
if (llmnrd_sock_ipv4 >= 0)
close(llmnrd_sock_ipv4);
free(hostname);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
|