res_pktccops.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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: %u\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: %u 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 %u < %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 (%us) 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=%u\n",
  725. cmts->name, (unsigned)recmsg->verflag,
  726. recmsg->opcode, (unsigned)recmsg->clienttype, recmsg->length);
  727. if (recmsg->object != NULL) {
  728. pobject = recmsg->object;
  729. while (pobject != NULL) {
  730. ast_debug(3, " OBJECT: length=%i cnum=%i ctype=%i\n", pobject->length, pobject->cnum, pobject->ctype);
  731. if (recmsg->opcode == 1 && pobject->cnum == 1 && pobject->ctype == 1 ) {
  732. cmts->handle = ntohl(*((uint32_t *) pobject->contents));
  733. ast_debug(3, " REQ client handle: %u\n", cmts->handle);
  734. cmts->state = 2;
  735. cmts->katimer = time(NULL);
  736. } else if (pobject->cnum == 9 && pobject->ctype == 1) {
  737. sobjp = pobject->contents;
  738. subscrid = 0;
  739. recvtrid = 0;
  740. scommand = 0;
  741. pktcerror = 0;
  742. actcount = 0;
  743. gateid = 0;
  744. reason = 0;
  745. subreason = 0;
  746. while (sobjp < (pobject->contents + pobject->length - 4)) {
  747. sobjlen = ntohs(*((uint16_t *) sobjp));
  748. snst = ntohs(*((uint16_t *) (sobjp + 2)));
  749. ast_debug(3, " S-Num S-type: 0x%.4x len: %i\n", (unsigned)snst, sobjlen);
  750. if (snst == 0x0101 ) {
  751. recvtrid = ntohs(*((uint16_t *) (sobjp + 4)));
  752. scommand = ntohs(*((uint16_t *) (sobjp + 6)));
  753. ast_debug(3, " Transaction Identifier command: %i trid %i\n", scommand, recvtrid);
  754. } else if (snst == 0x0201) {
  755. subscrid = ntohl(*((uint32_t *) (sobjp + 4)));
  756. ast_debug(3, " Subscriber ID: 0x%.8x\n", subscrid);
  757. } else if (snst == 0x0301) {
  758. gateid = ntohl(*((uint32_t *) (sobjp + 4)));
  759. ast_debug(3, " Gate ID: 0x%x 0x%.8x\n", gateid, gateid);
  760. } else if (snst == 0x0401) {
  761. actcount = ntohs(*((uint16_t *) (sobjp + 6)));
  762. ast_debug(3, " Activity Count: %i\n", actcount);
  763. } else if (snst == 0x0901) {
  764. pktcerror = ntohl(*((uint32_t *) (sobjp + 4)));
  765. ast_debug(3, " PKTC Error: 0x%.8x\n", pktcerror);
  766. } else if (snst == 0x0d01) {
  767. reason = ntohs(*((uint16_t *) (sobjp + 4)));
  768. subreason = ntohs(*((uint16_t *) (sobjp + 6)));
  769. ast_debug(3, " Reason: %d Subreason: %d\n", reason, subreason);
  770. }
  771. sobjp += sobjlen;
  772. if (!sobjlen)
  773. break;
  774. }
  775. if (scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE || scommand == PKTCCOPS_SCOMMAND_GATE_OPEN) {
  776. AST_LIST_LOCK(&gate_list);
  777. AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
  778. if (gate->cmts == cmts && gate->gateid == gateid) {
  779. if (scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE && gate->state != GATE_CLOSED && gate->state != GATE_CLOSED_ERR ) {
  780. ast_debug(3, "COPS Gate Close Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  781. if (subreason) {
  782. gate->state = GATE_CLOSED_ERR;
  783. PKTCCOPS_DESTROY_CURRENT_GATE;
  784. } else {
  785. gate->state = GATE_CLOSED;
  786. PKTCCOPS_DESTROY_CURRENT_GATE;
  787. }
  788. break;
  789. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_OPEN && gate->state == GATE_ALLOCATED) {
  790. ast_debug(3, "COPS Gate Open Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  791. gate->state = GATE_OPEN;
  792. if (gate->gate_open) {
  793. ast_debug(3, "Calling GATE-OPEN callback function\n");
  794. gate->gate_open(gate);
  795. gate->gate_open = NULL;
  796. }
  797. break;
  798. }
  799. }
  800. }
  801. AST_LIST_TRAVERSE_SAFE_END;
  802. AST_LIST_UNLOCK(&gate_list);
  803. } 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) {
  804. AST_LIST_LOCK(&gate_list);
  805. AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
  806. if (gate->cmts == cmts && gate->trid == recvtrid) {
  807. gate->gateid = gateid;
  808. gate->checked = time(NULL);
  809. if (scommand == PKTCCOPS_SCOMMAND_GATE_SET_ACK) {
  810. ast_debug(3, "COPS Gate Set Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  811. gate->state = GATE_ALLOCATED;
  812. if (gate->got_dq_gi) {
  813. gate->got_dq_gi(gate);
  814. gate->got_dq_gi = NULL;
  815. }
  816. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_SET_ERR) {
  817. ast_debug(3, "COPS Gate Set Error TrId: %i ErrorCode: 0x%.8x CMTS: %s\n ", recvtrid, pktcerror, cmts->name);
  818. gate->state = GATE_ALLOC_FAILED;
  819. if (gate->got_dq_gi) {
  820. gate->got_dq_gi(gate);
  821. gate->got_dq_gi = NULL;
  822. }
  823. PKTCCOPS_DESTROY_CURRENT_GATE;
  824. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ACK) {
  825. ast_debug(3, "COPS Gate Info Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  826. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ERR) {
  827. ast_debug(3, "COPS Gate Info Error Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  828. gate->state = GATE_ALLOC_FAILED;
  829. PKTCCOPS_DESTROY_CURRENT_GATE;
  830. } else if (scommand == PKTCCOPS_SCOMMAND_GATE_DELETE_ACK) {
  831. ast_debug(3, "COPS Gate Deleted Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
  832. gate->state = GATE_DELETED;
  833. PKTCCOPS_DESTROY_CURRENT_GATE;
  834. }
  835. gate->in_transaction = 0;
  836. break;
  837. }
  838. }
  839. AST_LIST_TRAVERSE_SAFE_END;
  840. AST_LIST_UNLOCK(&gate_list);
  841. }
  842. }
  843. pobject = pobject->next;
  844. }
  845. }
  846. if (recmsg->opcode == 6 && recmsg->object && recmsg->object->cnum == 11 && recmsg->object->ctype == 1) {
  847. ast_debug(3, "COPS: Client open %s\n", cmts->name);
  848. sendmsg->msg = NULL;
  849. sendmsg->verflag = 0x10;
  850. sendmsg->opcode = 7; /* Client Accept */
  851. sendmsg->clienttype = 0x8008; /* =PacketCable */
  852. sendmsg->length = COPS_HEADER_SIZE + COPS_OBJECT_HEADER_SIZE + 4;
  853. sendmsg->object = malloc(sizeof(struct pktcobj));
  854. sendmsg->object->length = 4 + COPS_OBJECT_HEADER_SIZE;
  855. sendmsg->object->cnum = 10; /* keppalive timer*/
  856. sendmsg->object->ctype = 1;
  857. sendmsg->object->contents = malloc(sizeof(uint32_t));
  858. *((uint32_t *) sendmsg->object->contents) = htonl(cmts->keepalive & 0x0000ffff);
  859. sendmsg->object->next = NULL;
  860. cops_sendmsg(cmts->sfd, sendmsg);
  861. cops_freemsg(sendmsg);
  862. } else if (recmsg->opcode == 9) {
  863. ast_debug(3, "COPS: Keepalive Request got echoing back %s\n", cmts->name);
  864. cops_sendmsg(cmts->sfd, recmsg);
  865. cmts->state = 2;
  866. cmts->katimer = time(NULL);
  867. }
  868. }
  869. if (len <= 0) {
  870. ast_debug(3, "COPS: lost connection to %s\n", cmts->name);
  871. close(cmts->sfd);
  872. cmts->sfd = -1;
  873. cmts->state = 0;
  874. }
  875. cops_freemsg(recmsg);
  876. }
  877. }
  878. AST_LIST_UNLOCK(&cmts_list);
  879. }
  880. if (pktcreload) {
  881. ast_debug(3, "Reloading pktccops...\n");
  882. AST_LIST_LOCK(&gate_list);
  883. AST_LIST_LOCK(&cmts_list);
  884. pktccops_unregister_ippools();
  885. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  886. cmts->need_delete = 1;
  887. }
  888. load_pktccops_config();
  889. AST_LIST_TRAVERSE_SAFE_BEGIN(&cmts_list, cmts, list) {
  890. if (cmts && cmts->need_delete) {
  891. AST_LIST_TRAVERSE(&gate_list, gate, list) {
  892. if (gate->cmts == cmts) {
  893. ast_debug(3, "Null gate %s\n", gate->cmts->name);
  894. gate->cmts = NULL;
  895. }
  896. gate->in_transaction = 0;
  897. }
  898. AST_LIST_UNLOCK(&gate_list);
  899. ast_debug(3, "removing cmts: %s\n", cmts->name);
  900. if (cmts->sfd > 0) {
  901. close(cmts->sfd);
  902. }
  903. AST_LIST_REMOVE_CURRENT(list);
  904. free(cmts);
  905. }
  906. }
  907. AST_LIST_TRAVERSE_SAFE_END;
  908. AST_LIST_UNLOCK(&cmts_list);
  909. AST_LIST_UNLOCK(&gate_list);
  910. pktcreload = 2;
  911. }
  912. pthread_testcancel();
  913. }
  914. return NULL;
  915. }
  916. static int restart_pktc_thread(void)
  917. {
  918. if (pktccops_thread == AST_PTHREADT_STOP) {
  919. return 0;
  920. }
  921. if (ast_mutex_lock(&pktccops_lock)) {
  922. ast_log(LOG_WARNING, "Unable to lock pktccops\n");
  923. return -1;
  924. }
  925. if (pktccops_thread == pthread_self()) {
  926. ast_mutex_unlock(&pktccops_lock);
  927. ast_log(LOG_WARNING, "Cannot kill myself\n");
  928. return -1;
  929. }
  930. if (pktccops_thread != AST_PTHREADT_NULL) {
  931. /* Wake up the thread */
  932. pthread_kill(pktccops_thread, SIGURG);
  933. } else {
  934. /* Start a new monitor */
  935. if (ast_pthread_create_background(&pktccops_thread, NULL, do_pktccops, NULL) < 0) {
  936. ast_mutex_unlock(&pktccops_lock);
  937. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  938. return -1;
  939. }
  940. }
  941. ast_mutex_unlock(&pktccops_lock);
  942. return 0;
  943. }
  944. static int load_pktccops_config(void)
  945. {
  946. static char *cfg = "res_pktccops.conf";
  947. struct ast_config *config;
  948. struct ast_variable *v;
  949. struct cops_cmts *cmts;
  950. struct cops_ippool *new_ippool;
  951. const char *host, *cat, *port;
  952. int update;
  953. int res = 0;
  954. uint16_t t1_temp, t7_temp, t8_temp;
  955. uint32_t keepalive_temp;
  956. unsigned int a,b,c,d,e,f,g,h;
  957. struct ast_flags config_flags = {0};
  958. if (!(config = ast_config_load(cfg, config_flags))) {
  959. ast_log(LOG_WARNING, "Unable to load config file res_pktccops.conf\n");
  960. return -1;
  961. }
  962. for (cat = ast_category_browse(config, NULL); cat; cat = ast_category_browse(config, cat)) {
  963. if (!strcmp(cat, "general")) {
  964. for (v = ast_variable_browse(config, cat); v; v = v->next) {
  965. if (!strcasecmp(v->name, "t1")) {
  966. t1 = atoi(v->value);
  967. } else if (!strcasecmp(v->name, "t7")) {
  968. t7 = atoi(v->value);
  969. } else if (!strcasecmp(v->name, "t8")) {
  970. t8 = atoi(v->value);
  971. } else if (!strcasecmp(v->name, "keepalive")) {
  972. keepalive = atoi(v->value);
  973. } else if (!strcasecmp(v->name, "gateinfoperiod")) {
  974. gateinfoperiod = atoi(v->value);
  975. } else if (!strcasecmp(v->name, "gatetimeout")) {
  976. gatetimeout = atoi(v->value);
  977. } else {
  978. ast_log(LOG_WARNING, "Unkown option %s in general section of res_ptkccops.conf\n", v->name);
  979. }
  980. }
  981. } else {
  982. /* Defaults */
  983. host = NULL;
  984. port = NULL;
  985. t1_temp = t1;
  986. t7_temp = t7;
  987. t8_temp = t8;
  988. keepalive_temp = keepalive;
  989. for (v = ast_variable_browse(config, cat); v; v = v->next) {
  990. if (!strcasecmp(v->name, "host")) {
  991. host = v->value;
  992. } else if (!strcasecmp(v->name, "port")) {
  993. port = v->value;
  994. } else if (!strcasecmp(v->name, "t1")) {
  995. t1_temp = atoi(v->value);
  996. } else if (!strcasecmp(v->name, "t7")) {
  997. t7_temp = atoi(v->value);
  998. } else if (!strcasecmp(v->name, "t8")) {
  999. t8_temp = atoi(v->value);
  1000. } else if (!strcasecmp(v->name, "keepalive")) {
  1001. keepalive_temp = atoi(v->value);
  1002. } else if (!strcasecmp(v->name, "pool")) {
  1003. /* we weill parse it in 2nd round */
  1004. } else {
  1005. ast_log(LOG_WARNING, "Unkown option %s in res_ptkccops.conf\n", v->name);
  1006. }
  1007. }
  1008. update = 0;
  1009. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  1010. if (!strcmp(cmts->name, cat)) {
  1011. update = 1;
  1012. break;
  1013. }
  1014. }
  1015. if (!update) {
  1016. cmts = ast_calloc(1, sizeof(*cmts));
  1017. if (!cmts) {
  1018. res = -1;
  1019. break;
  1020. }
  1021. AST_LIST_INSERT_HEAD(&cmts_list, cmts, list);
  1022. }
  1023. if (cat) {
  1024. ast_copy_string(cmts->name, cat, sizeof(cmts->name));
  1025. }
  1026. if (host) {
  1027. ast_copy_string(cmts->host, host, sizeof(cmts->host));
  1028. }
  1029. if (port) {
  1030. ast_copy_string(cmts->port, port, sizeof(cmts->port));
  1031. } else {
  1032. ast_copy_string(cmts->port, DEFAULT_COPS_PORT, sizeof(cmts->port));
  1033. }
  1034. cmts->t1 = t1_temp;
  1035. cmts->t7 = t7_temp;
  1036. cmts->t8 = t8_temp;
  1037. cmts->keepalive = keepalive_temp;
  1038. if (!update) {
  1039. cmts->state = 0;
  1040. cmts->sfd = -1;
  1041. }
  1042. cmts->need_delete = 0;
  1043. for (v = ast_variable_browse(config, cat); v; v = v->next) {
  1044. /* parse ipppol when we have cmts ptr */
  1045. if (!strcasecmp(v->name, "pool")) {
  1046. if (sscanf(v->value, "%3u.%3u.%3u.%3u %3u.%3u.%3u.%3u", &a, &b, &c, &d, &e, &f, &g, &h) == 8) {
  1047. new_ippool = ast_calloc(1, sizeof(*new_ippool));
  1048. if (!new_ippool) {
  1049. res = -1;
  1050. break;
  1051. }
  1052. new_ippool->start = a << 24 | b << 16 | c << 8 | d;
  1053. new_ippool->stop = e << 24 | f << 16 | g << 8 | h;
  1054. new_ippool->cmts = cmts;
  1055. pktccops_add_ippool(new_ippool);
  1056. } else {
  1057. ast_log(LOG_WARNING, "Invalid ip pool format in res_pktccops.conf\n");
  1058. }
  1059. }
  1060. }
  1061. }
  1062. }
  1063. ast_config_destroy(config);
  1064. return res;
  1065. }
  1066. static char *pktccops_show_cmtses(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1067. {
  1068. struct cops_cmts *cmts;
  1069. char statedesc[16];
  1070. int katimer;
  1071. switch(cmd) {
  1072. case CLI_INIT:
  1073. e->command = "pktccops show cmtses";
  1074. e->usage =
  1075. "Usage: pktccops show cmtses\n"
  1076. " List PacketCable COPS CMTSes.\n";
  1077. return NULL;
  1078. case CLI_GENERATE:
  1079. return NULL;
  1080. }
  1081. ast_cli(a->fd, "%-16s %-24s %-12s %7s\n", "Name ", "Host ", "Status ", "KA timer ");
  1082. ast_cli(a->fd, "%-16s %-24s %-12s %7s\n", "------------", "--------------------", "----------", "-----------");
  1083. AST_LIST_LOCK(&cmts_list);
  1084. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  1085. katimer = -1;
  1086. if (cmts->state == 2) {
  1087. ast_copy_string(statedesc, "Connected", sizeof(statedesc));
  1088. katimer = (int) (time(NULL) - cmts->katimer);
  1089. } else if (cmts->state == 1) {
  1090. ast_copy_string(statedesc, "Connecting", sizeof(statedesc));
  1091. } else {
  1092. ast_copy_string(statedesc, "N/A", sizeof(statedesc));
  1093. }
  1094. ast_cli(a->fd, "%-16s %-15s:%-8s %-12s %-7d\n", cmts->name, cmts->host, cmts->port, statedesc, katimer);
  1095. }
  1096. AST_LIST_UNLOCK(&cmts_list);
  1097. return CLI_SUCCESS;
  1098. }
  1099. static char *pktccops_show_gates(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1100. {
  1101. struct cops_gate *gate;
  1102. char state_desc[16];
  1103. switch(cmd) {
  1104. case CLI_INIT:
  1105. e->command = "pktccops show gates";
  1106. e->usage =
  1107. "Usage: pktccops show gates\n"
  1108. " List PacketCable COPS GATEs.\n";
  1109. return NULL;
  1110. case CLI_GENERATE:
  1111. return NULL;
  1112. }
  1113. ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"CMTS", "Gate-Id","MTA", "Status", "AllocTime", "CheckTime");
  1114. ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"--------------" ,"----------", "----------", "--------", "--------", "--------\n");
  1115. AST_LIST_LOCK(&cmts_list);
  1116. AST_LIST_LOCK(&gate_list);
  1117. AST_LIST_TRAVERSE(&gate_list, gate, list) {
  1118. if (gate->state == GATE_ALLOC_FAILED) {
  1119. ast_copy_string(state_desc, "Failed", sizeof(state_desc));
  1120. } else if (gate->state == GATE_ALLOC_PROGRESS) {
  1121. ast_copy_string(state_desc, "In Progress", sizeof(state_desc));
  1122. } else if (gate->state == GATE_ALLOCATED) {
  1123. ast_copy_string(state_desc, "Allocated", sizeof(state_desc));
  1124. } else if (gate->state == GATE_CLOSED) {
  1125. ast_copy_string(state_desc, "Closed", sizeof(state_desc));
  1126. } else if (gate->state == GATE_CLOSED_ERR) {
  1127. ast_copy_string(state_desc, "ClosedErr", sizeof(state_desc));
  1128. } else if (gate->state == GATE_OPEN) {
  1129. ast_copy_string(state_desc, "Open", sizeof(state_desc));
  1130. } else if (gate->state == GATE_DELETED) {
  1131. ast_copy_string(state_desc, "Deleted", sizeof(state_desc));
  1132. } else {
  1133. ast_copy_string(state_desc, "N/A", sizeof(state_desc));
  1134. }
  1135. ast_cli(a->fd, "%-16s 0x%.8x 0x%08x %-10s %10i %10i %u\n", (gate->cmts) ? gate->cmts->name : "null" , gate->gateid, gate->mta,
  1136. state_desc, (int) (time(NULL) - gate->allocated), (gate->checked) ? (int) (time(NULL) - gate->checked) : 0, (unsigned int) gate->in_transaction);
  1137. }
  1138. AST_LIST_UNLOCK(&cmts_list);
  1139. AST_LIST_UNLOCK(&gate_list);
  1140. return CLI_SUCCESS;
  1141. }
  1142. static char *pktccops_show_pools(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1143. {
  1144. struct cops_ippool *ippool;
  1145. char start[32];
  1146. char stop[32];
  1147. switch(cmd) {
  1148. case CLI_INIT:
  1149. e->command = "pktccops show pools";
  1150. e->usage =
  1151. "Usage: pktccops show pools\n"
  1152. " List PacketCable COPS ip pools of MTAs.\n";
  1153. return NULL;
  1154. case CLI_GENERATE:
  1155. return NULL;
  1156. }
  1157. ast_cli(a->fd, "%-16s %-18s %-7s\n", "Start ", "Stop ", "CMTS ");
  1158. ast_cli(a->fd, "%-16s %-18s %-7s\n", "----------", "----------", "--------");
  1159. AST_LIST_LOCK(&ippool_list);
  1160. AST_LIST_TRAVERSE(&ippool_list, ippool, list) {
  1161. snprintf(start, sizeof(start), "%3u.%3u.%3u.%3u", ippool->start >> 24, (ippool->start >> 16) & 0x000000ff, (ippool->start >> 8) & 0x000000ff, ippool->start & 0x000000ff);
  1162. snprintf(stop, sizeof(stop), "%3u.%3u.%3u.%3u", ippool->stop >> 24, (ippool->stop >> 16) & 0x000000ff, (ippool->stop >> 8) & 0x000000ff, ippool->stop & 0x000000ff);
  1163. ast_cli(a->fd, "%-16s %-18s %-16s\n", start, stop, ippool->cmts->name);
  1164. }
  1165. AST_LIST_UNLOCK(&ippool_list);
  1166. return CLI_SUCCESS;
  1167. }
  1168. static char *pktccops_gatedel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1169. {
  1170. int found = 0;
  1171. int trid;
  1172. uint32_t gateid;
  1173. struct cops_gate *gate;
  1174. struct cops_cmts *cmts;
  1175. switch (cmd) {
  1176. case CLI_INIT:
  1177. e->command = "pktccops gatedel";
  1178. e->usage =
  1179. "Usage: pktccops gatedel <cmts> <gateid>\n"
  1180. " Send Gate-Del to cmts.\n";
  1181. return NULL;
  1182. case CLI_GENERATE:
  1183. return NULL;
  1184. }
  1185. if (a->argc < 4)
  1186. return CLI_SHOWUSAGE;
  1187. AST_LIST_LOCK(&cmts_list);
  1188. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  1189. if (!strcmp(cmts->name, a->argv[2])) {
  1190. ast_cli(a->fd, "Found cmts: %s\n", cmts->name);
  1191. found = 1;
  1192. break;
  1193. }
  1194. }
  1195. AST_LIST_UNLOCK(&cmts_list);
  1196. if (!found)
  1197. return CLI_SHOWUSAGE;
  1198. trid = cops_trid++;
  1199. if (!sscanf(a->argv[3], "%x", &gateid)) {
  1200. ast_cli(a->fd, "bad gate specification (%s)\n", a->argv[3]);
  1201. return CLI_SHOWUSAGE;
  1202. }
  1203. found = 0;
  1204. AST_LIST_LOCK(&gate_list);
  1205. AST_LIST_TRAVERSE(&gate_list, gate, list) {
  1206. if (gate->gateid == gateid && gate->cmts == cmts) {
  1207. found = 1;
  1208. break;
  1209. }
  1210. }
  1211. if (!found) {
  1212. ast_cli(a->fd, "gate not found: %s\n", a->argv[3]);
  1213. return CLI_SHOWUSAGE;
  1214. }
  1215. AST_LIST_UNLOCK(&gate_list);
  1216. cops_gate_cmd(GATE_DEL, cmts, trid, 0, 0, 0, 0, 0, 0, gate);
  1217. return CLI_SUCCESS;
  1218. }
  1219. static char *pktccops_gateset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1220. {
  1221. int foundcmts = 0;
  1222. int trid;
  1223. unsigned int an,bn,cn,dn;
  1224. uint32_t mta, ssip;
  1225. struct cops_cmts *cmts;
  1226. switch (cmd) {
  1227. case CLI_INIT:
  1228. e->command = "pktccops gateset";
  1229. e->usage =
  1230. "Usage: pktccops gateset <cmts> <mta> <acctcount> <bitrate> <packet size> <switch ip> <switch port>\n"
  1231. " Send Gate-Set to cmts.\n";
  1232. return NULL;
  1233. case CLI_GENERATE:
  1234. return NULL;
  1235. }
  1236. if (a->argc < 9)
  1237. return CLI_SHOWUSAGE;
  1238. if (!strcmp(a->argv[2], "null")) {
  1239. cmts = NULL;
  1240. } else {
  1241. AST_LIST_LOCK(&cmts_list);
  1242. AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
  1243. if (!strcmp(cmts->name, a->argv[2])) {
  1244. ast_cli(a->fd, "Found cmts: %s\n", cmts->name);
  1245. foundcmts = 1;
  1246. break;
  1247. }
  1248. }
  1249. AST_LIST_UNLOCK(&cmts_list);
  1250. if (!foundcmts) {
  1251. ast_cli(a->fd, "CMTS not found: %s\n", a->argv[2]);
  1252. return CLI_SHOWUSAGE;
  1253. }
  1254. }
  1255. trid = cops_trid++;
  1256. if (sscanf(a->argv[3], "%3u.%3u.%3u.%3u", &an, &bn, &cn, &dn) != 4) {
  1257. ast_cli(a->fd, "MTA specification (%s) does not look like an ipaddr\n", a->argv[3]);
  1258. return CLI_SHOWUSAGE;
  1259. }
  1260. mta = an << 24 | bn << 16 | cn << 8 | dn;
  1261. if (sscanf(a->argv[7], "%3u.%3u.%3u.%3u", &an, &bn, &cn, &dn) != 4) {
  1262. ast_cli(a->fd, "SSIP specification (%s) does not look like an ipaddr\n", a->argv[7]);
  1263. return CLI_SHOWUSAGE;
  1264. }
  1265. ssip = an << 24 | bn << 16 | cn << 8 | dn;
  1266. 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);
  1267. return CLI_SUCCESS;
  1268. }
  1269. static char *pktccops_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1270. {
  1271. switch (cmd) {
  1272. case CLI_INIT:
  1273. e->command = "pktccops set debug {on|off}";
  1274. e->usage =
  1275. "Usage: pktccops set debug {on|off}\n"
  1276. " Turn on/off debuging\n";
  1277. return NULL;
  1278. case CLI_GENERATE:
  1279. return NULL;
  1280. }
  1281. if (a->argc != e->args)
  1282. return CLI_SHOWUSAGE;
  1283. if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
  1284. pktccopsdebug = 1;
  1285. ast_cli(a->fd, "PktcCOPS Debugging Enabled\n");
  1286. } else if (!strncasecmp(a->argv[e->args - 1], "off", 2)) {
  1287. pktccopsdebug = 0;
  1288. ast_cli(a->fd, "PktcCOPS Debugging Disabled\n");
  1289. } else {
  1290. return CLI_SHOWUSAGE;
  1291. }
  1292. return CLI_SUCCESS;
  1293. }
  1294. static struct ast_cli_entry cli_pktccops[] = {
  1295. AST_CLI_DEFINE(pktccops_show_cmtses, "List PacketCable COPS CMTSes"),
  1296. AST_CLI_DEFINE(pktccops_show_gates, "List PacketCable COPS GATEs"),
  1297. AST_CLI_DEFINE(pktccops_show_pools, "List PacketCable MTA pools"),
  1298. AST_CLI_DEFINE(pktccops_gateset, "Send Gate-Set to cmts"),
  1299. AST_CLI_DEFINE(pktccops_gatedel, "Send Gate-Det to cmts"),
  1300. AST_CLI_DEFINE(pktccops_debug, "Enable/Disable COPS debugging")
  1301. };
  1302. static int pktccops_add_ippool(struct cops_ippool *ippool)
  1303. {
  1304. if (ippool) {
  1305. AST_LIST_LOCK(&ippool_list);
  1306. AST_LIST_INSERT_HEAD(&ippool_list, ippool, list);
  1307. AST_LIST_UNLOCK(&ippool_list);
  1308. return 0;
  1309. } else {
  1310. ast_log(LOG_WARNING, "Attempted to register NULL ippool?\n");
  1311. return -1;
  1312. }
  1313. }
  1314. static void pktccops_unregister_cmtses(void)
  1315. {
  1316. struct cops_cmts *cmts;
  1317. struct cops_gate *gate;
  1318. AST_LIST_LOCK(&cmts_list);
  1319. while ((cmts = AST_LIST_REMOVE_HEAD(&cmts_list, list))) {
  1320. if (cmts->sfd > 0) {
  1321. close(cmts->sfd);
  1322. }
  1323. free(cmts);
  1324. }
  1325. AST_LIST_UNLOCK(&cmts_list);
  1326. AST_LIST_LOCK(&gate_list);
  1327. while ((gate = AST_LIST_REMOVE_HEAD(&gate_list, list))) {
  1328. free(gate);
  1329. }
  1330. AST_LIST_UNLOCK(&gate_list);
  1331. }
  1332. static void pktccops_unregister_ippools(void)
  1333. {
  1334. struct cops_ippool *ippool;
  1335. AST_LIST_LOCK(&ippool_list);
  1336. while ((ippool = AST_LIST_REMOVE_HEAD(&ippool_list, list))) {
  1337. free(ippool);
  1338. }
  1339. AST_LIST_UNLOCK(&ippool_list);
  1340. }
  1341. static int load_module(void)
  1342. {
  1343. int res;
  1344. AST_LIST_LOCK(&cmts_list);
  1345. res = load_pktccops_config();
  1346. AST_LIST_UNLOCK(&cmts_list);
  1347. if (res == -1) {
  1348. return AST_MODULE_LOAD_DECLINE;
  1349. }
  1350. ast_cli_register_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry));
  1351. restart_pktc_thread();
  1352. return 0;
  1353. }
  1354. static int unload_module(void)
  1355. {
  1356. if (!ast_mutex_lock(&pktccops_lock)) {
  1357. if ((pktccops_thread != AST_PTHREADT_NULL) && (pktccops_thread != AST_PTHREADT_STOP)) {
  1358. pthread_cancel(pktccops_thread);
  1359. pthread_kill(pktccops_thread, SIGURG);
  1360. pthread_join(pktccops_thread, NULL);
  1361. }
  1362. pktccops_thread = AST_PTHREADT_STOP;
  1363. ast_mutex_unlock(&pktccops_lock);
  1364. } else {
  1365. ast_log(LOG_ERROR, "Unable to lock the pktccops_thread\n");
  1366. return -1;
  1367. }
  1368. ast_cli_unregister_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry));
  1369. pktccops_unregister_cmtses();
  1370. pktccops_unregister_ippools();
  1371. pktccops_thread = AST_PTHREADT_NULL;
  1372. return 0;
  1373. }
  1374. static int reload_module(void)
  1375. {
  1376. /* Prohibit unloading */
  1377. if (pktcreload) {
  1378. ast_log(LOG_NOTICE, "Previous reload in progress, please wait!\n");
  1379. return -1;
  1380. }
  1381. pktcreload = 1;
  1382. return 0;
  1383. }
  1384. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "PktcCOPS manager for MGCP",
  1385. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  1386. .load = load_module,
  1387. .unload = unload_module,
  1388. .reload = reload_module,
  1389. );