fixing modify mid issue

This commit is contained in:
David Yat Sin 2012-07-03 11:48:37 -04:00
parent 5ec258b388
commit a4c4155232
1 changed files with 24 additions and 21 deletions

View File

@ -12,7 +12,7 @@
static switch_xml_config_item_t *get_instructions(megaco_profile_t *profile) ;
static switch_xml_config_item_t *get_peer_instructions(mg_peer_profile_t *profile) ;
static int mg_sap_id;
static switch_status_t modify_mid(char* mid);
static switch_status_t modify_mid(char** pmid);
/****************************************************************************************************************************/
switch_status_t config_profile(megaco_profile_t *profile, switch_bool_t reload)
@ -59,10 +59,11 @@ switch_status_t config_profile(megaco_profile_t *profile, switch_bool_t reload)
profile->total_peers++;
}
if(SWITCH_STATUS_FALSE == (status = modify_mid(profile->mid))){
if(SWITCH_STATUS_FALSE == (status = modify_mid(&profile->mid))){
goto done;
}
profile->idx = ++mg_sap_id;
/* we should break from here , profile name should be unique */
@ -100,7 +101,7 @@ switch_status_t config_profile(megaco_profile_t *profile, switch_bool_t reload)
goto done;
}
if(SWITCH_STATUS_FALSE == (status = modify_mid(peer_profile->mid))){
if(SWITCH_STATUS_FALSE == (status = modify_mid(&peer_profile->mid))){
goto done;
}
@ -213,55 +214,57 @@ static switch_xml_config_item_t *get_instructions(megaco_profile_t *profile) {
/****************************************************************************************************************************/
static switch_status_t modify_mid(char* mid)
static switch_status_t modify_mid(char** pmid)
{
char dup[64];
char* mid = *pmid;
char* dup;
char* val[10];
int count;
switch_status_t status = SWITCH_STATUS_FALSE;
switch_assert(mid);
memset(&dup[0],0,sizeof(dup));
dup = strdup(mid);
/* If MID type is IP then add mid into [] brackets ,
* If MID type is domain then add mid into <> brackets *
*/
strcpy(&dup[0],mid);
count = switch_split(&dup[0], '.', val);
count = switch_split(dup, '.', val);
if(!count) {
/* Input string is not separated by '.', check if its separated by '-' as format could be xxx-xx-xxx/xxx-xx-xx-xxx */
memset(&dup[0],0,sizeof(dup));
strcpy(&dup[0],mid);
if(0 == (count = switch_split(dup, '-', val))){
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid input MID string[%s]\n",mid);
return SWITCH_STATUS_FALSE;
goto done;
}
}
if(('<' == val[0][0]) || ('[' == val[0][0])){
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "MID = %s is already prefixed with proper brackets \n",mid);
return SWITCH_STATUS_SUCCESS;
status = SWITCH_STATUS_SUCCESS;
goto done;
}
/*first check could be if count is 3 means domain name as generally we have xxx-xx-xxx/xxx.xx.xxx domain */
if(3 == count){
/* domain-type, add value into <> */
memset(&dup[0],0,sizeof(dup));
strcpy(&dup[0],mid);
sprintf(mid,"<%s>",dup);
*pmid = switch_mprintf("<%s>", mid);
free(mid);
mid = *pmid;
}else if(4 == count){
/* IP address in xxx.xxx.xxx.xxx format */
memset(&dup[0],0,sizeof(dup));
strcpy(&dup[0],mid);
sprintf(mid,"[%s]",dup);
*pmid = switch_mprintf("[%s]", mid);
free(mid);
mid = *pmid;
}else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid input MID string[%s]\n",mid);
return SWITCH_STATUS_FALSE;
goto done;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Added proper brackets to MID = %s \n",mid);
return SWITCH_STATUS_SUCCESS;
status = SWITCH_STATUS_SUCCESS;
done:
return status;
}