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>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <sys/types.h>
31 #include <sys/socket.h>
40 #include <arpa/inet.h>
43 #include "asterisk/file.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/config.h"
47 #include "asterisk/options.h"
48 #include "asterisk/pbx.h"
49 #include "asterisk/module.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/lock.h"
52 #define AST_API_MODULE
53 #include "asterisk/pktccops.h"
55 #define DEFAULT_COPS_PORT "2126"
57 #define COPS_HEADER_SIZE 8
58 #define COPS_OBJECT_HEADER_SIZE 4
59 #define GATE_SET_OBJ_SIZE 144
60 #define GATEID_OBJ_SIZE 8
61 #define GATE_INFO_OBJ_SIZE 24
63 #define PKTCCOPS_SCOMMAND_GATE_ALLOC 1
64 #define PKTCCOPS_SCOMMAND_GATE_ALLOC_ACK 2
65 #define PKTCCOPS_SCOMMAND_GATE_ALLOC_ERR 3
66 #define PKTCCOPS_SCOMMAND_GATE_SET 4
67 #define PKTCCOPS_SCOMMAND_GATE_SET_ACK 5
68 #define PKTCCOPS_SCOMMAND_GATE_SET_ERR 6
69 #define PKTCCOPS_SCOMMAND_GATE_INFO 7
70 #define PKTCCOPS_SCOMMAND_GATE_INFO_ACK 8
71 #define PKTCCOPS_SCOMMAND_GATE_INFO_ERR 9
72 #define PKTCCOPS_SCOMMAND_GATE_DELETE 10
73 #define PKTCCOPS_SCOMMAND_GATE_DELETE_ACK 11
74 #define PKTCCOPS_SCOMMAND_GATE_DELETE_ERR 12
75 #define PKTCCOPS_SCOMMAND_GATE_OPEN 13
76 #define PKTCCOPS_SCOMMAND_GATE_CLOSE 14
78 AST_MUTEX_DEFINE_STATIC(pktccops_lock);
79 static pthread_t pktccops_thread = AST_PTHREADT_NULL;
80 static uint16_t cops_trid = 0;
91 unsigned char verflag;
95 struct pktcobj *object;
96 char *msg; /* != NULL if not packet cable message received */
100 int direction; /* 0-DS, 1-US */
102 int flags; /* 0x00 */
103 int sessionclass; /* normal voip: 0x01, high priority voip: 0x02, unspecified: 0x00 */
112 uint32_t r; /* Token Bucket Rate */
113 uint32_t b; /* token Bucket Size */
114 uint32_t p; /* Peak Data Rate */
115 uint32_t m; /* Minimum Policed Size*/
116 uint32_t mm; /* Maximum Policed Size */
118 uint32_t s; /* Allowable Jitter*/
123 AST_LIST_ENTRY(cops_cmts) list;
141 AST_LIST_ENTRY(cops_ippool) list;
144 struct cops_cmts *cmts;
147 static uint16_t t1 = 250;
148 static uint16_t t7 = 200;
149 static uint16_t t8 = 300;
150 static uint32_t keepalive = 60;
151 static int pktccopsdebug = 0;
152 static int pktcreload = 0;
153 static int gateinfoperiod = 60;
154 static int gatetimeout = 150;
156 AST_LIST_HEAD_STATIC(cmts_list, cops_cmts);
157 AST_LIST_HEAD_STATIC(ippool_list, cops_ippool);
158 AST_LIST_HEAD_STATIC(gate_list, cops_gate);
160 static int pktccops_add_ippool(struct cops_ippool *ippool);
161 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);
162 static void pktccops_unregister_ippools(void);
163 static int load_pktccops_config(void);
165 static uint32_t ftoieeef(float n)
172 static uint16_t cops_constructgatespec(struct gatespec *gs, char *res)
178 *res = (char) gs->direction;
179 *(res + 1) = (char) gs->protocolid;
180 *(res + 2) = (char) gs->flags;
181 *(res + 3) = (char) gs->sessionclass;
183 *((uint32_t *) (res + 4)) = gs->srcip;
184 *((uint32_t *) (res + 8)) = gs->dstip;
186 *((uint16_t *) (res + 12)) = gs->srcp;
187 *((uint16_t *) (res + 14)) = gs->dstp;
189 *(res + 16) = (char) gs->diffserv;
190 *(res + 17) = 0; /* reserved */
191 *(res + 18) = 0; /* reserved */
192 *(res + 19) = 0; /* reserved */
194 *((uint16_t *) (res + 20)) = gs->t1;
195 *(res + 22) = 0; /* reserved */
196 *(res + 23) = 0; /* reserved */
198 *((uint16_t *) (res + 24)) = gs->t7;
199 *((uint16_t *) (res + 26)) = gs->t8;
201 *((uint32_t *) (res + 28)) = gs->r;
202 *((uint32_t *) (res + 32)) = gs->b;
203 *((uint32_t *) (res + 36)) = gs->p;
204 *((uint32_t *) (res + 40)) = gs->m;
205 *((uint32_t *) (res + 44)) = gs->mm;
206 *((uint32_t *) (res + 48)) = gs->rate;
207 *((uint32_t *) (res + 52)) = gs->s;
208 return 56; /* length */
211 static uint16_t cops_construct_gate (int cmd, char *p, uint16_t trid,
212 uint32_t mtahost, uint32_t actcount, float rate, uint32_t psizegateid,
213 uint32_t ssip, uint16_t ssport, uint32_t gateid, struct cops_cmts *cmts)
218 ast_debug(3, "CMD: %d\n", cmd);
220 /* Transaction Identifier 8 octets */
222 *(p + offset++) = 8; /* length */
223 *(p + offset++) = 1; /* snum */
224 *(p + offset++) = 1; /* stype */
225 *((uint16_t *) (p + offset)) = htons(trid);
228 *(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 */
230 /*Subscriper Identifier 8 octets */
232 *(p + offset++) = 8; /* length */
233 *(p + offset++) = 2; /* snum */
234 *(p + offset++) = 1; /* stype */
235 *((uint32_t *) (p + offset)) = htonl(mtahost);
238 if (cmd == GATE_INFO || cmd == GATE_SET_HAVE_GATEID || cmd == GATE_DEL) {
239 /* Gate ID 8 Octets */
241 *(p + offset++) = 8; /* length */
242 *(p + offset++) = 3; /* snum */
243 *(p + offset++) = 1; /* stype */
244 *((uint32_t *) (p + offset)) = htonl(gateid);
246 if (cmd == GATE_INFO || cmd == GATE_DEL) {
252 /* Activity Count 8 octets */
254 *(p + offset++) = 8; /* length */
255 *(p + offset++) = 4; /* snum */
256 *(p + offset++) = 1; /* stype */
257 *((uint32_t *) (p + offset)) = htonl(actcount);
261 /* Gate Spec 2*60 Octets */
262 gs.direction = 0; /* DS */
263 gs.protocolid = 17; /* UDP */
266 gs.srcip = htonl(ssip);
267 gs.dstip = htonl(mtahost);
268 gs.srcp = htons(ssport);
270 /* gs.diffserv = 0xa0;*/
272 gs.t1 = htons(cmts->t1);
273 gs.t7 = htons(cmts->t7);
274 gs.t8 = htons(cmts->t8);
275 gs.r = ftoieeef(rate);
276 gs.b = ftoieeef(psizegateid);
277 gs.p = ftoieeef(rate);
278 gs.m = htonl((uint32_t) psizegateid);
279 gs.mm = htonl((uint32_t) psizegateid);
280 gs.rate = ftoieeef(rate);
286 *(p + offset) = 60; /* length */
288 *(p + offset) = 5; /* snum */
290 *(p + offset) = 1; /* stype */
292 offset += cops_constructgatespec(&gs, p + offset);
295 gs.direction = 1; /* US */
296 gs.srcip = htonl(mtahost);
297 gs.dstip = htonl(ssip);
299 gs.dstp = htons(ssport);
302 *(p + offset) = 60; /* length */
304 *(p + offset) = 5; /* snum */
306 *(p + offset) = 1; /* stype */
308 offset += cops_constructgatespec(&gs, p + offset);
313 static int cops_getmsg (int sfd, struct copsmsg *recmsg)
316 char buf[COPS_HEADER_SIZE];
317 struct pktcobj *pobject = NULL;
318 uint16_t *ubuf = (uint16_t *) buf;
320 recmsg->object = NULL;
321 len = recv(sfd, buf, COPS_HEADER_SIZE, MSG_DONTWAIT);
322 if (len < COPS_HEADER_SIZE) {
325 recmsg->verflag = *buf;
326 recmsg->opcode = *(buf + 1);
327 recmsg->clienttype = ntohs(*((uint16_t *) (buf + 2)));
328 recmsg->length = ntohl(*((uint32_t *) (buf + 4)));
330 if (recmsg->clienttype != 0x8008 ) {
331 if (!(recmsg->msg = malloc(recmsg->length - COPS_HEADER_SIZE))) {
334 lent = recv(sfd, recmsg->msg, recmsg->length - COPS_HEADER_SIZE, MSG_DONTWAIT);
335 if (lent < recmsg->length - COPS_HEADER_SIZE) {
340 /* PacketCable Objects */
341 while (len < recmsg->length) {
342 if (len == COPS_HEADER_SIZE) {
344 if (!(recmsg->object = malloc(sizeof(struct pktcobj)))) {
347 pobject = recmsg->object;
349 if (!(pobject->next = malloc(sizeof(struct pktcobj)))) {
352 pobject = pobject->next;
354 pobject->next = NULL;
355 lent = recv(sfd, buf, COPS_OBJECT_HEADER_SIZE, MSG_DONTWAIT);
356 if (lent < COPS_OBJECT_HEADER_SIZE) {
357 ast_debug(3, "Too short object header len: %i\n", lent);
361 pobject->length = ntohs(*ubuf);
362 pobject->cnum = *(buf + 2);
363 pobject->ctype = *(buf + 3);
364 if (!(pobject->contents = malloc(pobject->length - COPS_OBJECT_HEADER_SIZE))) {
367 lent = recv(sfd, pobject->contents, pobject->length - COPS_OBJECT_HEADER_SIZE, MSG_DONTWAIT);
368 if (lent < pobject->length - COPS_OBJECT_HEADER_SIZE) {
369 ast_debug(3, "Too short object content len: %i\n", lent);
378 static int cops_sendmsg (int sfd, struct copsmsg * sendmsg)
382 struct pktcobj *pobject;
388 ast_debug(3, "COPS: sending opcode: %i len: %i\n", sendmsg->opcode, sendmsg->length);
389 if (sendmsg->length < COPS_HEADER_SIZE) {
390 ast_log(LOG_WARNING, "COPS: invalid msg size!!!\n");
393 if (!(buf = malloc((size_t) sendmsg->length))) {
396 *buf = sendmsg->verflag ;
397 *(buf + 1) = sendmsg->opcode;
398 *((uint16_t *)(buf + 2)) = htons(sendmsg->clienttype);
399 *((uint32_t *)(buf + 4)) = htonl(sendmsg->length);
401 if (sendmsg->msg != NULL) {
402 memcpy(buf + COPS_HEADER_SIZE, sendmsg->msg, sendmsg->length - COPS_HEADER_SIZE);
403 } else if (sendmsg->object != NULL) {
405 pobject = sendmsg->object;
406 while(pobject != NULL) {
407 ast_debug(3, "COPS: Sending Object : cnum: %i ctype %i len: %i\n", pobject->cnum, pobject->ctype, pobject->length);
408 if (sendmsg->length < bufpos + pobject->length) {
409 ast_log(LOG_WARNING, "COPS: Invalid msg size len: %i objectlen: %i\n", sendmsg->length, pobject->length);
413 *(uint16_t *) (buf + bufpos) = htons(pobject->length);
414 *(buf + bufpos + 2) = pobject->cnum;
415 *(buf + bufpos + 3) = pobject->ctype;
416 if (sendmsg->length < pobject->length + bufpos) {
417 ast_log(LOG_WARNING, "COPS: Error sum of object len more the msg len %i < %i\n", sendmsg->length, pobject->length + bufpos);
421 memcpy((buf + bufpos + 4), pobject->contents, pobject->length - 4);
422 bufpos += pobject->length;
423 pobject = pobject->next;
428 #ifdef HAVE_MSG_NOSIGNAL
429 #define SENDFLAGS MSG_NOSIGNAL | MSG_DONTWAIT
431 #define SENDFLAGS MSG_DONTWAIT
433 if (send(sfd, buf, sendmsg->length, SENDFLAGS) == -1) {
434 ast_log(LOG_WARNING, "COPS: Send failed errno=%i\n", errno);
443 static void cops_freemsg(struct copsmsg *p)
445 struct pktcobj *pnext;
448 while (p->object != NULL) {
449 pnext = p->object->next;
450 ast_free(p->object->contents);
451 p->object->contents = NULL;
458 struct cops_gate * AST_OPTIONAL_API_NAME(ast_pktccops_gate_alloc)(int cmd,
459 struct cops_gate *gate, uint32_t mta, uint32_t actcount, float bitrate,
460 uint32_t psize, uint32_t ssip, uint16_t ssport,
461 int (* const got_dq_gi) (struct cops_gate *gate),
462 int (* const gate_remove) (struct cops_gate *gate))
468 if (cmd == GATE_SET_HAVE_GATEID && gate) {
469 ast_debug(3, "------- gate modify gateid 0x%x ssip: 0x%x\n", gate->gateid, ssip);
470 /* TODO implement it */
471 ast_log(LOG_WARNING, "Modify GateID not implemented\n");
474 if ((gate = cops_gate_cmd(cmd, NULL, cops_trid++, mta, actcount, bitrate, psize, ssip, ssport, gate))) {
475 ast_debug(3, "COPS: Allocating gate for mta: 0x%x\n", mta);
476 gate->got_dq_gi = got_dq_gi;
477 gate->gate_remove = gate_remove;
480 ast_debug(3, "COPS: Couldn't allocate gate for mta: 0x%x\n", mta);
485 static struct cops_gate *cops_gate_cmd(int cmd, struct cops_cmts *cmts,
486 uint16_t trid, uint32_t mta, uint32_t actcount, float bitrate,
487 uint32_t psize, uint32_t ssip, uint16_t ssport, struct cops_gate *gate)
489 struct copsmsg *gateset;
490 struct cops_gate *new;
491 struct cops_ippool *ippool;
493 if (cmd == GATE_DEL) {
502 AST_LIST_LOCK(&ippool_list);
503 AST_LIST_TRAVERSE(&ippool_list, ippool, list) {
504 if (mta >= ippool->start && mta <= ippool->stop) {
509 AST_LIST_UNLOCK(&ippool_list);
511 ast_log(LOG_WARNING, "COPS: couldn't find cmts for mta: 0x%x\n", mta);
515 ast_log(LOG_WARNING, "CMTS: %s not connected\n", cmts->name);
520 if (cmd == GATE_SET) {
521 new = ast_calloc(1, sizeof(*new));
525 new->state = GATE_ALLOC_PROGRESS;
526 new->checked = time(NULL);
527 new->allocated = time(NULL);
529 new->got_dq_gi = NULL;
530 new->gate_remove = NULL;
531 new->gate_open = NULL;
532 new->tech_pvt = NULL;
534 AST_LIST_LOCK(&gate_list);
535 AST_LIST_INSERT_HEAD(&gate_list, new, list);
536 AST_LIST_UNLOCK(&gate_list);
544 gate->in_transaction = time(NULL);
546 if (!(gateset = malloc(sizeof(struct copsmsg)))) {
551 gateset->verflag = 0x10;
552 gateset->opcode = 2; /* Decision */
553 gateset->clienttype = 0x8008; /* =PacketCable */
556 gateset->object = malloc(sizeof(struct pktcobj));
557 if (!gateset->object) {
558 cops_freemsg(gateset);
562 gateset->object->length = COPS_OBJECT_HEADER_SIZE + 4;
563 gateset->object->cnum = 1; /* Handle */
564 gateset->object->ctype = 1; /* client */
565 if (!(gateset->object->contents = malloc(sizeof(uint32_t)))) {
566 cops_freemsg(gateset);
570 *((uint32_t *) gateset->object->contents) = htonl(cmts->handle);
573 if (!(gateset->object->next = malloc(sizeof(struct pktcobj)))) {
574 cops_freemsg(gateset);
578 gateset->object->next->length = COPS_OBJECT_HEADER_SIZE + 4;
579 gateset->object->next->cnum = 2; /* Context */
580 gateset->object->next->ctype = 1; /* Context */
581 if (!(gateset->object->next->contents = malloc(sizeof(uint32_t)))) {
582 cops_freemsg(gateset);
586 *((uint32_t *) gateset->object->next->contents) = htonl(0x00080000); /* R-Type = 8 configuration request, M-Type = 0 */
588 /* Decision Object: Flags */
589 if (!(gateset->object->next->next = malloc(sizeof(struct pktcobj)))) {
590 cops_freemsg(gateset);
594 gateset->object->next->next->length = COPS_OBJECT_HEADER_SIZE + 4;
595 gateset->object->next->next->cnum = 6; /* Decision */
596 gateset->object->next->next->ctype = 1; /* Flags */
597 if (!(gateset->object->next->next->contents = malloc(sizeof(uint32_t)))) {
598 cops_freemsg(gateset);
602 *((uint32_t *) gateset->object->next->next->contents) = htonl(0x00010001); /* Install, Trigger Error */
604 /* Decision Object: Data */
605 if (!(gateset->object->next->next->next = malloc(sizeof(struct pktcobj)))) {
606 cops_freemsg(gateset);
610 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);
611 gateset->object->next->next->next->cnum = 6; /* Decision */
612 gateset->object->next->next->next->ctype = 4; /* Decision Data */
613 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));
614 if (!gateset->object->next->next->next->contents) {
615 cops_freemsg(gateset);
619 gateset->object->next->next->next->next = NULL;
621 gateset->length = COPS_HEADER_SIZE + gateset->object->length + gateset->object->next->length + gateset->object->next->next->length + gateset->object->next->next->next->length;
623 if ((cmd == GATE_INFO || cmd == GATE_SET_HAVE_GATEID || cmd == GATE_DEL) && gate) {
624 ast_debug(1, "Construct gate with gateid: 0x%x\n", gate->gateid);
625 cops_construct_gate(cmd, gateset->object->next->next->next->contents, trid, mta, actcount, bitrate, psize, ssip, ssport, gate->gateid, cmts);
627 ast_debug(1, "Construct new gate\n");
628 cops_construct_gate(cmd, gateset->object->next->next->next->contents, trid, mta, actcount, bitrate, psize, ssip, ssport, 0, cmts);
631 ast_debug(3, "send cmd\n");
633 cops_sendmsg(cmts->sfd, gateset);
634 cops_freemsg(gateset);
639 static int cops_connect(char *host, char *port)
641 int s, sfd = -1, flags;
642 struct addrinfo hints;
644 struct addrinfo *result;
645 #ifdef HAVE_SO_NOSIGPIPE
649 memset(&hints, 0, sizeof(struct addrinfo));
651 hints.ai_family = AF_UNSPEC;
652 hints.ai_socktype = SOCK_STREAM;
654 hints.ai_protocol = 0;
656 s = getaddrinfo(host, port, &hints, &result);
658 ast_log(LOG_WARNING, "COPS: getaddrinfo: %s\n", gai_strerror(s));
662 for (rp = result; rp != NULL; rp = rp->ai_next) {
663 sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
665 ast_log(LOG_WARNING, "Failed socket\n");
667 flags = fcntl(sfd, F_GETFL);
668 fcntl(sfd, F_SETFL, flags | O_NONBLOCK);
669 #ifdef HAVE_SO_NOSIGPIPE
670 setsockopt(sfd, SO_SOCKET, SO_NOSIGPIPE, &trueval, sizeof(trueval));
672 connect(sfd, rp->ai_addr, rp->ai_addrlen);
674 ast_log(LOG_WARNING, "Failed connect\n");
677 freeaddrinfo(result);
679 ast_debug(3, "Connecting to cmts: %s:%s\n", host, port);
683 #define PKTCCOPS_DESTROY_CURRENT_GATE \
684 AST_LIST_REMOVE_CURRENT(list); \
685 if (gate->gate_remove) { \
686 gate->gate_remove(gate); \
690 static void *do_pktccops(void *data)
693 struct copsmsg *recmsg, *sendmsg;
694 struct copsmsg recmsgb, sendmsgb;
697 struct pktcobj *pobject;
698 struct cops_cmts *cmts;
699 struct cops_gate *gate;
701 uint16_t snst, sobjlen, scommand, recvtrid, actcount, reason, subreason;
702 uint32_t gateid, subscrid, pktcerror;
703 time_t last_exec = 0;
708 ast_debug(3, "COPS: thread started\n");
715 AST_LIST_LOCK(&cmts_list);
716 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
717 if (last_exec != time(NULL)) {
718 if (cmts->state == 2 && cmts->katimer + cmts->keepalive < time(NULL)) {
719 ast_log(LOG_WARNING, "KA timer (%is) expired cmts: %s\n", cmts->keepalive, cmts->name);
727 FD_SET(cmts->sfd, &rfds);
728 if (cmts->sfd > nfds) nfds = cmts->sfd;
730 cmts->sfd = cops_connect(cmts->host, cmts->port);
734 FD_SET(cmts->sfd, &rfds);
735 if (cmts->sfd > nfds) nfds = cmts->sfd;
740 AST_LIST_UNLOCK(&cmts_list);
742 if (last_exec != time(NULL)) {
743 last_exec = time(NULL);
744 AST_LIST_LOCK(&gate_list);
745 AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
747 if (gate->deltimer && gate->deltimer < time(NULL)) {
748 gate->deltimer = time(NULL) + 5;
749 gate->trid = cops_trid++;
750 cops_gate_cmd(GATE_DEL, gate->cmts, gate->trid, 0, 0, 0, 0, 0, 0, gate);
751 ast_debug(3, "COPS: requested Gate-Del: CMTS: %s gateid: 0x%x\n", (gate->cmts) ? gate->cmts->name : "null", gate->gateid);
753 if (time(NULL) - gate->checked > gatetimeout) {
754 ast_debug(3, "COPS: remove from list GATE, CMTS: %s gateid: 0x%x\n", (gate->cmts) ? gate->cmts->name : "null", gate->gateid);
755 gate->state = GATE_TIMEOUT;
756 PKTCCOPS_DESTROY_CURRENT_GATE;
757 } else if (time(NULL) - gate->checked > gateinfoperiod && (gate->state == GATE_ALLOCATED || gate->state == GATE_OPEN)) {
758 if (gate->cmts && (!gate->in_transaction || ( gate->in_transaction + 5 ) < time(NULL))) {
759 gate->trid = cops_trid++;
760 ast_debug(3, "COPS: Gate-Info send to CMTS: %s gateid: 0x%x\n", gate->cmts->name, gate->gateid);
761 cops_gate_cmd(GATE_INFO, gate->cmts, gate->trid, gate->mta, 0, 0, 0, 0, 0, gate);
766 AST_LIST_TRAVERSE_SAFE_END;
767 AST_LIST_UNLOCK(&gate_list);
770 if (pktcreload == 2) {
773 if ((res = select(nfds + 1, &rfds, NULL, NULL, &tv))) {
774 AST_LIST_LOCK(&cmts_list);
775 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
776 if (FD_ISSET(cmts->sfd, &rfds)) {
777 len = cops_getmsg(cmts->sfd, recmsg);
779 ast_debug(3, "COPS: got from %s:\n Header: versflag=0x%.2x opcode=%i clienttype=0x%.4x msglength=%i\n",
780 cmts->name, recmsg->verflag, recmsg->opcode, recmsg->clienttype, recmsg->length);
781 if (recmsg->object != NULL) {
782 pobject = recmsg->object;
783 while (pobject != NULL) {
784 ast_debug(3, " OBJECT: length=%i cnum=%i ctype=%i\n", pobject->length, pobject->cnum, pobject->ctype);
785 if (recmsg->opcode == 1 && pobject->cnum == 1 && pobject->ctype == 1 ) {
786 cmts->handle = ntohl(*((uint32_t *) pobject->contents));
787 ast_debug(3, " REQ client handle: %i\n", cmts->handle);
789 cmts->katimer = time(NULL);
790 } else if (pobject->cnum == 9 && pobject->ctype == 1) {
791 sobjp = pobject->contents;
800 while (sobjp < (pobject->contents + pobject->length - 4)) {
801 sobjlen = ntohs(*((uint16_t *) sobjp));
802 snst = ntohs(*((uint16_t *) (sobjp + 2)));
803 ast_debug(3, " S-Num S-type: 0x%.4x len: %i\n", snst, sobjlen);
804 if (snst == 0x0101 ) {
805 recvtrid = ntohs(*((uint16_t *) (sobjp + 4)));
806 scommand = ntohs(*((uint16_t *) (sobjp + 6)));
807 ast_debug(3, " Transaction Identifier command: %i trid %i\n", scommand, recvtrid);
808 } else if (snst == 0x0201) {
809 subscrid = ntohl(*((uint32_t *) (sobjp + 4)));
810 ast_debug(3, " Subscriber ID: 0x%.8x\n", subscrid);
811 } else if (snst == 0x0301) {
812 gateid = ntohl(*((uint32_t *) (sobjp + 4)));
813 ast_debug(3, " Gate ID: 0x%x 0x%.8x\n", gateid, gateid);
814 } else if (snst == 0x0401) {
815 actcount = ntohs(*((uint16_t *) (sobjp + 6)));
816 ast_debug(3, " Activity Count: %i\n", actcount);
817 } else if (snst == 0x0901) {
818 pktcerror = ntohl(*((uint32_t *) (sobjp + 4)));
819 ast_debug(3, " PKTC Error: 0x%.8x\n", pktcerror);
820 } else if (snst == 0x0d01) {
821 reason = ntohs(*((uint16_t *) (sobjp + 4)));
822 subreason = ntohs(*((uint16_t *) (sobjp + 6)));
823 ast_debug(3, " Reason: %u Subreason: %u\n", reason, subreason);
829 if (scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE || scommand == PKTCCOPS_SCOMMAND_GATE_OPEN) {
830 AST_LIST_LOCK(&gate_list);
831 AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
832 if (gate->cmts == cmts && gate->gateid == gateid) {
833 if (scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE && gate->state != GATE_CLOSED && gate->state != GATE_CLOSED_ERR ) {
834 ast_debug(3, "COPS Gate Close Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
836 gate->state = GATE_CLOSED_ERR;
837 PKTCCOPS_DESTROY_CURRENT_GATE;
839 gate->state = GATE_CLOSED;
840 PKTCCOPS_DESTROY_CURRENT_GATE;
843 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_OPEN && gate->state == GATE_ALLOCATED) {
844 ast_debug(3, "COPS Gate Open Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
845 gate->state = GATE_OPEN;
846 if (gate->gate_open) {
847 ast_debug(3, "Calling GATE-OPEN callback function\n");
848 gate->gate_open(gate);
849 gate->gate_open = NULL;
855 AST_LIST_TRAVERSE_SAFE_END;
856 AST_LIST_UNLOCK(&gate_list);
857 } 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) {
858 AST_LIST_LOCK(&gate_list);
859 AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) {
860 if (gate->cmts == cmts && gate->trid == recvtrid) {
861 gate->gateid = gateid;
862 gate->checked = time(NULL);
863 if (scommand == PKTCCOPS_SCOMMAND_GATE_SET_ACK) {
864 ast_debug(3, "COPS Gate Set Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
865 gate->state = GATE_ALLOCATED;
866 if (gate->got_dq_gi) {
867 gate->got_dq_gi(gate);
868 gate->got_dq_gi = NULL;
870 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_SET_ERR) {
871 ast_debug(3, "COPS Gate Set Error TrId: %i ErrorCode: 0x%.8x CMTS: %s\n ", recvtrid, pktcerror, cmts->name);
872 gate->state = GATE_ALLOC_FAILED;
873 if (gate->got_dq_gi) {
874 gate->got_dq_gi(gate);
875 gate->got_dq_gi = NULL;
877 PKTCCOPS_DESTROY_CURRENT_GATE;
878 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ACK) {
879 ast_debug(3, "COPS Gate Info Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
880 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ERR) {
881 ast_debug(3, "COPS Gate Info Error Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
882 gate->state = GATE_ALLOC_FAILED;
883 PKTCCOPS_DESTROY_CURRENT_GATE;
884 } else if (scommand == PKTCCOPS_SCOMMAND_GATE_DELETE_ACK) {
885 ast_debug(3, "COPS Gate Deleted Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name);
886 gate->state = GATE_DELETED;
887 PKTCCOPS_DESTROY_CURRENT_GATE;
889 gate->in_transaction = 0;
893 AST_LIST_TRAVERSE_SAFE_END;
894 AST_LIST_UNLOCK(&gate_list);
897 pobject = pobject->next;
901 if (recmsg->opcode == 6 && recmsg->object && recmsg->object->cnum == 11 && recmsg->object->ctype == 1) {
902 ast_debug(3, "COPS: Client open %s\n", cmts->name);
904 sendmsg->verflag = 0x10;
905 sendmsg->opcode = 7; /* Client Accept */
906 sendmsg->clienttype = 0x8008; /* =PacketCable */
907 sendmsg->length = COPS_HEADER_SIZE + COPS_OBJECT_HEADER_SIZE + 4;
908 sendmsg->object = malloc(sizeof(struct pktcobj));
909 sendmsg->object->length = 4 + COPS_OBJECT_HEADER_SIZE;
910 sendmsg->object->cnum = 10; /* keppalive timer*/
911 sendmsg->object->ctype = 1;
912 sendmsg->object->contents = malloc(sizeof(uint32_t));
913 *((uint32_t *) sendmsg->object->contents) = htonl(cmts->keepalive & 0x0000ffff);
914 sendmsg->object->next = NULL;
915 cops_sendmsg(cmts->sfd, sendmsg);
916 cops_freemsg(sendmsg);
917 } else if (recmsg->opcode == 9) {
918 ast_debug(3, "COPS: Keepalive Request got echoing back %s\n", cmts->name);
919 cops_sendmsg(cmts->sfd, recmsg);
921 cmts->katimer = time(NULL);
925 ast_debug(3, "COPS: lost connection to %s\n", cmts->name);
930 cops_freemsg(recmsg);
933 AST_LIST_UNLOCK(&cmts_list);
936 ast_debug(3, "Reloading pktccops...\n");
937 AST_LIST_LOCK(&gate_list);
938 AST_LIST_LOCK(&cmts_list);
939 pktccops_unregister_ippools();
940 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
941 cmts->need_delete = 1;
943 load_pktccops_config();
944 AST_LIST_TRAVERSE_SAFE_BEGIN(&cmts_list, cmts, list) {
945 if (cmts && cmts->need_delete) {
946 AST_LIST_TRAVERSE(&gate_list, gate, list) {
947 if (gate->cmts == cmts) {
948 ast_debug(3, "Null gate %s\n", gate->cmts->name);
951 gate->in_transaction = 0;
953 AST_LIST_UNLOCK(&gate_list);
954 ast_debug(3, "removing cmts: %s\n", cmts->name);
958 AST_LIST_REMOVE_CURRENT(list);
962 AST_LIST_TRAVERSE_SAFE_END;
963 AST_LIST_UNLOCK(&cmts_list);
964 AST_LIST_UNLOCK(&gate_list);
967 pthread_testcancel();
972 static int restart_pktc_thread(void)
974 if (pktccops_thread == AST_PTHREADT_STOP) {
977 if (ast_mutex_lock(&pktccops_lock)) {
978 ast_log(LOG_WARNING, "Unable to lock pktccops\n");
981 if (pktccops_thread == pthread_self()) {
982 ast_mutex_unlock(&pktccops_lock);
983 ast_log(LOG_WARNING, "Cannot kill myself\n");
986 if (pktccops_thread != AST_PTHREADT_NULL) {
987 /* Wake up the thread */
988 pthread_kill(pktccops_thread, SIGURG);
990 /* Start a new monitor */
991 if (ast_pthread_create_background(&pktccops_thread, NULL, do_pktccops, NULL) < 0) {
992 ast_mutex_unlock(&pktccops_lock);
993 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
997 ast_mutex_unlock(&pktccops_lock);
1001 static int load_pktccops_config(void)
1003 static char *cfg = "res_pktccops.conf";
1004 struct ast_config *config;
1005 struct ast_variable *v;
1006 struct cops_cmts *cmts;
1007 struct cops_ippool *new_ippool;
1008 const char *host, *cat, *port;
1011 uint16_t t1_temp, t7_temp, t8_temp;
1012 uint32_t keepalive_temp;
1013 unsigned int a,b,c,d,e,f,g,h;
1014 struct ast_flags config_flags = {0};
1016 if (!(config = ast_config_load(cfg, config_flags))) {
1017 ast_log(LOG_WARNING, "Unable to load config file res_pktccops.conf\n");
1020 for (cat = ast_category_browse(config, NULL); cat; cat = ast_category_browse(config, cat)) {
1021 if (!strcmp(cat, "general")) {
1022 for (v = ast_variable_browse(config, cat); v; v = v->next) {
1023 if (!strcasecmp(v->name, "t1")) {
1024 t1 = atoi(v->value);
1025 } else if (!strcasecmp(v->name, "t7")) {
1026 t7 = atoi(v->value);
1027 } else if (!strcasecmp(v->name, "t8")) {
1028 t8 = atoi(v->value);
1029 } else if (!strcasecmp(v->name, "keepalive")) {
1030 keepalive = atoi(v->value);
1031 } else if (!strcasecmp(v->name, "gateinfoperiod")) {
1032 gateinfoperiod = atoi(v->value);
1033 } else if (!strcasecmp(v->name, "gatetimeout")) {
1034 gatetimeout = atoi(v->value);
1036 ast_log(LOG_WARNING, "Unkown option %s in general section of res_ptkccops.conf\n", v->name);
1047 keepalive_temp = keepalive;
1049 for (v = ast_variable_browse(config, cat); v; v = v->next) {
1050 if (!strcasecmp(v->name, "host")) {
1052 } else if (!strcasecmp(v->name, "port")) {
1054 } else if (!strcasecmp(v->name, "t1")) {
1055 t1_temp = atoi(v->value);
1056 } else if (!strcasecmp(v->name, "t7")) {
1057 t7_temp = atoi(v->value);
1058 } else if (!strcasecmp(v->name, "t8")) {
1059 t8_temp = atoi(v->value);
1060 } else if (!strcasecmp(v->name, "keepalive")) {
1061 keepalive_temp = atoi(v->value);
1062 } else if (!strcasecmp(v->name, "pool")) {
1063 /* we weill parse it in 2nd round */
1065 ast_log(LOG_WARNING, "Unkown option %s in res_ptkccops.conf\n", v->name);
1070 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
1071 if (!strcmp(cmts->name, cat)) {
1078 cmts = ast_calloc(1, sizeof(*cmts));
1083 AST_LIST_INSERT_HEAD(&cmts_list, cmts, list);
1086 ast_copy_string(cmts->name, cat, sizeof(cmts->name));
1089 ast_copy_string(cmts->host, host, sizeof(cmts->host));
1092 ast_copy_string(cmts->port, port, sizeof(cmts->port));
1094 ast_copy_string(cmts->port, DEFAULT_COPS_PORT, sizeof(cmts->port));
1100 cmts->keepalive = keepalive_temp;
1105 cmts->need_delete = 0;
1106 for (v = ast_variable_browse(config, cat); v; v = v->next) {
1107 /* parse ipppol when we have cmts ptr */
1108 if (!strcasecmp(v->name, "pool")) {
1109 if (sscanf(v->value, "%3u.%3u.%3u.%3u %3u.%3u.%3u.%3u", &a, &b, &c, &d, &e, &f, &g, &h) == 8) {
1110 new_ippool = ast_calloc(1, sizeof(*new_ippool));
1115 new_ippool->start = a << 24 | b << 16 | c << 8 | d;
1116 new_ippool->stop = e << 24 | f << 16 | g << 8 | h;
1117 new_ippool->cmts = cmts;
1118 pktccops_add_ippool(new_ippool);
1120 ast_log(LOG_WARNING, "Invalid ip pool format in res_pktccops.conf\n");
1126 ast_config_destroy(config);
1130 static char *pktccops_show_cmtses(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1132 struct cops_cmts *cmts;
1138 e->command = "pktccops show cmtses";
1140 "Usage: pktccops show cmtses\n"
1141 " List PacketCable COPS CMTSes.\n";
1148 ast_cli(a->fd, "%-16s %-24s %-12s %7s\n", "Name ", "Host ", "Status ", "KA timer ");
1149 ast_cli(a->fd, "%-16s %-24s %-12s %7s\n", "------------", "--------------------", "----------", "-----------");
1150 AST_LIST_LOCK(&cmts_list);
1151 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
1153 if (cmts->state == 2) {
1154 ast_copy_string(statedesc, "Connected", sizeof(statedesc));
1155 katimer = (int) (time(NULL) - cmts->katimer);
1156 } else if (cmts->state == 1) {
1157 ast_copy_string(statedesc, "Connecting", sizeof(statedesc));
1159 ast_copy_string(statedesc, "N/A", sizeof(statedesc));
1161 ast_cli(a->fd, "%-16s %-15s:%-8s %-12s %-7d\n", cmts->name, cmts->host, cmts->port, statedesc, katimer);
1163 AST_LIST_UNLOCK(&cmts_list);
1167 static char *pktccops_show_gates(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1169 struct cops_gate *gate;
1170 char state_desc[16];
1174 e->command = "pktccops show gates";
1176 "Usage: pktccops show gates\n"
1177 " List PacketCable COPS GATEs.\n";
1184 ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"CMTS", "Gate-Id","MTA", "Status", "AllocTime", "CheckTime");
1185 ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"--------------" ,"----------", "----------", "--------", "--------", "--------\n");
1186 AST_LIST_LOCK(&cmts_list);
1187 AST_LIST_LOCK(&gate_list);
1188 AST_LIST_TRAVERSE(&gate_list, gate, list) {
1189 if (gate->state == GATE_ALLOC_FAILED) {
1190 ast_copy_string(state_desc, "Failed", sizeof(state_desc));
1191 } else if (gate->state == GATE_ALLOC_PROGRESS) {
1192 ast_copy_string(state_desc, "In Progress", sizeof(state_desc));
1193 } else if (gate->state == GATE_ALLOCATED) {
1194 ast_copy_string(state_desc, "Allocated", sizeof(state_desc));
1195 } else if (gate->state == GATE_CLOSED) {
1196 ast_copy_string(state_desc, "Closed", sizeof(state_desc));
1197 } else if (gate->state == GATE_CLOSED_ERR) {
1198 ast_copy_string(state_desc, "ClosedErr", sizeof(state_desc));
1199 } else if (gate->state == GATE_OPEN) {
1200 ast_copy_string(state_desc, "Open", sizeof(state_desc));
1201 } else if (gate->state == GATE_DELETED) {
1202 ast_copy_string(state_desc, "Deleted", sizeof(state_desc));
1204 ast_copy_string(state_desc, "N/A", sizeof(state_desc));
1207 ast_cli(a->fd, "%-16s 0x%.8x 0x%08x %-10s %10i %10i %u\n", (gate->cmts) ? gate->cmts->name : "null" , gate->gateid, gate->mta,
1208 state_desc, (int) (time(NULL) - gate->allocated), (gate->checked) ? (int) (time(NULL) - gate->checked) : 0, (unsigned int) gate->in_transaction);
1210 AST_LIST_UNLOCK(&cmts_list);
1211 AST_LIST_UNLOCK(&gate_list);
1215 static char *pktccops_show_pools(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1217 struct cops_ippool *ippool;
1223 e->command = "pktccops show pools";
1225 "Usage: pktccops show pools\n"
1226 " List PacketCable COPS ip pools of MTAs.\n";
1233 ast_cli(a->fd, "%-16s %-18s %-7s\n", "Start ", "Stop ", "CMTS ");
1234 ast_cli(a->fd, "%-16s %-18s %-7s\n", "----------", "----------", "--------");
1235 AST_LIST_LOCK(&ippool_list);
1236 AST_LIST_TRAVERSE(&ippool_list, ippool, list) {
1237 snprintf(start, sizeof(start), "%3u.%3u.%3u.%3u", ippool->start >> 24, (ippool->start >> 16) & 0x000000ff, (ippool->start >> 8) & 0x000000ff, ippool->start & 0x000000ff);
1239 snprintf(stop, sizeof(stop), "%3u.%3u.%3u.%3u", ippool->stop >> 24, (ippool->stop >> 16) & 0x000000ff, (ippool->stop >> 8) & 0x000000ff, ippool->stop & 0x000000ff);
1240 ast_cli(a->fd, "%-16s %-18s %-16s\n", start, stop, ippool->cmts->name);
1242 AST_LIST_UNLOCK(&ippool_list);
1246 static char *pktccops_gatedel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1251 struct cops_gate *gate;
1252 struct cops_cmts *cmts;
1256 e->command = "pktccops gatedel";
1258 "Usage: pktccops gatedel <cmts> <gateid>\n"
1259 " Send Gate-Del to cmts.\n";
1266 return CLI_SHOWUSAGE;
1268 AST_LIST_LOCK(&cmts_list);
1269 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
1270 if (!strcmp(cmts->name, a->argv[2])) {
1271 ast_cli(a->fd, "Found cmts: %s\n", cmts->name);
1276 AST_LIST_UNLOCK(&cmts_list);
1279 return CLI_SHOWUSAGE;
1282 if (!sscanf(a->argv[3], "%x", &gateid)) {
1283 ast_cli(a->fd, "bad gate specification (%s)\n", a->argv[3]);
1284 return CLI_SHOWUSAGE;
1288 AST_LIST_LOCK(&gate_list);
1289 AST_LIST_TRAVERSE(&gate_list, gate, list) {
1290 if (gate->gateid == gateid && gate->cmts == cmts) {
1297 ast_cli(a->fd, "gate not found: %s\n", a->argv[3]);
1298 return CLI_SHOWUSAGE;
1301 AST_LIST_UNLOCK(&gate_list);
1302 cops_gate_cmd(GATE_DEL, cmts, trid, 0, 0, 0, 0, 0, 0, gate);
1306 static char *pktccops_gateset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1310 unsigned int an,bn,cn,dn;
1312 struct cops_cmts *cmts;
1313 struct cops_gate *gate;
1317 e->command = "pktccops gateset";
1319 "Usage: pktccops gateset <cmts> <mta> <acctcount> <bitrate> <packet size> <switch ip> <switch port>\n"
1320 " Send Gate-Set to cmts.\n";
1327 return CLI_SHOWUSAGE;
1329 if (!strncmp(a->argv[2], "null", sizeof(a->argv[2]))) {
1332 AST_LIST_LOCK(&cmts_list);
1333 AST_LIST_TRAVERSE(&cmts_list, cmts, list) {
1334 if (!strcmp(cmts->name, a->argv[2])) {
1335 ast_cli(a->fd, "Found cmts: %s\n", cmts->name);
1340 AST_LIST_UNLOCK(&cmts_list);
1342 ast_cli(a->fd, "CMTS not found: %s\n", a->argv[2]);
1343 return CLI_SHOWUSAGE;
1348 if (sscanf(a->argv[3], "%3u.%3u.%3u.%3u", &an, &bn, &cn, &dn) != 4) {
1349 ast_cli(a->fd, "MTA specification (%s) does not look like an ipaddr\n", a->argv[3]);
1350 return CLI_SHOWUSAGE;
1352 mta = an << 24 | bn << 16 | cn << 8 | dn;
1354 if (sscanf(a->argv[7], "%3u.%3u.%3u.%3u", &an, &bn, &cn, &dn) != 4) {
1355 ast_cli(a->fd, "SSIP specification (%s) does not look like an ipaddr\n", a->argv[7]);
1356 return CLI_SHOWUSAGE;
1358 ssip = an << 24 | bn << 16 | cn << 8 | dn;
1360 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);
1364 static char *pktccops_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1368 e->command = "pktccops set debug {on|off}";
1370 "Usage: pktccops set debug {on|off}\n"
1371 " Turn on/off debuging\n";
1377 if (a->argc != e->args)
1378 return CLI_SHOWUSAGE;
1379 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
1381 ast_cli(a->fd, "PktcCOPS Debugging Enabled\n");
1382 } else if (!strncasecmp(a->argv[e->args - 1], "off", 2)) {
1384 ast_cli(a->fd, "PktcCOPS Debugging Disabled\n");
1386 return CLI_SHOWUSAGE;
1392 static struct ast_cli_entry cli_pktccops[] = {
1393 AST_CLI_DEFINE(pktccops_show_cmtses, "List PacketCable COPS CMTSes"),
1394 AST_CLI_DEFINE(pktccops_show_gates, "List PacketCable COPS GATEs"),
1395 AST_CLI_DEFINE(pktccops_show_pools, "List PacketCable MTA pools"),
1396 AST_CLI_DEFINE(pktccops_gateset, "Send Gate-Set to cmts"),
1397 AST_CLI_DEFINE(pktccops_gatedel, "Send Gate-Det to cmts"),
1398 AST_CLI_DEFINE(pktccops_debug, "Enable/Disable COPS debugging")
1401 static int pktccops_add_ippool(struct cops_ippool *ippool)
1404 AST_LIST_LOCK(&ippool_list);
1405 AST_LIST_INSERT_HEAD(&ippool_list, ippool, list);
1406 AST_LIST_UNLOCK(&ippool_list);
1409 ast_log(LOG_WARNING, "Attempted to register NULL ippool?\n");
1414 static void pktccops_unregister_cmtses(void)
1416 struct cops_cmts *cmts;
1417 struct cops_gate *gate;
1418 AST_LIST_LOCK(&cmts_list);
1419 while ((cmts = AST_LIST_REMOVE_HEAD(&cmts_list, list))) {
1420 if (cmts->sfd > 0) {
1425 AST_LIST_UNLOCK(&cmts_list);
1427 AST_LIST_LOCK(&gate_list);
1428 while ((gate = AST_LIST_REMOVE_HEAD(&gate_list, list))) {
1431 AST_LIST_UNLOCK(&gate_list);
1434 static void pktccops_unregister_ippools(void)
1436 struct cops_ippool *ippool;
1437 AST_LIST_LOCK(&ippool_list);
1438 while ((ippool = AST_LIST_REMOVE_HEAD(&ippool_list, list))) {
1441 AST_LIST_UNLOCK(&ippool_list);
1444 static int load_module(void)
1447 AST_LIST_LOCK(&cmts_list);
1448 res = load_pktccops_config();
1449 AST_LIST_UNLOCK(&cmts_list);
1451 return AST_MODULE_LOAD_DECLINE;
1453 ast_cli_register_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry));
1454 restart_pktc_thread();
1458 static int unload_module(void)
1460 if (!ast_mutex_lock(&pktccops_lock)) {
1461 if ((pktccops_thread != AST_PTHREADT_NULL) && (pktccops_thread != AST_PTHREADT_STOP)) {
1462 pthread_cancel(pktccops_thread);
1463 pthread_kill(pktccops_thread, SIGURG);
1464 pthread_join(pktccops_thread, NULL);
1466 pktccops_thread = AST_PTHREADT_STOP;
1467 ast_mutex_unlock(&pktccops_lock);
1469 ast_log(LOG_WARNING, "Unable to lock the pktccops_thread\n");
1473 ast_cli_unregister_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry));
1474 pktccops_unregister_cmtses();
1475 pktccops_unregister_ippools();
1476 pktccops_thread = AST_PTHREADT_NULL;
1480 static int reload_module(void)
1482 /* Prohibit unloading */
1484 ast_log(LOG_NOTICE, "Previous reload in progress, please wait!\n");
1491 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "PktcCOPS manager",
1492 .load = load_module,
1493 .unload = unload_module,
1494 .reload = reload_module,