/* = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = Name : generate_paging_conf.sh Author : ssc Version : v1.0 Copyright : ZYCOO copyright Description : Generate paging info from mysql to paging conf file = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = */ #include #include #include #include #include #include #include #include MYSQL *g_conn; // mysql 连接 MYSQL_RES *d_res; // mysql device记录集 MYSQL_ROW d_row; // 字符串数组,mysql 记录行 MYSQL_RES *u_res; // mysql user记录集 MYSQL_ROW u_row; // 字符串数组,mysql 记录行 #define MAX_TRUNK_SIZE 256 #define MAX_SIZE 2048 #define MIDLE_SIZE 512 #define MINI_SIZE 64 #define PJSIP_DEV_AORS "/etc/asterisk/pjsip_dev_aors.conf" #define PJSIP_DEV_AUTHS "/etc/asterisk/pjsip_dev_auths.conf" #define PJSIP_DEV_EPS "/etc/asterisk/pjsip_dev_eps.conf" #define PJSIP_USR_AORS "/etc/asterisk/pjsip_usr_aors.conf" #define PJSIP_USR_AUTHS "/etc/asterisk/pjsip_usr_auths.conf" #define PJSIP_USR_EPS "/etc/asterisk/pjsip_usr_eps.conf" #define EXTEN_HINTS "/etc/asterisk/extensions_hints_exten.conf" #define KEYVALLEN 100 #define VERSION "V1.0.1" #define QUERY_SIP_DEV_SQL "SELECT id,exten,cid_number,qualify_timeout,qualify_frequency,context,fullname,secret,videosupport,transport,default_expiration,ulaw,alaw,g722,opus,h264,recordin,recordout from t_pbx_users_extension order by exten asc" #define QUERY_SIP_USR_SQL "SELECT id,exten,cid_number,qualify_timeout,qualify_frequency,context,fullname,secret,videosupport,transport,default_expiration,ulaw,alaw,g722,opus,h264,dtlscertfile,dtlscafile,recordin,recordout from t_pbx_users_webrtc order by exten asc" char g_host_name[MINI_SIZE]; char g_user_name[MINI_SIZE]; char g_password[MINI_SIZE]; char g_db_name[MINI_SIZE]; const unsigned int g_db_port = 3306; char * mytime(){ time_t my_time; time(&my_time); char *time_string = ctime(&my_time); if (time_string[strlen(time_string) - 1] == '\n') { time_string[strlen(time_string) - 1] = '\0'; } return time_string; } void print_mysql_error(const char *msg) { // 打印最后一次错误 if (msg) printf("%s: %s\n", msg, mysql_error(g_conn)); else puts(mysql_error(g_conn)); } int executesql(const char * sql) { /*query the database according the sql*/ if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败 return -1; // 表示失败 return 0; // 成功执行 } int init_mysql() { // 初始化连接 // init the database connection g_conn = mysql_init(NULL); /* connect the database */ if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败 return -1; // 是否连接已经可用 if (executesql("set names utf8")) // 如果失败 return -1; return 0; // 返回成功 } /* ** DATABASE TABLE "t_pbx_users_extension" `id` int(16) NOT NULL AUTO_INCREMENT, `exten` varchar(32) NOT NULL, `cid_number` varchar(32) NOT NULL DEFAULT '', `qualify_timeout` smallint NOT NULL DEFAULT 60, `qualify_frequency` smallint NOT NULL DEFAULT 120, `call-limit` varchar(2) NOT NULL DEFAULT '5', `host` varchar(16) NOT NULL DEFAULT 'dynamic', `context` varchar(64) NOT NULL DEFAULT '', `fullname` varchar(64) NOT NULL DEFAULT '', `secret` varchar(128) NOT NULL DEFAULT '', `outboundcid` varchar(32) NOT NULL DEFAULT '', `email` varchar(64) NOT NULL DEFAULT '', `nat` varchar(32) NOT NULL DEFAULT 'force_rport,comedia', `directmedia` varchar(3) NOT NULL DEFAULT 'no', `dtmfmode` varchar(8) NOT NULL DEFAULT 'rfc2833', `videosupport` varchar(3) NOT NULL DEFAULT 'no', `transport` varchar(11) NOT NULL DEFAULT 'udp', `encryption` varchar(3) NOT NULL DEFAULT 'no', `srtpcapable` varchar(3) NOT NULL DEFAULT 'no', `default_expiration` smallint NOT NULL DEFAULT 600, `ulaw` bool NOT NULL DEFAULT 1, `alaw` bool NOT NULL DEFAULT 1, `g722` bool NOT NULL DEFAULT 1, `g729` bool NOT NULL DEFAULT 0, `g726` bool NOT NULL DEFAULT 0, `gsm` bool NOT NULL DEFAULT 0, `speex` bool NOT NULL DEFAULT 0, `h261` bool NOT NULL DEFAULT 0, `h263` bool NOT NULL DEFAULT 0, `h263p` bool NOT NULL DEFAULT 0, `h264` bool NOT NULL DEFAULT 0, `vp8` bool DEFAULT 0, `opus` bool DEFAULT 0, `recordin` varchar(32) NOT NULL DEFAULT '1', `recordout` varchar(32) NOT NULL DEFAULT '1', */ int main(int argc, char **argv) { char codecs[64]; memset(g_host_name, 0, sizeof(g_host_name)); memset(g_user_name, 0, sizeof(g_user_name)); memset(g_password, 0, sizeof(g_password)); memset(g_db_name, 0, sizeof(g_db_name)); strcpy(g_host_name,getenv("MYSQL")); strcpy(g_user_name,getenv("MYSQL_USER")); strcpy(g_password,getenv("MYSQL_PASSWORD")); strcpy(g_db_name,getenv("MYSQL_DATABASE")); if (init_mysql()){ print_mysql_error(NULL); exit(1); } if (executesql(QUERY_SIP_DEV_SQL)){ print_mysql_error(NULL); exit(1); } d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集 FILE *conf_dev_aors = fopen(PJSIP_DEV_AORS, "w+"); FILE *conf_dev_auths = fopen(PJSIP_DEV_AUTHS, "w+"); FILE *conf_dev_eps = fopen(PJSIP_DEV_EPS, "w+"); FILE *conf_exten_hints = fopen(EXTEN_HINTS, "w+"); if (conf_dev_aors == NULL){ perror("Open aors conf file Error: "); exit(1); } fprintf(conf_dev_aors, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_dev_aors.conf (/etc/asterisk/pjsip_dev_aors.conf)\n\ ;! Generator: Generator PJSIP AORS\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_dev_auths == NULL){ perror("Open auths conf file Error: "); exit(1); } fprintf(conf_dev_auths, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_dev_auths.conf (/etc/asterisk/pjsip_dev_auths.conf)\n\ ;! Generator: Generator PJSIP AUTHS\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_dev_eps == NULL){ perror("Open endpoint conf file Error: "); exit(1); } fprintf(conf_dev_eps, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_dev_auths.conf (/etc/asterisk/pjsip_dev_eps.conf)\n\ ;! Generator: Generator PJSIP ENDPOINTS\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); fprintf(conf_exten_hints, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: extensions_hints_exten.conf (/etc/asterisk/extensions_hints_exten.conf)\n\ ;! Generator: Generator Extension Hints\n\ ;! Creation Date: %s\n\ ;!\n\n\ [channelhints_exten]\n\ ",\ mytime()\ ); while ((d_row = mysql_fetch_row(d_res))) { // 打印结果集 if (d_row[1] == NULL || d_row[6] == NULL || d_row[9] == NULL) { printf("some feild is empty!\n"); continue; } fprintf(conf_dev_aors, "\ [%s]\n\ type = aor\n\ max_contacts = 1\n\ remove_existing = yes\n\ remove_unavailable = yes\n\ default_expiration = %s\n\ qualify_timeout = %s\n\ qualify_frequency = %s\n\ authenticate_qualify = no\n\ support_path = yes\n\ \n",\ d_row[1],\ d_row[10],\ d_row[3],\ d_row[4]\ ); fprintf(conf_dev_auths, "\ [%s]\n\ type = auth\n\ auth_type = userpass\n\ password = %s\n\ username = %s\n\ \n",\ d_row[1],\ d_row[7],\ d_row[1]\ ); memset(codecs, 0, sizeof(codecs)); if(d_row[11]) { strcpy(codecs,"ulaw"); } if(d_row[12]) { if(strlen(codecs)) { strcat(codecs,",alaw"); } else { strcpy(codecs,"alaw"); } } if(d_row[13]) { if(strlen(codecs)) { strcat(codecs,",g722"); } else { strcpy(codecs,"g722"); } } if(d_row[14]) { if(strlen(codecs)) { strcat(codecs,",opus"); } else { strcpy(codecs,"opus"); } } if(d_row[15]) { if(strlen(codecs)) { strcat(codecs,",h264"); } else { strcpy(codecs,"h264"); } } fprintf(conf_dev_eps, "\ [%s]\n\ type = endpoint\n\ transport = transport-%s\n\ aors = %s\n\ auth = %s\n\ context = %s\n\ allow = %s\n\ direct_media = no\n\ connected_line_method = update\n\ direct_media_method = update\n\ direct_media_glare_mitigation = incoming\n\ disable_direct_media_on_nat = yes\n\ dtmf_mode = rfc4733\n\ force_rport = yes\n\ ice_support = no\n\ identify_by = username\n\ moh_suggest = default\n\ outbound_proxy = \n\ rewrite_contact = no\n\ rtp_symmetric = no\n\ send_diversion = no\n\ send_pai = no\n\ send_rpid = no\n\ callerid = %s <%s>\n\ media_encryption = no\n\ inband_progress = no\n\ call_group = 1\n\ pickup_group = 1\n\ device_state_busy_at = 0\n\ language = \n\ allow_transfer = yes\n\ allow_subscribe = yes\n\ sdp_session = IP Audio Center\n\ tos_audio = ef\n\ tos_video = AF41\n\ cos_audio = 5\n\ cos_video = 4\n\ media_use_received_transport = no\n\ user_eq_phone = no\n\ rtp_keepalive = 60\n\ rtp_timeout = 60\n\ rtp_timeout_hold = 300\n\ ;acl = remote_extensions_acl\n\ rtcp_mux = no\n\ codec_prefs_incoming_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\ codec_prefs_incoming_offer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\ codec_prefs_outgoing_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\ codec_prefs_outgoing_offer = prefer:pending, operation:union, keep:all, transcode:allow\n\ \n",\ d_row[1],\ d_row[9],\ d_row[1],\ d_row[1],\ d_row[5],\ codecs,\ d_row[6],\ d_row[1]\ ); fprintf(conf_exten_hints, "\ exten => %s,hint,PJSIP/%s,CustomPresence:%s\n\ \n",\ d_row[1],\ d_row[1],\ d_row[1]\ ); } if (executesql(QUERY_SIP_USR_SQL)){ print_mysql_error(NULL); exit(1); } u_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集 FILE *conf_usr_aors = fopen(PJSIP_USR_AORS, "w+"); FILE *conf_usr_auths = fopen(PJSIP_USR_AUTHS, "w+"); FILE *conf_usr_eps = fopen(PJSIP_USR_EPS, "w+"); if (conf_usr_aors == NULL){ perror("Open user aors conf file Error: "); exit(1); } fprintf(conf_usr_aors, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_usr_aors.conf (/etc/asterisk/pjsip_usr_aors.conf)\n\ ;! Generator: Generator PJSIP AORS\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_usr_auths == NULL){ perror("Open user auths conf file Error: "); exit(1); } fprintf(conf_usr_auths, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_usr_auths.conf (/etc/asterisk/pjsip_usr_auths.conf)\n\ ;! Generator: Generator PJSIP AUTHS\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_usr_eps == NULL){ perror("Open user endpoint conf file Error: "); exit(1); } fprintf(conf_usr_eps, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_usr_auths.conf (/etc/asterisk/pjsip_usr_eps.conf)\n\ ;! Generator: Generator PJSIP ENDPOINTS\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); while ((u_row = mysql_fetch_row(u_res))) { // 打印结果集 if (u_row[1] == NULL || u_row[6] == NULL || u_row[9] == NULL) { printf("some feild is empty!\n"); continue; } fprintf(conf_usr_aors, "\ [%s]\n\ type = aor\n\ max_contacts = 1\n\ remove_existing = yes\n\ remove_unavailable = yes\n\ default_expiration = %s\n\ qualify_timeout = %s\n\ qualify_frequency = %s\n\ authenticate_qualify = no\n\ support_path = yes\n\ \n",\ u_row[1],\ u_row[10],\ u_row[3],\ u_row[4]\ ); fprintf(conf_usr_auths, "\ [%s]\n\ type = auth\n\ auth_type = userpass\n\ password = %s\n\ username = %s\n\ \n",\ u_row[1],\ u_row[7],\ u_row[1]\ ); memset(codecs, 0, sizeof(codecs)); if(atoi(u_row[11])) { strcpy(codecs,"ulaw"); } if(atoi(u_row[12])) { if(strlen(codecs)) { strcat(codecs,",alaw"); } else { strcpy(codecs,"alaw"); } } if(atoi(u_row[13])) { if(strlen(codecs)) { strcat(codecs,",g722"); } else { strcpy(codecs,"g722"); } } if(atoi(u_row[14])) { if(strlen(codecs)) { strcat(codecs,",opus"); } else { strcpy(codecs,"opus"); } } if(atoi(u_row[15])) { if(strlen(codecs)) { strcat(codecs,",h264"); } else { strcpy(codecs,"h264"); } } fprintf(conf_usr_eps, "\ [%s]\n\ type = endpoint\n\ transport = transport-wss\n\ aors = %s\n\ auth = %s\n\ context = %s\n\ allow = %s\n\ direct_media = no\n\ connected_line_method = update\n\ direct_media_method = update\n\ direct_media_glare_mitigation = incoming\n\ disable_direct_media_on_nat = yes\n\ dtmf_mode = rfc4733\n\ force_rport = no\n\ use_avpf=yes\n\ media_encryption=dtls\n\ dtls_verify=fingerprint\n\ dtls_setup=actpass\n\ ice_support=yes\n\ media_use_received_transport=yes\n\ rtcp_mux=yes\n\ identify_by = username\n\ moh_suggest = default\n\ outbound_proxy = \n\ rewrite_contact = no\n\ rtp_symmetric = no\n\ send_diversion = no\n\ send_pai = no \n\ send_rpid = no \n\ callerid = %s <%s>\n\ inband_progress = no\n\ call_group = 1\n\ pickup_group = 1\n\ device_state_busy_at = 1\n\ language = en\n\ allow_transfer = yes\n\ allow_subscribe = yes\n\ sdp_session = IP Audio Center\n\ tos_audio = ef\n\ tos_video = AF41\n\ cos_audio = 5\n\ cos_video = 4\n\ dtls_cert_file = %s\n\ dtls_ca_file = %s\n\ user_eq_phone = no\n\ rtp_keepalive = 60\n\ rtp_timeout = 60\n\ rtp_timeout_hold = 300\n\ ;acl = local_extensions_acl\n\ codec_prefs_incoming_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\ codec_prefs_incoming_offer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\ codec_prefs_outgoing_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\ codec_prefs_outgoing_offer = prefer:pending, operation:union, keep:all, transcode:allow\n\ \n",\ u_row[1],\ u_row[1],\ u_row[1],\ u_row[5],\ codecs,\ u_row[6],\ u_row[1],\ u_row[16],\ u_row[17]\ ); fprintf(conf_exten_hints, "\ exten => %s,hint,PJSIP/%s,CustomPresence:%s\n\ \n",\ u_row[1],\ u_row[1],\ u_row[1]\ ); } fclose(conf_dev_aors); fclose(conf_dev_auths); fclose(conf_dev_eps); fclose(conf_usr_aors); fclose(conf_usr_auths); fclose(conf_usr_eps); fclose(conf_exten_hints); mysql_free_result(d_res); // 释放结果集 mysql_free_result(u_res); mysql_close(g_conn); // 关闭链接 }