[Freeswitch-svn] [commit] r2060 - in freeswitch/branches/stkn: conf src/mod/endpoints/mod_pjsip

Freeswitch SVN stkn at freeswitch.org
Sun Jul 23 16:34:21 EDT 2006


Author: stkn
Date: Sun Jul 23 16:34:21 2006
New Revision: 2060

Modified:
   freeswitch/branches/stkn/conf/freeswitch.xml
   freeswitch/branches/stkn/src/mod/endpoints/mod_pjsip/mod_pjsip.c

Log:
Add ip=x.x.x.x and support for different transport types (udp only for now).

Modified: freeswitch/branches/stkn/conf/freeswitch.xml
==============================================================================
--- freeswitch/branches/stkn/conf/freeswitch.xml	(original)
+++ freeswitch/branches/stkn/conf/freeswitch.xml	Sun Jul 23 16:34:21 2006
@@ -77,7 +77,9 @@
 
     <configuration name="pjsip.conf" description="PJSIP Configuration">
       <profile name="default">
-	<!-- <param name="ip" value="1.2.3.4"> -->
+	<!-- <param name="ip" value="1.2.3.4"/> -->
+	<!-- note: only udp transport supported for now! -->
+	<!-- <param name="transport" value="udp,tcp,tls"/> -->
         <param name="port" value="5060"/>
         <param name="max-calls" value="10"/>
         <param name="dialplan" value="XML"/>

Modified: freeswitch/branches/stkn/src/mod/endpoints/mod_pjsip/mod_pjsip.c
==============================================================================
--- freeswitch/branches/stkn/src/mod/endpoints/mod_pjsip/mod_pjsip.c	(original)
+++ freeswitch/branches/stkn/src/mod/endpoints/mod_pjsip/mod_pjsip.c	Sun Jul 23 16:34:21 2006
@@ -46,6 +46,8 @@
 static pjsip_inv_callback invite_cb;
 static pj_pool_t *pjsip_pool;
 
