Exemple #1
0
event_t *shm_copy_event(event_t *e)
{
	event_t *ev = NULL;
	param_t *p1, *p2;
	int size;

	ev = (event_t *)shm_malloc(sizeof(event_t));
	if(ev == NULL) {
		ERR_MEM(SHARE_MEM);
	}
	memset(ev, 0, sizeof(event_t));

	ev->name.s = (char *)shm_malloc(e->name.len * sizeof(char));
	if(ev->name.s == NULL) {
		ERR_MEM(SHARE_MEM);
	}
	memcpy(ev->name.s, e->name.s, e->name.len);
	ev->name.len = e->name.len;

	p1 = e->params.list;
	while(p1) {
		size = sizeof(param_t) + (p1->name.len + p1->body.len) * sizeof(char);
		p2 = (param_t *)shm_malloc(size);
		if(p2 == NULL) {
			ERR_MEM(SHARE_MEM);
		}
		memset(p2, 0, size);

		size = sizeof(param_t);
		CONT_COPY(p2, p2->name, p1->name);
		if(p1->body.s && p1->body.len)
			CONT_COPY(p2, p2->body, p1->body);
		p2->next = ev->params.list;
		ev->params.list = p2;

		/* Update parameter hooks in the shmmem copy, this is needed so that
		 * we can test for the presence of the sla parameter even in the
		 * shared copy of the event */
		if(e->params.hooks.event_dialog.call_id == p1)
			ev->params.hooks.event_dialog.call_id = p2;
		if(e->params.hooks.event_dialog.from_tag == p1)
			ev->params.hooks.event_dialog.from_tag = p2;
		if(e->params.hooks.event_dialog.to_tag == p1)
			ev->params.hooks.event_dialog.to_tag = p2;
		if(e->params.hooks.event_dialog.include_session_description == p1)
			ev->params.hooks.event_dialog.include_session_description = p2;
		if(e->params.hooks.event_dialog.sla == p1)
			ev->params.hooks.event_dialog.sla = p2;

		p1 = p1->next;
	}
	ev->type = e->type;

	return ev;

error:
	shm_free_event(ev);
	return NULL;
}
Exemple #2
0
subs_t* mem_copy_subs_noc(subs_t* s)
{
	int size;
	subs_t* dest;

	size= sizeof(subs_t)+ s->pres_uri.len+ s->to_user.len
		+ s->to_domain.len+ s->from_user.len+ s->from_domain.len+ s->callid.len
		+ s->to_tag.len+ s->from_tag.len+s->event_id.len
		+ s->local_contact.len + s->record_route.len+
		+ s->reason.len+ 1;

	dest= (subs_t*)shm_malloc(size);
	if(dest== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memset(dest, 0, size);
	size= sizeof(subs_t);

	CONT_COPY(dest, dest->pres_uri, s->pres_uri);
	CONT_COPY(dest, dest->to_user, s->to_user);
	CONT_COPY(dest, dest->to_domain, s->to_domain);
	CONT_COPY(dest, dest->from_user, s->from_user);
	CONT_COPY(dest, dest->from_domain, s->from_domain);
	CONT_COPY(dest, dest->to_tag, s->to_tag);
	CONT_COPY(dest, dest->from_tag, s->from_tag);
	CONT_COPY(dest, dest->callid, s->callid);
	CONT_COPY(dest, dest->local_contact, s->local_contact);
	CONT_COPY(dest, dest->record_route, s->record_route);
	if(s->event_id.s)
		CONT_COPY(dest, dest->event_id, s->event_id);
	if(s->reason.s)
		CONT_COPY(dest, dest->reason, s->reason);

	dest->event= s->event;
	dest->local_cseq= s->local_cseq;
	dest->remote_cseq= s->remote_cseq;
	dest->status= s->status;
	dest->version= s->version;
	dest->expires= s->expires;
	dest->db_flag= s->db_flag;
	dest->sockinfo = s->sockinfo;

	dest->contact.s= (char*)shm_malloc(s->contact.len);
	if(dest->contact.s== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memcpy(dest->contact.s, s->contact.s, s->contact.len);
	dest->contact.len= s->contact.len;

	return dest;

error:
	if(dest)
			shm_free(dest);
	return NULL;
}
Exemple #3
0
int insert_phtable(str* pres_uri, int event, char* sphere)
{
	unsigned int hash_code;
	pres_entry_t* p= NULL;
	int size;

	hash_code= core_case_hash(pres_uri, NULL, phtable_size);

	lock_get(&pres_htable[hash_code].lock);
	
	p= search_phtable(pres_uri, event, hash_code);
	if(p)
	{
		p->publ_count++;
		lock_release(&pres_htable[hash_code].lock);
		return 0;
	}
	size= sizeof(pres_entry_t)+ pres_uri->len* sizeof(char);

	p= (pres_entry_t*)shm_malloc(size);
	if(p== NULL)
	{
		lock_release(&pres_htable[hash_code].lock);
		ERR_MEM(SHARE_MEM);
	}
	memset(p, 0, size);

	size= sizeof(pres_entry_t);
	p->pres_uri.s= (char*)p+ size;
	memcpy(p->pres_uri.s, pres_uri->s, pres_uri->len);
	p->pres_uri.len= pres_uri->len;
	
	if(sphere)
	{
		p->sphere= (char*)shm_malloc((strlen(sphere)+ 1)*sizeof(char));
		if(p->sphere== NULL)
		{
			lock_release(&pres_htable[hash_code].lock);
			shm_free(p);
			ERR_MEM(SHARE_MEM);
		}
		strcpy(p->sphere, sphere);
	}

	p->event= event;
	p->publ_count=1;

	/* link the item in the hash table */
	p->next= pres_htable[hash_code].entries->next;
	pres_htable[hash_code].entries->next= p;

	lock_release(&pres_htable[hash_code].lock);
	
	return 0;

error:
	return -1;
}
Exemple #4
0
event_t* shm_copy_event(event_t* e)
{
	event_t* ev= NULL;
	param_t* p1, *p2;
	int size;

	ev= (event_t*)shm_malloc(sizeof(event_t));
	if(ev== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memset(ev, 0, sizeof(event_t));

	ev->text.s= (char*)shm_malloc(e->text.len);
	if(ev->text.s== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memcpy(ev->text.s, e->text.s, e->text.len);
	ev->text.len= e->text.len;

	p1= e->params;
	while(p1)
	{
		size= sizeof(param_t)+ p1->name.len+ p1->body.len;
		p2= (param_t*)shm_malloc(size);
		if(p2== NULL)
		{
			ERR_MEM(SHARE_MEM);
		}
		memset(p2, 0, size);

		size= sizeof(param_t);
		CONT_COPY(p2, p2->name, p1->name);
		if(p1->body.s && p1->body.len)
			CONT_COPY(p2, p2->body, p1->body);
		p2->next= ev->params;
		ev->params= p2;
		p1= p1->next;
	}
	ev->parsed= e->parsed;

	return ev;

error:
	shm_free_event(ev);
	return NULL;
}
Exemple #5
0
pres_entry_t* insert_phtable(str* pres_uri, int event, str* etag, char* sphere, int init_turn)
{
	unsigned int hash_code;
	pres_entry_t* p= NULL;
	int size;

	size= sizeof(pres_entry_t)+ pres_uri->len;
	p= (pres_entry_t*)shm_malloc(size);
	if(p== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memset(p, 0, size);

	size= sizeof(pres_entry_t);
	p->pres_uri.s= (char*)p+ size;
	memcpy(p->pres_uri.s, pres_uri->s, pres_uri->len);
	p->pres_uri.len= pres_uri->len;

	if(sphere)
	{
		p->sphere= (char*)shm_malloc(strlen(sphere)+ 1);
		if(p->sphere== NULL)
		{
			ERR_MEM(SHARE_MEM);
		}
		strcpy(p->sphere, sphere);
	}
	p->event= event;
	update_pres_etag(p, etag);

	hash_code= core_hash(pres_uri, NULL, phtable_size);
	lock_get(&pres_htable[hash_code].lock);

	p->next= pres_htable[hash_code].entries->next;
	pres_htable[hash_code].entries->next= p;

	p->last_turn = init_turn;

	lock_release(&pres_htable[hash_code].lock);

	return p;

error:
	if(p)
		shm_free(p);
	return NULL;
}
Exemple #6
0
subs_t* mem_copy_subs(subs_t* s, int mem_type)
{
	int size;
	subs_t* dest;

	size= sizeof(subs_t)+ s->pres_uri.len+ s->to_user.len
		+ s->to_domain.len+ s->from_user.len+ s->from_domain.len+ s->callid.len
		+ s->to_tag.len+ s->from_tag.len+s->event_id.len
		+ s->local_contact.len+ s->contact.len+ s->record_route.len+
		+ s->reason.len+ 1;

	if(mem_type == PKG_MEM_TYPE)
		dest= (subs_t*)pkg_malloc(size);
	else
		dest= (subs_t*)shm_malloc(size);

	if(dest== NULL)
	{
		ERR_MEM((mem_type==PKG_MEM_TYPE)?PKG_MEM_STR:SHARE_MEM);
	}
	memset(dest, 0, size);
	size= sizeof(subs_t);

	CONT_COPY(dest, dest->pres_uri, s->pres_uri);
	CONT_COPY(dest, dest->to_user, s->to_user);
	CONT_COPY(dest, dest->to_domain, s->to_domain);
	CONT_COPY(dest, dest->from_user, s->from_user);
	CONT_COPY(dest, dest->from_domain, s->from_domain);
	CONT_COPY(dest, dest->to_tag, s->to_tag);
	CONT_COPY(dest, dest->from_tag, s->from_tag);
	CONT_COPY(dest, dest->callid, s->callid);
	CONT_COPY(dest, dest->local_contact, s->local_contact);
	CONT_COPY(dest, dest->contact, s->contact);
	CONT_COPY(dest, dest->record_route, s->record_route);
	if(s->event_id.s)
		CONT_COPY(dest, dest->event_id, s->event_id);
	if(s->reason.s)
		CONT_COPY(dest, dest->reason, s->reason);

	dest->event= s->event;
	dest->local_cseq= s->local_cseq;
	dest->remote_cseq= s->remote_cseq;
	dest->status= s->status;
	dest->version= s->version;
	dest->expires= s->expires;
	dest->db_flag= s->db_flag;
	dest->sockinfo= s->sockinfo;

	return dest;

error:
	if(dest)
	{
		if(mem_type == PKG_MEM_TYPE)
			pkg_free(dest);
		else
			shm_free(dest);
	}
	return NULL;
}
Exemple #7
0
phtable_t* new_phtable(void)
{
	phtable_t* htable= NULL;
	int i, j;

	i = 0;
	htable= (phtable_t*)shm_malloc(phtable_size* sizeof(phtable_t));
	if(htable== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memset(htable, 0, phtable_size* sizeof(phtable_t));

	for(i= 0; i< phtable_size; i++)
	{
		if(lock_init(&htable[i].lock)== 0)
		{
			LM_ERR("initializing lock [%d]\n", i);
			goto error;
		}
		htable[i].entries= (pres_entry_t*)shm_malloc(sizeof(pres_entry_t));
		if(htable[i].entries== NULL)
		{
			ERR_MEM(SHARE_MEM);
		}
		memset(htable[i].entries, 0, sizeof(pres_entry_t));
		htable[i].entries->next= NULL;
	}

	return htable;

error:
	if(htable)
	{
		for(j=0; j< i; j++)
		{
			if(htable[i].entries)
				shm_free(htable[i].entries);
			else 
				break;
			lock_destroy(&htable[i].lock);
		}
		shm_free(htable);
	}
	return NULL;

}
Exemple #8
0
shtable_t new_shtable(int hash_size)
{
	shtable_t htable= NULL;
	int i, j;

	i = 0;
	htable= (subs_entry_t*)shm_malloc(hash_size* sizeof(subs_entry_t));
	if(htable== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memset(htable, 0, hash_size* sizeof(subs_entry_t));
	for(i= 0; i< hash_size; i++)
	{
		if(lock_init(&htable[i].lock)== 0)
		{
			LM_ERR("initializing lock [%d]\n", i);
			goto error;
		}
		htable[i].entries= (subs_t*)shm_malloc(sizeof(subs_t));
		if(htable[i].entries== NULL)
		{
			lock_destroy(&htable[i].lock);
			ERR_MEM(SHARE_MEM);
		}
		memset(htable[i].entries, 0, sizeof(subs_t));
		htable[i].entries->next= NULL;
	}

	return htable;

error:
	if(htable)
	{
		for(j=0; j< i; j++)
		{
			lock_destroy(&htable[j].lock);
			shm_free(htable[j].entries);
		}
		shm_free(htable);
	}
	return NULL;

}
Exemple #9
0
dlg_t* b2b_client_build_dlg(b2b_dlg_t* dlg, dlg_leg_t* leg)
{
	dlg_t* td =NULL;

	td = (dlg_t*)pkg_malloc(sizeof(dlg_t));
	if(td == NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memset(td, 0, sizeof(dlg_t));

	td->loc_seq.value   = dlg->cseq[CALLER_LEG];
	dlg->cseq[CALLER_LEG]++;
	td->loc_seq.is_set  = 1;

	td->id.call_id = dlg->callid;
	td->id.loc_tag = dlg->tag[CALLER_LEG];

	td->loc_uri = dlg->from_uri;
	td->rem_uri = dlg->to_uri;
	td->loc_dname = dlg->from_dname;
	td->rem_dname = dlg->to_dname;

	if(leg)
	{
		if(leg->route_set.s && leg->route_set.len)
		{
			if(parse_rr_body(leg->route_set.s, leg->route_set.len,
				&td->route_set)< 0)
			{
				LM_ERR("failed to parse record route body\n");
				goto error;
			}
		}

		td->id.rem_tag = leg->tag;

		LM_DBG("Rem_target = %.*s\n", leg->contact.len, leg->contact.s);
		td->rem_target = leg->contact;
	}
	td->state= DLG_CONFIRMED ;
	td->send_sock = dlg->send_sock;
	if(dlg->send_sock)
		LM_DBG("send sock= %.*s\n", dlg->send_sock->address_str.len,
			dlg->send_sock->address_str.s);

	return td;
error:
	if(td)
		pkg_free(td);

	return 0;
}
Exemple #10
0
xcap_node_sel_t* xcapInitNodeSel(void)
{
	xcap_node_sel_t* nsel= NULL;

	nsel= (xcap_node_sel_t*)pkg_malloc(sizeof(xcap_node_sel_t));
	if(nsel== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memset(nsel, 0, sizeof(xcap_node_sel_t));
	nsel->steps= (step_t*)pkg_malloc(sizeof(step_t));
	if(nsel->steps== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memset(nsel->steps, 0, sizeof(step_t));
	nsel->last_step= nsel->steps;

	nsel->ns_list= (ns_list_t*)pkg_malloc(sizeof(ns_list_t));
	if(nsel->ns_list== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memset(nsel->ns_list, 0, sizeof(ns_list_t));
	nsel->last_ns= nsel->ns_list;

	return nsel;

error:
	if(nsel)
	{
		if(nsel->steps)
			pkg_free(nsel->steps);
		if(nsel->ns_list)
			pkg_free(nsel->ns_list);
		pkg_free(nsel);
	}

	return NULL;
}
Exemple #11
0
char* extract_sphere(str body)
{

	/* check for a rpid sphere element */
	xmlDocPtr doc= NULL;
	xmlNodePtr node;
	char* cont, *sphere= NULL;


	doc= xmlParseMemory(body.s, body.len);
	if(doc== NULL)
	{
		LM_ERR("failed to parse xml body\n");
		return NULL;
	}

	node= xmlNodeGetNodeByName(doc->children, "sphere", "rpid");

	if(node== NULL)
		node= xmlNodeGetNodeByName(doc->children, "sphere", "r");

	if(node)
	{
		LM_DBG("found sphere definition\n");
		cont= (char*)xmlNodeGetContent(node);
		if(cont== NULL)
		{
			LM_ERR("failed to extract sphere node content\n");
			goto error;
		}
		sphere= (char*)pkg_malloc(strlen(cont)+ 1);
		if(sphere== NULL)
		{
			xmlFree(cont);
			ERR_MEM(PKG_MEM_STR);
		}
		strcpy(sphere, cont);
		xmlFree(cont);
	}
	else
		LM_DBG("didn't find sphere definition\n");

error:
	xmlFreeDoc(doc);
	return sphere;
}
Exemple #12
0
static int shm_copy_xcap_list(void)
{
	xcap_serv_t* xs, *shm_xs, *prev_xs;
	int size;

	xs= xs_list;
	if(xs== NULL)
	{
		if(force_active== 0 && !integrated_xcap_server)
		{
			LM_ERR("no xcap_server parameter set\n");
			return -1;
		}
		return 0;
	}
	xs_list= NULL;
	size= sizeof(xcap_serv_t);
	
	while(xs)
	{
		size+= (strlen(xs->addr)+ 1)* sizeof(char);
		shm_xs= (xcap_serv_t*)shm_malloc(size);
		if(shm_xs== NULL)
		{
			ERR_MEM(SHARE_MEM);
		}
		memset(shm_xs, 0, size);
		size= sizeof(xcap_serv_t);

		shm_xs->addr= (char*)shm_xs+ size;
		strcpy(shm_xs->addr, xs->addr);
		shm_xs->port= xs->port;
		shm_xs->next= xs_list; 
		xs_list= shm_xs;

		prev_xs= xs;
		xs= xs->next;

		pkg_free(prev_xs);
	}
	return 0;

error:
	free_xs_list(xs_list, SHM_MEM_TYPE);
	return -1;
}
Exemple #13
0
dlg_t* b2b_server_build_dlg(b2b_dlg_t* dlg)
{
	dlg_t* td =NULL;

	td = (dlg_t*)pkg_malloc(sizeof(dlg_t));
	if(td == NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memset(td, 0, sizeof(dlg_t));

	td->loc_seq.value = dlg->cseq[CALLEE_LEG];
	td->loc_seq.is_set = 1;
	dlg->cseq[CALLEE_LEG]++;

	td->id.call_id = dlg->callid;
	td->id.rem_tag = dlg->tag[CALLER_LEG];
	td->id.loc_tag = dlg->tag[CALLEE_LEG];

	td->rem_target = dlg->contact[CALLER_LEG];

	td->loc_uri = dlg->to_uri;
	td->rem_uri = dlg->from_uri;
	td->loc_dname = dlg->to_dname;
	td->rem_dname = dlg->from_dname;

	if(dlg->route_set[CALLER_LEG].s && dlg->route_set[CALLER_LEG].len)
	{
		if(parse_rr_body(dlg->route_set[CALLER_LEG].s, dlg->route_set[CALLER_LEG].len,
			&td->route_set)< 0)
		{
			LM_ERR("failed to parse record route body\n");
			goto error;
		}
	}
	td->state= DLG_CONFIRMED ;
	td->send_sock = dlg->send_sock;

	return td;
error:
	if(td)
		pkg_free(td);

	return NULL;
}
int init_b2bl_htable(void)
{
	int i;
	b2bl_htable = (b2bl_table_t)shm_malloc(b2bl_hsize* sizeof(b2bl_entry_t));
	if(!b2bl_htable)
		ERR_MEM(SHARE_MEM);
	
	memset(b2bl_htable, 0, b2bl_hsize* sizeof(b2bl_entry_t));
	for(i= 0; i< b2bl_hsize; i++)
	{
		lock_init(&b2bl_htable[i].lock);
		b2bl_htable[i].first = NULL;
	}

	return 0;
error:
	return -1;
}
Exemple #15
0
int register_xcapcb( int types, xcap_cb f)
{
	xcap_callback_t* xcb;

	xcb= (xcap_callback_t*)shm_malloc(sizeof(xcap_callback_t));
	if(xcb== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memset(xcb, 0, sizeof(xcap_callback_t));

	xcb->callback= f;
	xcb->types= types;
	xcb->next= xcapcb_list;
	xcapcb_list= xcb;
	return 0;

error:
	return -1;
}
Exemple #16
0
/*
 * function that updates a subscription in hash table
 *	sets reply_code and reply_str in case of error and
 *	if different that server error
 * */
int update_rlsubs( subs_t* subs, unsigned int hash_code,
		int* reply_code, str* reply_str)
{
	subs_t* s, *ps;

	/* search the record in hash table */
	lock_get(&rls_table[hash_code].lock);

	s= pres_search_shtable(rls_table, subs->callid,
			subs->to_tag, subs->from_tag, hash_code);
	if(s== NULL)
	{
		LM_DBG("record not found in hash table\n");
		lock_release(&rls_table[hash_code].lock);
		return -1;
	}

	s->expires= subs->expires+ (int)time(NULL);
	
	if(s->db_flag == NO_UPDATEDB_FLAG)
		s->db_flag= UPDATEDB_FLAG;
	
	if(	s->remote_cseq>= subs->remote_cseq)
	{
		lock_release(&rls_table[hash_code].lock);
		LM_DBG("stale cseq stored cseq= %d - received cseq= %d\n", s->remote_cseq, subs->remote_cseq);
		*reply_code =  Stale_cseq_code;
		*reply_str = stale_cseq_rpl;
		return -1;
	}
	s->remote_cseq= subs->remote_cseq;

	subs->pres_uri.s= (char*)pkg_malloc(s->pres_uri.len* sizeof(char));
	if(subs->pres_uri.s== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memcpy(subs->pres_uri.s, s->pres_uri.s, s->pres_uri.len);
	subs->pres_uri.len= s->pres_uri.len;

	subs->local_cseq= s->local_cseq;
	subs->version= s->version;

	if(s->record_route.s && s->record_route.len)
	{
		subs->record_route.s= (char*)pkg_malloc(s->record_route.len);
		if(subs->record_route.s== NULL)
		{
			ERR_MEM(PKG_MEM_STR);
		}
		memcpy(subs->record_route.s, s->record_route.s, s->record_route.len);
		subs->record_route.len= s->record_route.len;
	}

	if(subs->expires== 0)
	{
		/* delete record from hash table */
		ps= rls_table[hash_code].entries;
		int found= 0;
		while(ps->next)
		{
			if(ps->next== s)
			{
				found= 1;
				break;
			}
			ps= ps->next;
		}
		if(found== 0)
		{
			LM_ERR("record not found\n");
			goto error;
		}
		ps->next= s->next;
		shm_free(s);
	
	/* delete from rls_presentity table also */
	}
	
	lock_release(&rls_table[hash_code].lock);

	return 0;

error:
	lock_release(&rls_table[hash_code].lock);
	return -1;
}
Exemple #17
0
static int pxml_add_xcap_server( modparam_t type, void* val)
{
	xcap_serv_t* xs;
	int size;
	char* serv_addr= (char*)val;
	char* sep= NULL;
	unsigned int port= 80;
	str serv_addr_str;
		
	serv_addr_str.s= serv_addr;
	serv_addr_str.len= strlen(serv_addr);

	sep= strchr(serv_addr, ':');
	if(sep)
	{	
		char* sep2= NULL;
		str port_str;
		
		sep2= strchr(sep+ 1, ':');
		if(sep2)
			sep= sep2;
		

		port_str.s= sep+ 1;
		port_str.len= serv_addr_str.len- (port_str.s- serv_addr);

		if(str2int(&port_str, &port)< 0)
		{
			LM_ERR("while converting string to int\n");
			goto error;
		}
		if(port< 0 || port> 65535)
		{
			LM_ERR("wrong port number\n");
			goto error;
		}
		*sep = '\0';
		serv_addr_str.len= sep- serv_addr;
	}

	size= sizeof(xcap_serv_t)+ (serv_addr_str.len+ 1)* sizeof(char);
	xs= (xcap_serv_t*)pkg_malloc(size);
	if(xs== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memset(xs, 0, size);
	size= sizeof(xcap_serv_t);

	xs->addr= (char*)xs+ size;
	strcpy(xs->addr, serv_addr);

	xs->port= port;
	/* check for duplicates */
	xs->next= xs_list;
	xs_list= xs;
	return 0;

error:
	free_xs_list(xs_list, PKG_MEM_TYPE);
	return -1;
}
Exemple #18
0
int parse_subs_state(str auth_state, str *reason, int *expires)
{
	str str_exp;
	char* smc= NULL;
	int len, flag= -1;

	if (strncmp(auth_state.s, "active", 6)== 0)
		flag= ACTIVE_STATE;

	if (strncmp(auth_state.s, "pending", 7)== 0)
		flag= PENDING_STATE; 

	if (strncmp(auth_state.s, "terminated", 10)== 0)
	{
		smc= strchr(auth_state.s, ';');
		if (smc== NULL)
		{
			LM_ERR("terminated state and no reason found");
			return -1;
		}
		if (strncmp(smc+1, "reason=", 7))
		{
			LM_ERR("terminated state and no reason found");
			return -1;
        	}
		len=  auth_state.len- 10- 1- 7;
		reason->s = (char*) pkg_malloc(len* sizeof(char));
		if (reason->s== NULL)
		{
			ERR_MEM(PKG_MEM_STR);
		}
		memcpy(reason->s, smc+ 8, len);
		reason->len= len;
		return TERMINATED_STATE;
	}
	
	if(flag> 0)
	{
		smc= strchr(auth_state.s, ';');
		if(smc== NULL)
		{
			LM_ERR("active or pending state and no expires parameter found");
			return -1;
		}	
		if(strncmp(smc+1, "expires=", 8))
		{
			LM_ERR("active or pending state and no expires parameter found");
			return -1;
		}
		str_exp.s= smc+ 9;
		str_exp.len= auth_state.s+ auth_state.len- smc- 9;

		if( str2int(&str_exp, (unsigned int*)expires)< 0)
		{
			LM_ERR("while getting int from str\n");
			return -1;
		}
		return flag;
	
	}

error:
	if (reason->s) pkg_free(reason->s);
	return -1;
}
Exemple #19
0
void subs_cback_func(struct cell *t, int cb_type, struct tmcb_params *ps)
{
	struct sip_msg* msg= NULL;
	int lexpire= 0;
	unsigned int cseq;
	ua_pres_t* presentity= NULL, *hentity= NULL;
	struct to_body *pto= NULL, *pfrom = NULL;
	int size= 0;
	int flag ;
	str record_route= {0, 0};
	int rt;
	str contact;
	int initial_request = 0;

	if(ps==NULL || ps->param== NULL || *ps->param== NULL )
	{
		LM_ERR("null callback parameter\n");
		return;
	}
	LM_DBG("completed with status %d\n",ps->code) ;
	hentity= (ua_pres_t*)(*ps->param);
	flag= hentity->flag;
	if(hentity->flag & XMPP_INITIAL_SUBS)
		hentity->flag= XMPP_SUBSCRIBE;

	/* get dialog information from reply message: callid, to_tag, from_tag */
	msg= ps->rpl;
	if(msg == NULL)
	{
		LM_ERR("no reply message found\n ");
		goto error;
	}

	if(msg== FAKED_REPLY)
	{
		/* delete record from hash_table and call registered functions */

		if(hentity->call_id.s== NULL) /* if a new requets failed-> do nothing*/
		{
			LM_DBG("initial Subscribe request failed\n");
			goto done;
		}
		lock_get(&HashT->p_records[hentity->hash_index].lock);
		presentity = get_htable_safe(hentity->hash_index, hentity->local_index);
		if(presentity)
		{
			delete_htable_safe(presentity, hentity->hash_index);
			lock_release(&HashT->p_records[hentity->hash_index].lock);
		}
		lock_release(&HashT->p_records[hentity->hash_index].lock);
		goto done;
	}

	if ( parse_headers(msg,HDR_EOH_F, 0)==-1 )
	{
		LM_ERR("when parsing headers\n");
		goto done;
	}

	/*if initial request */

	if(hentity->call_id.s== NULL)
	{
		initial_request = 1;
		if(ps->code>= 300)
		{
			LM_DBG("initial Subscribe request failed\n");
			goto done;
		}

		if( msg->callid==NULL || msg->callid->body.s==NULL)
		{
			LM_ERR("cannot parse callid header\n");
			goto done;
		}

		if (!msg->from || !msg->from->body.s)
		{
			LM_ERR("cannot find 'from' header!\n");
			goto done;
		}
		if (msg->from->parsed == NULL)
		{
			if ( parse_from_header( msg )<0 ) 
			{
				LM_ERR("cannot parse From header\n");
				goto done;
			}
		}
		pfrom = (struct to_body*)msg->from->parsed;

		if( pfrom->tag_value.s ==NULL || pfrom->tag_value.len == 0)
		{
			LM_ERR("no from tag value present\n");
			goto done;
		}
		if( msg->to==NULL || msg->to->body.s==NULL)
		{
			LM_ERR("cannot parse TO header\n");
			goto done;
		}

		pto = get_to(msg);
		if (pto == NULL || pto->error != PARSE_OK) {
			LM_ERR("failed to parse TO header\n");
			goto done;
		}

		if( pto->tag_value.s ==NULL || pto->tag_value.len == 0)
		{
			LM_ERR("no to tag value present\n");
			goto done;
		}
		hentity->call_id=  msg->callid->body;
		hentity->to_tag= pto->tag_value;
		hentity->from_tag= pfrom->tag_value;

	}

	/* extract the other necesary information for inserting a new record */
	if(ps->rpl->expires && msg->expires->body.len > 0)
	{
		if (!msg->expires->parsed && (parse_expires(msg->expires) < 0))
		{
			LM_ERR("cannot parse Expires header\n");
			goto done;
		}
		lexpire = ((exp_body_t*)msg->expires->parsed)->val;
		LM_DBG("lexpire= %d\n", lexpire);
	}

	if(ps->code >= 300 )
	{	/* if an error code and a stored dialog delete it and try to send 
		   a subscription with type= INSERT_TYPE, else return*/

		if(!initial_request)
		{
			subs_info_t subs;

			lock_get(&HashT->p_records[hentity->hash_index].lock);
			presentity = get_htable_safe(hentity->hash_index, hentity->local_index);
			if(presentity)
			{
				hentity->event= presentity->event;
				delete_htable_safe(presentity, hentity->hash_index);
				lock_release(&HashT->p_records[hentity->hash_index].lock);
			}
			lock_release(&HashT->p_records[hentity->hash_index].lock);

			memset(&subs, 0, sizeof(subs_info_t));
			subs.pres_uri= hentity->pres_uri;
			subs.to_uri  = hentity->to_uri;
			subs.watcher_uri= hentity->watcher_uri;
			subs.contact= &hentity->contact;

			if(hentity->remote_contact.s)
				subs.remote_target= &hentity->remote_contact;

			if(hentity->desired_expires== 0)
				subs.expires= -1;
			else
			if(hentity->desired_expires< (int)time(NULL))
				subs.expires= 0;
			else
				subs.expires= hentity->desired_expires- (int)time(NULL)+ 3;

			subs.flag= INSERT_TYPE;
			subs.source_flag= flag;
			subs.event= hentity->event;
			subs.id= hentity->id;
			subs.outbound_proxy= hentity->outbound_proxy;
			subs.extra_headers= &hentity->extra_headers;
			subs.cb_param= hentity->cb_param;

			if(send_subscribe(&subs)< 0)
			{
				LM_ERR("when trying to send SUBSCRIBE\n");
				goto done;
			}
		}
		goto done;
	}
	/*if a 2XX reply handle the two cases- an existing dialog and a new one*/

	/* extract the contact */
	if(msg->contact== NULL || msg->contact->body.s== NULL)
	{
		LM_ERR("no contact header found");
		goto error;
	}
	if( parse_contact(msg->contact) <0 )
	{
		LM_ERR(" cannot parse contact header\n");
		goto error;
	}

	if(msg->contact->parsed == NULL)
	{
		LM_ERR("cannot parse contact header\n");
		goto error;
	}
	contact = ((contact_body_t* )msg->contact->parsed)->contacts->uri;

	if(!initial_request)
	{
		/* do not delete the dialog - allow Notifies to be recognized as
		 * inside a known dialog */
	        if (lexpire == 0)
	                lexpire = 5;

		LM_DBG("*** Update expires\n");
		update_htable(hentity->hash_index, hentity->local_index, lexpire, NULL, &contact);
		goto done;
	}

	/* if a new dialog -> insert */
	if(lexpire== 0)
	{
		LM_DBG("expires= 0: no not insert\n");
		goto done;
	}

	if( msg->cseq==NULL || msg->cseq->body.s==NULL)
	{
		LM_ERR("cannot parse cseq header\n");
		goto done;
	}

	if( str2int( &(get_cseq(msg)->number), &cseq)< 0)
	{
		LM_ERR("while converting str to int\n");
		goto done;
    }

	/*process record route and add it to a string*/
	if (msg->record_route!=NULL)
	{
		rt = print_rr_body(msg->record_route, &record_route, 1, 0);
		if(rt != 0)
		{
			LM_ERR("parsing record route [%d]\n", rt);
			record_route.s=NULL;
			record_route.len=0;
		}
	}

	size= sizeof(ua_pres_t)+ 2*sizeof(str)+(hentity->pres_uri->len+ pto->uri.len+
		pfrom->uri.len+ pto->tag_value.len+ pfrom->tag_value.len
		+msg->callid->body.len+ record_route.len+ hentity->contact.len+
		hentity->id.len )*sizeof(char);

	presentity= (ua_pres_t*)shm_malloc(size);
	if(presentity== NULL)
	{
		LM_ERR("no more share memory\n");
		if(record_route.s)
			pkg_free(record_route.s);
		goto done;
	}
	memset(presentity, 0, size);
	size= sizeof(ua_pres_t);

	presentity->pres_uri= (str*)( (char*)presentity+ size);
	size+= sizeof(str);
	presentity->pres_uri->s= (char*)presentity+ size;
	memcpy(presentity->pres_uri->s, hentity->pres_uri->s, hentity->pres_uri->len);
	presentity->pres_uri->len= hentity->pres_uri->len;
	size+= hentity->pres_uri->len;

	presentity->to_uri.s= (char*)presentity+ size;
	memcpy(presentity->to_uri.s, pto->uri.s, pto->uri.len);
	presentity->to_uri.len= pto->uri.len;
	size+= pto->uri.len;

	presentity->watcher_uri= (str*)( (char*)presentity+ size);
	size+= sizeof(str);
	presentity->watcher_uri->s= (char*)presentity+ size;
	memcpy(presentity->watcher_uri->s, pfrom->uri.s, pfrom->uri.len);
	presentity->watcher_uri->len= pfrom->uri.len;
	size+= pfrom->uri.len;

	presentity->call_id.s= (char*)presentity + size;
	memcpy(presentity->call_id.s,msg->callid->body.s, 
		msg->callid->body.len);
	presentity->call_id.len= msg->callid->body.len;
	size+= presentity->call_id.len;

	presentity->to_tag.s= (char*)presentity + size;
	memcpy(presentity->to_tag.s,pto->tag_value.s, 
			pto->tag_value.len);
	presentity->to_tag.len= pto->tag_value.len;
	size+= pto->tag_value.len;

	presentity->from_tag.s= (char*)presentity + size;
	memcpy(presentity->from_tag.s,pfrom->tag_value.s, 
			pfrom->tag_value.len);
	presentity->from_tag.len= pfrom->tag_value.len;
	size+= pfrom->tag_value.len;

	if(record_route.len && record_route.s)
	{
		presentity->record_route.s= (char*)presentity + size;
		memcpy(presentity->record_route.s, record_route.s, record_route.len);
		presentity->record_route.len= record_route.len;
		size+= record_route.len;
		pkg_free(record_route.s);
	}


	presentity->contact.s= (char*)presentity + size;
	memcpy(presentity->contact.s, hentity->contact.s, hentity->contact.len);
	presentity->contact.len= hentity->contact.len;
	size+= hentity->contact.len;

	if(hentity->id.s)
	{
		presentity->id.s=(char*)presentity+ size;
		memcpy(presentity->id.s, hentity->id.s, 
			hentity->id.len);
		presentity->id.len= hentity->id.len; 
		size+= presentity->id.len;
	}

	if(hentity->extra_headers.s && hentity->extra_headers.len)
	{
		presentity->extra_headers.s= (char*)shm_malloc(hentity->extra_headers.len* sizeof(char));
		if(presentity->extra_headers.s== NULL)
		{
			ERR_MEM(SHARE_MEM);
		}
		memcpy(presentity->extra_headers.s, hentity->extra_headers.s, hentity->extra_headers.len);
		presentity->extra_headers.len= hentity->extra_headers.len;
	}

	/* write the remote contact filed */
	presentity->remote_contact.s= (char*)shm_malloc(contact.len* sizeof(char));
	if(presentity->remote_contact.s== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memcpy(presentity->remote_contact.s, contact.s, contact.len);
	presentity->remote_contact.len= contact.len;

	presentity->event|= hentity->event;
	presentity->flag= hentity->flag;
	presentity->etag.s= NULL;
	presentity->cseq= cseq;
	presentity->desired_expires= hentity->desired_expires;
	presentity->expires= lexpire+ (int)time(NULL);
	if(BLA_SUBSCRIBE & presentity->flag)
	{
		LM_DBG("BLA_SUBSCRIBE FLAG inserted\n");
	}
	LM_DBG("record for subscribe from %.*s to %.*s inserted in datatbase\n",
			presentity->watcher_uri->len, presentity->watcher_uri->s,
			presentity->pres_uri->len, presentity->pres_uri->s);
	insert_htable(presentity);

done:
	if(hentity->ua_flag == REQ_OTHER)
	{
		hentity->flag= flag;
		run_pua_callbacks( hentity, msg);
	}
error:
	if(hentity)
	{
	        if(presentity)
	        {
		        if(presentity->extra_headers.s)
		                shm_free(presentity->extra_headers.s);
		        if(presentity->remote_contact.s)
		                shm_free(presentity->remote_contact.s);
	        }
		shm_free(hentity);
		hentity= NULL;
	}
	return;
}
Exemple #20
0
int update_rlsubs( subs_t* subs, unsigned int hash_code)
{
	subs_t* s;

	if (dbmode == RLS_DB_ONLY)
	{
		LM_ERR( "update_rlsubs called in RLS_DB_ONLY mode\n" );
	} 

	/* search the record in hash table */
	lock_get(&rls_table[hash_code].lock);

	s= pres_search_shtable(rls_table, subs->callid,
			subs->to_tag, subs->from_tag, hash_code);
	if(s== NULL)
	{
		LM_DBG("record not found in hash table\n");
		lock_release(&rls_table[hash_code].lock);
		return -1;
	}

	s->expires= subs->expires+ (int)time(NULL);
	
	if(s->db_flag & NO_UPDATEDB_FLAG)
		s->db_flag= UPDATEDB_FLAG;
	
	if(	s->remote_cseq>= subs->remote_cseq)
	{
		lock_release(&rls_table[hash_code].lock);
		LM_DBG("stored cseq= %d\n", s->remote_cseq);
		return Stale_cseq_code;
	}

	s->remote_cseq= subs->remote_cseq;

	subs->pres_uri.s= (char*)pkg_malloc(s->pres_uri.len* sizeof(char));
	if(subs->pres_uri.s== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memcpy(subs->pres_uri.s, s->pres_uri.s, s->pres_uri.len);
	subs->pres_uri.len= s->pres_uri.len;

	if(s->record_route.s!=NULL && s->record_route.len>0)
	{
		subs->record_route.s =
				(char*)pkg_malloc(s->record_route.len* sizeof(char));
		if(subs->record_route.s==NULL)
		{
			ERR_MEM(PKG_MEM_STR);
		}
		memcpy(subs->record_route.s, s->record_route.s, s->record_route.len);
		subs->record_route.len= s->record_route.len;
	}

	subs->local_cseq= s->local_cseq;
	subs->version= s->version;

	lock_release(&rls_table[hash_code].lock);

	return 0;

error:
	lock_release(&rls_table[hash_code].lock);
	return -1;
}
Exemple #21
0
static void send_notifies(db1_res_t *result, int did_col, int resource_uri_col, int auth_state_col, int reason_col,
                   int pres_state_col, int content_type_col)
{
	int i;
	char* prev_did= NULL, * curr_did= NULL;
	db_row_t *row;	
	db_val_t *row_vals;
	char* resource_uri;
	str pres_state = {0, 0};
	xmlDocPtr rlmi_doc= NULL;
	xmlNodePtr list_node= NULL, instance_node= NULL, resource_node;
	unsigned int hash_code= 0;
	int size= BUF_REALLOC_SIZE, buf_len= 0;	
	char* buf= NULL, *auth_state= NULL, *boundary_string= NULL;
	str cid = {0,0};
	str content_type= {0, 0};
	int auth_state_flag;
	int chunk_len=0;
	str bstr= {0, 0};
	subs_t* dialog= NULL;
	int len_est = 0;
	int resource_added = 0; /* Flag to indicate that we have added at least one resource */

	/* generate the boundary string */
	boundary_string= generate_string(BOUNDARY_STRING_LEN);
	bstr.len= strlen(boundary_string);
	bstr.s= (char*)pkg_malloc((bstr.len+ 1)* sizeof(char));
	if(bstr.s== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memcpy(bstr.s, boundary_string, bstr.len);
	bstr.s[bstr.len]= '\0';

	/* Allocate an initial buffer for the multipart body.
	 * This buffer will be reallocated if neccessary */
	buf= pkg_malloc(size* sizeof(char));
	if(buf== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}

	LM_DBG("found %d records with updated state\n", result->n);
	for(i= 0; i< result->n; i++)
	{
		row = &result->rows[i];
		row_vals = ROW_VALUES(row);
		
		curr_did=     (char*)row_vals[did_col].val.string_val;
		resource_uri= (char*)row_vals[resource_uri_col].val.string_val;
		auth_state_flag=     row_vals[auth_state_col].val.int_val;
		pres_state.s=   (char*)row_vals[pres_state_col].val.string_val;
		pres_state.len = strlen(pres_state.s);
		trim(&pres_state);
		
		/* If we have moved onto a new resource list Subscribe dialog indentifier, 
		   send a NOTIFY for the previous ID and then drop the existing documents. */
		if(prev_did!= NULL && strcmp(prev_did, curr_did)) 
		{
			if (send_notify(&rlmi_doc, buf, buf_len, bstr, dialog, hash_code))
			{  
				LM_ERR("in send_notify\n");
				goto error;
			}
			len_est = 0;
			pkg_free(dialog);
			dialog= NULL;
		}

		/*if first or different*/
		if(prev_did==NULL || strcmp(prev_did, curr_did)!=0)
		{
			/* Work out a subscription from the did. */
			get_dialog_from_did(curr_did, &dialog, &hash_code);
			if(dialog== NULL)
			{
				prev_did = NULL;
				LM_INFO("Dialog is NULL\n");
				continue;
			}
		
			len_est = create_empty_rlmi_doc(&rlmi_doc, &list_node, &dialog->pres_uri, dialog->version, 0);
			len_est += 2*strlen(boundary_string)+4+102+2+50+strlen(resource_uri)+20;
			buf_len= 0;
			resource_added = 0;

			/* !!!! for now I will include the auth state without checking if 
			 * it has changed - > in future chech if it works */		
		}

		/* add a node in rlmi_doc and if any presence state registered add 
		 * it in the buffer */
		
		resource_node= xmlNewChild(list_node,NULL,BAD_CAST "resource", NULL);
		if(resource_node== NULL)
		{
			LM_ERR("when adding resource child\n");
			goto done;
		}
		xmlNewProp(resource_node, BAD_CAST "uri", BAD_CAST resource_uri);
		len_est += strlen (resource_uri) + 35; /* <resource uri="[uri]"></resource>/r/n */
		resource_added = 1;

		/* there might be more records with the same uri- more instances-
		 * search and add them all */
		
		while(1)
		{
			cid.s= NULL;
			cid.len= 0;
			
			auth_state= get_auth_string(auth_state_flag);
			if(auth_state== NULL)
			{
				LM_ERR("bad authorization status flag\n");
				goto error;
			}	
			len_est += strlen(auth_state) + 38; /* <instance id="12345678" state="[auth_state]" />r/n */

			if(auth_state_flag & ACTIVE_STATE)
			{
				cid.s= generate_cid(resource_uri, strlen(resource_uri));
				cid.len = strlen(cid.s);
				len_est += cid.len + 8; /* cid="[cid]" */
				content_type.s = (char*)row_vals[content_type_col].val.string_val;
				content_type.len = strlen(content_type.s);
				chunk_len = 4 + bstr.len
							+ 35
							+ 16 + cid.len
							+ 18 + content_type.len
							+ 4 + pres_state.len + 8;
				len_est += chunk_len;
			}
			else
			if(auth_state_flag & TERMINATED_STATE)
			{
				len_est += strlen(row_vals[resource_uri_col].val.string_val) + 10; /* reason="[resaon]" */
			}
            
			if (rls_max_notify_body_len > 0 && len_est > rls_max_notify_body_len)
			{
				/* We have a limit on body length set, and we were about to exceed it */
				if (resource_added == 1)
				{
					/* We added at least one resource. */
					LM_DBG("timer_send_notify hit the size limit. len_est = %d\n", len_est);
					if (send_notify(&rlmi_doc, buf, buf_len, bstr, dialog, hash_code))
					{
						LM_ERR("in send_notify\n");
						goto error;
					}
					i --;
				}
				else
				{
					LM_DBG("timer_send_notify hit the size limit. NO RESOURCE ADDED len_est = %d\n", len_est);
				}
				len_est = 0;

				pkg_free(dialog);
				dialog= NULL;
				curr_did=NULL;
				break;
			}

			/* OK, we are happy this will fit */
			instance_node= xmlNewChild(resource_node, NULL, BAD_CAST "instance", NULL);
			if(instance_node== NULL)
			{
				LM_ERR("while adding instance child\n");
				goto error;
			}	

			/* Instance ID should be unique for each instance node
 			   within a resource node.  The same instance ID can be
			   used in different resource nodes.  Instance ID needs
			   to remain the same for each resource instance in
			   future updates.  We can just use a common string
			   here because you will only get multiple instances
			   for a resource when the back-end SUBSCRIBE is forked
			   and pua does not support this.  If/when pua supports
			   forking of the SUBSCRIBEs it sends this will need to
			   be fixed properly. */
			xmlNewProp(instance_node, BAD_CAST "id", 
					BAD_CAST instance_id);
			if(auth_state_flag & ACTIVE_STATE)
			{
				xmlNewProp(instance_node, BAD_CAST "state", BAD_CAST auth_state);
			}
			else
			if(auth_state_flag & TERMINATED_STATE)
			{
				xmlNewProp(instance_node, BAD_CAST "reason",
						BAD_CAST row_vals[resource_uri_col].val.string_val);
			}
			xmlNewProp(instance_node, BAD_CAST "cid", BAD_CAST cid.s);

			/* add in the multipart buffer */
			if(cid.s)
			{
	
				while(buf_len + chunk_len >= size)
				{
					REALLOC_BUF
				}
				buf_len+= sprintf(buf+ buf_len, "--%.*s\r\n", bstr.len,
						bstr.s);
				buf_len+= sprintf(buf+ buf_len,
						"Content-Transfer-Encoding: binary\r\n");
				buf_len+= sprintf(buf+ buf_len, "Content-ID: <%.*s>\r\n",
						cid.len, cid.s);
				buf_len+= sprintf(buf+ buf_len, "Content-Type: %.*s\r\n\r\n",
						content_type.len, content_type.s);
				buf_len+= sprintf(buf+buf_len,"%.*s\r\n\r\n", pres_state.len,
						pres_state.s);
			}

			i++;
			if(i== result->n)
			{
				i--;
				break;
			}
	
			row = &result->rows[i];
			row_vals = ROW_VALUES(row);

			if(strncmp(resource_uri, row_vals[resource_uri_col].val.string_val,
					strlen(resource_uri))
				|| strncmp(curr_did, row_vals[did_col].val.string_val,
					strlen(curr_did)))
			{
				i--;
				break;
			}
			resource_uri= (char*)row_vals[resource_uri_col].val.string_val;
			auth_state_flag=     row_vals[auth_state_col].val.int_val;
			pres_state.s=   (char*)row_vals[pres_state_col].val.string_val;
			pres_state.len= strlen(pres_state.s);
			trim(&pres_state);
		}

		prev_did= curr_did;
	}

	if(rlmi_doc)
	{
		LM_DBG("timer_send_notify at end len_est = %d resource_added = %d\n", len_est, resource_added);
		if (resource_added == 1)
		{
			send_notify(&rlmi_doc, buf, buf_len, bstr, dialog, hash_code);
		}
		if(dialog)
		{
			pkg_free(dialog);
		}
		dialog= NULL;
	}

	
error:
done:
	if(bstr.s)
		pkg_free(bstr.s);

	if(buf)
		pkg_free(buf);
	if(dialog)
		pkg_free(dialog);
	return;
}
Exemple #22
0
int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
{
	db_key_t query_cols[5];
	db_val_t query_vals[5];
	db_key_t result_cols[3];
	int n_query_cols = 0;
	db_res_t *result = 0;
	db_row_t *row;
	db_val_t *row_vals;
	str body;
	str* doc= NULL;
	int n_result_cols= 0, xcap_doc_col;
	static str tmp1 = str_init("username");
	static str tmp2 = str_init("domain");
	static str tmp3 = str_init("doc_type");
	static str tmp4 = str_init("doc");

	*rules_doc= NULL;
	
	if(force_active)
		return 0;
	
	LM_DBG("[user]= %.*s\t[domain]= %.*s\n", 
			user->len, user->s,	domain->len, domain->s);
	/* first search in database */
	query_cols[n_query_cols] = &tmp1;
	query_vals[n_query_cols].type = DB_STR;
	query_vals[n_query_cols].nul = 0;
	query_vals[n_query_cols].val.str_val = *user;
	n_query_cols++;
	
	query_cols[n_query_cols] = &tmp2;
	query_vals[n_query_cols].type = DB_STR;
	query_vals[n_query_cols].nul = 0;
	query_vals[n_query_cols].val.str_val = *domain;
	n_query_cols++;
	
	query_cols[n_query_cols] = &tmp3;
	query_vals[n_query_cols].type = DB_INT;
	query_vals[n_query_cols].nul = 0;
	query_vals[n_query_cols].val.int_val= type;
	n_query_cols++;

	result_cols[xcap_doc_col= n_result_cols++] = &tmp4;
	
	if (pxml_dbf.use_table(pxml_db, &xcap_table) < 0) 
	{
		LM_ERR("in use_table-[table]= %.*s\n", xcap_table.len, xcap_table.s);
		return -1;
	}

	if( pxml_dbf.query(pxml_db, query_cols, 0 , query_vals, result_cols, 
				n_query_cols, 1, 0, &result)<0)
	{
		LM_ERR("while querying table xcap for [user]=%.*s\t[domain]= %.*s\n",
				user->len, user->s,	domain->len, domain->s);
		if(result)
			pxml_dbf.free_result(pxml_db, result);
		return -1;
	}
	if(result== NULL)
		return -1;

	if(result->n<= 0)
	{
		LM_DBG("No document found in db table for [user]=%.*s"
			"\t[domain]= %.*s\t[doc_type]= %d\n",user->len, user->s,
			domain->len, domain->s, type);
		
		if(!integrated_xcap_server)
		{
			if(http_get_rules_doc(*user, *domain, &body)< 0)
			{
				LM_ERR("fetching document with HTTP request from xcap server\n");
				goto error;
			}
			if(body.s && body.len)  /* if the document was found */
				goto done;
		}
		pxml_dbf.free_result(pxml_db, result);
		return 0;
	}
	
	row = &result->rows[xcap_doc_col];
	row_vals = ROW_VALUES(row);

	switch (row_vals[0].type) {
		case DB_STRING:
			LM_DBG("extracted db_string\n");
			body.s = (char*)row_vals[0].val.string_val;
			if (body.s)
				body.len = strlen(body.s);
			break;
		case DB_STR:
			LM_DBG("extracted db_str\n");
			body = row_vals[0].val.str_val;
			break;
		case DB_BLOB:
			LM_DBG("extracted db_blob\n");
			body = row_vals[0].val.blob_val;
			break;
		default:
			LM_ERR("unexpected column type %d\n",row_vals[0].type);
			goto error;
	}

	if(body.s== NULL)
	{
		LM_ERR("Xcap doc NULL\n");
		goto error;
	}	

	if(body.len== 0)
	{
		LM_ERR("Xcap doc empty\n");
		goto error;
	}			
	LM_DBG("xcap document:\n%.*s", body.len,body.s);

done:
	doc= (str*)pkg_malloc(sizeof(str));
	if(doc== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	doc->s= (char*)pkg_malloc(body.len* sizeof(char));
	if(doc->s== NULL)
	{
		pkg_free(doc);
		ERR_MEM(PKG_MEM_STR);
	}
	memcpy(doc->s, body.s, body.len);
	doc->len= body.len;

	*rules_doc= doc;

	if(result)
		pxml_dbf.free_result(pxml_db, result);

	return 0;

error:
	if(result)
		pxml_dbf.free_result(pxml_db, result);

	return -1;

}
Exemple #23
0
int add_event(pres_ev_t* event)
{
	pres_ev_t* ev= NULL;
	event_t parsed_event;
	str wipeer_name;
	char* sep;
	char buf[50];
	int not_in_list= 0;


	if(EvList == NULL)
	{
		LM_ERR("'presence' modules must be loaded before this module\n");
		return -1;
	}

	memset(&parsed_event, 0, sizeof(event_t));

	if(event->name.s== NULL || event->name.len== 0)
	{
		LM_ERR("NULL event name\n");
		return -1;
	}

	if(event->content_type.s== NULL || event->content_type.len== 0)
	{
		if (event->mandatory_body)
		{
			LM_ERR("NULL content_type param\n");
			return -1;
		}
	}

	ev= contains_event(&event->name, &parsed_event);
	if(ev== NULL)
	{
		not_in_list= 1;
		ev= (pres_ev_t*)shm_malloc(sizeof(pres_ev_t));
		if(ev== NULL)
		{
			free_event_params(parsed_event.params, PKG_MEM_TYPE);
			ERR_MEM(SHARE_MEM);
		}
		memset(ev, 0, sizeof(pres_ev_t));
		ev->name.s= (char*)shm_malloc(event->name.len);
		if(ev->name.s== NULL)
		{
			free_event_params(parsed_event.params, PKG_MEM_TYPE);
			ERR_MEM(SHARE_MEM);
		}
		memcpy(ev->name.s, event->name.s, event->name.len);
		ev->name.len= event->name.len;

		ev->evp= shm_copy_event(&parsed_event);
		if(ev->evp== NULL)
		{
			LM_ERR("copying event_t structure\n");
			free_event_params(parsed_event.params, PKG_MEM_TYPE);
			goto error;
		}
		free_event_params(parsed_event.params, PKG_MEM_TYPE);
	}
	else
	{
		free_event_params(parsed_event.params, PKG_MEM_TYPE);
		if(ev->content_type.s)
		{
			LM_DBG("Event already registered\n");
			return 0;
		}
	}

	ev->content_type.s = (char*)shm_malloc(event->content_type.len);
	if(ev->content_type.s== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	ev->content_type.len= event->content_type.len;
	memcpy(ev->content_type.s, event->content_type.s, event->content_type.len);

	sep= strchr(event->name.s, '.');
	if(sep != NULL && strncasecmp(sep+1, "winfo", 5)== 0)
	{
		ev->type= WINFO_TYPE;
		wipeer_name.s= event->name.s;
		wipeer_name.len= sep - event->name.s;
		ev->wipeer= contains_event(&wipeer_name, NULL);
	}
	else
	{
		ev->type= PUBL_TYPE;
		wipeer_name.s= buf;
		memcpy(wipeer_name.s, event->name.s, event->name.len);
		wipeer_name.len= event->name.len;
		memcpy(wipeer_name.s+ wipeer_name.len, ".winfo", 6);
		wipeer_name.len+= 6;
		ev->wipeer= contains_event(&wipeer_name, NULL);
	}

	if(ev->wipeer)
		ev->wipeer->wipeer= ev;

	if(event->req_auth &&
		( event->get_auth_status==0 ||event->get_rules_doc== 0))
	{
		LM_ERR("bad event structure\n");
		goto error;
	}

	ev->extra_hdrs= event->extra_hdrs;
	ev->req_auth= event->req_auth;
	ev->agg_nbody= event->agg_nbody;
	ev->apply_auth_nbody= event->apply_auth_nbody;
	ev->get_auth_status= event->get_auth_status;
	ev->get_rules_doc= event->get_rules_doc;
	ev->evs_publ_handl= event->evs_publ_handl;
	ev->evs_subs_handl= event->evs_subs_handl;
	ev->mandatory_body = event->mandatory_body;
	ev->mandatory_timeout_notification = event->mandatory_timeout_notification;
	ev->etag_not_new= event->etag_not_new;
	ev->aux_body_processing= event->aux_body_processing;
	ev->aux_free_body= event->aux_free_body;
	ev->free_body= event->free_body;
	ev->build_empty_pres_info= event->build_empty_pres_info;
	ev->default_expires= event->default_expires;

	if(not_in_list)
	{
		ev->next= EvList->events;
		EvList->events= ev;
	}
	EvList->ev_count++;

	LM_DBG("succesfully added event: %.*s - len= %d\n",ev->name.len,
			ev->name.s, ev->name.len);

	/* if event 'presence' set the pointer */
	if(ev->evp->parsed == EVENT_PRESENCE)
		*pres_event_p = ev;
	/* if event 'dialog' set the pointer */
	if(ev->evp->parsed == EVENT_DIALOG)
		*dialog_event_p = ev;

	return 0;
error:
	if(ev && not_in_list)
	{
		free_pres_event(ev);
	}
	return -1;
}
Exemple #24
0
str* offline_nbody(str* body)
{
	xmlDocPtr doc= NULL;
	xmlDocPtr new_doc= NULL;
	xmlNodePtr node, tuple_node= NULL, status_node;
	xmlNodePtr root_node, add_node, pres_node;
	str* new_body;

	doc= xmlParseMemory(body->s, body->len);
	if(doc==  NULL)
	{
		LM_ERR("while parsing xml memory\n");
		return NULL;
	}
	node= xmlDocGetNodeByName(doc, "basic", NULL);
	if(node== NULL)
	{
		LM_ERR("while extracting basic node\n");
		goto error;
	}
	xmlNodeSetContent(node, (const unsigned char*)"closed");

	tuple_node= xmlDocGetNodeByName(doc, "tuple", NULL);
	if(tuple_node== NULL)
	{
		LM_ERR("while extracting tuple node\n");
		goto error;
	}
	status_node= xmlDocGetNodeByName(doc, "status", NULL);
	if(status_node== NULL)
	{
		LM_ERR("while extracting tuple node\n");
		goto error;
	}

	pres_node= xmlDocGetNodeByName(doc, "presence", NULL);
	if(node== NULL)
	{
		LM_ERR("while extracting presence node\n");
		goto error;
	}

    new_doc = xmlNewDoc(BAD_CAST "1.0");
    if(new_doc==0)
		goto error;
	root_node= xmlCopyNode(pres_node, 2);
	if(root_node== NULL)
	{
		LM_ERR("while copying node\n");
		goto error;
	}
    xmlDocSetRootElement(new_doc, root_node);

	tuple_node= xmlCopyNode(tuple_node, 2);
	if(tuple_node== NULL)
	{
		LM_ERR("while copying node\n");
		goto error;
	}
	xmlAddChild(root_node, tuple_node);

	add_node= xmlCopyNode(status_node, 1);
	if(add_node== NULL)
	{
		LM_ERR("while copying node\n");
		goto error;
	}
	xmlAddChild(tuple_node, add_node);

	new_body = (str*)pkg_malloc(sizeof(str));
	if(new_body == NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	memset(new_body, 0, sizeof(str));

	xmlDocDumpFormatMemory(new_doc,(xmlChar**)(void*)&new_body->s,
		&new_body->len, 1);

	xmlFreeDoc(doc);
	xmlFreeDoc(new_doc);
	xmlCleanupParser();
	xmlMemoryDump();

	return new_body;

error:
	if(doc)
		xmlFreeDoc(doc);
	if(new_doc)
		xmlFreeDoc(new_doc);
	return NULL;

}		
Exemple #25
0
str* agregate_xmls(str* pres_user, str* pres_domain, str** body_array, int n)
{
	int i, j= 0, append ;
	xmlNodePtr p_root= NULL, new_p_root= NULL ;
	xmlDocPtr* xml_array ;
	xmlNodePtr node = NULL;
	xmlNodePtr add_node = NULL ;
	str *body= NULL;
	char* id= NULL, *tuple_id = NULL;

	xml_array = (xmlDocPtr*)pkg_malloc( (n+2)*sizeof(xmlDocPtr));
	if(xml_array== NULL)
	{
	
		LM_ERR("while alocating memory");
		return NULL;
	}
	memset(xml_array, 0, (n+2)*sizeof(xmlDocPtr)) ;

	for(i=0; i<n; i++)
	{
		if(body_array[i] == NULL )
			continue;

		xml_array[j] = NULL;
		xml_array[j] = xmlParseMemory( body_array[i]->s, body_array[i]->len );
		
		if( xml_array[j]== NULL)
		{
			LM_ERR("while parsing xml body message\n");
			goto error;
		}
		j++;

	} 

	if(j== 0)  /* no body */
	{
		if(xml_array)
			pkg_free(xml_array);
		return NULL;
	}

	j--;
	p_root = xmlDocGetNodeByName( xml_array[j], "presence", NULL);
	if(p_root ==NULL)
	{
		LM_ERR("while geting the xml_tree root\n");
		goto error;
	}

	for(i= j-1; i>=0; i--)
	{
		new_p_root= xmlDocGetNodeByName( xml_array[i], "presence", NULL);
		if(new_p_root ==NULL)
		{
			LM_ERR("while geting the xml_tree root\n");
			goto error;
		}

		append= 1;
		node= xmlNodeGetChildByName(new_p_root, "tuple");
		if(node != NULL)
		{
			tuple_id= xmlNodeGetAttrContentByName(node, "id");
			if(tuple_id== NULL)
			{
				LM_ERR("while extracting tuple id\n");
				goto error;
			}
			for (node = p_root->children; node!=NULL; node = node->next)
			{		
				if( xmlStrcasecmp(node->name,(unsigned char*)"text")==0)
					continue;
			
				if( xmlStrcasecmp(node->name,(unsigned char*)"tuple")==0)
				{
					id = xmlNodeGetAttrContentByName(node, "id");
					if(id== NULL)
					{
						LM_ERR("while extracting tuple id\n");
						goto error;
					}
				
					if(xmlStrcasecmp((unsigned char*)tuple_id,
								(unsigned char*)id )== 0)
					{
						append = 0;
						xmlFree(id);
						break;
					}
					xmlFree(id);
				}
			}
			xmlFree(tuple_id);
			tuple_id= NULL;
		}

		if(append) 
		{	
			for(node= new_p_root->children; node; node= node->next)
			{	
				add_node= xmlCopyNode(node, 1);
				if(add_node== NULL)
				{
					LM_ERR("while copying node\n");
					goto error;
				}
				if(xmlAddChild(p_root, add_node)== NULL)
				{
					LM_ERR("while adding child\n");
					goto error;
				}
								
			}
		}
	}

	body = (str*)pkg_malloc(sizeof(str));
	if(body == NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}

	xmlDocDumpFormatMemory(xml_array[j],(xmlChar**)(void*)&body->s, 
			&body->len, 1);	

  	for(i=0; i<=j; i++)
	{
		if(xml_array[i]!=NULL)
			xmlFreeDoc( xml_array[i]);
	}
	if(xml_array!=NULL)
		pkg_free(xml_array);
    
	xmlCleanupParser();
    xmlMemoryDump();

	return body;

error:
	if(xml_array!=NULL)
	{
		for(i=0; i<=j; i++)
		{
			if(xml_array[i]!=NULL)
				xmlFreeDoc( xml_array[i]);
		}
		pkg_free(xml_array);
	}
	if(tuple_id)
		xmlFree(tuple_id);
	if(body)
		pkg_free(body);

	return NULL;
}
Exemple #26
0
int get_rules_doc(str* user, str* domain, str *file_uri, int type, str** rules_doc)
{
	db_key_t query_cols[3];
	db_val_t query_vals[3];
	db_key_t result_cols[1];
	int n_query_cols = 0;
	db1_res_t *result = 0;
	db_row_t *row;
	db_val_t *row_vals;
	str body;
	str* doc= NULL;
	int n_result_cols= 0, xcap_doc_col;
	static str tmp1 = str_init("doc_type");
	static str tmp2 = str_init("doc_uri");
	static str tmp3 = str_init("username");
	static str tmp4 = str_init("domain");
	static str tmp5 = str_init("doc");

	if(force_active)
	{
		*rules_doc= NULL;
		return 0;
	}
	LM_DBG("[user]= %.*s\t[domain]= %.*s", 
			user->len, user->s,	domain->len, domain->s);

	/* first search in database */
	query_cols[n_query_cols] = &tmp1;
	query_vals[n_query_cols].type = DB1_INT;
	query_vals[n_query_cols].nul = 0;
	query_vals[n_query_cols].val.int_val= type;
	n_query_cols++;

	if (file_uri != NULL)
	{
		query_cols[n_query_cols] = &tmp2;
		query_vals[n_query_cols].type = DB1_STR;
		query_vals[n_query_cols].nul = 0;
		query_vals[n_query_cols].val.str_val = *file_uri;
		n_query_cols++;
	}
	else if (user != NULL && domain != NULL)
	{
		query_cols[n_query_cols] = &tmp3;
		query_vals[n_query_cols].type = DB1_STR;
		query_vals[n_query_cols].nul = 0;
		query_vals[n_query_cols].val.str_val = *user;
		n_query_cols++;
	
		query_cols[n_query_cols] = &tmp4;
		query_vals[n_query_cols].type = DB1_STR;
		query_vals[n_query_cols].nul = 0;
		query_vals[n_query_cols].val.str_val = *domain;
		n_query_cols++;
	}
	else
	{
		LM_ERR("Need to specify file uri _OR_ username and domain\n");
		return -1;
	}
	
	result_cols[xcap_doc_col= n_result_cols++] = &tmp5;
	
	if (pxml_dbf.use_table(pxml_db, &xcap_table) < 0) 
	{
		LM_ERR("in use_table-[table]= %.*s\n", xcap_table.len, xcap_table.s);
		return -1;
	}

	if( pxml_dbf.query(pxml_db, query_cols, 0 , query_vals, result_cols, 
				n_query_cols, 1, 0, &result)<0)
	{
		LM_ERR("while querying table xcap for [user]=%.*s\t[domain]= %.*s\n",
				user->len, user->s,	domain->len, domain->s);
		if(result)
			pxml_dbf.free_result(pxml_db, result);
		return -1;
	}
	if(result== NULL)
		return -1;

	if(result->n<= 0)
	{
		LM_DBG("No document found in db table for [user]=%.*s"
			"\t[domain]= %.*s\t[doc_type]= %d\n",user->len, user->s,
			domain->len, domain->s, type);
		
		if (!integrated_xcap_server && type != PRES_RULES)
		{
			LM_WARN("Cannot retrieve non pres-rules documents from"
				"external XCAP server\n");
		}
		else if(!integrated_xcap_server)
		{
			if(http_get_rules_doc(*user, *domain, &body)< 0)
			{
				LM_ERR("sending http GET request to xcap server\n");		
				goto error;
			}
			if(body.s && body.len)
				goto done; 
		}
		pxml_dbf.free_result(pxml_db, result);
		return 0;
	}	
	
	row = &result->rows[xcap_doc_col];
	row_vals = ROW_VALUES(row);

	body.s = (char*)row_vals[0].val.string_val;
	if(body.s== NULL)
	{
		LM_ERR("Xcap doc NULL\n");
		goto error;
	}	
	body.len = strlen(body.s);
	if(body.len== 0)
	{
		LM_ERR("Xcap doc empty\n");
		goto error;
	}			
	LM_DBG("xcap document:\n%.*s", body.len,body.s);

done:
	doc= (str*)pkg_malloc(sizeof(str));
	if(doc== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	doc->s= (char*)pkg_malloc(body.len* sizeof(char));
	if(doc->s== NULL)
	{
		pkg_free(doc);
		ERR_MEM(PKG_MEM_STR);
	}
	memcpy(doc->s, body.s, body.len);
	doc->len= body.len;

	*rules_doc= doc;

	if(result)
		pxml_dbf.free_result(pxml_db, result);

	return 1;

error:
	if(result)
		pxml_dbf.free_result(pxml_db, result);

	return -1;

}
void subs_cback_func(struct cell *t, int cb_type, struct tmcb_params *ps)
{
	struct sip_msg* msg= NULL;
	int lexpire= 0;
	unsigned int cseq;
	ua_pres_t* presentity= NULL, *hentity= NULL;
	struct to_body *pto = NULL, TO = {0}, *pfrom = NULL;
	int size= 0;
	unsigned int hash_code;
	int flag ;
	str record_route= {0, 0};
	int rt;
	str contact;
	int initial_request = 0;
	int end_transaction = 1;

	if( ps->param== NULL || *ps->param== NULL )
	{
		LM_ERR("null callback parameter\n");
		return;
	}

	if (dbmode == PUA_DB_ONLY && pua_dbf.start_transaction)
	{
		if (pua_dbf.start_transaction(pua_db, db_table_lock) < 0)
		{
			LM_ERR("in start_transaction\n");
			goto error;
		}
	}

	LM_DBG("completed with status %d\n",ps->code) ;
	hentity= (ua_pres_t*)(*ps->param);
	hash_code= core_hash(hentity->pres_uri,hentity->watcher_uri,
				HASH_SIZE);
	flag= hentity->flag;
	if(hentity->flag & XMPP_INITIAL_SUBS)
		hentity->flag= XMPP_SUBSCRIBE;

	/* get dialog information from reply message: callid, to_tag, from_tag */
	msg= ps->rpl;
	if(msg == NULL)
	{
		LM_ERR("no reply message found\n ");
		goto error;
	}

	if(msg== FAKED_REPLY)
	{
		struct hdr_field *callid = NULL, *from = NULL;
		struct to_body FROM = {0};

		callid = (struct hdr_field *) pkg_malloc(sizeof(struct hdr_field));
		if (callid == NULL)
		{
			LM_ERR("Out of memory\n");
			goto faked_error;
		}
		memset(callid, 0, sizeof(struct hdr_field));
		get_hdr_field(t->callid.s, t->callid.s + t->callid.len, callid);
		hentity->call_id = callid->body;

		from = (struct hdr_field *) pkg_malloc(sizeof(struct hdr_field));
		if (from == NULL)
		{
			LM_ERR("Out of memory\n");
			goto faked_error;
		}
		memset(from, 0, sizeof(struct hdr_field));
		get_hdr_field(t->from.s, t->from.s + t->from.len, from);
		parse_to(from->body.s, from->body.s + from->body.len + 1, &FROM);
		if(FROM.uri.len <= 0) 
		{
			LM_ERR("'From' header NOT parsed\n");
			goto faked_error;
		}
	
		hentity->call_id = callid->body;
		hentity->from_tag = (&FROM)->tag_value;
		hentity->to_tag.s = NULL;
		hentity->to_tag.len = 0;

		find_and_delete_dialog(hentity, hash_code);
faked_error:
		if (callid) pkg_free(callid);
		free_to_params(&FROM);
		if (from) pkg_free(from);
		goto done;
	}
	
	if ( parse_headers(msg,HDR_EOH_F, 0)==-1 )
	{
		LM_ERR("when parsing headers\n");
		goto error;
	}

	if(ps->rpl->expires && msg->expires->body.len > 0)
	{
		if (!msg->expires->parsed && (parse_expires(msg->expires) < 0))
		{
			LM_ERR("cannot parse Expires header\n");
			goto error;
		}
		lexpire = ((exp_body_t*)msg->expires->parsed)->val;
		LM_DBG("lexpire= %d\n", lexpire);
	}

	/*if initial request */
	if(hentity->call_id.s== NULL)
	{
		initial_request = 1;

		if( msg->callid==NULL || msg->callid->body.s==NULL)
		{
			LM_ERR("cannot parse callid header\n");
			goto error;
		}
		
		if (!msg->from || !msg->from->body.s)
		{
			LM_ERR("cannot find 'from' header!\n");
			goto error;
		}
		if (msg->from->parsed == NULL)
		{
			if ( parse_from_header( msg )<0 ) 
			{
				LM_ERR("cannot parse From header\n");
				goto error;
			}
		}
		pfrom = (struct to_body*)msg->from->parsed;
	
		if( pfrom->tag_value.s ==NULL || pfrom->tag_value.len == 0)
		{
			LM_ERR("no from tag value present\n");
			goto error;
		}

		hentity->call_id=  msg->callid->body;
		hentity->from_tag= pfrom->tag_value;

		if(ps->code >= 300 || lexpire == 0)
		{
			hentity->to_tag.s = NULL;
			hentity->to_tag.len = 0;
			find_and_delete_dialog(hentity, hash_code);
			goto done;
		}

		if( msg->to==NULL || msg->to->body.s==NULL)
		{
			LM_ERR("cannot parse TO header\n");
			goto error;
		}			
		if(msg->to->parsed != NULL)
		{
			pto = (struct to_body*)msg->to->parsed;
			LM_DBG("'To' header ALREADY PARSED: <%.*s>\n",pto->uri.len,pto->uri.s);
		}
		else
		{
			parse_to(msg->to->body.s,msg->to->body.s +
				msg->to->body.len + 1, &TO);
			if(TO.uri.len <= 0) 
			{
				LM_ERR("'To' header NOT parsed\n");
				goto error;
			}
			pto = &TO;
		}			
		if( pto->tag_value.s ==NULL || pto->tag_value.len == 0)
		{
			LM_ERR("no to tag value present\n");
			goto error;
		}
		hentity->to_tag= pto->tag_value;
	}

	if(ps->code >= 300 )
	{	/* if an error code and a stored dialog delete it and try to send 
		   a subscription with type= INSERT_TYPE, else return*/	
		
		subs_info_t subs;

		hentity->to_tag.s = NULL;
		hentity->to_tag.len = 0;
		find_and_delete_dialog(hentity, hash_code);

		if (dbmode == PUA_DB_ONLY && pua_dbf.end_transaction)
		{
			if (pua_dbf.end_transaction(pua_db) < 0)
			{
				LM_ERR("in end_transaction\n");
				goto error;
			}
		}

		end_transaction = 0;

		/* Redirect if the response 3XX */
		memset(&subs, 0, sizeof(subs_info_t));
		subs.pres_uri= hentity->pres_uri; 
		subs.watcher_uri= hentity->watcher_uri;
		subs.contact= &hentity->contact;

		if(hentity->remote_contact.s)
			subs.remote_target= &hentity->remote_contact;

		if(hentity->desired_expires== 0)
			subs.expires= -1;
		else
		if(hentity->desired_expires< (int)time(NULL))
			subs.expires= 0;
		else
			subs.expires= hentity->desired_expires- (int)time(NULL)+ 3;

		subs.flag= INSERT_TYPE;
		subs.source_flag= flag;
		subs.event= hentity->event;
		subs.id= hentity->id;
		subs.outbound_proxy= hentity->outbound_proxy;
		subs.extra_headers= hentity->extra_headers;
		subs.cb_param= hentity->cb_param;
	
		if(send_subscribe(&subs)< 0)
		{
			LM_ERR("when trying to send SUBSCRIBE\n");
			goto error;
		}
		goto done;
	}

	if(lexpire== 0 )
	{
		LM_DBG("lexpire= 0 Delete from hash table");
		find_and_delete_dialog(hentity, hash_code);
		goto done;
	}

	/* extract the contact */
	if(msg->contact== NULL || msg->contact->body.s== NULL)
	{
		LM_ERR("no contact header found");
		goto error;
	}
	if( parse_contact(msg->contact) <0 )
	{
		LM_ERR(" cannot parse contact header\n");
		goto error;
	}
	if(msg->contact->parsed == NULL)
	{
		LM_ERR("cannot parse contact header\n");
		goto error;
	}
	contact = ((contact_body_t* )msg->contact->parsed)->contacts->uri;

	if( msg->cseq==NULL || msg->cseq->body.s==NULL)
	{
		LM_ERR("cannot parse cseq header\n");
		goto error;
	}

	if( str2int( &(get_cseq(msg)->number), &cseq)< 0)
	{
		LM_ERR("while converting str to int\n");
		goto error;
	}

	if(initial_request == 0)
	{
		hentity->cseq = cseq;
		find_and_update_dialog(hentity, hash_code, lexpire, &contact);
		goto done;
	}

	/*process record route and add it to a string*/
	if (msg->record_route!=NULL)
	{
		rt = print_rr_body(msg->record_route, &record_route, 1, 0);
		if(rt != 0)
		{
			LM_ERR("parsing record route [%d]\n", rt);	
			record_route.s=NULL;
			record_route.len=0;
		}
	}

	size= sizeof(ua_pres_t)+ 2*sizeof(str)+( pto->uri.len+
		pfrom->uri.len+ pto->tag_value.len+ pfrom->tag_value.len
		+msg->callid->body.len+ record_route.len+ hentity->contact.len+
		hentity->id.len )*sizeof(char);

	if(hentity->extra_headers)
		size+= sizeof(str)+ hentity->extra_headers->len*sizeof(char);

	presentity= (ua_pres_t*)shm_malloc(size);
	if(presentity== NULL)
	{
		LM_ERR("no more share memory\n");
		goto error;
	}
	
	memset(presentity, 0, size);
	size= sizeof(ua_pres_t);

	presentity->pres_uri= (str*)( (char*)presentity+ size);
	size+= sizeof(str);
	presentity->pres_uri->s= (char*)presentity+ size;
	memcpy(presentity->pres_uri->s, pto->uri.s, pto->uri.len);
	presentity->pres_uri->len= pto->uri.len;
	size+= pto->uri.len;

	presentity->watcher_uri= (str*)( (char*)presentity+ size);
	size+= sizeof(str);
	presentity->watcher_uri->s= (char*)presentity+ size;
	memcpy(presentity->watcher_uri->s, pfrom->uri.s, pfrom->uri.len);
	presentity->watcher_uri->len= pfrom->uri.len;
	size+= pfrom->uri.len;

	presentity->call_id.s= (char*)presentity + size;
	memcpy(presentity->call_id.s,msg->callid->body.s, 
		msg->callid->body.len);
	presentity->call_id.len= msg->callid->body.len;
	size+= presentity->call_id.len;

	presentity->to_tag.s= (char*)presentity + size;
	memcpy(presentity->to_tag.s,pto->tag_value.s, 
			pto->tag_value.len);
	presentity->to_tag.len= pto->tag_value.len;
	size+= pto->tag_value.len;

	presentity->from_tag.s= (char*)presentity + size;
	memcpy(presentity->from_tag.s,pfrom->tag_value.s, 
			pfrom->tag_value.len);
	presentity->from_tag.len= pfrom->tag_value.len;
	size+= pfrom->tag_value.len;

	if(record_route.len && record_route.s)
	{
		presentity->record_route.s= (char*)presentity + size;
		memcpy(presentity->record_route.s, record_route.s, record_route.len);
		presentity->record_route.len= record_route.len;
		size+= record_route.len;
		pkg_free(record_route.s);
		record_route.s = NULL;
	}

	presentity->contact.s= (char*)presentity + size;
	memcpy(presentity->contact.s, hentity->contact.s, hentity->contact.len);
	presentity->contact.len= hentity->contact.len;
	size+= hentity->contact.len;

	if(hentity->id.s)
	{
		presentity->id.s=(char*)presentity+ size;
		memcpy(presentity->id.s, hentity->id.s, 
			hentity->id.len);
		presentity->id.len= hentity->id.len; 
		size+= presentity->id.len;
	}

	if(hentity->extra_headers)
	{
		presentity->extra_headers= (str*)((char*)presentity+ size);
		size+= sizeof(str);
		presentity->extra_headers->s=(char*)presentity+ size;
		memcpy(presentity->extra_headers->s, hentity->extra_headers->s, 
			hentity->extra_headers->len);
		presentity->extra_headers->len= hentity->extra_headers->len; 
		size+= hentity->extra_headers->len;
	}

	/* write the remote contact filed */
	presentity->remote_contact.s= (char*)shm_malloc(contact.len* sizeof(char));
	if(presentity->remote_contact.s==NULL)
	{
		ERR_MEM(SHARE_MEM);
	}
	memcpy(presentity->remote_contact.s, contact.s, contact.len);
	presentity->remote_contact.len= contact.len;

	presentity->event|= hentity->event;
	presentity->flag= hentity->flag;
	presentity->etag.s= NULL;
	presentity->cseq= cseq;
	presentity->desired_expires= hentity->desired_expires;
	presentity->expires= lexpire+ (int)time(NULL);
	if(BLA_SUBSCRIBE & presentity->flag)
	{
		LM_DBG("BLA_SUBSCRIBE FLAG inserted\n");
	}	
	LM_DBG("record for subscribe from %.*s to %.*s inserted in datatbase\n",
			presentity->watcher_uri->len, presentity->watcher_uri->s,
			presentity->pres_uri->len, presentity->pres_uri->s);

	if (dbmode==PUA_DB_ONLY)
	{
		if (pua_dbf.end_transaction)
		{
			if (pua_dbf.end_transaction(pua_db) < 0)
			{
				LM_ERR("in end_transaction\n");
				goto error;
			}
		}

		if (pua_dbf.start_transaction)
		{
			if (pua_dbf.start_transaction(pua_db, db_table_lock) < 0)
			{
				LM_ERR("in start_transaction\n");
				goto error;
			}
		}

		if (convert_temporary_dialog_puadb(presentity) < 0)
		{
			LM_ERR("Could not convert temporary dialog into a dialog\n");
			goto error;
		}
	}
	else
	{
		if (convert_temporary_dialog(presentity) < 0)
		{
			LM_ERR("Could not convert temporary dialog into a dialog\n");
			goto error;
		}
	}

done:
	if(hentity->ua_flag == REQ_OTHER)
	{
		hentity->flag= flag;
		run_pua_callbacks( hentity, msg);
	}

	if (dbmode == PUA_DB_ONLY && pua_dbf.end_transaction && end_transaction)
	{
		if (pua_dbf.end_transaction(pua_db) < 0)
		{
			LM_ERR("in end_transaction\n");
			goto error;
		}
	}

	goto end;

error:	
        if (presentity)
	{
		if (presentity->remote_contact.s) shm_free(presentity->remote_contact.s);
	 	shm_free(presentity);
	}

	if(record_route.s)
		pkg_free(record_route.s);

	if (dbmode == PUA_DB_ONLY && pua_dbf.abort_transaction)
	{
		if (pua_dbf.abort_transaction(pua_db) < 0)
			LM_ERR("in abort_transaction\n");
	}

end:

	if(hentity)
	{	
		shm_free(hentity);
		hentity= NULL;
	}

	free_to_params(&TO);
	return;
}
Exemple #28
0
int reply_200(struct sip_msg* msg, str* local_contact, int expires, str* rtag)
{
	char* hdr_append;
	int len;
	int lexpire_len;
	char *lexpire_s;
	char* p;

	lexpire_s = int2str((unsigned long)expires, &lexpire_len);

	len = 9 /*"Expires: "*/ + lexpire_len + CRLF_LEN
		+ 10 /*"Contact: <"*/ + local_contact->len + 1 /*">"*/
		+ ((msg->rcv.proto!=PROTO_UDP)?15/*";transport=xxxx"*/:0)
		+ CRLF_LEN + 18 /*Require: eventlist*/ + CRLF_LEN;

	hdr_append = (char *)pkg_malloc( len );
	if(hdr_append == NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}

	p = hdr_append;
	/* expires header */
	memcpy(p, "Expires: ", 9);
	p += 9;
	memcpy(p,lexpire_s,lexpire_len);
	p += lexpire_len;
	memcpy(p,CRLF,CRLF_LEN);
	p += CRLF_LEN;
	/* contact header */
	memcpy(p,"Contact: <", 10);
	p += 10;
	memcpy(p,local_contact->s,local_contact->len);
	p += local_contact->len;
	if (msg->rcv.proto!=PROTO_UDP) {
		memcpy(p,";transport=",11);
		p += 11;
		p = proto2str(msg->rcv.proto, p);
		if (p==NULL) {
			LM_ERR("invalid proto\n");
			goto error;
		}
	}
	*(p++) = '>';
	memcpy(p, CRLF, CRLF_LEN);
	p += CRLF_LEN;

	memcpy(p, "Require: eventlist", 18);
	p += 18;
	memcpy(p, CRLF, CRLF_LEN);
	p += CRLF_LEN;

	if (add_lump_rpl( msg, hdr_append, p-hdr_append, LUMP_RPL_HDR)==0 )
	{
		LM_ERR("unable to add lump_rl\n");
		goto error;
	}

	if( rls_sigb.reply( msg, 200, &su_200_rpl, rtag)< 0)
	{
		LM_ERR("failed to send reply\n");
		goto error;
	}	
	pkg_free(hdr_append);
	return 0;

error:
	pkg_free(hdr_append);
	return -1;
}	
Exemple #29
0
int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
{
    db_key_t query_cols[5];
    db_val_t query_vals[5];
    db_key_t result_cols[3];
    int n_query_cols = 0;
    db1_res_t *result = 0;
    db_row_t *row;
    db_val_t *row_vals;
    str body;
    str* doc= NULL;
    int n_result_cols= 0, xcap_doc_col;
    static str tmp1 = str_init("username");
    static str tmp2 = str_init("domain");
    static str tmp3 = str_init("doc_type");
    static str tmp4 = str_init("doc");

    LM_DBG("[user]= %.*s\t[domain]= %.*s", 
	   user->len, user->s, domain->len, domain->s);

    query_cols[n_query_cols] = &tmp1;
    query_vals[n_query_cols].type = DB1_STR;
    query_vals[n_query_cols].nul = 0;
    query_vals[n_query_cols].val.str_val = *user;
    n_query_cols++;
    
    query_cols[n_query_cols] = &tmp2;
    query_vals[n_query_cols].type = DB1_STR;
    query_vals[n_query_cols].nul = 0;
    query_vals[n_query_cols].val.str_val = *domain;
    n_query_cols++;
    
    query_cols[n_query_cols] = &tmp3;
    query_vals[n_query_cols].type = DB1_INT;
    query_vals[n_query_cols].nul = 0;
    query_vals[n_query_cols].val.int_val= type;
    n_query_cols++;

    result_cols[xcap_doc_col= n_result_cols++] = &tmp4;

    if (pres_dbf.query(pres_dbh, query_cols, 0 , query_vals, result_cols, 
		       n_query_cols, 1, 0, &result) < 0) {
	LM_ERR("while querying table xcap for [user]=%.*s\t[domain]= %.*s\n",
	       user->len, user->s, domain->len, domain->s);
	if (result)
	    pres_dbf.free_result(pres_dbh, result);
	return -1;
    }

    if(result == NULL)
	return -1;

    if (result->n <= 0) {
	LM_DBG("No document found in db table for [user]=%.*s"
	       "\t[domain]= %.*s\t[doc_type]= %d\n",user->len, user->s,
	       domain->len, domain->s, type);
	pres_dbf.free_result(pres_dbh, result);
	return 0;
    }	
	
    row = &result->rows[xcap_doc_col];
    row_vals = ROW_VALUES(row);

    body.s = (char*)row_vals[0].val.string_val;
    if (body.s== NULL) {
	LM_ERR("Xcap doc NULL\n");
	goto error;
    }	
    body.len = strlen(body.s);
    if (body.len== 0) {
	LM_ERR("Xcap doc empty\n");
	goto error;
    }			
    LM_DBG("xcap document:\n%.*s", body.len,body.s);
    
    doc= (str*)pkg_malloc(sizeof(str));
    if (doc== NULL) {
	ERR_MEM(PKG_MEM_STR);
    }
    doc->s= (char*)pkg_malloc(body.len* sizeof(char));
    if (doc->s== NULL) {
	pkg_free(doc);
	ERR_MEM(PKG_MEM_STR);
    }
    memcpy(doc->s, body.s, body.len);
    doc->len= body.len;

    *rules_doc= doc;

    if (result)
	pres_dbf.free_result(pres_dbh, result);

    return 0;

error:
    if (result)
	pres_dbf.free_result(pres_dbh, result);

    return -1;

}
Exemple #30
0
int add_event(pres_ev_t* event)
{
	pres_ev_t* ev= NULL;
	event_t parsed_event;
	str wipeer_name;
	char* sep;
	char buf[50];
	int not_in_list= 0;

	memset(&parsed_event, 0, sizeof(event_t));

	if(event->name.s== NULL || event->name.len== 0)
	{
		LM_ERR("NULL event name\n");
		return -1;
	}

	if(event->content_type.s== NULL || event->content_type.len== 0)
	{
		LM_ERR("NULL content_type param\n");
		return -1;
	}
	
	ev= contains_event(&event->name, &parsed_event);
	if(ev== NULL)
	{
		not_in_list= 1;
		ev= (pres_ev_t*)shm_malloc(sizeof(pres_ev_t));
		if(ev== NULL)
		{
			free_event_params(parsed_event.params, PKG_MEM_TYPE);
			ERR_MEM(SHARE_MEM);
		}
		memset(ev, 0, sizeof(pres_ev_t));
		ev->name.s= (char*)shm_malloc(event->name.len* sizeof(char));
		if(ev->name.s== NULL)
		{
			free_event_params(parsed_event.params, PKG_MEM_TYPE);
			ERR_MEM(SHARE_MEM);
		}
		memcpy(ev->name.s, event->name.s, event->name.len);
		ev->name.len= event->name.len;

		ev->evp= shm_copy_event(&parsed_event);
		if(ev->evp== NULL)
		{
			LM_ERR("copying event_t structure\n");
			free_event_params(parsed_event.params, PKG_MEM_TYPE);
			goto error;
		}
		free_event_params(parsed_event.params, PKG_MEM_TYPE);
	}
	else
	{
		free_event_params(parsed_event.params, PKG_MEM_TYPE);
		if(ev->content_type.s)
		{
			LM_DBG("Event already registered\n");
			return 0;
		}
	}

	ev->content_type.s=(char*)shm_malloc(event->content_type.len* sizeof(char)) ;
	if(ev->content_type.s== NULL)
	{
		ERR_MEM(SHARE_MEM);
	}	
	ev->content_type.len= event->content_type.len;
	memcpy(ev->content_type.s, event->content_type.s, event->content_type.len);

	sep= strchr(event->name.s, '.');
	if(sep && strncmp(sep+1, "winfo", 5)== 0)
	{	
		ev->type= WINFO_TYPE;
		wipeer_name.s= event->name.s;
		wipeer_name.len= sep - event->name.s;
		ev->wipeer= contains_event(&wipeer_name, NULL);
	}
	else
	{	
		ev->type= PUBL_TYPE;
		wipeer_name.s= buf;
		memcpy(wipeer_name.s, event->name.s, event->name.len);
		wipeer_name.len= event->name.len;
		memcpy(wipeer_name.s+ wipeer_name.len, ".winfo", 6);
		wipeer_name.len+= 6;
		ev->wipeer= contains_event(&wipeer_name, NULL);
	}
	
	if(ev->wipeer)	
		ev->wipeer->wipeer= ev;

	if(event->req_auth && 
		( event->get_auth_status==0 ||event->get_rules_doc== 0))
	{
		LM_ERR("bad event structure\n");
		goto error;
	}
	ev->req_auth= event->req_auth;
	ev->agg_nbody= event->agg_nbody;
	ev->apply_auth_nbody= event->apply_auth_nbody;
	ev->get_auth_status= event->get_auth_status;
	ev->get_rules_doc= event->get_rules_doc;
	ev->evs_publ_handl= event->evs_publ_handl;
	ev->etag_not_new= event->etag_not_new;
	ev->free_body= event->free_body;
	ev->default_expires= event->default_expires;

	if(not_in_list)
	{
		ev->next= EvList->events;
		EvList->events= ev;
	}
	EvList->ev_count++;
	
	LM_DBG("succesfully added event: %.*s - len= %d\n",ev->name.len,
			ev->name.s, ev->name.len);
	return 0;
error:
	if(ev && not_in_list)
	{
		free_pres_event(ev);	
	}
	return -1;
}