/
net_udp.c
467 lines (400 loc) · 11.7 KB
/
net_udp.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
/*
* Copyright (C) 2014-2015 OpenSIPS Foundation
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of opensips, a free SIP server.
*
* opensips 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 2 of the License, or
* (at your option) any later version
*
* opensips 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* History:
* -------
* 2015-02-09 first version (bogdan)
*/
#include <unistd.h>
#include "../pt.h"
#include "../daemonize.h"
#include "../reactor.h"
#include "../timer.h"
#include "net_udp.h"
#define UDP_SELECT_TIMEOUT 1
/* if the UDP network layer is used or not by some protos */
static int udp_disabled = 1;
extern void handle_sigs(void);
/* initializes the UDP network layer */
int udp_init(void)
{
unsigned int i;
/* first we do auto-detection to see if there are any UDP based
* protocols loaded */
for ( i=PROTO_FIRST ; i<PROTO_LAST ; i++ )
if (is_udp_based_proto(i)) {udp_disabled=0;break;}
return 0;
}
/* destroys the UDP network layer */
void udp_destroy(void)
{
return;
}
/* tells how many processes the UDP layer will create */
int udp_count_processes(void)
{
struct socket_info *si;
int n, i;
if (udp_disabled)
return 0;
for( i=0,n=0 ; i<PROTO_LAST ; i++)
if (protos[i].id!=PROTO_NONE && is_udp_based_proto(i))
for( si=protos[i].listeners ; si; si=si->next)
n+=si->children;
return n;
}
#ifdef USE_MCAST
/**
* Setup a multicast receiver socket, supports IPv4 and IPv6.
* \param sock socket
* \param addr receiver address
* \return zero on success, -1 otherwise
*/
static int setup_mcast_rcvr(int sock, union sockaddr_union* addr)
{
struct ip_mreq mreq;
struct ipv6_mreq mreq6;
if (addr->s.sa_family==AF_INET){
memcpy(&mreq.imr_multiaddr, &addr->sin.sin_addr,
sizeof(struct in_addr));
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq,
sizeof(mreq))==-1){
LM_ERR("setsockopt: %s\n", strerror(errno));
return -1;
}
} else if (addr->s.sa_family==AF_INET6){
memcpy(&mreq6.ipv6mr_multiaddr, &addr->sin6.sin6_addr,
sizeof(struct in6_addr));
mreq6.ipv6mr_interface = 0;
#ifdef __OS_linux
if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6,
#else
if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
#endif
sizeof(mreq6))==-1){
LM_ERR("setsockopt:%s\n", strerror(errno));
return -1;
}
} else {
LM_ERR("unsupported protocol family\n");
return -1;
}
return 0;
}
#endif /* USE_MCAST */
/**
* Initialize a UDP socket, supports multicast, IPv4 and IPv6.
* \param si socket that should be bind
* \return zero on success, -1 otherwise
*
* @status_flags - extra status flags to be set for the socket fd
*/
int udp_init_listener(struct socket_info *si, int status_flags)
{
union sockaddr_union* addr;
int optval;
#ifdef USE_MCAST
unsigned char m_optval;
#endif
addr=&si->su;
si->proto=PROTO_UDP;
if (init_su(addr, &si->address, si->port_no)<0){
LM_ERR("could not init sockaddr_union\n");
goto error;
}
si->socket = socket(AF2PF(addr->s.sa_family), SOCK_DGRAM, 0);
if (si->socket==-1){
LM_ERR("socket: %s\n", strerror(errno));
goto error;
}
/* make socket non-blocking */
if (status_flags) {
optval=fcntl(si->socket, F_GETFL);
if (optval==-1){
LM_ERR("fnctl failed: (%d) %s\n", errno, strerror(errno));
goto error;
}
if (fcntl(si->socket,F_SETFL,optval|status_flags)==-1){
LM_ERR("set non-blocking failed: (%d) %s\n",
errno, strerror(errno));
goto error;
}
}
/* set sock opts? */
optval=1;
if (setsockopt(si->socket, SOL_SOCKET, SO_REUSEADDR ,
(void*)&optval, sizeof(optval)) ==-1){
LM_ERR("setsockopt: %s\n", strerror(errno));
goto error;
}
/* tos */
optval=tos;
if (setsockopt(si->socket, IPPROTO_IP, IP_TOS, (void*)&optval,
sizeof(optval)) ==-1){
LM_WARN("setsockopt tos: %s\n", strerror(errno));
/* continue since this is not critical */
}
#if defined (__linux__) && defined(UDP_ERRORS)
optval=1;
/* enable error receiving on unconnected sockets */
if(setsockopt(si->socket, SOL_IP, IP_RECVERR,
(void*)&optval, sizeof(optval)) ==-1){
LM_ERR("setsockopt: %s\n", strerror(errno));
goto error;
}
#endif
#ifdef USE_MCAST
if ((si->flags & SI_IS_MCAST)
&& (setup_mcast_rcvr(si->socket, addr)<0)){
goto error;
}
/* set the multicast options */
if (addr->s.sa_family==AF_INET){
m_optval = mcast_loopback;
if (setsockopt(si->socket, IPPROTO_IP, IP_MULTICAST_LOOP,
&m_optval, sizeof(m_optval))==-1){
LM_WARN("setsockopt(IP_MULTICAST_LOOP): %s\n", strerror(errno));
/* it's only a warning because we might get this error if the
network interface doesn't support multicasting */
}
if (mcast_ttl>=0){
m_optval = mcast_ttl;
if (setsockopt(si->socket, IPPROTO_IP, IP_MULTICAST_TTL,
&m_optval, sizeof(m_optval))==-1){
LM_ERR("setsockopt (IP_MULTICAST_TTL): %s\n", strerror(errno));
goto error;
}
}
} else if (addr->s.sa_family==AF_INET6){
if (setsockopt(si->socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
&mcast_loopback, sizeof(mcast_loopback))==-1){
LM_WARN("setsockopt (IPV6_MULTICAST_LOOP): %s\n", strerror(errno));
/* it's only a warning because we might get this error if the
network interface doesn't support multicasting */
}
if (mcast_ttl>=0){
if (setsockopt(si->socket, IPPROTO_IP, IPV6_MULTICAST_HOPS,
&mcast_ttl, sizeof(mcast_ttl))==-1){
LM_ERR("setssckopt (IPV6_MULTICAST_HOPS): %s\n",
strerror(errno));
goto error;
}
}
} else {
LM_ERR("unsupported protocol family %d\n", addr->s.sa_family);
goto error;
}
#endif /* USE_MCAST */
if (probe_max_sock_buff(si->socket,0,MAX_RECV_BUFFER_SIZE,
BUFFER_INCREMENT)==-1) goto error;
if (bind(si->socket, &addr->s, sockaddru_len(*addr))==-1){
LM_ERR("bind(%x, %p, %d) on %s: %s\n", si->socket, &addr->s,
(unsigned)sockaddru_len(*addr), si->address_str.s,
strerror(errno));
if (addr->s.sa_family==AF_INET6)
LM_ERR("might be caused by using a link "
" local address, try site local or global\n");
goto error;
}
return 0;
error:
return -1;
}
inline static int handle_io(struct fd_map* fm, int idx,int event_type)
{
int n,read;
switch(fm->type){
case F_UDP_READ:
update_stat( pt[process_no].load, +1 );
n = protos[((struct socket_info*)fm->data)->proto].net.
read( fm->data /*si*/, &read);
update_stat( pt[process_no].load, -1 );
return n;
case F_TIMER_JOB:
handle_timer_job();
return 0;
case F_SCRIPT_ASYNC:
async_resume_f( fm->fd, fm->data);
return 0;
default:
LM_CRIT("uknown fd type %d in UDP worker\n", fm->type);
return -1;
}
return -1;
}
/**
* Main UDP receiver loop, processes data from the network, does some error
* checking and save it in an allocated buffer. This data is then forwarded
* to the receive_msg function. If an dynamic buffer is used, the buffer
* must be freed in later steps.
* \see receive_msg
* \see main_loop
* \return -1 for errors
*/
int udp_rcv_loop( struct socket_info *si )
{
/* create the reactor for UDP proc */
if ( init_worker_reactor("UDP_worker",open_files_limit,RCT_PRIO_MAX)<0 ) {
LM_ERR("failed to init reactor\n");
goto error;
}
/* init: start watching for the timer jobs */
if (reactor_add_reader( timer_fd_out, F_TIMER_JOB, RCT_PRIO_TIMER,NULL)<0){
LM_CRIT("failed to add timer pipe_out to reactor\n");
goto error;
}
/* init: start watching the SIP UDP fd */
if (reactor_add_reader( si->socket, F_UDP_READ, RCT_PRIO_NET, si)<0) {
LM_CRIT("failed to add UDP listen socket to reactor\n");
goto error;
}
/* launch the reactor */
reactor_main_loop( UDP_SELECT_TIMEOUT, error , );
error:
destroy_worker_reactor();
return -1;
}
int udp_init_nofork(void)
{
struct socket_info *si;
stat_var *load_p = NULL;
int p;
if (udp_disabled || (si=protos[PROTO_UDP].listeners)==NULL) {
LM_ERR("configuration error: no UDP listener in NO FORK mode\n");
return -1;
}
/* only one address, we ignore all the others */
if (si->next) {
LM_WARN("using only the first UDP listen address (no fork)\n");
}
for( p=PROTO_FIRST ; p<PROTO_LAST ; p++ )
if (p!=PROTO_UDP && protos[p].listeners)
LM_WARN("discarding all listen addresses for protocol %s\n",
protos[p].name);
if (udp_init_listener(si, O_NONBLOCK) < 0) {
LM_ERR("failed to init the single UDP listener\n");
return -1;
}
protos[PROTO_UDP].sendipv4 = si;
protos[PROTO_UDP].sendipv6 = si; /*FIXME*/
if (register_udp_load_stat(&si->sock_str,&load_p,1/*children*/)!=0){
LM_ERR("failed to init load statistics\n");
return -1;
}
pt[process_no].load = load_p;
return 0;
}
int udp_start_nofork(void)
{
struct socket_info *si;
int rc;
/* this was tested by udp_init_nofork !!! */
si = protos[PROTO_UDP].listeners;
/* main process, receive loop */
set_proc_attrs("stand-alone SIP receiver %.*s",
si->sock_str.len, si->sock_str.s );
bind_address = si; /* shortcut */
/* We will call child_init even if we
* do not fork - and it will be called with rank 1 because
* in fact we behave like a child, not like main process */
if (init_child(1) < 0) {
LM_ERR("init_child failed in don't fork\n");
return -1;
}
if (startup_rlist.a)
run_startup_route();
clean_write_pipeend();
LM_DBG("waiting for status code from children\n");
rc = wait_for_all_children();
if (rc < 0) {
LM_ERR("failed to successfully init children\n");
return rc;
}
return udp_rcv_loop( si );
}
/* starts all UDP related processes */
int udp_start_processes(int *chd_rank, int *startup_done)
{
struct socket_info *si;
stat_var *load_p = NULL;
pid_t pid;
int i,p;
if (udp_disabled)
return 0;
for( p=PROTO_FIRST ; p<PROTO_LAST ; p++ ) {
if ( !is_udp_based_proto(p) )
continue;
for(si=protos[p].listeners; si ; si=si->next ) {
if (register_udp_load_stat(&si->sock_str,&load_p,si->children)!=0){
LM_ERR("failed to init load statistics\n");
goto error;
}
for (i=0;i<si->children;i++) {
(*chd_rank)++;
if ( (pid=internal_fork( "UDP receiver"))<0 ) {
LM_CRIT("cannot fork UDP process\n");
goto error;
} else if (pid==0) {
/* new UDP process */
/* set a more detailed description */
set_proc_attrs("SIP receiver %.*s ",
si->sock_str.len, si->sock_str.s);
bind_address=si; /* shortcut */
if (init_child(*chd_rank) < 0) {
report_failure_status();
if (*chd_rank == 1 && startup_done)
*startup_done = -1;
exit(-1);
}
/* first UDP proc runs statup_route (if defined) */
if(*chd_rank == 1 && startup_done!=NULL) {
LM_DBG("runing startup for first UDP\n");
if(run_startup_route()< 0) {
report_failure_status();
*startup_done = -1;
LM_ERR("Startup route processing failed\n");
exit(-1);
}
*startup_done = 1;
}
report_conditional_status( (!no_daemon_mode), 0);
/* all UDP listeners on same interface
* have same SHM load pointer */
pt[process_no].load = load_p;
udp_rcv_loop( si );
exit(-1);
} else {
/*parent*/
/* wait for first proc to finish the startup route */
if (*chd_rank == 1 && startup_done)
while(!(*startup_done)) {
usleep(5);
handle_sigs();
}
}
} /* procs per listener */
} /* looping through the listeners per proto */
} /* looping through the available protos */
return 0;
error:
return -1;
}