+#define DEFAULT_PORT 5060
+
 #define PJ_MAX_STRLEN(str, pjstr) ( sizeof(str) > pjstr.slen ? pjstr.slen : sizeof(str) )
 
 static struct {
@@ -61,8 +63,11 @@
 struct pjsip_profile {
 	pj_str_t local_uri;
 	pj_str_t local_contact;
-	pj_sockaddr_in sip_addr;	//<- really needed?
 
+	pj_sockaddr_in sip_addr;
+	char *sip_ip;
+	int sip_port;
+
 	/* */
 	char name[128];
 	char local_addr[128];
@@ -71,14 +76,13 @@
 	char *codec_order[SWITCH_MAX_CODECS];
 	int codec_order_last;
 
-	int sip_port;
-
 	int max_calls;
 	int calls;
+
+	int flags;
 	switch_hash_t *call_hash;
 };
 
-
 typedef enum {
 	TFLAG_OUTBOUND = (1 << 0),
 	TFLAG_INBOUND = (1 << 1),
@@ -93,6 +97,14 @@
 	TFLAG_TIMER = (1 << 20)
 } tech_flag_t;
 
+typedef enum {
+	PFLAG_TRANSPORT_UDP = (1 << 0),
+	PFLAG_TRANSPORT_TCP = (1 << 1),
+	PFLAG_TRANSPORT_TLS = (1 << 2)
+} profile_flag_t;
+
+#define MAX_TRANSPORTS 3
+
 #define set_param(ptr, val) if ( ptr ) { free( ptr ); ptr = NULL; } if ( val ) { ptr = strdup( val ); }
 
 static switch_status_t create_sdp( pj_pool_t *pool,
@@ -1436,6 +1448,7 @@
 {
 	char *cfgname = MOD_PJSIP_CFG;
 	switch_xml_t config, xml, xmlprofile, param;
+	switch_status_t result = SWITCH_STATUS_SUCCESS;
 
 	if( !( xml = switch_xml_open_cfg( cfgname, &config, NULL ) )) {
                 switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not open config %s\n", cfgname );
@@ -1453,12 +1466,16 @@
 
 		if( !(profile = switch_core_alloc( module_pool, sizeof(struct pjsip_profile))) ) {
 	                switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to allocate new profile %s\n", profile_name );
-        	        return SWITCH_STATUS_TERM;	
+        	        result = SWITCH_STATUS_TERM;
+			goto out;
 		}
 		memset( profile, 0, sizeof( struct pjsip_profile ) );
 
 		switch_copy_string( profile->name, profile_name, sizeof( profile->name ) );
 
+		profile->sip_addr.sin_family = PJ_AF_INET;
+		profile->sip_addr.sin_port = pj_htons( DEFAULT_PORT );
+
 		for( param = switch_xml_child( xmlprofile, "param" ); param; param = param->next )
 		{
 			char *var = (char *) switch_xml_attr_soft( param, "name" );
@@ -1467,8 +1484,19 @@
 			if ( !strcmp( var, "max-calls" ) ) {
 				profile->max_calls = atoi( val );
 			}
+			else if ( !strcmp( var, "ip" ) ) {
+				pj_str_t tmp;
+
+				if( !pj_inet_aton( pj_cstr( &tmp, val ), &profile->sip_addr.sin_addr ) ) {
+	 				switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid address: %s\n", val );
+					result = SWITCH_STATUS_TERM;
+					goto out;
+				}
+				set_param( profile->sip_ip, val );
+			}
 			else if ( !strcmp( var, "port" ) ) {
 				profile->sip_port = atoi( val );
+				profile->sip_addr.sin_port = pj_htons( profile->sip_port );
 			}
 			else if ( !strcmp( var, "dialplan" ) ) {
 				set_param( profile->dialplan, val );
@@ -1477,18 +1505,51 @@
 				set_param( profile->codec_string, val );
 				profile->codec_order_last = switch_separate_string( profile->codec_string, ',', profile->codec_order, SWITCH_MAX_CODECS );
 			}
+			else if ( !strcmp( var, "transport" ) ) {
+				char *tmp[MAX_TRANSPORTS];
+				int max, n;
+
+				max = switch_separate_string( val, ',', tmp, MAX_TRANSPORTS );
+				for( n = 0; n < max; n++ ) {
+					char *tp = tmp[n];
+
+					if( !strcmp( tp, "tcp" ) ) {
+						switch_set_flag( profile, PFLAG_TRANSPORT_TCP );
+					}
+					else if( !strcmp( tp, "udp" ) ) {
+						switch_set_flag( profile, PFLAG_TRANSPORT_UDP );
+					}
+					else if( !strcmp( tp, "tls" ) ) {
+						switch_set_flag( profile, PFLAG_TRANSPORT_TLS );
+					}
+					else {
+						switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unknown transport: %s\n", tp );
+						result = SWITCH_STATUS_TERM;
+						goto out;
+					}
+				}
+			}
 			else {
 				switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unknown option %s (ignored)\n", var );
 			}
 		}		
 
+		/* set default udp transport if none given */
+		if( !switch_test_flag( profile, PFLAG_TRANSPORT_UDP ) &&
+		    !switch_test_flag( profile, PFLAG_TRANSPORT_TCP ) &&
+		    !switch_test_flag( profile, PFLAG_TRANSPORT_TLS ) )
+		{
+			switch_set_flag( profile, PFLAG_TRANSPORT_UDP );
+		}
+
 		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Adding profile %s\n", profile->name );
 		switch_core_hash_insert( globals.profiles_hash, profile->name, profile );
 	}
 
+out:
 	switch_xml_free( xml );
 
-	return SWITCH_STATUS_SUCCESS;
+	return result;
 }
 /* END: mid-level functions */
 
@@ -1579,23 +1640,47 @@
 	for( p_entry = switch_hash_first( module_pool, globals.profiles_hash ); p_entry; p_entry = switch_hash_next( p_entry ) )
 	{
 		struct pjsip_profile *profile;
-		pjsip_transport *transport;
 		void *val;
 
 		switch_hash_this( p_entry, NULL, NULL, &val );
 		profile = val;
 
-		/* add udp transport */
-		profile->sip_addr.sin_family = PJ_AF_INET;
-		profile->sip_addr.sin_addr.s_addr = 0;
-		profile->sip_addr.sin_port = pj_htons( profile->sip_port );
+		if( switch_test_flag( profile, PFLAG_TRANSPORT_UDP ) ) {
+			pjsip_transport *transport;
 
-		status = pjsip_udp_transport_start( globals.sip_endpt, &profile->sip_addr, NULL, 1, &transport );
-		if ( status != PJ_SUCCESS ) {
-	 		switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create UDP transport\n" );
-			return SWITCH_STATUS_TERM;
+			/*
+			 * Standard UDP SIP Transport 
+			 */
+			status = pjsip_udp_transport_start( globals.sip_endpt, &profile->sip_addr, NULL, 1, &transport );
+			if ( status != PJ_SUCCESS ) {
+	 			switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create UDP transport: %s [%s:%d]\n", profile->name, profile->sip_ip, profile->sip_port );
+				return SWITCH_STATUS_TERM;
+			}
+
+			/* add transports name to hash */
+			switch_core_hash_insert( globals.transport_hash, transport->obj_name, profile );
 		}
+#if 0
+/* pjsip_tpfactory doesn't have an obj_name,
+   will have to find another solution to do the
+   incoming request -> profile lookup in on_incoming_call() */
 
+		if( switch_test_flag( profile, PFLAG_TRANSPORT_TCP ) ) {
+			pjsip_tpfactory *transport;
+
+			/*
+			 * TCP SIP Transport 
+			 */
+			status = pjsip_tcp_transport_start( globals.sip_endpt, &profile->sip_addr, 1, &transport );
+			if ( status != PJ_SUCCESS ) {
+	 			switch_log_printf( SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create TCP transport: %s [%s:%d]\n", profile->name, profile->sip_ip, profile->sip_port );
+				return SWITCH_STATUS_TERM;
+			}
+
+			/* add transports name to hash */
+			switch_core_hash_insert( globals.transport_hash, transport->obj_name, profile );
+		}
+#endif
 		/* */
 		{
 			const pj_str_t *hostname;
@@ -1614,9 +1699,6 @@
 
 		/* init call hash */
         	switch_core_hash_init( &profile->call_hash, module_pool );
-
-		/* add transports name to hash */
-		switch_core_hash_insert( globals.transport_hash, transport->obj_name, profile );
 	}
 
 	/* init transaction handling */



More information about the Freeswitch-svn mailing list