⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdt.c

📁 用来作为linux中SIP SERVER,完成VOIP网络电话中服务器的功能
💻 C
📖 第 1 页 / 共 2 页
字号:
		}	/* if the user part begin with the prefix for PSTN users, extract the code*/	if (msg->parsed_uri.user.len<=0)	{		DBG("PDT: prefix2domain: user part of the message was empty\n");		return 1;	}		if(prefix_len>0 && strncasecmp(prefix, msg->parsed_uri.user.s, prefix_len)!=0)	{		DBG("PDT: prefix2domain: PSTN prefix did not matched\n");		return 1;				}	i=0;	code=0;	digit = msg->parsed_uri.user.s[prefix_len+i]-'0';	while (digit != code_terminator)	{		if (digit<0 || digit>9)		{					DBG("PDT: prefix2domain: domain_code not well formed\n");			return -1;		}				if(MAX_CODE_10<code || (MAX_CODE_10==code && MAX_CODE-MAX_CODE_R<=digit))		{			DBG("PDT: prefix2domain: domain_code not well formed\n");			return -1;			}		code=code*10+digit;		i++;		digit = msg->parsed_uri.user.s[prefix_len+i]-'0';	}		    	/* find the domain that corresponds to that code */	if(!(host_port=get_domain_from_hash(hash->chash, hash->hash_size, code)))	{		LOG(L_ERR, "PDT: get_domain_from_hash: required " 					"code %d is not allocated yet\n", code);		return -1;	}		/* update the new uri */	if(update_new_uri(msg, prefix_len+i+1, host_port)<0)	{		DBG("PDT: prefix2domain: new_uri cannot be updated\n");		return -1;	}	return 1;}/* change the uri according to translation of the prefix */int update_new_uri(struct sip_msg *msg, int code_len, char* host_port){	char *tmp;	int uri_len = 0, user_len = 0; 		/* flag to show that ruri is not parsed */	msg->parsed_uri_ok = 0;	/* compute the new uri length */	uri_len = 4/*sip:*/ + msg->parsed_uri.user.len-code_len +			( msg->parsed_uri.passwd.len ? msg->parsed_uri.passwd.len + 1:0 ) + 			strlen(host_port) + 1/*@*/ +			(msg->parsed_uri.params.len ? msg->parsed_uri.params.len + 1:0 ) +			(msg->parsed_uri.headers.len ? msg->parsed_uri.headers.len + 1:0 );		if (uri_len > MAX_URI_SIZE) 	{		LOG(L_ERR, "PDT: update_new_uri(): uri is too long\n");		return -1;	}	/* space for the new uri */	tmp = (char*)pkg_malloc(uri_len+1);	if(tmp == NULL)		{		LOG(L_ERR, "PDT: update_new_uri: error allocating space\n");		return -1;	}	/* construct the new uri */	strcpy(tmp, "sip:");		/* add user part */	user_len = msg->parsed_uri.user.len-code_len;	strncat(tmp, msg->parsed_uri.user.s+code_len, user_len);	/* add password, if that exists */	if(msg->parsed_uri.passwd.s && msg->parsed_uri.passwd.len > 0)	{		strcat(tmp, ":");		strncat(tmp, msg->parsed_uri.passwd.s, 				msg->parsed_uri.passwd.len);	}	strcat(tmp,"@");		/* add host(and port) part of the uri */	strcat(tmp, host_port);	if(msg->parsed_uri.params.s && msg->parsed_uri.params.len > 0)	{		strcat(tmp, ";");		strncat(tmp, msg->parsed_uri.params.s, msg->parsed_uri.params.len);	}		if(msg->parsed_uri.headers.s && msg->parsed_uri.headers.len > 0)	{		strcat(tmp, "?");		strncat(tmp, msg->parsed_uri.headers.s, msg->parsed_uri.headers.len);	}		/* free space of the old new_uri */	if(msg->new_uri.s)	{		pkg_free(msg->new_uri.s);		msg->new_uri.len = 0;	}		/* setup the new uri */	msg->new_uri.s = tmp;	msg->new_uri.len = uri_len;	// here to clear		DBG("PDT: update_new_uri: len=%d uri=%.*s\n", msg->new_uri.len, 			msg->new_uri.len, msg->new_uri.s);		return 0;}static void mod_destroy(void){	DBG("PDT: mod_destroy : Cleaning up\n");	if (hash)		free_double_hash(hash);	if (db_con && pdt_dbf.close)		pdt_dbf.close(db_con);	if (next_code)		shm_free(next_code);	lock_destroy(&l);}/* 	Fifo command example: 	":get_domaincode:[response_file]\n	 domain_name\n	 authorization_to_register_domains\n	 \n 	"  */int get_domainprefix(FILE *stream, char *response_file){	db_key_t db_keys[NR_KEYS];	db_val_t db_vals[NR_KEYS];	db_op_t  db_ops[NR_KEYS] = {OP_EQ, OP_EQ};	code_t code;	dc_t* cell; 		char domain_name[256];	str sdomain;	char authorization[10];	str sauth;	int authorized=0;			/* read a line -the domain name parameter- from the fifo */	sdomain.s = domain_name;	if(!read_line(sdomain.s, 255, stream, &sdomain.len) || sdomain.len==0)		{		LOG(L_ERR, "PDT: get_domaincode: could not read from fifo\n");		fifo_reply(response_file, "400 |get_domaincode: could not " 						"read from fifo\n");		return 1;	}	domain_name[sdomain.len] = '\0';	/* read a line -the authorization to register new domains- from the fifo */	sauth.s = authorization;	if(!read_line(sauth.s, 3, stream, &sauth.len) || sauth.len==0)	{			LOG(L_ERR, "PDT: get_domaincode: could not read from fifo\n");		fifo_reply(response_file, "400 |get_domaincode: could not "						"read from fifo\n");		return 1;	}	/* see what kind of user we have */	authorized = sauth.s[0]-'0';	lock_get(&l);	/* search the domain in the hashtable */	cell = get_code_from_hash(hash->dhash, hash->hash_size, domain_name);		/* the domain is registered */	if(cell)	{		lock_release(&l);					/* domain already in the database */			fifo_reply(response_file, "201 |Domain name= %.*s"				"Domain code= %d%d\n",				sdomain.len, sdomain.s, cell->code, code_terminator);		return 0;			}		/* domain not registered yet */	/* user not authorized to register new domains */		if(!authorized)	{		lock_release(&l);		fifo_reply(response_file, "203 |Domain name not registered yet\n");		return 0;	}	code = *next_code;	*next_code = apply_correction(code+1);			/* prepare for insertion into database */	db_keys[0] = DB_KEY_CODE;	db_keys[1] = DB_KEY_NAME;	db_vals[0].type = DB_INT;	db_vals[0].nul = 0;	db_vals[0].val.int_val = code;	db_vals[1].type = DB_STR;	db_vals[1].nul = 0;	db_vals[1].val.str_val.s = sdomain.s;	db_vals[1].val.str_val.len = sdomain.len;	DBG("%d %.*s\n", code, sdomain.len, sdomain.s);				/* insert a new domain into database */	if(pdt_dbf.insert(db_con, db_keys, db_vals, NR_KEYS)<0)	{		/* next available code is still code */		*next_code = code;		lock_release(&l);		LOG(L_ERR, "PDT: get_domaincode: error storing a"				" new domain\n");		fifo_reply(response_file, "204 |Cannot register the new domain in a"					" consistent way\n");		return -1;	}		/* insert the new domain into hashtables, too */	cell = new_cell(sdomain.s, code);	if(add_to_double_hash(hash, cell)<0)		goto error;			lock_release(&l);	/* user authorized to register new domains */	fifo_reply(response_file, "202 |Domain name= %.*s"		"	New domain code=  %d%d\n",		sdomain.len, sdomain.s, code, code_terminator);	return 0;	error:	/* next available code is still code */	*next_code = code;	/* delete from database */	if(pdt_dbf.delete(db_con, db_keys, db_ops, db_vals, NR_KEYS)<0)		LOG(L_ERR,"PDT: get_domaincode: database/share-memory are inconsistent\n");	lock_release(&l);		return -1;}static int get_domainprefix_unixsock(str* msg){	db_key_t db_keys[NR_KEYS];	db_val_t db_vals[NR_KEYS];	db_op_t  db_ops[NR_KEYS] = {OP_EQ, OP_EQ};	code_t code;	dc_t* cell; 	str sdomain, sauth;	int authorized=0;			/* read a line -the domain name parameter- from the fifo */	if(unixsock_read_line(&sdomain, msg) != 0)		{		unixsock_reply_asciiz("400 Domain expected\n");		goto send_err;	}	/* read a line -the authorization to register new domains- from the fifo */	if(unixsock_read_line(&sauth, msg) != 0)	{			unixsock_reply_asciiz("400 Authorization expected\n");		goto send_err;	}	sdomain.s[sdomain.len] = '\0';	/* see what kind of user we have */	authorized = sauth.s[0]-'0';	lock_get(&l);	/* search the domain in the hashtable */	cell = get_code_from_hash(hash->dhash, hash->hash_size, sdomain.s);		/* the domain is registered */	if(cell)	{		lock_release(&l);					/* domain already in the database */		unixsock_reply_printf("201 Domain name=%.*s Domain code=%d%d\n",				      sdomain.len, ZSW(sdomain.s), cell->code, code_terminator);		unixsock_reply_send();		return 0;			}		/* domain not registered yet */	/* user not authorized to register new domains */		if(!authorized)	{		lock_release(&l);		unixsock_reply_asciiz("203 Domain name not registered yet\n");		unixsock_reply_send();		return 0;	}	code = *next_code;	*next_code = apply_correction(code+1);			/* prepare for insertion into database */	db_keys[0] = DB_KEY_CODE;	db_keys[1] = DB_KEY_NAME;	db_vals[0].type = DB_INT;	db_vals[0].nul = 0;	db_vals[0].val.int_val = code;	db_vals[1].type = DB_STR;	db_vals[1].nul = 0;	db_vals[1].val.str_val.s = sdomain.s;	db_vals[1].val.str_val.len = sdomain.len;	DBG("%d %.*s\n", code, sdomain.len, sdomain.s);				/* insert a new domain into database */	if(pdt_dbf.insert(db_con, db_keys, db_vals, NR_KEYS)<0)	{		/* next available code is still code */		*next_code = code;		lock_release(&l);		LOG(L_ERR, "PDT: get_domaincode: error storing a"				" new domain\n");		unixsock_reply_asciiz("204 Cannot register the new domain in a consistent way\n");		unixsock_reply_send();		return -1;	}		/* insert the new domain into hashtables, too */	cell = new_cell(sdomain.s, code);	if(add_to_double_hash(hash, cell)<0)		goto error;			lock_release(&l);	/* user authorized to register new domains */	unixsock_reply_printf("202 Domain name=%.*s New domain code=%d%d\n",			      sdomain.len, ZSW(sdomain.s), code, code_terminator);	unixsock_reply_send();	return 0; error:	/* next available code is still code */	*next_code = code;	/* delete from database */	if(pdt_dbf.delete(db_con, db_keys, db_ops, db_vals, NR_KEYS)<0)		LOG(L_ERR,"PDT: get_domaincode: database/share-memory are inconsistent\n");	lock_release(&l);	unixsock_reply_asciiz("500 Database/shared-memory are inconsistent\n");send_err:	unixsock_reply_send();	return -1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -