On 05/15/2013 12:14 PM, Daniel-Constantin Mierla wrote:
Hello,

On 5/15/13 6:43 AM, kiran bhosale wrote:
Hi
we have developed the custom module where in we have created shared memory area which is not used by any other module or process and we locked it using locks as recommended. but because of locks the kamailio is getting crashed every now and then . is it really important to protect the shared memory area with locks. if yes then how to avoid the crash ??

so to locate the crash we built kamailio wit h MM_DBG , and other GCC debug flags . but we are getting error "/No symbol table info available/" . how to locate the bugs. the kamailio is built wit h 1 MB of shared memory on MIPS based VOIP gateway which is having * MB of flash and 16 MB or memory (RAM) .
set ulimit-c unlimited in order to get a core file, then use gdb to get the backtrace from the core. It will show where the crash happened.

Protection to shared memory is needed only when there is potential concurrent access.

Cheers,
Daniel
--
Daniel-Constantin Mierla -http://www.asipto.com
http://twitter.com/#!/miconda  -http://www.linkedin.com/in/miconda
Kamailio Advanced Training, San Francisco, USA - June 24-27, 2013
   *http://asipto.com/u/katu  *


_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
sr-users@lists.sip-router.org
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
we did that as well but we get these backtrace "No symbol table info available " errors when we tried to debug the core file. our requirement is simple . we have not used any DB for storage of users. instead we have implemented local structures for them. but we want to delete these users after the "expires" becomes zero. but when we tried to delete them using kamailio timer or "register_timer" function . it is in no way accessing these local structures . which is why we used shared memory. but it is leading to crash . even we tried to unstrip the kamailio and ld.so . but that also didn't give exact location of crash. our kamailio footprint is around 1.7 MB with logs and 900 KB without logs.


1) is there any possibilty of other process/module corrupting our data it being a shared memroy ?? 2) the modules we used are tm.so , sl.so registrar.so textops.so, auth.so auth_db.so and syntel.so(our module).
3) when  run with  10 instances  of  SIPP it  crashed after  two hours.
is it mandatory to use shared memory for users to be deleted periodically. isnt there any workaround to access local variables from kamailio timers.
i can  share  our code  with you .

please  help


/*
 * Copyright (C) 2013 Syntel Telecom

 *
 * Author: Kiran Bhosale 
 *	 
 * This file is part of sip-router, a free SIP server.
 *
 * sip-router is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * sip-router is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Notes:  14/5/2013 :solved  the issue  of  kamailio getting  crashed  because 
 of  this  module.it was suggested that  the shared  memory  has to  protected  
by  lock which  leaded to freqquent kamailio crashes .now when built  without 
the lock it  runs successfully. 
  */
/***************************************************************************
 * INCLUDES
 ***************************************************************************/
#include "syntel.h"

/***************************************************************************
 * DEFINES
 ***************************************************************************/
/* Called/Calling Party Number related variables */
#define CALLED_PARTY_NUM					1
#define CALLING_PARTY_NUM					2
#define FACILITY_IE							3

/*Supplementary services defines*/
#define SS_ID_CALL_HOLD                     0x01
#define SS_ID_CALL_RETRIEVE                 0x02
#define SS_ID_CALL_TRANSFER                 0x03
#define SS_ID_CONFERENCE                    0x04
#define SS_ID_CANCEL                       	0x05

#define MAX_BUFFER 							256
#define MAX_URI 							128
#define MAX_USER 							8
#define DEFAULT_EXPIRES 					3600
#define EXPRESSION_LENGTH 					100
#define MAX_IP 								32
#define MAX_EXTENSIONS 						16
#define TRUE								1
#define FALSE								0



/***************************************************************************
 * DATA TYPES
 ***************************************************************************/
typedef struct
{
	int max_bindings;
	int expires_value[MAX_USER];
	char ip_addr[MAX_USER][MAX_IP];
	int call_hold_flag;
} syntel_users;

typedef struct
{
	long mtype;
	char mtext[MAX_BUFFER];
} msg_buf;

/***************************************************************************
 * GLOBAL VARIABLES
 ***************************************************************************/
int timer_interval = 1;
static int kamailio_q_tx_id;
static syntel_users *syntel_database = NULL;
//static gen_lock_t *syntel_lock = NULL;
static char *machine_ip;

/***************************************************************************
 * FUNCTION DECLARATION
 ***************************************************************************/
