/* ============================================================================ Name : generate_trunk_conf.sh Author : ssc Version : v1.0 Copyright : ZYCOO copyright Description : Generate trunk info from mysql to turnk conf file ============================================================================ */ #include #include #include #include #include #include #include #include MYSQL *g_conn; // mysql 连接 MYSQL_RES *g_res; // mysql 记录集 MYSQL_ROW g_row; // 字符串数组,mysql 记录行 #define MAX_TRUNK_SIZE 256 #define MIDLE_SIZE 512 #define MINI_SIZE 64 #define TINY_SIZE 4 #define PJSIP_TRUNK_REGS "/etc/asterisk/pjsip_trunk_regs.conf" #define PJSIP_TRUNK_AUTHS "/etc/asterisk/pjsip_trunk_auths.conf" #define PJSIP_TRUNK_EPS "/etc/asterisk/pjsip_trunk_eps.conf" #define PJSIP_TRUNK_AORS "/etc/asterisk/pjsip_trunk_aors.conf" #define PJSIP_TRUNK_IDFS "/etc/asterisk/pjsip_trunk_idfs.conf" #define TRUNK_GLOBAL_FILE "/etc/asterisk/extensions_trunks_global_custom.conf" #define DID_CONF_FILE "/etc/asterisk/extensions_extendid_custom.conf" #define DOD_CONF_FILE "/etc/asterisk/extensions_extendod_custom.conf" #define KEYVALLEN 100 #define VERSION "V2.0.1" #define QUERY_TRUNK_SQL "select trunkactive,trunkstyle,trunk,host,port,outbound_proxy,voipusername,authuser,\ fromuser,fromdomain,contact,voipsecret,qualify,qualifyfreq,transport,context,\ dtmfmode,videosupport,encryption,srtpcapable,alaw,ulaw,g722,g729,g726,gsm,\ speex,h261,h263,h263p,h264,vp8,opus,outboundcid,prefix from t_pbx_users_voiptrunk" #define QUERY_DID_SQL "select didnumber,type,exten from t_pbx_inbound_did" #define QUERY_DOD_SQL "select dodexten,trunkname,trunk from v_extension_trunk_dod" 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; // 返回成功 } // mysql> desc users_voiptrunk; // +---------------+--------------+------+-----+---------------------+----------------+ // | Field | Type | Null | Key | Default | Extra | // +---------------+--------------+------+-----+---------------------+----------------+ // | _id | int(16) | NO | PRI | NULL | auto_increment | // | _trunkactive | varchar(3) | NO | | yes | | // | _trunk | varchar(64) | NO | UNI | | | // | _trunkname | varchar(64) | NO | UNI | NULL | | // | _trunkstyle | varchar(64) | NO | | | | // | _host | varchar(64) | NO | | yes | | // | _port | varchar(8) | NO | | | | // | _voipusername | varchar(64) | NO | | | | // | _authuser | varchar(64) | NO | | | | // | _fromuser | varchar(64) | NO | | | | // | _fromdomain | varchar(64) | NO | | | | // | _contact | varchar(64) | NO | | | | // | _voipsecret | varchar(128) | NO | | | | // | _outboundcid | varchar(32) | NO | | | | // | _prefix | varchar(16) | NO | | | | // | _qualify | varchar(8) | NO | | 2000 | | // | _qualifyfreq | varchar(8) | NO | | 60 | | // | _transport | varchar(16) | NO | | udp | | // | _context | varchar(32) | NO | | default | | // | _hasexten | varchar(3) | NO | | yes | | // | _dtmfmode | varchar(8) | NO | | rfc2833 | | // | _videosupport | varchar(3) | NO | | no | | // | _encryption | varchar(3) | NO | | yes | | // | _srtpcapable | varchar(3) | NO | | no | | // | _prack | varchar(3) | NO | | no | | // | _alaw | int(1) | NO | | 1 | | // | _ulaw | int(1) | NO | | 1 | | // | _g722 | int(1) | NO | | 0 | | // | _g729 | int(1) | NO | | 0 | | // | _g726 | int(1) | NO | | 0 | | // | _gsm | int(1) | NO | | 0 | | // | _speex | int(1) | NO | | 0 | | // | _h261 | int(1) | NO | | 0 | | // | _h263 | int(1) | NO | | 0 | | // | _h263p | int(1) | NO | | 0 | | // | _h264 | int(1) | NO | | 0 | | // | _vp8 | int(1) | YES | | 0 | | // | _opus | int(1) | YES | | 0 | | // | _recordin | varchar(32) | NO | | | | // | _recordout | varchar(32) | NO | | | | // +---------------+--------------+------+-----+---------------------+----------------+ // 55 rows in set (0.00 sec) typedef struct trunk { char trunkactive[MINI_SIZE]; //0 //* char trunkstyle[MINI_SIZE]; //1 //* char trunk[MINI_SIZE]; //2 //* char host[MINI_SIZE]; //3 //* char port[MINI_SIZE]; //4 //* char outbound_proxy[MINI_SIZE];//5 char voipusername[MINI_SIZE]; //6 //* char authuser[MINI_SIZE]; //7 char fromuser[MINI_SIZE]; //8 char fromdomain[MINI_SIZE]; //9 char contact[MINI_SIZE]; //10 char voipsecret[MINI_SIZE]; //11 //* char qualify[MINI_SIZE]; //12 char qualifyfreq[MINI_SIZE]; //13 char transport[MINI_SIZE]; //14 char context[MINI_SIZE]; //15 //* char dtmfmode[MINI_SIZE]; //16 char videosupport[MINI_SIZE]; //17 char encryption[MINI_SIZE]; //18 char srtpcapable[MINI_SIZE]; //19 char alaw[TINY_SIZE]; //20 char ulaw[TINY_SIZE]; //21 char g722[TINY_SIZE]; //22 char g729[TINY_SIZE]; //23 char g726[TINY_SIZE]; //24 char gsm[TINY_SIZE]; //25 char speex[TINY_SIZE]; //26 char h261[TINY_SIZE]; //27 char h263[TINY_SIZE]; //28 char h263p[TINY_SIZE]; //29 char h264[TINY_SIZE]; //30 char vp8[TINY_SIZE]; //31 char opus[TINY_SIZE]; //32 char outboundcid[MINI_SIZE]; //33 char prefix[MINI_SIZE]; //34 char allow[MIDLE_SIZE]; char contact_uri[MIDLE_SIZE]; char match[MINI_SIZE]; char identify_by[MINI_SIZE]; } TrunkObject; typedef struct did { char didnumber[MINI_SIZE]; char type[MINI_SIZE]; char exten[MINI_SIZE]; } DidObject; typedef struct dod { char dodexten[MINI_SIZE]; char trunkname[MINI_SIZE]; char trunk[MINI_SIZE]; } DodObject; int main(int argc, char **argv) { if (argc == 2){ if (strcmp(argv[1], "-v") == 0) { printf("%s: %s\n", argv[0], VERSION); return EXIT_SUCCESS; }else if (strcmp(argv[1], "trunk") == 0 || strcmp(argv[1], "did") == 0 || strcmp(argv[1], "dod") == 0){ }else{ printf("Usage:%s -v | trunk | did | dod\n", argv[0]); return EXIT_SUCCESS; } }else{ printf("Usage:%s -v | trunk | did | dod\n", argv[0]); return EXIT_SUCCESS; } 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 (strcmp(argv[1], "trunk") == 0){ if (executesql(QUERY_TRUNK_SQL)){ print_mysql_error(NULL); exit(1); } g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集 // int iNum_rows = mysql_num_rows(g_res); // 得到记录的行数 // int iNum_fields = mysql_num_fields(g_res); // 得到记录的列数 TrunkObject trunkObject[MAX_TRUNK_SIZE]; memset(trunkObject, 0, sizeof(trunkObject)); char registrationString[MIDLE_SIZE]; int trunk_size = 0; while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集 // printf("%s\t%s\n", g_row[0], g_row[1]); // 第一,第二字段 if (g_row[0] == NULL || strcmp(g_row[0], "yes") != 0 || g_row[1] == NULL || g_row[2] == NULL || g_row[3] == NULL \ || g_row[4] == NULL || g_row[15] == NULL){ printf("some feild is empty!\n"); continue; } // trunkactive,trunkstyle,trunk,host,port,outbound_proxy,voipusername,authuser,\ fromuser,fromdomain,contact,voipsecret,qualify,qualifyfreq,transport,context,\ ,dtmfmode,videosupport,encryption,srtpcapable,alaw,ulaw,g722,g729,g726,gsm\ ,speex,h261,h263,h263p,h264,vp8,opus,outboundcid,prefix strcpy(trunkObject[trunk_size].trunkactive, g_row[0]); strcpy(trunkObject[trunk_size].trunkstyle, g_row[1]); strcpy(trunkObject[trunk_size].trunk, g_row[2]); strcpy(trunkObject[trunk_size].host, g_row[3]); strcpy(trunkObject[trunk_size].port, g_row[4]); if(g_row[5] != NULL){ sprintf(trunkObject[trunk_size].outbound_proxy,"sip:%s\\;lr", g_row[5]); strcpy(trunkObject[trunk_size].match, g_row[5]); }else{ strcpy(trunkObject[trunk_size].outbound_proxy, ""); strcpy(trunkObject[trunk_size].match, trunkObject[trunk_size].host); } strcpy(trunkObject[trunk_size].voipusername, g_row[6]); if (g_row[7] != NULL){ strcpy(trunkObject[trunk_size].authuser, g_row[7]); }else{ strcpy(trunkObject[trunk_size].authuser, trunkObject[trunk_size].voipusername); } if (g_row[8] != NULL){ strcpy(trunkObject[trunk_size].fromuser, g_row[8]); }else{ strcpy(trunkObject[trunk_size].fromuser, trunkObject[trunk_size].voipusername); } if (g_row[9] != NULL){ strcpy(trunkObject[trunk_size].fromdomain, g_row[9]); }else{ strcpy(trunkObject[trunk_size].fromdomain, trunkObject[trunk_size].host); } if (g_row[10] != NULL){ strcpy(trunkObject[trunk_size].contact, g_row[10]); }else{ strcpy(trunkObject[trunk_size].contact, trunkObject[trunk_size].voipusername); } strcpy(trunkObject[trunk_size].voipsecret, g_row[11]); strcpy(trunkObject[trunk_size].qualify, g_row[12]); strcpy(trunkObject[trunk_size].qualifyfreq, g_row[13]); strcpy(trunkObject[trunk_size].transport, g_row[14]); strcpy(trunkObject[trunk_size].context, g_row[15]); strcpy(trunkObject[trunk_size].dtmfmode, g_row[16]); strcpy(trunkObject[trunk_size].videosupport, g_row[17]); strcpy(trunkObject[trunk_size].encryption, g_row[18]); strcpy(trunkObject[trunk_size].srtpcapable, g_row[19]); strcpy(trunkObject[trunk_size].alaw, g_row[20]); strcpy(trunkObject[trunk_size].ulaw, g_row[21]); strcpy(trunkObject[trunk_size].g722, g_row[22]); strcpy(trunkObject[trunk_size].g729, g_row[23]); strcpy(trunkObject[trunk_size].g726, g_row[24]); strcpy(trunkObject[trunk_size].gsm, g_row[25]); strcpy(trunkObject[trunk_size].speex, g_row[26]); strcpy(trunkObject[trunk_size].h261, g_row[27]); strcpy(trunkObject[trunk_size].h263, g_row[28]); strcpy(trunkObject[trunk_size].h263p, g_row[29]); strcpy(trunkObject[trunk_size].h264, g_row[30]); strcpy(trunkObject[trunk_size].vp8, g_row[31]); strcpy(trunkObject[trunk_size].opus, g_row[32]); if (g_row[33] != NULL){ strcpy(trunkObject[trunk_size].outboundcid, g_row[33]); }else{ strcpy(trunkObject[trunk_size].outboundcid, ""); } if (g_row[34] != NULL){ strcpy(trunkObject[trunk_size].prefix, g_row[34]); }else{ strcpy(trunkObject[trunk_size].prefix, ""); } if(strlen(trunkObject[trunk_size].voipusername) > 0) { sprintf(trunkObject[trunk_size].contact_uri,"sip:%s@%s", trunkObject[trunk_size].voipusername, trunkObject[trunk_size].host); strcpy(trunkObject[trunk_size].identify_by, "auth_username"); } else { sprintf(trunkObject[trunk_size].contact_uri,"sip:%s", trunkObject[trunk_size].host); strcpy(trunkObject[trunk_size].identify_by, "ip"); } memset(trunkObject[trunk_size].allow, '\0', sizeof(trunkObject[trunk_size].allow)); if (strcmp(trunkObject[trunk_size].alaw, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "alaw,"); strcpy(trunkObject[trunk_size].allow, "alaw,"); } if (strcmp(trunkObject[trunk_size].ulaw, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sulaw,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "ulaw,"); } if (strcmp(trunkObject[trunk_size].g722, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sg722,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "g722,"); } if (strcmp(trunkObject[trunk_size].g729, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sg729,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "g729,"); } if (strcmp(trunkObject[trunk_size].g726, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sg726,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "g726,"); } if (strcmp(trunkObject[trunk_size].speex, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sspeex,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "speex,"); } if (strcmp(trunkObject[trunk_size].gsm, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sgsm,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "gsm,"); } if (strcmp(trunkObject[trunk_size].h261, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sh261,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "h261,"); } if (strcmp(trunkObject[trunk_size].h263, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sh263,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "h263,"); } if (strcmp(trunkObject[trunk_size].h263p, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sh263p,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "h263p,"); } if (strcmp(trunkObject[trunk_size].h264, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sh264,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "h264,"); } if (strcmp(trunkObject[trunk_size].vp8, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%svp8,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "vp8,"); } if (strcmp(trunkObject[trunk_size].opus, "1") == 0){ //sprintf(trunkObject[trunk_size].allow, "%sopus,", trunkObject[trunk_size].allow); strcat(trunkObject[trunk_size].allow, "opus,"); } // printf("trunk:%s\n", trunkObject[trunk_size].trunk); trunkObject[trunk_size].allow[strlen(trunkObject[trunk_size].allow)-1]='\0'; // printf("allow:%s\n", trunkObject[trunk_size].allow); trunk_size++; } // printf("trunk_size:%d\n", trunk_size); FILE *conf_trunk_regs = fopen(PJSIP_TRUNK_REGS, "w+"); FILE *conf_trunk_auths = fopen(PJSIP_TRUNK_AUTHS, "w+"); FILE *conf_trunk_eps = fopen(PJSIP_TRUNK_EPS, "w+"); FILE *conf_trunk_aors = fopen(PJSIP_TRUNK_AORS, "w+"); FILE *conf_trunk_idfs = fopen(PJSIP_TRUNK_IDFS, "w+"); if (conf_trunk_regs == NULL){ perror("Open sip registration conf file Error: "); exit(1); } fprintf(conf_trunk_regs, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_trunk_regs.conf (/etc/asterisk/pjsip_trunk_regs.conf)\n\ ;! Generator: Generator Trunk Registrations\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_trunk_auths == NULL){ perror("Open trunk auth file Error: "); exit(1); } fprintf(conf_trunk_auths, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_trunk_auths.conf (/etc/asterisk/pjsip_trunk_auths.conf)\n\ ;! Generator: Generator Trunk Auths\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_trunk_eps == NULL){ perror("Open trunk endpoint conf file Error: "); exit(1); } fprintf(conf_trunk_eps, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_trunk_eps.conf (/etc/asterisk/pjsip_trunk_eps.conf)\n\ ;! Generator: Generator Trunk Endpoints\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_trunk_aors == NULL){ perror("Open trunk aor conf file Error: "); exit(1); } fprintf(conf_trunk_aors, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_trunk_aors.conf (/etc/asterisk/pjsip_trunk_aors.conf)\n\ ;! Generator: Generator Trunk Aors\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_trunk_idfs == NULL){ perror("Open trunk identify conf file Error: "); exit(1); } fprintf(conf_trunk_idfs, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: pjsip_trunk_idfs.conf (/etc/asterisk/pjsip_trunk_idfs.conf)\n\ ;! Generator: Generator Trunk Identify\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); int i = 0; while(i < trunk_size){ if(strcmp(trunkObject[i].trunkactive, "yes") == 0){ if(strcmp(trunkObject[i].trunkstyle, "Peer") != 0 && strlen(trunkObject[i].voipusername) > 0) { fprintf(conf_trunk_regs, "\ [%s]\n\ type = registration\n\ transport = transport-%s\n\ outbound_auth = %s_auth\n\ server_uri = sip:%s:%s\n\ client_uri = sip:%s@%s\n\ contact_user = %s\n\ retry_interval = 60\n\ forbidden_retry_interval = 600\n\ expiration = 3600\n\ line = yes\n\ outbound_proxy = %s\n\ endpoint = %s\ \n", \ trunkObject[i].trunk,\ trunkObject[i].transport,\ trunkObject[i].trunk,\ trunkObject[i].host,\ trunkObject[i].port,\ trunkObject[i].voipusername,\ trunkObject[i].fromdomain,\ trunkObject[i].contact,\ trunkObject[i].outbound_proxy,\ trunkObject[i].trunk\ ); fprintf(conf_trunk_auths, "\ [%s_auth]\n\ type = auth\n\ auth_type = userpass\n\ password = %s\n\ username = %s\n\ \n", \ trunkObject[i].trunk,\ trunkObject[i].voipsecret,\ trunkObject[i].authuser\ ); fprintf(conf_trunk_eps, "\ [%s]\n\ type = endpoint\n\ transport = transport-%s\n\ context = %s\n\ disallow = all\n\ allow = %s\n\ aors = %s\n\ direct_media = yes\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 = %s\n\ force_rport = no\n\ ice_support = no\n\ identify_by = %s\n\ outbound_auth = %s_auth\n\ outbound_proxy = %s\n\ rewrite_contact = no\n\ rtp_symmetric = no\n\ send_diversion = no\n\ send_pai = no\n\ send_rpid = no\n\ media_encryption = no\n\ inband_progress = no\n\ device_state_busy_at = 0\n\ allow_transfer = yes\n\ allow_subscribe = yes\n\ sdp_session = IP Audio Center\n\ tos_audio = ef\n\ tos_video = AF41\n\ from_domain = %s\n\ from_user = %s\n\ cos_audio = 5\n\ cos_video = 4\n\ user_eq_phone = no\n\ rtp_keepalive = 60\n\ rtp_timeout = 60\n\ rtp_timeout_hold = 300\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",\ trunkObject[i].trunk,\ trunkObject[i].transport,\ trunkObject[i].context,\ trunkObject[i].allow,\ trunkObject[i].trunk,\ trunkObject[i].dtmfmode,\ trunkObject[i].identify_by,\ trunkObject[i].trunk,\ trunkObject[i].outbound_proxy,\ trunkObject[i].fromdomain,\ trunkObject[i].fromuser\ ); fprintf(conf_trunk_aors, "\ [%s]\n\ type=aor\n\ contact=%s\n\ default_expiration=600\n\ max_contacts=100\n\ minimum_expiration=60\n\ remove_existing=no\n\ qualify_frequency=%s\n\ authenticate_qualify=no\n\ maximum_expiration=3600\n\ outbound_proxy=%s\n\ qualify_timeout=%s\n\ \n",\ trunkObject[i].trunk,\ trunkObject[i].contact_uri,\ trunkObject[i].qualifyfreq,\ trunkObject[i].outbound_proxy,\ trunkObject[i].qualify\ ); fprintf(conf_trunk_idfs, "\ [%s]\n\ type=identify\n\ endpoint=%s\n\ match=%s\n\ ",\ trunkObject[i].trunk,\ trunkObject[i].trunk,\ trunkObject[i].match\ ); } else { fprintf(conf_trunk_auths, "\ [%s_auth]\n\ type = auth\n\ auth_type = userpass\n\ password = %s\n\ username = %s\n\ \n", \ trunkObject[i].voipusername,\ trunkObject[i].voipsecret,\ trunkObject[i].authuser\ ); fprintf(conf_trunk_eps, "\ [%s]\n\ type = endpoint\n\ transport = transport-%s\n\ context = %s\n\ disallow = all\n\ allow = %s\n\ aors = %s\n\ direct_media = yes\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 = %s\n\ force_rport = no\n\ ice_support = no\n\ identify_by = %s\n\ outbound_auth = %s_auth\n\ rewrite_contact = no\n\ rtp_symmetric = no\n\ send_diversion = no\n\ send_pai = no\n\ send_rpid = no\n\ media_encryption = no\n\ inband_progress = no\n\ device_state_busy_at = 0\n\ allow_transfer = yes\n\ allow_subscribe = yes\n\ sdp_session = IP Audio Center\n\ tos_audio = ef\n\ tos_video = AF41\n\ from_user = %s\n\ cos_audio = 5\n\ cos_video = 4\n\ user_eq_phone = no\n\ rtp_keepalive = 60\n\ rtp_timeout = 60\n\ rtp_timeout_hold = 300\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",\ trunkObject[i].voipusername,\ trunkObject[i].transport,\ trunkObject[i].context,\ trunkObject[i].allow,\ trunkObject[i].voipusername,\ trunkObject[i].dtmfmode,\ trunkObject[i].identify_by,\ trunkObject[i].voipusername,\ trunkObject[i].fromuser\ ); fprintf(conf_trunk_aors, "\ [%s]\n\ type=aor\n\ default_expiration=600\n\ max_contacts=1\n\ minimum_expiration=60\n\ remove_existing=yes\n\ qualify_frequency=%s\n\ authenticate_qualify=no\n\ maximum_expiration=3600\n\ qualify_timeout=%s\n\ \n",\ trunkObject[i].voipusername,\ trunkObject[i].qualifyfreq,\ trunkObject[i].qualify\ ); } } i++; } fclose(conf_trunk_aors); fclose(conf_trunk_auths); fclose(conf_trunk_eps); fclose(conf_trunk_idfs); fclose(conf_trunk_regs); FILE *global_fp = fopen(TRUNK_GLOBAL_FILE, "w+"); if (global_fp == NULL){ perror("Open trunk conf file Error: "); exit(1); } fprintf(global_fp, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: extensions_trunks_global_custom.conf (/etc/asterisk/extensions_trunks_global_custom.conf)\n\ ;! Generator: Generator Trunk\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); i = 0; // CID_$trunk = $outboundcid // P_CID_$trunk=$trunkcid_preferred // $trunk=SIP/$trunk/$prefix if (trunk_size > 0){ while(i < trunk_size){ fprintf(global_fp, "\ CID_%s = %s\n\ %s=SIP/%s/%s\ \n", \ trunkObject[i].trunk,\ trunkObject[i].outboundcid,\ trunkObject[i].trunk,\ trunkObject[i].trunk,\ trunkObject[i].prefix\ ); i++; } } fclose(global_fp); }else if (strcmp(argv[1], "did") == 0){ if (executesql(QUERY_DID_SQL)){ print_mysql_error(NULL); exit(1); } g_res = mysql_store_result(g_conn); DidObject didObject[MAX_TRUNK_SIZE]; memset(didObject, 0, sizeof(didObject)); int did_size = 0; while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集 if (g_row[0] != NULL){ strcpy(didObject[did_size].didnumber, g_row[0]); }else{ strcpy(didObject[did_size].didnumber, ""); } if (g_row[1] != NULL){ strcpy(didObject[did_size].type, g_row[1]); }else{ strcpy(didObject[did_size].type, ""); } if (g_row[2] != NULL){ strcpy(didObject[did_size].exten, g_row[2]); }else{ strcpy(didObject[did_size].exten, ""); } did_size++; } FILE *did_fp = fopen(DID_CONF_FILE, "w+"); if (did_fp == NULL){ perror("Open did conf file Error: "); exit(1); } fprintf(did_fp, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: extensions_extendid_custom.conf (/etc/asterisk/extensions_extendid_custom.conf)\n\ ;! Generator: Generator did\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); int m = 0; if (did_size > 0){ while(m < did_size){ if (strcmp(didObject[m].didnumber, "") != 0 && strcmp(didObject[m].type, "") != 0){ //exten => _didnumber,1,Goto(default,_destexten,1) if(strcmp(didObject[m].type, "extension") == 0){ fprintf(did_fp, "\ exten => %s,1,Goto(default,%s,1)\ \n", \ didObject[m].didnumber,\ didObject[m].exten\ ); }else if(strcmp(didObject[m].type, "ivr") == 0){ fprintf(did_fp, "\ exten => %s,1,Goto(voicemenu-custom-%s,%s,1)\ \n", \ didObject[m].didnumber,\ didObject[m].exten,\ didObject[m].exten\ ); }else if(strcmp(didObject[m].type, "group") == 0){ fprintf(did_fp, "\ exten => %s,1,Goto(paging-group-%s,%s,1)\ \n", \ didObject[m].didnumber,\ didObject[m].exten,\ didObject[m].exten\ ); }else if(strcmp(didObject[m].type, "trigger") == 0){ fprintf(did_fp, "\ exten => %s,1,Goto(call-trigger,${EXTEN},1)\ \n", \ didObject[m].didnumber\ ); }else if(strcmp(didObject[m].type, "user") == 0){ int id = 100000 + atoi(didObject[m].exten); fprintf(did_fp, "\ exten => %s,1,Goto(manager-queue-%d,s,1)\ \n", \ didObject[m].didnumber,\ id\ ); } } m++; } } fclose(did_fp); }else if (strcmp(argv[1], "dod") == 0){ if (executesql(QUERY_DOD_SQL)){ print_mysql_error(NULL); exit(1); } g_res = mysql_store_result(g_conn); DodObject dodObject[MAX_TRUNK_SIZE]; memset(dodObject, 0, sizeof(dodObject)); int dod_size = 0; while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集 if (g_row[0] != NULL){ strcpy(dodObject[dod_size].dodexten, g_row[0]); }else{ strcpy(dodObject[dod_size].dodexten, ""); } if (g_row[1] != NULL){ strcpy(dodObject[dod_size].trunkname, g_row[1]); }else{ strcpy(dodObject[dod_size].trunkname, ""); } if (g_row[2] != NULL){ strcpy(dodObject[dod_size].trunk, g_row[2]); }else{ strcpy(dodObject[dod_size].trunk, ""); } dod_size++; } FILE *dod_fp = fopen(DOD_CONF_FILE, "w+"); if (dod_fp == NULL){ perror("Open dod conf file Error: "); exit(1); } fprintf(dod_fp, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: extensions_extendod_custom.conf (/etc/asterisk/extensions_extendod_custom.conf)\n\ ;! Generator: Generator dod\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); int n = 0; if (dod_size > 0){ while(n < dod_size){ if (strcmp(dodObject[n].dodexten, "") != 0 && strcmp(dodObject[n].trunk, "") != 0){ //exten => _X./$dodexten,1, Macro(trunkdial-failover,,,${EXTEN},$trunk${EXTEN:0}) fprintf(dod_fp, "\ exten => _X./%s,1, Gosub(trunkdial-failover,s,1(,,,${EXTEN},${%s}${EXTEN:0}))\ \n", \ dodObject[n].dodexten,\ dodObject[n].trunk\ ); } n++; } } fclose(dod_fp); }else{ printf("Usage:%s [-v] | trunk | did | dod\n", argv[0]); } mysql_free_result(g_res); // 释放结果集 mysql_close(g_conn); // 关闭链接 return EXIT_SUCCESS; }