1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Ken Rice <krice@freeswitch.org>
28  * Paul D. Tinsley <pdt at jackhammer.org>
29  * Bret McDanel <trixter AT 0xdecafbad.com>
30  * Marcel Barbulescu <marcelbarbulescu@gmail.com>
31  * Raymond Chandler <intralanman@gmail.com>
32  * Emmanuel Schmidbauer <e.schmidbauer@gmail.com>
33  *
34  *
35  * mod_sofia.h -- SOFIA SIP Endpoint
36  *
37  */
38 
39 /*Defines etc..*/
40 /*************************************************************************************************************************************************************/
41 #define SOFIA_RECOVER "sofia"
42 #define MANUAL_BYE 1
43 #define SQL_CACHE_TIMEOUT 300
44 #define DEFAULT_NONCE_TTL 60
45 #define IREG_SECONDS 30
46 #define IPING_SECONDS 30
47 #define IPING_FREQUENCY 1
48 #define GATEWAY_SECONDS 1
49 #define SOFIA_QUEUE_SIZE 50000
50 #define HAVE_APR
51 #include <switch.h>
52 #define SOFIA_NAT_SESSION_TIMEOUT 90
53 #define SOFIA_MAX_ACL 100
54 #ifdef _MSC_VER
55 #define HAVE_FUNCTION 1
56 #else
57 #define HAVE_FUNC 1
58 #endif
59 
60 #define MAX_CODEC_CHECK_FRAMES 50
61 #define MAX_MISMATCH_FRAMES 5
62 #define MODNAME "mod_sofia"
63 #define SOFIA_DEFAULT_CONTACT_USER MODNAME
64 static const switch_state_handler_table_t noop_state_handler = { 0 };
65 struct sofia_gateway;
66 typedef struct sofia_gateway sofia_gateway_t;
67 
68 struct sofia_gateway_subscription;
69 typedef struct sofia_gateway_subscription sofia_gateway_subscription_t;
70 
71 struct sofia_profile;
72 typedef struct sofia_profile sofia_profile_t;
73 #define NUA_MAGIC_T sofia_profile_t
74 
75 typedef struct sofia_private sofia_private_t;
76 
77 struct private_object;
78 typedef struct private_object private_object_t;
79 #define NUA_HMAGIC_T sofia_private_t
80 
81 #define SOFIA_SESSION_TIMEOUT "sofia_session_timeout"
82 #define MY_EVENT_REGISTER "sofia::register"
83 #define MY_EVENT_PRE_REGISTER "sofia::pre_register"
84 #define MY_EVENT_REGISTER_ATTEMPT "sofia::register_attempt"
85 #define MY_EVENT_REGISTER_FAILURE "sofia::register_failure"
86 #define MY_EVENT_UNREGISTER "sofia::unregister"
87 #define MY_EVENT_EXPIRE "sofia::expire"
88 #define MY_EVENT_GATEWAY_STATE "sofia::gateway_state"
89 #define MY_EVENT_SIP_USER_STATE "sofia::sip_user_state"
90 #define MY_EVENT_NOTIFY_REFER "sofia::notify_refer"
91 #define MY_EVENT_REINVITE "sofia::reinvite"
92 #define MY_EVENT_GATEWAY_ADD "sofia::gateway_add"
93 #define MY_EVENT_GATEWAY_DEL "sofia::gateway_delete"
94 #define MY_EVENT_RECOVERY "sofia::recovery_recv"
95 #define MY_EVENT_RECOVERY_SEND "sofia::recovery_send"
96 #define MY_EVENT_RECOVERY_RECOVERED "sofia::recovery_recovered"
97 #define MY_EVENT_ERROR "sofia::error"
98 #define MY_EVENT_PROFILE_START "sofia::profile_start"
99 #define MY_EVENT_NOTIFY_WATCHED_HEADER "sofia::notify_watched_header"
100 #define MY_EVENT_WRONG_CALL_STATE "sofia::wrong_call_state"
101 
102 #define MY_EVENT_TRANSFEROR "sofia::transferor"
103 #define MY_EVENT_TRANSFEREE "sofia::transferee"
104 #define MY_EVENT_REPLACED "sofia::replaced"
105 #define MY_EVENT_INTERCEPTED "sofia::intercepted"
106 
107 #define MY_EVENT_BYE_RESPONSE "sofia::bye_response"
108 
109 #define MULTICAST_EVENT "multicast::event"
110 #define SOFIA_REPLACES_HEADER "_sofia_replaces_"
111 #define SOFIA_CHAT_PROTO "sip"
112 #define SOFIA_MULTIPART_PREFIX "sip_mp_"
113 #define SOFIA_MULTIPART_PREFIX_T "~sip_mp_"
114 #define SOFIA_SIP_HEADER_PREFIX "sip_h_"
115 #define SOFIA_SIP_INFO_HEADER_PREFIX "sip_info_h_"
116 #define SOFIA_SIP_INFO_HEADER_PREFIX_T "~sip_info_h_"
117 #define SOFIA_SIP_RESPONSE_HEADER_PREFIX "sip_rh_"
118 #define SOFIA_SIP_RESPONSE_HEADER_PREFIX_T "~sip_rh_"
119 #define SOFIA_SIP_BYE_HEADER_PREFIX "sip_bye_h_"
120 #define SOFIA_SIP_BYE_HEADER_PREFIX_T "~sip_bye_h_"
121 #define SOFIA_SIP_PROGRESS_HEADER_PREFIX "sip_ph_"
122 #define SOFIA_SIP_PROGRESS_HEADER_PREFIX_T "~sip_ph_"
123 #define SOFIA_SIP_HEADER_PREFIX_T "~sip_h_"
124 #define SOFIA_DEFAULT_PORT "5060"
125 #define SOFIA_DEFAULT_TLS_PORT "5061"
126 #define SOFIA_REFER_TO_VARIABLE "sip_refer_to"
127 //#define SOFIA_HAS_CRYPTO_VARIABLE "rtp_has_crypto"
128 //#define SOFIA_HAS_VIDEO_CRYPTO_VARIABLE "sip_has_video_crypto"
129 //#define SOFIA_CRYPTO_MANDATORY_VARIABLE "sip_crypto_mandatory"
130 #define FREESWITCH_SUPPORT "update_display,send_info"
131 
132 #include <switch_stun.h>
133 #include <sofia-sip/nua.h>
134 #include <sofia-sip/sip_status.h>
135 #include <sofia-sip/sdp.h>
136 #include <sofia-sip/sip_protos.h>
137 #include <sofia-sip/auth_module.h>
138 #include <sofia-sip/su_md5.h>
139 #include <sofia-sip/su_log.h>
140 #include <sofia-sip/su_strlst.h>
141 #include <sofia-sip/nea.h>
142 #include <sofia-sip/msg_addr.h>
143 #include <sofia-sip/tport_tag.h>
144 #include <sofia-sip/sip_extra.h>
145 #include "nua_stack.h"
146 #include "sofia-sip/msg_parser.h"
147 #include "sofia-sip/sip_parser.h"
148 #include "sofia-sip/tport_tag.h"
149 #include <sofia-sip/msg.h>
150 #include <sofia-sip/uniqueid.h>
151 
152 typedef enum {
153 	SOFIA_CONFIG_LOAD = 0,
154 	SOFIA_CONFIG_RESCAN,
155 	SOFIA_CONFIG_RESPAWN
156 } sofia_config_t;
157 
158 typedef struct sofia_dispatch_event_s {
159 	nua_saved_event_t event[1];
160 	nua_handle_t *nh;
161 	nua_event_data_t const *data;
162 	su_time_t when;
163 	sip_t *sip;
164 	nua_t *nua;
165 	sofia_profile_t *profile;
166 	int save;
167 	switch_core_session_t *session;
168 	switch_core_session_t *init_session;
169 	switch_memory_pool_t *pool;
170 	struct sofia_dispatch_event_s *next;
171 } sofia_dispatch_event_t;
172 
173 struct sofia_private {
174 	char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
175 	char *uuid;
176 	char gateway_name[256];
177 	char auth_gateway_name[256];
178 	char *call_id;
179 	char *network_ip;
180 	char *network_port;
181 	char *key;
182 	char *user;
183 	char *realm;
184 	int destroy_nh;
185 	int destroy_me;
186 	int is_call;
187 	int is_static;
188 	switch_time_t ping_sent;
189 	char *rfc7989_uuid;
190 };
191 
192 #define set_param(ptr,val) if (ptr) {free(ptr) ; ptr = NULL;} if (val) {ptr = strdup(val);}
193 #define set_anchor(t,m) if (t->Anchor) {delete t->Anchor;} t->Anchor = new SipMessage(m);
194 #define sofia_private_free(_pvt) if (_pvt && ! _pvt->is_static) {free(_pvt);} _pvt = NULL;
195 
196 
197 /* Local Structures */
198 /*************************************************************************************************************************************************************/
199 struct sip_alias_node {
200 	char *url;
201 	nua_t *nua;
202 	struct sip_alias_node *next;
203 };
204 
205 typedef struct sip_alias_node sip_alias_node_t;
206 
207 typedef enum {
208 	MFLAG_REFER = (1 << 0),
209 	MFLAG_REGISTER = (1 << 1)
210 } MFLAGS;
211 
212 typedef enum {
213 	PFLAG_AUTH_CALLS,
214 	PFLAG_AUTH_MESSAGES,
215 	PFLAG_PASS_RFC2833,
216 	PFLAG_BLIND_REG,
217 	PFLAG_AUTH_ALL,
218 	PFLAG_FULL_ID,
219 	PFLAG_MULTIREG_CONTACT,
220 	PFLAG_DISABLE_TRANSCODING,
221 	PFLAG_RUNNING,
222 	PFLAG_RESPAWN,
223 	PFLAG_MULTIREG,
224 	PFLAG_TLS,
225 	PFLAG_CHECKUSER,
226 	PFLAG_SECURE,
227 	PFLAG_BLIND_AUTH,
228 	PFLAG_WORKER_RUNNING,
229 	PFLAG_UNREG_OPTIONS_FAIL,
230 	PFLAG_DISABLE_TIMER,
231 	PFLAG_ENABLE_RFC5626,
232 	PFLAG_DISABLE_100REL,
233 	PFLAG_AGGRESSIVE_NAT_DETECTION,
234 	PFLAG_RECIEVED_IN_NAT_REG_CONTACT,
235 	PFLAG_3PCC,
236 	PFLAG_3PCC_PROXY,
237 	PFLAG_3PCC_REINVITE_BRIDGED_ON_ACK,
238 	PFLAG_CALLID_AS_UUID,
239 	PFLAG_UUID_AS_CALLID,
240 	PFLAG_MANAGE_SHARED_APPEARANCE,
241 	PFLAG_STANDBY,
242 	PFLAG_DISABLE_SRV,
243 	PFLAG_DISABLE_SRV503,
244 	PFLAG_DISABLE_NAPTR,
245 	PFLAG_NAT_OPTIONS_PING,
246 	PFLAG_UDP_NAT_OPTIONS_PING,
247 	PFLAG_ALL_REG_OPTIONS_PING,
248 	PFLAG_MESSAGE_QUERY_ON_REGISTER,
249 	PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER,
250 	PFLAG_MANUAL_REDIRECT,
251 	PFLAG_T38_PASSTHRU,
252 	PFLAG_AUTO_NAT,
253 	PFLAG_SIPCOMPACT,
254 	PFLAG_PRESENCE_PRIVACY,
255 	PFLAG_PASS_CALLEE_ID,
256 	PFLAG_LOG_AUTH_FAIL,
257 	PFLAG_FORWARD_MWI_NOTIFY,
258 	PFLAG_TRACK_CALLS,
259 	PFLAG_DESTROY,
260 	PFLAG_EXTENDED_INFO_PARSING,
261 	PFLAG_CID_IN_1XX,
262 	PFLAG_IN_DIALOG_CHAT,
263 	PFLAG_DEL_SUBS_ON_REG_REUSE,
264 	PFLAG_IGNORE_183NOSDP,
265 	PFLAG_PRESENCE_PROBE_ON_REGISTER,
266 	PFLAG_PRESENCE_ON_REGISTER,
267 	PFLAG_PRESENCE_ON_FIRST_REGISTER,
268 	PFLAG_NO_CONNECTION_REUSE,
269 	PFLAG_RTP_NOTIMER_DURING_BRIDGE,
270 	PFLAG_LIBERAL_DTMF,
271  	PFLAG_AUTO_ASSIGN_PORT,
272  	PFLAG_AUTO_ASSIGN_TLS_PORT,
273 	PFLAG_SHUTDOWN,
274 	PFLAG_PRESENCE_MAP,
275 	PFLAG_OPTIONS_RESPOND_503_ON_BUSY,
276 	PFLAG_PRESENCE_DISABLE_EARLY,
277 	PFLAG_CONFIRM_BLIND_TRANSFER,
278 	PFLAG_THREAD_PER_REG,
279 	PFLAG_MWI_USE_REG_CALLID,
280 	PFLAG_FIRE_MESSAGE_EVENTS,
281 	PFLAG_ALLOW_UPDATE,
282 	PFLAG_SEND_DISPLAY_UPDATE,
283 	PFLAG_RUNNING_TRANS,
284 	PFLAG_SOCKET_TCP_KEEPALIVE,
285 	PFLAG_TCP_KEEPALIVE,
286 	PFLAG_TCP_PINGPONG,
287 	PFLAG_TCP_PING2PONG,
288 	PFLAG_MESSAGES_RESPOND_200_OK,
289 	PFLAG_SUBSCRIBE_RESPOND_200_OK,
290 	PFLAG_PARSE_ALL_INVITE_HEADERS,
291 	PFLAG_TCP_UNREG_ON_SOCKET_CLOSE,
292 	PFLAG_TLS_ALWAYS_NAT,
293 	PFLAG_TCP_ALWAYS_NAT,
294 	PFLAG_ENABLE_CHAT,
295 	PFLAG_AUTH_SUBSCRIPTIONS,
296 	PFLAG_PROXY_REFER,
297 	PFLAG_CHANNEL_XML_FETCH_ON_NIGHTMARE_TRANSFER,
298 	PFLAG_MAKE_EVERY_TRANSFER_A_NIGHTMARE,
299 	PFLAG_FIRE_TRANFER_EVENTS,
300 	PFLAG_BLIND_AUTH_ENFORCE_RESULT,
301 	PFLAG_BLIND_AUTH_REPLY_403,
302 	PFLAG_PROXY_HOLD,
303 	PFLAG_PROXY_INFO,
304 	PFLAG_PROXY_MESSAGE,
305 	PFLAG_FIRE_BYE_RESPONSE_EVENTS,
306 	PFLAG_AUTO_INVITE_100,
307 	PFLAG_UPDATE_REFRESHER,
308 	PFLAG_RFC7989_SESSION_ID,
309 	PFLAG_RFC7989_FORCE_OLD,
310 	PFLAG_AUTH_REQUIRE_USER,
311 	PFLAG_AUTH_CALLS_ACL_ONLY,
312 	PFLAG_USE_PORT_FOR_ACL_CHECK,
313 
314 	/* No new flags below this line */
315 	PFLAG_MAX
316 } PFLAGS;
317 
318 typedef enum {
319 	PFLAG_NDLB_TO_IN_200_CONTACT = (1 << 0),
320 	PFLAG_NDLB_BROKEN_AUTH_HASH = (1 << 1),
321 	PFLAG_NDLB_EXPIRES_IN_REGISTER_RESPONSE = (1 << 2)
322 } sofia_NDLB_t;
323 
324 typedef enum {
325 	TFLAG_IO,
326 	TFLAG_CHANGE_MEDIA,
327 	TFLAG_OUTBOUND,
328 	TFLAG_READING,
329 	TFLAG_WRITING,
330 	TFLAG_HUP,
331 	TFLAG_RTP,
332 	TFLAG_BYE,
333 	TFLAG_ANS,
334 	TFLAG_EARLY_MEDIA,
335 	TFLAG_3PCC,
336 	TFLAG_READY,
337 	TFLAG_REFER,
338 	TFLAG_NOHUP,
339 	TFLAG_NOSDP_REINVITE,
340 	TFLAG_NAT,
341 	TFLAG_SIMPLIFY,
342 	TFLAG_SIP_HOLD,
343 	TFLAG_SIP_HOLD_INACTIVE,
344 	TFLAG_INB_NOMEDIA,
345 	TFLAG_LATE_NEGOTIATION,
346 	TFLAG_SDP,
347 	TFLAG_NEW_SDP,
348 	TFLAG_TPORT_LOG,
349 	TFLAG_SENT_UPDATE,
350 	TFLAG_PROXY_MEDIA,
351 	TFLAG_ZRTP_PASSTHRU,
352 	TFLAG_HOLD_LOCK,
353 	TFLAG_3PCC_HAS_ACK,
354 	TFLAG_UPDATING_DISPLAY,
355 	TFLAG_ENABLE_SOA,
356 	TFLAG_RECOVERED,
357 	TFLAG_AUTOFLUSH_DURING_BRIDGE,
358 	TFLAG_3PCC_INVITE,
359 	TFLAG_NOREPLY,
360 	TFLAG_GOT_ACK,
361 	TFLAG_CAPTURE,
362 	TFLAG_REINVITED,
363 	TFLAG_PASS_ACK,
364 	TFLAG_KEEPALIVE,
365 	TFLAG_SKIP_EARLY,
366 	/* No new flags below this line */
367 	TFLAG_MAX
368 } TFLAGS;
369 
370 #define SOFIA_MAX_MSG_QUEUE 64
371 #define SOFIA_MSG_QUEUE_SIZE 1000
372 
373 struct mod_sofia_globals {
374 	switch_memory_pool_t *pool;
375 	switch_hash_t *profile_hash;
376 	switch_hash_t *gateway_hash;
377 	switch_mutex_t *hash_mutex;
378 	uint32_t callid;
379 	int32_t running;
380 	int32_t threads;
381 	int cpu_count;
382 	int max_msg_queues;
383 	switch_mutex_t *mutex;
384 	char guess_ip[80];
385 	char hostname[512];
386 	switch_queue_t *presence_queue;
387 	switch_queue_t *msg_queue;
388 	switch_queue_t *general_event_queue;
389 	switch_thread_t *msg_queue_thread[SOFIA_MAX_MSG_QUEUE];
390 	int msg_queue_len;
391 	struct sofia_private destroy_private;
392 	struct sofia_private keep_private;
393 	int guess_mask;
394 	char guess_mask_str[16];
395 	int debug_presence;
396 	int debug_sla;
397 	int auto_restart;
398 	int reg_deny_binding_fetch_and_no_lookup; /* backwards compatibility */
399 	int auto_nat;
400 	int tracelevel;
401 	char *capture_server;
402 	int rewrite_multicasted_fs_path;
403 	int presence_flush;
404 	switch_thread_t *presence_thread;
405 	uint32_t max_reg_threads;
406 	time_t presence_epoch;
407 	int presence_year;
408 	int abort_on_empty_external_ip;
409 };
410 extern struct mod_sofia_globals mod_sofia_globals;
411 
412 typedef enum {
413 	REG_FLAG_AUTHED,
414 	REG_FLAG_CALLERID,
415 
416 	/* No new flags below this line */
417 	REG_FLAG_MAX
418 } reg_flags_t;
419 
420 typedef enum {
421 	CID_TYPE_RPID,
422 	CID_TYPE_PID,
423 	CID_TYPE_NONE
424 } sofia_cid_type_t;
425 
426 typedef enum {
427 	REG_STATE_UNREGED,
428 	REG_STATE_TRYING,
429 	REG_STATE_REGISTER,
430 	REG_STATE_REGED,
431 	REG_STATE_UNREGISTER,
432 	REG_STATE_FAILED,
433 	REG_STATE_FAIL_WAIT,
434 	REG_STATE_EXPIRED,
435 	REG_STATE_NOREG,
436 	REG_STATE_TIMEOUT,
437 	REG_STATE_LAST
438 } reg_state_t;
439 
440 typedef enum {
441 	SOFIA_TRANSPORT_UNKNOWN = 0,
442 	SOFIA_TRANSPORT_UDP,
443 	SOFIA_TRANSPORT_TCP,
444 	SOFIA_TRANSPORT_TCP_TLS,
445 	SOFIA_TRANSPORT_SCTP,
446 	SOFIA_TRANSPORT_WS,
447 	SOFIA_TRANSPORT_WSS
448 } sofia_transport_t;
449 
450 typedef enum {
451 	SOFIA_TLS_VERSION_SSLv2 = (1 << 0),
452 	SOFIA_TLS_VERSION_SSLv3 = (1 << 1),
453 	SOFIA_TLS_VERSION_TLSv1 = (1 << 2),
454 	SOFIA_TLS_VERSION_TLSv1_1 = (1 << 3),
455 	SOFIA_TLS_VERSION_TLSv1_2 = (1 << 4),
456 } sofia_tls_version_t;
457 
458 typedef enum {
459 	SOFIA_GATEWAY_DOWN,
460 	SOFIA_GATEWAY_UP,
461 
462 	SOFIA_GATEWAY_INVALID
463 } sofia_gateway_status_t;
464 
465 typedef enum {
466        SOFIA_REG_REACHABLE,
467        SOFIA_REG_UNREACHABLE,
468 
469        SOFIA_REG_INVALID
470 } sofia_sip_user_status_t;
471 
472 typedef enum {
473 	SUB_STATE_UNSUBED,
474 	SUB_STATE_TRYING,
475 	SUB_STATE_SUBSCRIBE,
476 	SUB_STATE_SUBED,
477 	SUB_STATE_UNSUBSCRIBE,
478 	SUB_STATE_FAILED,
479 	SUB_STATE_FAIL_WAIT,
480 	SUB_STATE_EXPIRED,
481 	SUB_STATE_NOSUB,
482 	v_STATE_LAST
483 } sub_state_t;
484 
485 struct sofia_gateway_subscription {
486 	sofia_gateway_t *gateway;
487 	sofia_private_t *sofia_private;
488 	nua_handle_t *nh;
489 	char *expires_str;
490 	char *event;				/* eg, 'message-summary' to subscribe to MWI events */
491 	char *content_type;			/* eg, application/simple-message-summary in the case of MWI events */
492 	char *request_uri;
493 	uint32_t freq;
494 	int32_t retry_seconds;
495 	time_t expires;
496 	time_t retry;
497 	sub_state_t state;
498 	struct sofia_gateway_subscription *next;
499 };
500 
501 struct sofia_gateway {
502 	sofia_private_t *sofia_private;
503 	nua_handle_t *nh;
504 	sofia_profile_t *profile;
505 	char *name;
506 	char *register_scheme;
507 	char *register_realm;
508 	char *register_username;
509 	char *auth_username;
510 	char *register_password;
511 	char *register_from;
512 	char *options_from_uri;
513 	char *options_to_uri;
514 	char *options_user_agent;
515 	char *register_contact;
516 	char *extension;
517 	char *real_extension;
518 	char *register_to;
519 	char *register_proxy;
520 	char *register_sticky_proxy;
521 	char *outbound_sticky_proxy;
522 	char *register_context;
523 	char *expires_str;
524 	char *register_url;
525 	char *destination_prefix;
526 	char *from_domain;
527 	sofia_transport_t register_transport;
528 	uint32_t freq;
529 	time_t expires;
530 	time_t retry;
531 	time_t ping;
532 	time_t reg_timeout;
533 	int pinging;
534 	sofia_gateway_status_t status;
535 	switch_time_t uptime;
536 	uint32_t contact_in_ping;
537 	uint32_t ping_freq;
538 	int ping_count;
539 	int ping_max;
540 	int ping_min;
541 	switch_time_t ping_sent;
542 	float ping_time;
543 	switch_bool_t ping_monitoring;
544 	uint8_t flags[REG_FLAG_MAX];
545 	int32_t retry_seconds;
546 	int32_t fail_908_retry_seconds;
547 	int32_t reg_timeout_seconds;
548 	int32_t failure_status;
549 	reg_state_t state;
550 	switch_memory_pool_t *pool;
551 	int deleted;
552 	switch_event_t *ib_vars;
553 	switch_event_t *ob_vars;
554 	uint32_t ib_calls;
555 	uint32_t ob_calls;
556 	uint32_t ib_failed_calls;
557 	uint32_t ob_failed_calls;
558 	char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
559 	int failures;
560 	struct sofia_gateway *next;
561 	sofia_gateway_subscription_t *subscriptions;
562 	int distinct_to;
563 	sofia_cid_type_t cid_type;
564 	char register_network_ip[80];
565 	int register_network_port;
566 };
567 
568 typedef enum {
569 	PRES_TYPE_NONE = 0,
570 	PRES_TYPE_FULL = 1,
571 	PRES_TYPE_PASSIVE = 2,
572 	PRES_TYPE_PNP = 3
573 } sofia_presence_type_t;
574 
575 typedef enum {
576 	PRES_HELD_EARLY = 0,
577 	PRES_HELD_CONFIRMED = 1,
578 	PRES_HELD_TERMINATED = 2
579 } sofia_presence_held_calls_type_t;
580 
581 typedef enum {
582 	MEDIA_OPT_NONE = 0,
583 	MEDIA_OPT_MEDIA_ON_HOLD = (1 << 0),
584 	MEDIA_OPT_BYPASS_AFTER_ATT_XFER = (1 << 1),
585 	MEDIA_OPT_BYPASS_AFTER_HOLD = (1 << 2)
586 } sofia_media_options_t;
587 
588 typedef enum {
589 	   PAID_DEFAULT = 0,
590 	   PAID_USER,
591 	   PAID_USER_DOMAIN,
592 	   PAID_VERBATIM
593 } sofia_paid_type_t;
594 
595 #define MAX_RTPIP 50
596 
597 typedef enum {
598 	KA_MESSAGE,
599 	KA_INFO
600 } ka_type_t;
601 
602 struct sofia_profile {
603 	int debug;
604 	int parse_invite_tel_params;
605 	char *name;
606 	char *domain_name;
607 	char *dbname;
608 	char *dialplan;
609 	char *context;
610 	char *shutdown_type;
611 	char *extrtpip;
612 	char *rtpip[MAX_RTPIP];
613 	char *rtpip6[MAX_RTPIP];
614 	char *jb_msec;
615 	switch_payload_t te;
616 	switch_payload_t recv_te;
617 	uint32_t rtpip_index;
618 	uint32_t rtpip_next;
619 	uint32_t rtpip_index6;
620 	uint32_t rtpip_next6;
621 	char *rtcp_audio_interval_msec;
622 	char *rtcp_video_interval_msec;
623 
624 	char *sdp_username;
625 	char *sipip;
626 	char *extsipip;
627 	char *url;
628 	char *public_url;
629 	char *bindurl;
630 	char *ws_bindurl;
631 	char *wss_bindurl;
632 	char *tls_url;
633 	char *tls_public_url;
634 	char *tls_bindurl;
635 	char *tcp_public_contact;
636 	char *tls_public_contact;
637 	char *tcp_contact;
638 	char *tls_contact;
639 	char *sla_contact;
640 	char *sipdomain;
641 	char *timer_name;
642 	char *hold_music;
643 	char *outbound_proxy;
644 	char *bind_params;
645 	char *tls_bind_params;
646 	char *tls_cert_dir;
647 	char *reg_domain;
648 	char *sub_domain;
649 	char *reg_db_domain;
650 	char *user_agent;
651 	char *record_template;
652 	char *record_path;
653 	char *presence_hosts;
654 	char *presence_privacy;
655 	char *challenge_realm;
656 	char *pnp_prov_url;
657 	char *pnp_notify_profile;
658 	int sip_user_ping_max;
659 	int sip_user_ping_min;
660 	sofia_cid_type_t cid_type;
661 	switch_core_media_dtmf_t dtmf_type;
662 	int auto_restart;
663 	switch_port_t sip_port;
664 	switch_port_t extsipport;
665 	switch_port_t tls_sip_port;
666 	char *tls_ciphers;
667 	int tls_version;
668 	unsigned int tls_timeout;
669 	char *inbound_codec_string;
670 	char *outbound_codec_string;
671 	int running;
672 	int dtmf_duration;
673 	uint8_t flags[TFLAG_MAX];
674 	uint8_t pflags[PFLAG_MAX];
675 	switch_core_media_flag_t media_flags[SCMF_MAX];
676 	unsigned int mflags;
677 	unsigned int ndlb;
678 	unsigned int mndlb;
679 	uint32_t max_calls;
680 	uint32_t nonce_ttl;
681 	uint32_t max_auth_validity;
682 	nua_t *nua;
683 	switch_memory_pool_t *pool;
684 	su_root_t *s_root;
685 	sip_alias_node_t *aliases;
686 	switch_payload_t cng_pt;
687 	uint32_t codec_flags;
688 	switch_mutex_t *ireg_mutex;
689 	switch_mutex_t *dbh_mutex;
690 	switch_mutex_t *gateway_mutex;
691 	sofia_gateway_t *gateways;
692 	//su_home_t *home;
693 	switch_hash_t *chat_hash;
694 	switch_hash_t *reg_nh_hash;
695 	switch_hash_t *mwi_debounce_hash;
696 	//switch_core_db_t *master_db;
697 	switch_thread_rwlock_t *rwlock;
698 	switch_mutex_t *flag_mutex;
699 	uint32_t inuse;
700 	time_t started;
701 	uint32_t session_timeout;
702 	uint32_t minimum_session_expires;
703 	uint32_t max_proceeding;
704 	uint32_t rtp_timeout_sec;
705 	uint32_t rtp_hold_timeout_sec;
706 	char *odbc_dsn;
707 	char *pre_trans_execute;
708 	char *post_trans_execute;
709 	char *inner_pre_trans_execute;
710 	char *inner_post_trans_execute;
711 	switch_sql_queue_manager_t *qm;
712 	char *acl[SOFIA_MAX_ACL];
713 	char *acl_pass_context[SOFIA_MAX_ACL];
714 	char *acl_fail_context[SOFIA_MAX_ACL];
715 	uint32_t acl_count;
716 	char *proxy_acl[SOFIA_MAX_ACL];
717 	uint32_t proxy_acl_count;
718 	char *reg_acl[SOFIA_MAX_ACL];
719 	uint32_t reg_acl_count;
720 	char *nat_acl[SOFIA_MAX_ACL];
721 	uint32_t nat_acl_count;
722 	char *cand_acl[SWITCH_MAX_CAND_ACL];
723 	uint32_t cand_acl_count;
724 	int server_rport_level;
725 	int client_rport_level;
726 	sofia_presence_type_t pres_type;
727 	sofia_presence_held_calls_type_t pres_held_type;
728 	sofia_media_options_t media_options;
729 	uint32_t force_subscription_expires;
730 	uint32_t force_publish_expires;
731 	char *user_agent_filter;
732 	uint32_t max_registrations_perext;
733 	switch_rtp_bug_flag_t auto_rtp_bugs;
734 	switch_rtp_bug_flag_t manual_rtp_bugs;
735 	switch_rtp_bug_flag_t manual_video_rtp_bugs;
736 	uint32_t ib_calls;
737 	uint32_t ob_calls;
738 	uint32_t ib_failed_calls;
739 	uint32_t ob_failed_calls;
740 	uint32_t timer_t1;
741 	uint32_t timer_t1x64;
742 	uint32_t timer_t2;
743 	uint32_t timer_t4;
744 	char *contact_user;
745 	char *local_network;
746 	uint32_t trans_timeout;
747 	switch_time_t last_sip_event;
748 	switch_time_t last_root_step;
749 	uint32_t step_timeout;
750 	uint32_t event_timeout;
751 	int watchdog_enabled;
752 	switch_mutex_t *gw_mutex;
753 	uint32_t queued_events;
754 	uint32_t last_cseq;
755 	int tls_only;
756 	int tls_verify_date;
757 	enum tport_tls_verify_policy tls_verify_policy;
758 	int tls_verify_depth;
759 	char *tls_passphrase;
760 	char *tls_verify_in_subjects_str;
761 	su_strlst_t *tls_verify_in_subjects;
762 	uint32_t sip_force_expires;
763 	uint32_t sip_force_expires_min;
764 	uint32_t sip_force_expires_max;
765 	uint32_t sip_expires_max_deviation;
766 	uint32_t sip_expires_late_margin;
767 	uint32_t sip_subscription_max_deviation;
768 	int ireg_seconds;
769 	int iping_seconds;
770 	int iping_freq;
771 	sofia_paid_type_t paid_type;
772 	uint32_t rtp_digit_delay;
773 	switch_queue_t *event_queue;
774 	switch_thread_t *thread;
775 	switch_core_media_vflag_t vflags;
776 	char *ws_ip;
777 	switch_port_t ws_port;
778 	char *wss_ip;
779 	switch_port_t wss_port;
780 	int socket_tcp_keepalive;
781 	int tcp_keepalive;
782 	int tcp_pingpong;
783 	int tcp_ping2pong;
784 	ka_type_t keepalive;
785 	int bind_attempts;
786 	int bind_attempt_interval;
787 	char *proxy_notify_events;
788 	char *proxy_info_content_types;
789 	char *rfc7989_filter;
790 	char *acl_inbound_x_token_header;
791 	char *acl_proxy_x_token_header;
792 };
793 
794 
795 struct private_object {
796 	sofia_private_t *sofia_private;
797 	uint8_t flags[TFLAG_MAX];
798 	switch_core_session_t *session;
799 	switch_channel_t *channel;
800 	switch_media_handle_t *media_handle;
801 	switch_core_media_params_t mparams;
802 	switch_caller_profile_t *caller_profile;
803 	sofia_profile_t *profile;
804 	char *reply_contact;
805 	char *from_uri;
806 	char *from_user;
807 	char *to_uri;
808 	char *callid;
809 	char *contact_url;
810 	char *from_str;
811 	char *rpid;
812 	char *asserted_id;
813 	char *preferred_id;
814 	char *privacy;
815 	char *gateway_from_str;
816 	char *dest;
817 	char *dest_to;
818 	char *key;
819 	char *kick;
820 	char *origin;
821 	char *hash_key;
822 	char *chat_from;
823 	char *chat_to;
824 	char *e_dest;
825 	char *call_id;
826 	char *invite_contact;
827 	char *local_url;
828 	char *gateway_name;
829 	char *record_route;
830 	char *route_uri;
831 	char *x_freeswitch_support_remote;
832 	char *x_freeswitch_support_local;
833 	char *last_sent_callee_id_name;
834 	char *last_sent_callee_id_number;
835 	char *proxy_refer_uuid;
836 	msg_t *proxy_refer_msg;
837 	switch_mutex_t *flag_mutex;
838 	switch_mutex_t *sofia_mutex;
839 	switch_payload_t te;
840 	switch_payload_t recv_te;
841 	switch_payload_t bte;
842 	switch_payload_t cng_pt;
843 	switch_payload_t bcng_pt;
844 	sofia_transport_t transport;
845 	nua_handle_t *nh;
846 	nua_handle_t *nh2;
847 	sip_contact_t *contact;
848 	int q850_cause;
849 	int got_bye;
850 	int sent_100;
851 	nua_event_t want_event;
852 	switch_rtp_bug_flag_t rtp_bugs;
853 	char *user_via;
854 	char *redirected;
855 	sofia_cid_type_t cid_type;
856 	uint32_t session_timeout;
857 	enum nua_session_refresher session_refresher;
858 	int update_refresher;
859 	char **watch_headers;
860 	char *respond_phrase;
861 	int respond_code;
862 	char *respond_dest;
863 	switch_time_t last_vid_info;
864 	uint32_t keepalive;
865 	uint32_t sent_invites;
866 	uint32_t recv_invites;
867 	uint8_t sent_last_invite;
868 	uint32_t req_media_counter;
869 };
870 
871 
872 struct callback_t {
873 	char *val;
874 	switch_size_t len;
875 	switch_console_callback_match_t *list;
876 	int matches;
877 	time_t time;
878 	const char *contact_str;
879 	long exptime;
880 };
881 
882 typedef enum {
883 	REG_REGISTER,
884 	REG_AUTO_REGISTER,
885 	REG_INVITE,
886 } sofia_regtype_t;
887 
888 typedef enum {
889 	AUTH_OK,
890 	AUTH_FORBIDDEN,
891 	AUTH_STALE,
892 	AUTH_RENEWED,
893 } auth_res_t;
894 
895 typedef struct {
896 	char *to;
897 	char *contact;
898 	char *route;
899 	char *route_uri;
900 } sofia_destination_t;
901 
902 typedef struct {
903 	char network_ip[80];
904 	int network_port;
905 	const char *is_nat;
906 	int is_auto_nat;
907 	int fs_path;
908 } sofia_nat_parse_t;
909 
910 
911 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
912 
913 
914 
915 #define sofia_test_media_flag(obj, flag) ((obj)->media_flags[flag] ? 1 : 0)
916 #define sofia_set_media_flag(obj, flag) (obj)->media_flags[flag] = 1
917 #define sofia_set_media_flag_locked(obj, flag) assert(obj->flag_mutex != NULL);\
918 switch_mutex_lock(obj->flag_mutex);\
919 (obj)->media_flags[flag] = 1;\
920 switch_mutex_unlock(obj->flag_mutex);
921 #define sofia_clear_media_flag_locked(obj, flag) switch_mutex_lock(obj->flag_mutex); (obj)->media_flags[flag] = 0; switch_mutex_unlock(obj->flag_mutex);
922 #define sofia_clear_media_flag(obj, flag) (obj)->media_flags[flag] = 0
923 
924 
925 #define sofia_test_pflag(obj, flag) ((obj)->pflags[flag] ? 1 : 0)
926 #define sofia_set_pflag(obj, flag) (obj)->pflags[flag] = 1
927 #define sofia_set_pflag_locked(obj, flag) assert(obj->flag_mutex != NULL);\
928 switch_mutex_lock(obj->flag_mutex);\
929 (obj)->pflags[flag] = 1;\
930 switch_mutex_unlock(obj->flag_mutex);
931 #define sofia_clear_pflag_locked(obj, flag) switch_mutex_lock(obj->flag_mutex); (obj)->pflags[flag] = 0; switch_mutex_unlock(obj->flag_mutex);
932 #define sofia_clear_pflag(obj, flag) (obj)->pflags[flag] = 0
933 
934 
935 
936 #define sofia_set_flag_locked(obj, flag) assert(obj->flag_mutex != NULL);\
937 switch_mutex_lock(obj->flag_mutex);\
938 (obj)->flags[flag] = 1;\
939 switch_mutex_unlock(obj->flag_mutex);
940 #define sofia_set_flag(obj, flag) (obj)->flags[flag] = 1
941 #define sofia_clear_flag(obj, flag) (obj)->flags[flag] = 0
942 #define sofia_clear_flag_locked(obj, flag) switch_mutex_lock(obj->flag_mutex); (obj)->flags[flag] = 0; switch_mutex_unlock(obj->flag_mutex);
943 #define sofia_test_flag(obj, flag) ((obj)->flags[flag] ? 1 : 0)
944 
945 /* Function Prototypes */
946 /*************************************************************************************************************************************************************/
947 
948 void sofia_glue_global_standby(switch_bool_t on);
949 
950 switch_status_t sofia_media_activate_rtp(private_object_t *tech_pvt);
951 
952 const char *sofia_media_get_codec_string(private_object_t *tech_pvt);
953 
954 void sofia_glue_attach_private(switch_core_session_t *session, sofia_profile_t *profile, private_object_t *tech_pvt, const char *channame);
955 
956 switch_status_t sofia_glue_do_invite(switch_core_session_t *session);
957 
958 uint8_t sofia_media_negotiate_sdp(switch_core_session_t *session, const char *r_sdp, switch_sdp_type_t type);
959 
960 void sofia_handle_sip_i_refer(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, switch_core_session_t *session, sip_t const *sip,
961 								sofia_dispatch_event_t *de, tagi_t tags[]);
962 
963 switch_status_t sofia_proxy_sip_i_info(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, switch_core_session_t *session, sip_t const *sip,
964 								sofia_dispatch_event_t *de, tagi_t tags[]);
965 void sofia_handle_sip_i_info(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, switch_core_session_t *session, sip_t const *sip,
966 								sofia_dispatch_event_t *de, tagi_t tags[]);
967 switch_status_t sofia_proxy_sip_i_message(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, switch_core_session_t *session, sip_t const *sip,
968 										  sofia_dispatch_event_t *de, tagi_t tags[]);
969 void sofia_handle_sip_i_invite(switch_core_session_t *session, nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, sofia_dispatch_event_t *de, tagi_t tags[]);
970 void sofia_handle_sip_i_invite_replaces(switch_core_session_t *session, switch_channel_t *channel, switch_channel_t *b_channel, char* uuid, private_object_t *tech_pvt, sip_call_info_t *call_info, sofia_profile_t *profile, char *is_nat, sip_t const *sip);
971 
972 
973 void sofia_reg_handle_sip_i_register(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t **sofia_private, sip_t const *sip,
974 								sofia_dispatch_event_t *de,
975 									 tagi_t tags[]);
976 
977 void sofia_event_callback(nua_event_t event,
978 						  int status,
979 						  char const *phrase,
980 						  nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip,
981 						  tagi_t tags[]);
982 
983 void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void *obj);
984 
985 void launch_sofia_profile_thread(sofia_profile_t *profile);
986 
987 switch_status_t sofia_presence_chat_send(switch_event_t *message_event);
988 
989 #define RFC7989_SESSION_UUID_LEN 32
990 #define RFC7989_SESSION_UUID_NULL "00000000000000000000000000000000"
991 
992 int sofia_glue_is_valid_session_id(const char *session_id);
993 void sofia_glue_store_session_id(switch_core_session_t *session, sofia_profile_t *profile, sip_t const *sip, switch_bool_t is_reply);
994 char *sofia_glue_session_id_header(switch_core_session_t *session, sofia_profile_t *profile);
995 
996 /*
997  * \brief Sets the "ep_codec_string" channel variable, parsing r_sdp and taing codec_string in consideration
998  * \param channel Current channel
999  * \param codec_string The profile's codec string or NULL if inexistant
1000  * \param sdp The parsed SDP content
1001  */
1002 void sofia_media_set_r_sdp_codec_string(switch_core_session_t *session, const char *codec_string, sdp_session_t *sdp);
1003 switch_status_t sofia_media_tech_media(private_object_t *tech_pvt, const char *r_sdp, switch_sdp_type_t type);
1004 char *sofia_reg_find_reg_url(sofia_profile_t *profile, const char *user, const char *host, char *val, switch_size_t len);
1005 void event_handler(switch_event_t *event);
1006 void sofia_presence_event_handler(switch_event_t *event);
1007 
1008 
1009 void sofia_presence_cancel(void);
1010 switch_status_t config_sofia(sofia_config_t reload, char *profile_name);
1011 switch_status_t config_gateway(const char *profile_name, const char *gateway_name);
1012 void sofia_reg_auth_challenge(sofia_profile_t *profile, nua_handle_t *nh, sofia_dispatch_event_t *de,
1013 							  sofia_regtype_t regtype, const char *realm, int stale, long exptime);
1014 auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t const *authorization,
1015 								sip_t const *sip,
1016 								sofia_dispatch_event_t *de, const char *regstr, char *np, size_t nplen, char *ip, int network_port, switch_event_t **v_event,
1017 								long exptime, sofia_regtype_t regtype, const char *to_user, switch_event_t **auth_params, long *reg_count, switch_xml_t *user_xml);
1018 
1019 
1020 void sofia_reg_handle_sip_r_challenge(int status,
1021 									  char const *phrase,
1022 									  nua_t *nua, sofia_profile_t *profile,
1023 									  nua_handle_t *nh, sofia_private_t *sofia_private,
1024 									  switch_core_session_t *session, sofia_gateway_t *gateway, sip_t const *sip,
1025 								sofia_dispatch_event_t *de, tagi_t tags[]);
1026 void sofia_reg_handle_sip_r_register(int status,
1027 									 char const *phrase,
1028 									 nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip,
1029 								sofia_dispatch_event_t *de,
1030 									 tagi_t tags[]);
1031 void sofia_handle_sip_i_options(int status, char const *phrase, nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private,
1032 								sip_t const *sip,
1033 								sofia_dispatch_event_t *de, tagi_t tags[]);
1034 void sofia_presence_handle_sip_i_publish(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip,
1035 								sofia_dispatch_event_t *de,
1036 										 tagi_t tags[]);
1037 void sofia_presence_handle_sip_i_message(int status, char const *phrase, nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh,
1038 										 switch_core_session_t *session, sofia_private_t *sofia_private, sip_t const *sip,
1039 								sofia_dispatch_event_t *de, tagi_t tags[]);
1040 void sofia_presence_handle_sip_r_subscribe(int status, char const *phrase, nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh,
1041 										   sofia_private_t *sofia_private, sip_t const *sip,
1042 								sofia_dispatch_event_t *de, tagi_t tags[]);
1043 void sofia_presence_handle_sip_i_subscribe(int status, char const *phrase, nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh,
1044 										   sofia_private_t *sofia_private, sip_t const *sip,
1045 								sofia_dispatch_event_t *de, tagi_t tags[]);
1046 
1047 void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic);
1048 void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex);
1049 void sofia_glue_actually_execute_sql_trans(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex);
1050 void sofia_glue_execute_sql_now(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic);
1051 void sofia_glue_execute_sql_soon(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic);
1052 void sofia_reg_check_expire(sofia_profile_t *profile, time_t now, int reboot);
1053 void sofia_reg_check_ping_expire(sofia_profile_t *profile, time_t now, int interval);
1054 void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now);
1055 void sofia_sub_check_gateway(sofia_profile_t *profile, time_t now);
1056 void sofia_reg_unregister(sofia_profile_t *profile);
1057 
1058 
1059 void sofia_glue_pass_sdp(private_object_t *tech_pvt, char *sdp);
1060 switch_call_cause_t sofia_glue_sip_cause_to_freeswitch(int status);
1061 void sofia_glue_do_xfer_invite(switch_core_session_t *session);
1062 uint8_t sofia_reg_handle_register_token(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip,
1063 								  sofia_dispatch_event_t *de,
1064 								  sofia_regtype_t regtype, char *key,
1065 								  uint32_t keylen, switch_event_t **v_event, const char *is_nat, sofia_private_t **sofia_private_p, switch_xml_t *user_xml, const char *sw_acl_token);
1066 #define sofia_reg_handle_register(_nua_, _profile_, _nh_, _sip_, _de_, _regtype_, _key_, _keylen_, _v_event_, _is_nat_, _sofia_private_p_, _user_xml_) sofia_reg_handle_register_token(_nua_, _profile_, _nh_, _sip_, _de_, _regtype_, _key_, _keylen_, _v_event_, _is_nat_, _sofia_private_p_, _user_xml_, NULL)
1067 extern switch_endpoint_interface_t *sofia_endpoint_interface;
1068 void sofia_presence_set_chat_hash(private_object_t *tech_pvt, sip_t const *sip);
1069 switch_status_t sofia_on_hangup(switch_core_session_t *session);
1070 char *sofia_glue_get_url_from_contact(char *buf, uint8_t to_dup);
1071 char *sofia_glue_get_path_from_contact(char *buf);
1072 char *sofia_glue_get_profile_url(sofia_profile_t *profile, char *remote_ip, const sofia_transport_t transport);
1073 void sofia_presence_set_hash_key(char *hash_key, int32_t len, sip_t const *sip);
1074 void sofia_glue_sql_close(sofia_profile_t *profile, time_t prune);
1075 int sofia_glue_init_sql(sofia_profile_t *profile);
1076 char *sofia_overcome_sip_uri_weakness(switch_core_session_t *session, const char *uri, const sofia_transport_t transport, switch_bool_t uri_only,
1077 									  const char *params, const char *invite_tel_params);
1078 switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback,
1079 											  void *pdata);
1080 char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len);
1081 void sofia_glue_del_profile(sofia_profile_t *profile);
1082 
1083 switch_status_t sofia_glue_add_profile(char *key, sofia_profile_t *profile);
1084 void sofia_glue_release_profile__(const char *file, const char *func, int line, sofia_profile_t *profile);
1085 #define sofia_glue_release_profile(x) sofia_glue_release_profile__(__FILE__, __SWITCH_FUNC__, __LINE__,  x)
1086 
1087 sofia_profile_t *sofia_glue_find_profile__(const char *file, const char *func, int line, const char *key);
1088 #define sofia_glue_find_profile(x) sofia_glue_find_profile__(__FILE__, __SWITCH_FUNC__, __LINE__,  x)
1089 
1090 #define sofia_glue_profile_rdlock(x) sofia_glue_profile_rdlock__(__FILE__, __SWITCH_FUNC__, __LINE__, x)
1091 switch_status_t sofia_glue_profile_rdlock__(const char *file, const char *func, int line, sofia_profile_t *profile);
1092 
1093 switch_status_t sofia_reg_add_gateway(sofia_profile_t *profile, const char *key, sofia_gateway_t *gateway);
1094 sofia_gateway_t *sofia_reg_find_gateway__(const char *file, const char *func, int line, const char *key);
1095 #define sofia_reg_find_gateway(x) sofia_reg_find_gateway__(__FILE__, __SWITCH_FUNC__, __LINE__,  x)
1096 
1097 sofia_gateway_t *sofia_reg_find_gateway_by_realm__(const char *file, const char *func, int line, const char *key);
1098 #define sofia_reg_find_gateway_by_realm(x) sofia_reg_find_gateway_by_realm__(__FILE__, __SWITCH_FUNC__, __LINE__,  x)
1099 
1100 sofia_gateway_subscription_t *sofia_find_gateway_subscription(sofia_gateway_t *gateway_ptr, const char *event);
1101 
1102 #define sofia_reg_gateway_rdlock(x) sofia_reg_gateway_rdlock__(__FILE__, __SWITCH_FUNC__, __LINE__,  x)
1103 switch_status_t sofia_reg_gateway_rdlock__(const char *file, const char *func, int line, sofia_gateway_t *gateway);
1104 
1105 void sofia_reg_release_gateway__(const char *file, const char *func, int line, sofia_gateway_t *gateway);
1106 #define sofia_reg_release_gateway(x) sofia_reg_release_gateway__(__FILE__, __SWITCH_FUNC__, __LINE__, x);
1107 
1108 #define sofia_use_soa(_t) sofia_test_flag(_t, TFLAG_ENABLE_SOA)
1109 
1110 #define sofia_test_extra_headers(val) (((!strncasecmp(val, "X-", 2) && strncasecmp(val, "X-FS-", 5)) || !strncasecmp(val, "P-", 2) || !strncasecmp(val, "On", 2)) ? 1 : 0)
1111 
1112 #define check_decode(_var, _session) do {								\
1113 		assert(_session);												\
1114 		if (!zstr(_var)) {								\
1115 			int d = 0;													\
1116 			char *p;													\
1117 			if (strchr(_var, '%')) {									\
1118 				char *tmp = switch_core_session_strdup(_session, _var);	\
1119 				switch_url_decode(tmp);									\
1120 				_var = tmp;												\
1121 				d++;													\
1122 			}															\
1123 			if ((p = strchr(_var, '"'))) {								\
1124 				if (!d) {												\
1125 					char *tmp = switch_core_session_strdup(_session, _var); \
1126 					_var = tmp;											\
1127 				}														\
1128 				if ((p = strchr(_var, '"'))) {							\
1129 					_var = p+1;											\
1130 				}														\
1131 				if ((p = strrchr(_var, '"'))) {							\
1132 					*p = '\0';											\
1133 				}														\
1134 			}															\
1135 		}																\
1136 																		\
1137 		break;											\
1138 	} while (0)
1139 
1140 
1141 /*
1142  * Transport handling helper functions
1143  */
1144 sofia_transport_t sofia_glue_via2transport(const sip_via_t * via);
1145 sofia_transport_t sofia_glue_url2transport(const url_t *url);
1146 sofia_transport_t sofia_glue_str2transport(const char *str);
1147 enum tport_tls_verify_policy sofia_glue_str2tls_verify_policy(const char * str);
1148 
1149 const char *sofia_glue_transport2str(const sofia_transport_t tp);
1150 char *sofia_glue_find_parameter(const char *str, const char *param);
1151 char *sofia_glue_find_parameter_value(switch_core_session_t *session, const char *str, const char *param);
1152 char *sofia_glue_create_via(switch_core_session_t *session, const char *ip, switch_port_t port, sofia_transport_t transport);
1153 char *sofia_glue_create_external_via(switch_core_session_t *session, sofia_profile_t *profile, sofia_transport_t transport);
1154 char *sofia_glue_strip_uri(const char *str);
1155 
1156 int sofia_glue_transport_has_tls(const sofia_transport_t tp);
1157 const char *sofia_glue_get_unknown_header(sip_t const *sip, const char *name);
1158 switch_status_t sofia_media_build_crypto(private_object_t *tech_pvt, int index, switch_rtp_crypto_key_type_t type, switch_rtp_crypto_direction_t direction);
1159 
1160 void sofia_presence_event_thread_start(void);
1161 void sofia_reg_expire_call_id(sofia_profile_t *profile, const char *call_id, int reboot);
1162 void sofia_reg_check_call_id(sofia_profile_t *profile, const char *call_id);
1163 void sofia_reg_check_sync(sofia_profile_t *profile);
1164 
1165 
1166 char *sofia_glue_get_register_host(const char *uri);
1167 const char *sofia_glue_strip_proto(const char *uri);
1168 void sofia_glue_del_gateway(sofia_gateway_t *gp);
1169 void sofia_glue_gateway_list(sofia_profile_t *profile, switch_stream_handle_t *stream, int up);
1170 void sofia_glue_del_every_gateway(sofia_profile_t *profile);
1171 void sofia_reg_send_reboot(sofia_profile_t *profile, const char *callid, const char *user, const char *host, const char *contact, const char *user_agent,
1172 						   const char *network_ip);
1173 void sofia_glue_restart_all_profiles(void);
1174 const char *sofia_state_string(int state);
1175 void sofia_wait_for_reply(struct private_object *tech_pvt, nua_event_t event, uint32_t timeout);
1176 
1177 /* sofia api */
1178 switch_status_t cmd_json_status(char **argv, int argc, switch_stream_handle_t *stream);
1179 uint32_t sofia_profile_reg_count(sofia_profile_t *profile);
1180 void add_sofia_json_apis(switch_loadable_module_interface_t **module_interface);
1181 
1182 /*
1183  * Logging control functions
1184  */
1185 
1186 /*!
1187  * \brief Changes the loglevel of a sofia component
1188  * \param name the sofia component on which to change the loglevel, or "all" to change them all
1189  * \note Valid components are "all", "default" (sofia's default logger), "tport", "iptsec", "nea", "nta", "nth_client", "nth_server", "nua", "soa", "sresolv", "stun"
1190  * \return SWITCH_STATUS_SUCCESS or SWITCH_STATUS_FALSE if the component isnt valid, or the level is out of range
1191  */
1192 switch_status_t sofia_set_loglevel(const char *name, int level);
1193 
1194 /*!
1195  * \brief Gets the loglevel of a sofia component
1196  * \param name the sofia component on which to change the loglevel
1197  * \note Valid components are "default" (sofia's default logger), "tport", "iptsec", "nea", "nta", "nth_client", "nth_server", "nua", "soa", "sresolv", "stun"
1198  * \return the component's loglevel, or -1 if the component isn't valid
1199  */
1200 int sofia_get_loglevel(const char *name);
1201 switch_status_t list_profiles_full(const char *line, const char *cursor, switch_console_callback_match_t **matches, switch_bool_t show_aliases);
1202 switch_status_t list_profiles(const char *line, const char *cursor, switch_console_callback_match_t **matches);
1203 
1204 sofia_cid_type_t sofia_cid_name2type(const char *name);
1205 void sofia_glue_get_addr(msg_t *msg, char *buf, size_t buflen, int *port);
1206 sofia_destination_t *sofia_glue_get_destination(char *data);
1207 void sofia_glue_free_destination(sofia_destination_t *dst);
1208 switch_status_t sofia_glue_send_notify(sofia_profile_t *profile, const char *user, const char *host, const char *event, const char *contenttype,
1209 									   const char *body, const char *o_contact, const char *network_ip, const char *call_id);
1210 char *sofia_glue_get_extra_headers(switch_channel_t *channel, const char *prefix);
1211 void sofia_glue_set_extra_headers(switch_core_session_t *session, sip_t const *sip, const char *prefix);
1212 char *sofia_glue_get_extra_headers_from_event(switch_event_t *event, const char *prefix);
1213 char *sofia_glue_get_non_extra_unknown_headers(sip_t const *sip);
1214 void sofia_update_callee_id(switch_core_session_t *session, sofia_profile_t *profile, sip_t const *sip, switch_bool_t send);
1215 void sofia_send_callee_id(switch_core_session_t *session, const char *name, const char *number);
1216 int sofia_sla_supported(sip_t const *sip);
1217 int sofia_glue_recover(switch_bool_t flush);
1218 int sofia_glue_profile_recover(sofia_profile_t *profile, switch_bool_t flush);
1219 void sofia_profile_destroy(sofia_profile_t *profile);
1220 switch_status_t sip_dig_function(_In_opt_z_ const char *cmd, _In_opt_ switch_core_session_t *session, _In_ switch_stream_handle_t *stream);
1221 const char *sofia_gateway_status_name(sofia_gateway_status_t status);
1222 void sofia_reg_fire_custom_gateway_state_event(sofia_gateway_t *gateway, int status, const char *phrase);
1223 const char *sofia_sip_user_status_name(sofia_sip_user_status_t status);
1224 void sofia_reg_fire_custom_sip_user_state_event(sofia_profile_t *profile, const char *sip_user, const char *contact,
1225 							const char* from_user, const char* from_host, const char *call_id, sofia_sip_user_status_t status, int options_res, const char *phrase);
1226 uint32_t sofia_reg_reg_count(sofia_profile_t *profile, const char *user, const char *host);
1227 char *sofia_media_get_multipart(switch_core_session_t *session, const char *prefix, const char *sdp, char **mp_type);
1228 int sofia_glue_tech_simplify(private_object_t *tech_pvt);
1229 switch_console_callback_match_t *sofia_reg_find_reg_url_multi(sofia_profile_t *profile, const char *user, const char *host);
1230 switch_console_callback_match_t *sofia_reg_find_reg_url_with_positive_expires_multi(sofia_profile_t *profile, const char *user, const char *host, time_t reg_time, const char *contact_str, long exptime);
1231 switch_bool_t sofia_glue_profile_exists(const char *key);
1232 void sofia_glue_global_siptrace(switch_bool_t on);
1233 void sofia_glue_global_capture(switch_bool_t on);
1234 void sofia_glue_global_watchdog(switch_bool_t on);
1235 uint32_t sofia_presence_get_cseq(sofia_profile_t *profile);
1236 
1237 void sofia_glue_build_vid_refresh_message(switch_core_session_t *session, const char *pl);
1238 char *sofia_glue_gen_contact_str(sofia_profile_t *profile, sip_t const *sip, nua_handle_t *nh, sofia_dispatch_event_t *de, sofia_nat_parse_t *np);
1239 void sofia_glue_pause_jitterbuffer(switch_core_session_t *session, switch_bool_t on);
1240 void sofia_process_dispatch_event(sofia_dispatch_event_t **dep);
1241 void sofia_process_dispatch_event_in_thread(sofia_dispatch_event_t **dep);
1242 char *sofia_glue_get_host(const char *str, switch_memory_pool_t *pool);
1243 void sofia_presence_check_subscriptions(sofia_profile_t *profile, time_t now);
1244 void sofia_msg_thread_start(int idx);
1245 void crtp_init(switch_loadable_module_interface_t *module_interface);
1246 int sofia_recover_callback(switch_core_session_t *session);
1247 void sofia_glue_set_name(private_object_t *tech_pvt, const char *channame);
1248 private_object_t *sofia_glue_new_pvt(switch_core_session_t *session);
1249 switch_status_t sofia_init(void);
1250 void sofia_glue_fire_events(sofia_profile_t *profile);
1251 void sofia_event_fire(sofia_profile_t *profile, switch_event_t **event);
1252 void sofia_queue_message(sofia_dispatch_event_t *de);
1253 int sofia_glue_check_nat(sofia_profile_t *profile, const char *network_ip);
1254 void general_event_handler(switch_event_t *event);
1255 
1256 switch_status_t sofia_glue_ext_address_lookup(sofia_profile_t *profile, char **ip, switch_port_t *port,
1257 											  const char *sourceip, switch_memory_pool_t *pool);
1258 void sofia_reg_check_socket(sofia_profile_t *profile, const char *call_id, const char *network_addr, const char *network_ip);
1259 void sofia_reg_close_handles(sofia_profile_t *profile);
1260 
1261 void write_csta_xml_chunk(switch_event_t *event, switch_stream_handle_t stream, const char *csta_event, char *fwd_type);
1262 void sofia_glue_clear_soa(switch_core_session_t *session, switch_bool_t partner);
1263 
1264 /* For Emacs:
1265  * Local Variables:
1266  * mode:c
1267  * indent-tabs-mode:t
1268  * tab-width:4
1269  * c-basic-offset:4
1270  * End:
1271  * For VIM:
1272  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
1273  */
1274