1 /*
2     Copyright (C) 2002-2009  Thomas Ries <tries@gmx.net>
3 
4     This file is part of Siproxd.
5 
6     Siproxd is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     Siproxd is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with Siproxd; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20 
21 /* $Id: siproxd.h 524 2015-09-19 12:56:27Z hb9xar $ */
22 
23 #ifdef DMALLOC
24  #include <dmalloc.h>
25 #endif
26 #include <limits.h>
27 
28 /*
29  * table to hold the client registrations
30  */
31 struct urlmap_s {
32    int  active;
33    int  expires;
34    osip_uri_t *true_url;	// true URL of UA  (inbound URL)
35    osip_uri_t *masq_url;	// masqueraded URL (outbound URL)
36    osip_uri_t *reg_url;		// registered URL  (masq URL as wished by UA)
37 };
38 /*
39  * the difference between masq_url and reg_url is,
40  * the reg URL *always* holds the url registered by the UA.
41  * the masq_url may contain a different URL due to an additional
42  * masquerading feature (mask_host, masked_host config options)
43  */
44 
45 
46 
47 /*
48  * Array of strings - used within configuration store
49  */
50 #define CFG_STRARR_SIZE		128	/* max 128 entries in array */
51 typedef struct {
52    int  used;
53    char *string[CFG_STRARR_SIZE];
54 } stringa_t;
55 
56 /*
57  * global configuration option table
58  */
59 struct siproxd_config {
60    char *configfile;
61    int   config_search;
62    /* everything below here will be filled according to the config file */
63    unsigned int debuglevel;
64    int debugport;
65    char *inbound_if;
66    char *outbound_if;
67    char *outbound_host;
68    int sip_listen_port;
69    int daemonize;
70    int silence_log;
71    int rtp_port_low;
72    int rtp_port_high;
73    int rtp_timeout;
74    int rtp_dscp;
75    int rtp_proxy_enable;
76    int rtp_input_dejitter;
77    int rtp_output_dejitter;
78    char *user;
79    char *chrootjail;
80    char *hosts_allow_reg;
81    char *hosts_allow_sip;
82    char *hosts_deny_sip;
83    char *proxy_auth_realm;
84    char *proxy_auth_passwd;
85    char *proxy_auth_pwfile;
86    stringa_t mask_host;
87    stringa_t masked_host;
88    char *outbound_proxy_host;
89    int  outbound_proxy_port;
90    stringa_t outbound_proxy_domain_name;
91    stringa_t outbound_proxy_domain_host;
92    stringa_t outbound_proxy_domain_port;
93    char *registrationfile;
94    char *pid_file;
95    int  default_expires;
96    int  autosave_registrations;
97    char *ua_string;
98    int   use_rport;
99    int   obscure_loops;
100    char *plugin_dir;
101    stringa_t load_plugin;
102    int   sip_dscp;
103    int   tcp_timeout;
104    int   tcp_connect_timeout;
105    int   tcp_keepalive;
106    int   thread_stack_size;
107 };
108 
109 /*
110  * control structure for config file parser
111  */
112 typedef struct {
113    int  int4;
114    char *string;
115 } defval_t;
116 typedef struct {
117    char *keyword;
118    enum type {TYP_INT4, TYP_STRING, TYP_FLOAT, TYP_STRINGA} type;
119    void *dest;
120    defval_t defval;
121 } cfgopts_t;
122 
123 /*
124  * SIP ticket
125  */
126 typedef struct {
127    char *raw_buffer;		/* raw UDP packet */
128    size_t  raw_buffer_len;		/* length of raw data */
129    osip_message_t *sipmsg;	/* SIP */
130    struct sockaddr_in from;	/* received from */
131 #define PROTO_UNKN -1
132 #define PROTO_UDP  1
133 #define PROTO_TCP  2
134    int protocol;		/* received by protocol */
135 #define DIRTYP_UNKNOWN		0
136 #define REQTYP_INCOMING		1
137 #define REQTYP_OUTGOING		2
138 #define RESTYP_INCOMING		3
139 #define RESTYP_OUTGOING		4
140    int direction;		/* direction as determined by proxy */
141    struct sockaddr_in next_hop;	/* next hop as determined by plugin or proxy */
142 } sip_ticket_t;
143 
144 
145 /*
146  * Client_ID - used to identify the two sides of a Call when one
147  * call is routed twice (in->out and back out->in) through siproxd
148  * e.g. local UA1 is calling local UA2 via an external Registrar
149  */
150 #define CLIENT_ID_SIZE	128
151 typedef struct {
152    char    idstring[CLIENT_ID_SIZE];
153    struct  in_addr from_ip;
154    /*... maybe more to come ...*/
155 } client_id_t;
156 
157 
158 /*
159  * Function prototypes
160  */
161 
162 /*				function returns STS_* status values     vvv */
163 
164 /* sock.c */
165 int sipsock_listen(void);						/*X*/
166 //int sipsock_wait(void);
167 int sipsock_waitfordata(char *buf, size_t bufsize,
168                         struct sockaddr_in *from, int *protocol);
169 int sipsock_send(struct in_addr addr, int port,	int protocol,		/*X*/
170                  char *buffer, size_t size);
171 int sockbind(struct in_addr ipaddr, int localport, int protocol, int errflg);
172 int tcp_find(struct sockaddr_in dst_addr);
173 
174 /* register.c */
175 void register_init(void);
176 void register_save(void);
177 int  register_client(sip_ticket_t *ticket, int force_lcl_masq);		/*X*/
178 void register_agemap(void);
179 int  register_response(sip_ticket_t *ticket, int flag);			/*X*/
180 int  register_set_expire(sip_ticket_t *ticket);				/*X*/
181 
182 /* proxy.c */
183 int proxy_request (sip_ticket_t *ticket);				/*X*/
184 int proxy_response (sip_ticket_t *ticket);				/*X*/
185 int proxy_rewrite_invitation_body(sip_ticket_t *ticket, int direction); /*X*/
186 int proxy_rewrite_request_uri(osip_message_t *mymsg, int idx);		/*X*/
187 int proxy_rewrite_useragent(sip_ticket_t *ticket);			/*X*/
188 
189 /* route_processing.c */
190 int route_preprocess(sip_ticket_t *ticket);				/*X*/
191 int route_add_recordroute(sip_ticket_t *ticket);			/*X*/
192 int route_purge_recordroute(sip_ticket_t *ticket);			/*X*/
193 int route_postprocess(sip_ticket_t *ticket);				/*X*/
194 int route_determine_nexthop(sip_ticket_t *ticket,
195                             struct in_addr *dest, in_port_t *port);	/*X*/
196 
197 /* utils.c */
198 int  get_ip_by_host(char *hostname, struct in_addr *addr);		/*X*/
199 void secure_enviroment (void);
200 int  get_ip_by_ifname(char *ifname, struct in_addr *retaddr);		/*X*/
201 int  get_interface_ip(int interface, struct in_addr *retaddr);		/*X*/
202 int  get_interface_real_ip(int interface, struct in_addr *retaddr);	/*X*/
203 char *utils_inet_ntoa(struct in_addr in);
204 int  utils_inet_aton(const char *cp, struct in_addr *inp);
205 int  createpidfile(char *pidfilename);					/*X*/
206 int  compare_client_id(client_id_t cid1, client_id_t cid2);		/*X*/
207 int  is_empty_sockaddr(struct sockaddr_in *sockaddr);			/*X*/
208 
209 /* sip_utils.c */
210 osip_message_t * msg_make_template_reply (sip_ticket_t *ticket, int code);
211 int  check_vialoop (sip_ticket_t *ticket);				/*X*/
212 int  is_via_local (osip_via_t *via);					/*X*/
213 int  compare_url(osip_uri_t *url1, osip_uri_t *url2);			/*X*/
214 int  compare_callid(osip_call_id_t *cid1, osip_call_id_t *cid2);	/*X*/
215 int  is_sipuri_local (sip_ticket_t *ticket);				/*X*/
216 int  sip_gen_response(sip_ticket_t *ticket, int code);			/*X*/
217 int  sip_add_myvia (sip_ticket_t *ticket, int interface);		/*X*/
218 int  sip_del_myvia (sip_ticket_t *ticket);				/*X*/
219 int  sip_rewrite_contact (sip_ticket_t *ticket, int direction);		/*X*/
220 int  sip_calculate_branch_id (sip_ticket_t *ticket, char *id);		/*X*/
221 int  sip_find_outbound_proxy(sip_ticket_t *ticket, struct in_addr *addr,
222                              in_port_t *port);				/*X*/
223 int  sip_find_direction(sip_ticket_t *ticket, int *urlidx);		/*X*/
224 int  sip_fixup_asterisk(char *buff, size_t *buflen);			/*X*/
225 int  sip_obscure_callid(sip_ticket_t *ticket);				/*X*/
226 int  sip_add_received_param(sip_ticket_t *ticket);			/*X*/
227 int  sip_get_received_param(sip_ticket_t *ticket,
228                             struct in_addr *dest, in_port_t *port);	/*X*/
229 
230 /* readconf.c */
231 int  read_config(char *name, int search, cfgopts_t cfgopts[], char *filter); /*X*/
232 
233 /* rtpproxy.c */
234 int  rtpproxy_init( void );						/*X*/
235 int  rtp_start_fwd (osip_call_id_t *callid, client_id_t client_id,	/*X*/
236                     int direction, int call_direction, int media_stream_no,
237                     struct in_addr outbound_ipaddr, int *outboundport,
238                     struct in_addr lcl_client_ipaddr, int lcl_clientport,
239                     int isrtp);
240 int  rtp_stop_fwd (osip_call_id_t *callid, int direction);		/*X*/
241 
242 /* accessctl.c */
243 int  accesslist_check(struct sockaddr_in from);
244 int  process_aclist (char *aclist, struct sockaddr_in from);
245 
246 /* security.c */
247 int  security_check_raw(char *sip_buffer, size_t size);			/*X*/
248 int  security_check_sip(sip_ticket_t *ticket);				/*X*/
249 
250 /* auth.c */
251 int  authenticate_proxy(osip_message_t *sipmsg);			/*X*/
252 int  auth_include_authrq(osip_message_t *sipmsg);			/*X*/
253 void CvtHex(unsigned char *hash, unsigned char *hashstring);
254 
255 /* fwapi.c */
256 int fwapi_start_rtp(int rtp_direction,
257                     struct in_addr local_ipaddr, int local_port,
258                     struct in_addr remote_ipaddr, int remote_port);
259 int fwapi_stop_rtp(int rtp_direction,
260                    struct in_addr local_ipaddr, int local_port,
261                    struct in_addr remote_ipaddr, int remote_port);
262 
263 /* sip_layer.c */
264 int sip_message_parse(osip_message_t * sip,    const char *buf, size_t len);
265 int sip_message_to_str(osip_message_t * sip,   char **dest,     size_t *len);
266 int sip_body_to_str(const osip_body_t * body,  char **dest,     size_t *len);
267 int sip_message_set_body(osip_message_t * sip, const char *buf, size_t len);
268 
269 /* plugins.c */
270 int load_plugins (void);
271 int call_plugins(int stage, sip_ticket_t *ticket);
272 int unload_plugins(void);
273 
274 /*
275  * some constant definitions
276  */
277 #define SIP_PORT	5060	/* default port to listen */
278 #define DEFAULT_MAXFWD	70	/* default Max-Forward count */
279 #define DEFAULT_EXPIRES	3600	/* default Expires timeout */
280 
281 #define TCP_IDLE_TO	300	/* TCP connection idle timeout in seconds */
282 #define TCP_CONNECT_TO	500	/* TCP connect() timeout in msec */
283 
284 #define URLMAP_SIZE	128	/* number of URL mapping table entries	*/
285 				/* this limits the number of clients!	*/
286 
287 #define SOURCECACHE_SIZE 256	/* number of return addresses		*/
288 #define DEJITTERLIMIT	1500000	/* max value for dejitter configuration */
289 
290 #define RTPPROXY_SIZE	256	/* number of rtp proxy entries		*/
291 				/* this limits the number of calls!	*/
292 
293 #define BUFFER_SIZE	8196	/* input buffer for read from socket	*/
294 #define RTP_BUFFER_SIZE	1520	/* max size of an RTP frame		*/
295 				/* (assume approx one Ethernet MTU)	*/
296 
297 #define PATH_STRING_SIZE 256	/* max size of an file path		*/
298 #define URL_STRING_SIZE	128	/* max size of an URL/URI string	*/
299 #define STATUSCODE_SIZE	5	/* size of string representation of status */
300 #define DNS_CACHE_SIZE	256	/* number of entries in internal DNS cache */
301 #define DNS_ATTEMPTS	3	/* number of attempts to resolve a name
302 				   before it is marked as bad */
303 #define DNS_GOOD_AGE	60	/* maximum age of a good cache entry (sec) */
304 #define DNS_BAD_AGE	600	/* maximum age of a bad cache entry (sec) */
305 #define IFADR_CACHE_SIZE 32	/* number of entries in internal IFADR cache */
306 #define IFADR_MAX_AGE	5	/* max. age of the IF address cache (sec) */
307 #define IFNAME_SIZE	16	/* max string length of a interface name */
308 #define HOSTNAME_SIZE	128	/* max string length of a hostname	*/
309 #define USERNAME_SIZE	128	/* max string length of a username (auth) */
310 #define PASSWORD_SIZE	128	/* max string length of a password (auth) */
311 #define IPSTRING_SIZE	16	/* stringsize of IP address xxx.xxx.xxx.xxx */
312 #define VIA_BRANCH_SIZE	128	/* max string length for via branch param */
313 				/* scratch buffer for gethostbyname_r() */
314 
315 #if defined(PR_NETDB_BUF_SIZE)
316    #define GETHOSTBYNAME_BUFLEN PR_NETDB_BUF_SIZE
317 #else
318    #define GETHOSTBYNAME_BUFLEN 1024
319 #endif
320 
321 /* constants for security testing */
322 #define SEC_MINLEN	16	/* minimum received length */
323 #define SEC_MAXLINELEN	2048	/* maximum acceptable length of one line
324 				   in the SIP telegram (security check)
325 				   Careful: Proxy-Authorization lines may
326 				   get quite long */
327 
328 /* symbols for access control */
329 #define ACCESSCTL_SIP	1	/* for access control - SIP allowed	*/
330 #define ACCESSCTL_REG	2	/* --"--              - registr. allowed */
331 
332 /* symbolic return stati */
333 #define STS_SUCCESS	0	/* SUCCESS				*/
334 #define STS_TRUE	0	/* TRUE					*/
335 #define STS_FAILURE	1	/* FAILURE				*/
336 #define STS_FALSE	1	/* FALSE				*/
337 #define STS_NEED_AUTH	1001	/* need authentication			*/
338 #define STS_SIP_SENT	2001	/* SIP packet is already sent, end of dialog */
339 
340 /* symbolic direction of data */
341 #define DIR_INCOMING	1
342 #define DIR_OUTGOING	2
343 
344 /* Interfaces */
345 #define IF_OUTBOUND 0
346 #define IF_INBOUND  1
347 
348 /* various */
349 #ifndef satoi
350 #define satoi atoi  /* used in libosips MSG_TEST_CODE macro ... */
351 #endif
352 
353 
354 /*
355  * Macro that limits the frequency of this particular code
356  * block to no faster than every 'a' seconds. Used for logging
357  */
358 #define LIMIT_LOG_RATE(a) \
359         static time_t last=0; \
360         time_t now; \
361         int dolog=0; \
362         time(&now); \
363         if ((last+(a)) <= now) {last=now; dolog=1;} \
364         if (dolog)
365 
366 /*
367  * if the following symbol 'GPL' is defined, building siproxd will
368  * include all features. If not defined, some features that will
369  * conflict with a non-GPL distribution license will be disabled.
370  *
371  * If you wish to distribute siproxd under another license than GPL
372  * (commercial License for example), contact the author to elaborate
373  * the details.
374  */
375 #define GPL
376