forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_info.h
470 lines (414 loc) · 12.3 KB
/
socket_info.h
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
/*
* 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
*/
/*!
* \file
* \brief Find & manage listen addresses.
* Contains code that initializes and handles server listen addresses
* lists (struct socket_info). It is used mainly on startup.
*/
#ifndef socket_info_h
#define socket_info_h
#include <stdlib.h>
#include "ip_addr.h"
#include "dprint.h"
#include "globals.h"
#include "net/trans.h"
#include "ut.h"
struct socket_info {
int socket;
str name; /*!< name - eg.: foo.bar or 10.0.0.1 */
struct ip_addr address; /*!< ip address */
str address_str; /*!< ip address converted to string -- optimization*/
unsigned short port_no; /*!< port number */
str port_no_str; /*!< port number converted to string -- optimization*/
enum si_flags flags; /*!< SI_IS_IP | SI_IS_LO | SI_IS_MCAST | SI_IS_ANYCAST */
union sockaddr_union su;
int proto; /*!< tcp or udp*/
str sock_str;
str adv_sock_str;
str adv_name_str; /* Advertised name of this interface */
str adv_port_str; /* Advertised port of this interface */
struct ip_addr adv_address; /* Advertised address in ip_addr form (for find_si) */
unsigned short adv_port; /* optimization for grep_sock_info() */
unsigned short children;
/* these are IP-level local/remote ports used during the last write op via
* this sock (or a connection belonging to this sock). These values are
* optional (populated only by the TCP-based protocol, for ephemeral ports.
* Note: they are populate ONLY by a write op and they are not ever reset,
* they are simply overwritten by the next write op on this socket/conn.
* IMPORTANT: when reading them, be sure you are just after a write ops,
* otherwise you may read old data here */
unsigned short last_local_real_port;
unsigned short last_remote_real_port;
struct socket_info* next;
struct socket_info* prev;
};
#define NUM_IP_OCTETS 4
#define PROTO_NAME_MAX_SIZE 8 /* CHANGEME if you define a bigger protocol name
* currently hep_tcp - biggest proto */
int add_listen_iface(char* name, unsigned short port, unsigned short proto,
char *adv_name, unsigned short adv_port,
unsigned short children, enum si_flags flags);
struct socket_info *
new_sock_info(char* name, unsigned short port, unsigned short proto,
char *adv_name, unsigned short adv_port,
unsigned short children, enum si_flags flags);
int new_sock2list(char* name, unsigned short port, unsigned short proto,
char *adv_name, unsigned short adv_port, unsigned short children,
enum si_flags flags, struct socket_info** list);
int fix_socket_list(struct socket_info **);
/*
* This function will retrieve a list of all ip addresses and ports that
* OpenSIPS is listening on, with respect to the transport protocol specified
* with 'protocol'.
*
* The first parameter, ipList, is a pointer to a pointer. It will be assigned
* new block of memory holding the IP Addresses and ports being listened to
* with respect to 'protocol'. The array maps a 2D array into a 1 dimensional
* space, and is layed out as follows:
*
* The first NUM_IP_OCTETS indices will be the IP address, and the next index
* the port. So if NUM_IP_OCTETS is equal to 4 and there are two IP addresses
* found, then:
*
* - ipList[0] will be the first octet of the first ip address
* - ipList[3] will be the last octet of the first ip address.
* - iplist[4] will be the port of the first ip address
* -
* - iplist[5] will be the first octet of the first ip address,
* - and so on.
*
* The function will return the number of sockets which were found. This can
* be used to index into ipList.
*
* NOTE: This function assigns a block of memory equal to:
*
* returnedValue * (NUM_IP_OCTETS + 1) * sizeof(int);
*
* Therefore it is CRUCIAL that you free ipList when you are done with
* its contents, to avoid a nasty memory leak.
*/
int get_socket_list_from_proto(unsigned int **ipList, int protocol);
/*
* Returns the sum of the number of bytes waiting to be consumed on all network
* interfaces and transports that OpenSIPS is listening on.
*
* Note: This currently only works on systems supporting the
* /proc/net/[tcp|udp] interface. On other systems, zero will always
* be returned. Details of why this is so can be found in
* network_stats.c
*/
int get_total_bytes_waiting(int only_proto);
void print_aliases();
struct socket_info* grep_sock_info(str* host, unsigned short port,
unsigned short proto);
struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
unsigned short proto);
#define set_sip_defaults( _port, _proto) \
do { \
if (_proto==PROTO_NONE) _proto = PROTO_UDP; \
if (_port==0) { \
if (_proto==PROTO_TLS) _port = SIPS_PORT; else\
_port = SIP_PORT; \
} \
} while(0)
/*! \brief helper function:
* \return next protocol, if the last one is reached return 0
* \note useful for cycling on the supported protocols */
static inline int next_proto(unsigned short proto)
{
for( proto++ ; proto<PROTO_LAST ; proto++ )
if (protos[proto].id!=PROTO_NONE)
return proto;
return PROTO_NONE;
}
/*! \brief gets first non-null socket_info structure
* (useful if for. e.g we are not listening on any udp sockets )
*/
inline static struct socket_info* get_first_socket(void)
{
int p;
for( p=0 ; p<PROTO_LAST ; p++ )
if (protos[p].listeners)
return protos[p].listeners;
return NULL;
}
/*! \brief Sets protocol
* \return -1 on error, 0 on success
*/
inline static int parse_proto(unsigned char* s, long len, int* proto)
{
#define PROTO2UINT(a, b, c) (( (((unsigned int)(a))<<16)+ \
(((unsigned int)(b))<<8)+ \
((unsigned int)(c)) ) | 0x20202020)
unsigned int i;
unsigned int j;
/* must support 2-char arrays for ws
* must support 3-char arrays for udp, tcp, tls, wss
* must support 4-char arrays for sctp
* must support 7-char arrays for hep_tcp and hep_udp */
*proto=PROTO_NONE;
if ((len < 2 || len > 4) && len != 7) return -1;
i=PROTO2UINT(s[0], s[1], s[2]);
switch(i){
case PROTO2UINT('u', 'd', 'p'):
if(len==3) { *proto=PROTO_UDP; return 0; }
break;
case PROTO2UINT('t', 'c', 'p'):
if(len==3) { *proto=PROTO_TCP; return 0; }
break;
case PROTO2UINT('t', 'l', 's'):
if(len==3) { *proto=PROTO_TLS; return 0; }
break;
case PROTO2UINT('s', 'c', 't'):
if(len==4 && (s[3]=='p' || s[3]=='P')) {
*proto=PROTO_SCTP; return 0;
}
break;
case PROTO2UINT('w', 's', 's'):
if(len==3) { *proto=PROTO_WSS; return 0; }
break;
case PROTO2UINT('b', 'i', 'n'):
if(len==3) { *proto=PROTO_BIN; return 0; }
break;
case PROTO2UINT('h', 'e', 'p'):
if (len != 7 || s[3] != '_') return -1;
j=PROTO2UINT(s[4], s[5], s[6]);
switch (j) {
case PROTO2UINT('u','d', 'p'):
*proto=PROTO_HEP_UDP;
return 0;
case PROTO2UINT('t','c', 'p'):
*proto=PROTO_HEP_TCP;
return 0;
default:
return -1;
}
break;
default:
if(len==2 && (s[0]|0x20)=='w' && (s[1]|0x20)=='s') {
*proto=PROTO_WS; return 0;
}
return -1;
}
return -1;
}
/*! \brief
* parses [proto:]host[:port] where proto= udp|tcp|tls
* \return 0 on success and -1 on failure
*/
inline static int parse_phostport(char* s, int slen, char** host, int* hlen,
int* port, int* proto)
{
char* first; /* first ':' occurrence */
char* second; /* second ':' occurrence */
char* p;
int bracket;
str tmp;
char* end;
first=second=0;
bracket=0;
end = s + slen;
/* find the first 2 ':', ignoring possible ipv6 addresses
* (substrings between [])
*/
for(p=s; p<end ; p++){
switch(*p){
case '[':
bracket++;
if (bracket>1) goto error_brackets;
break;
case ']':
bracket--;
if (bracket<0) goto error_brackets;
break;
case ':':
if (bracket==0){
if (first==0) first=p;
else if( second==0) second=p;
else goto error_colons;
}
break;
}
}
if (p==s) return -1;
if (*(p-1)==':') goto error_colons;
if (first==0){ /* no ':' => only host */
*host=s;
*hlen=(int)(p-s);
*port=0;
*proto=0;
return 0;
}
if (second){ /* 2 ':' found => check if valid */
if (parse_proto((unsigned char*)s, first-s, proto)<0)
goto error_proto;
tmp.s = second+1;
tmp.len = end - tmp.s;
if (str2int( &tmp, (unsigned int*)port )==-1) goto error_port;
*host=first+1;
*hlen=(int)(second-*host);
return 0;
}
/* only 1 ':' found => it's either proto:host or host:port */
tmp.s = first+1;
tmp.len = end - tmp.s;
if (str2int( &tmp, (unsigned int*)port )==-1) {
/* invalid port => it's proto:host */
if (parse_proto((unsigned char*)s, first-s, proto)<0) goto error_proto;
*port=0;
*host=first+1;
*hlen=(int)(p-*host);
}else{
/* valid port => its host:port */
*proto=0;
*host=s;
*hlen=(int)(first-*host);
}
return 0;
error_brackets:
LM_ERR("too many brackets in %s\n", s);
return -1;
error_colons:
LM_ERR(" too many colons in %s\n", s);
return -1;
error_proto:
LM_ERR("bad protocol in %s\n", s);
return -1;
error_port:
LM_ERR("bad port number in %s\n", s);
return -1;
}
/* function will write the proto as string, starting from the p pointer. The
new resulting proto will be returned (where writing ended) */
static inline char* proto2str(int proto, char *p)
{
switch (proto) {
case PROTO_UDP:
*(p++) = 'u';
*(p++) = 'd';
*(p++) = 'p';
break;
case PROTO_TCP:
*(p++) = 't';
*(p++) = 'c';
*(p++) = 'p';
break;
case PROTO_TLS:
*(p++) = 't';
*(p++) = 'l';
*(p++) = 's';
break;
case PROTO_SCTP:
*(p++) = 's';
*(p++) = 'c';
*(p++) = 't';
*(p++) = 'p';
break;
case PROTO_WS:
*(p++) = 'w';
*(p++) = 's';
break;
case PROTO_WSS:
*(p++) = 'w';
*(p++) = 's';
*(p++) = 's';
break;
case PROTO_BIN:
*(p++) = 'b';
*(p++) = 'i';
*(p++) = 'n';
break;
case PROTO_HEP_UDP:
*(p++) = 'h';
*(p++) = 'e';
*(p++) = 'p';
*(p++) = '_';
*(p++) = 'u';
*(p++) = 'd';
*(p++) = 'p';
break;
case PROTO_HEP_TCP:
*(p++) = 'h';
*(p++) = 'e';
*(p++) = 'p';
*(p++) = '_';
*(p++) = 't';
*(p++) = 'c';
*(p++) = 'p';
break;
default:
LM_CRIT("unsupported proto %d\n", proto);
}
return p;
}
static inline char *proto2a(int proto)
{
static char b[8]; /* IMPORTANT - keep this max aligned with the proto2str
* with an extra +1 for NULL terminator */
char *p;
/* print the proto name */
p = proto2str( proto, b);
/* make it null terminated */
*p = '\0';
return b;
}
#define MAX_SOCKET_STR ( 4 + 1 + IP_ADDR_MAX_STR_SIZE+1+INT2STR_MAX_LEN+1)
#define sock_str_len(_sock) ( 3 + 1*((_sock)->proto==PROTO_SCTP) + 1 + \
(_sock)->address_str.len + 1 + (_sock)->port_no_str.len)
static inline char* socket2str(struct socket_info *sock, char *s, int* len, int adv)
{
static char buf[MAX_SOCKET_STR];
char *p,*p1;
if (s) {
/* buffer provided -> check lenght */
if ( sock_str_len(sock) > *len ) {
LM_ERR("buffer too short\n");
return 0;
}
p = p1 = s;
} else {
p = p1 = buf;
}
p = proto2str( sock->proto, p);
if (p==NULL) return 0;
*(p++) = ':';
if(adv) {
memcpy( p, sock->adv_name_str.s, sock->adv_name_str.len);
p += sock->adv_name_str.len;
*(p++) = ':';
memcpy( p, sock->adv_port_str.s, sock->adv_port_str.len);
p += sock->adv_port_str.len;
} else {
memcpy( p, sock->address_str.s, sock->address_str.len);
p += sock->address_str.len;
*(p++) = ':';
memcpy( p, sock->port_no_str.s, sock->port_no_str.len);
p += sock->port_no_str.len;
}
*len = (int)(long)(p-p1);
LM_DBG("<%.*s>\n",*len,p1);
return p1;
}
#define get_sock_info_list(_proto) \
((_proto>=PROTO_FIRST && _proto<PROTO_LAST)?(&protos[_proto].listeners):0)
int probe_max_sock_buff( int sock, int buff_choice, int buff_max,
int buff_increment);
#endif