📄 mysql.c
字号:
/***********************************************************************\ SIP Server Database Module for Mysql 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 <mysql.h>#include <string.h>#include <stdlib.h>#include "../sipd.h"#define DBHOST "localhost"#define DBUSER "root"#define DBNAME "sip"#define NG -1#define LOGIN \ if(NG==login_db(&connection)){\ return NG;\ }#define CLOSE mysql_close(&connection);typedef struct { int valid; char reg_user[CLEN]; char reg_domain[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_domain[CLEN]; char tag[255]; char reg_user[CLEN]; char reg_domain[CLEN]; int start; int expires; char contact_host[CLEN]; short contact_port; int cseq; int nseq; char callid[255]; char userAgent[CLEN];} SUBDB;static int debug=0;static void mes2reg(MESSAGE *mes,REGDB *r);static int login_db(MYSQL *mysql);static void mes2reg(MESSAGE *mes,REGDB *r);static void reg2mes(REGDB *r,MESSAGE *mes);static void mes2sub(MESSAGE *mes,SUBDB *r,int nseq);static void sub2mes(SUBDB *r,MESSAGE *mes);static int insert_record_regdb(REGDB *r);static int insert_record_subdb(SUBDB *r);static int delete_record_regdb(REGDB *r);static int delete_record_subdb(SUBDB *r);static int delete_record_subdb_with_callid(SUBDB *r);static int get_record_regdb(REGDB *r);static int get_record_regdb_with_callid(REGDB *r);static int get_expired_regdb(REGDB *r);static int get_expired_subdb(SUBDB *r);static int get_record_subdb(SUBDB *r,MESSAGE **pptr);#ifdef TESTstatic void ExpTest(void);#endifstatic int login_db(MYSQL *mysql){ MYSQL *c; mysql_init(mysql); c=mysql_real_connect(mysql, DBHOST, DBUSER,NULL, DBNAME, 0,NULL,0); if(c==NULL){ printf("%s\n",mysql_error(mysql)); return NG ; } return 0;}static void mes2reg(MESSAGE *mes,REGDB *r){ strcpy(r->reg_user,mes->header.from.username); strcpy(r->reg_domain,mes->header.from.host); //2003 Sep.24, Modified Hata //strcpy(r->tag,mes->header.from.param.tag); strcpy(r->tag,mes->header.from.aux); if(mes->header.contact!=NULL){ 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_domain); if(mes->header.contact!=NULL){ 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); /*2003 Sep 24 Mofified by Hata*/ strcpy(mes->header.from.aux,r->tag);}static void mes2sub(MESSAGE *mes,SUBDB *r,int nseq){ strcpy(r->sub_user,mes->header.from.username); strcpy(r->sub_domain,mes->header.from.host); //2003 Sep.24, Modified Hata //strcpy(r->tag,mes->header.from.param.tag); strcpy(r->tag,mes->header.from.aux); strcpy(r->reg_user,mes->header.to.username); strcpy(r->reg_domain,mes->header.to.host); if(mes->header.contact!=NULL){ strcpy(r->contact_host,mes->header.contact->host); r->contact_port=mes->header.contact->port; } r->cseq=mes->header.cseq.seq; r->nseq=nseq; 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_domain); strcpy(mes->header.from.param.tag,r->tag); strcpy(mes->header.to.username,r->reg_user); strcpy(mes->header.to.host,r->reg_domain); if(mes->header.contact!=NULL){ 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); /*2003 Sep 24 Mofified by Hata*/ strcpy(mes->header.from.aux,r->tag);} static int insert_record_regdb(REGDB *r){ MYSQL connection; char buff[256]; int status; time_t t; time(&t); LOGIN sprintf(buff,"insert into t_register values('%s','%s','%s',%d,%d,'%s',%d,%d,'%s')", r->reg_user,r->reg_domain,r->tag, (unsigned int)t,r->expires, r->contact_host,r->contact_port, r->cseq,r->callid);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 status;}static int insert_record_subdb(SUBDB *r){ MYSQL connection; char buff[256]; int status; time_t t; time(&t); LOGIN sprintf(buff,"insert into t_subscribe values('%s','%s','%s','%s','%s',%d,%d,'%s',%d,%d,%d,'%s','%s')", r->sub_user,r->sub_domain, r->reg_user,r->reg_domain,r->tag, (unsigned int)t,r->expires, r->contact_host,r->contact_port, r->cseq,r->nseq,r->callid,r->userAgent);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 status;}static int delete_record_regdb(REGDB *r){ MYSQL connection; char buff[256]; int status; LOGIN sprintf(buff,"delete from t_register \ where user='%s' and domain='%s' and callid='%s'", r->reg_user,r->reg_domain,r->callid);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 status;}static int delete_record_subdb(SUBDB *r){ MYSQL connection; char buff[256]; int status; LOGIN sprintf(buff,"delete 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 status;}static int delete_record_subdb_with_callid(SUBDB *r){ MYSQL connection; char buff[256]; int status; LOGIN sprintf(buff,"delete from t_subscribe where \ reg_user='%s' and reg_domain='%s' and \ sub_user='%s' and sub_domain='%s' and \ callid='%s'", r->reg_user,r->reg_domain, r->sub_user,r->sub_domain,r->callid);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 status;}static int get_record_regdb(REGDB *r){ MYSQL_RES *result; MYSQL_ROW row; MYSQL connection; char buff[256]; int status; int ret; LOGIN sprintf(buff,"select tag,start,expires,contact_host,contact_port,cseq,callid from t_register where user='%s' and domain='%s'", r->reg_user,r->reg_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); ret=0; if((row=mysql_fetch_row(result))!=NULL){ if(row[0]!=NULL) strcpy(r->tag,row[0]); if(row[1]!=NULL) r->start=atoi(row[1]); if(row[2]!=NULL) r->expires=atoi(row[2]); if(row[3]!=NULL) strcpy(r->contact_host,row[3]); if(row[4]!=NULL) r->contact_port=atoi(row[4]); if(row[5]!=NULL) r->cseq=atoi(row[5]); if(row[6]!=NULL) strcpy(r->callid,row[6]); ret=1; } mysql_free_result(result); CLOSE //レコ〖ドが1改でもあれば1、なければ0が手る return ret;}static int get_record_regdb_with_callid(REGDB *r){ MYSQL_RES *result; MYSQL_ROW row; MYSQL connection; char buff[256]; int status; int ret; LOGIN sprintf(buff,"select tag,start,expires,contact_host,contact_port,cseq,callid from t_register where user='%s' and domain='%s' and callid='%s'", r->reg_user,r->reg_domain,r->callid);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); ret=0; if((row=mysql_fetch_row(result))!=NULL){ if(row[0]!=NULL) strcpy(r->tag,row[0]); if(row[1]!=NULL) r->start=atoi(row[1]); if(row[2]!=NULL) r->expires=atoi(row[2]); if(row[3]!=NULL) strcpy(r->contact_host,row[3]); if(row[4]!=NULL) r->contact_port=atoi(row[4]); if(row[5]!=NULL) r->cseq=atoi(row[5]); if(row[6]!=NULL) strcpy(r->callid,row[6]); ret=1; } mysql_free_result(result); CLOSE //レコ〖ドが1改でもあれば1、なければ0が手る return ret;}static int get_expired_regdb(REGDB *r){ MYSQL_RES *result; MYSQL_ROW row; MYSQL connection; char buff[256]; int status; int ret; time_t t; t=time(NULL); LOGIN sprintf(buff,"select user,domain,tag,start,expires,contact_host,contact_port,cseq,callid from t_register where expires<%d and expires>0", (unsigned int)t);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); ret=0; if((row=mysql_fetch_row(result))!=NULL){ if(row[0]!=NULL) strcpy(r->reg_user,row[0]); if(row[1]!=NULL) strcpy(r->reg_domain,row[1]); if(row[2]!=NULL) strcpy(r->tag,row[2]); if(row[3]!=NULL) r->start=atoi(row[3]); if(row[4]!=NULL) r->expires=atoi(row[4]); if(row[5]!=NULL) strcpy(r->contact_host,row[5]); if(row[6]!=NULL) r->contact_port=atoi(row[6]); if(row[7]!=NULL) r->cseq=atoi(row[7]); if(row[8]!=NULL) strcpy(r->callid,row[8]); ret=1; } mysql_free_result(result); CLOSE return ret;}static int get_expired_subdb(SUBDB *r){ MYSQL_RES *result; MYSQL_ROW row; MYSQL connection; char buff[256]; int status; int ret; time_t t; t=time(NULL); LOGIN 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 expires<%d", (unsigned int)t);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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -