]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
let sip have stun and make suncc happy
authorAnthony Minessale <anthony.minessale@gmail.com>
Wed, 5 Apr 2006 22:46:50 +0000 (22:46 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Wed, 5 Apr 2006 22:46:50 +0000 (22:46 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@1054 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_stun.h
src/mod/endpoints/mod_dingaling/mod_dingaling.c
src/mod/endpoints/mod_exosip/mod_exosip.c
src/switch_stun.c

index 84ebfd5b7bb6cce9acc6fde08d42a5d7356f96cc..a48023576660f4629c594bb6646f108e8bc5b7f1 100644 (file)
@@ -110,7 +110,7 @@ typedef struct {
 
 typedef struct {
        switch_stun_packet_header_t header;
-       switch_stun_packet_attribute_t first_attribute;
+       uint8_t first_attribute[];
 } switch_stun_packet_t;
 
 typedef struct {
@@ -218,7 +218,7 @@ SWITCH_DECLARE(switch_status) switch_stun_lookup (char **ip,
   \param packet the packet in question
   \param attribute the pointer to set up
 */
-#define switch_stun_packet_first_attribute(packet, attribute)  attribute = &packet->first_attribute;
+#define switch_stun_packet_first_attribute(packet, attribute)  attribute = (switch_stun_packet_attribute_t *)(&packet->first_attribute);
 
 /*!
   \brief Increment an attribute pointer to the next attribute in it's packet
index 9c240c23f7f53a537b8f8c03488020e03cc31baf..3725f1aa467bda21932c8c86931bb0d2651171cc 100644 (file)
@@ -268,17 +268,38 @@ static void *SWITCH_THREAD_FUNC negotiate_thread_run(switch_thread *thread, void
                        if (! switch_test_flag(tech_pvt, TFLAG_RTP_READY)) {
                                ldl_candidate_t cand[1];
                                char *advip = tech_pvt->profile->extip ? tech_pvt->profile->extip : tech_pvt->profile->ip;
+                               char *err;
+
                                memset(cand, 0, sizeof(cand));
                                switch_stun_random_string(tech_pvt->local_user, 16, NULL);
 
-                               cand[0].name = "rtp";
-                               cand[0].address = advip;
+
                                cand[0].port = tech_pvt->local_port;
+                               cand[0].address = advip;
+                               
+                               if (!strncasecmp(advip, "stun:", 5)) {
+                                       cand[0].address = tech_pvt->profile->ip;
+                                       if (switch_stun_lookup(&cand[0].address,
+                                                                                  &cand[0].port,
+                                                                                  advip + 5,
+                                                                                  SWITCH_STUN_DEFAULT_PORT,
+                                                                                  &err,
+                                                                                  switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
+                                               switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Stun Failed! %s:%d [%s]\n", advip + 5, SWITCH_STUN_DEFAULT_PORT, err);
+                                               switch_channel_hangup(channel);
+                                               break;
+                                       }
+                                       switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Stun Success %s:%d\n", cand[0].address, cand[0].port);
+                                       cand[0].type = "stun";
+                               } else {
+                                       cand[0].type = "local";
+                               }
+
+                               cand[0].name = "rtp";
                                cand[0].username = tech_pvt->local_user;
                                cand[0].password = tech_pvt->local_user;
                                cand[0].pref = 1;
                                cand[0].protocol = "udp";
-                               cand[0].type = "local";
                                tech_pvt->cand_id = ldl_session_candidates(tech_pvt->dlsession, cand, 1);
                        }
                }
index 8e7fdd79f2ce8263cd6dc54aa19418d02b9d3953..b5c5c2819749bb0805d61adfea7dd0b8a9a29315 100644 (file)
@@ -242,18 +242,42 @@ static switch_status exosip_on_init(switch_core_session *session)
 
        if (switch_test_flag(tech_pvt, TFLAG_OUTBOUND)) {
                char *dest_uri;
+               char *ip, *err;
+               switch_port_t sdp_port;
                switch_codec_interface *codecs[SWITCH_MAX_CODECS];
                int num_codecs = 0;
                /* do SIP Goodies... */
 
+               /* Decide on local IP and rtp port */
+               tech_pvt->local_sdp_audio_port = switch_rtp_request_port();
+               sdp_port = tech_pvt->local_sdp_audio_port;
                /* Generate callerid URI */
-               eXosip_guess_localip(AF_INET, localip, 128);
-               snprintf(from_uri, sizeof(from_uri), "<sip:%s@%s>", tech_pvt->caller_profile->caller_id_number, localip);
+
+               if (!strncasecmp(globals.host, "stun:", 5)) {
+                       if (switch_stun_lookup(&ip,
+                                                                  &sdp_port,
+                                                                  globals.host + 5,
+                                                                  SWITCH_STUN_DEFAULT_PORT,
+                                                                  &err,
+                                                                  switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
+                               switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Stun Failed! %s:%d [%s]\n", globals.port + 5, SWITCH_STUN_DEFAULT_PORT, err);
+                               switch_channel_hangup(channel);
+                               return SWITCH_STATUS_FALSE;
+                       }
+                       switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Stun Success [%s]:[%d]\n", ip, sdp_port);
+               } else if (!strcasecmp(globals.host, "guess")) {
+                       eXosip_guess_localip(AF_INET, localip, 128);                    
+                       ip = localip;
+               } else {
+                       ip = globals.host;
+               }
+
+               snprintf(from_uri, sizeof(from_uri), "<sip:%s@%s>", tech_pvt->caller_profile->caller_id_number, ip);
+
                /* Setup codec negotiation stuffs */
                osip_rfc3264_init(&tech_pvt->sdp_config);
-               /* Decide on local IP and rtp port */
-               strncpy(tech_pvt->local_sdp_audio_ip, localip, sizeof(tech_pvt->local_sdp_audio_ip));
-               tech_pvt->local_sdp_audio_port = switch_rtp_request_port();
+               strncpy(tech_pvt->local_sdp_audio_ip, ip, sizeof(tech_pvt->local_sdp_audio_ip));
+
                /* Initialize SDP */
                sdp_message_init(&tech_pvt->local_sdp);
                sdp_message_v_version_set(tech_pvt->local_sdp, "0");
@@ -262,7 +286,7 @@ static switch_status exosip_on_init(switch_core_session *session)
                sdp_message_s_name_set(tech_pvt->local_sdp, "SIP Call");
                sdp_message_c_connection_add(tech_pvt->local_sdp, -1, "IN", "IP4", tech_pvt->local_sdp_audio_ip, NULL, NULL);
                sdp_message_t_time_descr_add(tech_pvt->local_sdp, "0", "0");
-               snprintf(port, sizeof(port), "%i", tech_pvt->local_sdp_audio_port);
+               snprintf(port, sizeof(port), "%i", sdp_port);
                sdp_message_m_media_add(tech_pvt->local_sdp, "audio", port, NULL, "RTP/AVP");
                /* Add in every codec we support on this outbound call */
                if (globals.codec_string) {
@@ -1059,6 +1083,8 @@ static switch_status exosip_create_call(eXosip_event_t * event)
                struct private_object *tech_pvt;
                switch_codec_interface *codecs[SWITCH_MAX_CODECS];
                int num_codecs = 0;
+               switch_port_t sdp_port;
+               char *ip, *err;
 
                switch_core_session_add_stream(session, NULL);
                if ((tech_pvt = (struct private_object *) switch_core_session_alloc(session, sizeof(struct private_object))) != 0) {
@@ -1099,12 +1125,33 @@ static switch_status exosip_create_call(eXosip_event_t * event)
                        return SWITCH_STATUS_GENERR;
                }
 
-               eXosip_guess_localip(AF_INET, tech_pvt->local_sdp_audio_ip, 50);
                tech_pvt->local_sdp_audio_port = switch_rtp_request_port();
+               sdp_port = tech_pvt->local_sdp_audio_port;
+
+               if (!strncasecmp(globals.host, "stun:", 5)) {
+                       if (switch_stun_lookup(&ip,
+                                                                  &sdp_port,
+                                                                  globals.host + 5,
+                                                                  SWITCH_STUN_DEFAULT_PORT,
+                                                                  &err,
+                                                                  switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
+                               switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Stun Failed! %s:%d [%s]\n", globals.port + 5, SWITCH_STUN_DEFAULT_PORT, err);
+                               switch_channel_hangup(channel);
+                               return SWITCH_STATUS_FALSE;
+                       }
+                       switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Stun Success [%s]:[%d]\n", ip, sdp_port);
+                       switch_copy_string(tech_pvt->local_sdp_audio_ip, ip, sizeof(tech_pvt->local_sdp_audio_ip));
+               } else if (!strcasecmp(globals.host, "guess")) {
+                       eXosip_guess_localip(AF_INET, tech_pvt->local_sdp_audio_ip, sizeof(tech_pvt->local_sdp_audio_ip));                      
+                       ip = tech_pvt->local_sdp_audio_ip;
+               } else {
+                       ip = globals.host;
+                       switch_copy_string(tech_pvt->local_sdp_audio_ip, ip, sizeof(tech_pvt->local_sdp_audio_ip));
+               }
+
                osip_rfc3264_init(&tech_pvt->sdp_config);
                /* Add in what codecs we support locally */
 
-
                if (globals.codec_string) {
                        num_codecs = switch_loadable_module_get_codecs_sorted(codecs,
                                                                                                                                  SWITCH_MAX_CODECS,
@@ -1112,9 +1159,8 @@ static switch_status exosip_create_call(eXosip_event_t * event)
                                                                                                                                  globals.codec_order_last);
                        
                } else {
-                       num_codecs =
-                               switch_loadable_module_get_codecs(switch_core_session_get_pool(session), codecs,
-                                                                                                 sizeof(codecs) / sizeof(codecs[0]));
+                       num_codecs = switch_loadable_module_get_codecs(switch_core_session_get_pool(session), codecs,
+                                                                                                                  sizeof(codecs) / sizeof(codecs[0]));
                }
 
                if (num_codecs > 0) {
@@ -1162,8 +1208,8 @@ static switch_status exosip_create_call(eXosip_event_t * event)
                sdp_message_o_origin_set(tech_pvt->local_sdp, "FreeSWITCH", "0", "0", "IN", "IP4",
                                                                 tech_pvt->local_sdp_audio_ip);
                sdp_message_s_name_set(tech_pvt->local_sdp, "SIP Call");
-               sdp_message_c_connection_add(tech_pvt->local_sdp, -1, "IN", "IP4", tech_pvt->local_sdp_audio_ip, NULL, NULL);
-               snprintf(port, sizeof(port), "%i", tech_pvt->local_sdp_audio_port);
+               sdp_message_c_connection_add(tech_pvt->local_sdp, -1, "IN", "IP4", ip, NULL, NULL);
+               snprintf(port, sizeof(port), "%i", sdp_port);
                sdp_message_m_port_set(tech_pvt->local_sdp, 0, osip_strdup(port));
 
                conn = eXosip_get_audio_connection(remote_sdp);
@@ -1582,8 +1628,8 @@ static int config_exosip(int reload)
        }
 
        if (!globals.host) {
-               switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Setting host to 'localhost' Good luck with that...\n");
-               set_global_host("localhost");
+               switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Setting host to 'guess'\n");
+               set_global_host("guess");
        }
 
        if (!globals.codec_ms) {
index eb65f90c3948017adef13f03d7f71eae71e1505e..34e10f818ab204311dc97f7eb38201a5f50cacf5 100644 (file)
@@ -121,7 +121,6 @@ SWITCH_DECLARE(switch_stun_packet_t *)switch_stun_packet_parse(uint8_t *buf, uin
        packet = (switch_stun_packet_t *) buf;
        packet->header.type = ntohs(packet->header.type);
        packet->header.length = ntohs(packet->header.length);
-       attr = &packet->first_attribute;
        switch_stun_packet_first_attribute(packet, attr);
        do {
                attr->length = ntohs(attr->length);