2 * find & manage listen addresses
4 * Copyright (C) 2001-2003 FhG Fokus
6 * This file is part of Kamailio, a free SIP server.
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
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.
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
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.
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
41 #define MAX_SOCKET_STR (sizeof("unknown") - 1 + IP_ADDR_MAX_STR_SIZE + \
42 INT2STR_MAX_LEN + 2 + 2)
44 /* Maximum length for advertise string of listen socket */
45 #define MAX_SOCKET_ADVERTISE_STR 511
47 int socket2str(char* s, int* len, struct socket_info* si);
48 int socketinfo2str(char* s, int* len, struct socket_info* si, int mode);
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)
53 /* struct socket_info is defined in ip_addr.h */
55 extern struct socket_info* udp_listen;
57 extern struct socket_info* tcp_listen;
60 extern struct socket_info* tls_listen;
63 extern struct socket_info* sctp_listen;
66 extern enum sip_protos nxt_proto[PROTO_LAST+1];
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
78 extern int socket_types;
80 void init_proto_order(void);
82 int add_listen_iface(char* name, struct name_lst* nlst,
83 unsigned short port, unsigned short proto,
85 int add_listen_iface_name(char* name, struct name_lst* addr_l,
86 unsigned short port, unsigned short proto, char *sockname,
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,
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,
96 int fix_all_socket_lists(void);
97 void print_all_socket_lists(void);
98 void print_aliases(void);
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);
108 struct socket_info** get_sock_info_list(unsigned short proto);
110 int parse_phostport(char* s, char** host, int* hlen,
111 int* port, int* proto);
113 int parse_proto(unsigned char* s, long len, int* proto);
115 char* get_valid_proto_name(unsigned short proto);
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 */
121 static inline int next_proto(unsigned short proto)
123 if (proto>PROTO_LAST)
124 LM_ERR("unknown proto %d\n", proto);
126 return nxt_proto[proto];
132 /* gets first non-null socket_info structure
133 * (useful if for. e.g we are not listening on any udp sockets )
135 inline static struct socket_info* get_first_socket(void)
137 if (udp_listen) return udp_listen;
139 else if (tcp_listen) return tcp_listen;
142 else if (sctp_listen) return sctp_listen;
146 else if (tls_listen) return tls_listen;
152 /* structure to break down 'proto:host:port' */
153 typedef struct _sr_phostp {
159 struct socket_info* lookup_local_socket(str *phostp);
160 int parse_protohostport(str* ins, sr_phostp_t *r);
162 unsigned int ipv6_get_netif_scope(char *ipval);