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

📄 db.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 <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <time.h>#include "sipd.h"typedef struct {	int	valid;	char	reg_user[CLEN];	char	reg_host[CLEN];	char	tag[255];	int	start;	int	expires;	char	contact_host[CLEN];	short	contact_port;	int	cseq;	char	callid[255];} REGDB;typedef struct {	int	valid;	char	sub_user[CLEN];	char	sub_host[CLEN];	char	tag[255];	char	reg_user[CLEN];	char	reg_host[CLEN];	int	start;	int	expires;	char	contact_host[CLEN];	short	contact_port;	int	cseq;	int	nseq;	char	callid[255];	char	userAgent[CLEN];} SUBDB;static	REGDB	regdb[USER_MAX];static	SUBDB	subdb[USER_MAX];static void mes2reg(MESSAGE *mes,REGDB *r){	strcpy(r->reg_user,mes->header.from.username);	strcpy(r->reg_host,mes->header.from.host);	strcpy(r->contact_host,(mes->header.contact)->host);	r->contact_port=(mes->header.contact)->port;	r->cseq=mes->header.cseq.seq;	strcpy(r->callid,mes->header.callid);	r->start=time(NULL);	r->expires=time(NULL)+mes->header.expires;}static void reg2mes(REGDB *r,MESSAGE *mes){	strcpy(mes->header.from.username,r->reg_user);	strcpy(mes->header.from.host,r->reg_host);	strcpy((mes->header.contact)->host,r->contact_host);	(mes->header.contact)->port= r->contact_port;	mes->header.cseq.seq = r->cseq;	strcpy(mes->header.callid,r->callid);}		static void mes2sub(MESSAGE *mes,SUBDB *r){	strcpy(r->sub_user,mes->header.from.username);	strcpy(r->sub_host,mes->header.from.host);	strcpy(r->reg_user,mes->header.to.username);	strcpy(r->reg_host,mes->header.to.host);	strcpy(r->contact_host,(mes->header.contact)->host);	r->contact_port=(mes->header.contact)->port;	r->cseq=mes->header.cseq.seq;	r->nseq=1;	strcpy(r->callid,mes->header.callid);	strcpy(r->userAgent,mes->header.userAgent);	r->start=time(NULL);	r->expires=time(NULL)+mes->header.expires;}		static void sub2mes(SUBDB *r,MESSAGE *mes){	strcpy(mes->header.from.username,r->sub_user);	strcpy(mes->header.from.host,r->sub_host);	strcpy(mes->header.to.username,r->reg_user);	strcpy(mes->header.to.host,r->reg_host);	strcpy((mes->header.contact)->host,r->contact_host);	(mes->header.contact)->port = r->contact_port;	mes->header.cseq.seq=r->cseq;	strcpy(mes->header.callid,r->callid);	strcpy(mes->header.userAgent,r->userAgent);}		static int insert_record_regdb(REGDB *r){	int i;	for(i=0;i<USER_MAX;i++){		if(regdb[i].valid!=0){			continue;		}		memcpy(&regdb[i],r,sizeof(REGDB));		regdb[i].valid=1;		return 0;	}	return -1;}static int insert_record_subdb(SUBDB *r){	int i;	for(i=0;i<USER_MAX;i++){		if(subdb[i].valid!=0){			continue;		}		memcpy(&subdb[i],r,sizeof(SUBDB));		subdb[i].valid=1;		return 0;	}	return -1;}static int delete_record_regdb(REGDB *r){	int i;	for(i=0;i<USER_MAX;i++){		if(regdb[i].valid==0){			continue;		}		if( 			(strcmp(regdb[i].reg_user,r->reg_user)==0)&&			(strcmp(regdb[i].reg_host,r->reg_host)==0)){			regdb[i].valid=0;		}	}	return 0;}static int delete_record_subdb(SUBDB *r){	int i;	for(i=0;i<USER_MAX;i++){		if(subdb[i].valid==0){			continue;		}		if( 			(strcmp(subdb[i].sub_user,r->sub_user)==0)&&			(strcmp(subdb[i].sub_host,r->sub_host)==0)&&			(strcmp(subdb[i].reg_user,r->reg_user)==0)&&			(strcmp(subdb[i].reg_host,r->reg_host)==0)			){			subdb[i].valid=0;		}	}	return 0;}static int get_record_regdb(REGDB *r){	int i;	for(i=0;i<USER_MAX;i++){		if(regdb[i].valid==0){			continue;		}		if( 			(strcmp(regdb[i].reg_user,r->reg_user)==0)&&			(strcmp(regdb[i].reg_host,r->reg_host)==0)){			memcpy(r,&regdb[i],sizeof(REGDB));			return 1;		}	}	return 0;}static int get_expired_regdb(REGDB *r){	int i;	time_t t;	t=time(NULL);	for(i=0;i<USER_MAX;i++){		if(regdb[i].valid==0){			continue;		}		if( regdb[i].expires<t){			memcpy(r,&regdb[i],sizeof(REGDB));			return 1;		}	}	return 0;}static int get_expired_subdb(SUBDB *r){	int i;	time_t t;	t=time(NULL);	for(i=0;i<USER_MAX;i++){		if(subdb[i].valid==0){			continue;		}		if( subdb[i].expires<t){			memcpy(r,&subdb[i],sizeof(SUBDB));			return 1;		}	}	return 0;}static int get_record_subdb(SUBDB *r,MESSAGE **pptr){	int i;	MESSAGE	*ptr;	int n;	int hit;	SUBDB	rr;	for(n=0,i=0;i<USER_MAX;i++){		if(subdb[i].valid==0){			continue;		}		hit=0;		if(r->sub_user[0]!='\0'){			if( 		 	    (strcmp(subdb[i].sub_user,r->sub_user)==0)&&			    (strcmp(subdb[i].sub_host,r->sub_host)==0)&&			    (strcmp(subdb[i].reg_user,r->reg_user)==0)&&			    (strcmp(subdb[i].reg_host,r->reg_host)==0)			){				hit=1;			}else{				hit=0;			}		}else{			if( 			    (strcmp(subdb[i].reg_user,r->reg_user)==0)&&			    (strcmp(subdb[i].reg_host,r->reg_host)==0)			){				hit=1;			}else{				hit=0;			}		}		if(hit==1){			//Hit!			if(pptr!=NULL){				memcpy(&rr,&subdb[i],sizeof(SUBDB));				ptr=(MESSAGE *)malloc(sizeof(MESSAGE));				if(ptr==NULL){					break;				}				pptr[n++]=ptr;				sub2mes(&rr,ptr);			}else{				memcpy(r,&subdb[i],sizeof(SUBDB));				return 1;			}		}	}	return n;}	int UpdateREGDB(MESSAGE *mes){	REGDB	r;	int	res;	mes2reg(mes,&r);	if(mes->header.expires==0){		delete_record_regdb(&r);		res=0;	}else{		res=get_record_regdb(&r);		if(res==1){//Exists that record			delete_record_regdb(&r);			mes2reg(mes,&r);		}		res=insert_record_regdb(&r);	}}int UpdateSUBDB(MESSAGE *mes){	SUBDB	r;	int	res;	mes2sub(mes,&r);	if(mes->header.expires==0){		delete_record_subdb(&r);		res=0;	}else{		res=get_record_subdb(&r,NULL);		if(res==1){//Exists that record			delete_record_subdb(&r);			mes2sub(mes,&r);		}		res=insert_record_subdb(&r);	}}int GetREGDB(MESSAGE *mes){	REGDB	r;	int	res;	mes2reg(mes,&r);	res=get_record_regdb(&r);	if(res==1){//Exists that record		reg2mes(&r,mes);	}	return res;}int GetSUBDB(MESSAGE *mes,MESSAGE **ptr){	SUBDB	r;	int	res;	mes2sub(mes,&r);	res=get_record_subdb(&r,ptr);	if(res==1 && ptr==NULL){//Exists that record		sub2mes(&r,mes);	}	return res;}int AddNSeqSUBDB(MESSAGE *mes){	SUBDB	r;	int	res;	mes2sub(mes,&r);	res=get_record_subdb(&r,NULL);	if(res==1){//Exists that record		delete_record_subdb(&r);		res=r.nseq;		r.nseq++;		insert_record_subdb(&r);	}else{		return -1;	}	return res;}int ExpiredREGDB(MESSAGE *mes){	REGDB	r;	int res;	res=get_expired_regdb(&r);	if(res==1){		reg2mes(&r,mes);	}	return res;}int ExpiredSUBDB(MESSAGE *mes){	SUBDB	r;	int res;	res=get_expired_subdb(&r);	if(res==1){		sub2mes(&r,mes);	}	return res;}			void InitDB(void){	int i;	for(i=0;i<USER_MAX;i++){		regdb[i].valid=subdb[i].valid=0;	}}void ClearDB(void){        MESSAGE mes;        int     ret;        int     i;FORE        printf("*** initializing Database\n");DEND        for(i=0;;i++){                ret=ExpiredREGDB(&mes);                if(ret==0){                        break;                }                mes.header.expires=0;                ret=UpdateREGDB(&mes);        }FORE        printf("*** Delete %d record(s) from t_register\n",i);DEND        for(i=0;;i++){                ret=ExpiredSUBDB(&mes);                if(ret==0){                        break;                }                mes.header.expires=0;                ret=UpdateSUBDB(&mes);        }FORE        printf("*** Delete %d record(s) from t_subscribe\n",i);DEND}void DumpREGDB(void){	time_t	t;	int i;	time(&t);	for(i= 0; i<USER_MAX;i++){		if(regdb[i].valid==1){			printf("-------------------------REG %3d-----\n",i);			printf("ID:%s@%s\n",regdb[i].reg_user,						regdb[i].reg_host);			printf("Expires:%d\t%d\n",					regdb[i].start, regdb[i].expires-t);			printf("CSeq:%d\n",regdb[i].cseq);			printf("Call-ID:%s\n",regdb[i].callid);			printf("Contact:%s:%d\n",regdb[i].contact_host,					regdb[i].contact_port);		}	}}void DumpSUBDB(void){	time_t	t;	int	i;	time(&t);	for(i= 0; i<USER_MAX;i++){		if(subdb[i].valid==1){			printf("-------------------------SUB %3d-----\n",i);			printf("ID:%s@%s\t%s@%s\n",				subdb[i].sub_user, subdb[i].sub_host,				subdb[i].reg_user, subdb[i].reg_host);			printf("Expires:%d\t%d\n",					subdb[i].start, subdb[i].expires-t);			printf("CSeq:%d\n",subdb[i].cseq);			printf("NSeq:%d\n",subdb[i].nseq);			printf("Call-ID:%s\n",subdb[i].callid);			printf("Contact:%s:%d\n",subdb[i].contact_host,					subdb[i].contact_port);		}	}}#ifdef TESTRegTest(){	MESSAGE	mes;	MESSAGE	mes2;	int	res;	strcpy(mes.header.from.username,"natsumi");	strcpy(mes.header.from.host,"sphere");	strcpy(mes.header.contact.host,"202.229.156.228");	mes.header.contact.port=5060;	mes.header.expires=100;	sprintf(mes.header.callid,			"testtest%d@sphere.ad.jp",time(NULL));	mes.header.cseq.seq=5;	UpdateREGDB(&mes);//CREATE natsumi		DumpREGDB();	mes.header.cseq.seq=6; 	UpdateREGDB(&mes);//UPDATE natsumi		DumpREGDB();	strcpy(mes.header.from.username,"erina");	UpdateREGDB(&mes);//CREATE erina	DumpREGDB();	strcpy(mes.header.from.username,"natsumi");	mes.header.expires=0;	UpdateREGDB(&mes);//DELETE natsumi 	DumpREGDB();	strcpy(mes2.header.from.username,"erina");	strcpy(mes2.header.from.host,"sphere");	res=GetREGDB(&mes2);	if(res==1){		printf("Hit\n");		printf("ID:%s@%s\n",mes2.header.from.username,				mes2.header.from.host);		printf("CONTACT:%s:%d\n",mes2.header.contact.host,				mes2.header.contact.port);		printf("Call-ID:%s\n",mes2.header.callid);		printf("CSeq:%d\n",mes2.header.cseq.seq);	}else{		printf("None\n");	}}SubTest(){	int	i;	MESSAGE 	mes;	MESSAGE		*ptr[USER_MAX];	for(i=0;i<USER_MAX;i++) ptr[i]=NULL;	strcpy(mes.header.from.username,"natsumi");	strcpy(mes.header.from.host,"sphere");	strcpy(mes.header.to.username,"erina");	strcpy(mes.header.to.host,"sphere");	strcpy(mes.header.contact.host,"202.229.156.228");	mes.header.contact.port=5060;	mes.header.expires=5;	sprintf(mes.header.callid,			"testtest%d@sphere.ad.jp",time(NULL));	mes.header.cseq.seq=5;	UpdateSUBDB(&mes);	DumpSUBDB();	mes.header.cseq.seq=6;	UpdateSUBDB(&mes);	DumpSUBDB();	strcpy(mes.header.from.username,"hata");	UpdateSUBDB(&mes);	DumpSUBDB();	strcpy(mes.header.to.username,"yoko");	UpdateSUBDB(&mes);	DumpSUBDB();	mes.header.from.username[0]='\0';	strcpy(mes.header.to.username,"erina");	GetSUBDB(&mes,ptr);	for(i=0;i<USER_MAX;i++){		if(ptr[i]!=NULL){			printf("================GET %d========\n",i);			printf("ID:%s@%s\t%s@%s\n",					ptr[i]->header.to.username,					ptr[i]->header.to.host,					ptr[i]->header.from.username,					ptr[i]->header.from.host);			free(ptr[i]);		}	}	strcpy(mes.header.from.username,"hata");	printf("res=%d\n",AddNSeqSUBDB(&mes));	printf("res=%d\n",AddNSeqSUBDB(&mes));	printf("res=%d\n",AddNSeqSUBDB(&mes));	printf("res=%d\n",AddNSeqSUBDB(&mes));	printf("res=%d\n",AddNSeqSUBDB(&mes));	DumpSUBDB();	mes.header.expires=0;	UpdateSUBDB(&mes);	DumpSUBDB();}static void ExpTest(void){	int i;	MESSAGE	mes;	int res;	for(i=0;i<10;i++){		sleep(1);		printf("WAkeup==================\n");		DumpSUBDB();		res=ExpiredSUBDB(&mes);		if(res==0){			continue;		}		mes.header.expires=0;		UpdateSUBDB(&mes);		memset(&mes,0,sizeof(MESSAGE));		}}main(){	InitDB();	SubTest();	ExpTest();}#endif 

⌨️ 快捷键说明

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