static void timer_1sec_routine(unsigned int ticks, void* param);
static void syntel_create_msg_que(void);
static int mod_init(void);
static void syntel_init_db(void);
static void mod_destroy(void);
static int s_get_ip(void);
static int syntel_get_source_ip(struct sip_msg* msg , char * foo , char  *bar );
static int syntel_rewrite_uri(struct sip_msg* msg , char * foo , char  *bar );
static int syntel_save_on_register(struct sip_msg* msg , char * foo , char  *bar);
static void get_from_user(struct sip_msg* msg , char* from_user);
static int syntel_outgoing_call(struct  sip_msg *msg, char  *foo , char  *bar);
static int syntel_cancel_ic_call(struct sip_msg *msg, char *foo, char *bar);
static void send_msg(char *msg, int len);
static int syntel_call_hold(struct sip_msg *msg, char *foo, char *bar);
static int syntel_call_retrieve(struct sip_msg *msg, char *foo, char *bar);
static int syntel_reset_flag(struct sip_msg *msg, char *foo, char *bar);
static int syntel_call_transfer(struct sip_msg *msg, char *foo, char *bar);
static void send_transfer_target(char *target,int extNo );
static int syntel_check_db(char  *uri, char *extNo);
static int syntel_parse_contacts(struct sip_msg *msg, char *registered_contact);
static int  syntel_is_registered(struct sip_msg *msg, char *foo, char *bar);
static int syntel_save_on_invite(struct sip_msg *msg, char *foo, char *bar);
static int get_server_ip(struct sip_msg *msg, pv_param_t *param,pv_value_t *res);
static void get_to_uri(struct sip_msg *msg,  char *user);

/***************************************************************************
 * GLOBAL DECLARATION
 ***************************************************************************/
cmd_export_t cmds[] = {
	{"syntel_get_source_ip",syntel_get_source_ip, 0, 0, 0,  REQUEST_ROUTE},
	{"syntel_rewrite_uri", syntel_rewrite_uri, 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | ANY_ROUTE},
	{"syntel_is_registered", syntel_is_registered, 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | ANY_ROUTE},
	{"syntel_save_on_register", syntel_save_on_register, 0, 0, 0, REQUEST_ROUTE | ANY_ROUTE},
	{"syntel_save_on_invite", syntel_save_on_invite, 0, 0, 0, REQUEST_ROUTE | ANY_ROUTE},
	{"syntel_outgoing_call", syntel_outgoing_call, 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | ANY_ROUTE},
	{"syntel_call_hold",syntel_call_hold, 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | ANY_ROUTE},
	{"syntel_call_retrieve",syntel_call_retrieve, 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | ANY_ROUTE},
	{"syntel_cancel_ic_call",syntel_cancel_ic_call, 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | ANY_ROUTE},
	{"syntel_reset_flag",syntel_reset_flag, 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | ANY_ROUTE},
	{"syntel_call_transfer",syntel_call_transfer, 0, 0, 0, REQUEST_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE | ANY_ROUTE},
	{0, 0, 0, 0, 0, 0}
};

static pv_export_t mod_pvs[] = {
	{{"server_ip", (sizeof("server_ip")-1)}, /* URI of the first Route-Header */
	PVT_OTHER,get_server_ip, 0, 0, 0, 0, 0},
	{{0, 0}, 0, 0, 0, 0, 0, 0, 0}
};

struct module_exports exports = {
	"syntel",
	DEFAULT_DLFLAGS,	/* dlopen flags */
	cmds,				/* Exported functions */
	0,					/* params */
	0,					/* exported statistics */
	0,					/* exported MI functions NULL */
	mod_pvs,					/* exported pseudo-variables */
	0,					/* extra processes */
	mod_init,			/* Module initialization function */
	0,					/* Response function */
	mod_destroy,		/* Destroy function */
	0					/* Child initialization function */
};

/***************************************************************************
 * GLOBAL FUNCTION DEFINITIONS
 ***************************************************************************/
static void get_to_uri(struct sip_msg *msg,  char *user)
{
	struct sip_uri *uri = NULL;
	int i;
	char *ptr;
	
	if ((uri = parse_to_uri(msg)))
	{	
		i=0;
		ptr=uri->user.s;
		while(*ptr!='@')
		{
			user[i++] = *ptr++;
		}
		user[i] = '\0';	
	}
}

static void get_from_user(struct sip_msg *msg, char  *from_user)
{
	struct sip_uri *uri;
	int i;
	char *ptr;	

	uri=parse_from_uri(msg);
	if(uri!=NULL)
	{			
		ptr = &(uri->user.s[0]);
		i = 0;
		while(*ptr!='@')
		{
			from_user[i++] = *ptr++;
		}
		from_user[i] = '\0';					
	}
}

int syntel_check_db(char  *uri, char *extNo)
{
	int counter;
	int bindingNo;
	syntel_users *pSyntelDb;
	
	*extNo = MAX_EXTENSIONS;
	//lock_get(syntel_lock);	
	for (counter = 0; counter < MAX_EXTENSIONS ; counter++)
	{	
		pSyntelDb = &syntel_database[counter];
		for (bindingNo = 0; bindingNo < pSyntelDb->max_bindings; bindingNo++)
		{
			if (NULL != strstr(uri, pSyntelDb->ip_addr[bindingNo]))
			{
				//lock_release(syntel_lock);
				*extNo = counter;
				return TRUE;			
			}
		}
	}
	//lock_release(syntel_lock);
	
	return FALSE;
}

int syntel_delete(void)
{
	int counter;
	int bindingNo;
	syntel_users *pSyntelDb;
	int i;
	
	//lock_get(syntel_lock);
	for (counter = 0; counter < MAX_EXTENSIONS ; counter++)
	{
		pSyntelDb = &syntel_database[counter];
		for (bindingNo = 0; bindingNo < pSyntelDb->max_bindings; bindingNo++)
		{
			if (pSyntelDb->expires_value[bindingNo] <= 0)
	        {
				LM_ERR("\n \n  the URI  to be deleted %s \n ",pSyntelDb->ip_addr[bindingNo]);
				pSyntelDb->expires_value[bindingNo] = 0;
				pSyntelDb->ip_addr[bindingNo][0] = '\0';
				for(i = bindingNo;i < pSyntelDb->max_bindings-1 ; i++)
				{
					pSyntelDb->expires_value[i] = pSyntelDb->expires_value[i + 1];
					memcpy(pSyntelDb->ip_addr[i], pSyntelDb->ip_addr[i + 1], strlen(pSyntelDb->ip_addr[i + 1]));
				}					
				pSyntelDb->max_bindings--;
            }
            else 
			{ pSyntelDb->expires_value[bindingNo]--; }
		}
	}
	//lock_release(syntel_lock);	
	
	return 0;
}

static int syntel_outgoing_call(struct sip_msg *msg, char *foo, char *bar)
{
	char user[MAX_USER];
	char final_uri[MAX_URI];
	str uri;
	int lineNo = 0;
	int bindingNo = 0;
	int Retval = -1;
	syntel_users *pSyntelDb;
	
	get_to_uri(msg,user);
	for (lineNo = 0; lineNo < MAX_EXTENSIONS; lineNo++)
	{
		if (!strcmp(request_uri[lineNo],user))
		{
			//lock_get(syntel_lock);
			pSyntelDb = &syntel_database[lineNo];
			if (pSyntelDb->max_bindings)
			{
				for (bindingNo = 0; bindingNo < pSyntelDb->max_bindings; bindingNo++)
				{
					strcpy(final_uri, "sip:");  //  sip  
					strcat(final_uri, pSyntelDb->ip_addr[bindingNo]);  //ip address
					strcat(final_uri, ":");	 //colon 
					strcat(final_uri, "5060"); //  port number  
					uri.s = final_uri;
					uri.len = strlen(final_uri);
					if (bindingNo == 0)
					{ rewrite_uri(msg, &uri); }
					else
					{ append_branch(msg, &uri, 0, 0, Q_UNSPECIFIED, 0, 0); }
				}
				//lock_release(syntel_lock);
				Retval = 1;
			}
			else
			{
				//lock_release(syntel_lock);
			}
			break;	
		}
	}
	
	return Retval;
}

