Beispiel #1
0
sr_phonenum_item_t *sr_phonenum_add_item(str *name)
{
	sr_phonenum_item_t *it = NULL;
	unsigned int hashid = 0;

	hashid = get_hash1_raw(name->s, name->len);

	it = _sr_phonenum_list;
	while(it != NULL) {
		if(it->hashid == hashid && it->pvclass.len == name->len
				&& strncmp(it->pvclass.s, name->s, name->len) == 0)
			return it;
		it = it->next;
	}
	/* add new */
	it = (sr_phonenum_item_t *)pkg_malloc(sizeof(sr_phonenum_item_t));
	if(it == NULL) {
		LM_ERR("no more pkg\n");
		return NULL;
	}
	memset(it, 0, sizeof(sr_phonenum_item_t));
	it->pvclass.s = (char *)pkg_malloc(name->len + 1);
	if(it->pvclass.s == NULL) {
		LM_ERR("no more pkg.\n");
		pkg_free(it);
		return NULL;
	}
	memcpy(it->pvclass.s, name->s, name->len);
	it->pvclass.s[name->len] = '\0';
	it->pvclass.len = name->len;
	it->hashid = hashid;
	it->next = _sr_phonenum_list;
	_sr_phonenum_list = it;
	return it;
}
Beispiel #2
0
/* looks for a certificate in a table and increases access counter of that
   table item */
int get_cert_from_table(ttable *ptable, str *skey, tcert_item *ptarget)
{
	tcert_item* tmp_tcert_item;
	unsigned int uhash;
	int iret=0;

	uhash=get_hash1_raw(skey->s, skey->len) & (CERTIFICATE_TABLE_ENTRIES-1);

	/* we lock the whole bucket */
	lock_element(&ptable->entries[uhash]);

	tmp_tcert_item = search_item_in_table_unsafe(ptable,
												 (const void *)skey,
												 uhash);
	/* make a copy of found certificate and after the certificate
	 * verification we'll add it to certificate table */
	if (tmp_tcert_item) {
		memcpy(ptarget->scertpem.s, tmp_tcert_item->scertpem.s, tmp_tcert_item->scertpem.len);
		ptarget->scertpem.len=tmp_tcert_item->scertpem.len;
		/* we accessed this certificate */
		tmp_tcert_item->uaccessed++;
	}
	else
		iret=1;

	release_element(&ptable->entries[uhash]);

	return iret;
}
Beispiel #3
0
/* inserts an item to table, and removes the least item if the table is full */
int addcert2table(ttable *ptable, tcert_item *pcert)
{
	tcert_item *pshmcert;
	unsigned int uhash;

	if (!(pshmcert=(tcert_item *)shm_malloc(sizeof(*pshmcert)))) {
		LOG(L_ERR, "AUTH_IDENTITY:addcert2table: No enough shared memory\n");
		return -1;
	}
	memset(pshmcert, 0, sizeof(*pshmcert));
	if (str_duplicate(&pshmcert->surl, &pcert->surl))
		return -2;

	if (str_duplicate(&pshmcert->scertpem, &pcert->scertpem))
		return -3;

	pshmcert->ivalidbefore=pcert->ivalidbefore;
	pshmcert->uaccessed=1;

	uhash=get_hash1_raw(pcert->surl.s, pcert->surl.len) & (CERTIFICATE_TABLE_ENTRIES-1);

	if (insert_into_table(ptable, (void*)pshmcert, uhash))
		return -4;

	return 0;
}
Beispiel #4
0
tr_export_t* tr_lookup_class(str *tclass)
{
	tr_item_t *tri;
	unsigned int trid;

	if(tclass==0 || tclass->s==0)
	{
		LM_ERR("bad parameters\n");
		return NULL;
	}

	/* search in TR table */
	// trid = get_hash1_raw(tclass->s, tclass->len);
	trid = get_hash1_raw(tclass->s, tclass->len);
	tri = _tr_table[trid%TR_TABLE_SIZE];
	while(tri)
	{
		if(tri->trid==trid && tri->tre.tclass.len==tclass->len
				&& memcmp(tri->tre.tclass.s, tclass->s, tclass->len)==0)
			return &(tri->tre);
		tri = tri->next;
	}

	return NULL;
}
Beispiel #5
0
str *_dbg_pvcache_lookup(pv_spec_t *spec)
{
	dbg_pvcache_t *pvi;
	unsigned int pvid;
	str *name = NULL;

	if(spec==NULL)
		return NULL;

	if(_dbg_pvcache==NULL)
		return NULL;

	pvid = get_hash1_raw((char *)&spec, sizeof(pv_spec_t*));
	pvi = _dbg_pvcache[pvid%DBG_PVCACHE_SIZE];
	while(pvi)
	{
		if(pvi->spec==spec) {
			return pvi->pvname;
		}
		pvi = pvi->next;
	}
	name = pv_cache_get_name(spec);
	if(name!=NULL)
	{
		/*LM_DBG("Add name[%.*s] to pvcache\n", name->len, name->s);*/
		dbg_assign_add(name, spec);
	}
	return name;
}
Beispiel #6
0
pv_spec_t* pv_cache_lookup(str *name)
{
	pv_cache_t *pvi;
	unsigned int pvid;
	int found;

	if(_pv_cache_set==0)
		return NULL;

	pvid = get_hash1_raw(name->s, name->len);
	pvi = _pv_cache[pvid%PV_CACHE_SIZE];
	while(pvi)
	{
		if(pvi->pvid == pvid) {
			if(pvi->pvname.len==name->len)
			{
				found = strncmp(pvi->pvname.s, name->s, name->len);

				if(found==0)
				{
					LM_DBG("pvar [%.*s] found in cache\n",
							name->len, name->s);
					return &pvi->spec;
				}
			}
		}
		pvi = pvi->next;
	}
	return NULL;
}
Beispiel #7
0
static sr_xavp_t *xavp_new_value(str *name, sr_xval_t *val)
{
	sr_xavp_t *avp;
	int size;
	unsigned int id;

	if(name==NULL || name->s==NULL || val==NULL)
		return NULL;
	id = get_hash1_raw(name->s, name->len);

	size = sizeof(sr_xavp_t) + name->len + 1;
	if(val->type == SR_XTYPE_STR)
		size += val->v.s.len + 1;
	avp = (sr_xavp_t*)shm_malloc(size);
	if(avp==NULL)
		return NULL;
	memset(avp, 0, size);
	avp->id = id;
	avp->name.s = (char*)avp + sizeof(sr_xavp_t);
	memcpy(avp->name.s, name->s, name->len);
	avp->name.s[name->len] = '\0';
	avp->name.len = name->len;
	memcpy(&avp->val, val, sizeof(sr_xval_t));
	if(val->type == SR_XTYPE_STR)
	{
		avp->val.v.s.s = avp->name.s + avp->name.len + 1;
		memcpy(avp->val.v.s.s, val->v.s.s, val->v.s.len);
		avp->val.v.s.s[val->v.s.len] = '\0';
		avp->val.v.s.len = val->v.s.len;
	}

	return avp;
}
Beispiel #8
0
void do_uac_auth(str *method, str *uri,
		struct uac_credential *crd,
		struct authenticate_body *auth,
		HASHHEX response)
{
	HASHHEX ha1;
	HASHHEX ha2;

	if((auth->flags&QOP_AUTH) || (auth->flags&QOP_AUTH_INT))
	{
		/* if qop generate nonce-count and cnonce */
		cnonce.s = int2str(get_hash1_raw(auth->nonce.s, auth->nonce.len), 
						   &cnonce.len);

		/* do authentication */
		uac_calc_HA1( crd, auth, &cnonce, ha1);
		uac_calc_HA2( method, uri,
			auth, 0/*hentity*/, ha2 );

		uac_calc_response( ha1, ha2, auth, &nc, &cnonce, response);
		auth->nc = &nc;
		auth->cnonce = &cnonce;
	} else {
		/* do authentication */
		uac_calc_HA1( crd, auth, 0/*cnonce*/, ha1);
		uac_calc_HA2( method, uri,
			auth, 0/*hentity*/, ha2 );

		uac_calc_response( ha1, ha2, auth, 0/*nc*/, 0/*cnonce*/, response);
	}
}
Beispiel #9
0
int xavp_count(str *name, sr_xavp_t **start)
{
	sr_xavp_t *avp;
	unsigned int id;
	int n = 0;

	if(name==NULL || name->s==NULL)
		return -1;
	id = get_hash1_raw(name->s, name->len);
	
	if(start)
		avp = *start;
	else
		avp=*_xavp_list_crt;
	while(avp)
	{
		if(avp->id==id && avp->name.len==name->len
				&& strncmp(avp->name.s, name->s, name->len)==0)
		{
			n++;
		}
		avp=avp->next;
	}

	return n;
}
Beispiel #10
0
static sr_xavp_t *xavp_get_internal(str *name, sr_xavp_t **list, int idx, sr_xavp_t **prv)
{
	sr_xavp_t *avp;
	unsigned int id;
	int n = 0;

	if(name==NULL || name->s==NULL)
		return NULL;
	id = get_hash1_raw(name->s, name->len);
	
	if(list && *list)
		avp = *list;
	else
		avp = *_xavp_list_crt;
	while(avp)
	{
		if(avp->id==id && avp->name.len==name->len
				&& strncmp(avp->name.s, name->s, name->len)==0)
		{
			if(idx==n)
				return avp;
			n++;
		}
		if(prv)
			*prv = avp;
		avp = avp->next;
	}
	return NULL;
}
Beispiel #11
0
int redisc_free_reply(str *name)
{
	redisc_reply_t *rpl;
	unsigned int hid;

	if(name==NULL || name->len==0) {
		LM_ERR("invalid parameters");
		return -1;
	}

	hid = get_hash1_raw(name->s, name->len);

	rpl = _redisc_rpl_list;
	while(rpl) {

		if(rpl->hname==hid && rpl->rname.len==name->len
		   && strncmp(rpl->rname.s, name->s, name->len)==0) {
			if(rpl->rplRedis) {
				freeReplyObject(rpl->rplRedis);
				rpl->rplRedis = NULL;
			}

			return 0;
		}

		rpl = rpl->next;
	}

	/* reply entry not found. */
	return -1;
}
Beispiel #12
0
/* returns flag entry or 0 on not found */
inline static struct flag_entry* get_flag_entry(char* name, int len)
{
	int h;
	/* get hash */
	h=get_hash1_raw(name, len) & (FLAGS_NAME_HASH_ENTRIES-1);
	return flag_search(&name2flags[h], name, len);
}
Beispiel #13
0
pv_xcap_uri_t *pv_xcap_uri_get_struct(str *name)
{
	unsigned int id;
	pv_xcap_uri_t *it;

	id = get_hash1_raw(name->s, name->len);
	it = _pv_xcap_uri_root;

	while(it!=NULL)
	{
		if(id == it->id && name->len==it->name.len
				&& strncmp(name->s, it->name.s, name->len)==0)
		{
			LM_DBG("uri found [%.*s]\n", name->len, name->s);
			return it;
		}
		it = it->next;
	}

	it = (pv_xcap_uri_t*)pkg_malloc(sizeof(pv_xcap_uri_t));
	if(it==NULL)
	{
		LM_ERR("no more pkg\n");
		return NULL;
	}
	memset(it, 0, sizeof(pv_xcap_uri_t));

	it->id = id;
	it->name = *name;

	it->next = _pv_xcap_uri_root;
	_pv_xcap_uri_root = it;
	return it;
}
Beispiel #14
0
/* resgiter a new flag name and associates it with pos
 * pos== -1 => any position will do 
 * returns flag pos on success (>=0)
 *         -1  flag is an alias for an already existing flag
 *         -2  flag already registered
 *         -3  mem. alloc. failure
 *         -4  invalid pos
 *         -5 no free flags */
int register_flag(char* name, int pos)
{
	struct flag_entry* e;
	int len;
	unsigned int r;
	static unsigned int crt_flag=0;
	unsigned int last_flag;
	unsigned int h;
	
	len=strlen(name);
	h=get_hash1_raw(name, len) & (FLAGS_NAME_HASH_ENTRIES-1);
	/* check if the name already exists */
	e=flag_search(&name2flags[h], name, len);
	if (e){
		LOG(L_ERR, "ERROR: register_flag: flag %.*s already registered\n",
					len, name);
		return -2;
	}
	/* check if there is already another flag registered at pos */
	if (pos!=-1){
		if ((pos<0) || (pos>MAX_FLAG)){
			LOG(L_ERR, "ERROR: register_flag: invalid flag %.*s "
					"position(%d)\n", len, name, pos);
			return -4;
		}
		if (registered_flags[pos]!=0){
			LOG(L_WARN, "WARNING: register_flag:  %.*s:  flag %d already in "
					"use under another name\n", len, name, pos);
			/* continue */
		}
	}else{
		/* alloc an empty flag */
		last_flag=crt_flag+(MAX_FLAG+1);
		for (; crt_flag!=last_flag; crt_flag++){
			r=crt_flag%(MAX_FLAG+1);
			if (registered_flags[r]==0){
				pos=r;
				break;
			}
		}
		if (pos==-1){
			LOG(L_ERR, "ERROR: register_flag: could not register %.*s"
					" - too many flags\n", len, name);
			return -5;
		}
	}
	registered_flags[pos]++;
	
	e=pkg_malloc(sizeof(struct flag_entry));
	if (e==0){
		LOG(L_ERR, "ERROR: register_flag: memory allocation failure\n");
		return -3;
	}
	e->name.s=name;
	e->name.len=len;
	e->no=pos;
	clist_insert(&name2flags[h], e, next, prev);
	return pos;
}
Beispiel #15
0
/*
 * Hash function
 */
static inline int hash_func(udomain_t* _d, unsigned char* _s, int _l)
{
#ifdef HASH_STRING_OPTIMIZE
	return get_hash1_raw((char*)_s, _l) % UDOMAIN_HASH_SIZE;
#else
	return get_hash1_raw2((char*)_s, _l) % UDOMAIN_HASH_SIZE;
#endif
}
Beispiel #16
0
sr_xavp_t *xavp_extract(str *name, sr_xavp_t **list)
{
	sr_xavp_t *avp = 0;
	sr_xavp_t *foo;
	sr_xavp_t *prv = 0;
	unsigned int id;

	if(name==NULL || name->s==NULL) {
		if(list!=NULL) {
			avp = *list;
			if(avp!=NULL) {
				*list = avp->next;
				avp->next = NULL;
			}
		} else {
			avp = *_xavp_list_crt;
			if(avp!=NULL) {
				*_xavp_list_crt = avp->next;
				avp->next = NULL;
			}
		}
		
		return avp;
	}

	id = get_hash1_raw(name->s, name->len);
	if(list!=NULL)
		avp = *list;
	else
		avp = *_xavp_list_crt;
	while(avp)
	{
		foo = avp;
		avp=avp->next;
		if(foo->id==id && foo->name.len==name->len
				&& strncmp(foo->name.s, name->s, name->len)==0)
		{
			if(prv!=NULL)
				prv->next=foo->next;
			else if(list!=NULL)
				*list = foo->next;
			else
				*_xavp_list_crt = foo->next;
			foo->next = NULL;
			return foo;
		} else {
			prv = foo;
		}
	}
	return NULL;
}
Beispiel #17
0
int redisc_add_server(char *spec)
{
	param_t *pit=NULL;
	param_hooks_t phooks;
	redisc_server_t *rsrv=NULL;
	str s;

	s.s = spec;
	s.len = strlen(spec);
	if(s.s[s.len-1]==';')
		s.len--;
	if (parse_params(&s, CLASS_ANY, &phooks, &pit)<0)
	{
		LM_ERR("failed parsing params value\n");
		goto error;
	}
	rsrv = (redisc_server_t*)pkg_malloc(sizeof(redisc_server_t));
	if(rsrv==NULL)
	{
		LM_ERR("no more pkg\n");
		goto error;
	}
	memset(rsrv, 0, sizeof(redisc_server_t));
	rsrv->attrs = pit;
	for (pit = rsrv->attrs; pit; pit=pit->next)
	{
		if(pit->name.len==4 && strncmp(pit->name.s, "name", 4)==0) {
			rsrv->sname = &pit->body;
			rsrv->hname = get_hash1_raw(rsrv->sname->s, rsrv->sname->len);
			break;
		}
	}
	if(rsrv->sname==NULL)
	{
		LM_ERR("no server name\n");
		goto error;
	}
	rsrv->next = _redisc_srv_list;
	_redisc_srv_list = rsrv;

	return 0;
error:
	if(pit!=NULL)
		free_params(pit);
	if(rsrv!=NULL)
		pkg_free(rsrv);
	return -1;
}
Beispiel #18
0
sr_phonenum_record_t *sr_phonenum_get_record(str *name)
{
	sr_phonenum_item_t *it = NULL;
	unsigned int hashid = 0;

	hashid = get_hash1_raw(name->s, name->len);

	it = _sr_phonenum_list;
	while(it != NULL) {
		if(it->hashid == hashid && it->pvclass.len == name->len
				&& strncmp(it->pvclass.s, name->s, name->len) == 0)
			return &it->r;
		it = it->next;
	}
	return NULL;
}
Beispiel #19
0
redisc_server_t *redisc_get_server(str *name)
{
	redisc_server_t *rsrv=NULL;
	unsigned int hname;

	hname = get_hash1_raw(name->s, name->len);
	rsrv=_redisc_srv_list;
	while(rsrv!=NULL)
	{
		if(rsrv->hname==hname && rsrv->sname->len==name->len
				&& strncmp(rsrv->sname->s, name->s, name->len)==0)
			return rsrv;
		rsrv=rsrv->next;
	}
	return NULL;
}
Beispiel #20
0
pv_spec_t* pv_cache_add(str *name)
{
	pv_cache_t *pvn;
	unsigned int pvid;
	char *p;

	if(_pv_cache_set==0)
	{
		LM_DBG("PV cache not initialized, doing it now\n");
		pv_init_cache();
	}
	if(_pv_cache_counter+1>=cfg_get(core, core_cfg, pv_cache_limit)) {
		if(_pv_cache_counter+1==cfg_get(core, core_cfg, pv_cache_limit)) {
			LM_WARN("pv cache limit is going to be exceeded"
					" - pkg memory may get filled with pv declarations\n");
		} else {
			if(cfg_get(core, core_cfg, pv_cache_action)==1) {
				pv_cache_drop();
			}
		}
	}
	pvid = get_hash1_raw(name->s, name->len);
	pvn = (pv_cache_t*)pkg_malloc(sizeof(pv_cache_t) + name->len + 1);
	if(pvn==0)
	{
		LM_ERR("no more memory\n");
		return NULL;
	}
	memset(pvn, 0, sizeof(pv_cache_t) + name->len + 1);
	pvn->pvname.len = name->len;
	pvn->pvname.s = (char*)pvn + sizeof(pv_cache_t);
	memcpy(pvn->pvname.s, name->s, name->len);
	p = pv_parse_spec(&pvn->pvname, &pvn->spec);

	if(p==NULL)
	{
		pkg_free(pvn);
		return NULL;
	}
	pvn->pvid = pvid;
	pvn->next = _pv_cache[pvid%PV_CACHE_SIZE];
	_pv_cache[pvid%PV_CACHE_SIZE] = pvn;

	LM_DBG("pvar [%.*s] added in cache\n", name->len, name->s);
	return &pvn->spec;
}
Beispiel #21
0
/* Remove xavps
 * idx: <0 remove all xavps with the same name
 *      >=0 remove only the specified index xavp
 * Returns number of xavps that were deleted
 */
