summaryrefslogtreecommitdiff
path: root/src/plugins/relay/relay-server.c
blob: 2a25b6a2f7b972835821da30a68b340cc6b071a8 (plain)
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
/*
 * Copyright (C) 2003-2011 Sebastien 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/>.
 */

/*
 * relay-server.c: server functions for relay plugin
 */

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.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;


/*
 * relay_server_get_protocol_args: get protocol and arguments from a string
 *                                 with format "protocol.args"
 *                                 Note: *protocol and *protocol_args must be
 *                                 freed after use
 */

void
relay_server_get_protocol_args (const char *protocol_and_args,
                                char **protocol, char **protocol_args)
{
    char *pos;
    
    pos = strchr (protocol_and_args, '.');
    if (pos)
    {
        *protocol = weechat_strndup (protocol_and_args,
                                     pos - protocol_and_args);
        *protocol_args = strdup (pos + 1);
    }
    else
    {
        *protocol = strdup (protocol_and_args);
        *protocol_args = strdup ("*");
    }
}

/*
 * relay_server_search: search server by protocol.args
 */

struct t_relay_server *
relay_server_search (const char *protocol_and_args)
{
    char *protocol, *protocol_args;
    struct t_relay_server *ptr_server;
    
    relay_server_get_protocol_args (protocol_and_args,
                                    &protocol, &protocol_args);
    
    ptr_server = NULL;
    
    if (protocol && protocol_args)
    {
        for (ptr_server = relay_servers; ptr_server;
             ptr_server = ptr_server->next_server)
        {
            if ((strcmp (protocol, relay_protocol_string[ptr_server->protocol]) == 0)
                && (strcmp (protocol_args, ptr_server->protocol_args) == 0))
            {
                break;
            }
        }
    }
    
    if (protocol)
        free (protocol);
    if (protocol_args)
        free (protocol_args);
    
    return ptr_server;
}

/*
 * relay_server_search_port: search server by port
 */

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;
}

/*
 * relay_server_close_socket: close 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.%s (port %d)"),
                            RELAY_PLUGIN_NAME,
                            relay_protocol_string[server->protocol],
                            server->protocol_args,
                            server->port);
        }
    }
}

/*
 * relay_server_sock_cb: read 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;
    unsigned int client_length;
    int client_fd;
    char ipv4_address[INET_ADDRSTRLEN + 1], *ptr_address;
    
    /* make C compiler happy */
    (void) fd;
    
    server = (struct t_relay_server *)data;
    
    client_length = sizeof (client_addr);
    memset (&client_addr, 0, client_length);
    
    client_fd = accept (server->sock, (struct sockaddr *) &client_addr,
                        &client_length);
    if (client_fd < 0)
    {
        weechat_printf (NULL,
                        _("%s%s: cannot accept client on port %d (%s.%s)"),
                        weechat_prefix ("error"),
                        RELAY_PLUGIN_NAME,
                        server->port,
                        relay_protocol_string[server->protocol],
                        server->protocol_args);
        return WEECHAT_RC_OK;
    }
    
    ptr_address = NULL;
    if (inet_ntop (AF_INET,
                   &(client_addr.sin_addr),
                   ipv4_address,
                   INET_ADDRSTRLEN))
    {
        ptr_address = ipv4_address;
    }
    
    relay_client_new (client_fd, ptr_address, server);
    
    return WEECHAT_RC_OK;
}

/*
 * relay_server_create_socket: create socket and server on port
 */

int
relay_server_create_socket (struct t_relay_server *server)
{
    int set, max_clients;
    struct sockaddr_in server_addr;
    
    server->sock = socket (AF_INET, SOCK_STREAM, 0);
    if (server->sock < 0)
    {
        weechat_printf (NULL,
                        _("%s%s: cannot create socket"),
                        weechat_prefix ("error"), RELAY_PLUGIN_NAME);
        return 0;
    }
    
    set = 1;
    if (setsockopt (server->sock, SOL_SOCKET, SO_REUSEADDR,
                    (void *) &set, sizeof (set)) < 0)
    {
        weechat_printf (NULL,
                        _("%s%s: cannot set socket option "
                          "\"SO_REUSEADDR\""),
                        weechat_prefix ("error"), RELAY_PLUGIN_NAME);
        close (server->sock);
        server->sock = -1;
        return 0;
    }
    
    set = 1;
    if (setsockopt (server->sock, SOL_SOCKET, SO_KEEPALIVE,
                    (void *) &set, sizeof (set)) < 0)
    {
        weechat_printf (NULL,
                        _("%s%s: cannot set socket option "
                          "\"SO_KEEPALIVE\""),
                        weechat_prefix ("error"), RELAY_PLUGIN_NAME);
        close (server->sock);
        server->sock = -1;
        return 0;
    }
    
    memset(&server_addr, 0, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    if (weechat_config_string (relay_config_network_bind_address)
        && weechat_config_string (relay_config_network_bind_address)[0])
    {
        server_addr.sin_addr.s_addr = inet_addr (weechat_config_string (relay_config_network_bind_address));
    }
    else
    {
        server_addr.sin_addr.s_addr = INADDR_ANY;
    }
    server_addr.sin_port = htons (server->port);
    
    if (bind (server->sock, (struct sockaddr *) &server_addr,
              sizeof (server_addr)) < 0)
    {
        weechat_printf (NULL,
                        _("%s%s: error with \"bind\" on port %d (%s.%s)"),
                        weechat_prefix ("error"), RELAY_PLUGIN_NAME,
                        server->port,
                        relay_protocol_string[server->protocol],
                        server->protocol_args);
        close (server->sock);
        server->sock = -1;
        return 0;
    }
    
    max_clients = weechat_config_integer (relay_config_network_max_clients);
    
    listen (server->sock, max_clients);
    
    weechat_printf (NULL,
                    _("%s: listening on port %d (relay: %s.%s, max %d clients)"),
                    RELAY_PLUGIN_NAME,
                    server->port,
                    relay_protocol_string[server->protocol],
                    server->protocol_args,
                    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;
}

/*
 * relay_server_new: add a socket relaying on a port
 */

struct t_relay_server *
relay_server_new (enum t_relay_protocol protocol,
                  const char *protocol_args,
                  int port)
{
    struct t_relay_server *new_server;

    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 = protocol;
        new_server->protocol_args =
            (protocol_args) ? strdup (protocol_args) : strdup ("*");
        new_server->port = port;
        new_server->sock = -1;
        new_server->hook_fd = NULL;
        new_server->start_time = 0;
        
        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;
        
        relay_server_create_socket (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;
}

/*
 * relay_server_update_port: update 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);
    }
}

/*
 * relay_server_free: remove 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_args)
        free (server->protocol_args);
    
    free (server);
    
    relay_servers = new_relay_servers;
}

/*
 * relay_server_free_all: remove all servers
 */

void
relay_server_free_all ()
{
    while (relay_servers)
    {
        relay_server_free (relay_servers);
    }
}

/*
 * relay_server_print_log: print server infos in log (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. . . . . . : %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 ("  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 ("  prev_server . . . . : 0x%lx", ptr_server->prev_server);
        weechat_log_printf ("  next_server . . . . : 0x%lx", ptr_server->next_server);
    }
}