static int syntel_call_transfer(struct sip_msg *msg, char *foo, char *bar)
{
	int  Retval=0;
	struct hdr_field  *refer_to;
	char *refer_to_str;
	int  len=0;
	char temp_from_user[MAX_USER];
	int extNo;
	
	/*get the ID registered */
	get_from_user(msg, temp_from_user);
	if (msg->refer_to != NULL)
	{
		if ((!(msg->refer_to)->parsed) && (parse_refer_to_header(msg) < 0)) 
		{
			LM_ERR("check_refer_to(): Error while parsing Refer-To body\n");
			return -1;
		}
		refer_to = msg->refer_to;
		len = ((struct to_body*)refer_to->parsed)->uri.len;
		if (len > EXPRESSION_LENGTH) 
		{
			LM_ERR("check_refer_to(): Refer-To header field is too long: %d chars\n", len);
			return -1;
		}
		refer_to_str=strtok(((struct to_body*)refer_to->parsed)->uri.s,"@");
		refer_to_str[len] = '\0';
		while (*refer_to_str!=':')
		{
			refer_to_str++;
		}
		refer_to_str++;	
		
		/*check out whether  its registered */	
		/*get the ID registered */
		get_from_user(msg, temp_from_user);		
		for (extNo = 0; extNo < MAX_EXTENSIONS; extNo++)
		{
			if (!strcmp (request_uri[extNo],temp_from_user))
			{
				send_transfer_target(refer_to_str,extNo);			
				Retval=1;				
				break;
			}
		}
	}
        
	return Retval;
}

static int get_server_ip(struct sip_msg *msg, pv_param_t *param,pv_value_t *res)
{	
	str server_ip;
	
	server_ip.s=machine_ip;
	server_ip.len=strlen(machine_ip);
	return pv_get_strval(msg, param, res, &server_ip);	
}

static void send_transfer_target(char *target, int extNo)
{
	int  dataLen=0;
	int  target_len;
	char dataBuf[MAX_BUFFER];
	
	target_len = strlen(target);
	dataBuf[dataLen++] = FACILITY_IE;
	dataBuf[dataLen++] = target_len + 3;
	dataBuf[dataLen++] = SS_ID_CALL_TRANSFER;
	dataBuf[dataLen++] = 1;			
	dataBuf[dataLen++] = (extNo | 0x80);
	memcpy(&dataBuf[dataLen], target, target_len);
	dataLen += target_len;	
	send_msg(dataBuf, dataLen);	
}

static int syntel_cancel_ic_call (struct sip_msg *msg, char *foo, char *bar)
{
	int dataLen=0;
	char dataBuf[MAX_BUFFER], extNo;
	char temp_from_user[MAX_USER];
	
	get_from_user(msg, temp_from_user);
	for (extNo = 0; extNo < MAX_EXTENSIONS; extNo++)
	{
		if (!strcmp(request_uri[extNo], temp_from_user))
		{
			dataBuf[dataLen++] = FACILITY_IE;
			dataBuf[dataLen++] = 3;
			dataBuf[dataLen++] = SS_ID_CANCEL;
			dataBuf[dataLen++] = 1;
			dataBuf[dataLen++] = (extNo | 0x80);
			break;	
		}
	}

	send_msg(dataBuf, dataLen);
	return 1;
}


static int syntel_call_hold(struct sip_msg *msg, char *foo, char *bar)
{
	int  dataLen=0;
	char dataBuf[MAX_BUFFER], extNo;
	char temp_from_user[MAX_USER];
	syntel_users *pSyntelDb;
	
  	get_from_user (msg, temp_from_user);
	for (extNo = 0; extNo < MAX_EXTENSIONS; extNo++)
	{
		if (!strcmp (request_uri[extNo], temp_from_user))
		{
			dataBuf[dataLen++] = FACILITY_IE;
			dataBuf[dataLen++] = 3;
			dataBuf[dataLen++] = SS_ID_CALL_HOLD;
			dataBuf[dataLen++] = 1;
			dataBuf[dataLen++] = (extNo | 0x80);	
			//lock_get(syntel_lock);
			pSyntelDb = &syntel_database[extNo];
			pSyntelDb->call_hold_flag = TRUE;
			//lock_release(syntel_lock);
			break;
		}	
	}

	send_msg(dataBuf, dataLen);
	return 1;
}

static int syntel_reset_flag(struct sip_msg *msg, char *foo, char *bar)
{
	syntel_users *pSyntelDb;
	char extNo;
	char  temp_from_user[MAX_USER];
	
	get_from_user (msg, temp_from_user);
	for (extNo = 0; extNo < MAX_EXTENSIONS; extNo++)
	{
		if (!strcmp (request_uri[extNo], temp_from_user))
		{
			//lock_get(syntel_lock);
			pSyntelDb = &syntel_database[extNo];
			pSyntelDb->call_hold_flag = FALSE;
			//lock_release(syntel_lock);
			break;
		}
	}
	return 0;
}
static int syntel_call_retrieve(struct sip_msg *msg, char *foo, char *bar)
{
	int  dataLen=0;
	char dataBuf[MAX_BUFFER], extNo;
	char  temp_from_user[MAX_USER];
	syntel_users *pSyntelDb;

	get_from_user (msg, temp_from_user);
	for (extNo = 0; extNo < MAX_EXTENSIONS; extNo++)
	{
		if (!strcmp (request_uri[extNo], temp_from_user))
		{
			dataBuf[dataLen++] = FACILITY_IE;
			dataBuf[dataLen++] = 3;
			dataBuf[dataLen++] = SS_ID_CALL_RETRIEVE;
			dataBuf[dataLen++] = 1;
			dataBuf[dataLen++] = (extNo | 0x80);

			//lock_get(syntel_lock);
			pSyntelDb = &syntel_database[extNo];
 			if(pSyntelDb->call_hold_flag)
			{
				send_msg(dataBuf, dataLen);
				pSyntelDb->call_hold_flag = FALSE;
			}
			//lock_release(syntel_lock);
			break;
		}
	}

	return TRUE;
}

static void timer_1sec_routine(unsigned int ticks, void* param)
{
    syntel_delete();
}

static void syntel_delete_record(char *uri)
{
	int counter;
	int bindingNo;
	syntel_users *pSyntelDb;
	int i;
	
	//lock_get(syntel_lock);
	for (counter = 0; counter < MAX_EXTENSIONS ; counter++)
	{	
		pSyntelDb = &syntel_database[counter];
		for (bindingNo = 0; bindingNo < pSyntelDb->max_bindings; bindingNo++)
		{
			if (strstr(uri,pSyntelDb->ip_addr[bindingNo]) != NULL)
			{
				pSyntelDb->expires_value[bindingNo] = 0;
				pSyntelDb->ip_addr[bindingNo][0] = '\0';
				for(i = bindingNo; i < (pSyntelDb->max_bindings - 1) ; i++)
				{
					pSyntelDb->expires_value[i] = pSyntelDb->expires_value[i + 1];
					memcpy(pSyntelDb->ip_addr[i], pSyntelDb->ip_addr[i + 1], strlen(pSyntelDb->ip_addr[i + 1]));	
				}					
				pSyntelDb->max_bindings--;											
			}
		}
	}
	//lock_release(syntel_lock);
}

int write_to_db(char *uri, int lineNo, int expires)
{
	int Retval = 0;
	syntel_users *pSyntelDb;
	
	//lock_get(syntel_lock);
	pSyntelDb = &syntel_database[lineNo];
	if (pSyntelDb->max_bindings < MAX_USER)
	{
		strcpy(pSyntelDb->ip_addr[pSyntelDb->max_bindings] , uri);
		pSyntelDb->expires_value[pSyntelDb->max_bindings] = expires;	
		LM_ERR("\nIP  = %s\n",pSyntelDb->ip_addr[pSyntelDb->max_bindings]);	
		pSyntelDb->max_bindings++;
		Retval = 1;		
	}
	else
	{
		LM_ERR("\nmax limit reached \n\n");		
	}
	//lock_release(syntel_lock);
	return Retval;
}

static void copy_uri(char *uri, char *user, char *final_uri)
{
	char  *ptr,*ptr1;
	int  i;
	i = 0;
	ptr=&uri[0];
	while(*ptr!=':')
	{
		++ptr;
	}
	ptr++;
	ptr1 = ptr;
	while(*ptr!='@')
	{	
		user[i++]=*ptr++;	
	}
	user[i] = '\0';
	
	i = 0;
	while(*ptr1!=':')
	{	
		final_uri[i++]=*ptr1++;	
	}
	final_uri[i] = '\0';
}

static int syntel_write_record(char *uri, int expires)
{
	int Retval = 0;
	char final_uri[64];
	int extNo;
	char user[16];
	
	copy_uri(uri, user, final_uri);
	for (extNo = 0; extNo < MAX_EXTENSIONS; extNo++)
	{
		if (!strcmp(user, request_uri[extNo]))
		{ write_to_db(final_uri, extNo, expires); }
	}
	return Retval;
}

