<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][16765] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=16765">16765</a></dd>
<dt>Author</dt> <dd>sathieu</dd>
<dt>Date</dt> <dd>2010-02-24 06:00:11 -0600 (Wed, 24 Feb 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Skinny: Milestone 3: Parse calls coming to skinny

Also don't free profile members (managed via pools) to avoid crash on module unload</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnymod_skinnyc">freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodendpointsmod_skinnymod_skinnyc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c (16764 => 16765)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c        2010-02-24 12:00:04 UTC (rev 16764)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c        2010-02-24 12:00:11 UTC (rev 16765)
</span><span class="lines">@@ -59,6 +59,11 @@
</span><span class="cx">         char *odbc_user;
</span><span class="cx">         char *odbc_pass;
</span><span class="cx">         switch_odbc_handle_t *master_odbc;
</span><ins>+        /* stats */
+        uint32_t ib_calls;
+        uint32_t ob_calls;
+        uint32_t ib_failed_calls;
+        uint32_t ob_failed_calls;        
</ins><span class="cx">         /* listener */
</span><span class="cx">         int listener_threads;
</span><span class="cx">         switch_mutex_t *listener_mutex;        
</span><span class="lines">@@ -156,6 +161,8 @@
</span><span class="cx">         switch_mutex_t *mutex;
</span><span class="cx">         switch_mutex_t *flag_mutex;
</span><span class="cx">         //switch_thread_cond_t *cond;
</span><ins>+        char *dest_profile;
+        char *dest;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> typedef struct private_object private_t;
</span><span class="lines">@@ -361,6 +368,7 @@
</span><span class="cx"> 
</span><span class="cx"> /* SKINNY FUNCTIONS */
</span><span class="cx"> static switch_status_t skinny_send_reply(listener_t *listener, skinny_message_t *reply);
</span><ins>+static switch_call_cause_t skinny_session_create(switch_core_session_t *session);
</ins><span class="cx"> 
</span><span class="cx"> /* LISTENER FUNCTIONS */
</span><span class="cx"> static switch_status_t keepalive_listener(listener_t *listener, void *pvt);
</span><span class="lines">@@ -373,6 +381,7 @@
</span><span class="cx">         const char *line = &quot;=================================================================================================&quot;;
</span><span class="cx">         switch_assert(profile);
</span><span class="cx">         stream-&gt;write_function(stream, &quot;%s\n&quot;, line);
</span><ins>+        /* prefs */
</ins><span class="cx">         stream-&gt;write_function(stream, &quot;Name             \t%s\n&quot;, profile-&gt;name);
</span><span class="cx">         stream-&gt;write_function(stream, &quot;Domain Name      \t%s\n&quot;, profile-&gt;domain);
</span><span class="cx">         stream-&gt;write_function(stream, &quot;IP               \t%s\n&quot;, profile-&gt;ip);
</span><span class="lines">@@ -380,7 +389,14 @@
</span><span class="cx">         stream-&gt;write_function(stream, &quot;Dialplan         \t%s\n&quot;, profile-&gt;dialplan);
</span><span class="cx">         stream-&gt;write_function(stream, &quot;Keep-Alive       \t%d\n&quot;, profile-&gt;keep_alive);
</span><span class="cx">         stream-&gt;write_function(stream, &quot;Date-Format      \t%s\n&quot;, profile-&gt;date_format);
</span><ins>+        /* db */
</ins><span class="cx">         stream-&gt;write_function(stream, &quot;DBName           \t%s\n&quot;, profile-&gt;dbname ? profile-&gt;dbname : switch_str_nil(profile-&gt;odbc_dsn));
</span><ins>+        /* stats */
+        stream-&gt;write_function(stream, &quot;CALLS-IN         \t%d\n&quot;, profile-&gt;ib_calls);
+        stream-&gt;write_function(stream, &quot;FAILED-CALLS-IN  \t%d\n&quot;, profile-&gt;ib_failed_calls);
+        stream-&gt;write_function(stream, &quot;CALLS-OUT        \t%d\n&quot;, profile-&gt;ob_calls);
+        stream-&gt;write_function(stream, &quot;FAILED-CALLS-OUT \t%d\n&quot;, profile-&gt;ob_failed_calls);
+        /* listener */
</ins><span class="cx">         stream-&gt;write_function(stream, &quot;Listener-Threads \t%d\n&quot;, profile-&gt;listener_threads);
</span><span class="cx">         stream-&gt;write_function(stream, &quot;%s\n&quot;, line);
</span><span class="cx"> 
</span><span class="lines">@@ -388,7 +404,7 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-static skinny_profile_t *get_profile(const char *profile_name)
</del><ins>+static skinny_profile_t *skinny_find_profile(const char *profile_name)
</ins><span class="cx"> {
</span><span class="cx">         return (skinny_profile_t *) switch_core_hash_find(globals.profile_hash, profile_name);
</span><span class="cx"> }
</span><span class="lines">@@ -781,46 +797,95 @@
</span><span class="cx">                                                                                                         switch_caller_profile_t *outbound_profile,
</span><span class="cx">                                                                                                         switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags, switch_call_cause_t *cancel_cause)
</span><span class="cx"> {
</span><del>-        if ((*new_session = switch_core_session_request(skinny_endpoint_interface, SWITCH_CALL_DIRECTION_OUTBOUND, pool)) != 0) {
-                private_t *tech_pvt;
-                switch_channel_t *channel;
-                switch_caller_profile_t *caller_profile;
</del><ins>+        switch_call_cause_t cause = SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
+        switch_core_session_t *nsession = NULL;
+        private_t *tech_pvt;
+        
+        char *profile_name, *dest;
+        skinny_profile_t *profile = NULL;
+        char name[128];
+        switch_channel_t *channel;
+        switch_caller_profile_t *caller_profile;
</ins><span class="cx"> 
</span><del>-                switch_core_session_add_stream(*new_session, NULL);
-                if ((tech_pvt = (private_t *) switch_core_session_alloc(*new_session, sizeof(private_t))) != 0) {
-                        channel = switch_core_session_get_channel(*new_session);
-                        tech_init(tech_pvt, *new_session);
-                } else {
-                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(*new_session), SWITCH_LOG_CRIT, &quot;Hey where is my memory pool?\n&quot;);
-                        switch_core_session_destroy(new_session);
-                        return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
-                }
</del><ins>+        if (!outbound_profile || zstr(outbound_profile-&gt;destination_number)) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Invalid Destination\n&quot;);
+                goto error;
+        }
</ins><span class="cx"> 
</span><del>-                if (outbound_profile) {
-                        char name[128];
</del><ins>+        if (!(nsession = switch_core_session_request(skinny_endpoint_interface, SWITCH_CALL_DIRECTION_OUTBOUND, pool))) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Error Creating Session\n&quot;);
+                goto error;
+        }
</ins><span class="cx"> 
</span><del>-                        snprintf(name, sizeof(name), &quot;SKINNY/%s&quot;, outbound_profile-&gt;destination_number);
-                        switch_channel_set_name(channel, name);
</del><ins>+        if (!(tech_pvt = (struct private_object *) switch_core_session_alloc(nsession, sizeof(*tech_pvt)))) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, &quot;Error Creating Session private object\n&quot;);
+                goto error;
+        }
</ins><span class="cx"> 
</span><del>-                        caller_profile = switch_caller_profile_clone(*new_session, outbound_profile);
-                        switch_channel_set_caller_profile(channel, caller_profile);
-                        tech_pvt-&gt;caller_profile = caller_profile;
-                } else {
-                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(*new_session), SWITCH_LOG_ERROR, &quot;Doh! no caller profile\n&quot;);
-                        switch_core_session_destroy(new_session);
-                        return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
-                }
</del><ins>+        tech_init(tech_pvt, nsession);
</ins><span class="cx"> 
</span><ins>+        if(!(profile_name = switch_core_session_strdup(nsession, outbound_profile-&gt;destination_number))) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, &quot;Error Creating Session Info\n&quot;);
+                goto error;
+        }
</ins><span class="cx"> 
</span><ins>+        if (!(dest = strchr(profile_name, '/'))) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, &quot;Invalid Skinny URL. Should be skinny/&lt;profile&gt;/&lt;number&gt;.\n&quot;);
+                cause = SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
+                goto error;
+        }
+        *dest++ = '\0';
</ins><span class="cx"> 
</span><del>-                switch_channel_set_flag(channel, CF_OUTBOUND);
-                switch_set_flag_locked(tech_pvt, TFLAG_OUTBOUND);
-                switch_channel_set_state(channel, CS_INIT);
-                return SWITCH_CAUSE_SUCCESS;
</del><ins>+        profile = skinny_find_profile(profile_name);
+        if (!(profile = skinny_find_profile(profile_name))) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, &quot;Invalid Profile %s\n&quot;, profile_name);
+                cause = SWITCH_CAUSE_UNALLOCATED_NUMBER;
+                goto error;
</ins><span class="cx">         }
</span><ins>+        
+        tech_pvt-&gt;dest = switch_core_session_strdup(nsession, dest);
+        tech_pvt-&gt;dest_profile = switch_core_session_strdup(nsession, profile_name);
+        snprintf(name, sizeof(name), &quot;SKINNY/%s/%s&quot;, profile-&gt;name, dest);
</ins><span class="cx"> 
</span><del>-        return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
</del><ins>+        channel = switch_core_session_get_channel(nsession);
+        switch_channel_set_name(channel, name);
+        
</ins><span class="cx"> 
</span><ins>+        caller_profile = switch_caller_profile_clone(nsession, outbound_profile);
+        switch_channel_set_caller_profile(channel, caller_profile);
+        tech_pvt-&gt;caller_profile = caller_profile;
+
+        switch_channel_set_flag(channel, CF_OUTBOUND);
+        switch_set_flag_locked(tech_pvt, TFLAG_OUTBOUND);
+        switch_channel_set_state(channel, CS_INIT);
+
+        cause = skinny_session_create(nsession);
+        
+        if (!(cause == SWITCH_CAUSE_SUCCESS)) {
+                goto error;
+        }
+        
+        *new_session = nsession;
+        cause = SWITCH_CAUSE_SUCCESS;
+        goto done;
+
+  error:
+        if (nsession) {
+                switch_core_session_destroy(&amp;nsession);
+        }
+        *pool = NULL;
+
+
+  done:
+
+        if (profile) {
+                if (cause == SWITCH_CAUSE_SUCCESS) {
+                        profile-&gt;ob_calls++;
+                } else {
+                        profile-&gt;ob_failed_calls++;
+                }
+        }
+        return cause;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static switch_status_t channel_receive_event(switch_core_session_t *session, switch_event_t *event)
</span><span class="lines">@@ -1381,6 +1446,12 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static switch_call_cause_t skinny_session_create(switch_core_session_t *session)
+{
+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, &quot;Channel not implemented\n&quot;);
+        return SWITCH_CAUSE_CHAN_NOT_IMPLEMENTED;
+}
+
</ins><span class="cx"> /*****************************************************************************/
</span><span class="cx"> /* LISTENER FUNCTIONS */
</span><span class="cx"> /*****************************************************************************/
</span><span class="lines">@@ -1910,7 +1981,7 @@
</span><span class="cx"> static switch_status_t cmd_status_profile(const char *profile_name, switch_stream_handle_t *stream)
</span><span class="cx"> {
</span><span class="cx">         skinny_profile_t *profile;
</span><del>-        if ((profile = get_profile(profile_name))) {
</del><ins>+        if ((profile = skinny_find_profile(profile_name))) {
</ins><span class="cx">                 dump_profile(profile, stream);
</span><span class="cx">         } else {
</span><span class="cx">                 stream-&gt;write_function(stream, &quot;Profile not found!\n&quot;);
</span><span class="lines">@@ -1922,7 +1993,7 @@
</span><span class="cx"> static switch_status_t cmd_status_profile_device(const char *profile_name, const char *device_name, switch_stream_handle_t *stream)
</span><span class="cx"> {
</span><span class="cx">         skinny_profile_t *profile;
</span><del>-        if ((profile = get_profile(profile_name))) {
</del><ins>+        if ((profile = skinny_find_profile(profile_name))) {
</ins><span class="cx">                 dump_device(profile, device_name, stream);
</span><span class="cx">         } else {
</span><span class="cx">                 stream-&gt;write_function(stream, &quot;Profile not found!\n&quot;);
</span><span class="lines">@@ -2041,7 +2112,7 @@
</span><span class="cx">                 return status;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if((profile = get_profile(argv[3]))) {
</del><ins>+        if((profile = skinny_find_profile(argv[3]))) {
</ins><span class="cx">                 if ((sql = switch_mprintf(&quot;SELECT device_name FROM skinny_devices&quot;))) {
</span><span class="cx">                         skinny_execute_sql_callback(profile, profile-&gt;listener_mutex, sql, skinny_list_devices_callback, &amp;h);
</span><span class="cx">                         switch_safe_free(sql);
</span><span class="lines">@@ -2139,9 +2210,10 @@
</span><span class="cx"> 
</span><span class="cx">         globals.running = 0;
</span><span class="cx"> 
</span><ins>+        /* kill listeners */
</ins><span class="cx">         walk_listeners(kill_listener, NULL);
</span><span class="cx"> 
</span><del>-        /* launch listeners */
</del><ins>+        /* close sockets */
</ins><span class="cx">         for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
</span><span class="cx">                 switch_hash_this(hi, NULL, NULL, &amp;val);
</span><span class="cx">                 profile = (skinny_profile_t *) val;
</span><span class="lines">@@ -2157,14 +2229,6 @@
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        /* Free dynamically allocated strings */
-        for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
-                switch_hash_this(hi, NULL, NULL, &amp;val);
-                profile = (skinny_profile_t *) val;
-                switch_safe_free(profile-&gt;domain);
-                switch_safe_free(profile-&gt;ip);
-                switch_safe_free(profile-&gt;dialplan);
-        }
</del><span class="cx">         switch_safe_free(globals.codec_string);
</span><span class="cx">         switch_safe_free(globals.codec_rates_string);
</span><span class="cx">         
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>