]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
mod_sofia: fix bugzilla 6341 - dialplan not replacing user in contact string
authorMoises Silva <moy@sangoma.com>
Mon, 28 Feb 2011 21:36:28 +0000 (16:36 -0500)
committerMoises Silva <moy@sangoma.com>
Mon, 28 Feb 2011 21:37:58 +0000 (16:37 -0500)
src/mod/endpoints/mod_sofia/mod_sofia.c

index 5ce681880ce7fae9a95830be7a6e30981c7f1c97..82f4c2ca0280ceaaf802ea688f0660f2b1be2c20 100644 (file)
@@ -3552,102 +3552,104 @@ SWITCH_STANDARD_API(sofia_contact_function)
                                }
                        }
 
-                       if (user_replacement) {
-                               int urlcount = 0;
-                               int copyerr = 0;
-                               char *newreply = NULL;
-                               char *urlstart = NULL;
-                               char *newptr = NULL;
-                               char *bufend = NULL;
-                               char *str = reply;
-                               switch_size_t copysize = 0;
-                               switch_size_t replacesize = strlen(user_replacement);
-                               switch_size_t allocsize = 0;
-                               
-                               /* first pass to count how many URLs we have */
-                               while ((urlstart = strcasestr(str, "sip:")) || (urlstart = strcasestr(str, "sips:"))) {
-                                       urlcount++;
-                                       str = urlstart + 4;
-                               }
+               }
 
-                               if (!urlcount) {
-                                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): no sip URLs found to replace the user\n");
-                                       copyerr++;
-                                       goto copydone;
-                               }
+               switch_mutex_unlock(mod_sofia_globals.hash_mutex);
+       } 
+       
+       reply = (char *) mystream.data;
 
-                               /* this allocates a bit more than needed but better safe than sorry doing more funky math */
-                               allocsize = strlen(reply) + (urlcount * replacesize);
-                               newreply = switch_core_session_alloc(session, allocsize);
-                               if (!newreply) {
-                                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): no buffer space available for replacement\n");
-                                       copyerr++;
-                                       goto copydone;
-                               }
+       if (user_replacement) {
+               int urlcount = 0;
+               int copyerr = 0;
+               char *newreply = NULL;
+               char *urlstart = NULL;
+               char *newptr = NULL;
+               char *bufend = NULL;
+               char *str = reply;
+               switch_size_t copysize = 0;
+               switch_size_t replacesize = strlen(user_replacement);
+               switch_size_t allocsize = 0;
+
+               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "sofia_contact(): trying to replace %s in %s\n",
+                               user_replacement, str);
+               
+               /* first pass to count how many URLs we have */
+               while ((urlstart = strcasestr(str, "sip:")) || (urlstart = strcasestr(str, "sips:"))) {
+                       urlcount++;
+                       str = urlstart + 4;
+               }
 
-                               /* get a working pointer to the new reply */
-                               newptr = newreply;
+               if (!urlcount) {
+                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): no sip URLs found to replace the user\n");
+                       copyerr++;
+                       goto copydone;
+               }
 
-                               /* pointer to the end of the allocated buffer for safety checks */
-                               bufend = newreply + allocsize;
-                               *bufend = 0;
+               /* this allocates a bit more than needed but better safe than sorry doing more funky math */
+               allocsize = strlen(reply) + (urlcount * replacesize);
+               newreply = switch_core_session_alloc(session, allocsize);
+               if (!newreply) {
+                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): no buffer space available for replacement\n");
+                       copyerr++;
+                       goto copydone;
+               }
 
-                               /* go thru all the urls and replace the user part */
-                               str = reply;
-                               while ((urlstart = strcasestr(str, "sip:")) || (urlstart = strcasestr(str, "sips:"))) {
+               /* get a working pointer to the new reply */
+               newptr = newreply;
 
-                                       /* found an URL, copy up to the start of the url */
-                                       copysize = ( urlstart - str ) + 4;
+               /* pointer to the end of the allocated buffer for safety checks */
+               bufend = newreply + allocsize;
+               *bufend = 0;
 
-                                       /* double check boundaries before copying anything at all (this should not happen) */
-                                       if ((newptr + copysize + replacesize) >= bufend) {
-                                               copyerr++;
-                                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): wow buffer was not big enough!\n");
-                                               break;
-                                       }
+               /* go thru all the urls and replace the user part */
+               str = reply;
+               while ((urlstart = strcasestr(str, "sip:")) || (urlstart = strcasestr(str, "sips:"))) {
 
-                                       /* copy the original contact string except for the user */
-                                       memcpy(newptr, str, copysize);
-                                       newptr += copysize;
+                       /* found an URL, copy up to the start of the url */
+                       copysize = ( urlstart - str ) + 4;
 
-                                       /* copy the user replacement */
-                                       memcpy(newptr, user_replacement, replacesize);
-                                       newptr += replacesize;
+                       /* double check boundaries before copying anything at all (this should not happen) */
+                       if ((newptr + copysize + replacesize) >= bufend) {
+                               copyerr++;
+                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): wow buffer was not big enough!\n");
+                               break;
+                       }
 
-                                       /* skip the original user part */
-                                       str = strchr(urlstart, '@');
-                                       if (!str) {
-                                               copyerr++;
-                                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): not host part found for contact\n");
-                                               break;
-                                       }
-                                       /* continue searching for the next sip: URL */
-                               }
+                       /* copy the original contact string except for the user */
+                       memcpy(newptr, str, copysize);
+                       newptr += copysize;
 
-copydone:
-                               if (!copyerr) {
-                                       /* copy the remaining reply string */
-                                       copysize = strlen(str);
-                                       if ((newptr + copysize) >= bufend) {
-                                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): wow buffer was not big enough, close, but not enough!\n");
-                                       } else {
-                                               strcpy(newptr, str);
-                                               reply = newreply;
-                                       }
-                               } else {
-                                       reply = "error/replacement error";
-                               }
-                       }
+                       /* copy the user replacement */
+                       memcpy(newptr, user_replacement, replacesize);
+                       newptr += replacesize;
 
-                       stream->write_function(stream, "%s", reply);
-                       reply = NULL;
+                       /* skip the original user part */
+                       str = strchr(urlstart, '@');
+                       if (!str) {
+                               copyerr++;
+                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): not host part found for contact\n");
+                               break;
+                       }
+                       /* continue searching for the next sip: URL */
+               }
 
-                       switch_safe_free(mystream.data);
+copydone:
+               if (!copyerr) {
+                       /* copy the remaining reply string */
+                       copysize = strlen(str);
+                       if ((newptr + copysize) >= bufend) {
+                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): wow buffer was not big enough, close, but not enough!\n");
+                       } else {
+                               strcpy(newptr, str);
+                               reply = newreply;
+                               goto end;
+                       }
+               } else {
+                       /* on error, we do nothing and just default to the original stream returned (mystream.data) */
                }
+       }
 
-               switch_mutex_unlock(mod_sofia_globals.hash_mutex);
-       } 
-       
        reply = (char *) mystream.data;
 
  end: