generate_pjsip_conf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == =
  3. Name : generate_paging_conf.sh
  4. Author : ssc
  5. Version : v1.0
  6. Copyright : ZYCOO copyright
  7. Description : Generate paging info from mysql to paging conf file
  8. = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == =
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <assert.h>
  15. #include <time.h>
  16. #include <ctype.h>
  17. #include <mysql/mysql.h>
  18. MYSQL *g_conn; // mysql 连接
  19. MYSQL_RES *d_res; // mysql device记录集
  20. MYSQL_ROW d_row; // 字符串数组,mysql 记录行
  21. MYSQL_RES *u_res; // mysql user记录集
  22. MYSQL_ROW u_row; // 字符串数组,mysql 记录行
  23. #define MAX_TRUNK_SIZE 256
  24. #define MAX_SIZE 2048
  25. #define MIDLE_SIZE 512
  26. #define MINI_SIZE 64
  27. #define PJSIP_DEV_AORS "/etc/asterisk/pjsip_dev_aors.conf"
  28. #define PJSIP_DEV_AUTHS "/etc/asterisk/pjsip_dev_auths.conf"
  29. #define PJSIP_DEV_EPS "/etc/asterisk/pjsip_dev_eps.conf"
  30. #define PJSIP_USR_AORS "/etc/asterisk/pjsip_usr_aors.conf"
  31. #define PJSIP_USR_AUTHS "/etc/asterisk/pjsip_usr_auths.conf"
  32. #define PJSIP_USR_EPS "/etc/asterisk/pjsip_usr_eps.conf"
  33. #define EXTEN_HINTS "/etc/asterisk/extensions_hints_exten.conf"
  34. #define KEYVALLEN 100
  35. #define VERSION "V1.0.1"
  36. #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"
  37. #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"
  38. char g_host_name[MINI_SIZE];
  39. char g_user_name[MINI_SIZE];
  40. char g_password[MINI_SIZE];
  41. char g_db_name[MINI_SIZE];
  42. const unsigned int g_db_port = 3306;
  43. char * mytime(){
  44. time_t my_time;
  45. time(&my_time);
  46. char *time_string = ctime(&my_time);
  47. if (time_string[strlen(time_string) - 1] == '\n')
  48. {
  49. time_string[strlen(time_string) - 1] = '\0';
  50. }
  51. return time_string;
  52. }
  53. void print_mysql_error(const char *msg) { // 打印最后一次错误
  54. if (msg)
  55. printf("%s: %s\n", msg, mysql_error(g_conn));
  56. else
  57. puts(mysql_error(g_conn));
  58. }
  59. int executesql(const char * sql) {
  60. /*query the database according the sql*/
  61. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  62. return -1; // 表示失败
  63. return 0; // 成功执行
  64. }
  65. int init_mysql() { // 初始化连接
  66. // init the database connection
  67. g_conn = mysql_init(NULL);
  68. /* connect the database */
  69. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  70. return -1;
  71. // 是否连接已经可用
  72. if (executesql("set names utf8")) // 如果失败
  73. return -1;
  74. return 0; // 返回成功
  75. }
  76. /*
  77. ** DATABASE TABLE "t_pbx_users_extension"
  78. `id` int(16) NOT NULL AUTO_INCREMENT,
  79. `exten` varchar(32) NOT NULL,
  80. `cid_number` varchar(32) NOT NULL DEFAULT '',
  81. `qualify_timeout` smallint NOT NULL DEFAULT 60,
  82. `qualify_frequency` smallint NOT NULL DEFAULT 120,
  83. `call-limit` varchar(2) NOT NULL DEFAULT '5',
  84. `host` varchar(16) NOT NULL DEFAULT 'dynamic',
  85. `context` varchar(64) NOT NULL DEFAULT '',
  86. `fullname` varchar(64) NOT NULL DEFAULT '',
  87. `secret` varchar(128) NOT NULL DEFAULT '',
  88. `outboundcid` varchar(32) NOT NULL DEFAULT '',
  89. `email` varchar(64) NOT NULL DEFAULT '',
  90. `nat` varchar(32) NOT NULL DEFAULT 'force_rport,comedia',
  91. `directmedia` varchar(3) NOT NULL DEFAULT 'no',
  92. `dtmfmode` varchar(8) NOT NULL DEFAULT 'rfc2833',
  93. `videosupport` varchar(3) NOT NULL DEFAULT 'no',
  94. `transport` varchar(11) NOT NULL DEFAULT 'udp',
  95. `encryption` varchar(3) NOT NULL DEFAULT 'no',
  96. `srtpcapable` varchar(3) NOT NULL DEFAULT 'no',
  97. `default_expiration` smallint NOT NULL DEFAULT 600,
  98. `ulaw` bool NOT NULL DEFAULT 1,
  99. `alaw` bool NOT NULL DEFAULT 1,
  100. `g722` bool NOT NULL DEFAULT 1,
  101. `g729` bool NOT NULL DEFAULT 0,
  102. `g726` bool NOT NULL DEFAULT 0,
  103. `gsm` bool NOT NULL DEFAULT 0,
  104. `speex` bool NOT NULL DEFAULT 0,
  105. `h261` bool NOT NULL DEFAULT 0,
  106. `h263` bool NOT NULL DEFAULT 0,
  107. `h263p` bool NOT NULL DEFAULT 0,
  108. `h264` bool NOT NULL DEFAULT 0,
  109. `vp8` bool DEFAULT 0,
  110. `opus` bool DEFAULT 0,
  111. `recordin` varchar(32) NOT NULL DEFAULT '1',
  112. `recordout` varchar(32) NOT NULL DEFAULT '1',
  113. */
  114. int main(int argc, char **argv) {
  115. char codecs[64];
  116. memset(g_host_name, 0, sizeof(g_host_name));
  117. memset(g_user_name, 0, sizeof(g_user_name));
  118. memset(g_password, 0, sizeof(g_password));
  119. memset(g_db_name, 0, sizeof(g_db_name));
  120. strcpy(g_host_name,getenv("MYSQL"));
  121. strcpy(g_user_name,getenv("MYSQL_USER"));
  122. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  123. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  124. if (init_mysql()){
  125. print_mysql_error(NULL);
  126. exit(1);
  127. }
  128. if (executesql(QUERY_SIP_DEV_SQL)){
  129. print_mysql_error(NULL);
  130. exit(1);
  131. }
  132. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  133. FILE *conf_dev_aors = fopen(PJSIP_DEV_AORS, "w+");
  134. FILE *conf_dev_auths = fopen(PJSIP_DEV_AUTHS, "w+");
  135. FILE *conf_dev_eps = fopen(PJSIP_DEV_EPS, "w+");
  136. FILE *conf_exten_hints = fopen(EXTEN_HINTS, "w+");
  137. if (conf_dev_aors == NULL){
  138. perror("Open aors conf file Error: ");
  139. exit(1);
  140. }
  141. fprintf(conf_dev_aors, ";!\n\
  142. ;! Automatically generated configuration file\n\
  143. ;! Filename: pjsip_dev_aors.conf (/etc/asterisk/pjsip_dev_aors.conf)\n\
  144. ;! Generator: Generator PJSIP AORS\n\
  145. ;! Creation Date: %s\n\
  146. ;!\n\n\
  147. ",\
  148. mytime()\
  149. );
  150. if (conf_dev_auths == NULL){
  151. perror("Open auths conf file Error: ");
  152. exit(1);
  153. }
  154. fprintf(conf_dev_auths, ";!\n\
  155. ;! Automatically generated configuration file\n\
  156. ;! Filename: pjsip_dev_auths.conf (/etc/asterisk/pjsip_dev_auths.conf)\n\
  157. ;! Generator: Generator PJSIP AUTHS\n\
  158. ;! Creation Date: %s\n\
  159. ;!\n\n\
  160. ",\
  161. mytime()\
  162. );
  163. if (conf_dev_eps == NULL){
  164. perror("Open endpoint conf file Error: ");
  165. exit(1);
  166. }
  167. fprintf(conf_dev_eps, ";!\n\
  168. ;! Automatically generated configuration file\n\
  169. ;! Filename: pjsip_dev_auths.conf (/etc/asterisk/pjsip_dev_eps.conf)\n\
  170. ;! Generator: Generator PJSIP ENDPOINTS\n\
  171. ;! Creation Date: %s\n\
  172. ;!\n\n\
  173. ",\
  174. mytime()\
  175. );
  176. fprintf(conf_exten_hints, ";!\n\
  177. ;! Automatically generated configuration file\n\
  178. ;! Filename: extensions_hints_exten.conf (/etc/asterisk/extensions_hints_exten.conf)\n\
  179. ;! Generator: Generator Extension Hints\n\
  180. ;! Creation Date: %s\n\
  181. ;!\n\n\
  182. [channelhints_exten]\n\
  183. ",\
  184. mytime()\
  185. );
  186. while ((d_row = mysql_fetch_row(d_res)))
  187. { // 打印结果集
  188. if (d_row[1] == NULL || d_row[6] == NULL || d_row[9] == NULL)
  189. {
  190. printf("some feild is empty!\n");
  191. continue;
  192. }
  193. fprintf(conf_dev_aors, "\
  194. [%s]\n\
  195. type = aor\n\
  196. max_contacts = 1\n\
  197. remove_existing = yes\n\
  198. remove_unavailable = yes\n\
  199. default_expiration = %s\n\
  200. qualify_timeout = %s\n\
  201. qualify_frequency = %s\n\
  202. authenticate_qualify = no\n\
  203. support_path = yes\n\
  204. \n",\
  205. d_row[1],\
  206. d_row[10],\
  207. d_row[3],\
  208. d_row[4]\
  209. );
  210. fprintf(conf_dev_auths, "\
  211. [%s]\n\
  212. type = auth\n\
  213. auth_type = userpass\n\
  214. password = %s\n\
  215. username = %s\n\
  216. \n",\
  217. d_row[1],\
  218. d_row[7],\
  219. d_row[1]\
  220. );
  221. memset(codecs, 0, sizeof(codecs));
  222. if(d_row[11])
  223. {
  224. strcpy(codecs,"ulaw");
  225. }
  226. if(d_row[12])
  227. {
  228. if(strlen(codecs))
  229. {
  230. strcat(codecs,",alaw");
  231. }
  232. else
  233. {
  234. strcpy(codecs,"alaw");
  235. }
  236. }
  237. if(d_row[13])
  238. {
  239. if(strlen(codecs))
  240. {
  241. strcat(codecs,",g722");
  242. }
  243. else
  244. {
  245. strcpy(codecs,"g722");
  246. }
  247. }
  248. if(d_row[14])
  249. {
  250. if(strlen(codecs))
  251. {
  252. strcat(codecs,",opus");
  253. }
  254. else
  255. {
  256. strcpy(codecs,"opus");
  257. }
  258. }
  259. if(d_row[15])
  260. {
  261. if(strlen(codecs))
  262. {
  263. strcat(codecs,",h264");
  264. }
  265. else
  266. {
  267. strcpy(codecs,"h264");
  268. }
  269. }
  270. fprintf(conf_dev_eps, "\
  271. [%s]\n\
  272. type = endpoint\n\
  273. transport = transport-%s\n\
  274. aors = %s\n\
  275. auth = %s\n\
  276. context = %s\n\
  277. allow = %s\n\
  278. direct_media = no\n\
  279. connected_line_method = update\n\
  280. direct_media_method = update\n\
  281. direct_media_glare_mitigation = incoming\n\
  282. disable_direct_media_on_nat = yes\n\
  283. dtmf_mode = rfc4733\n\
  284. force_rport = yes\n\
  285. ice_support = no\n\
  286. identify_by = username\n\
  287. moh_suggest = default\n\
  288. outbound_proxy = \n\
  289. rewrite_contact = no\n\
  290. rtp_symmetric = no\n\
  291. send_diversion = no\n\
  292. send_pai = no\n\
  293. send_rpid = no\n\
  294. callerid = %s <%s>\n\
  295. media_encryption = no\n\
  296. inband_progress = no\n\
  297. call_group = 1\n\
  298. pickup_group = 1\n\
  299. device_state_busy_at = 0\n\
  300. language = \n\
  301. allow_transfer = yes\n\
  302. allow_subscribe = yes\n\
  303. sdp_session = IP Audio Center\n\
  304. tos_audio = ef\n\
  305. tos_video = AF41\n\
  306. cos_audio = 5\n\
  307. cos_video = 4\n\
  308. media_use_received_transport = no\n\
  309. user_eq_phone = no\n\
  310. rtp_keepalive = 60\n\
  311. rtp_timeout = 60\n\
  312. rtp_timeout_hold = 300\n\
  313. ;acl = remote_extensions_acl\n\
  314. rtcp_mux = no\n\
  315. codec_prefs_incoming_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
  316. codec_prefs_incoming_offer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
  317. codec_prefs_outgoing_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
  318. codec_prefs_outgoing_offer = prefer:pending, operation:union, keep:all, transcode:allow\n\
  319. \n",\
  320. d_row[1],\
  321. d_row[9],\
  322. d_row[1],\
  323. d_row[1],\
  324. d_row[5],\
  325. codecs,\
  326. d_row[6],\
  327. d_row[1]\
  328. );
  329. fprintf(conf_exten_hints, "\
  330. exten => %s,hint,PJSIP/%s,CustomPresence:%s\n\
  331. \n",\
  332. d_row[1],\
  333. d_row[1],\
  334. d_row[1]\
  335. );
  336. }
  337. if (executesql(QUERY_SIP_USR_SQL)){
  338. print_mysql_error(NULL);
  339. exit(1);
  340. }
  341. u_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  342. FILE *conf_usr_aors = fopen(PJSIP_USR_AORS, "w+");
  343. FILE *conf_usr_auths = fopen(PJSIP_USR_AUTHS, "w+");
  344. FILE *conf_usr_eps = fopen(PJSIP_USR_EPS, "w+");
  345. if (conf_usr_aors == NULL){
  346. perror("Open user aors conf file Error: ");
  347. exit(1);
  348. }
  349. fprintf(conf_usr_aors, ";!\n\
  350. ;! Automatically generated configuration file\n\
  351. ;! Filename: pjsip_usr_aors.conf (/etc/asterisk/pjsip_usr_aors.conf)\n\
  352. ;! Generator: Generator PJSIP AORS\n\
  353. ;! Creation Date: %s\n\
  354. ;!\n\n\
  355. ",\
  356. mytime()\
  357. );
  358. if (conf_usr_auths == NULL){
  359. perror("Open user auths conf file Error: ");
  360. exit(1);
  361. }
  362. fprintf(conf_usr_auths, ";!\n\
  363. ;! Automatically generated configuration file\n\
  364. ;! Filename: pjsip_usr_auths.conf (/etc/asterisk/pjsip_usr_auths.conf)\n\
  365. ;! Generator: Generator PJSIP AUTHS\n\
  366. ;! Creation Date: %s\n\
  367. ;!\n\n\
  368. ",\
  369. mytime()\
  370. );
  371. if (conf_usr_eps == NULL){
  372. perror("Open user endpoint conf file Error: ");
  373. exit(1);
  374. }
  375. fprintf(conf_usr_eps, ";!\n\
  376. ;! Automatically generated configuration file\n\
  377. ;! Filename: pjsip_usr_auths.conf (/etc/asterisk/pjsip_usr_eps.conf)\n\
  378. ;! Generator: Generator PJSIP ENDPOINTS\n\
  379. ;! Creation Date: %s\n\
  380. ;!\n\n\
  381. ",\
  382. mytime()\
  383. );
  384. while ((u_row = mysql_fetch_row(u_res)))
  385. { // 打印结果集
  386. if (u_row[1] == NULL || u_row[6] == NULL || u_row[9] == NULL)
  387. {
  388. printf("some feild is empty!\n");
  389. continue;
  390. }
  391. fprintf(conf_usr_aors, "\
  392. [%s]\n\
  393. type = aor\n\
  394. max_contacts = 1\n\
  395. remove_existing = yes\n\
  396. remove_unavailable = yes\n\
  397. default_expiration = %s\n\
  398. qualify_timeout = %s\n\
  399. qualify_frequency = %s\n\
  400. authenticate_qualify = no\n\
  401. support_path = yes\n\
  402. \n",\
  403. u_row[1],\
  404. u_row[10],\
  405. u_row[3],\
  406. u_row[4]\
  407. );
  408. fprintf(conf_usr_auths, "\
  409. [%s]\n\
  410. type = auth\n\
  411. auth_type = userpass\n\
  412. password = %s\n\
  413. username = %s\n\
  414. \n",\
  415. u_row[1],\
  416. u_row[7],\
  417. u_row[1]\
  418. );
  419. memset(codecs, 0, sizeof(codecs));
  420. if(atoi(u_row[11]))
  421. {
  422. strcpy(codecs,"ulaw");
  423. }
  424. if(atoi(u_row[12]))
  425. {
  426. if(strlen(codecs))
  427. {
  428. strcat(codecs,",alaw");
  429. }
  430. else
  431. {
  432. strcpy(codecs,"alaw");
  433. }
  434. }
  435. if(atoi(u_row[13]))
  436. {
  437. if(strlen(codecs))
  438. {
  439. strcat(codecs,",g722");
  440. }
  441. else
  442. {
  443. strcpy(codecs,"g722");
  444. }
  445. }
  446. if(atoi(u_row[14]))
  447. {
  448. if(strlen(codecs))
  449. {
  450. strcat(codecs,",opus");
  451. }
  452. else
  453. {
  454. strcpy(codecs,"opus");
  455. }
  456. }
  457. if(atoi(u_row[15]))
  458. {
  459. if(strlen(codecs))
  460. {
  461. strcat(codecs,",h264");
  462. }
  463. else
  464. {
  465. strcpy(codecs,"h264");
  466. }
  467. }
  468. fprintf(conf_usr_eps, "\
  469. [%s]\n\
  470. type = endpoint\n\
  471. transport = transport-wss\n\
  472. aors = %s\n\
  473. auth = %s\n\
  474. context = %s\n\
  475. allow = %s\n\
  476. direct_media = no\n\
  477. connected_line_method = update\n\
  478. direct_media_method = update\n\
  479. direct_media_glare_mitigation = incoming\n\
  480. disable_direct_media_on_nat = yes\n\
  481. dtmf_mode = rfc4733\n\
  482. force_rport = no\n\
  483. webrtc = yes\n\
  484. identify_by = username\n\
  485. moh_suggest = default\n\
  486. outbound_proxy = \n\
  487. rewrite_contact = no\n\
  488. rtp_symmetric = no\n\
  489. send_diversion = no\n\
  490. send_pai = no \n\
  491. send_rpid = no \n\
  492. callerid = %s <%s>\n\
  493. use_avpf = yes\n\
  494. inband_progress = no\n\
  495. call_group = 1\n\
  496. pickup_group = 1\n\
  497. device_state_busy_at = 1\n\
  498. language = en\n\
  499. allow_transfer = yes\n\
  500. allow_subscribe = yes\n\
  501. sdp_session = IP Audio Center\n\
  502. tos_audio = ef\n\
  503. tos_video = AF41\n\
  504. cos_audio = 5\n\
  505. cos_video = 4\n\
  506. dtls_cert_file = %s\n\
  507. dtls_ca_file = %s\n\
  508. user_eq_phone = no\n\
  509. rtp_keepalive = 60\n\
  510. rtp_timeout = 60\n\
  511. rtp_timeout_hold = 300\n\
  512. ;acl = local_extensions_acl\n\
  513. rtcp_mux = yes\n\
  514. codec_prefs_incoming_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
  515. codec_prefs_incoming_offer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
  516. codec_prefs_outgoing_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
  517. codec_prefs_outgoing_offer = prefer:pending, operation:union, keep:all, transcode:allow\n\
  518. \n",\
  519. u_row[1],\
  520. u_row[1],\
  521. u_row[1],\
  522. u_row[5],\
  523. codecs,\
  524. u_row[6],\
  525. u_row[1],\
  526. u_row[16],\
  527. u_row[17]\
  528. );
  529. fprintf(conf_exten_hints, "\
  530. exten => %s,hint,PJSIP/%s,CustomPresence:%s\n\
  531. \n",\
  532. u_row[1],\
  533. u_row[1],\
  534. u_row[1]\
  535. );
  536. }
  537. fclose(conf_dev_aors);
  538. fclose(conf_dev_auths);
  539. fclose(conf_dev_eps);
  540. fclose(conf_usr_aors);
  541. fclose(conf_usr_auths);
  542. fclose(conf_usr_eps);
  543. fclose(conf_exten_hints);
  544. mysql_free_result(d_res); // 释放结果集
  545. mysql_free_result(u_res);
  546. mysql_close(g_conn); // 关闭链接
  547. }