1 /*
2  * find & manage listen addresses
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio 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  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to" the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22 
23 /*! \file
24  * \brief Kamailio core :: listen address
25  * This file contains code that initializes and handles Kamailio listen addresses
26  * lists (struct socket_info). It is used mainly on startup.
27  *
28  */
29 
30 
31 #ifndef socket_info_h
32 #define socket_info_h
33 
34 #include "ip_addr.h"
35 #include "dprint.h"
36 #include "globals.h"
37 
38 /* This macro evaluates to the maximum length of string buffer needed to print
39  * the text description of any socket, not counting the terminating zero added
40  * by socket2str */
41 #define MAX_SOCKET_STR (sizeof("unknown") - 1 + IP_ADDR_MAX_STR_SIZE + \
42 	INT2STR_MAX_LEN + 2 + 2)
43 
44 /* Maximum length for advertise string of listen socket */
45 #define MAX_SOCKET_ADVERTISE_STR	511
46 
47 int socket2str(char* s, int* len, struct socket_info* si);
48 int socketinfo2str(char* s, int* len, struct socket_info* si, int mode);
49 
50 /* Helper macro that results in the default port based on the protocol */
51 #define proto_default_port(proto) ((proto==PROTO_TLS)?SIPS_PORT:SIP_PORT)
52 
53 /* struct socket_info is defined in ip_addr.h */
54 
55 extern struct socket_info* udp_listen;
56 #ifdef USE_TCP
57 extern struct socket_info* tcp_listen;
58 #endif
59 #ifdef USE_TLS
60 extern struct socket_info* tls_listen;
61 #endif
62 #ifdef USE_SCTP
63 extern struct socket_info* sctp_listen;
64 #endif
65 
66 extern enum sip_protos nxt_proto[PROTO_LAST+1];
67 
68 
69 
70 /* flags for finding out the address types */
71 #define SOCKET_T_IPV4  1
72 #define SOCKET_T_IPV6  2
73 #define SOCKET_T_UDP   4
74 #define SOCKET_T_TCP   8
75 #define SOCKET_T_TLS  16
76 #define SOCKET_T_SCTP 32
77 
78 extern int socket_types;
79 
80 void init_proto_order(void);
81 
82 int add_listen_iface(char* name, struct name_lst* nlst,
83 						unsigned short port, unsigned short proto,
84 						enum si_flags flags);
85 int add_listen_iface_name(char* name, struct name_lst* addr_l,
86 						unsigned short port, unsigned short proto, char *sockname,
87 						enum si_flags flags);
88 int add_listen_advertise_iface(char* name, struct name_lst* nlst,
89 						unsigned short port, unsigned short proto,
90 						char *useaddr, unsigned short useport,
91 						enum si_flags flags);
92 int add_listen_advertise_iface_name(char* name, struct name_lst* nlst,
93 						unsigned short port, unsigned short proto,
94 						char *useaddr, unsigned short useport, char *sockname,
95 						enum si_flags flags);
96 int fix_all_socket_lists(void);
97 void print_all_socket_lists(void);
98 void print_aliases(void);
99 
100 struct socket_info* grep_sock_info(str* host, unsigned short port,
101 										unsigned short proto);
102 struct socket_info* grep_sock_info_by_port(unsigned short port,
103 											unsigned short proto);
104 struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
105 												unsigned short proto);
106 socket_info_t* ksr_get_socket_by_name(str *sockname);
107 
108 struct socket_info** get_sock_info_list(unsigned short proto);
109 
110 int parse_phostport(char* s, char** host, int* hlen,
111 								 int* port, int* proto);
112 
113 int parse_proto(unsigned char* s, long len, int* proto);
114 
115 char* get_valid_proto_name(unsigned short proto);
116 
117 /* helper function:
118  * returns next protocol, if the last one is reached return 0
119  * useful for cycling on the supported protocols
120  * order: udp, tcp, tls, sctp */
next_proto(unsigned short proto)121 static inline int next_proto(unsigned short proto)
122 {
123 	if (proto>PROTO_LAST)
124 		LM_ERR("unknown proto %d\n", proto);
125 	else
126 		return nxt_proto[proto];
127 	return 0;
128 }
129 
130 
131 
132 /* gets first non-null socket_info structure
133  * (useful if for. e.g we are not listening on any udp sockets )
134  */
get_first_socket(void)135 inline static struct socket_info* get_first_socket(void)
136 {
137 	if (udp_listen) return udp_listen;
138 #ifdef USE_TCP
139 	else if (tcp_listen) return tcp_listen;
140 #endif
141 #ifdef USE_SCTP
142 	else if (sctp_listen) return sctp_listen;
143 #endif
144 #ifdef USE_TCP
145 #ifdef USE_TLS
146 	else if (tls_listen) return tls_listen;
147 #endif
148 #endif
149 	return 0;
150 }
151 
152 /* structure to break down 'proto:host:port' */
153 typedef struct _sr_phostp {
154 	int proto;
155 	str host;
156 	int port;
157 } sr_phostp_t;
158 
159 struct socket_info* lookup_local_socket(str *phostp);
160 int parse_protohostport(str* ins, sr_phostp_t *r);
161 
162 unsigned int ipv6_get_netif_scope(char *ipval);
163 
164 #endif
165