static int syntel_parse_contacts(struct sip_msg *msg, char *registered_contact)
{
	int Retval = 0;
	struct hdr_field *ptr;
	
	parse_headers (msg, HDR_CONTACT_T, 0);
	if (msg->contact)
	{
		ptr = msg->contact;
		if (ptr)
		{
			if (ptr->type == HDR_CONTACT_T)
			{
				if (!ptr->parsed && (parse_contact (ptr) < 0))
				{
					LM_ERR ("failed to parse Contact body\n");
					Retval = -6;
				}
			}
			strncpy (registered_contact, (ptr->body.s), RURI_LEN);
			registered_contact[RURI_LEN] = '\0';
		}
	}
	return Retval;
}

static int syntel_save_on_register(struct sip_msg *msg, char *foo, char *bar)
{
	int is_registered;
	int expires = 0;
	char registered_contact[RURI_LEN];
	char extNo;
	
	if (syntel_parse_contacts(msg, registered_contact))
	{
		LM_ERR ("syntel_parse_contacts  failed\n");
	}
	else
	{	
		is_registered = FALSE;	 		
		if (syntel_check_db(registered_contact, &extNo))
		{
			is_registered = TRUE;			
		}
		expires = atoi(msg->expires->body.s);
		if ( expires > 0 && !is_registered)
		{
			 syntel_write_record(registered_contact, expires);
		}
		else if(expires <= 0)			
		{
			syntel_delete_record(registered_contact);
		}
	}
	
	return 1;
}

static int syntel_save_on_invite(struct sip_msg *msg, char *foo, char *bar)
{
	int Retval = -1;
	char registered_contact[RURI_LEN];
	
	if (syntel_parse_contacts(msg, registered_contact))
	{
		LM_ERR ("syntel_parse_contacts  failed\n");
	}
	else 
	{		
		Retval = syntel_write_record(registered_contact,DEFAULT_EXPIRES);
	} 
	 
	return Retval;
}

static int  syntel_is_registered(struct sip_msg *msg, char *foo, char *bar)
{
	int Retval=-1;
	char registered_contact[RURI_LEN];
	char extNo;
	
	syntel_parse_contacts(msg,registered_contact);				
	if (syntel_check_db(registered_contact, &extNo))
	{
		Retval = TRUE;						
	}
	
	return Retval;
}

static int syntel_rewrite_uri(struct sip_msg *msg, char *foo, char *bar)
{
	int Retval = -1;
	str ruri;
	char extNo = 0;
	char final_ruri[RURI_MAX];
	char registered_contact[RURI_LEN];
	syntel_parse_contacts(msg, registered_contact);	
	if (syntel_check_db(registered_contact, &extNo))
	{
		/*copy all the  ruri details */
		strcpy(final_ruri, "sip:");
		strcat(final_ruri, request_uri[extNo]);
		strcat(final_ruri, "@");
		strcat(final_ruri, machine_ip);
		strcat(final_ruri, ":");
		strcat(final_ruri, "5070");
		ruri.s = final_ruri;
		ruri.len = strlen(final_ruri);
		LM_ERR("final URI = %s\n",final_ruri);		
		/*now call to rewrite URI */
		rewrite_uri(msg, &ruri);
		Retval = TRUE;
	}

	return Retval;
}

static int syntel_get_source_ip(struct sip_msg *msg, char *foo, char *bar)
{
	str s;
	int retVal = -1;
	if (msg!=NULL)
	{
		s.s = ip_addr2a(&msg->rcv.src_ip);
		s.len = strlen(s.s);
		if (!strcmp(s.s, machine_ip))
		{
			retVal = 1;
		}
		else
		{
			LM_ERR("empty parameter \n");
		}
	}

	return retVal;
}

