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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
|
/*
* relay-server.c - server functions for relay plugin
*
* Copyright (C) 2003-2015 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat 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; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat 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 WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "../weechat-plugin.h"
#include "relay.h"
#include "relay-server.h"
#include "relay-buffer.h"
#include "relay-client.h"
#include "relay-config.h"
struct t_relay_server *relay_servers = NULL;
struct t_relay_server *last_relay_server = NULL;
/*
* Extracts protocol, arguments and options from a string with format
* "options.protocol.args".
*
* Examples:
*
* string ipv4 ipv6 ssl protocol protocol_args
* ---------------------------------------------------------------
* irc.freenode 1 1 0 irc freenode
* ssl.irc.freenode 1 1 1 irc freenode
* ipv4.irc.freenode 1 0 0 irc freenode
* ipv6.irc.freenode 0 1 0 irc freenode
* ipv4.ipv6.irc.freenode 1 1 0 irc freenode
* ipv6.ssl.irc.freenode 0 1 1 irc freenode
* weechat 1 1 0 weechat
* ssl.weechat 1 1 1 weechat
* ipv6.ssl.weechat 0 1 1 weechat
*
* Note: *protocol and *protocol_args must be freed after use.
*/
void
relay_server_get_protocol_args (const char *protocol_and_args,
int *ipv4, int *ipv6, int *ssl,
char **protocol, char **protocol_args)
{
int opt_ipv4, opt_ipv6, opt_ssl;
char *pos;
opt_ipv4 = -1;
opt_ipv6 = -1;
opt_ssl = 0;
while (1)
{
if (strncmp (protocol_and_args, "ipv4.", 5) == 0)
{
opt_ipv4 = 1;
protocol_and_args += 5;
}
else if (strncmp (protocol_and_args, "ipv6.", 5) == 0)
{
opt_ipv6 = 1;
protocol_and_args += 5;
}
else if (strncmp (protocol_and_args, "ssl.", 4) == 0)
{
opt_ssl = 1;
protocol_and_args += 4;
}
else
break;
}
if ((opt_ipv4 == -1) && (opt_ipv6 == -1))
{
/*
* no IPv4/IPv6 specified, then use defaults:
* - IPv4 enabled
* - IPv6 enabled if option relay.network.ipv6 is on
*/
opt_ipv4 = 1;
opt_ipv6 = weechat_config_boolean (relay_config_network_ipv6);
}
else
{
if (opt_ipv4 == -1)
opt_ipv4 = 0;
if (opt_ipv6 == -1)
opt_ipv6 = 0;
}
if (!opt_ipv4 && !opt_ipv6)
{
/* both IPv4/IPv6 disabled (should never occur!) */
opt_ipv4 = 1;
}
if (ipv4)
*ipv4 = opt_ipv4;
if (ipv6)
*ipv6 = opt_ipv6;
if (ssl)
*ssl = opt_ssl;
pos = strchr (protocol_and_args, '.');
if (pos)
{
if (protocol)
{
*protocol = weechat_strndup (protocol_and_args,
pos - protocol_and_args);
}
if (protocol_args)
*protocol_args = strdup (pos + 1);
}
else
{
if (protocol)
*protocol = strdup (protocol_and_args);
if (protocol_args)
*protocol_args = NULL;
}
}
/*
* Searches for a server by protocol.args.
*
* Returns pointer to server, NULL if not found.
*/
struct t_relay_server *
relay_server_search (const char *protocol_and_args)
{
struct t_relay_server *ptr_server;
for (ptr_server = relay_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
if (strcmp (protocol_and_args, ptr_server->protocol_string) == 0)
return ptr_server;
}
return NULL;
}
/*
* Searches for a server by port.
*
* Returns pointer to new server, NULL if not found.
*/
struct t_relay_server *
relay_server_search_port (int port)
{
struct t_relay_server *ptr_server;
for (ptr_server = relay_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
if (ptr_server->port == port)
return ptr_server;
}
/* server not found */
return NULL;
}
/*
* Closes socket for a relay server.
*/
void
relay_server_close_socket (struct t_relay_server *server)
{
if (server->hook_fd)
{
weechat_unhook (server->hook_fd);
server->hook_fd = NULL;
}
if (server->sock >= 0)
{
close (server->sock);
server->sock = -1;
if (!relay_signal_upgrade_received)
{
weechat_printf (NULL,
_("%s: socket closed for %s (port %d)"),
RELAY_PLUGIN_NAME, server->protocol_string,
server->port);
}
}
}
/*
* Reads data from a client which is connecting on socket.
*/
int
relay_server_sock_cb (void *data, int fd)
{
struct t_relay_server *server;
struct sockaddr_in client_addr;
struct sockaddr_in6 client_addr6;
socklen_t client_addr_size;
void *ptr_addr;
int client_fd, flags, set;
char ipv4_address[INET_ADDRSTRLEN + 1], ipv6_address[INET6_ADDRSTRLEN + 1];
char *ptr_ip_address;
/* make C compiler happy */
(void) fd;
server = (struct t_relay_server *)data;
if (server->ipv6)
{
ptr_addr = &client_addr6;
client_addr_size = sizeof (struct sockaddr_in6);
}
else
{
ptr_addr = &client_addr;
client_addr_size = sizeof (struct sockaddr_in);
}
memset (ptr_addr, 0, client_addr_size);
client_fd = accept (server->sock, (struct sockaddr *)ptr_addr,
&client_addr_size);
if (client_fd < 0)
{
weechat_printf (NULL,
_("%s%s: cannot accept client on port %d (%s): error %d %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
server->port, server->protocol_string,
errno, strerror (errno));
return WEECHAT_RC_OK;
}
ptr_ip_address = NULL;
if (server->ipv6)
{
if (inet_ntop (AF_INET6,
&(client_addr6.sin6_addr),
ipv6_address,
INET6_ADDRSTRLEN))
{
ptr_ip_address = ipv6_address;
if (strncmp (ptr_ip_address, "::ffff:", 7) == 0)
{
/* actually an IPv4-mapped IPv6 address, so skip "::ffff:" */
ptr_ip_address += 7;
}
}
}
else
{
if (inet_ntop (AF_INET,
&(client_addr.sin_addr),
ipv4_address,
INET_ADDRSTRLEN))
{
ptr_ip_address = ipv4_address;
}
}
/* check if IP is allowed, if not, just close socket */
if (relay_config_regex_allowed_ips
&& (regexec (relay_config_regex_allowed_ips, ptr_ip_address, 0, NULL, 0) != 0))
{
if (weechat_relay_plugin->debug >= 1)
{
weechat_printf (NULL,
_("%s%s: IP address \"%s\" not allowed for relay"),
weechat_prefix ("error"),
RELAY_PLUGIN_NAME,
ptr_ip_address);
}
close (client_fd);
return WEECHAT_RC_OK;
}
/* set non-blocking mode for socket */
flags = fcntl (client_fd, F_GETFL);
if (flags == -1)
flags = 0;
fcntl (client_fd, F_SETFL, flags | O_NONBLOCK);
/* set socket option SO_REUSEADDR */
set = 1;
if (setsockopt (client_fd, SOL_SOCKET, SO_REUSEADDR,
(void *) &set, sizeof (set)) < 0)
{
weechat_printf (NULL,
_("%s%s: cannot set socket option \"%s\" to %d: "
"error %d %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
"SO_REUSEADDR", set, errno, strerror (errno));
close (client_fd);
return WEECHAT_RC_OK;
}
/* add the client */
relay_client_new (client_fd, ptr_ip_address, server);
return WEECHAT_RC_OK;
}
/*
* Creates socket and server on port.
*
* Returns:
* 1: OK
* 0: error
*/
int
relay_server_create_socket (struct t_relay_server *server)
{
int domain, set, max_clients, addr_size;
struct sockaddr_in server_addr;
struct sockaddr_in6 server_addr6;
const char *bind_address;
void *ptr_addr;
bind_address = weechat_config_string (relay_config_network_bind_address);
if (server->ipv6)
{
domain = AF_INET6;
memset (&server_addr6, 0, sizeof (struct sockaddr_in6));
server_addr6.sin6_family = domain;
server_addr6.sin6_port = htons (server->port);
server_addr6.sin6_addr = in6addr_any;
if (bind_address && bind_address[0])
{
if (!inet_pton (domain, bind_address, &server_addr6.sin6_addr))
{
weechat_printf (NULL,
/* TRANSLATORS: second "%s" is "IPv4" or "IPv6" */
_("%s%s: invalid bind address \"%s\" for %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
bind_address, "IPv6");
return 0;
}
}
ptr_addr = &server_addr6;
addr_size = sizeof (struct sockaddr_in6);
}
else
{
domain = AF_INET;
memset (&server_addr, 0, sizeof (struct sockaddr_in));
server_addr.sin_family = domain;
server_addr.sin_port = htons (server->port);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind_address && bind_address[0])
{
if (!inet_pton (domain, bind_address, &server_addr.sin_addr))
{
weechat_printf (NULL,
/* TRANSLATORS: second "%s" is "IPv4" or "IPv6" */
_("%s%s: invalid bind address \"%s\" for %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
bind_address, "IPv4");
return 0;
}
}
ptr_addr = &server_addr;
addr_size = sizeof (struct sockaddr_in);
}
/* create socket */
server->sock = socket (domain, SOCK_STREAM, 0);
if (server->sock < 0)
{
weechat_printf (NULL,
_("%s%s: cannot create socket: error %d %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
errno, strerror (errno));
if (errno == EAFNOSUPPORT)
{
weechat_printf (NULL,
_("%s%s: try /set relay.network.ipv6 off"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME);
}
return 0;
}
#ifdef IPV6_V6ONLY
/* set option IPV6_V6ONLY to 0 or 1 */
if (server->ipv6)
{
set = (server->ipv4) ? 0 : 1;
if (setsockopt (server->sock, IPPROTO_IPV6, IPV6_V6ONLY,
(void *) &set, sizeof (set)) < 0)
{
weechat_printf (NULL,
_("%s%s: cannot set socket option \"%s\" "
"to %d: error %d %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
"IPV6_V6ONLY", set, errno, strerror (errno));
close (server->sock);
server->sock = -1;
return 0;
}
}
#endif /* IPV6_V6ONLY */
/* set option SO_REUSEADDR to 1 */
set = 1;
if (setsockopt (server->sock, SOL_SOCKET, SO_REUSEADDR,
(void *) &set, sizeof (set)) < 0)
{
weechat_printf (NULL,
_("%s%s: cannot set socket option \"%s\" to %d: "
"error %d %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
"SO_REUSEADDR", set, errno, strerror (errno));
close (server->sock);
server->sock = -1;
return 0;
}
/* set option SO_KEEPALIVE to 1 */
set = 1;
if (setsockopt (server->sock, SOL_SOCKET, SO_KEEPALIVE,
(void *) &set, sizeof (set)) < 0)
{
weechat_printf (NULL,
_("%s%s: cannot set socket option \"%s\" to %d: "
"error %d %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
"SO_KEEPALIVE", set, errno, strerror (errno));
close (server->sock);
server->sock = -1;
return 0;
}
/* bind */
if (bind (server->sock, (struct sockaddr *)ptr_addr, addr_size) < 0)
{
weechat_printf (NULL,
_("%s%s: cannot \"bind\" on port %d (%s): error %d %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
server->port, server->protocol_string,
errno, strerror (errno));
close (server->sock);
server->sock = -1;
return 0;
}
max_clients = weechat_config_integer (relay_config_network_max_clients);
if (listen (server->sock, max_clients) != 0)
{
weechat_printf (NULL,
_("%s%s: cannot \"listen\" on port %d (%s): error %d %s"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME,
server->port, server->protocol_string,
errno, strerror (errno));
close (server->sock);
server->sock = -1;
return 0;
}
weechat_printf (NULL,
_("%s: listening on port %d (relay: %s, %s, max %d clients)"),
RELAY_PLUGIN_NAME,
server->port,
server->protocol_string,
((server->ipv4 && server->ipv6) ? "IPv4+6" : ((server->ipv6) ? "IPv6" : "IPv4")),
max_clients);
server->hook_fd = weechat_hook_fd (server->sock,
1, 0, 0,
&relay_server_sock_cb,
server);
server->start_time = time (NULL);
return 1;
}
/*
* Adds a socket relaying on a port.
*
* Returns pointer to new server, NULL if error.
*/
struct t_relay_server *
relay_server_new (const char *protocol_string, enum t_relay_protocol protocol,
const char *protocol_args, int port, int ipv4, int ipv6,
int ssl)
{
struct t_relay_server *new_server;
if (!protocol_string)
return NULL;
if (relay_server_search_port (port))
{
weechat_printf (NULL, _("%s%s: error: port \"%d\" is already used"),
weechat_prefix ("error"),
RELAY_PLUGIN_NAME, port);
return NULL;
}
new_server = malloc (sizeof (*new_server));
if (new_server)
{
new_server->protocol_string = strdup (protocol_string);
new_server->protocol = protocol;
new_server->protocol_args =
(protocol_args) ? strdup (protocol_args) : NULL;
new_server->port = port;
new_server->ipv4 = ipv4;
new_server->ipv6 = ipv6;
new_server->ssl = ssl;
new_server->sock = -1;
new_server->hook_fd = NULL;
new_server->start_time = 0;
new_server->last_client_disconnect = 0;
relay_server_create_socket (new_server);
new_server->prev_server = NULL;
new_server->next_server = relay_servers;
if (relay_servers)
relay_servers->prev_server = new_server;
else
last_relay_server = new_server;
relay_servers = new_server;
}
else
{
weechat_printf (NULL,
_("%s%s: not enough memory for listening on new port"),
weechat_prefix ("error"), RELAY_PLUGIN_NAME);
}
return new_server;
}
/*
* Updates port in a server.
*/
void
relay_server_update_port (struct t_relay_server *server, int port)
{
if (port != server->port)
{
relay_server_close_socket (server);
server->port = port;
relay_server_create_socket (server);
}
}
/*
* Removes a server.
*/
void
relay_server_free (struct t_relay_server *server)
{
struct t_relay_server *new_relay_servers;
if (!server)
return;
/* remove server from list */
if (last_relay_server == server)
last_relay_server = server->prev_server;
if (server->prev_server)
{
(server->prev_server)->next_server = server->next_server;
new_relay_servers = relay_servers;
}
else
new_relay_servers = server->next_server;
if (server->next_server)
(server->next_server)->prev_server = server->prev_server;
/* free data */
relay_server_close_socket (server);
if (server->protocol_string)
free (server->protocol_string);
if (server->protocol_args)
free (server->protocol_args);
free (server);
relay_servers = new_relay_servers;
}
/*
* Removes all servers.
*/
void
relay_server_free_all ()
{
while (relay_servers)
{
relay_server_free (relay_servers);
}
}
/*
* Adds a server in an infolist.
*
* Returns:
* 1: OK
* 0: error
*/
int
relay_server_add_to_infolist (struct t_infolist *infolist,
struct t_relay_server *server)
{
struct t_infolist_item *ptr_item;
if (!infolist || !server)
return 0;
ptr_item = weechat_infolist_new_item (infolist);
if (!ptr_item)
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "protocol_string", server->protocol_string))
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "protocol", server->protocol))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "protocol_args", server->protocol_args))
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "port", server->port))
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "ipv4", server->ipv4))
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "ipv6", server->ipv6))
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "ssl", server->ssl))
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "sock", server->sock))
return 0;
if (!weechat_infolist_new_var_pointer (ptr_item, "hook_fd", server->hook_fd))
return 0;
if (!weechat_infolist_new_var_time (ptr_item, "start_time", server->start_time))
return 0;
if (!weechat_infolist_new_var_time (ptr_item, "last_client_disconnect", server->last_client_disconnect))
return 0;
return 1;
}
/*
* Prints servers in WeeChat log file (usually for crash dump).
*/
void
relay_server_print_log ()
{
struct t_relay_server *ptr_server;
for (ptr_server = relay_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
weechat_log_printf ("");
weechat_log_printf ("[relay server (addr:0x%lx)]", ptr_server);
weechat_log_printf (" protocol_string . . . : '%s'", ptr_server->protocol_string);
weechat_log_printf (" protocol. . . . . . . : %d (%s)",
ptr_server->protocol,
relay_protocol_string[ptr_server->protocol]);
weechat_log_printf (" protocol_args . . . . : '%s'", ptr_server->protocol_args);
weechat_log_printf (" port. . . . . . . . . : %d", ptr_server->port);
weechat_log_printf (" ipv4. . . . . . . . . : %d", ptr_server->ipv4);
weechat_log_printf (" ipv6. . . . . . . . . : %d", ptr_server->ipv6);
weechat_log_printf (" ssl . . . . . . . . . : %d", ptr_server->ssl);
weechat_log_printf (" sock. . . . . . . . . : %d", ptr_server->sock);
weechat_log_printf (" hook_fd . . . . . . . : 0x%lx", ptr_server->hook_fd);
weechat_log_printf (" start_time. . . . . . : %ld", ptr_server->start_time);
weechat_log_printf (" last_client_disconnect: %ld", ptr_server->last_client_disconnect);
weechat_log_printf (" prev_server . . . . . : 0x%lx", ptr_server->prev_server);
weechat_log_printf (" next_server . . . . . : 0x%lx", ptr_server->next_server);
}
}
|