Browse code

- added missing socket_info files

Andrei Pelinescu-Onciul authored on 24/10/2003 20:29:02
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,621 @@
0
+/* $Id$
1
+ *
2
+ * find & manage listen addresses 
3
+ *
4
+ * Copyright (C) 2001-2003 Fhg Fokus
5
+ *
6
+ * This file is part of ser, a free SIP server.
7
+ *
8
+ * ser is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * For a license to use the ser software under conditions
14
+ * other than those described here, or to purchase support for this
15
+ * software, please contact iptel.org by e-mail at the following addresses:
16
+ *    info@iptel.org
17
+ *
18
+ * ser is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License 
24
+ * along with this program; if not, write to the Free Software 
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ */
27
+/*
28
+ * This file contains code that initializes and handles ser listen addresses
29
+ * lists (struct socket_info). It is used mainly on startup.
30
+ * 
31
+ * History:
32
+ * --------
33
+ *  2003-10-22  created by andrei
34
+ */
35
+
36
+
37
+#include <string.h>
38
+#include <errno.h>
39
+#include <unistd.h>
40
+#include <sys/types.h>
41
+#include <sys/socket.h>
42
+#include <sys/utsname.h>
43
+#include <stdio.h>
44
+
45
+#include <sys/ioctl.h>
46
+#include <net/if.h>
47
+#ifdef HAVE_SYS_SOCKIO_H
48
+#include <sys/sockio.h>
49
+#endif
50
+
51
+#include "globals.h"
52
+#include "socket_info.h"
53
+#include "dprint.h"
54
+#include "mem/mem.h"
55
+#include "ut.h"
56
+#include "resolve.h"
57
+#include "name_alias.h"
58
+
59
+
60
+
61
+/* list manip. functions (internal use only) */
62
+
63
+
64
+/* append */
65
+#define sock_listadd(head, el) \
66
+	do{\
67
+		if (*(head)==0) *(head)=(el); \
68
+		else{ \
69
+			for((el)->next=*(head); (el)->next->next;\
70
+					(el)->next=(el)->next->next); \
71
+			(el)->next->next=(el); \
72
+			(el)->prev=(el)->next; \
73
+			(el)->next=0; \
74
+		}\
75
+	}while(0)
76
+
77
+
78
+/* insert after "after" */
79
+#define sock_listins(el, after) \
80
+	do{ \
81
+		if ((after)){\
82
+			(el)->next=(after)->next; \
83
+			if ((after)->next) (after)->next->prev=(el); \
84
+			(after)->next=(el); \
85
+			(el)->prev=(after); \
86
+		}else{ /* after==0 = list head */ \
87
+			(after)=(el); \
88
+			(el)->next=(el)->prev=0; \
89
+		}\
90
+	}while(0)
91
+
92
+
93
+#define sock_listrm(head, el) \
94
+	do {\
95
+		if (*(head)==(el)) *(head)=(el)->next; \
96
+		if ((el)->next) (el)->next->prev=(el)->prev; \
97
+		if ((el)->prev) (el)->prev->next=(el)->next; \
98
+	}while(0)
99
+
100
+
101
+
102
+/* another helper function, it just creates a socket_info struct */
103
+static inline struct socket_info* new_sock_info(	char* name,
104
+								unsigned short port, unsigned short proto,
105
+								enum si_flags flags)
106
+{
107
+	struct socket_info* si;
108
+	
109
+	si=(struct socket_info*) pkg_malloc(sizeof(struct socket_info));
110
+	if (si==0) goto error;
111
+	memset(si, 0, sizeof(struct socket_info));
112
+	si->name.len=strlen(name);
113
+	si->name.s=(char*)pkg_malloc(si->name.len+1); /* include \0 */
114
+	if (si->name.s==0) goto error;
115
+	memcpy(si->name.s, name, si->name.len+1);
116
+	/* set port & proto */
117
+	si->port_no=port;
118
+	si->proto=proto;
119
+	si->flags=flags;
120
+	return si;
121
+error:
122
+	LOG(L_ERR, "ERROR: new_sock_info: memory allocation error\n");
123
+	if (si) pkg_free(si);
124
+	return 0;
125
+}
126
+
127
+
128
+
129
+/*  delete a socket_info struct */
130
+static void free_sock_info(struct socket_info* si)
131
+{
132
+	if(si){
133
+		if(si->name.s) pkg_free(si->name.s);
134
+		if(si->address_str.s) pkg_free(si->address_str.s);
135
+		if(si->port_no_str.s) pkg_free(si->port_no_str.s);
136
+	}
137
+}
138
+
139
+
140
+
141
+static char* get_proto_name(unsigned short proto)
142
+{
143
+	switch(proto){
144
+		case PROTO_NONE:
145
+			return "*";
146
+		case PROTO_UDP:
147
+			return "udp";
148
+#ifdef USE_TCP
149
+		case PROTO_TCP:
150
+			return "tcp";
151
+#endif
152
+#ifdef USE_TLS
153
+		case PROTO_TLS:
154
+			return "tls";
155
+#endif
156
+		default:
157
+			return "unknown";
158
+	}
159
+}
160
+
161
+
162
+static struct socket_info** get_sock_info_list(unsigned short proto)
163
+{
164
+	
165
+	switch(proto){
166
+		case PROTO_UDP:
167
+			return &udp_listen;
168
+			break;
169
+#ifdef USE_TCP
170
+		case PROTO_TCP:
171
+			return &tcp_listen;
172
+			break;
173
+#endif
174
+#ifdef USE_TLS
175
+		case PROTO_TLS:
176
+			return &tls_listen;
177
+			break;
178
+#endif
179
+		default:
180
+			LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
181
+	}
182
+	return 0;
183
+}
184
+
185
+
186
+
187
+/* adds a new sock_info structure to the corresponding list
188
+ * return  0 on success, -1 on error */
189
+int new_sock2list(char* name, unsigned short port, unsigned short proto,
190
+						enum si_flags flags, struct socket_info** list)
191
+{
192
+	struct socket_info* si;
193
+	
194
+	si=new_sock_info(name, port, proto, flags);
195
+	if (si==0){
196
+		LOG(L_ERR, "ERROR: add_listen_iface: new_sock_info failed\n");
197
+		goto error;
198
+	}
199
+	sock_listadd(list, si);
200
+	return 0;
201
+error:
202
+	return -1;
203
+}
204
+
205
+
206
+
207
+/* adds a sock_info structure to the corresponding proto list
208
+ * return  0 on success, -1 on error */
209
+int add_listen_iface(char* name, unsigned short port, unsigned short proto,
210
+						enum si_flags flags)
211
+{
212
+	struct socket_info** list;
213
+	unsigned short c_proto;
214
+	
215
+	c_proto=(proto)?proto:PROTO_UDP;
216
+	do{
217
+		list=get_sock_info_list(c_proto);
218
+		if (list==0){
219
+			LOG(L_ERR, "ERROR: add_listen_iface: get_sock_info_list failed\n");
220
+			goto error;
221
+		}
222
+		if (new_sock2list(name, port, c_proto, flags, list)<0){
223
+			LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
224
+			goto error;
225
+		}
226
+	}while( (proto==0) && (c_proto=next_proto(c_proto)));
227
+	return 0;
228
+error:
229
+	return -1;
230
+}
231
+
232
+
233
+
234
+/* add all family type addresses of interface if_name to the socket_info array
235
+ * if if_name==0, adds all addresses on all interfaces
236
+ * WARNING: it only works with ipv6 addresses on FreeBSD
237
+ * return: -1 on error, 0 on success
238
+ */
239
+int add_interfaces(char* if_name, int family, unsigned short port,
240
+					unsigned short proto,
241
+					struct socket_info** list)
242
+{
243
+	struct ifconf ifc;
244
+	struct ifreq ifr;
245
+	struct ifreq ifrcopy;
246
+	char*  last;
247
+	char* p;
248
+	int size;
249
+	int lastlen;
250
+	int s;
251
+	char* tmp;
252
+	struct ip_addr addr;
253
+	int ret;
254
+	enum si_flags flags;
255
+	
256
+#ifdef HAVE_SOCKADDR_SA_LEN
257
+	#ifndef MAX
258
+		#define MAX(a,b) ( ((a)>(b))?(a):(b))
259
+	#endif
260
+#endif
261
+	/* ipv4 or ipv6 only*/
262
+	flags=SI_NONE;
263
+	s=socket(family, SOCK_DGRAM, 0);
264
+	ret=-1;
265
+	lastlen=0;
266
+	ifc.ifc_req=0;
267
+	for (size=10; ; size*=2){
268
+		ifc.ifc_len=size*sizeof(struct ifreq);
269
+		ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
270
+		if (ifc.ifc_req==0){
271
+			LOG(L_ERR, "ERROR: add_interfaces: memory allocation failure\n");
272
+			goto error;
273
+		}
274
+		if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
275
+			if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
276
+			LOG(L_ERR, "ERROR: add_interfaces: ioctl failed: %s\n",
277
+					strerror(errno));
278
+			goto error;
279
+		}
280
+		if  ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
281
+														   len not changed*/
282
+		lastlen=ifc.ifc_len;
283
+		/* try a bigger array*/
284
+		pkg_free(ifc.ifc_req);
285
+	}
286
+	
287
+	last=(char*)ifc.ifc_req+ifc.ifc_len;
288
+	for(p=(char*)ifc.ifc_req; p<last;
289
+			p+=(sizeof(ifr.ifr_name)+
290
+			#ifdef  HAVE_SOCKADDR_SA_LEN
291
+				MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
292
+			#else
293
+				( (ifr.ifr_addr.sa_family==AF_INET)?
294
+					sizeof(struct sockaddr_in):
295
+					((ifr.ifr_addr.sa_family==AF_INET6)?
296
+						sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
297
+			#endif
298
+				)
299
+		)
300
+	{
301
+		/* copy contents into ifr structure
302
+		 * warning: it might be longer (e.g. ipv6 address) */
303
+		memcpy(&ifr, p, sizeof(ifr));
304
+		if (ifr.ifr_addr.sa_family!=family){
305
+			/*printf("strange family %d skipping...\n",
306
+					ifr->ifr_addr.sa_family);*/
307
+			continue;
308
+		}
309
+		
310
+		/*get flags*/
311
+		ifrcopy=ifr;
312
+		if (ioctl(s, SIOCGIFFLAGS,  &ifrcopy)!=-1){ /* ignore errors */
313
+			/* ignore down ifs only if listening on all of them*/
314
+			if (if_name==0){ 
315
+				/* if if not up, skip it*/
316
+				if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
317
+			}
318
+		}
319
+		
320
+		
321
+		
322
+		if ((if_name==0)||
323
+			(strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
324
+			
325
+			/*add address*/
326
+			sockaddr2ip_addr(&addr, 
327
+					(struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
328
+			if ((tmp=ip_addr2a(&addr))==0) goto error;
329
+			/* check if loopback */
330
+			if (ifrcopy.ifr_flags & IFF_LOOPBACK) 
331
+				flags|=SI_IS_LO;
332
+			/* add it to one of the lists */
333
+			if (new_sock2list(tmp, port, proto, flags, list)!=0){
334
+				LOG(L_ERR, "ERROR: add_interfaces: new_sock2list failed\n");
335
+				goto error;
336
+			}
337
+			ret=0;
338
+		}
339
+			/*
340
+			printf("%s:\n", ifr->ifr_name);
341
+			printf("        ");
342
+			print_sockaddr(&(ifr->ifr_addr));
343
+			printf("        ");
344
+			ls_ifflags(ifr->ifr_name, family, options);
345
+			printf("\n");*/
346
+	}
347
+	pkg_free(ifc.ifc_req); /*clean up*/
348
+	close(s);
349
+	return  ret;
350
+error:
351
+	if (ifc.ifc_req) pkg_free(ifc.ifc_req);
352
+	close(s);
353
+	return -1;
354
+}
355
+
356
+
357
+
358
+/* fixes a socket list => resolve addresses, 
359
+ * interface names, fills missing members, remove duplicates */
360
+static int fix_socket_list(struct socket_info **list)
361
+{
362
+	struct socket_info* si;
363
+	struct socket_info* l;
364
+	struct socket_info* next;
365
+	char* tmp;
366
+	int len;
367
+	struct hostent* he;
368
+	char** h;
369
+	
370
+	/* try to change all the interface names into addresses
371
+	 *  --ugly hack */
372
+	
373
+	for (si=*list;si;){
374
+		next=si->next;
375
+		if (add_interfaces(si->name.s, AF_INET, si->port_no,
376
+							si->proto, list)!=-1){
377
+			/* success => remove current entry (shift the entire array)*/
378
+			sock_listrm(list, si);
379
+			free_sock_info(si);
380
+		}
381
+		si=next;
382
+	}
383
+	/* get ips & fill the port numbers*/
384
+#ifdef EXTRA_DEBUG
385
+	DBG("Listening on \n");
386
+#endif
387
+	for (si=*list;si;si=si->next){
388
+		/* fix port number, port_no should be !=0 here */
389
+		if (si->port_no==0){
390
+#ifdef USE_TLS
391
+			si->port_no= (si->proto==PROTO_TLS)?tls_port_no:port_no;
392
+#else
393
+			si->port_no= port_no;
394
+#endif
395
+		}
396
+		tmp=int2str(si->port_no, &len);
397
+		if (len>=MAX_PORT_LEN){
398
+			LOG(L_ERR, "ERROR: fix_socket_list: bad port number: %d\n", 
399
+						si->port_no);
400
+			goto error;
401
+		}
402
+		si->port_no_str.s=(char*)pkg_malloc(len+1);
403
+		if (si->port_no_str.s==0){
404
+			LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
405
+			goto error;
406
+		}
407
+		strncpy(si->port_no_str.s, tmp, len+1);
408
+		si->port_no_str.len=len;
409
+		
410
+		/* get "official hostnames", all the aliases etc. */
411
+		he=resolvehost(si->name.s);
412
+		if (he==0){
413
+			LOG(L_ERR, "ERROR: fix_socket_list: could not resolve %s\n",
414
+					si->name.s);
415
+			goto error;
416
+		}
417
+		/* check if we got the official name */
418
+		if (strcasecmp(he->h_name, si->name.s)!=0){
419
+			if (add_alias(si->name.s, si->name.len,
420
+							si->port_no, si->proto)<0){
421
+				LOG(L_ERR, "ERROR: fix_socket_list: add_alias failed\n");
422
+			}
423
+			/* change the oficial name */
424
+			pkg_free(si->name.s);
425
+			si->name.s=(char*)pkg_malloc(strlen(he->h_name)+1);
426
+			if (si->name.s==0){
427
+				LOG(L_ERR,  "ERROR: fix_socket_list: out of memory.\n");
428
+				goto error;
429
+			}
430
+			si->name.len=strlen(he->h_name);
431
+			strncpy(si->name.s, he->h_name, si->name.len+1);
432
+		}
433
+		/* add the aliases*/
434
+		for(h=he->h_aliases; h && *h; h++)
435
+			if (add_alias(*h, strlen(*h), si->port_no, si->proto)<0){
436
+				LOG(L_ERR, "ERROR: fix_socket_list: add_alias failed\n");
437
+			}
438
+		hostent2ip_addr(&si->address, he, 0); /*convert to ip_addr 
439
+														 format*/
440
+		if ((tmp=ip_addr2a(&si->address))==0) goto error;
441
+		si->address_str.s=(char*)pkg_malloc(strlen(tmp)+1);
442
+		if (si->address_str.s==0){
443
+			LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
444
+			goto error;
445
+		}
446
+		strncpy(si->address_str.s, tmp, strlen(tmp)+1);
447
+		/* set is_ip (1 if name is an ip address, 0 otherwise) */
448
+		si->address_str.len=strlen(tmp);
449
+		if 	(	(si->address_str.len==si->name.len)&&
450
+				(strncasecmp(si->address_str.s, si->name.s,
451
+								si->address_str.len)==0)
452
+			){
453
+				si->flags|=SI_IS_IP;
454
+				/* do rev. dns on it (for aliases)*/
455
+				he=rev_resolvehost(&si->address);
456
+				if (he==0){
457
+					LOG(L_WARN, "WARNING: fix_socket_list:"
458
+							" could not rev. resolve %s\n",
459
+							si->name.s);
460
+				}else{
461
+					/* add the aliases*/
462
+					if (add_alias(he->h_name, strlen(he->h_name),
463
+									si->port_no, si->proto)<0){
464
+						LOG(L_ERR, "ERROR: fix_socket_list:"
465
+									"add_alias failed\n");
466
+					}
467
+					for(h=he->h_aliases; h && *h; h++)
468
+						if (add_alias(*h,strlen(*h),si->port_no,si->proto)<0){
469
+							LOG(L_ERR, "ERROR: fix_socket_list:"
470
+									" add_alias failed\n");
471
+						}
472
+				}
473
+		}
474
+#ifdef EXTRA_DEBUG
475
+		printf("              %.*s [%s]:%s\n", si->name.len, 
476
+				si->name.s, si->address_str.s, si->port_no_str.s);
477
+#endif
478
+	}
479
+	/* removing duplicate addresses*/
480
+	for (si=*list;si; si=si->next){
481
+		for (l=si->next;l;){
482
+			next=l->next;
483
+			if ((si->port_no==l->port_no) &&
484
+				(si->address.af==l->address.af) &&
485
+				(memcmp(si->address.u.addr, l->address.u.addr, si->address.len)
486
+					== 0)
487
+				){
488
+#ifdef EXTRA_DEBUG
489
+				printf("removing duplicate %s [%s] ==  %s [%s]\n",
490
+						si->name.s, si->address_str.s,
491
+						 l->name.s, l->address_str.s);
492
+#endif
493
+				/* add the name to the alias list*/
494
+				if ((!(l->flags&& SI_IS_IP)) && (
495
+						(l->name.len!=si->name.len)||
496
+						(strncmp(l->name.s, si->name.s, si->name.len)!=0))
497
+					)
498
+					add_alias(l->name.s, l->name.len, l->port_no, l->proto);
499
+						
500
+				/* remove l*/
501
+				sock_listrm(list, l);
502
+				free_sock_info(l);
503
+			}
504
+			l=next;
505
+		}
506
+	}
507
+	return 0;
508
+error:
509
+	return -1;
510
+}
511
+
512
+
513
+
514
+/* fix all 3 socket lists
515
+ * return 0 on success, -1 on error */
516
+int fix_all_socket_lists()
517
+{
518
+	struct utsname myname;
519
+	
520
+	if ((udp_listen==0)
521
+#ifdef USE_TCP
522
+			&& (tcp_listen==0)
523
+#ifdef USE_TLS
524
+			&& (tls_listen==0)
525
+#endif
526
+#endif
527
+		){
528
+		/* get all listening ipv4 interfaces */
529
+		if (add_interfaces(0, AF_INET, 0,  PROTO_UDP, &udp_listen)==0){
530
+			/* if ok, try to add the others too */
531
+#ifdef USE_TCP
532
+			if (add_interfaces(0, AF_INET, 0,  PROTO_TCP, &tcp_listen)!=0)
533
+				goto error;
534
+#ifdef USE_TLS
535
+			if (add_interfaces(0, AF_INET, 0, PROTO_TLS, &tls_listen)!=0)
536
+				goto error;
537
+#endif
538
+#endif
539
+		}else{
540
+			/* if error fall back to get hostname */
541
+			/* get our address, only the first one */
542
+			if (uname (&myname) <0){
543
+				LOG(L_ERR, "ERROR: fix_all_socket_lists: cannot determine"
544
+						" hostname, try -l address\n");
545
+				goto error;
546
+			}
547
+			if (add_listen_iface(myname.nodename, 0, 0, 0)!=0){
548
+				LOG(L_ERR, "ERROR: fix_all_socket_lists: add_listen_iface "
549
+						"failed \n");
550
+				goto error;
551
+			}
552
+		}
553
+	}
554
+	if (fix_socket_list(&udp_listen)!=0){
555
+		LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
556
+				" udp failed\n");
557
+		goto error;
558
+	}
559
+#ifdef USE_TCP
560
+	if (fix_socket_list(&tcp_listen)!=0){
561
+		LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
562
+				" tcp failed\n");
563
+		goto error;
564
+	}
565
+#ifdef USE_TLS
566
+	if (fix_socket_list(&tls_listen)!=0){
567
+		LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
568
+				" tls failed\n");
569
+		goto error;
570
+	}
571
+#endif
572
+#endif
573
+	if ((udp_listen==0)
574
+#ifdef USE_TCP
575
+			&& (tcp_listen==0)
576
+#ifdef USE_TLS
577
+			&& (tls_listen==0)
578
+#endif
579
+#endif
580
+		){
581
+		LOG(L_ERR, "ERROR: fix_all_socket_lists: no listening sockets\n");
582
+		goto error;
583
+	}
584
+	return 0;
585
+error:
586
+	return -1;
587
+}
588
+
589
+
590
+
591
+void print_all_socket_lists()
592
+{
593
+	struct socket_info *si;
594
+	struct socket_info** list;
595
+	unsigned short proto;
596
+	
597
+	
598
+	proto=PROTO_UDP;
599
+	do{
600
+		list=get_sock_info_list(proto);
601
+		for(si=list?*list:0; si; si=si->next){
602
+			printf("             %s: %s [%s]:%s\n", get_proto_name(proto),
603
+						si->name.s, si->address_str.s, si->port_no_str.s);
604
+		}
605
+	}while((proto=next_proto(proto)));
606
+}
607
+
608
+
609
+void print_aliases()
610
+{
611
+	struct host_alias* a;
612
+
613
+	for(a=aliases; a; a=a->next) 
614
+		if (a->port)
615
+			printf("             %s: %.*s:%d\n", get_proto_name(a->proto), 
616
+					a->alias.len, a->alias.s, a->port);
617
+		else
618
+			printf("             %s: %.*s:*\n", get_proto_name(a->proto), 
619
+					a->alias.len, a->alias.s);
620
+}
0 621
new file mode 100644
... ...
@@ -0,0 +1,112 @@
0
+/* $Id$
1
+ *
2
+ * find & manage listen addresses 
3
+ *
4
+ * Copyright (C) 2001-2003 Fhg Fokus
5
+ *
6
+ * This file is part of ser, a free SIP server.
7
+ *
8
+ * ser is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * For a license to use the ser software under conditions
14
+ * other than those described here, or to purchase support for this
15
+ * software, please contact iptel.org by e-mail at the following addresses:
16
+ *    info@iptel.org
17
+ *
18
+ * ser is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License 
24
+ * along with this program; if not, write to" the Free Software 
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ */
27
+/*
28
+ * This file contains code that initializes and handles ser listen addresses
29
+ * lists (struct socket_info). It is used mainly on startup.
30
+ * 
31
+ * History:
32
+ * --------
33
+ *  2003-10-22  created by andrei
34
+ */
35
+
36
+
37
+#ifndef socket_info_h
38
+#define socket_info_h
39
+
40
+#include "ip_addr.h" 
41
+#include "dprint.h"
42
+/* struct socket_info is defined in ip_addr.h */
43
+
44
+struct socket_info* udp_listen;
45
+#ifdef USE_TCP
46
+struct socket_info* tcp_listen;
47
+#endif
48
+#ifdef USE_TLS
49
+struct socket_info* tls_listen;
50
+#endif
51
+
52
+
53
+int add_listen_iface(char* name, unsigned short port, unsigned short proto,
54
+							enum si_flags flags);
55
+int fix_all_socket_lists();
56
+void print_all_socket_lists();
57
+void print_aliases();
58
+
59
+
60
+/* helper function:
61
+ * returns next protocol, if the last one is reached return 0
62
+ * usefull for cycling on the supported protocols */
63
+static inline int next_proto(unsigned short proto)
64
+{
65
+	switch(proto){
66
+		case PROTO_NONE:
67
+			return PROTO_UDP;
68
+		case PROTO_UDP:
69
+#ifdef	USE_TCP
70
+			return PROTO_TCP;
71
+#else
72
+			return 0;
73
+#endif
74
+#ifdef USE_TCP
75
+		case PROTO_TCP:
76
+#ifdef USE_TLS
77
+			return PROTO_TLS;
78
+#else
79
+			return 0;
80
+#endif
81
+#endif
82
+#ifdef USE_TLS
83
+		case PROTO_TLS:
84
+			return 0;
85
+#endif
86
+		default:
87
+			LOG(L_ERR, "ERROR: next_proto: unknown proto %d\n", proto);
88
+	}
89
+	return 0;
90
+}
91
+
92
+
93
+
94
+/* gets first non-null socket_info structure
95
+ * (usefull if for. e.g we are not listening on any udp sockets )
96
+ */
97
+inline static struct socket_info* get_first_socket()
98
+{
99
+	if (udp_listen) return udp_listen;
100
+#ifdef USE_TCP
101
+	else if (tcp_listen) return tcp_listen;
102
+#ifdef USE_TLS
103
+	else if (tls_listen) return tls_listen;
104
+#endif
105
+#endif
106
+	return 0;
107
+}
108
+
109
+
110
+
111
+#endif