1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039 |
- /*
- ============================================================================
- Name : generate_trunk_conf.sh
- Author : ssc
- Version : v1.0
- Copyright : ZYCOO copyright
- Description : Generate trunk info from mysql to turnk conf file
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <assert.h>
- #include <time.h>
- #include <ctype.h>
- #include <mysql/mysql.h>
- 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]);
- if(strcmp(g_row[16],"rfc2833") == 0)
- strcpy(trunkObject[trunk_size].dtmfmode, "rfc4733");
- else
- 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)
- {
- if(strlen(trunkObject[i].voipusername) > 0)
- {
- fprintf(conf_trunk_regs, "\
- [%s]\n\
- type = registration\n\
- transport = transport-%s\n\
- outbound_auth = auth_%s\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, "\
- [auth_%s]\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 = auth_%s\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_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 =\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].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, "\
- [auth_%s]\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\
- auth = auth_%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 = auth_%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_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].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=%s\
- \n", \
- trunkObject[i].trunk,\
- trunkObject[i].outboundcid,\
- trunkObject[i].trunk,\
- trunkObject[i].trunk\
- );
-
- 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){
- if(strcmp(didObject[m].type, "extension") == 0){
- fprintf(did_fp, "\
- exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\
- \n", \
- didObject[m].didnumber,\
- didObject[m].exten,\
- 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;
- }
|