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

📄 util.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 <stdlib.h>#include <string.h>#include <time.h>#include "sipd.h"#include "parser.h"#include "md5.h"#define	DEBUG if(debug==1){#define DEND	}#ifdef MAIN int debug=1;#endifchar *SkipChars(char *ptr,char c){	for(;*ptr==c;ptr++){	}	return ptr;}char *SeparateLex1(char *buff,char c,char *optr,int *plen){	char	*ptr;	int	i=0;	ptr=buff;	if(buff==NULL)return 0;	for(ptr=buff;;ptr++){		if(*ptr==c){			++ptr;			break;		}else if(*ptr=='\0'){			ptr=NULL;			break;		}		i++;	}	if(i==0){		*plen=0;	}else if( i < *plen){		memcpy(optr,buff,i);		optr[i]='\0';		*plen=i;	}else{		*plen=-1;	}	return ptr;}int SeparateLex(char *buff,char c,char **optr,int n){	int i=0;	char *sptr;	char *ptr;	char *eot;	sptr=ptr=SkipChars(buff,c);	if(*ptr=='\0'){		return 0;	}	for(;;ptr++){		if(*ptr=='\0'){			strcpy(optr[i],sptr);			return i++;		}		if(*ptr==c){			eot=ptr;			ptr=SkipChars(ptr,c);			*eot='\0';			strcpy(optr[i],sptr);			i++;			if(i==n||*ptr=='\0') return i;				sptr=ptr;		}	}}void free_message_buffer(MESSAGE *mes){	VIA	*v;	VIA	*vnext;	URI	*url;	URI	*urlnext;	GENERAL	*g;	GENERAL	*gnext;	//Free buffer for VIA chaining buffer	for(v=mes->header.via;v!=NULL;){		vnext=v->next;		free(v);		v=vnext;	} 	//Free buffer for Contact  chaining buffer	for(url=mes->header.contact;url!=NULL;){		urlnext=url->next;		free(url);		url=urlnext;	}	//Free buffer for Route  chaining buffer	for(url=mes->header.route;url!=NULL;){		urlnext=url->next;		free(url);		url=urlnext;	}	//Free buffer for Record-Route  chaining buffer	for(url=mes->header.recordroute;url!=NULL;){		urlnext=url->next;		free(url);		url=urlnext;	}	//Auth	if(mes->header.authtc!=NULL){		free(mes->header.authtc);		mes->header.authtc=NULL;	}	if(mes->header.authrz!=NULL){		free(mes->header.authrz);		mes->header.authrz=NULL;	}	//Free buffer for General  chaining buffer	for(g=mes->header.general;g!=NULL;){		gnext=g->next;		free(g);		g=gnext;	} 	//Contents	if(mes->contents!=NULL){		free(mes->contents);		mes->contents=NULL;	}	//Buffer	if(mes->buff!=NULL){		free(mes->buff);		mes->buff=NULL;	}	free(mes);}int CalcHash(MESSAGE *mes,unsigned char *hash){#define	BUFF_SIZ	9182	unsigned char	in_buff[BUFF_SIZ];	int	i;	char	seq[8];	if(mes==NULL || hash==NULL){		return NG;	}	in_buff[0]='\0';	strcpy(in_buff,mes->header.to.username);	strcat(in_buff,mes->header.to.host);	if(strlen(in_buff) + strlen(mes->header.from.username) > BUFF_SIZ)		return NG;	strcat(in_buff,mes->header.from.username);	if(strlen(in_buff) + strlen(mes->header.from.host) > BUFF_SIZ)		return NG;	strcat(in_buff,mes->header.from.host);	if(strlen(in_buff) + strlen(mes->header.from.tag) > BUFF_SIZ)		return NG;	strcat(in_buff,mes->header.from.tag);	if(strlen(in_buff) + strlen(mes->header.callid) > BUFF_SIZ)		return NG;	strcat(in_buff,mes->header.callid);	if(strlen(in_buff) + strlen(mes->header.cseq.method) > BUFF_SIZ)		return NG;	strcat(in_buff,mes->header.cseq.method);	sprintf(seq,"%d",mes->header.cseq.seq);	if(strlen(in_buff) + strlen(seq) > BUFF_SIZ)		return NG;	strcat(in_buff,seq);DEBUG	printf("HASH:%s\n",in_buff);DEND	md5_calc(hash,in_buff,strlen(in_buff));DEBUG	i=0;	printf("HASH:");	for(i=0;i<HASH_LEN;i++){		printf("%02X",hash[i]);	}	printf("\n");DEND	return OK;}static void CvtHex(char *hash_bin,char *hash_asc){	unsigned short i;	unsigned char	j;	for(i=0;i<HASH_LEN;i++){		j=(hash_bin[i]>>4)&0xf;		if(j<=9)			hash_asc[i*2]=(j+'0');		else			hash_asc[i*2]=(j+'a'-10);		j=hash_bin[i]&0xf;		if(j<=9)			hash_asc[i*2+1]=(j+'0');		else			hash_asc[i*2+1]=(j+'a'-10);	}	hash_asc[HASH_LEN*2]='\0';}static void digest_HA1(unsigned char *hash,char *user,char *realm,char *passwd){	unsigned char hash_bin[HASH_LEN];	MD5_CTX	c;//	printf("%s:%s:%s\n",user,realm,passwd);//Hata	MD5Init(&c);	MD5Update(&c,user,strlen(user));	MD5Update(&c,":",1);	MD5Update(&c,realm,strlen(realm));	MD5Update(&c,":",1);	MD5Update(&c,passwd,strlen(passwd));	MD5Final(hash_bin,&c);	CvtHex(hash_bin,hash);//	printf("HA1:%s\n",hash); //Hata	return ;}static void digest_HA2(unsigned char *hash,char *method,char *uri){	unsigned char hash_bin[HASH_LEN];		MD5_CTX	c;//	printf("%s:%s\n",method,uri);	MD5Init(&c);	MD5Update(&c,method,strlen(method));	MD5Update(&c,":",1);	MD5Update(&c,uri,strlen(uri));	MD5Final(hash_bin,&c);	CvtHex(hash_bin,hash);//	printf("HA2:%s\n",hash); //Hata	return ;}static void digest_response(unsigned char *hash,		char *nonce,char *nc,char *cnonce,char *qop,		char *HA1,char *HA2){	unsigned char hash_bin[HASH_LEN];	MD5_CTX	c;//	printf("%s:%s:%s:%s:%s:%s\n",HA1,nonce,nc,cnonce,qop,HA2);	MD5Init(&c);	MD5Update(&c,HA1,32);	MD5Update(&c,":",1);	MD5Update(&c,nonce,strlen(nonce));	MD5Update(&c,":",1);	MD5Update(&c,nc,strlen(nc));	MD5Update(&c,":",1);	MD5Update(&c,cnonce,strlen(cnonce));	MD5Update(&c,":",1);	MD5Update(&c,qop,strlen(qop));	MD5Update(&c,":",1);	MD5Update(&c,HA2,32);	MD5Final(hash_bin,&c);	CvtHex(hash_bin,hash);//	printf("Response:%s\n",hash); //Hata	return ;}int CalcResponse(PAUTH *p){	char	HA1[HASH_LEN*2+1];	char	HA2[HASH_LEN*2+1];	char	response[HASH_LEN*2+1];	if(p==NULL){		logging(2,"CalcResponse:param error");		return NG;	}	if(strcmp(p->qop,"auth")!=0){		logging(2,"CalcResponse:qop is not auth");		return NG;	}	digest_HA1(HA1,p->username,p->realm,p->passwd);	digest_HA2(HA2,p->method,p->uri);	digest_response(response,p->nonce,p->nc,p->cnonce,p->qop,HA1,HA2);	strcpy(p->response,response);	return OK;}/************************/#ifdef MAINmain(){	char	*username="";	char	*nonce="1121030407281420";	char	*realm="Registered Users";	char	*passwd="mypasswd";	char	*uri="sip:011177@61.213.238.86;user=phone";	char	*cnonce="0D03C005";	char	nc[9]="00000001";	char	*qop="auth";	char	*method="INVITE";	char	HA1[36];	char	HA2[36];	char	response[36];	char	bin[16];	digest_HA1(HA1,username,realm,passwd);	digest_HA2(HA2,method,uri);	digest_response(response,nonce,nc,cnonce,qop,HA1,HA2);	printf("response:%s\n",response);}#endif

⌨️ 快捷键说明

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