2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2009, Attila Domjan
6 * Attila Domjan <attila.domjan.hu@gmail.com>
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.
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.
21 * \brief PacketCable COPS
23 * \author Attila Domjan <attila.domjan.hu@gmail.com>
26 * This module is an add-on to chan_mgcp. It adds support for the
27 * PacketCable MGCP variation called NCS. Res_pktccops implements COPS
28 * (RFC 2748), a protocol used to manage dynamic bandwith allocation in
29 * CMTS's (HFC gateways). When you use NCS, you need to talk COPS with
30 * the CMTS to complete the calls.
34 <defaultenabled>no</defaultenabled>
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41 #include <sys/types.h>
42 #include <sys/socket.h>
51 #include <arpa/inet.h>
54 #include "asterisk/file.h"
55 #include "asterisk/logger.h"
56 #include "asterisk/channel.h"
57 #include "asterisk/config.h"
58 #include "asterisk/options.h"
59 #include "asterisk/pbx.h"
60 #include "asterisk/module.h"
61 #include "asterisk/cli.h"
62 #include "asterisk/lock.h"
63 #define AST_API_MODULE
64 #include "asterisk/pktccops.h"
66 #define DEFAULT_COPS_PORT "2126"
68 #define COPS_HEADER_SIZE 8
69 #define COPS_OBJECT_HEADER_SIZE 4
70 #define GATE_SET_OBJ_SIZE 144
71 #define GATEID_OBJ_SIZE 8
72 #define GATE_INFO_OBJ_SIZE 24
74 #define PKTCCOPS_SCOMMAND_GATE_ALLOC 1
75 #define PKTCCOPS_SCOMMAND_GATE_ALLOC_ACK 2
76 #define PKTCCOPS_SCOMMAND_GATE_ALLOC_ERR 3
77 #define PKTCCOPS_SCOMMAND_GATE_SET 4
78 #define PKTCCOPS_SCOMMAND_GATE_SET_ACK 5
79 #define PKTCCOPS_SCOMMAND_GATE_SET_ERR 6
80 #define PKTCCOPS_SCOMMAND_GATE_INFO 7
81 #define PKTCCOPS_SCOMMAND_GATE_INFO_ACK 8
82 #define PKTCCOPS_SCOMMAND_GATE_INFO_ERR 9
83 #define PKTCCOPS_SCOMMAND_GATE_DELETE 10
84 #define PKTCCOPS_SCOMMAND_GATE_DELETE_ACK 11
85 #define PKTCCOPS_SCOMMAND_GATE_DELETE_ERR 12
86 #define PKTCCOPS_SCOMMAND_GATE_OPEN 13
87 #define PKTCCOPS_SCOMMAND_GATE_CLOSE 14
89 AST_MUTEX_DEFINE_STATIC(pktccops_lock);
90 static pthread_t pktccops_thread = AST_PTHREADT_NULL;
91 static uint16_t cops_trid = 0;
102 unsigned char verflag;
103 unsigned char opcode;
106 struct pktcobj *object;
107 char *msg; /* != NULL if not packet cable message received */
111 int direction; /* 0-DS, 1-US */
113 int flags; /* 0x00 */
114 int sessionclass; /* normal voip: 0x01, high priority voip: 0x02, unspecified: 0x00 */
123 uint32_t r; /* Token Bucket Rate */
124 uint32_t b; /* token Bucket Size */
125 uint32_t p; /* Peak Data Rate */
126 uint32_t m; /* Minimum Policed Size*/
127 uint32_t mm; /* Maximum Policed Size */
129 uint32_t s; /* Allowable Jitter*/
134 AST_LIST_ENTRY(cops_cmts) list;
152 AST_LIST_ENTRY(cops_ippool) list;
155 struct cops_cmts *cmts;
158 static uint16_t t1 = 250;
159 static uint16_t t7 = 200;
160 static uint16_t t8 = 300;
161 static uint32_t keepalive = 60;
162 static int pktccopsdebug = 0;
163 static int pktcreload = 0;
164 static int gateinfoperiod = 60;
165 static int gatetimeout = 150;
167 AST_LIST_HEAD_STATIC(cmts_list, cops_cmts);
168 AST_LIST_HEAD_STATIC(ippool_list, cops_ippool);
169 AST_LIST_HEAD_STATIC(gate_list, cops_gate);
171 static int pktccops_add_ippool(struct cops_ippool *ippool);
172 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);
173 static void pktccops_unregister_ippools(void);
174 static int load_pktccops_config(void);
176 static uint32_t ftoieeef(float n)
183 static uint16_t cops_constructgatespec(struct gatespec *gs, char *res)
189 *res = (char) gs->direction;
190 *(res + 1) = (char) gs->protocolid;
191 *(res + 2) = (char) gs->flags;
192 *(res + 3) = (char) gs->sessionclass;
194 *((uint32_t *) (res + 4)) = gs->srcip;
195 *((uint32_t *) (res + 8)) = gs->dstip;
197 *((uint16_t *) (res + 12)) = gs->srcp;
198 *((uint16_t *) (res + 14)) = gs->dstp;
200 *(res + 16) = (char) gs->diffserv;
201 *(res + 17) = 0; /* reserved */
202 *(res + 18) = 0; /* reserved */
203 *(res + 19) = 0; /* reserved */
205 *((uint16_t *) (res + 20)) = gs->t1;
206 *(res + 22) = 0; /* reserved */
207 *(res + 23) = 0; /* reserved */
209 *((uint16_t *) (res + 24)) = gs->t7;
210 *((uint16_t *) (res + 26)) = gs->t8;
212 *((uint32_t *) (res + 28)) = gs->r;
213 *((uint32_t *) (res + 32)) = gs->b;
214 *((uint32_t *) (res + 36)) = gs->p;
215 *((uint32_t *) (res + 40)) = gs->m;
216 *((uint32_t *) (res + 44)) = gs->mm;
217 *((uint32_t *) (res + 48)) = gs->rate;
218 *((uint32_t *) (res + 52)) = gs->s;
219 return 56; /* length */
222 static uint16_t cops_construct_gate (int cmd, char *p, uint16_t trid,
223 uint32_t mtahost, uint32_t actcount, float rate, uint32_t psizegateid,
224 uint32_t ssip, uint16_t ssport, uint32_t gateid, struct cops_cmts *cmts)
229 ast_debug(3, "CMD: %d\n", cmd);
231 /* Transaction Identifier 8 octets */
233 *(p + offset++) = 8; /* length */
234 *(p + offset++) = 1; /* snum */
235 *(p + offset++) = 1; /* stype */
236 *((uint16_t *) (p + offset)) = htons(trid);
239 *(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 */
241 /*Subscriper Identifier 8 octets */
243 *(p + offset++) = 8; /* length */
244 *(p + offset++) = 2; /* snum */
245 *(p + offset++) = 1; /* stype */
246 *((uint32_t *) (p + offset)) = htonl(mtahost);
249 if (cmd == GATE_INFO || cmd == GATE_SET_HAVE_GATEID || cmd == GATE_DEL) {
250 /* Gate ID 8 Octets */
252 *(p + offset++) = 8; /* length */
253 *(p + offset++) = 3; /* snum */
254 *(p + offset++) = 1; /* stype */
255 *((uint32_t *) (p + offset)) = htonl(gateid);
257 if (cmd == GATE_INFO || cmd == GATE_DEL) {
263 /* Activity Count 8 octets */
265 *(p + offset++) = 8; /* length */
266 *(p + offset++) = 4; /* snum */
267 *(p + offset++) = 1; /* stype */
268 *((uint32_t *) (p + offset)) = htonl(actcount);
272 /* Gate Spec 2*60 Octets */
273 gs.direction = 0; /* DS */
274 gs.protocolid = 17; /* UDP */
277 gs.srcip = htonl(ssip);
278 gs.dstip = htonl(mtahost);
279 gs.srcp = htons(ssport);
281 /* gs.diffserv = 0xa0;*/
283 gs.t1 = htons(cmts->t1);
284 gs.t7 = htons(cmts->t7);
285 gs.t8 = htons(cmts->t8);
286 gs.r = ftoieeef(rate);
287 gs.b = ftoieeef(psizegateid);
288 gs.p = ftoieeef(rate);
289 gs.m = htonl((uint32_t) psizegateid);
290 gs.mm = htonl((uint32_t) psizegateid);
291 gs.rate = ftoieeef(rate);
297 *(p + offset) = 60; /* length */
299 *(p + offset) = 5; /* snum */
301 *(p + offset) = 1; /* stype */
303 offset += cops_constructgatespec(&gs, p + offset);
306 gs.direction = 1; /* US */
307 gs.srcip = htonl(mtahost);
308 gs.dstip = htonl(ssip);
310 gs.dstp = htons(ssport);
313 *(p + offset) = 60; /* length */
315 *(p + offset) = 5; /* snum */
317 *(p + offset) = 1; /* stype */
319 offset += cops_constructgatespec(&gs, p + offset);
324 static int cops_getmsg (int sfd, struct copsmsg *recmsg)
327 char buf[COPS_HEADER_SIZE];
328 struct pktcobj *pobject = NULL;
329 uint16_t *ubuf = (uint16_t *) buf;
331 recmsg->object = NULL;
332 len = recv(sfd, buf, COPS_HEADER_SIZE, MSG_DONTWAIT);
333 if (len < COPS_HEADER_SIZE) {
336 recmsg->verflag = *buf;
337 recmsg->opcode = *(buf + 1);
338 recmsg->clienttype = ntohs(*((uint16_t *) (buf + 2)));
339 recmsg->length = ntohl(*((uint32_t *) (buf + 4)));
341 if (recmsg->clienttype != 0x8008 ) {
342 if (!(recmsg->msg = malloc(recmsg->length - COPS_HEADER_SIZE))) {
345 lent = recv(sfd, recmsg->msg, recmsg->length - COPS_HEADER_SIZE, MSG_DONTWAIT);
346 if (lent < recmsg->length - COPS_HEADER_SIZE) {
351 /* PacketCable Objects */
352 while (len < recmsg->length) {
353 if (len == COPS_HEADER_SIZE) {
355 if (!(recmsg->object = malloc(sizeof(struct pktcobj)))) {
358 pobject = recmsg->object;
360 if (!(pobject->next = malloc(sizeof(struct pktcobj)))) {
363 pobject = pobject->next;
365 pobject->next = NULL;
366 lent = recv(sfd, buf, COPS_OBJECT_HEADER_SIZE, MSG_DONTWAIT);
367 if (lent < COPS_OBJECT_HEADER_SIZE) {
368 ast_debug(3, "Too short object header len: %i\n", lent);
372 pobject->length = ntohs(*ubuf);
373 pobject->cnum = *(buf + 2);
374 pobject->ctype = *(buf + 3);
375 if (!(pobject->contents = malloc(pobject->length - COPS_OBJECT_HEADER_SIZE))) {
378 lent = recv(sfd, pobject->contents, pobject->length - COPS_OBJECT_HEADER_SIZE, MSG_DONTWAIT);
379 if (lent < pobject->length - COPS_OBJECT_HEADER_SIZE) {
380 ast_debug(3, "Too short object content len: %i\n", lent);
389 static int cops_sendmsg (int sfd, struct copsmsg * sendmsg)
393 struct pktcobj *pobject;
399 ast_debug(3, "COPS: sending opcode: %i len: %i\n", sendmsg->opcode, sendmsg->length);
400 if (sendmsg->length < COPS_HEADER_SIZE) {
401 ast_log(LOG_WARNING, "COPS: invalid msg size!!!\n");
404 if (!(buf = malloc((size_t) sendmsg->length))) {
407 *buf = sendmsg->verflag ;
408 *(buf + 1) = sendmsg->opcode;
409 *((uint16_t *)(buf + 2)) = htons(sendmsg->clienttype);
410 *((uint32_t *)(buf + 4)) = htonl(sendmsg->length);
412 if (sendmsg->msg != NULL) {
413 memcpy(buf + COPS_HEADER_SIZE, sendmsg->msg, sendmsg->length - COPS_HEADER_SIZE);
414 } else if (sendmsg->object != NULL) {
416 pobject = sendmsg->object;
417 while(pobject != NULL) {
418 ast_debug(3, "COPS: Sending Object : cnum: %i ctype %i len: %i\n", pobject->cnum, pobject->ctype, pobject->length);
419 if (sendmsg->length < bufpos + pobject->length) {
420 ast_log(LOG_WARNING, "COPS: Invalid msg size len: %i objectlen: %i\n", sendmsg->length, pobject->length);
424 *(uint16_t *) (buf + bufpos) = htons(pobject->length);
425 *(buf + bufpos + 2) = pobject->cnum;
426 *(buf + bufpos + 3) = pobject->ctype;
427 if (sendmsg->length < pobject->length + bufpos) {
428 ast_log(LOG_WARNING, "COPS: Error sum of object len more the msg len %i < %i\n", sendmsg->length, pobject->length + bufpos);
432 memcpy((buf + bufpos + 4), pobject->contents, pobject->length - 4);
433 bufpos += pobject->length;
434 pobject = pobject->next;
439 #ifdef HAVE_MSG_NOSIGNAL
440 #define SENDFLAGS MSG_NOSIGNAL | MSG_DONTWAIT
442 #define SENDFLAGS MSG_DONTWAIT
444 if (send(sfd, buf, sendmsg->length, SENDFLAGS) == -1) {
445 ast_log(LOG_WARNING, "COPS: Send failed errno=%i\n", errno);
454 static void cops_freemsg(struct copsmsg *p)
456 struct pktcobj *pnext;
459 while (p->object != NULL) {
460 pnext = p->object->next;
461 ast_free(p->object->contents);
462 p->object->contents = NULL;
469 struct cops_gate * AST_OPTIONAL_API_NAME(ast_pktccops_gate_alloc)(int cmd,
470 struct cops_gate *gate, uint32_t mta, uint32_t actcount, float bitrate,
471 uint32_t psize, uint32_t ssip, uint16_t ssport,
472 int (* const got_dq_gi) (struct cops_gate *gate),
473 int (* const gate_remove) (struct cops_gate *gate))
479 if (cmd == GATE_SET_HAVE_GATEID && gate) {
480 ast_debug(3, "------- gate modify gateid 0x%x ssip: 0x%x\n", gate->gateid, ssip);
481 /* TODO implement it */
482 ast_log(LOG_WARNING, "Modify GateID not implemented\n");
485 if ((gate = cops_gate_cmd(cmd, NULL, cops_trid++, mta, actcount, bitrate, psize, ssip, ssport, gate))) {
486 ast_debug(3, "COPS: Allocating gate for mta: 0x%x\n", mta);
487 gate->got_dq_gi = got_dq_gi;
488 gate->gate_remove = gate_remove;
491 ast_debug(3, "COPS: Couldn't allocate gate for mta: 0x%x\n", mta);
496 static struct cops_gate *cops_gate_cmd(int cmd, struct cops_cmts *cmts,
497 uint16_t trid, uint32_t mta, uint32_t actcount, float bitrate,
498 uint32_t psize, uint32_t ssip, uint16_t ssport, struct cops_gate *gate)
500 struct copsmsg *gateset;
501 struct cops_gate *new;
502 struct cops_ippool *ippool;
504 if (cmd == GATE_DEL) {
513 AST_LIST_LOCK(&ippool_list);
514 AST_LIST_TRAVERSE(&ippool_list, ippool, list) {
515 if (mta >= ippool->start && mta <= ippool->stop) {
520 AST_LIST_UNLOCK(&ippool_list);
522 ast_log(LOG_WARNING, "COPS: couldn't find cmts for mta: 0x%x\n", mta);
526 ast_log(LOG_WARNING, "CMTS: %s not connected\n", cmts->name);
531 if (cmd == GATE_SET) {
532 new = ast_calloc(1, sizeof(*new));
536 new->state = GATE_ALLOC_PROGRESS;
537 new->checked = time(NULL);
538 new->allocated = time(NULL);
540 new->got_dq_gi = NULL;
541 new->gate_remove = NULL;
542 new->gate_open = NULL;
543 new->tech_pvt = NULL;
545 AST_LIST_LOCK(&gate_list);
546 AST_LIST_INSERT_HEAD(&gate_list, new, list);
547 AST_LIST_UNLOCK(&gate_list);
555 gate->in_transaction = time(NULL);
557 if (!(gateset = malloc(sizeof(struct copsmsg)))) {
562 gateset->verflag = 0x10;
563 gateset->opcode = 2; /* Decision */
564 gateset->clienttype = 0x8008; /* =PacketCable */
567 gateset->object = malloc(sizeof(struct pktcobj));
568 if (!gateset->object) {
569 cops_freemsg(gateset);
573 gateset->object->length = COPS_OBJECT_HEADER_SIZE + 4;
574 gateset->object->cnum = 1; /* Handle */
575 gateset->object->ctype = 1; /* client */
576 if (!(gateset->object->contents = malloc(sizeof(uint32_t)))) {
577 cops_freemsg(gateset);
581 *((uint32_t *) gateset->object->contents) = htonl(cmts->handle);
584 if (!(gateset->object->next = malloc(sizeof(struct pktcobj)))) {
585 cops_freemsg(gateset);
589 gateset->object->next->length = COPS_OBJECT_HEADER_SIZE + 4;
590 gateset->object->next->cnum = 2; /* Context */
591 gateset->object->next->ctype = 1; /* Context */
592 if (!(gateset->object->next->contents = malloc(sizeof(uint32_t)))) {
593 cops_freemsg(gateset);
597 *((uint32_t *) gateset->object->next->contents) = htonl(0x00080000); /* R-Type = 8 configuration request, M-Type = 0 */
599 /* Decision Object: Flags */
600 if (!(gateset->object->next->next = malloc(sizeof(struct pktcobj)))) {
601 cops_freemsg(gateset);
605 gateset->object->next->next->length = COPS_OBJECT_HEADER_SIZE + 4;
606 gateset->object->next->next->cnum = 6; /* Decision */
607 gateset->object->next->next->ctype = 1; /* Flags */
608 if (!(gateset->object->next->next->contents = malloc(sizeof(uint32_t)))) {
609 cops_freemsg(gateset);
613 *((uint32_t *) gateset->object->next->next->contents) = htonl(0x00010001); /* Install, Trigger Error */
615 /* Decision Object: Data */
616 if (!(gateset->object->next->next->next = malloc(sizeof(struct pktcobj)))) {
617 cops_freemsg(gateset);
621 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);
622 gateset->object->next->next->next->cnum = 6; /* Decision */
623 gateset->object->next->next->next->ctype = 4; /* Decision Data */
624 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));
625 if (!gateset->object->next->next->next->contents) {
626 cops_freemsg(gateset);
630 gateset->object->next->next->next->next = NULL;
632 gateset->length = COPS_HEADER_SIZE + gateset->object->length + gateset->object->next->length + gateset->object->next->next->length + gateset->object->next->next->next->length;
634 if ((cmd == GATE_INFO || cmd == GATE_SET_HAVE_GATEID || cmd == GATE_DEL) && gate) {
635 ast_debug(1, "Construct gate with gateid: 0x%x\n", gate->gateid);
636 cops_construct_gate(cmd, gateset->object->next->next->next->contents, trid, mta, actcount, bitrate, psize, ssip, ssport, gate->gateid, cmts);
638 ast_debug(1, "Construct new gate\n");
639 cops_construct_gate(cmd, gateset->object->next->next->next->contents, trid, mta, actcount, bitrate, psize, ssip, ssport, 0, cmts);
642 ast_debug(3, "send cmd\n");
644 cops_sendmsg(cmts->sfd, gateset);
645 cops_freemsg(gateset);
650 static int cops_connect(char *host, char *port)
652 int s, sfd = -1, flags;
653 struct addrinfo hints;
655 struct addrinfo *result;
656 #ifdef HAVE_SO_NOSIGPIPE
660 memset(&hints, 0, sizeof(struct addrinfo));
662 hints.ai_family = AF_UNSPEC;
663 hints.ai_socktype = SOCK_STREAM;
665 hints.ai_protocol = 0;
667 s = getaddrinfo(host, port, &hints, &result);
669 ast_log(LOG_WARNING, "COPS: getaddrinfo: %s\n", gai_strerror(s));
673 for (rp = result; rp != NULL; rp = rp->ai_next) {
674 sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
676 ast_log(LOG_WARNING, "Failed socket\n");
678 flags = fcntl(sfd, F_GETFL);
679 fcntl(sfd, F_SETFL, flags | O_NONBLOCK);
680 #ifdef HAVE_SO_NOSIGPIPE
681 setsockopt(sfd, SO_SOCKET, SO_NOSIGPIPE, &trueval, sizeof(trueval));
683 connect(sfd, rp->ai_addr, rp->ai_addrlen);
685 ast_log(LOG_WARNING, "Failed connect\n");
688 freeaddrinfo(result);
690 ast_debug(3, "Connecting to cmts: %s:%s\n", host, port);
694 #define PKTCCOPS_DESTROY_CURRENT_GATE \
695 AST_LIST_REMOVE_CURRENT(list); \
696 if (gate->gate_remove) { \
697 gate->gate_remove(gate); \
701 static void *do_pktccops(void *data)
704 struct copsmsg *recmsg, *sendmsg;
705 struct copsmsg recmsgb, sendmsgb;
708 struct pktcobj *pobject;
709 struct cops_cmts *cmts;
710 struct cops_gate *gate;
712 uint16_t snst, sobjlen, scommand, recvtrid, actcount, reason, subreason;
713 uint32_t gateid, subscrid, pktcerror;
714 time_t last_exec = 0;
719 ast_debug(3, "COPS: thread started\n");
726 AST_LIST_LOCK(&cmts_list);
727 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
728 if (last_exec != time(NULL)) {
729 if (cmts->state == 2 && cmts->katimer + cmts->keepalive < time(NULL)) {
730 ast_log(LOG_WARNING, "KA timer (%is) expired cmts: %s\n", cmts->keepalive, cmts->name);
738 FD_SET(cmts->sfd, &rfds);
739 if (cmts->sfd > nfds) nfds = cmts->sfd;
741 cmts->sfd = cops_connect(cmts->host, cmts->port);
745 FD_SET(cmts->sfd, &rfds);
746 if (cmts->sfd > nfds) nfds = cmts->sfd;
751 AST_LIST_UNLOCK(&cmts_list);
753 if (last_exec != time(NULL)) {
754 last_exec = time(NULL);
755 AST_LIST_LOCK(&gate_list);
756 AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
758 if (gate->deltimer && gate->deltimer < time(NULL)) {
759 gate->deltimer = time(NULL) + 5;
760 gate->trid = cops_trid++;
761 cops_gate_cmd(GATE_DEL, gate->cmts, gate->trid, 0, 0, 0, 0, 0, 0, gate);
762 ast_debug(3, "COPS: requested Gate-Del: CMTS: %s gateid: 0x%x\n", (gate->cmts) ? gate->cmts->name : "null", gate->gateid);
764 if (time(NULL) - gate->checked > gatetimeout) {
765 ast_debug(3, "COPS: remove from list GATE, CMTS: %s gateid: 0x%x\n", (gate->cmts) ? gate->cmts->name : "null", gate->gateid);
766 gate->state = GATE_TIMEOUT;
767 PKTCCOPS_DESTROY_CURRENT_GATE;
768 } else if (time(NULL) - gate->checked > gateinfoperiod && (gate->state == GATE_ALLOCATED || gate->state == GATE_OPEN)) {
769 if (gate->cmts && (!gate->in_transaction || ( gate->in_transaction + 5 ) < time(NULL))) {
770 gate->trid = cops_trid++;
771 ast_debug(3, "COPS: Gate-Info send to CMTS: %s gateid: 0x%x\n", gate->cmts->name, gate->gateid);
772 cops_gate_cmd(GATE_INFO, gate->cmts, gate->trid, gate->mta, 0, 0, 0, 0, 0, gate);
777 AST_LIST_TRAVERSE_SAFE_END;
778 AST_LIST_UNLOCK(&gate_list);
781 if (pktcreload == 2) {
784 if ((res = select(nfds + 1, &rfds, NULL, NULL, &tv))) {
785 AST_LIST_LOCK(&cmts_list);
786 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
787 if (FD_ISSET(cmts->sfd, &rfds)) {
788 len = cops_getmsg(cmts->sfd, recmsg);
790 ast_debug(3, "COPS: got from %s:\n Header: versflag=0x%.2x opcode=%i clienttype=0x%.4x msglength=%i\n",
791 cmts->name, recmsg->verflag, recmsg->opcode, recmsg->clienttype, recmsg->length);
792 if (recmsg->object != NULL) {
793 pobject = recmsg->object;
794 while (pobject != NULL) {
795 ast_debug(3, " OBJECT: length=%i cnum=%i ctype=%i\n", pobject->length, pobject->cnum, pobject->ctype);
796 if (recmsg->opcode == 1 && pobject->cnum == 1 && pobject->ctype == 1 ) {
797 cmts->handle = ntohl(*((uint32_t *) pobject->contents));
798 ast_debug(3, " REQ client handle: %i\n", cmts->handle);
800 cmts->katimer = time(NULL);
801 } else if (pobject->cnum == 9 && pobject->ctype == 1) {
802 sobjp = pobject->contents;
811 while (sobjp < (pobject->contents + pobject->length - 4)) {
812 sobjlen = ntohs(*((uint16_t *) sobjp));
813 snst = ntohs(*((uint16_t *) (sobjp + 2)));
814 ast_debug(3, " S-Num S-type: 0x%.4x len: %i\n", snst, sobjlen);
815 if (snst == 0x0101 ) {
816 recvtrid = ntohs(*((uint16_t *) (sobjp + 4)));
817 scommand = ntohs(*((uint16_t *) (sobjp + 6)));
818 ast_debug(3, " Transaction Identifier command: %i trid %i\n", scommand, recvtrid);
819 } else if (snst == 0x0201) {
820 subscrid = ntohl(*((uint32_t *) (sobjp + 4)));
821 ast_debug(3, " Subscriber ID: 0x%.8x\n", subscrid);
822 } else if (snst == 0x0301) {
823 gateid = ntohl(*((uint32_t *) (sobjp + 4)));
824 ast_debug(3, " Gate ID: 0x%x 0x%.8x\n", gateid, gateid);
825 } else if (snst == 0x0401) {
826 actcount = ntohs(*((uint16_t *) (sobjp + 6)));
827 ast_debug(3, " Activity Count: %i\n", actcount);
828 } else if (snst == 0x0901) {
829 pktcerror = ntohl(*((uint32_t *) (sobjp + 4)));
830 ast_debug(3, " PKTC Error: 0x%.8x\n", pktcerror);
831 } else if (snst == 0x0d01) {
832 reason = ntohs(*((uint16_t *) (sobjp + 4)));
833 subreason = ntohs(*((uint16_t *) (sobjp + 6)));
834 ast_debug(3, " Reason: %u Subreason: %u\n", reason, subreason);
840 if (scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE || scommand == PKTCCOPS_SCOMMAND_GATE_OPEN) {
841 AST_LIST_LOCK(&gate_list);
842 AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
843 if (gate->cmts == cmts && gate->gateid == gateid) {
844 if (scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE && gate->state != GATE_CLOSED && gate->state != GATE_CLOSED_ERR ) {
845 ast_debug(3, "COPS Gate Close Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
847 gate->state = GATE_CLOSED_ERR;
848 PKTCCOPS_DESTROY_CURRENT_GATE;
850 gate->state = GATE_CLOSED;
851 PKTCCOPS_DESTROY_CURRENT_GATE;
854 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_OPEN && gate->state == GATE_ALLOCATED) {
855 ast_debug(3, "COPS Gate Open Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
856 gate->state = GATE_OPEN;
857 if (gate->gate_open) {
858 ast_debug(3, "Calling GATE-OPEN callback function\n");
859 gate->gate_open(gate);
860 gate->gate_open = NULL;
866 AST_LIST_TRAVERSE_SAFE_END;
867 AST_LIST_UNLOCK(&gate_list);
868 } 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) {
869 AST_LIST_LOCK(&gate_list);
870 AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
871 if (gate->cmts == cmts && gate->trid == recvtrid) {
872 gate->gateid = gateid;
873 gate->checked = time(NULL);
874 if (scommand == PKTCCOPS_SCOMMAND_GATE_SET_ACK) {
875 ast_debug(3, "COPS Gate Set Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
876 gate->state = GATE_ALLOCATED;
877 if (gate->got_dq_gi) {
878 gate->got_dq_gi(gate);
879 gate->got_dq_gi = NULL;
881 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_SET_ERR) {
882 ast_debug(3, "COPS Gate Set Error TrId: %i ErrorCode: 0x%.8x CMTS: %s\n ", recvtrid, pktcerror, cmts->name);
883 gate->state = GATE_ALLOC_FAILED;
884 if (gate->got_dq_gi) {
885 gate->got_dq_gi(gate);
886 gate->got_dq_gi = NULL;
888 PKTCCOPS_DESTROY_CURRENT_GATE;
889 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ACK) {
890 ast_debug(3, "COPS Gate Info Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
891 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ERR) {
892 ast_debug(3, "COPS Gate Info Error Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
893 gate->state = GATE_ALLOC_FAILED;
894 PKTCCOPS_DESTROY_CURRENT_GATE;
895 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_DELETE_ACK) {
896 ast_debug(3, "COPS Gate Deleted Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
897 gate->state = GATE_DELETED;
898 PKTCCOPS_DESTROY_CURRENT_GATE;
900 gate->in_transaction = 0;
904 AST_LIST_TRAVERSE_SAFE_END;
905 AST_LIST_UNLOCK(&gate_list);
908 pobject = pobject->next;
912 if (recmsg->opcode == 6 && recmsg->object && recmsg->object->cnum == 11 && recmsg->object->ctype == 1) {
913 ast_debug(3, "COPS: Client open %s\n", cmts->name);
915 sendmsg->verflag = 0x10;
916 sendmsg->opcode = 7; /* Client Accept */
917 sendmsg->clienttype = 0x8008; /* =PacketCable */
918 sendmsg->length = COPS_HEADER_SIZE + COPS_OBJECT_HEADER_SIZE + 4;
919 sendmsg->object = malloc(sizeof(struct pktcobj));
920 sendmsg->object->length = 4 + COPS_OBJECT_HEADER_SIZE;
921 sendmsg->object->cnum = 10; /* keppalive timer*/
922 sendmsg->object->ctype = 1;
923 sendmsg->object->contents = malloc(sizeof(uint32_t));
924 *((uint32_t *) sendmsg->object->contents) = htonl(cmts->keepalive & 0x0000ffff);
925 sendmsg->object->next = NULL;
926 cops_sendmsg(cmts->sfd, sendmsg);
927 cops_freemsg(sendmsg);
928 } else if (recmsg->opcode == 9) {
929 ast_debug(3, "COPS: Keepalive Request got echoing back %s\n", cmts->name);
930 cops_sendmsg(cmts->sfd, recmsg);
932 cmts->katimer = time(NULL);
936 ast_debug(3, "COPS: lost connection to %s\n", cmts->name);
941 cops_freemsg(recmsg);
944 AST_LIST_UNLOCK(&cmts_list);
947 ast_debug(3, "Reloading pktccops...\n");
948 AST_LIST_LOCK(&gate_list);
949 AST_LIST_LOCK(&cmts_list);
950 pktccops_unregister_ippools();
951 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
952 cmts->need_delete = 1;
954 load_pktccops_config();
955 AST_LIST_TRAVERSE_SAFE_BEGIN(&cmts_list, cmts, list) {
956 if (cmts && cmts->need_delete) {
957 AST_LIST_TRAVERSE(&gate_list, gate, list) {
958 if (gate->cmts == cmts) {
959 ast_debug(3, "Null gate %s\n", gate->cmts->name);
962 gate->in_transaction = 0;
964 AST_LIST_UNLOCK(&gate_list);
965 ast_debug(3, "removing cmts: %s\n", cmts->name);
969 AST_LIST_REMOVE_CURRENT(list);
973 AST_LIST_TRAVERSE_SAFE_END;
974 AST_LIST_UNLOCK(&cmts_list);
975 AST_LIST_UNLOCK(&gate_list);
978 pthread_testcancel();
983 static int restart_pktc_thread(void)
985 if (pktccops_thread == AST_PTHREADT_STOP) {
988 if (ast_mutex_lock(&pktccops_lock)) {
989 ast_log(LOG_WARNING, "Unable to lock pktccops\n");
992 if (pktccops_thread == pthread_self()) {
993 ast_mutex_unlock(&pktccops_lock);
994 ast_log(LOG_WARNING, "Cannot kill myself\n");
997 if (pktccops_thread != AST_PTHREADT_NULL) {
998 /* Wake up the thread */
999 pthread_kill(pktccops_thread, SIGURG);
1001 /* Start a new monitor */
1002 if (ast_pthread_create_background(&pktccops_thread, NULL, do_pktccops, NULL) < 0) {
1003 ast_mutex_unlock(&pktccops_lock);
1004 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1008 ast_mutex_unlock(&pktccops_lock);
1012 static int load_pktccops_config(void)
1014 static char *cfg = "res_pktccops.conf";
1015 struct ast_config *config;
1016 struct ast_variable *v;
1017 struct cops_cmts *cmts;
1018 struct cops_ippool *new_ippool;
1019 const char *host, *cat, *port;
1022 uint16_t t1_temp, t7_temp, t8_temp;
1023 uint32_t keepalive_temp;
1024 unsigned int a,b,c,d,e,f,g,h;
1025 struct ast_flags config_flags = {0};
1027 if (!(config = ast_config_load(cfg, config_flags))) {
1028 ast_log(LOG_WARNING, "Unable to load config file res_pktccops.conf\n");
1031 for (cat = ast_category_browse(config, NULL); cat; cat = ast_category_browse(config, cat)) {
1032 if (!strcmp(cat, "general")) {
1033 for (v = ast_variable_browse(config, cat); v; v = v->next) {
1034 if (!strcasecmp(v->name, "t1")) {
1035 t1 = atoi(v->value);
1036 } else if (!strcasecmp(v->name, "t7")) {
1037 t7 = atoi(v->value);
1038 } else if (!strcasecmp(v->name, "t8")) {
1039 t8 = atoi(v->value);
1040 } else if (!strcasecmp(v->name, "keepalive")) {
1041 keepalive = atoi(v->value);
1042 } else if (!strcasecmp(v->name, "gateinfoperiod")) {
1043 gateinfoperiod = atoi(v->value);
1044 } else if (!strcasecmp(v->name, "gatetimeout")) {
1045 gatetimeout = atoi(v->value);
1047 ast_log(LOG_WARNING, "Unkown option %s in general section of res_ptkccops.conf\n", v->name);
1058 keepalive_temp = keepalive;
1060 for (v = ast_variable_browse(config, cat); v; v = v->next) {
1061 if (!strcasecmp(v->name, "host")) {
1063 } else if (!strcasecmp(v->name, "port")) {
1065 } else if (!strcasecmp(v->name, "t1")) {
1066 t1_temp = atoi(v->value);
1067 } else if (!strcasecmp(v->name, "t7")) {
1068 t7_temp = atoi(v->value);
1069 } else if (!strcasecmp(v->name, "t8")) {
1070 t8_temp = atoi(v->value);
1071 } else if (!strcasecmp(v->name, "keepalive")) {
1072 keepalive_temp = atoi(v->value);
1073 } else if (!strcasecmp(v->name, "pool")) {
1074 /* we weill parse it in 2nd round */
1076 ast_log(LOG_WARNING, "Unkown option %s in res_ptkccops.conf\n", v->name);
1081 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
1082 if (!strcmp(cmts->name, cat)) {
1089 cmts = ast_calloc(1, sizeof(*cmts));
1094 AST_LIST_INSERT_HEAD(&cmts_list, cmts, list);
1097 ast_copy_string(cmts->name, cat, sizeof(cmts->name));
1100 ast_copy_string(cmts->host, host, sizeof(cmts->host));
1103 ast_copy_string(cmts->port, port, sizeof(cmts->port));
1105 ast_copy_string(cmts->port, DEFAULT_COPS_PORT, sizeof(cmts->port));
1111 cmts->keepalive = keepalive_temp;
1116 cmts->need_delete = 0;
1117 for (v = ast_variable_browse(config, cat); v; v = v->next) {
1118 /* parse ipppol when we have cmts ptr */
1119 if (!strcasecmp(v->name, "pool")) {
1120 if (sscanf(v->value, "%3u.%3u.%3u.%3u %3u.%3u.%3u.%3u", &a, &b, &c, &d, &e, &f, &g, &h) == 8) {
1121 new_ippool = ast_calloc(1, sizeof(*new_ippool));
1126 new_ippool->start = a << 24 | b << 16 | c << 8 | d;
1127 new_ippool->stop = e << 24 | f << 16 | g << 8 | h;
1128 new_ippool->cmts = cmts;
1129 pktccops_add_ippool(new_ippool);
1131 ast_log(LOG_WARNING, "Invalid ip pool format in res_pktccops.conf\n");
1137 ast_config_destroy(config);
1141 static char *pktccops_show_cmtses(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1143 struct cops_cmts *cmts;
1149 e->command = "pktccops show cmtses";
1151 "Usage: pktccops show cmtses\n"
1152 " List PacketCable COPS CMTSes.\n";
1159 ast_cli(a->fd, "%-16s %-24s %-12s %7s\n", "Name ", "Host ", "Status ", "KA timer ");
1160 ast_cli(a->fd, "%-16s %-24s %-12s %7s\n", "------------", "--------------------", "----------", "-----------");
1161 AST_LIST_LOCK(&cmts_list);
1162 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
1164 if (cmts->state == 2) {
1165 ast_copy_string(statedesc, "Connected", sizeof(statedesc));
1166 katimer = (int) (time(NULL) - cmts->katimer);
1167 } else if (cmts->state == 1) {
1168 ast_copy_string(statedesc, "Connecting", sizeof(statedesc));
1170 ast_copy_string(statedesc, "N/A", sizeof(statedesc));
1172 ast_cli(a->fd, "%-16s %-15s:%-8s %-12s %-7d\n", cmts->name, cmts->host, cmts->port, statedesc, katimer);
1174 AST_LIST_UNLOCK(&cmts_list);
1178 static char *pktccops_show_gates(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1180 struct cops_gate *gate;
1181 char state_desc[16];
1185 e->command = "pktccops show gates";
1187 "Usage: pktccops show gates\n"
1188 " List PacketCable COPS GATEs.\n";
1195 ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"CMTS", "Gate-Id","MTA", "Status", "AllocTime", "CheckTime");
1196 ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"--------------" ,"----------", "----------", "--------", "--------", "--------\n");
1197 AST_LIST_LOCK(&cmts_list);
1198 AST_LIST_LOCK(&gate_list);
1199 AST_LIST_TRAVERSE(&gate_list, gate, list) {
1200 if (gate->state == GATE_ALLOC_FAILED) {
1201 ast_copy_string(state_desc, "Failed", sizeof(state_desc));
1202 } else if (gate->state == GATE_ALLOC_PROGRESS) {
1203 ast_copy_string(state_desc, "In Progress", sizeof(state_desc));
1204 } else if (gate->state == GATE_ALLOCATED) {
1205 ast_copy_string(state_desc, "Allocated", sizeof(state_desc));
1206 } else if (gate->state == GATE_CLOSED) {
1207 ast_copy_string(state_desc, "Closed", sizeof(state_desc));
1208 } else if (gate->state == GATE_CLOSED_ERR) {
1209 ast_copy_string(state_desc, "ClosedErr", sizeof(state_desc));
1210 } else if (gate->state == GATE_OPEN) {
1211 ast_copy_string(state_desc, "Open", sizeof(state_desc));
1212 } else if (gate->state == GATE_DELETED) {
1213 ast_copy_string(state_desc, "Deleted", sizeof(state_desc));
1215 ast_copy_string(state_desc, "N/A", sizeof(state_desc));
1218 ast_cli(a->fd, "%-16s 0x%.8x 0x%08x %-10s %10i %10i %u\n", (gate->cmts) ? gate->cmts->name : "null" , gate->gateid, gate->mta,
1219 state_desc, (int) (time(NULL) - gate->allocated), (gate->checked) ? (int) (time(NULL) - gate->checked) : 0, (unsigned int) gate->in_transaction);
1221 AST_LIST_UNLOCK(&cmts_list);
1222 AST_LIST_UNLOCK(&gate_list);
1226 static char *pktccops_show_pools(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1228 struct cops_ippool *ippool;
1234 e->command = "pktccops show pools";
1236 "Usage: pktccops show pools\n"
1237 " List PacketCable COPS ip pools of MTAs.\n";
1244 ast_cli(a->fd, "%-16s %-18s %-7s\n", "Start ", "Stop ", "CMTS ");
1245 ast_cli(a->fd, "%-16s %-18s %-7s\n", "----------", "----------", "--------");
1246 AST_LIST_LOCK(&ippool_list);
1247 AST_LIST_TRAVERSE(&ippool_list, ippool, list) {
1248 snprintf(start, sizeof(start), "%3u.%3u.%3u.%3u", ippool->start >> 24, (ippool->start >> 16) & 0x000000ff, (ippool->start >> 8) & 0x000000ff, ippool->start & 0x000000ff);
1250 snprintf(stop, sizeof(stop), "%3u.%3u.%3u.%3u", ippool->stop >> 24, (ippool->stop >> 16) & 0x000000ff, (ippool->stop >> 8) & 0x000000ff, ippool->stop & 0x000000ff);
1251 ast_cli(a->fd, "%-16s %-18s %-16s\n", start, stop, ippool->cmts->name);
1253 AST_LIST_UNLOCK(&ippool_list);
1257 static char *pktccops_gatedel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1262 struct cops_gate *gate;
1263 struct cops_cmts *cmts;
1267 e->command = "pktccops gatedel";
1269 "Usage: pktccops gatedel <cmts> <gateid>\n"
1270 " Send Gate-Del to cmts.\n";
1277 return CLI_SHOWUSAGE;
1279 AST_LIST_LOCK(&cmts_list);
1280 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
1281 if (!strcmp(cmts->name, a->argv[2])) {
1282 ast_cli(a->fd, "Found cmts: %s\n", cmts->name);
1287 AST_LIST_UNLOCK(&cmts_list);
1290 return CLI_SHOWUSAGE;
1293 if (!sscanf(a->argv[3], "%x", &gateid)) {
1294 ast_cli(a->fd, "bad gate specification (%s)\n", a->argv[3]);
1295 return CLI_SHOWUSAGE;
1299 AST_LIST_LOCK(&gate_list);
1300 AST_LIST_TRAVERSE(&gate_list, gate, list) {
1301 if (gate->gateid == gateid && gate->cmts == cmts) {
1308 ast_cli(a->fd, "gate not found: %s\n", a->argv[3]);
1309 return CLI_SHOWUSAGE;
1312 AST_LIST_UNLOCK(&gate_list);
1313 cops_gate_cmd(GATE_DEL, cmts, trid, 0, 0, 0, 0, 0, 0, gate);
1317 static char *pktccops_gateset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1321 unsigned int an,bn,cn,dn;
1323 struct cops_cmts *cmts;
1324 struct cops_gate *gate;
1328 e->command = "pktccops gateset";
1330 "Usage: pktccops gateset <cmts> <mta> <acctcount> <bitrate> <packet size> <switch ip> <switch port>\n"
1331 " Send Gate-Set to cmts.\n";
1338 return CLI_SHOWUSAGE;
1340 if (!strncmp(a->argv[2], "null", sizeof(a->argv[2]))) {
1343 AST_LIST_LOCK(&cmts_list);
1344 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
1345 if (!strcmp(cmts->name, a->argv[2])) {
1346 ast_cli(a->fd, "Found cmts: %s\n", cmts->name);
1351 AST_LIST_UNLOCK(&cmts_list);
1353 ast_cli(a->fd, "CMTS not found: %s\n", a->argv[2]);
1354 return CLI_SHOWUSAGE;
1359 if (sscanf(a->argv[3], "%3u.%3u.%3u.%3u", &an, &bn, &cn, &dn) != 4) {
1360 ast_cli(a->fd, "MTA specification (%s) does not look like an ipaddr\n", a->argv[3]);
1361 return CLI_SHOWUSAGE;
1363 mta = an << 24 | bn << 16 | cn << 8 | dn;
1365 if (sscanf(a->argv[7], "%3u.%3u.%3u.%3u", &an, &bn, &cn, &dn) != 4) {
1366 ast_cli(a->fd, "SSIP specification (%s) does not look like an ipaddr\n", a->argv[7]);
1367 return CLI_SHOWUSAGE;
1369 ssip = an << 24 | bn << 16 | cn << 8 | dn;
1371 gate = 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);
1375 static char *pktccops_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1379 e->command = "pktccops set debug {on|off}";
1381 "Usage: pktccops set debug {on|off}\n"
1382 " Turn on/off debuging\n";
1388 if (a->argc != e->args)
1389 return CLI_SHOWUSAGE;
1390 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
1392 ast_cli(a->fd, "PktcCOPS Debugging Enabled\n");
1393 } else if (!strncasecmp(a->argv[e->args - 1], "off", 2)) {
1395 ast_cli(a->fd, "PktcCOPS Debugging Disabled\n");
1397 return CLI_SHOWUSAGE;
1403 static struct ast_cli_entry cli_pktccops[] = {
1404 AST_CLI_DEFINE(pktccops_show_cmtses, "List PacketCable COPS CMTSes"),
1405 AST_CLI_DEFINE(pktccops_show_gates, "List PacketCable COPS GATEs"),
1406 AST_CLI_DEFINE(pktccops_show_pools, "List PacketCable MTA pools"),
1407 AST_CLI_DEFINE(pktccops_gateset, "Send Gate-Set to cmts"),
1408 AST_CLI_DEFINE(pktccops_gatedel, "Send Gate-Det to cmts"),
1409 AST_CLI_DEFINE(pktccops_debug, "Enable/Disable COPS debugging")
1412 static int pktccops_add_ippool(struct cops_ippool *ippool)
1415 AST_LIST_LOCK(&ippool_list);
1416 AST_LIST_INSERT_HEAD(&ippool_list, ippool, list);
1417 AST_LIST_UNLOCK(&ippool_list);
1420 ast_log(LOG_WARNING, "Attempted to register NULL ippool?\n");
1425 static void pktccops_unregister_cmtses(void)
1427 struct cops_cmts *cmts;
1428 struct cops_gate *gate;
1429 AST_LIST_LOCK(&cmts_list);
1430 while ((cmts = AST_LIST_REMOVE_HEAD(&cmts_list, list))) {
1431 if (cmts->sfd > 0) {
1436 AST_LIST_UNLOCK(&cmts_list);
1438 AST_LIST_LOCK(&gate_list);
1439 while ((gate = AST_LIST_REMOVE_HEAD(&gate_list, list))) {
1442 AST_LIST_UNLOCK(&gate_list);
1445 static void pktccops_unregister_ippools(void)
1447 struct cops_ippool *ippool;
1448 AST_LIST_LOCK(&ippool_list);
1449 while ((ippool = AST_LIST_REMOVE_HEAD(&ippool_list, list))) {
1452 AST_LIST_UNLOCK(&ippool_list);
1455 static int load_module(void)
1458 AST_LIST_LOCK(&cmts_list);
1459 res = load_pktccops_config();
1460 AST_LIST_UNLOCK(&cmts_list);
1462 return AST_MODULE_LOAD_DECLINE;
1464 ast_cli_register_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry));
1465 restart_pktc_thread();
1469 static int unload_module(void)
1471 if (!ast_mutex_lock(&pktccops_lock)) {
1472 if ((pktccops_thread != AST_PTHREADT_NULL) && (pktccops_thread != AST_PTHREADT_STOP)) {
1473 pthread_cancel(pktccops_thread);
1474 pthread_kill(pktccops_thread, SIGURG);
1475 pthread_join(pktccops_thread, NULL);
1477 pktccops_thread = AST_PTHREADT_STOP;
1478 ast_mutex_unlock(&pktccops_lock);
1480 ast_log(LOG_ERROR, "Unable to lock the pktccops_thread\n");
1484 ast_cli_unregister_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry));
1485 pktccops_unregister_cmtses();
1486 pktccops_unregister_ippools();
1487 pktccops_thread = AST_PTHREADT_NULL;
1491 static int reload_module(void)
1493 /* Prohibit unloading */
1495 ast_log(LOG_NOTICE, "Previous reload in progress, please wait!\n");
1502 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "PktcCOPS manager for MGCP",
1503 .load = load_module,
1504 .unload = unload_module,
1505 .reload = reload_module,