static int xavp_rm_internal(str *name, sr_xavp_t **head, int idx)
{
	sr_xavp_t *avp;
	sr_xavp_t *foo;
	sr_xavp_t *prv=0;
	unsigned int id;
	int n=0;
	int count=0;

	if(name==NULL || name->s==NULL)
		return 0;

	id = get_hash1_raw(name->s, name->len);
	if(head!=NULL)
		avp = *head;
	else
		avp = *_xavp_list_crt;
	while(avp)
	{
		foo = avp;
		avp=avp->next;
		if(foo->id==id && foo->name.len==name->len
				&& strncmp(foo->name.s, name->s, name->len)==0)
		{
			if(idx<0 || idx==n)
			{
				if(prv!=NULL)
					prv->next=foo->next;
				else if(head!=NULL)
					*head = foo->next;
				else
					*_xavp_list_crt = foo->next;
				xavp_free(foo);
				if(idx>=0)
					return 1;
				count++;
			}
			n++;
		} else {
			prv = foo;
		}
	}
	return count;
}
Beispiel #22
0
redisc_server_t *redisc_get_server(str *name)
{
	redisc_server_t *rsrv=NULL;
	unsigned int hname;

	hname = get_hash1_raw(name->s, name->len);
	LM_DBG("Hash %u (%.*s)\n", hname, name->len, name->s);
	rsrv=_redisc_srv_list;
	while(rsrv!=NULL)
	{
		LM_DBG("Entry %u (%.*s)\n", rsrv->hname, rsrv->sname->len, rsrv->sname->s);
		if(rsrv->hname==hname && rsrv->sname->len==name->len
				&& strncmp(rsrv->sname->s, name->s, name->len)==0)
			return rsrv;
		rsrv=rsrv->next;
	}
	LM_DBG("No entry found.\n");
	return NULL;
}
Beispiel #23
0
int dbg_assign_add(str *name, pv_spec_t *spec)
{
	dbg_pvcache_t *pvn, *last, *next;
	unsigned int pvid;

	if(name==NULL||spec==NULL)
		return -1;

	if(_dbg_pvcache==NULL)
		return -1;

	pvid = get_hash1_raw((char *)&spec, sizeof(pv_spec_t*));
	pvn = (dbg_pvcache_t*)pkg_malloc(sizeof(dbg_pvcache_t));
	if(pvn==NULL)
	{
		LM_ERR("no more memory\n");
		return -1;
	}
	memset(pvn, 0, sizeof(dbg_pvcache_t));
	pvn->pvname = name;
	pvn->spec = spec;
	next = _dbg_pvcache[pvid%DBG_PVCACHE_SIZE];
	if(next==NULL)
	{
		_dbg_pvcache[pvid%DBG_PVCACHE_SIZE] = pvn;
	}
	else
	{
		while(next)
		{
			last = next;
			next = next->next;
		}
		last->next = pvn;
	}
	return 0;
}
Beispiel #24
0
pv_spec_t* pv_cache_add(str *name)
{
	pv_cache_t *pvn;
	unsigned int pvid;
	char *p;

	if(_pv_cache_set==0)
	{
		LM_DBG("PV cache not initialized, doing it now\n");
		pv_init_cache();
	}
	pvid = get_hash1_raw(name->s, name->len);
	pvn = (pv_cache_t*)pkg_malloc(sizeof(pv_cache_t) + name->len + 1);
	if(pvn==0)
	{
		LM_ERR("no more memory\n");
		return NULL;
	}
	memset(pvn, 0, sizeof(pv_item_t) + name->len + 1);
	p = pv_parse_spec(name, &pvn->spec);

	if(p==NULL)
	{
		pkg_free(pvn);
		return NULL;
	}
	pvn->pvname.len = name->len;
	pvn->pvname.s = (char*)pvn + sizeof(pv_cache_t);
	memcpy(pvn->pvname.s, name->s, name->len);
	pvn->pvid = pvid;
	pvn->next = _pv_cache[pvid%PV_CACHE_SIZE];
	_pv_cache[pvid%PV_CACHE_SIZE] = pvn;

	LM_DBG("pvar [%.*s] added in cache\n", name->len, name->s);
	return &pvn->spec;
}
Beispiel #25
0
redisc_reply_t *redisc_get_reply(str *name)
{
	redisc_reply_t *rpl;
	unsigned int hid;

	hid = get_hash1_raw(name->s, name->len);

	for(rpl=_redisc_rpl_list; rpl; rpl=rpl->next) {
		if(rpl->hname==hid && rpl->rname.len==name->len
				&& strncmp(rpl->rname.s, name->s, name->len)==0)
			return rpl;
	}
	/* not found - add a new one */

	rpl = (redisc_reply_t*)pkg_malloc(sizeof(redisc_reply_t));
	if(rpl==NULL)
	{
		LM_ERR("no more pkg\n");
		return NULL;
	}
	memset(rpl, 0, sizeof(redisc_reply_t));
	rpl->hname = hid;
	rpl->rname.s = (char*)pkg_malloc(name->len+1);
	if(rpl->rname.s==NULL)
	{
		LM_ERR("no more pkg.\n");
		pkg_free(rpl);
		return NULL;
	}
	strncpy(rpl->rname.s, name->s, name->len);
	rpl->rname.len = name->len;
	rpl->rname.s[name->len] = '\0';
	rpl->next = _redisc_rpl_list;
	_redisc_rpl_list = rpl;
	return rpl;
}
Beispiel #26
0
pv_export_t* pv_lookup_spec_name(str *pvname, pv_spec_p e)
{
	pv_item_t *pvi;
	unsigned int pvid;

	if(pvname==0 || e==0)
	{
		LM_ERR("bad parameters\n");
		return NULL;
	}

	/* search in PV table */
	// pvid = get_hash1_raw(pvname->s, pvname->len);
	pvid = get_hash1_raw(pvname->s, pvname->len);
	pvi = _pv_table[pvid%PV_TABLE_SIZE];
	while(pvi)
	{
		if(pvi->pvid > pvid)
			break;

		if(pvi->pvid==pvid && pvi->pve.name.len==pvname->len
			&& memcmp(pvi->pve.name.s, pvname->s, pvname->len)==0)
		{
			/*LM_DBG("found [%.*s] [%d]\n", pvname->len, pvname->s,
					_pv_names_table[i].type);*/
			/* copy data from table to spec */
			e->type = pvi->pve.type;
			e->getf = pvi->pve.getf;
			e->setf = pvi->pve.setf;
			return &(pvi->pve);
		}
		pvi = pvi->next;
	}

	return NULL;
}
Beispiel #27
0
/*!
 * \brief SQL REPLACE implementation
 * \param _h structure representing database connection
 * \param _k key names
 * \param _v values of the keys
 * \param _n number of key=value pairs
 * \param _un number of keys to build the unique key, starting from first
 * \param _m mode - first update, then insert, or first insert, then update
 * \return 0 on success, negative on failure
 */
int db_postgres_replace(const db1_con_t* _h, const db_key_t* _k,
		const db_val_t* _v, const int _n, const int _un, const int _m)
{
	unsigned int pos = 0;
	int i;

	if(_un > _n)
	{
		LM_ERR("number of columns for unique key is too high\n");
		return -1;
	}

	if(_un > 0)
	{
		for(i=0; i<_un; i++)
		{
			if(!VAL_NULL(&_v[i]))
			{
				switch(VAL_TYPE(&_v[i]))
				{
					case DB1_INT:
						pos += VAL_UINT(&_v[i]);
						break;
					case DB1_STR:
						pos += get_hash1_raw((VAL_STR(&_v[i])).s,
									(VAL_STR(&_v[i])).len);
						break;
					case DB1_STRING:
						pos += get_hash1_raw(VAL_STRING(&_v[i]),
									strlen(VAL_STRING(&_v[i])));
						break;
					default:
						break;
				}
			}
		}
		pos &= (_pg_lock_size-1);
		lock_set_get(_pg_lock_set, pos);
		if(db_postgres_update(_h, _k, 0, _v, _k + _un,
						_v + _un, _un, _n -_un)< 0)
		{
			LM_ERR("update failed\n");
			lock_set_release(_pg_lock_set, pos);
			return -1;
		}

		if (db_postgres_affected_rows(_h) <= 0)
		{
			if(db_postgres_insert(_h, _k, _v, _n)< 0)
			{
				LM_ERR("insert failed\n");
				lock_set_release(_pg_lock_set, pos);
				return -1;
			}
			LM_DBG("inserted new record in database table\n");
		} else {
			LM_DBG("updated record in database table\n");
		}
		lock_set_release(_pg_lock_set, pos);
	} else {
		if(db_postgres_insert(_h, _k, _v, _n)< 0)
		{
			LM_ERR("direct insert failed\n");
			return -1;
		}
		LM_DBG("directly inserted new record in database table\n");
	}
	return 0;
}
Beispiel #28
0
int tr_table_add(tr_export_t *e)
{
	tr_item_t *tri = NULL;
	tr_item_t *trj = NULL;
	tr_item_t *trn = NULL;
	int found;
	unsigned int trid;

	if(e==NULL || e->tclass.s==NULL)
	{
		LM_ERR("invalid parameters\n");
		return -1;
	}

	if(_tr_table_set==0)
	{
		LM_DBG("TR table not initialized, doing it now\n");
		tr_init_table();
	}

	found = 0;
	// trid = get_hash1_raw(e->tclass.s, e->tclass.len);
	trid = get_hash1_raw(e->tclass.s, e->tclass.len);

	tri = _tr_table[trid%TR_TABLE_SIZE];
	while(tri)
	{
		if(tri->tre.tclass.len==e->tclass.len)
		{
			found = strncmp(tri->tre.tclass.s, e->tclass.s, e->tclass.len);
			if(found==0)
			{
				LM_ERR("TR class [%.*s] already exists\n", e->tclass.len,
						e->tclass.s);
				return -1;
			}
		}
		trj = tri;
		tri = tri->next;
	}

	trn = (tr_item_t*)pkg_malloc(sizeof(tr_item_t));
	if(trn==0)
	{
		LM_ERR("no more memory\n");
		return -1;
	}
	memset(trn, 0, sizeof(tr_item_t));
	memcpy(&(trn->tre), e, sizeof(tr_export_t));
	trn->trid = trid;

	//LM_DBG("TR class [%.*s] added to entry [%d]\n", e->tclass.len,
	//					e->tclass.s, trid%TR_TABLE_SIZE);
	if(trj==0)
	{
		trn->next = _tr_table[trid%TR_TABLE_SIZE];
		_tr_table[trid%TR_TABLE_SIZE] = trn;
		goto done;
	}
	trn->next = trj->next;
	trj->next = trn;

done:
	return 0;
}
Beispiel #29
0
int pv_table_add(pv_export_t *e)
{
	char *p;
	str  *in;
	pv_item_t *pvi = NULL;
	pv_item_t *pvj = NULL;
	pv_item_t *pvn = NULL;
	int found;
	unsigned int pvid;

	if(e==NULL || e->name.s==NULL || e->getf==NULL || e->type==PVT_NONE)
	{
		LM_ERR("invalid parameters\n");
		return -1;
	}

	if(_pv_table_set==0)
	{
		LM_DBG("PV table not initialized, doing it now\n");
		pv_init_table();
	}
	in = &(e->name);
	p = in->s;
	while(is_in_str(p,in) && is_pv_valid_char(*p))
		p++;
	if(is_in_str(p,in))
	{
		LM_ERR("invalid char [%c] in [%.*s]\n", *p, in->len, in->s);
		return -1;
	}
	found = 0;
	//pvid = get_hash1_raw(in->s, in->len);
	pvid = get_hash1_raw(in->s, in->len);

	pvi = _pv_table[pvid%PV_TABLE_SIZE];
	while(pvi)
	{
		if(pvi->pvid > pvid)
			break;
		if(pvi->pve.name.len==in->len)
		{
			found = strncmp(pvi->pve.name.s, in->s, in->len);

			if(found==0)
			{
				LM_ERR("pvar [%.*s] already exists\n", in->len, in->s);
				return -1;
			}
		}
		pvj = pvi;
		pvi = pvi->next;
	}

	pvn = (pv_item_t*)pkg_malloc(sizeof(pv_item_t));
	if(pvn==0)
	{
		LM_ERR("no more memory\n");
		return -1;
	}
	memset(pvn, 0, sizeof(pv_item_t));
	memcpy(&(pvn->pve), e, sizeof(pv_export_t));
	pvn->pvid = pvid;

	if(pvj==0)
	{
		pvn->next = _pv_table[pvid%PV_TABLE_SIZE];
		_pv_table[pvid%PV_TABLE_SIZE] = pvn;
		goto done;
	}
	pvn->next = pvj->next;
	pvj->next = pvn;

done:
	return 0;
}
Beispiel #30
0
/* inserts a callid item to table, and removes the least item if the table is full */
int proc_cid(ttable *ptable,
			 str *scid,
			 str *sftag,
			 unsigned int ucseq,
			 time_t ivalidbefore)
{
	tcid_item *pshmcid, *pcid_item;
	tdlg_item *pshmdlg, *pdlg_item, *pdlg_item_prev;
	unsigned int uhash;

	/* we suppose that this SIP request is not replayed so it doesn't exist in
	   the table so we prepare to insert */
	if (!(pshmdlg=(tdlg_item *)shm_malloc(sizeof(*pshmdlg)))) {
		LOG(L_ERR, "AUTH_IDENTITY:addcid2table: No enough shared memory\n");
		return -1;
	}
	memset(pshmdlg, 0, sizeof(*pshmdlg));
	if (str_duplicate(&pshmdlg->sftag, sftag))
		return -2;
	pshmdlg->ucseq=ucseq;


	/* we're looking for this call-id item if exists */
	uhash=get_hash1_raw(scid->s, scid->len) & (CALLID_TABLE_ENTRIES-1);

	lock_element(&ptable->entries[uhash]);

	pcid_item = search_item_in_table_unsafe(ptable,
											(const void *)scid, /* Call-id is the key */
											uhash);
	/* we've found one call-id so we're looking for the required SIP request */
	if (pcid_item) {
		for (pdlg_item=pcid_item->pdlgs, pdlg_item_prev=NULL;
		     pdlg_item;
			 pdlg_item=pdlg_item->pnext) {
			if (pdlg_item->sftag.len==sftag->len
				&& !memcmp(pdlg_item->sftag.s, sftag->s, sftag->len)) {
				/* we found this call with this from tag */
				if (pdlg_item->ucseq>=ucseq) {
					/* we've found this or older request in the table!
					   this call is replayed! */
					release_element(&ptable->entries[uhash]);

					shm_free(pshmdlg->sftag.s);
					shm_free(pshmdlg);
					return AUTH_FOUND;
				} else {
					/* this is another later request whithin this dialog so we
					   update the saved cseq */
					pdlg_item->ucseq=ucseq;
					release_element(&ptable->entries[uhash]);

					shm_free(pshmdlg->sftag.s);
					shm_free(pshmdlg);
					return 0;
				}
			}
			/* we save the previous dialog item in order to append a new item more easily */
			pdlg_item_prev ?
				(pdlg_item_prev=pdlg_item_prev->pnext) :
				(pdlg_item_prev=pdlg_item);
		}
		/* we append this to item dialogs*/
		pdlg_item_prev->pnext=pshmdlg;
		/* this is the latest request; we hold all request concerned this
		   call-id until the latest request is valid */
		pcid_item->ivalidbefore=ivalidbefore;
	}

	release_element(&ptable->entries[uhash]);

	if (!pcid_item) {
		/* this is the first request with this call-id */
		if (!(pshmcid=(tcid_item *)shm_malloc(sizeof(*pshmcid)))) {
			LOG(L_ERR, "AUTH_IDENTITY:addcid2table: No enough shared memory\n");
			return -4;
		}
		memset(pshmcid, 0, sizeof(*pshmcid));
		if (str_duplicate(&pshmcid->scid, scid)) {
			return -5;
		}
		pshmcid->ivalidbefore=ivalidbefore;
		pshmcid->pdlgs=pshmdlg;
		if (insert_into_table(ptable, (void*)pshmcid, uhash))
			return -6;
	}

	return 0;
}