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

📄 buffer.c

📁 sip代理服务器源码
💻 C
字号:
/***********************************************************************\	SIP Server		Date		Ver		Author		MemoRandom	Jul 3,2002	1.0		Hiroaki Hata	Created	(C) 2002 All Copyrights reserved. *************************************************************************/#include <sys/types.h>#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <string.h>#include <netinet/in.h>#include <netdb.h>#include "udp.h"#include "sipd.h"#include "parser.h"#define	DEBUG if(debug==1){#define DBGstatic void params_to_asc(URIPARAM *param,char *buff);static int check_ip(char *p){	for(;*p!='\0';p++){		if(*p>='0' && *p<='9') continue;		if(*p=='.') continue;		return NG;	}	return OK;}int MakeURItoASC(URI *uri,char *buff,int type){	//type 1 崇柑あり	//type 2 猖乖あり	char	line[CLEN];	int	crlf=0;	int	brace=0;	char	header[8];		crlf= type & CRLF;	brace = type & BRACE;		if(brace != 0){		strcpy(header,"<sip:");		}else{		strcpy(header,"sip:");		}	if(buff==NULL) {		logging(2,"param error MakeURItoASC");		return NG;	}	*buff='\0';		if(uri->username[0]!='\0'){		if(uri->port!=0){			sprintf(line,"%s%s@%s:%d", 				header,uri->username, uri->host,uri->port);		}else{			sprintf(line,"%s%s@%s", 				header,uri->username, uri->host);		}	}else{		if(uri->port!=0){			sprintf(line,"%s%s:%d", 				header, uri->host,uri->port);		}else{			sprintf(line,"%s%s", 				header, uri->host);		}	}	strcat(buff,line);	*line='\0';	params_to_asc(&uri->param,line);	strcat(buff,line);	if(brace!=0) strcat(buff,">");	strcat(buff,uri->aux);	if(crlf!=0) strcat(buff,"\r\n");	return OK;}int MakeSendBuffer(MESSAGE *mes,char *buff){		char	line[CLEN];	VIA	*via;	URI	*p;	GENERAL	*g;	int i;			if(buff==NULL) return -1;	*buff='\0';		//--------------------------------------------------First Line	if(mes->start.type==REQUEST){		if(mes->start.requri.username[0]=='\0'){			sprintf(line,"%s sip:%s",				mes->start.method,				mes->start.requri.host);			strcat(buff,line);		}else{			sprintf(line,"%s sip:%s@%s",				mes->start.method,				mes->start.requri.username,				mes->start.requri.host);			strcat(buff,line);		}		if(mes->start.requri.port>0){			sprintf(line,":%d",mes->start.requri.port);			strcat(buff,line);		}		*line='\0';		params_to_asc(&mes->start.requri.param,line);		strcat(buff,line);		strcat(buff," ");		strcat(buff,mes->start.proto);		strcat(buff,"\r\n");	}else{		sprintf(line,"%s\r\n",mes->start.response);		strcat(buff,line);	}	//-------------------------------------------------Via Header	via=mes->header.via;	if(via==NULL){		logging(1,"no via entry(334)\n");		return NG;	}	i=0;	for(;via!=NULL;via=via->next){		sprintf(line,"Via: %s/%s/%s %s:%d",			via->proto,			via->ver,			via->trans,			via->host,			via->port);		strcat(buff,line);		*line='\0';		params_to_asc(&via->param,line);		strcat(buff,line);		strcat(buff,"\r\n");	}	//-----------------------------------------------------CALL-ID	sprintf(line,"Call-ID: %s\r\n",mes->header.callid);	strcat(buff,line);	//-----------------------------------------------------CSEQ	if(*mes->header.cseq.method!='\0'){		sprintf(line,"CSeq: %d %s\r\n",			mes->header.cseq.seq,			mes->header.cseq.method);		strcat(buff,line);	}	//-------------------------------------------------------To	strcat(buff,"To: ");	MakeURItoASC(&mes->header.to,line,BRACE|CRLF);	strcat(buff,line);	//-------------------------------------------------------From	strcat(buff,"From: ");	MakeURItoASC(&mes->header.from,line,BRACE|CRLF);	strcat(buff,line);	//-------------------------------------------------Record-Route 	for( p=mes->header.recordroute;p!=NULL;p=p->next){		strcat(buff,"Record-Route: ");		MakeURItoASC(p,line,BRACE|CRLF);		strcat(buff,line);	}	//-------------------------------------------------Route 	for( p=mes->header.route;p!=NULL;p=p->next){		if(NG==check_ip(p->host)) break;		strcat(buff,"Route: ");		MakeURItoASC(p,line,BRACE|CRLF);		strcat(buff,line);	}	//------------------------------------------------- Contact	for( p=mes->header.contact;p!=NULL;p=p->next){		strcat(buff,"Contact: ");		if(mes->header.expires==0){			strcpy(p->aux,";expires=0");		}		MakeURItoASC(p,line,BRACE|CRLF);		strcat(buff,line);	}	//-------------------------------------------------Authorization	if(mes->header.authrz!=NULL){		strcat(buff,"Proxy-Authorization: Digest ");		sprintf(line,"username=\"%s\",\r\n",mes->header.authrz->username);		strcat(buff,line);		sprintf(line,"\trealm=\"%s\",\r\n",mes->header.authrz->realm);		strcat(buff,line);		sprintf(line,"\tnonce=\"%s\",\r\n",mes->header.authrz->nonce);		strcat(buff,line);		sprintf(line,"\turi=\"%s\",\r\n",mes->header.authrz->uri);		strcat(buff,line);		sprintf(line,"\tqop=%s,\r\n",mes->header.authrz->qop);//Hata 0530		strcat(buff,line);		sprintf(line,"\tnc=%s,\r\n",mes->header.authrz->nc);//Hata 0530		strcat(buff,line);		sprintf(line,"\tcnonce=\"%s\",\r\n",mes->header.authrz->cnonce);		strcat(buff,line);		sprintf(line,"\tresponse=\"%s\",\r\n",mes->header.authrz->response);		strcat(buff,line);		sprintf(line,"\topaque=\"%s\",\r\n",mes->header.authrz->opaque);		strcat(buff,line);/*		if(*mes->header.authrz->domain!='\0'){			sprintf(line,"domain=\"%s\",",mes->header.authrz->domain);			strcat(buff,line);		}		if(*mes->header.authrz->algorithm!='\0'){			sprintf(line,"algorithm=\"%s\",",mes->header.authrz->algorithm);			strcat(buff,line);		}		if(*mes->header.authrz->stale!='\0'){			sprintf(line,"stale=\"%s\",",mes->header.authrz->stale);			strcat(buff,line);		}		if(*mes->header.authrz->aux!='\0'){			strcat(buff,mes->header.authrz->aux);		}*/		//乖琐にカンマがあったら猴近		if(buff[strlen(buff)-3]==',') buff[strlen(buff)-3]='\0';		strcat(buff,"\r\n");	}	//-------------------------------------------------General Header	for(g=mes->header.general;g!=NULL;g=g->next){		if(*g->body){			if(strncmp(g->body,"Proxy-Auth",10)==0){				continue;			}			//			strcat(buff,g->body);			strcat(buff,"\r\n");		}	}		//--------------------------------------------------User Agent	if(*mes->header.userAgent!='\0'){		sprintf(line,"User-Agent: %s\r\n",mes->header.userAgent);		strcat(buff,line);	}	//----------------------------------------------------Expires	if(mes->header.expires>=0){		sprintf(line,"Expires: %d\r\n",mes->header.expires);		strcat(buff,line);	}	//-------------------------------------------------Content-Type	if(mes->header.contentType!='\0'&& 			mes->header.contentLength!=0){		sprintf(line,"Content-Type: %s\r\n",mes->header.contentType);		strcat(buff,line);	}	//----------------------------------------------------MaxForwards	if(mes->header.maxforwards>0){		sprintf(line,"Max-Forwards: %d\r\n",mes->header.maxforwards);		strcat(buff,line);	}	//-------------------------------------------------Content-Length	sprintf(line,"Content-Length: %d\r\n",mes->header.contentLength);	strcat(buff,line);	//-------------------------------------------------Contents	strcat(buff,"\r\n");	if(mes->contents!=NULL){		/*Modified Hata Aug 23 2003 */		//NULL矢机で姜わってなければ途纷なゴミが烧缅するバグ 		//strcat(buff,mes->contents);		strncat(buff,mes->contents,mes->header.contentLength);	}	return OK;	}static void params_to_asc(URIPARAM *param,char *buff){	char	line[CLEN];	if(param==NULL||buff==NULL) {		logging(2,"Invaid param params_to_asc)");		return ;	}	if(*param->tag){		sprintf(line,";tag=%s",param->tag);		strcat(buff,line);	}/*	if(*param->maddr){		sprintf(line,";maddr=%s",param->maddr);		strcat(buff,line);	}*/	if(*param->branch){		sprintf(line,";branch=%s",param->branch);		strcat(buff,line);	}	if(param->ttl){		sprintf(line,";ttl=%d",param->ttl);		strcat(buff,line);	}	if(*param->transport){		sprintf(line,";transport=%s",param->transport);		strcat(buff,line);	}	if(*param->user){		sprintf(line,";user=%s",param->user);		strcat(buff,line);	}	if(*param->method){		sprintf(line,";method=%s",param->method);		strcat(buff,line);	}	if(param->lr){		strcat(buff,";lr");	}	strcat(buff,param->aux);}

⌨️ 快捷键说明

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