forked from pi-hole/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.c
588 lines (501 loc) · 16.1 KB
/
socket.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* Socket connection routines
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "FTL.h"
#include "api.h"
// The backlog argument defines the maximum length
// to which the queue of pending connections for
// telnetfd may grow. If a connection request arrives
// when the queue is full, the client may receive an
// error with an indication of ECONNREFUSED or, if
// the underlying protocol supports retransmission,
// the request may be ignored so that a later
// reattempt at connection succeeds.
#define BACKLOG 5
// File descriptors
int socketfd, telnetfd4 = 0, telnetfd6 = 0;
bool dualstack = false;
bool ipv4telnet = false, ipv6telnet = false;
bool istelnet[MAXCONNS];
void saveport(void)
{
FILE *f;
if((f = fopen(FTLfiles.port, "w+")) == NULL)
{
logg("WARNING: Unable to write used port to file.");
logg(" Continuing anyway (API might not find the port).");
}
else
{
fprintf(f, "%i", config.port);
fclose(f);
}
}
bool bind_to_telnet_port_IPv4(int *socketdescriptor)
{
// IPv4 socket
*socketdescriptor = socket(AF_INET, SOCK_STREAM, 0);
if(*socketdescriptor < 0)
{
logg("Error opening IPv4 telnet socket: %s (%i)", strerror(errno), errno);
exit(EXIT_FAILURE);
}
// Set SO_REUSEADDR to allow re-binding to the port that has been used
// previously by FTL. A common pattern is that you change FTL's
// configuration file and need to restart that server to make it reload
// its configuration. Without SO_REUSEADDR, the bind() call in the restarted
// new instance will fail if there were connections open to the previous
// instance when you killed it. Those connections will hold the TCP port in
// the TIME_WAIT state for 30-120 seconds, so you fall into case 1 above.
setsockopt(*socketdescriptor, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int));
struct sockaddr_in serv_addr4;
// set all values in the buffer to zero
memset(&serv_addr4, 0, sizeof(serv_addr4));
serv_addr4.sin_family = AF_INET;
if(config.socket_listenlocal)
serv_addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
else
serv_addr4.sin_addr.s_addr = INADDR_ANY;
// Bind to IPv4 port
serv_addr4.sin_port = htons(config.port);
if(bind(*socketdescriptor, (struct sockaddr *) &serv_addr4, sizeof(serv_addr4)) < 0)
{
logg("Error listening on IPv4 port %i: %s (%i)", config.port, strerror(errno), errno);
return false;
}
// The listen system call allows the process to listen on the socket for connections
if(listen(*socketdescriptor, BACKLOG) == -1)
{
logg("Error listening on IPv4 socket: %s (%i)", strerror(errno), errno);
return false;
}
logg("Listening on port %i for incoming IPv4 telnet connections", config.port);
return true;
}
bool bind_to_telnet_port_IPv6(int *socketdescriptor)
{
// IPv6 socket
*socketdescriptor = socket(AF_INET6, SOCK_STREAM, 0);
if(*socketdescriptor < 0)
{
logg("Error opening IPv6 telnet socket: %s (%i)", strerror(errno), errno);
exit(EXIT_FAILURE);
}
// If this flag is set to true (nonzero), then the socket is re‐
// stricted to sending and receiving IPv6 packets only. In this
// case, an IPv4 and an IPv6 application can bind to a single port
// at the same time.
setsockopt(*socketdescriptor, IPPROTO_IPV6, IPV6_V6ONLY, &(int){ 1 }, sizeof(int));
// Set SO_REUSEADDR to allow re-binding to the port that has been used
// previously by FTL. A common pattern is that you change FTL's
// configuration file and need to restart that server to make it reload
// its configuration. Without SO_REUSEADDR, the bind() call in the restarted
// new instance will fail if there were connections open to the previous
// instance when you killed it. Those connections will hold the TCP port in
// the TIME_WAIT state for 30-120 seconds, so you fall into case 1 above.
setsockopt(*socketdescriptor, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int));
struct sockaddr_in6 serv_addr;
// set all values in the buffer to zero
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin6_family = AF_INET6;
if(config.socket_listenlocal)
serv_addr.sin6_addr = in6addr_loopback;
else
serv_addr.sin6_addr = in6addr_any;
// The bind() system call binds a socket to an address,
// in this case the address of the current host and
// port number on which the server will run.
// convert this to network byte order using the function htons()
// which converts a port number in host byte order to a port number
// in network byte order
// Bind to IPv6 socket
serv_addr.sin6_port = htons(config.port);
if(bind(*socketdescriptor, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
logg("Error listening on IPv6 port %i: %s (%i)", config.port, strerror(errno), errno);
return false;
}
// The listen system call allows the process to listen on the socket for connections
if(listen(*socketdescriptor, BACKLOG) == -1)
{
logg("Error listening on IPv6 socket: %s (%i)", strerror(errno), errno);
return false;
}
logg("Listening on port %i for incoming IPv6 telnet connections", config.port);
return true;
}
void bind_to_unix_socket(int *socketdescriptor)
{
*socketdescriptor = socket(AF_LOCAL, SOCK_STREAM, 0);
if(*socketdescriptor < 0)
{
logg("Error opening Unix socket");
exit(EXIT_FAILURE);
}
// Make sure unix socket file handle does not exist, if it exists, remove it
unlink(FTLfiles.socketfile);
struct sockaddr_un address;
address.sun_family = AF_LOCAL;
strcpy(address.sun_path, FTLfiles.socketfile);
// Bild to Unix socket handle
errno = 0;
if(bind(*socketdescriptor, (struct sockaddr *) &address, sizeof (address)) != 0)
{
logg("Error on binding on Unix socket %s: %s (%i)", FTLfiles.socketfile, strerror(errno), errno);
exit(EXIT_FAILURE);
}
// The listen system call allows the process to listen on the Unix socket for connections
if(listen(*socketdescriptor, BACKLOG) == -1)
{
logg("Error listening on Unix socket: %s (%i)", strerror(errno), errno);
exit(EXIT_FAILURE);
}
logg("Listening on Unix socket");
}
// Called from main() at graceful shutdown
void removeport(void)
{
FILE *f;
if((f = fopen(FTLfiles.port, "w+")) == NULL)
{
logg("WARNING: Unable to empty port file");
return;
}
fclose(f);
}
void seom(int sock)
{
if(istelnet[sock])
ssend(sock, "---EOM---\n\n");
else
pack_eom(sock);
}
void ssend(int sock, const char *format, ...)
{
char *buffer;
va_list args;
va_start(args, format);
int ret = vasprintf(&buffer, format, args);
va_end(args);
if(ret > 0)
{
if(!write(sock, buffer, strlen(buffer)))
logg("WARNING: Socket write returned error %s (%i)", strerror(errno), errno);
free(buffer);
}
}
void swrite(int sock, void *value, size_t size) {
if(write(sock, value, size) == -1)
logg("WARNING: Socket write returned error code %i", errno);
}
int checkClientLimit(int socket) {
if(socket < MAXCONNS)
{
if(debugclients)
logg("Client connected: %i", socket);
return socket;
}
else
{
if(debugclients)
logg("Client denied (at max capacity of %i): %i", MAXCONNS, socket);
close(socket);
return -1;
}
}
int listener(int sockfd, char type)
{
struct sockaddr_un un_addr;
struct sockaddr_in in4_addr;
struct sockaddr_in6 in6_addr;
socklen_t socklen = 0;
int socket;
switch(type)
{
case 0: // Unix socket
memset(&un_addr, 0, sizeof(un_addr));
socklen = sizeof(un_addr);
return accept(sockfd, (struct sockaddr *) &un_addr, &socklen);
case 4: // Internet socket (IPv4)
memset(&in4_addr, 0, sizeof(in4_addr));
socklen = sizeof(un_addr);
socket = accept(sockfd, (struct sockaddr *) &in4_addr, &socklen);
return checkClientLimit(socket);
case 6: // Internet socket (IPv6)
memset(&in6_addr, 0, sizeof(in6_addr));
socklen = sizeof(un_addr);
socket = accept(sockfd, (struct sockaddr *) &in6_addr, &socklen);
return checkClientLimit(socket);
default: // Should not happen
logg("Cannot listen on type %i connection, code error!", type);
exit(EXIT_FAILURE);
}
}
void close_telnet_socket(void)
{
removeport();
// Using global variable here
if(telnetfd4)
close(telnetfd4);
if(telnetfd6)
close(telnetfd6);
}
void close_unix_socket(void)
{
// The process has to take care of unlinking the socket file description on exit
unlink(FTLfiles.socketfile);
// Using global variable here
close(socketfd);
}
void *telnet_connection_handler_thread(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int*)socket_desc;
// Set connection type to telnet
istelnet[sock] = true;
// Store copy only for displaying the debug messages
int sockID = sock;
char client_message[SOCKETBUFFERLEN] = "";
// Set thread name
char threadname[16];
sprintf(threadname,"telnet-%i",sockID);
prctl(PR_SET_NAME,threadname,0,0,0);
//Receive from client
ssize_t n;
while((n = recv(sock,client_message,SOCKETBUFFERLEN-1, 0)))
{
if (n > 0)
{
char *message = strdup(client_message);
if(message == NULL) break;
// Clear client message receive buffer
memset(client_message, 0, sizeof client_message);
// Lock FTL data structure, since it is likely that it will be changed here
// Requests should not be processed/answered when data is about to change
enable_thread_lock(threadname);
process_request(message, &sock);
free(message);
// Release thread lock
disable_thread_lock(threadname);
if(sock == 0)
{
// Client disconnected by sending EOT or ">quit"
break;
}
}
else if(n == -1)
{
if(debugclients) logg("Telnet connection interrupted (%s), ID: %i", strerror(errno), sockID);
break;
}
}
if(debugclients)
logg("Telnet disconnected, ID: %i", sockID);
//Free the socket pointer
if(sock != 0)
close(sock);
free(socket_desc);
return false;
}
void *socket_connection_handler_thread(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int*)socket_desc;
// Set connection type to not telnet
istelnet[sock] = false;
// Store copy only for displaying the debug messages
int sockID = sock;
char client_message[SOCKETBUFFERLEN] = "";
// Set thread name
char threadname[16];
sprintf(threadname,"socket-%i",sockID);
prctl(PR_SET_NAME,threadname,0,0,0);
//Receive from client
ssize_t n;
while((n = recv(sock,client_message,SOCKETBUFFERLEN-1, 0)))
{
if (n > 0)
{
char *message = strdup(client_message);
if(message == NULL) break;
// Clear client message receive buffer
memset(client_message, 0, sizeof client_message);
// Lock FTL data structure, since it is likely that it will be changed here
// Requests should not be processed/answered when data is about to change
enable_thread_lock(threadname);
process_request(message, &sock);
free(message);
// Release thread lock
disable_thread_lock(threadname);
if(sock == 0)
{
// Socket connection interrupted by sending EOT or ">quit"
break;
}
}
else if(n == -1)
{
if(debugclients) logg("Unix socket connection interrupted (%s), ID: %i", strerror(errno), sockID);
break;
}
}
if(debugclients) logg("Socket disconnected, ID: %i", sockID);
//Free the socket pointer
if(sock != 0)
close(sock);
free(socket_desc);
return false;
}
void bind_sockets(void)
{
// Initialize IPv4 telnet socket
if(bind_to_telnet_port_IPv4(&telnetfd4))
ipv4telnet = true;
// Initialize IPv6 telnet socket
// only if IPv6 interfaces are available
if(ipv6_available())
if(bind_to_telnet_port_IPv6(&telnetfd6))
ipv6telnet = true;
saveport();
// Initialize Unix socket
bind_to_unix_socket(&socketfd);
}
void *telnet_listening_thread_IPv4(void *args)
{
// We will use the attributes object later to start all threads in detached mode
pthread_attr_t attr;
// Initialize thread attributes object with default attribute values
pthread_attr_init(&attr);
// When a detached thread terminates, its resources are automatically released back to
// the system without the need for another thread to join with the terminated thread
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// Set thread name
prctl(PR_SET_NAME,"telnet-IPv4",0,0,0);
// Listen as long as FTL is not killed
while(!killed)
{
// Look for new clients that want to connect
int csck = listener(telnetfd4, 4);
if(csck == -1)
{
logg("IPv4 telnet error: %s (%i)", strerror(errno), errno);
continue;
}
// Allocate memory used to transport client socket ID to client listening thread
int *newsock;
newsock = calloc(1,sizeof(int));
if(newsock == NULL) break;
*newsock = csck;
pthread_t telnet_connection_thread;
// Create a new thread
if(pthread_create( &telnet_connection_thread, &attr, telnet_connection_handler_thread, (void*) newsock ) != 0)
{
// Log the error code description
logg("WARNING: Unable to open telnet processing thread, error: %s", strerror(errno));
}
}
return false;
}
void *telnet_listening_thread_IPv6(void *args)
{
// We will use the attributes object later to start all threads in detached mode
pthread_attr_t attr;
// Initialize thread attributes object with default attribute values
pthread_attr_init(&attr);
// When a detached thread terminates, its resources are automatically released back to
// the system without the need for another thread to join with the terminated thread
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// Set thread name
prctl(PR_SET_NAME,"telnet-IPv6",0,0,0);
// Listen as long as FTL is not killed
while(!killed)
{
// Look for new clients that want to connect
int csck = listener(telnetfd6, 6);
if(csck == -1)
{
logg("IPv6 telnet error: %s (%i)", strerror(errno), errno);
continue;
}
// Allocate memory used to transport client socket ID to client listening thread
int *newsock;
newsock = calloc(1,sizeof(int));
if(newsock == NULL) break;
*newsock = csck;
pthread_t telnet_connection_thread;
// Create a new thread
if(pthread_create( &telnet_connection_thread, &attr, telnet_connection_handler_thread, (void*) newsock ) != 0)
{
// Log the error code description
logg("WARNING: Unable to open telnet processing thread, error: %s", strerror(errno));
}
}
return false;
}
void *socket_listening_thread(void *args)
{
// We will use the attributes object later to start all threads in detached mode
pthread_attr_t attr;
// Initialize thread attributes object with default attribute values
pthread_attr_init(&attr);
// When a detached thread terminates, its resources are automatically released back to
// the system without the need for another thread to join with the terminated thread
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// Set thread name
prctl(PR_SET_NAME,"socket listener",0,0,0);
// Listen as long as FTL is not killed
while(!killed)
{
// Look for new clients that want to connect
int csck = listener(socketfd, 0);
if(csck < 0) continue;
// Allocate memory used to transport client socket ID to client listening thread
int *newsock;
newsock = calloc(1,sizeof(int));
if(newsock == NULL) break;
*newsock = csck;
pthread_t socket_connection_thread;
// Create a new thread
if(pthread_create( &socket_connection_thread, &attr, socket_connection_handler_thread, (void*) newsock ) != 0)
{
// Log the error code description
logg("WARNING: Unable to open socket processing thread, error: %s", strerror(errno));
}
}
return false;
}
bool ipv6_available(void)
{
struct ifaddrs *allInterfaces;
int iface[2] = { 0 };
// Get all interfaces
if (getifaddrs(&allInterfaces) == 0)
{
struct ifaddrs *interface;
// Loop over interfaces
for (interface = allInterfaces; interface != NULL; interface = interface->ifa_next)
{
unsigned int flags = interface->ifa_flags;
struct sockaddr *addr = interface->ifa_addr;
// Check only for up and running IPv4, IPv6 interfaces
if ((flags & (IFF_UP|IFF_RUNNING)) && addr != NULL)
{
iface[addr->sa_family == AF_INET6 ? 1 : 0]++;
// For now unused debug statement
// logg("Interface %s is %s", interface->ifa_name, addr->sa_family == AF_INET6 ? "IPv6" : "IPv4");
}
}
freeifaddrs(allInterfaces);
}
if(debug)
{
logg("Found %i IPv4 and %i IPv6 capable interfaces", iface[0], iface[1]);
}
return (iface[1] > 0);
}