static int s_get_ip(void)
{
	int fd;
	struct ifreq ifr;
	fd = socket(AF_INET, SOCK_DGRAM, 0);
	ifr.ifr_addr.sa_family = AF_INET;	/*get an IPv4 IP address */
	strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1);	/* get address attached to "eth0" */
	//strncpy(ifr.ifr_name, "esw0", IFNAMSIZ - 1);	/* get address attached to "esw0" for NFS server */
	ioctl(fd, SIOCGIFADDR, &ifr);
	close(fd);
	machine_ip = inet_ntoa(((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr);
	return 1;
}

static int initialize_lock()
{
//    syntel_lock = lock_alloc();
  //  if (syntel_lock == 0)
    //{
     //   LM_ERR("\nno shared  menory for the lock\n");
    //}
    //lock_init(syntel_lock);
    //LM_ERR("\nlock initialised\n\n");
}

static void syntel_create_msg_que(void)
{
    key_t key = KAMAILIO_MSGQ_KEY;
	
    if ((kamailio_q_tx_id = msgget(key,0666)) == -1)
    {
        perror("msgget tx msg q");
        exit(0);
    }
	
    LM_ERR("Kamailio Transmit Message Queue opened successfully.\n"); 
}

static void syntel_init_db(void)
{
	int counter;
	int bindings;
	syntel_users  *pSyntelDb = NULL;
	
	//lock_get(syntel_lock);
	for (counter = 0; counter < MAX_EXTENSIONS ; counter++)
	{	
		pSyntelDb = &syntel_database[counter];
		pSyntelDb->max_bindings = 0;
		pSyntelDb->call_hold_flag = FALSE;
		for(bindings = 0; bindings < MAX_USER; bindings++)
		{
			memset(pSyntelDb->expires_value, 0, MAX_USER);
			pSyntelDb->ip_addr[bindings][0] = '\0';
		}
	}
	//lock_release(syntel_lock);
}

static int mod_init(void)
{
	if (s_get_ip())
	{ LM_DBG ("the IP address of system %s\n", machine_ip); }

	syntel_database = (syntel_users *)shm_malloc(MAX_EXTENSIONS * sizeof(syntel_users));
	if (syntel_database == NULL)
	{ LM_ERR("\n error  in allocating memory  reason= %s\n",strerror(errno)); }
	LM_ERR("shared memory attached\n");
// 	initialize_lock();
	register_timer(timer_1sec_routine, 0, timer_interval);
	syntel_create_msg_que();
	syntel_init_db();
	return 0;
}

static void mod_destroy (void)
{
    shm_free(syntel_database);
    //lock_destroy(syntel_lock);
    //lock_dealloc(syntel_lock);
}

static void send_msg(char *msg, int len)
{
    msg_buf buf;
    int dataLen;

    memset(&buf,0,sizeof(buf));
	buf.mtype=KAMAILIO_TO_L1_COMMANDS;
    memcpy(buf.mtext,msg,len);
    buf.mtext[len]='\0';

    /* Check whether data sent or not */
    dataLen = msgsnd(kamailio_q_tx_id, &buf, strlen(buf.mtext), IPC_NOWAIT);
    if (dataLen <= -1)
    {  
		LM_ERR("\n msg send error %s\n\n",strerror(errno));
    }
}
#include "../../sr_module.h"
#include "../../mem/mem.h"
#include "../../mem/shm_mem.h"
#include "../../lib/kmi/mi.h"
#include "../../ut.h"
#include "../../dset.h"
#include "../../str.h"
#include "../../xavp.h"
#include "../../config.h"
#include "../../action.h"
#include "../../mod_fix.h"
#include "../../timer.h"
#include "../../locking.h"
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <poll.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <net/if.h>
#include "../../parser/msg_parser.h"
#include "../../trim.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_expires.h"
#include "../../parser/contact/parse_contact.h"
#include "../../parser/parse_uri.h"
#include "../../parser/parse_content.h"
#include "../../parser/parser_f.h"
#include "../../parser/sdp/sdp_helpr_funcs.h"
#include "../../modules/sanity/sanity.h"
#include "../../parser/parse_refer_to.h"
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/time.h>
#include <signal.h>
#include <pthread.h>


MODULE_VERSION

#define MAX_AOR_LEN      256
#define INITIAL_ALLOC    512
#define NETBUFSIZE       200
#define RURI_MAX         64
#define MAX_EXTENSIONS   16
#define RURI_LEN         26
#define KAMAILIO_TO_L1_COMMANDS     3
#define KAMAILIO_MSGQ_KEY           2000
#define TIMER_TICKS         "TIMER_TICK"

char request_uri[MAX_EXTENSIONS][4] = {"111", "112", "113", "114", "115", "116", "117", "118", 
	 				   "119", "120", "121",	"122", "123", "124", "125", "126"};
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
sr-users@lists.sip-router.org
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users

Reply via email to