res_pktccops.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Attila Domjan
  5. *
  6. * Attila Domjan <attila.domjan.hu@gmail.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!\file
  19. *
  20. * \brief PacketCable COPS
  21. *
  22. * \author Attila Domjan <attila.domjan.hu@gmail.com>
  23. *
  24. * \note
  25. * This module is an add-on to chan_mgcp. It adds support for the
  26. * PacketCable MGCP variation called NCS. Res_pktccops implements COPS
  27. * (RFC 2748), a protocol used to manage dynamic bandwith allocation in
  28. * CMTS's (HFC gateways). When you use NCS, you need to talk COPS with
  29. * the CMTS to complete the calls.
  30. */
  31. /*** MODULEINFO
  32. <defaultenabled>no</defaultenabled>
  33. <support_level>extended</support_level>
  34. ***/
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <fcntl.h>
  40. #include <netdb.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include <string.h>
  45. #include <time.h>
  46. #include <errno.h>
  47. #include <arpa/inet.h>
  48. #include <signal.h>
  49. #include "asterisk/file.h"
  50. #include "asterisk/logger.h"
  51. #include "asterisk/channel.h"
  52. #include "asterisk/config.h"
  53. #include "asterisk/options.h"
  54. #include "asterisk/pbx.h"
  55. #include "asterisk/module.h"
  56. #include "asterisk/cli.h"
  57. #include "asterisk/lock.h"
  58. #define AST_API_MODULE
  59. #include "asterisk/pktccops.h"
  60. #define DEFAULT_COPS_PORT "2126"
  61. #define COPS_HEADER_SIZE 8
  62. #define COPS_OBJECT_HEADER_SIZE 4
  63. #define GATE_SET_OBJ_SIZE 144
  64. #define GATEID_OBJ_SIZE 8
  65. #define GATE_INFO_OBJ_SIZE 24
  66. #define PKTCCOPS_SCOMMAND_GATE_ALLOC 1
  67. #define PKTCCOPS_SCOMMAND_GATE_ALLOC_ACK 2
  68. #define PKTCCOPS_SCOMMAND_GATE_ALLOC_ERR 3
  69. #define PKTCCOPS_SCOMMAND_GATE_SET 4
  70. #define PKTCCOPS_SCOMMAND_GATE_SET_ACK 5
  71. #define PKTCCOPS_SCOMMAND_GATE_SET_ERR 6
  72. #define PKTCCOPS_SCOMMAND_GATE_INFO 7
  73. #define PKTCCOPS_SCOMMAND_GATE_INFO_ACK 8
  74. #define PKTCCOPS_SCOMMAND_GATE_INFO_ERR 9
  75. #define PKTCCOPS_SCOMMAND_GATE_DELETE 10
  76. #define PKTCCOPS_SCOMMAND_GATE_DELETE_ACK 11
  77. #define PKTCCOPS_SCOMMAND_GATE_DELETE_ERR 12
  78. #define PKTCCOPS_SCOMMAND_GATE_OPEN 13
  79. #define PKTCCOPS_SCOMMAND_GATE_CLOSE 14
  80. AST_MUTEX_DEFINE_STATIC(pktccops_lock);
  81. static pthread_t pktccops_thread = AST_PTHREADT_NULL;
  82. static uint16_t cops_trid = 0;
  83. struct pktcobj {
  84. uint16_t length;
  85. unsigned char cnum;
  86. unsigned char ctype;
  87. char *contents;
  88. struct pktcobj *next;
  89. };
  90. struct copsmsg {
  91. unsigned char verflag;
  92. unsigned char opcode;
  93. uint16_t clienttype;
  94. uint32_t length;
  95. struct pktcobj *object;
  96. char *msg; /* != NULL if not packet cable message received */
  97. };
  98. struct gatespec {
  99. int direction; /* 0-DS, 1-US */
  100. int protocolid;
  101. int flags; /* 0x00 */
  102. int sessionclass; /* normal voip: 0x01, high priority voip: 0x02, unspecified: 0x00 */
  103. uint32_t srcip;
  104. uint32_t dstip;
  105. uint16_t srcp;
  106. uint16_t dstp;
  107. int diffserv;
  108. uint16_t t1;
  109. uint16_t t7;
  110. uint16_t t8;
  111. uint32_t r; /* Token Bucket Rate */
  112. uint32_t b; /* token Bucket Size */
  113. uint32_t p; /* Peak Data Rate */
  114. uint32_t m; /* Minimum Policed Size*/
  115. uint32_t mm; /* Maximum Policed Size */
  116. uint32_t rate;
  117. uint32_t s; /* Allowable Jitter*/
  118. };
  119. struct cops_cmts {
  120. AST_LIST_ENTRY(cops_cmts) list;
  121. char name[80];
  122. char host[80];
  123. char port[80];
  124. uint16_t t1;
  125. uint16_t t7;
  126. uint16_t t8;
  127. uint32_t keepalive;
  128. uint32_t handle;
  129. int state;
  130. time_t contime;
  131. time_t katimer;
  132. int sfd;
  133. int need_delete;
  134. };
  135. struct cops_ippool {
  136. AST_LIST_ENTRY(cops_ippool) list;
  137. uint32_t start;
  138. uint32_t stop;
  139. struct cops_cmts *cmts;
  140. };
  141. static uint16_t t1 = 250;
  142. static uint16_t t7 = 200;
  143. static uint16_t t8 = 300;
  144. static uint32_t keepalive = 60;
  145. static int pktccopsdebug = 0;
  146. static int pktcreload = 0;
  147. static int gateinfoperiod = 60;
  148. static int gatetimeout = 150;
  149. AST_LIST_HEAD_STATIC(cmts_list, cops_cmts);
  150. AST_LIST_HEAD_STATIC(ippool_list, cops_ippool);
  151. AST_LIST_HEAD_STATIC(gate_list, cops_gate);
  152. static int pktccops_add_ippool(struct cops_ippool *ippool);
  153. static struct cops_gate *cops_gate_cmd(int cmd, struct cops_cmts *cmts, uint16_t trid, uint32_t mta, uint32_t actcount, float bitrate, uint32_t psize, uint32_t ssip, uint16_t ssport, struct cops_gate *gate);
  154. static void pktccops_unregister_ippools(void);
  155. static int load_pktccops_config(void);
  156. static uint32_t ftoieeef(float n)
  157. {
  158. uint32_t res;
  159. memcpy(&res, &n, 4);
  160. return htonl(res);
  161. }
  162. static uint16_t cops_constructgatespec(struct gatespec *gs, char *res)
  163. {
  164. if (res == NULL) {
  165. return 0;
  166. }
  167. *res = (char) gs->direction;
  168. *(res + 1) = (char) gs->protocolid;
  169. *(res + 2) = (char) gs->flags;
  170. *(res + 3) = (char) gs->sessionclass;
  171. *((uint32_t *) (res + 4)) = gs->srcip;
  172. *((uint32_t *) (res + 8)) = gs->dstip;
  173. *((uint16_t *) (res + 12)) = gs->srcp;
  174. *((uint16_t *) (res + 14)) = gs->dstp;
  175. *(res + 16) = (char) gs->diffserv;
  176. *(res + 17) = 0; /* reserved */
  177. *(res + 18) = 0; /* reserved */
  178. *(res + 19) = 0; /* reserved */
  179. *((uint16_t *) (res + 20)) = gs->t1;
  180. *(res + 22) = 0; /* reserved */
  181. *(res + 23) = 0; /* reserved */
  182. *((uint16_t *) (res + 24)) = gs->t7;
  183. *((uint16_t *) (res + 26)) = gs->t8;
  184. *((uint32_t *) (res + 28)) = gs->r;
  185. *((uint32_t *) (res + 32)) = gs->b;
  186. *((uint32_t *) (res + 36)) = gs->p;
  187. *((uint32_t *) (res + 40)) = gs->m;
  188. *((uint32_t *) (res + 44)) = gs->mm;
  189. *((uint32_t *) (res + 48)) = gs->rate;
  190. *((uint32_t *) (res + 52)) = gs->s;
  191. return 56; /* length */
  192. };
  193. static uint16_t cops_construct_gate (int cmd, char *p, uint16_t trid,
  194. uint32_t mtahost, uint32_t actcount, float rate, uint32_t psizegateid,
  195. uint32_t ssip, uint16_t ssport, uint32_t gateid, struct cops_cmts *cmts)
  196. {
  197. struct gatespec gs;
  198. int offset = 0;
  199. ast_debug(3, "CMD: %d\n", cmd);
  200. /* Transaction Identifier 8 octets */
  201. *(p + offset++) = 0;
  202. *(p + offset++) = 8; /* length */
  203. *(p + offset++) = 1; /* snum */
  204. *(p + offset++) = 1; /* stype */
  205. *((uint16_t *) (p + offset)) = htons(trid);
  206. offset += 2;
  207. *(p + offset++) = 0;
  208. *(p + offset++) = (cmd == GATE_DEL) ? PKTCCOPS_SCOMMAND_GATE_DELETE : (cmd != GATE_INFO) ? PKTCCOPS_SCOMMAND_GATE_SET : PKTCCOPS_SCOMMAND_GATE_INFO; /* 4: GATE-SET, 7: GATE-INFO */
  209. /*Subscriper Identifier 8 octets */
  210. *(p + offset++) = 0;
  211. *(p + offset++) = 8; /* length */
  212. *(p + offset++) = 2; /* snum */
  213. *(p + offset++) = 1; /* stype */
  214. *((uint32_t *) (p + offset)) = htonl(mtahost);
  215. offset += 4;
  216. if (cmd == GATE_INFO || cmd == GATE_SET_HAVE_GATEID || cmd == GATE_DEL) {
  217. /* Gate ID 8 Octets */
  218. *(p + offset++) = 0;
  219. *(p + offset++) = 8; /* length */
  220. *(p + offset++) = 3; /* snum */
  221. *(p + offset++) = 1; /* stype */
  222. *((uint32_t *) (p + offset)) = htonl(gateid);
  223. offset += 4;
  224. if (cmd == GATE_INFO || cmd == GATE_DEL) {
  225. return offset;
  226. }
  227. }
  228. /* Activity Count 8 octets */
  229. *(p + offset++) = 0;
  230. *(p + offset++) = 8; /* length */
  231. *(p + offset++) = 4; /* snum */
  232. *(p + offset++) = 1; /* stype */
  233. *((uint32_t *) (p + offset)) = htonl(actcount);
  234. offset += 4;
  235. /* Gate Spec 2*60 Octets */
  236. gs.direction = 0; /* DS */
  237. gs.protocolid = 17; /* UDP */
  238. gs.flags = 0;
  239. gs.sessionclass = 1;
  240. gs.srcip = htonl(ssip);
  241. gs.dstip = htonl(mtahost);
  242. gs.srcp = htons(ssport);
  243. gs.dstp = 0;
  244. /* gs.diffserv = 0xa0;*/
  245. gs.diffserv = 0;
  246. gs.t1 = htons(cmts->t1);
  247. gs.t7 = htons(cmts->t7);
  248. gs.t8 = htons(cmts->t8);
  249. gs.r = ftoieeef(rate);
  250. gs.b = ftoieeef(psizegateid);
  251. gs.p = ftoieeef(rate);
  252. gs.m = htonl((uint32_t) psizegateid);
  253. gs.mm = htonl((uint32_t) psizegateid);
  254. gs.rate = ftoieeef(rate);
  255. gs.s = htonl(800);
  256. *(p + offset) = 0;
  257. offset++;
  258. *(p + offset) = 60; /* length */
  259. offset++;
  260. *(p + offset) = 5; /* snum */
  261. offset++;
  262. *(p + offset) = 1; /* stype */
  263. offset++;
  264. offset += cops_constructgatespec(&gs, p + offset);
  265. gs.direction = 1; /* US */
  266. gs.srcip = htonl(mtahost);
  267. gs.dstip = htonl(ssip);
  268. gs.srcp = 0;
  269. gs.dstp = htons(ssport);
  270. *(p + offset) = 0;
  271. offset++;
  272. *(p + offset) = 60; /* length */
  273. offset++;
  274. *(p + offset) = 5; /* snum */
  275. offset++;
  276. *(p + offset) = 1; /* stype */
  277. offset++;
  278. offset += cops_constructgatespec(&gs, p + offset);
  279. return(offset);
  280. }
  281. static int cops_getmsg (int sfd, struct copsmsg *recmsg)
  282. {
  283. int len, lent;
  284. char buf[COPS_HEADER_SIZE];
  285. struct pktcobj *pobject = NULL;
  286. uint16_t *ubuf = (uint16_t *) buf;
  287. recmsg->msg = NULL;
  288. recmsg->object = NULL;
  289. len = recv(sfd, buf, COPS_HEADER_SIZE, MSG_DONTWAIT);
  290. if (len < COPS_HEADER_SIZE) {
  291. return len;
  292. }
  293. recmsg->verflag = *buf;
  294. recmsg->opcode = *(buf + 1);
  295. recmsg->clienttype = ntohs(*((uint16_t *) (buf + 2)));
  296. recmsg->length = ntohl(*((uint32_t *) (buf + 4)));
  297. /* Eg KA msg*/
  298. if (recmsg->clienttype != 0x8008 ) {
  299. if (!(recmsg->msg = malloc(recmsg->length - COPS_HEADER_SIZE))) {
  300. return -1;
  301. }
  302. lent = recv(sfd, recmsg->msg, recmsg->length - COPS_HEADER_SIZE, MSG_DONTWAIT);
  303. if (lent < recmsg->length - COPS_HEADER_SIZE) {
  304. return lent;
  305. }
  306. len += len;
  307. } else {
  308. /* PacketCable Objects */
  309. while (len < recmsg->length) {
  310. if (len == COPS_HEADER_SIZE) {
  311. /* 1st round */
  312. if (!(recmsg->object = malloc(sizeof(struct pktcobj)))) {
  313. return -1;
  314. }
  315. pobject = recmsg->object;
  316. } else {
  317. if (!(pobject->next = malloc(sizeof(struct pktcobj)))) {
  318. return -1;
  319. }
  320. pobject = pobject->next;
  321. }
  322. pobject->next = NULL;
  323. lent = recv(sfd, buf, COPS_OBJECT_HEADER_SIZE, MSG_DONTWAIT);
  324. if (lent < COPS_OBJECT_HEADER_SIZE) {
  325. ast_debug(3, "Too short object header len: %i\n", lent);
  326. return lent;
  327. }
  328. len += lent;
  329. pobject->length = ntohs(*ubuf);
  330. pobject->cnum = *(buf + 2);
  331. pobject->ctype = *(buf + 3);
  332. if (!(pobject->contents = malloc(pobject->length - COPS_OBJECT_HEADER_SIZE))) {
  333. return -1;
  334. }
  335. lent = recv(sfd, pobject->contents, pobject->length - COPS_OBJECT_HEADER_SIZE, MSG_DONTWAIT);
  336. if (lent < pobject->length - COPS_OBJECT_HEADER_SIZE) {
  337. ast_debug(3, "Too short object content len: %i\n", lent);
  338. return lent;
  339. }
  340. len += lent;
  341. }
  342. }
  343. return len;
  344. }
  345. static int cops_sendmsg (int sfd, struct copsmsg * sendmsg)
  346. {
  347. char *buf;
  348. int bufpos;
  349. struct pktcobj *pobject;
  350. if (sfd < 0) {
  351. return -1;
  352. }
  353. ast_debug(3, "COPS: sending opcode: %i len: %i\n", sendmsg->opcode, sendmsg->length);
  354. if (sendmsg->length < COPS_HEADER_SIZE) {
  355. ast_log(LOG_WARNING, "COPS: invalid msg size!!!\n");
  356. return -1;
  357. }
  358. if (!(buf = malloc((size_t) sendmsg->length))) {
  359. return -1;
  360. }
  361. *buf = sendmsg->verflag ;
  362. *(buf + 1) = sendmsg->opcode;
  363. *((uint16_t *)(buf + 2)) = htons(sendmsg->clienttype);
  364. *((uint32_t *)(buf + 4)) = htonl(sendmsg->length);
  365. if (sendmsg->msg != NULL) {
  366. memcpy(buf + COPS_HEADER_SIZE, sendmsg->msg, sendmsg->length - COPS_HEADER_SIZE);
  367. } else if (sendmsg->object != NULL) {
  368. bufpos = 8;
  369. pobject = sendmsg->object;
  370. while(pobject != NULL) {
  371. ast_debug(3, "COPS: Sending Object : cnum: %i ctype %i len: %i\n", pobject->cnum, pobject->ctype, pobject->length);
  372. if (sendmsg->length < bufpos + pobject->length) {
  373. ast_log(LOG_WARNING, "COPS: Invalid msg size len: %i objectlen: %i\n", sendmsg->length, pobject->length);
  374. free(buf);
  375. return -1;
  376. }
  377. *(uint16_t *) (buf + bufpos) = htons(pobject->length);
  378. *(buf + bufpos + 2) = pobject->cnum;
  379. *(buf + bufpos + 3) = pobject->ctype;
  380. if (sendmsg->length < pobject->length + bufpos) {
  381. ast_log(LOG_WARNING, "COPS: Error sum of object len more the msg len %i < %i\n", sendmsg->length, pobject->length + bufpos);
  382. free(buf);
  383. return -1;
  384. }
  385. memcpy((buf + bufpos + 4), pobject->contents, pobject->length - 4);
  386. bufpos += pobject->length;
  387. pobject = pobject->next;
  388. }
  389. }
  390. errno = 0;
  391. #ifdef HAVE_MSG_NOSIGNAL
  392. #define SENDFLAGS MSG_NOSIGNAL | MSG_DONTWAIT
  393. #else
  394. #define SENDFLAGS MSG_DONTWAIT
  395. #endif
  396. if (send(sfd, buf, sendmsg->length, SENDFLAGS) == -1) {
  397. ast_log(LOG_WARNING, "COPS: Send failed errno=%i\n", errno);
  398. free(buf);
  399. return -2;
  400. }
  401. #undef SENDFLAGS
  402. free(buf);
  403. return 0;
  404. }
  405. static void cops_freemsg(struct copsmsg *p)
  406. {
  407. struct pktcobj *pnext;
  408. free(p->msg);
  409. p->msg = NULL;
  410. while (p->object != NULL) {
  411. pnext = p->object->next;
  412. ast_free(p->object->contents);
  413. p->object->contents = NULL;
  414. ast_free(p->object);
  415. p->object = pnext;
  416. }
  417. p->object = NULL;
  418. }
  419. struct cops_gate * AST_OPTIONAL_API_NAME(ast_pktccops_gate_alloc)(int cmd,
  420. struct cops_gate *gate, uint32_t mta, uint32_t actcount, float bitrate,
  421. uint32_t psize, uint32_t ssip, uint16_t ssport,
  422. int (* const got_dq_gi) (struct cops_gate *gate),
  423. int (* const gate_remove) (struct cops_gate *gate))
  424. {
  425. while (pktcreload) {
  426. sched_yield();
  427. }
  428. if (cmd == GATE_SET_HAVE_GATEID && gate) {
  429. ast_debug(3, "------- gate modify gateid 0x%x ssip: 0x%x\n", gate->gateid, ssip);
  430. /* TODO implement it */
  431. ast_log(LOG_WARNING, "Modify GateID not implemented\n");
  432. }
  433. if ((gate = cops_gate_cmd(cmd, NULL, cops_trid++, mta, actcount, bitrate, psize, ssip, ssport, gate))) {
  434. ast_debug(3, "COPS: Allocating gate for mta: 0x%x\n", mta);
  435. gate->got_dq_gi = got_dq_gi;
  436. gate->gate_remove = gate_remove;
  437. return(gate);
  438. } else {
  439. ast_debug(3, "COPS: Couldn't allocate gate for mta: 0x%x\n", mta);
  440. return NULL;
  441. }
  442. }
  443. static struct cops_gate *cops_gate_cmd(int cmd, struct cops_cmts *cmts,
  444. uint16_t trid, uint32_t mta, uint32_t actcount, float bitrate,
  445. uint32_t psize, uint32_t ssip, uint16_t ssport, struct cops_gate *gate)
  446. {
  447. struct copsmsg *gateset;
  448. struct cops_gate *new;
  449. struct cops_ippool *ippool;
  450. if (cmd == GATE_DEL) {
  451. if (gate == NULL) {
  452. return NULL;
  453. } else {
  454. cmts = gate->cmts;
  455. }
  456. }
  457. if (!cmts) {
  458. AST_LIST_LOCK(&ippool_list);
  459. AST_LIST_TRAVERSE(&ippool_list, ippool, list) {
  460. if (mta >= ippool->start && mta <= ippool->stop) {
  461. cmts = ippool->cmts;
  462. break;
  463. }
  464. }
  465. AST_LIST_UNLOCK(&ippool_list);
  466. if (!cmts) {
  467. ast_log(LOG_WARNING, "COPS: couldn't find cmts for mta: 0x%x\n", mta);
  468. return NULL;
  469. }
  470. if (cmts->sfd < 0) {
  471. ast_log(LOG_WARNING, "CMTS: %s not connected\n", cmts->name);
  472. return NULL;
  473. }
  474. }
  475. if (cmd == GATE_SET) {
  476. new = ast_calloc(1, sizeof(*new));
  477. new->gateid = 0;
  478. new->trid = trid;
  479. new->mta = mta;
  480. new->state = GATE_ALLOC_PROGRESS;
  481. new->checked = time(NULL);
  482. new->allocated = time(NULL);
  483. new->cmts = cmts;
  484. new->got_dq_gi = NULL;
  485. new->gate_remove = NULL;
  486. new->gate_open = NULL;
  487. new->tech_pvt = NULL;
  488. new->deltimer = 0;
  489. AST_LIST_LOCK(&gate_list);
  490. AST_LIST_INSERT_HEAD(&gate_list, new, list);
  491. AST_LIST_UNLOCK(&gate_list);
  492. gate = new;
  493. } else {
  494. if (gate) {
  495. gate->trid = trid;
  496. }
  497. }
  498. gate->in_transaction = time(NULL);
  499. if (!(gateset = malloc(sizeof(struct copsmsg)))) {
  500. free(gateset);
  501. return NULL;
  502. }
  503. gateset->msg = NULL;
  504. gateset->verflag = 0x10;
  505. gateset->opcode = 2; /* Decision */
  506. gateset->clienttype = 0x8008; /* =PacketCable */
  507. /* Handle object */
  508. gateset->object = malloc(sizeof(struct pktcobj));
  509. if (!gateset->object) {
  510. cops_freemsg(gateset);
  511. free(gateset);
  512. return NULL;
  513. }
  514. gateset->object->length = COPS_OBJECT_HEADER_SIZE + 4;
  515. gateset->object->cnum = 1; /* Handle */
  516. gateset->object->ctype = 1; /* client */
  517. if (!(gateset->object->contents = malloc(sizeof(uint32_t)))) {
  518. cops_freemsg(gateset);
  519. free(gateset);
  520. return NULL;
  521. }
  522. *((uint32_t *) gateset->object->contents) = htonl(cmts->handle);
  523. /* Context Object */
  524. if (!(gateset->object->next = malloc(sizeof(struct pktcobj)))) {
  525. cops_freemsg(gateset);
  526. free(gateset);
  527. return NULL;
  528. }
  529. gateset->object->next->length = COPS_OBJECT_HEADER_SIZE + 4;
  530. gateset->object->next->cnum = 2; /* Context */
  531. gateset->object->next->ctype = 1; /* Context */
  532. if (!(gateset->object->next->contents = malloc(sizeof(uint32_t)))) {
  533. cops_freemsg(gateset);
  534. free(gateset);
  535. return NULL;
  536. }
  537. *((uint32_t *) gateset->object->next->contents) = htonl(0x00080000); /* R-Type = 8 configuration request, M-Type = 0 */
  538. /* Decision Object: Flags */
  539. if (!(gateset->object->next->next = malloc(sizeof(struct pktcobj)))) {
  540. cops_freemsg(gateset);
  541. free(gateset);
  542. return NULL;
  543. }
  544. gateset->object->next->next->length = COPS_OBJECT_HEADER_SIZE + 4;
  545. gateset->object->next->next->cnum = 6; /* Decision */
  546. gateset->object->next->next->ctype = 1; /* Flags */
  547. if (!(gateset->object->next->next->contents = malloc(sizeof(uint32_t)))) {
  548. cops_freemsg(gateset);
  549. free(gateset);
  550. return NULL;
  551. }
  552. *((uint32_t *) gateset->object->next->next->contents) = htonl(0x00010001); /* Install, Trigger Error */
  553. /* Decision Object: Data */
  554. if (!(gateset->object->next->next->next = malloc(sizeof(struct pktcobj)))) {
  555. cops_freemsg(gateset);
  556. free(gateset);
  557. return NULL;
  558. }
  559. gateset->object->next->next->next->length = COPS_OBJECT_HEADER_SIZE + ((cmd != GATE_INFO && cmd != GATE_DEL) ? GATE_SET_OBJ_SIZE : GATE_INFO_OBJ_SIZE) + ((cmd == GATE_SET_HAVE_GATEID) ? GATEID_OBJ_SIZE : 0);
  560. gateset->object->next->next->next->cnum = 6; /* Decision */
  561. gateset->object->next->next->next->ctype = 4; /* Decision Data */
  562. gateset->object->next->next->next->contents = malloc(((cmd != GATE_INFO && cmd != GATE_DEL) ? GATE_SET_OBJ_SIZE : GATE_INFO_OBJ_SIZE) + ((cmd == GATE_SET_HAVE_GATEID) ? GATEID_OBJ_SIZE : 0));
  563. if (!gateset->object->next->next->next->contents) {
  564. cops_freemsg(gateset);
  565. free(gateset);
  566. return NULL;
  567. }
  568. gateset->object->next->next->next->next = NULL;
  569. gateset->length = COPS_HEADER_SIZE + gateset->object->length + gateset->object->next->length + gateset->object->next->next->length + gateset->object->next->next->next->length;
  570. if ((cmd == GATE_INFO || cmd == GATE_SET_HAVE_GATEID || cmd == GATE_DEL) && gate) {
  571. ast_debug(1, "Construct gate with gateid: 0x%x\n", gate->gateid);
  572. cops_construct_gate(cmd, gateset->object->next->next->next->contents, trid, mta, actcount, bitrate, psize, ssip, ssport, gate->gateid, cmts);
  573. } else {
  574. ast_debug(1, "Construct new gate\n");
  575. cops_construct_gate(cmd, gateset->object->next->next->next->contents, trid, mta, actcount, bitrate, psize, ssip, ssport, 0, cmts);
  576. }
  577. if (pktccopsdebug) {
  578. ast_debug(3, "send cmd\n");
  579. }
  580. cops_sendmsg(cmts->sfd, gateset);
  581. cops_freemsg(gateset);
  582. free(gateset);
  583. return gate;
  584. }
  585. static int cops_connect(char *host, char *port)
  586. {
  587. int s, sfd = -1, flags;
  588. struct addrinfo hints;
  589. struct addrinfo *rp;
  590. struct addrinfo *result;
  591. #ifdef HAVE_SO_NOSIGPIPE
  592. int trueval = 1;
  593. #endif
  594. memset(&hints, 0, sizeof(struct addrinfo));
  595. hints.ai_family = AF_UNSPEC;
  596. hints.ai_socktype = SOCK_STREAM;
  597. hints.ai_flags = 0;
  598. hints.ai_protocol = 0;
  599. s = getaddrinfo(host, port, &hints, &result);
  600. if (s != 0) {
  601. ast_log(LOG_WARNING, "COPS: getaddrinfo: %s\n", gai_strerror(s));
  602. return -1;
  603. }
  604. for (rp = result; rp != NULL; rp = rp->ai_next) {
  605. sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  606. if (sfd == -1) {
  607. ast_log(LOG_WARNING, "Failed socket\n");
  608. }
  609. flags = fcntl(sfd, F_GETFL);
  610. fcntl(sfd, F_SETFL, flags | O_NONBLOCK);
  611. #ifdef HAVE_SO_NOSIGPIPE
  612. setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, &trueval, sizeof(trueval));
  613. #endif
  614. connect(sfd, rp->ai_addr, rp->ai_addrlen);
  615. if (sfd == -1) {
  616. ast_log(LOG_WARNING, "Failed connect\n");
  617. }
  618. }
  619. freeaddrinfo(result);
  620. ast_debug(3, "Connecting to cmts: %s:%s\n", host, port);
  621. return(sfd);
  622. }
  623. #define PKTCCOPS_DESTROY_CURRENT_GATE \
  624. AST_LIST_REMOVE_CURRENT(list); \
  625. if (gate->gate_remove) { \
  626. gate->gate_remove(gate); \
  627. } \
  628. ast_free(gate);
  629. static void *do_pktccops(void *data)
  630. {
  631. int res, nfds, len;
  632. struct copsmsg *recmsg, *sendmsg;
  633. struct copsmsg recmsgb, sendmsgb;
  634. struct pollfd *pfds = NULL, *tmp;
  635. struct pktcobj *pobject;
  636. struct cops_cmts *cmts;
  637. struct cops_gate *gate;
  638. char *sobjp;
  639. uint16_t snst, sobjlen, scommand, recvtrid, actcount, reason, subreason;
  640. uint32_t gateid, subscrid, pktcerror;
  641. time_t last_exec = 0;
  642. recmsg = &recmsgb;
  643. sendmsg = &sendmsgb;
  644. ast_debug(3, "COPS: thread started\n");
  645. for (;;) {
  646. ast_free(pfds);
  647. pfds = NULL;
  648. nfds = 0;
  649. AST_LIST_LOCK(&cmts_list);
  650. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  651. if (last_exec != time(NULL)) {
  652. if (cmts->state == 2 && cmts->katimer + cmts->keepalive < time(NULL)) {
  653. ast_log(LOG_WARNING, "KA timer (%is) expired cmts: %s\n", cmts->keepalive, cmts->name);
  654. cmts->state = 0;
  655. cmts->katimer = -1;
  656. close(cmts->sfd);
  657. cmts->sfd = -1;
  658. }
  659. }
  660. if (cmts->sfd > 0) {
  661. if (!(tmp = ast_realloc(pfds, (nfds + 1) * sizeof(*pfds)))) {
  662. continue;
  663. }
  664. pfds = tmp;
  665. pfds[nfds].fd = cmts->sfd;
  666. pfds[nfds].events = POLLIN;
  667. pfds[nfds].revents = 0;
  668. nfds++;
  669. } else {
  670. cmts->sfd = cops_connect(cmts->host, cmts->port);
  671. if (cmts->sfd > 0) {
  672. cmts->state = 1;
  673. if (cmts->sfd > 0) {
  674. if (!(tmp = ast_realloc(pfds, (nfds + 1) * sizeof(*pfds)))) {
  675. continue;
  676. }
  677. pfds = tmp;
  678. pfds[nfds].fd = cmts->sfd;
  679. pfds[nfds].events = POLLIN;
  680. pfds[nfds].revents = 0;
  681. nfds++;
  682. }
  683. }
  684. }
  685. }
  686. AST_LIST_UNLOCK(&cmts_list);
  687. if (last_exec != time(NULL)) {
  688. last_exec = time(NULL);
  689. AST_LIST_LOCK(&gate_list);
  690. AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
  691. if (gate) {
  692. if (gate->deltimer && gate->deltimer < time(NULL)) {
  693. gate->deltimer = time(NULL) + 5;
  694. gate->trid = cops_trid++;
  695. cops_gate_cmd(GATE_DEL, gate->cmts, gate->trid, 0, 0, 0, 0, 0, 0, gate);
  696. ast_debug(3, "COPS: requested Gate-Del: CMTS: %s gateid: 0x%x\n", (gate->cmts) ? gate->cmts->name : "null", gate->gateid);
  697. }
  698. if (time(NULL) - gate->checked > gatetimeout) {
  699. ast_debug(3, "COPS: remove from list GATE, CMTS: %s gateid: 0x%x\n", (gate->cmts) ? gate->cmts->name : "null", gate->gateid);
  700. gate->state = GATE_TIMEOUT;
  701. PKTCCOPS_DESTROY_CURRENT_GATE;
  702. } else if (time(NULL) - gate->checked > gateinfoperiod && (gate->state == GATE_ALLOCATED || gate->state == GATE_OPEN)) {
  703. if (gate->cmts && (!gate->in_transaction || ( gate->in_transaction + 5 ) < time(NULL))) {
  704. gate->trid = cops_trid++;
  705. ast_debug(3, "COPS: Gate-Info send to CMTS: %s gateid: 0x%x\n", gate->cmts->name, gate->gateid);
  706. cops_gate_cmd(GATE_INFO, gate->cmts, gate->trid, gate->mta, 0, 0, 0, 0, 0, gate);
  707. }
  708. }
  709. }
  710. }
  711. AST_LIST_TRAVERSE_SAFE_END;
  712. AST_LIST_UNLOCK(&gate_list);
  713. }
  714. if (pktcreload == 2) {
  715. pktcreload = 0;
  716. }
  717. if ((res = ast_poll(pfds, nfds, 1000))) {
  718. AST_LIST_LOCK(&cmts_list);
  719. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  720. int idx;
  721. if ((idx = ast_poll_fd_index(pfds, nfds, cmts->sfd)) > -1 && (pfds[idx].revents & POLLIN)) {
  722. len = cops_getmsg(cmts->sfd, recmsg);
  723. if (len > 0) {
  724. ast_debug(3, "COPS: got from %s:\n Header: versflag=0x%.2x opcode=%i clienttype=0x%.4x msglength=%i\n",
  725. cmts->name, recmsg->verflag, recmsg->opcode, recmsg->clienttype, recmsg->length);
  726. if (recmsg->object != NULL) {
  727. pobject = recmsg->object;
  728. while (pobject != NULL) {
  729. ast_debug(3, " OBJECT: length=%i cnum=%i ctype=%i\n", pobject->length, pobject->cnum, pobject->ctype);
  730. if (recmsg->opcode == 1 && pobject->cnum == 1 && pobject->ctype == 1 ) {
  731. cmts->handle = ntohl(*((uint32_t *) pobject->contents));
  732. ast_debug(3, " REQ client handle: %i\n", cmts->handle);
  733. cmts->state = 2;
  734. cmts->katimer = time(NULL);
  735. } else if (pobject->cnum == 9 && pobject->ctype == 1) {
  736. sobjp = pobject->contents;
  737. subscrid = 0;
  738. recvtrid = 0;
  739. scommand = 0;
  740. pktcerror = 0;
  741. actcount = 0;
  742. gateid = 0;
  743. reason = 0;
  744. subreason = 0;
  745. while (sobjp < (pobject->contents + pobject->length - 4)) {
  746. sobjlen = ntohs(*((uint16_t *) sobjp));
  747. snst = ntohs(*((uint16_t *) (sobjp + 2)));
  748. ast_debug(3, " S-Num S-type: 0x%.4x len: %i\n", snst, sobjlen);
  749. if (snst == 0x0101 ) {
  750. recvtrid = ntohs(*((uint16_t *) (sobjp + 4)));
  751. scommand = ntohs(*((uint16_t *) (sobjp + 6)));
  752. ast_debug(3, " Transaction Identifier command: %i trid %i\n", scommand, recvtrid);
  753. } else if (snst == 0x0201) {
  754. subscrid = ntohl(*((uint32_t *) (sobjp + 4)));
  755. ast_debug(3, " Subscriber ID: 0x%.8x\n", subscrid);
  756. } else if (snst == 0x0301) {
  757. gateid = ntohl(*((uint32_t *) (sobjp + 4)));
  758. ast_debug(3, " Gate ID: 0x%x 0x%.8x\n", gateid, gateid);
  759. } else if (snst == 0x0401) {
  760. actcount = ntohs(*((uint16_t *) (sobjp + 6)));
  761. ast_debug(3, " Activity Count: %i\n", actcount);
  762. } else if (snst == 0x0901) {
  763. pktcerror = ntohl(*((uint32_t *) (sobjp + 4)));
  764. ast_debug(3, " PKTC Error: 0x%.8x\n", pktcerror);
  765. } else if (snst == 0x0d01) {
  766. reason = ntohs(*((uint16_t *) (sobjp + 4)));
  767. subreason = ntohs(*((uint16_t *) (sobjp + 6)));
  768. ast_debug(3, " Reason: %u Subreason: %u\n", reason, subreason);
  769. }
  770. sobjp += sobjlen;
  771. if (!sobjlen)
  772. break;
  773. }
  774. if (scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE || scommand == PKTCCOPS_SCOMMAND_GATE_OPEN) {
  775. AST_LIST_LOCK(&gate_list);
  776. AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
  777. if (gate->cmts == cmts && gate->gateid == gateid) {
  778. if (scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE && gate->state != GATE_CLOSED && gate->state != GATE_CLOSED_ERR ) {
  779. ast_debug(3, "COPS Gate Close Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  780. if (subreason) {
  781. gate->state = GATE_CLOSED_ERR;
  782. PKTCCOPS_DESTROY_CURRENT_GATE;
  783. } else {
  784. gate->state = GATE_CLOSED;
  785. PKTCCOPS_DESTROY_CURRENT_GATE;
  786. }
  787. break;
  788. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_OPEN && gate->state == GATE_ALLOCATED) {
  789. ast_debug(3, "COPS Gate Open Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  790. gate->state = GATE_OPEN;
  791. if (gate->gate_open) {
  792. ast_debug(3, "Calling GATE-OPEN callback function\n");
  793. gate->gate_open(gate);
  794. gate->gate_open = NULL;
  795. }
  796. break;
  797. }
  798. }
  799. }
  800. AST_LIST_TRAVERSE_SAFE_END;
  801. AST_LIST_UNLOCK(&gate_list);
  802. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_SET_ACK || scommand == PKTCCOPS_SCOMMAND_GATE_SET_ERR || scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ACK || scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ERR || scommand == PKTCCOPS_SCOMMAND_GATE_DELETE_ACK) {
  803. AST_LIST_LOCK(&gate_list);
  804. AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
  805. if (gate->cmts == cmts && gate->trid == recvtrid) {
  806. gate->gateid = gateid;
  807. gate->checked = time(NULL);
  808. if (scommand == PKTCCOPS_SCOMMAND_GATE_SET_ACK) {
  809. ast_debug(3, "COPS Gate Set Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  810. gate->state = GATE_ALLOCATED;
  811. if (gate->got_dq_gi) {
  812. gate->got_dq_gi(gate);
  813. gate->got_dq_gi = NULL;
  814. }
  815. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_SET_ERR) {
  816. ast_debug(3, "COPS Gate Set Error TrId: %i ErrorCode: 0x%.8x CMTS: %s\n ", recvtrid, pktcerror, cmts->name);
  817. gate->state = GATE_ALLOC_FAILED;
  818. if (gate->got_dq_gi) {
  819. gate->got_dq_gi(gate);
  820. gate->got_dq_gi = NULL;
  821. }
  822. PKTCCOPS_DESTROY_CURRENT_GATE;
  823. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ACK) {
  824. ast_debug(3, "COPS Gate Info Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  825. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ERR) {
  826. ast_debug(3, "COPS Gate Info Error Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  827. gate->state = GATE_ALLOC_FAILED;
  828. PKTCCOPS_DESTROY_CURRENT_GATE;
  829. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_DELETE_ACK) {
  830. ast_debug(3, "COPS Gate Deleted Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  831. gate->state = GATE_DELETED;
  832. PKTCCOPS_DESTROY_CURRENT_GATE;
  833. }
  834. gate->in_transaction = 0;
  835. break;
  836. }
  837. }
  838. AST_LIST_TRAVERSE_SAFE_END;
  839. AST_LIST_UNLOCK(&gate_list);
  840. }
  841. }
  842. pobject = pobject->next;
  843. }
  844. }
  845. if (recmsg->opcode == 6 && recmsg->object && recmsg->object->cnum == 11 && recmsg->object->ctype == 1) {
  846. ast_debug(3, "COPS: Client open %s\n", cmts->name);
  847. sendmsg->msg = NULL;
  848. sendmsg->verflag = 0x10;
  849. sendmsg->opcode = 7; /* Client Accept */
  850. sendmsg->clienttype = 0x8008; /* =PacketCable */
  851. sendmsg->length = COPS_HEADER_SIZE + COPS_OBJECT_HEADER_SIZE + 4;
  852. sendmsg->object = malloc(sizeof(struct pktcobj));
  853. sendmsg->object->length = 4 + COPS_OBJECT_HEADER_SIZE;
  854. sendmsg->object->cnum = 10; /* keppalive timer*/
  855. sendmsg->object->ctype = 1;
  856. sendmsg->object->contents = malloc(sizeof(uint32_t));
  857. *((uint32_t *) sendmsg->object->contents) = htonl(cmts->keepalive & 0x0000ffff);
  858. sendmsg->object->next = NULL;
  859. cops_sendmsg(cmts->sfd, sendmsg);
  860. cops_freemsg(sendmsg);
  861. } else if (recmsg->opcode == 9) {
  862. ast_debug(3, "COPS: Keepalive Request got echoing back %s\n", cmts->name);
  863. cops_sendmsg(cmts->sfd, recmsg);
  864. cmts->state = 2;
  865. cmts->katimer = time(NULL);
  866. }
  867. }
  868. if (len <= 0) {
  869. ast_debug(3, "COPS: lost connection to %s\n", cmts->name);
  870. close(cmts->sfd);
  871. cmts->sfd = -1;
  872. cmts->state = 0;
  873. }
  874. cops_freemsg(recmsg);
  875. }
  876. }
  877. AST_LIST_UNLOCK(&cmts_list);
  878. }
  879. if (pktcreload) {
  880. ast_debug(3, "Reloading pktccops...\n");
  881. AST_LIST_LOCK(&gate_list);
  882. AST_LIST_LOCK(&cmts_list);
  883. pktccops_unregister_ippools();
  884. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  885. cmts->need_delete = 1;
  886. }
  887. load_pktccops_config();
  888. AST_LIST_TRAVERSE_SAFE_BEGIN(&cmts_list, cmts, list) {
  889. if (cmts && cmts->need_delete) {
  890. AST_LIST_TRAVERSE(&gate_list, gate, list) {
  891. if (gate->cmts == cmts) {
  892. ast_debug(3, "Null gate %s\n", gate->cmts->name);
  893. gate->cmts = NULL;
  894. }
  895. gate->in_transaction = 0;
  896. }
  897. AST_LIST_UNLOCK(&gate_list);
  898. ast_debug(3, "removing cmts: %s\n", cmts->name);
  899. if (cmts->sfd > 0) {
  900. close(cmts->sfd);
  901. }
  902. AST_LIST_REMOVE_CURRENT(list);
  903. free(cmts);
  904. }
  905. }
  906. AST_LIST_TRAVERSE_SAFE_END;
  907. AST_LIST_UNLOCK(&cmts_list);
  908. AST_LIST_UNLOCK(&gate_list);
  909. pktcreload = 2;
  910. }
  911. pthread_testcancel();
  912. }
  913. return NULL;
  914. }
  915. static int restart_pktc_thread(void)
  916. {
  917. if (pktccops_thread == AST_PTHREADT_STOP) {
  918. return 0;
  919. }
  920. if (ast_mutex_lock(&pktccops_lock)) {
  921. ast_log(LOG_WARNING, "Unable to lock pktccops\n");
  922. return -1;
  923. }
  924. if (pktccops_thread == pthread_self()) {
  925. ast_mutex_unlock(&pktccops_lock);
  926. ast_log(LOG_WARNING, "Cannot kill myself\n");
  927. return -1;
  928. }
  929. if (pktccops_thread != AST_PTHREADT_NULL) {
  930. /* Wake up the thread */
  931. pthread_kill(pktccops_thread, SIGURG);
  932. } else {
  933. /* Start a new monitor */
  934. if (ast_pthread_create_background(&pktccops_thread, NULL, do_pktccops, NULL) < 0) {
  935. ast_mutex_unlock(&pktccops_lock);
  936. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  937. return -1;
  938. }
  939. }
  940. ast_mutex_unlock(&pktccops_lock);
  941. return 0;
  942. }
  943. static int load_pktccops_config(void)
  944. {
  945. static char *cfg = "res_pktccops.conf";
  946. struct ast_config *config;
  947. struct ast_variable *v;
  948. struct cops_cmts *cmts;
  949. struct cops_ippool *new_ippool;
  950. const char *host, *cat, *port;
  951. int update;
  952. int res = 0;
  953. uint16_t t1_temp, t7_temp, t8_temp;
  954. uint32_t keepalive_temp;
  955. unsigned int a,b,c,d,e,f,g,h;
  956. struct ast_flags config_flags = {0};
  957. if (!(config = ast_config_load(cfg, config_flags))) {
  958. ast_log(LOG_WARNING, "Unable to load config file res_pktccops.conf\n");
  959. return -1;
  960. }
  961. for (cat = ast_category_browse(config, NULL); cat; cat = ast_category_browse(config, cat)) {
  962. if (!strcmp(cat, "general")) {
  963. for (v = ast_variable_browse(config, cat); v; v = v->next) {
  964. if (!strcasecmp(v->name, "t1")) {
  965. t1 = atoi(v->value);
  966. } else if (!strcasecmp(v->name, "t7")) {
  967. t7 = atoi(v->value);
  968. } else if (!strcasecmp(v->name, "t8")) {
  969. t8 = atoi(v->value);
  970. } else if (!strcasecmp(v->name, "keepalive")) {
  971. keepalive = atoi(v->value);
  972. } else if (!strcasecmp(v->name, "gateinfoperiod")) {
  973. gateinfoperiod = atoi(v->value);
  974. } else if (!strcasecmp(v->name, "gatetimeout")) {
  975. gatetimeout = atoi(v->value);
  976. } else {
  977. ast_log(LOG_WARNING, "Unkown option %s in general section of res_ptkccops.conf\n", v->name);
  978. }
  979. }
  980. } else {
  981. /* Defaults */
  982. host = NULL;
  983. port = NULL;
  984. t1_temp = t1;
  985. t7_temp = t7;
  986. t8_temp = t8;
  987. keepalive_temp = keepalive;
  988. for (v = ast_variable_browse(config, cat); v; v = v->next) {
  989. if (!strcasecmp(v->name, "host")) {
  990. host = v->value;
  991. } else if (!strcasecmp(v->name, "port")) {
  992. port = v->value;
  993. } else if (!strcasecmp(v->name, "t1")) {
  994. t1_temp = atoi(v->value);
  995. } else if (!strcasecmp(v->name, "t7")) {
  996. t7_temp = atoi(v->value);
  997. } else if (!strcasecmp(v->name, "t8")) {
  998. t8_temp = atoi(v->value);
  999. } else if (!strcasecmp(v->name, "keepalive")) {
  1000. keepalive_temp = atoi(v->value);
  1001. } else if (!strcasecmp(v->name, "pool")) {
  1002. /* we weill parse it in 2nd round */
  1003. } else {
  1004. ast_log(LOG_WARNING, "Unkown option %s in res_ptkccops.conf\n", v->name);
  1005. }
  1006. }
  1007. update = 0;
  1008. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  1009. if (!strcmp(cmts->name, cat)) {
  1010. update = 1;
  1011. break;
  1012. }
  1013. }
  1014. if (!update) {
  1015. cmts = ast_calloc(1, sizeof(*cmts));
  1016. if (!cmts) {
  1017. res = -1;
  1018. break;
  1019. }
  1020. AST_LIST_INSERT_HEAD(&cmts_list, cmts, list);
  1021. }
  1022. if (cat) {
  1023. ast_copy_string(cmts->name, cat, sizeof(cmts->name));
  1024. }
  1025. if (host) {
  1026. ast_copy_string(cmts->host, host, sizeof(cmts->host));
  1027. }
  1028. if (port) {
  1029. ast_copy_string(cmts->port, port, sizeof(cmts->port));
  1030. } else {
  1031. ast_copy_string(cmts->port, DEFAULT_COPS_PORT, sizeof(cmts->port));
  1032. }
  1033. cmts->t1 = t1_temp;
  1034. cmts->t7 = t7_temp;
  1035. cmts->t8 = t8_temp;
  1036. cmts->keepalive = keepalive_temp;
  1037. if (!update) {
  1038. cmts->state = 0;
  1039. cmts->sfd = -1;
  1040. }
  1041. cmts->need_delete = 0;
  1042. for (v = ast_variable_browse(config, cat); v; v = v->next) {
  1043. /* parse ipppol when we have cmts ptr */
  1044. if (!strcasecmp(v->name, "pool")) {
  1045. if (sscanf(v->value, "%3u.%3u.%3u.%3u %3u.%3u.%3u.%3u", &a, &b, &c, &d, &e, &f, &g, &h) == 8) {
  1046. new_ippool = ast_calloc(1, sizeof(*new_ippool));
  1047. if (!new_ippool) {
  1048. res = -1;
  1049. break;
  1050. }
  1051. new_ippool->start = a << 24 | b << 16 | c << 8 | d;
  1052. new_ippool->stop = e << 24 | f << 16 | g << 8 | h;
  1053. new_ippool->cmts = cmts;
  1054. pktccops_add_ippool(new_ippool);
  1055. } else {
  1056. ast_log(LOG_WARNING, "Invalid ip pool format in res_pktccops.conf\n");
  1057. }
  1058. }
  1059. }
  1060. }
  1061. }
  1062. ast_config_destroy(config);
  1063. return res;
  1064. }
  1065. static char *pktccops_show_cmtses(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1066. {
  1067. struct cops_cmts *cmts;
  1068. char statedesc[16];
  1069. int katimer;
  1070. switch(cmd) {
  1071. case CLI_INIT:
  1072. e->command = "pktccops show cmtses";
  1073. e->usage =
  1074. "Usage: pktccops show cmtses\n"
  1075. " List PacketCable COPS CMTSes.\n";
  1076. return NULL;
  1077. case CLI_GENERATE:
  1078. return NULL;
  1079. }
  1080. ast_cli(a->fd, "%-16s %-24s %-12s %7s\n", "Name ", "Host ", "Status ", "KA timer ");
  1081. ast_cli(a->fd, "%-16s %-24s %-12s %7s\n", "------------", "--------------------", "----------", "-----------");
  1082. AST_LIST_LOCK(&cmts_list);
  1083. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  1084. katimer = -1;
  1085. if (cmts->state == 2) {
  1086. ast_copy_string(statedesc, "Connected", sizeof(statedesc));
  1087. katimer = (int) (time(NULL) - cmts->katimer);
  1088. } else if (cmts->state == 1) {
  1089. ast_copy_string(statedesc, "Connecting", sizeof(statedesc));
  1090. } else {
  1091. ast_copy_string(statedesc, "N/A", sizeof(statedesc));
  1092. }
  1093. ast_cli(a->fd, "%-16s %-15s:%-8s %-12s %-7d\n", cmts->name, cmts->host, cmts->port, statedesc, katimer);
  1094. }
  1095. AST_LIST_UNLOCK(&cmts_list);
  1096. return CLI_SUCCESS;
  1097. }
  1098. static char *pktccops_show_gates(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1099. {
  1100. struct cops_gate *gate;
  1101. char state_desc[16];
  1102. switch(cmd) {
  1103. case CLI_INIT:
  1104. e->command = "pktccops show gates";
  1105. e->usage =
  1106. "Usage: pktccops show gates\n"
  1107. " List PacketCable COPS GATEs.\n";
  1108. return NULL;
  1109. case CLI_GENERATE:
  1110. return NULL;
  1111. }
  1112. ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"CMTS", "Gate-Id","MTA", "Status", "AllocTime", "CheckTime");
  1113. ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"--------------" ,"----------", "----------", "--------", "--------", "--------\n");
  1114. AST_LIST_LOCK(&cmts_list);
  1115. AST_LIST_LOCK(&gate_list);
  1116. AST_LIST_TRAVERSE(&gate_list, gate, list) {
  1117. if (gate->state == GATE_ALLOC_FAILED) {
  1118. ast_copy_string(state_desc, "Failed", sizeof(state_desc));
  1119. } else if (gate->state == GATE_ALLOC_PROGRESS) {
  1120. ast_copy_string(state_desc, "In Progress", sizeof(state_desc));
  1121. } else if (gate->state == GATE_ALLOCATED) {
  1122. ast_copy_string(state_desc, "Allocated", sizeof(state_desc));
  1123. } else if (gate->state == GATE_CLOSED) {
  1124. ast_copy_string(state_desc, "Closed", sizeof(state_desc));
  1125. } else if (gate->state == GATE_CLOSED_ERR) {
  1126. ast_copy_string(state_desc, "ClosedErr", sizeof(state_desc));
  1127. } else if (gate->state == GATE_OPEN) {
  1128. ast_copy_string(state_desc, "Open", sizeof(state_desc));
  1129. } else if (gate->state == GATE_DELETED) {
  1130. ast_copy_string(state_desc, "Deleted", sizeof(state_desc));
  1131. } else {
  1132. ast_copy_string(state_desc, "N/A", sizeof(state_desc));
  1133. }
  1134. ast_cli(a->fd, "%-16s 0x%.8x 0x%08x %-10s %10i %10i %u\n", (gate->cmts) ? gate->cmts->name : "null" , gate->gateid, gate->mta,
  1135. state_desc, (int) (time(NULL) - gate->allocated), (gate->checked) ? (int) (time(NULL) - gate->checked) : 0, (unsigned int) gate->in_transaction);
  1136. }
  1137. AST_LIST_UNLOCK(&cmts_list);
  1138. AST_LIST_UNLOCK(&gate_list);
  1139. return CLI_SUCCESS;
  1140. }
  1141. static char *pktccops_show_pools(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1142. {
  1143. struct cops_ippool *ippool;
  1144. char start[32];
  1145. char stop[32];
  1146. switch(cmd) {
  1147. case CLI_INIT:
  1148. e->command = "pktccops show pools";
  1149. e->usage =
  1150. "Usage: pktccops show pools\n"
  1151. " List PacketCable COPS ip pools of MTAs.\n";
  1152. return NULL;
  1153. case CLI_GENERATE:
  1154. return NULL;
  1155. }
  1156. ast_cli(a->fd, "%-16s %-18s %-7s\n", "Start ", "Stop ", "CMTS ");
  1157. ast_cli(a->fd, "%-16s %-18s %-7s\n", "----------", "----------", "--------");
  1158. AST_LIST_LOCK(&ippool_list);
  1159. AST_LIST_TRAVERSE(&ippool_list, ippool, list) {
  1160. snprintf(start, sizeof(start), "%3u.%3u.%3u.%3u", ippool->start >> 24, (ippool->start >> 16) & 0x000000ff, (ippool->start >> 8) & 0x000000ff, ippool->start & 0x000000ff);
  1161. snprintf(stop, sizeof(stop), "%3u.%3u.%3u.%3u", ippool->stop >> 24, (ippool->stop >> 16) & 0x000000ff, (ippool->stop >> 8) & 0x000000ff, ippool->stop & 0x000000ff);
  1162. ast_cli(a->fd, "%-16s %-18s %-16s\n", start, stop, ippool->cmts->name);
  1163. }
  1164. AST_LIST_UNLOCK(&ippool_list);
  1165. return CLI_SUCCESS;
  1166. }
  1167. static char *pktccops_gatedel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1168. {
  1169. int found = 0;
  1170. int trid;
  1171. uint32_t gateid;
  1172. struct cops_gate *gate;
  1173. struct cops_cmts *cmts;
  1174. switch (cmd) {
  1175. case CLI_INIT:
  1176. e->command = "pktccops gatedel";
  1177. e->usage =
  1178. "Usage: pktccops gatedel <cmts> <gateid>\n"
  1179. " Send Gate-Del to cmts.\n";
  1180. return NULL;
  1181. case CLI_GENERATE:
  1182. return NULL;
  1183. }
  1184. if (a->argc < 4)
  1185. return CLI_SHOWUSAGE;
  1186. AST_LIST_LOCK(&cmts_list);
  1187. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  1188. if (!strcmp(cmts->name, a->argv[2])) {
  1189. ast_cli(a->fd, "Found cmts: %s\n", cmts->name);
  1190. found = 1;
  1191. break;
  1192. }
  1193. }
  1194. AST_LIST_UNLOCK(&cmts_list);
  1195. if (!found)
  1196. return CLI_SHOWUSAGE;
  1197. trid = cops_trid++;
  1198. if (!sscanf(a->argv[3], "%x", &gateid)) {
  1199. ast_cli(a->fd, "bad gate specification (%s)\n", a->argv[3]);
  1200. return CLI_SHOWUSAGE;
  1201. }
  1202. found = 0;
  1203. AST_LIST_LOCK(&gate_list);
  1204. AST_LIST_TRAVERSE(&gate_list, gate, list) {
  1205. if (gate->gateid == gateid && gate->cmts == cmts) {
  1206. found = 1;
  1207. break;
  1208. }
  1209. }
  1210. if (!found) {
  1211. ast_cli(a->fd, "gate not found: %s\n", a->argv[3]);
  1212. return CLI_SHOWUSAGE;
  1213. }
  1214. AST_LIST_UNLOCK(&gate_list);
  1215. cops_gate_cmd(GATE_DEL, cmts, trid, 0, 0, 0, 0, 0, 0, gate);
  1216. return CLI_SUCCESS;
  1217. }
  1218. static char *pktccops_gateset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1219. {
  1220. int foundcmts = 0;
  1221. int trid;
  1222. unsigned int an,bn,cn,dn;
  1223. uint32_t mta, ssip;
  1224. struct cops_cmts *cmts;
  1225. switch (cmd) {
  1226. case CLI_INIT:
  1227. e->command = "pktccops gateset";
  1228. e->usage =
  1229. "Usage: pktccops gateset <cmts> <mta> <acctcount> <bitrate> <packet size> <switch ip> <switch port>\n"
  1230. " Send Gate-Set to cmts.\n";
  1231. return NULL;
  1232. case CLI_GENERATE:
  1233. return NULL;
  1234. }
  1235. if (a->argc < 9)
  1236. return CLI_SHOWUSAGE;
  1237. if (!strncmp(a->argv[2], "null", sizeof(a->argv[2]))) {
  1238. cmts = NULL;
  1239. } else {
  1240. AST_LIST_LOCK(&cmts_list);
  1241. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  1242. if (!strcmp(cmts->name, a->argv[2])) {
  1243. ast_cli(a->fd, "Found cmts: %s\n", cmts->name);
  1244. foundcmts = 1;
  1245. break;
  1246. }
  1247. }
  1248. AST_LIST_UNLOCK(&cmts_list);
  1249. if (!foundcmts) {
  1250. ast_cli(a->fd, "CMTS not found: %s\n", a->argv[2]);
  1251. return CLI_SHOWUSAGE;
  1252. }
  1253. }
  1254. trid = cops_trid++;
  1255. if (sscanf(a->argv[3], "%3u.%3u.%3u.%3u", &an, &bn, &cn, &dn) != 4) {
  1256. ast_cli(a->fd, "MTA specification (%s) does not look like an ipaddr\n", a->argv[3]);
  1257. return CLI_SHOWUSAGE;
  1258. }
  1259. mta = an << 24 | bn << 16 | cn << 8 | dn;
  1260. if (sscanf(a->argv[7], "%3u.%3u.%3u.%3u", &an, &bn, &cn, &dn) != 4) {
  1261. ast_cli(a->fd, "SSIP specification (%s) does not look like an ipaddr\n", a->argv[7]);
  1262. return CLI_SHOWUSAGE;
  1263. }
  1264. ssip = an << 24 | bn << 16 | cn << 8 | dn;
  1265. cops_gate_cmd(GATE_SET, cmts, trid, mta, atoi(a->argv[4]), atof(a->argv[5]), atoi(a->argv[6]), ssip, atoi(a->argv[8]), NULL);
  1266. return CLI_SUCCESS;
  1267. }
  1268. static char *pktccops_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1269. {
  1270. switch (cmd) {
  1271. case CLI_INIT:
  1272. e->command = "pktccops set debug {on|off}";
  1273. e->usage =
  1274. "Usage: pktccops set debug {on|off}\n"
  1275. " Turn on/off debuging\n";
  1276. return NULL;
  1277. case CLI_GENERATE:
  1278. return NULL;
  1279. }
  1280. if (a->argc != e->args)
  1281. return CLI_SHOWUSAGE;
  1282. if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
  1283. pktccopsdebug = 1;
  1284. ast_cli(a->fd, "PktcCOPS Debugging Enabled\n");
  1285. } else if (!strncasecmp(a->argv[e->args - 1], "off", 2)) {
  1286. pktccopsdebug = 0;
  1287. ast_cli(a->fd, "PktcCOPS Debugging Disabled\n");
  1288. } else {
  1289. return CLI_SHOWUSAGE;
  1290. }
  1291. return CLI_SUCCESS;
  1292. }
  1293. static struct ast_cli_entry cli_pktccops[] = {
  1294. AST_CLI_DEFINE(pktccops_show_cmtses, "List PacketCable COPS CMTSes"),
  1295. AST_CLI_DEFINE(pktccops_show_gates, "List PacketCable COPS GATEs"),
  1296. AST_CLI_DEFINE(pktccops_show_pools, "List PacketCable MTA pools"),
  1297. AST_CLI_DEFINE(pktccops_gateset, "Send Gate-Set to cmts"),
  1298. AST_CLI_DEFINE(pktccops_gatedel, "Send Gate-Det to cmts"),
  1299. AST_CLI_DEFINE(pktccops_debug, "Enable/Disable COPS debugging")
  1300. };
  1301. static int pktccops_add_ippool(struct cops_ippool *ippool)
  1302. {
  1303. if (ippool) {
  1304. AST_LIST_LOCK(&ippool_list);
  1305. AST_LIST_INSERT_HEAD(&ippool_list, ippool, list);
  1306. AST_LIST_UNLOCK(&ippool_list);
  1307. return 0;
  1308. } else {
  1309. ast_log(LOG_WARNING, "Attempted to register NULL ippool?\n");
  1310. return -1;
  1311. }
  1312. }
  1313. static void pktccops_unregister_cmtses(void)
  1314. {
  1315. struct cops_cmts *cmts;
  1316. struct cops_gate *gate;
  1317. AST_LIST_LOCK(&cmts_list);
  1318. while ((cmts = AST_LIST_REMOVE_HEAD(&cmts_list, list))) {
  1319. if (cmts->sfd > 0) {
  1320. close(cmts->sfd);
  1321. }
  1322. free(cmts);
  1323. }
  1324. AST_LIST_UNLOCK(&cmts_list);
  1325. AST_LIST_LOCK(&gate_list);
  1326. while ((gate = AST_LIST_REMOVE_HEAD(&gate_list, list))) {
  1327. free(gate);
  1328. }
  1329. AST_LIST_UNLOCK(&gate_list);
  1330. }
  1331. static void pktccops_unregister_ippools(void)
  1332. {
  1333. struct cops_ippool *ippool;
  1334. AST_LIST_LOCK(&ippool_list);
  1335. while ((ippool = AST_LIST_REMOVE_HEAD(&ippool_list, list))) {
  1336. free(ippool);
  1337. }
  1338. AST_LIST_UNLOCK(&ippool_list);
  1339. }
  1340. static int load_module(void)
  1341. {
  1342. int res;
  1343. AST_LIST_LOCK(&cmts_list);
  1344. res = load_pktccops_config();
  1345. AST_LIST_UNLOCK(&cmts_list);
  1346. if (res == -1) {
  1347. return AST_MODULE_LOAD_DECLINE;
  1348. }
  1349. ast_cli_register_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry));
  1350. restart_pktc_thread();
  1351. return 0;
  1352. }
  1353. static int unload_module(void)
  1354. {
  1355. if (!ast_mutex_lock(&pktccops_lock)) {
  1356. if ((pktccops_thread != AST_PTHREADT_NULL) && (pktccops_thread != AST_PTHREADT_STOP)) {
  1357. pthread_cancel(pktccops_thread);
  1358. pthread_kill(pktccops_thread, SIGURG);
  1359. pthread_join(pktccops_thread, NULL);
  1360. }
  1361. pktccops_thread = AST_PTHREADT_STOP;
  1362. ast_mutex_unlock(&pktccops_lock);
  1363. } else {
  1364. ast_log(LOG_ERROR, "Unable to lock the pktccops_thread\n");
  1365. return -1;
  1366. }
  1367. ast_cli_unregister_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry));
  1368. pktccops_unregister_cmtses();
  1369. pktccops_unregister_ippools();
  1370. pktccops_thread = AST_PTHREADT_NULL;
  1371. return 0;
  1372. }
  1373. static int reload_module(void)
  1374. {
  1375. /* Prohibit unloading */
  1376. if (pktcreload) {
  1377. ast_log(LOG_NOTICE, "Previous reload in progress, please wait!\n");
  1378. return -1;
  1379. }
  1380. pktcreload = 1;
  1381. return 0;
  1382. }
  1383. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "PktcCOPS manager for MGCP",
  1384. .load = load_module,
  1385. .unload = unload_module,
  1386. .reload = reload_module,
  1387. );