📄 mysql.c
字号:
ret=0; if((row=mysql_fetch_row(result))!=NULL){ if(row[0]!=NULL) strcpy(r->sub_user,row[0]); if(row[1]!=NULL) strcpy(r->sub_domain,row[1]); if(row[2]!=NULL) strcpy(r->reg_user,row[2]); if(row[3]!=NULL) strcpy(r->reg_domain,row[3]); if(row[4]!=NULL) strcpy(r->tag,row[4]); if(row[5]!=NULL) r->start=atoi(row[5]); if(row[6]!=NULL) r->expires=atoi(row[6]); if(row[7]!=NULL) strcpy(r->contact_host,row[7]); if(row[8]!=NULL) r->contact_port=atoi(row[8]); if(row[9]!=NULL) r->cseq=atoi(row[9]); if(row[10]!=NULL) r->nseq=atoi(row[10]); if(row[11]!=NULL) strcpy(r->callid,row[11]); if(row[12]!=NULL) strcpy(r->userAgent,row[12]); ret=1; } mysql_free_result(result); CLOSE return ret;}static int get_record_subdb(SUBDB *r,MESSAGE **pptr){ MYSQL_RES *result; MYSQL_ROW row; MYSQL connection; char buff[256]; int status; int ret; time_t t; SUBDB rr; MESSAGE *ptr; URI *uri; t=time(NULL); LOGIN if(r->sub_user[0]=='\0'){ sprintf(buff, "select sub_user,sub_domain,reg_user,reg_domain,\ tag,start,expires,contact_host,contact_port,cseq,nseq,\ callid,agent from t_subscribe\ where reg_user='%s' and reg_domain='%s'", r->reg_user,r->reg_domain); }else{ sprintf(buff, "select sub_user,sub_domain,reg_user,reg_domain,\ tag,start,expires,contact_host,contact_port,cseq,nseq,\ callid,agent from t_subscribe\ where reg_user='%s' and reg_domain='%s'\ and sub_user='%s' and sub_domain='%s'", r->reg_user,r->reg_domain, r->sub_user,r->sub_domain); }DEBUG printf("%s\n",buff);DEND status=mysql_real_query(&connection,buff,strlen(buff)); if(status !=0 ){ fprintf(stderr,"%s\n",mysql_error(&connection)); CLOSE return -1; } result = mysql_store_result(&connection); if(pptr==NULL){ ret=0; if((row=mysql_fetch_row(result))!=NULL){ if(row[0]!=NULL) strcpy(r->sub_user,row[0]); if(row[1]!=NULL) strcpy(r->sub_domain,row[1]); if(row[2]!=NULL) strcpy(r->reg_user,row[2]); if(row[3]!=NULL) strcpy(r->reg_domain,row[3]); if(row[4]!=NULL) strcpy(r->tag,row[4]); if(row[5]!=NULL) r->start=atoi(row[5]); if(row[6]!=NULL) r->expires=atoi(row[6]); if(row[7]!=NULL) strcpy(r->contact_host,row[7]); if(row[8]!=NULL) r->contact_port=atoi(row[8]); if(row[9]!=NULL) r->cseq=atoi(row[9]); if(row[10]!=NULL) r->nseq=atoi(row[10]); if(row[11]!=NULL) strcpy(r->callid,row[11]); if(row[12]!=NULL) strcpy(r->userAgent,row[12]); ret=1; } }else{ for(ret=0;(row=mysql_fetch_row(result))!=NULL;ret++){ if(ret>USER_MAX) break; memset(&rr,0,sizeof(SUBDB)); ptr=(MESSAGE *)malloc(sizeof(MESSAGE)); if(ptr==NULL) break; memset(ptr,0,sizeof(MESSAGE)); uri=(URI *)malloc(sizeof(URI)); if(uri==NULL){ free(ptr);break;} memset(uri,0,sizeof(URI)); ptr->header.contact=uri; pptr[ret]=ptr; if(row[0]!=NULL) strcpy(rr.sub_user,row[0]); if(row[1]!=NULL) strcpy(rr.sub_domain,row[1]); if(row[2]!=NULL) strcpy(rr.reg_user,row[2]); if(row[3]!=NULL) strcpy(rr.reg_domain,row[3]); if(row[4]!=NULL) strcpy(rr.tag,row[4]); if(row[5]!=NULL) rr.start=atoi(row[5]); if(row[6]!=NULL) rr.expires=atoi(row[6]); if(row[7]!=NULL) strcpy(rr.contact_host,row[7]); if(row[8]!=NULL) rr.contact_port=atoi(row[8]); if(row[9]!=NULL) rr.cseq=atoi(row[9]); if(row[10]!=NULL) rr.nseq=atoi(row[10]); if(row[11]!=NULL) strcpy(rr.callid,row[11]); if(row[12]!=NULL) strcpy(rr.userAgent,row[12]); sub2mes(&rr,ptr); } } mysql_free_result(result); CLOSE return ret;}int UpdateREGDB(MESSAGE *mes){ REGDB r; int res; int status; status=REG_ADD; mes2reg(mes,&r); if(mes->header.expires==0){ res=get_record_regdb_with_callid(&r); if(res==1){//Exists that record res=delete_record_regdb(&r); if(res==NG){ return NG; } status=REG_DEL; }else{ //Try to delete someone who isn't in DB status=REG_NONE; } }else{ res=get_record_regdb(&r); if(res==1){//Exists that record res=delete_record_regdb(&r); if(res==NG){ return NG; } mes2reg(mes,&r); res=insert_record_regdb(&r); if(res!=0){ status=NG; } status=REG_RENEW; }else{ res=insert_record_regdb(&r); if(res!=0){ status=NG; } status=REG_ADD; } }DEBUG printf("UpdateREGDB=%d\n",status);DEND return status;}int UpdateSUBDB(MESSAGE *mes){ SUBDB r; int res; int status; status=REG_ADD; mes2sub(mes,&r,1); if(mes->header.expires==0){ delete_record_subdb_with_callid(&r); status=REG_DEL; }else{ res=get_record_subdb(&r,NULL); if(res==1){//Exists that record delete_record_subdb(&r); mes2sub(mes,&r,r.nseq); status=REG_RENEW; } res=insert_record_subdb(&r); if(res!=0){ status=NG; } } return status;}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,1); 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,1); 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 res; } 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;}int ClearDB(void){ MESSAGE mes; int ret; int i; char bf[80]; memset(&mes,0,sizeof(MESSAGE)); for(i=0;;i++){ ret=ExpiredREGDB(&mes); if(ret==0){ break; } if(ret==NG){ return NG; } mes.header.expires=0; ret=UpdateREGDB(&mes); if(ret==NG){ return NG; } } if(i){ sprintf(bf,"Delete %d record(s) from t_register",i); logging(1,bf); } for(i=0;;i++){ ret=ExpiredSUBDB(&mes); if(ret==0){ break; } if(ret==NG){ return NG; } mes.header.expires=0; ret=UpdateSUBDB(&mes); if(ret==NG){ return NG; } } if(i){ sprintf(bf,"Delete %d record(s) from t_subscribe",i); logging(1,bf); } return OK;}int InitDB(void){ return ClearDB();} #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 + -