Index: channels/chan_mgcp.c =================================================================== --- channels/chan_mgcp.c (revisión: 206803) +++ channels/chan_mgcp.c (copia de trabajo) @@ -70,6 +70,7 @@ #include "asterisk/stringfields.h" #include "asterisk/abstract_jb.h" #include "asterisk/event.h" +#include "asterisk/pktccops.h" /* * Define to work around buggy dlink MGCP phone firmware which @@ -149,6 +150,9 @@ static int dtmfmode = 0; static int nat = 0; +static int ncs = 0; +static int pktcgatealloc = 0; +static int hangupongateremove = 0; static ast_group_t cur_callergroup = 0; static ast_group_t cur_pickupgroup = 0; @@ -295,6 +299,8 @@ int iseq; /*!< Not used? RTP? */ int outgoing; int alreadygone; + int sdpsent; + struct cops_gate *gate; struct mgcp_subchannel *next; /*!< for out circular linked list */ }; @@ -337,6 +343,9 @@ int hidecallerid; int dtmfmode; int amaflags; + int ncs; + int pktcgatealloc; + int hangupongateremove; int type; int slowsequence; /*!< MS: Sequence the endpoint as a whole */ int group; @@ -389,9 +398,10 @@ ast_mutex_t msgs_lock; /*!< queue lock */ int retransid; /*!< retrans timer id */ int delme; /*!< needed for reload */ + int realtime; struct mgcp_response *responses; struct mgcp_gateway *next; -} *gateways; +} *gateways = NULL; AST_MUTEX_DEFINE_STATIC(mgcp_reload_lock); static int mgcp_reloading = 0; @@ -407,6 +417,7 @@ static int transmit_response(struct mgcp_subchannel *sub, char *msg, struct mgcp_request *req, char *msgrest); static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone); static int transmit_modify_request(struct mgcp_subchannel *sub); +static int transmit_connect(struct mgcp_subchannel *sub); static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername); static int transmit_modify_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp, int codecs); static int transmit_connection_del(struct mgcp_subchannel *sub); @@ -429,12 +440,18 @@ static int mgcp_senddigit_begin(struct ast_channel *ast, char digit); static int mgcp_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration); static int mgcp_devicestate(void *data); -static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp); +static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp, char *tone); +static int transmit_connect_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp); +static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v); +static int mgcp_alloc_pktcgate(struct mgcp_subchannel *sub); +static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen); + + static const struct ast_channel_tech mgcp_tech = { .type = "MGCP", .description = tdesc, - .capabilities = AST_FORMAT_ULAW, + .capabilities = AST_FORMAT_ALAW, .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER, .requester = mgcp_request, .devicestate = mgcp_devicestate, @@ -448,6 +465,7 @@ .send_digit_begin = mgcp_senddigit_begin, .send_digit_end = mgcp_senddigit_end, .bridge = ast_rtp_instance_bridge, + .func_channel_read = acf_channel_read, }; static void mwi_event_cb(const struct ast_event *event, void *userdata) @@ -774,12 +792,15 @@ ast_mutex_lock(l); q = sub->cx_queue; /* delete pending cx cmds */ - while (q) { - r = q->next; - ast_free(q); - q = r; + /* bugy sb5120 */ + if(!sub->parent->ncs) { + while (q) { + r = q->next; + ast_free(q); + q = r; + } + *queue = NULL; } - *queue = NULL; break; case MGCP_CMD_CRCX: @@ -871,7 +892,7 @@ ast_verb(3, "MGCP distinctive callwait %s\n", tone); } } else { - ast_copy_string(tone, "L/wt", sizeof(tone)); + ast_copy_string(tone, (p->ncs ? "L/wt1" : "L/wt"), sizeof(tone)); if (mgcpdebug) { ast_verb(3, "MGCP normal callwait %s\n", tone); } @@ -902,6 +923,7 @@ res = 0; sub->outgoing = 1; sub->cxmode = MGCP_CX_RECVONLY; + ast_setstate(ast, AST_STATE_RINGING); if (p->type == TYPE_LINE) { if (!sub->rtp) { start_rtp(sub); @@ -928,7 +950,6 @@ res = -1; } ast_mutex_unlock(&sub->lock); - ast_queue_control(ast, AST_CONTROL_RINGING); return res; } @@ -965,6 +986,21 @@ } sub->owner = NULL; + + /* for deleting gate */ + if(p->pktcgatealloc && sub->gate) { + sub->gate->gate_open = NULL; + sub->gate->gate_remove = NULL; + sub->gate->got_dq_gi = NULL; + sub->gate->tech_pvt = NULL; + if(sub->gate->state == GATE_ALLOC_PROGRESS || sub->gate->state == GATE_ALLOCATED) { + pktccops_gate_alloc(GATE_DEL, sub->gate, 0, 0, 0, 0, 0, 0, NULL, NULL); + } else { + sub->gate->deltimer = time(NULL) + 5; + } + sub->gate = NULL; + } + if (!ast_strlen_zero(sub->cxident)) { transmit_connection_del(sub); } @@ -972,7 +1008,8 @@ if ((sub == p->sub) && sub->next->owner) { if (p->hookstate == MGCP_OFFHOOK) { if (sub->next->owner && ast_bridged_channel(sub->next->owner)) { - transmit_notify_request_with_callerid(p->sub, "L/wt", ast_bridged_channel(sub->next->owner)->cid.cid_num, ast_bridged_channel(sub->next->owner)->cid.cid_name); + // ncs fix! + transmit_notify_request_with_callerid(p->sub, (p->ncs ? "L/wt1" : "L/wt"), ast_bridged_channel(sub->next->owner)->cid.cid_num, ast_bridged_channel(sub->next->owner)->cid.cid_name); } } else { /* set our other connection as the primary and swith over to it */ @@ -985,7 +1022,7 @@ } } else if ((sub == p->sub->next) && p->hookstate == MGCP_OFFHOOK) { - transmit_notify_request(sub, "L/v"); + transmit_notify_request(sub, p->ncs ? "" : "L/v"); } else if (p->hookstate == MGCP_OFFHOOK) { transmit_notify_request(sub, "L/ro"); } else { @@ -1056,7 +1093,7 @@ mg = gateways; while(mg) { me = mg->endpoints; - ast_cli(a->fd, "Gateway '%s' at %s (%s)\n", mg->name, mg->addr.sin_addr.s_addr ? ast_inet_ntoa(mg->addr.sin_addr) : ast_inet_ntoa(mg->defaddr.sin_addr), mg->dynamic ? "Dynamic" : "Static"); + ast_cli(a->fd, "Gateway '%s' at %s (%s%s)\n", mg->name, mg->addr.sin_addr.s_addr ? ast_inet_ntoa(mg->addr.sin_addr) : ast_inet_ntoa(mg->defaddr.sin_addr), mg->realtime ? "Realtime, " : "", mg->dynamic ? "Dynamic" : "Static"); while(me) { /* Don't show wilcard endpoint */ if (strcmp(me->name, mg->wcardep) != 0) @@ -1254,11 +1291,17 @@ if (!(frame->subclass & ast->nativeformats)) { ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n", frame->subclass, ast->nativeformats, ast->readformat, ast->writeformat); - return -1; + /* return -1; */ } } if (sub) { ast_mutex_lock(&sub->lock); + if(!sub->sdpsent && sub->gate) { + if(sub->gate->state == GATE_ALLOCATED) { + ast_log(LOG_DEBUG, "GATE ALLOCATED, sending sdp\n"); + transmit_modify_with_sdp(sub, NULL, 0); + } + } if ((sub->parent->sub == sub) || !sub->parent->singlepath) { if (sub->rtp) { res = ast_rtp_instance_write(sub->rtp, frame); @@ -1318,12 +1361,18 @@ ast_log(LOG_DEBUG, "Stopping DTMF using inband/hybrid\n"); res = -1; /* Tell Asterisk to stop inband indications */ } else if (p->dtmfmode & MGCP_DTMF_RFC2833) { - ast_log(LOG_DEBUG, "Stopping DTMF using RFC2833\n"); - tmp[0] = 'D'; - tmp[1] = '/'; - tmp[2] = digit; - tmp[3] = '\0'; - transmit_notify_request(sub, tmp); + + ast_log(LOG_DEBUG, "Stopping DTMF using RFC2833\n"); + if (sub->parent->ncs) { + tmp[0] = digit; + tmp[1] = '\0'; + } else { + tmp[0] = 'D'; + tmp[1] = '/'; + tmp[2] = digit; + tmp[3] = '\0'; + } + transmit_notify_request(sub, tmp); ast_rtp_instance_dtmf_end(sub->rtp, digit); } else { ast_log(LOG_ERROR, "Don't know about DTMF_MODE %d\n", p->dtmfmode); @@ -1437,14 +1486,15 @@ #ifdef DLINK_BUGGY_FIRMWARE transmit_notify_request(sub, "rt"); #else - transmit_notify_request(sub, "G/rt"); + if(!sub->sdpsent) /* will hide the inband progress!!! */ + transmit_notify_request(sub, sub->parent->ncs ? "L/rt" : "G/rt"); #endif break; case AST_CONTROL_BUSY: transmit_notify_request(sub, "L/bz"); break; case AST_CONTROL_CONGESTION: - transmit_notify_request(sub, "G/cg"); + transmit_notify_request(sub, sub->parent->ncs ? "L/cg" : "G/cg"); break; case AST_CONTROL_HOLD: ast_moh_start(ast, data, NULL); @@ -1455,6 +1505,9 @@ case AST_CONTROL_SRCUPDATE: ast_rtp_instance_new_source(sub->rtp); break; + case AST_CONTROL_PROGRESS: + case AST_CONTROL_PROCEEDING: + transmit_modify_request(sub); case -1: transmit_notify_request(sub, ""); break; @@ -1573,7 +1626,7 @@ return ""; } -static char *__get_header(struct mgcp_request *req, char *name, int *start) +static char *__get_header(struct mgcp_request *req, char *name, int *start, char *def) { int x; int len = strlen(name); @@ -1589,13 +1642,13 @@ } } /* Don't return NULL, so get_header is always a valid pointer */ - return ""; + return def; } static char *get_header(struct mgcp_request *req, char *name) { int start = 0; - return __get_header(req, name, &start); + return __get_header(req, name, &start, ""); } /*! \brief get_csv: (SC:) get comma separated value */ @@ -1620,6 +1673,98 @@ return s; } +static struct mgcp_gateway *find_realtime_gw(char *name, char *at, struct sockaddr_in *sin) +{ + struct mgcp_gateway *g = NULL; + struct ast_variable *mgcpgwconfig = NULL; + struct ast_variable *mgcpepconfig = NULL; + struct ast_variable *gwv, *epname = NULL; + struct mgcp_endpoint *e; + char *c = NULL, *line; + char lines[256]; + char tmp[4096]; + int i, j; + if(mgcpdebug) ast_verbose ("*** find Realtime MGCPGW\n"); + + if (!(i = ast_check_realtime("mgcpgw")) || !(j = ast_check_realtime("mgcpep"))) + return NULL; + + if(!at || !strlen(at)) { + ast_log(LOG_DEBUG, "null gw name\n"); + return NULL; + } + + mgcpgwconfig = ast_load_realtime("mgcpgw", "name", at, NULL); + if(!mgcpgwconfig) + return NULL; + + gwv = mgcpgwconfig; + while(gwv) { + if(!strcasecmp(gwv->name, "lines")) { + strcpy(lines, gwv->value); + break; + } + gwv = gwv->next; + } + gwv = mgcpgwconfig; + while(gwv->next) + gwv = gwv->next; + if(lines) { + c = lines; + line = tmp; + while(*c) { + *line = *c; + if(*c == ',') { + *(line) = 0; + mgcpepconfig = ast_load_realtime("mgcpep", "name", at, "line", tmp, NULL); + gwv->next = mgcpepconfig; + + while(gwv->next) { + if(!strcasecmp(gwv->next->name, "line")) { + epname = gwv->next; + gwv->next = gwv->next->next; + } else { + gwv = gwv->next; + } + } + /* moving the line var to the end */ + if(epname) { + gwv->next = epname; + epname->next = NULL; + gwv = gwv->next; + } + mgcpepconfig = NULL; + line = tmp; + } else { + line ++; + } + c++; + } + } + gwv = mgcpgwconfig; + while(gwv) { + ast_log(LOG_DEBUG, "MGCP Realtime var: %s => %s\n", gwv->name, gwv->value); + gwv = gwv->next; + } + + if(mgcpgwconfig) { + g = build_gateway(at, mgcpgwconfig); + ast_variables_destroy(mgcpgwconfig); + } + if(g) { + g->next = gateways; + g->realtime = 1; + gateways = g; + e = g->endpoints; + while(e) { + transmit_audit_endpoint(e); + e->needaudit = 0; + e = e->next; + } + } + return g; +} + static struct mgcp_subchannel *find_subchannel_and_lock(char *name, int msgid, struct sockaddr_in *sin) { struct mgcp_endpoint *p = NULL; @@ -1645,6 +1790,8 @@ *c = '\0'; } g = gateways; + if(!g) + g = find_realtime_gw(name, at, sin); while(g) { if ((!name || !strcasecmp(g->name, at)) && (sin || g->addr.sin_addr.s_addr || g->defaddr.sin_addr.s_addr)) { @@ -1669,11 +1816,17 @@ else if (!name && sin) { if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) || (g->addr.sin_port != sin->sin_port)) { - g = g->next; + if(!g->next) + g = find_realtime_gw(name, at, sin); + else + g = g->next; continue; } } else { - g = g->next; + if(!g->next) + g = find_realtime_gw(name, at, sin); + else + g = g->next; continue; } /* SC */ @@ -1718,7 +1871,10 @@ break; } } - g = g->next; + if(!g->next) + g = find_realtime_gw(name, at, sin); + else + g = g->next; } ast_mutex_unlock(&gatelock); if (!sub) { @@ -1991,9 +2147,9 @@ req->header[req->headers] = req->data + req->len; /* check if we need brackets around the gw name */ if (p->parent->isnamedottedip) - snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %d %s@[%s] MGCP 1.0\r\n", verb, oseq, p->name, p->parent->name); + snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %d %s@[%s] MGCP 1.0%s\r\n", verb, oseq, p->name, p->parent->name, p->ncs ? " NCS 1.0" : ""); else - snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %d %s@%s MGCP 1.0\r\n", verb, oseq, p->name, p->parent->name); ++ snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %d %s@%s MGCP 1.0%s\r\n", verb, oseq, p->name, p->parent->name, p->ncs ? " NCS 1.0" : ""); req->len += strlen(req->header[req->headers]); if (req->headers < MGCP_MAX_HEADERS) req->headers++; @@ -2026,6 +2182,9 @@ struct mgcp_endpoint *p = sub->parent; struct mgcp_response *mgr; + if(!sub) + return -1; + respprep(&resp, p, msg, req, msgrest); mgr = ast_calloc(1, sizeof(*mgr) + resp.len + 1); if (mgr) { @@ -2140,8 +2299,8 @@ struct mgcp_request resp; char local[256]; char tmp[80]; + struct mgcp_endpoint *p = sub->parent; int x; - struct mgcp_endpoint *p = sub->parent; if (ast_strlen_zero(sub->cxident) && rtp) { /* We don't have a CXident yet, store the destination and @@ -2149,13 +2308,28 @@ ast_rtp_instance_get_remote_address(rtp, &sub->tmpdest); return 0; } - ast_copy_string(local, "p:20", sizeof(local)); + ast_copy_string(local, "e:on, s:off, p:20", sizeof(local)); for (x = 1; x <= AST_FORMAT_AUDIO_MASK; x <<= 1) { if (p->capability & x) { snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, x, 0)); strncat(local, tmp, sizeof(local) - strlen(local) - 1); } } + + if (sub->gate) { + if(sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) { + snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid); + strncat(local, tmp, sizeof(local) - strlen(local) - 1); + sub->sdpsent = 1; + } else { + /* oops wait */ + ast_log(LOG_DEBUG, "Waiting for opened gate...\n"); + sub->sdpsent = 0; + return 0; + } + } + + reqprep(&resp, p, "MDCX"); add_header(&resp, "C", sub->callid); add_header(&resp, "L", local); @@ -2175,35 +2349,127 @@ { struct mgcp_request resp; char local[256]; - char tmp[80]; + char tmp[80]; int x; struct mgcp_endpoint *p = sub->parent; - - ast_copy_string(local, "p:20", sizeof(local)); + + if (mgcpdebug) { + ast_verb(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n", + p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid); + } + + ast_copy_string(local, "e:on, s:off, p:20", sizeof(local)); + for (x = 1; x <= AST_FORMAT_AUDIO_MASK; x <<= 1) { if (p->capability & x) { snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, x, 0)); strncat(local, tmp, sizeof(local) - strlen(local) - 1); } } - if (mgcpdebug) { - ast_verb(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n", - p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid); - } - reqprep(&resp, p, "CRCX"); - add_header(&resp, "C", sub->callid); - add_header(&resp, "L", local); - add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]); - /* X header should not be sent. kept for compatibility */ - add_header(&resp, "X", sub->txident); - /*add_header(&resp, "S", "");*/ - add_sdp(&resp, sub, rtp); - /* fill in new fields */ - resp.cmd = MGCP_CMD_CRCX; - resp.trid = oseq; - return send_request(p, sub, &resp, oseq); /* SC */ + + if (sub->gate) { + if(sub->gate->state == GATE_ALLOCATED) { + snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid); + strncat(local, tmp, sizeof(local) - strlen(local) - 1); + } + } + sub->sdpsent = 1; + reqprep(&resp, p, "CRCX"); + add_header(&resp, "C", sub->callid); + add_header(&resp, "L", local); + add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]); + /* X header should not be sent. kept for compatibility */ + add_header(&resp, "X", sub->txident); + /*add_header(&resp, "S", "");*/ + add_sdp(&resp, sub, rtp); + /* fill in new fields */ + resp.cmd = MGCP_CMD_CRCX; + resp.trid = oseq; + return send_request(p, sub, &resp, oseq); /* SC */ } + + +static int mgcp_pktcgate_remove(struct cops_gate *gate) +{ + struct mgcp_subchannel *sub = gate->tech_pvt; + if(sub) { + ast_mutex_lock(&sub->lock); + ast_log(LOG_DEBUG, "Pktc: gate 0x%x deleted\n", gate->gateid); + if(sub->gate->state != GATE_CLOSED && sub->parent->hangupongateremove) { + sub->gate = NULL; + if(sub->owner) { + ast_softhangup(sub->owner, AST_CAUSE_REQUESTED_CHAN_UNAVAIL); + ast_channel_unlock(sub->owner); + } + } else { + sub->gate = NULL; + } + ast_mutex_unlock(&sub->lock); + } + return 1; +} + +static int mgcp_pktcgate_open(struct cops_gate *gate) +{ + struct mgcp_subchannel *sub = gate->tech_pvt; + if(sub) { + ast_mutex_lock(&sub->lock); + ast_log(LOG_DEBUG, "Pktc: gate 0x%x open\n", gate->gateid); + if (!sub->sdpsent) transmit_modify_with_sdp(sub, NULL, 0); + ast_mutex_unlock(&sub->lock); + } + return 1; +} + +static int mgcp_alloc_pktcgate(struct mgcp_subchannel *sub) +{ + struct mgcp_endpoint *p = sub->parent; + sub->gate = pktccops_gate_alloc(GATE_SET, NULL, ntohl(p->parent->addr.sin_addr.s_addr), + 8, 128000, 232, 0, 0, NULL, &mgcp_pktcgate_remove); + + if(!sub->gate) + return 0; + sub->gate->tech_pvt = sub; + sub->gate->gate_open = &mgcp_pktcgate_open; + return 1; +} + +static int transmit_connect(struct mgcp_subchannel *sub) +{ + struct mgcp_request resp; + char local[256]; + char tmp[80]; + int x; + struct mgcp_endpoint *p = sub->parent; + + ast_copy_string(local, "p:20, s:off, e:on", sizeof(local)); + + for (x = 1; x <= AST_FORMAT_AUDIO_MASK; x <<= 1) { + if (p->capability & x) { + snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, x, 0)); + strncat(local, tmp, sizeof(local) - strlen(local) - 1); + } + } + + if (mgcpdebug) { + ast_verbose(VERBOSE_PREFIX_3 "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n", + p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid); + } + sub->sdpsent = 0; + reqprep(&resp, p, "CRCX"); + add_header(&resp, "C", sub->callid); + add_header(&resp, "L", local); + add_header(&resp, "M", "inactive"); + /* X header should not be sent. kept for compatibility */ + add_header(&resp, "X", sub->txident); + /*add_header(&resp, "S", "");*/ + /* fill in new fields */ + resp.cmd = MGCP_CMD_CRCX; + resp.trid = oseq; + return send_request(p, sub, &resp, oseq); /* SC */ +} + static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone) { struct mgcp_request resp; @@ -2221,7 +2487,7 @@ add_header(&resp, "R", "L/hd(N)"); break; case MGCP_OFFHOOK: - add_header_offhook(sub, &resp); + add_header_offhook(sub, &resp, tone); break; } if (!ast_strlen_zero(tone)) { @@ -2263,7 +2529,7 @@ add_header(&resp, "R", "L/hd(N)"); break; case MGCP_OFFHOOK: - add_header_offhook(sub, &resp); + add_header_offhook(sub, &resp, tone); break; } if (!ast_strlen_zero(tone2)) { @@ -2283,6 +2549,9 @@ { struct mgcp_request resp; struct mgcp_endpoint *p = sub->parent; + int x, fc; + char local[256]; + char tmp[80]; if (ast_strlen_zero(sub->cxident)) { /* We don't have a CXident yet, store the destination and @@ -2293,8 +2562,36 @@ ast_verb(3, "Modified %s@%s-%d with new mode: %s on callid: %s\n", p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid); } + + ast_copy_string(local, "", sizeof(local)); + for (x=1;x<= AST_FORMAT_AUDIO_MASK; x <<= 1) { + if (p->capability & x) { + if(p->ncs && !fc) { + p->capability = x; /* sb5120e bug */ + break; + } else { + fc = 0; + snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, x, 0)); + } + strncat(local, tmp, sizeof(local) - strlen(local) - 1); + } + } + + if(!sub->sdpsent) { + if (sub->gate) { + if(sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) { + snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid); + strncat(local, tmp, sizeof(local) - strlen(local) - 1); + } else { + /* we still dont have gateid wait */ + return 0; + } + } + } + reqprep(&resp, p, "MDCX"); add_header(&resp, "C", sub->callid); + if(!sub->sdpsent) add_header(&resp, "L", local); add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]); /* X header should not be sent. kept for compatibility */ add_header(&resp, "X", sub->txident); @@ -2304,9 +2601,13 @@ add_header(&resp, "R", "L/hd(N)"); break; case MGCP_OFFHOOK: - add_header_offhook(sub, &resp); + add_header_offhook(sub, &resp, ""); break; } + if(!sub->sdpsent) { + add_sdp(&resp, sub, NULL); + sub->sdpsent = 1; + } /* fill in new fields */ resp.cmd = MGCP_CMD_MDCX; resp.trid = oseq; @@ -2314,16 +2615,36 @@ } -static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp) +static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp, char *tone) { - struct mgcp_endpoint *p = sub->parent; + struct mgcp_endpoint *p = sub->parent; + char tone_indicate_end = 0; + + /* We also should check the tone to indicate, because it have no sense + to request notify D/[0-9#*] (dtmf keys) if we are sending congestion + tone for example G/cg */ + if (p && (!strcasecmp(tone, (p->ncs ? "L/ro" : "G/cg")))) { + tone_indicate_end = 1; + } + + if (p && p->sub && p->sub->owner && + p->sub->owner->_state >= AST_STATE_RINGING && + (p->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID))) { + add_header(resp, "R", "L/hu(N),L/hf(N)"); - if (p && p->sub && p->sub->owner && p->sub->owner->_state >= AST_STATE_RINGING && (p->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID))) - add_header(resp, "R", "L/hu(N),L/hf(N)"); - else - add_header(resp, "R", "L/hu(N),L/hf(N),D/[0-9#*](N)"); + } else if (!tone_indicate_end){ + add_header(resp, "R", (p->ncs ? "L/hu(N),L/hf(N),L/[0-9#*](N)" : "L/hu(N),L/hf(N),D/[0-9#*](N)")); + } else { + if (mgcpdebug) { + ast_verbose("We don't want more digits if we will end the call\n"); + } + add_header(resp, "R", "L/hu(N),L/hf(N)"); + } } + + + static int transmit_audit_endpoint(struct mgcp_endpoint *p) { struct mgcp_request resp; @@ -2489,6 +2810,10 @@ break; } if (sub) { + if (!sub->cxident[0] && (req->cmd == MGCP_CMD_CRCX)) { + ast_log(LOG_NOTICE, "DLCX for all connections on %s due to error %d\n", gw->name, result); + transmit_connection_del(sub); + } if (sub->owner) { ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n", result, p->name, p->parent->name, sub ? sub->id:-1); @@ -2512,6 +2837,15 @@ } if (resp) { + /* responseAck: */ + if (result == 200 && (req->cmd == MGCP_CMD_CRCX || req->cmd == MGCP_CMD_MDCX)) { + if (sub) { + transmit_response(sub, "000", resp, "OK"); + if (sub->owner && sub->owner->_state == AST_STATE_RINGING) { + ast_queue_control(sub->owner, AST_CONTROL_RINGING); + } + } + } if (req->cmd == MGCP_CMD_CRCX) { if ((c = get_header(resp, "I"))) { if (!ast_strlen_zero(c) && sub) { @@ -2624,7 +2958,14 @@ /* Make a call*ID */ snprintf(sub->callid, sizeof(sub->callid), "%08lx%s", ast_random(), sub->txident); /* Transmit the connection create */ - transmit_connect_with_sdp(sub, NULL); + if(!sub->parent->pktcgatealloc) { + transmit_connect_with_sdp(sub, NULL); + } else { + transmit_connect(sub); + sub->gate = NULL; + if(!mgcp_alloc_pktcgate(sub)) + mgcp_queue_hangup(sub); + } ast_mutex_unlock(&sub->lock); } @@ -2643,6 +2984,7 @@ len = strlen(p->dtmf_buf); while(len < AST_MAX_EXTENSION-1) { + ast_log(LOG_DEBUG, "Dtmf buffer '%s' for '%s@%s'\n", p->dtmf_buf, p->name, p->parent->name); res = 1; /* Assume that we will get a digit */ while (strlen(p->dtmf_buf) == len){ ast_safe_sleep(chan, loop_pause); @@ -2689,6 +3031,7 @@ /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/ ast_indicate(chan, -1); ast_copy_string(chan->exten, p->dtmf_buf, sizeof(chan->exten)); + chan->cid.cid_dnid = ast_strdup(p->dtmf_buf); memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf)); ast_set_callerid(chan, p->hidecallerid ? "" : p->cid_num, @@ -2705,7 +3048,7 @@ ast_log(LOG_WARNING, "PBX exited non-zero\n"); /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_CONGESTION);*/ /*transmit_notify_request(p, "nbz", 1);*/ - transmit_notify_request(sub, "G/cg"); + transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg"); } return NULL; } @@ -2717,7 +3060,7 @@ } else if (res == 0) { ast_debug(1, "not enough digits (and no ambiguous match)...\n"); /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_CONGESTION);*/ - transmit_notify_request(sub, "G/cg"); + transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg"); /*dahdi_wait_event(p->subs[index].zfd);*/ ast_hangup(chan); memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf)); @@ -2739,7 +3082,7 @@ if (ast_pickup_call(chan)) { ast_log(LOG_WARNING, "No call pickup possible...\n"); /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_CONGESTION);*/ - transmit_notify_request(sub, "G/cg"); + transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg"); } memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf)); ast_hangup(chan); @@ -2965,12 +3308,12 @@ #ifdef DLINK_BUGGY_FIRMWARE transmit_notify_request(sub, "rt"); #else - transmit_notify_request(sub, "G/rt"); + transmit_notify_request(sub, p->ncs ? "L/rt" : "G/rt"); #endif c = mgcp_new(sub, AST_STATE_RING, NULL); if (!c) { ast_log(LOG_WARNING, "Unable to start PBX on channel %s@%s\n", p->name, p->parent->name); - transmit_notify_request(sub, "G/cg"); ++ transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg"); ast_hangup(c); } } else { @@ -3087,6 +3430,13 @@ if (!strcasecmp(ev, "hd")) { p->hookstate = MGCP_OFFHOOK; sub->cxmode = MGCP_CX_SENDRECV; + + if (p) { + /* When the endpoint have a Off hook transition we allways + starts without any previous dtmfs */ + memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf)); + } + handle_hd_hf(sub, ev); } else if (!strcasecmp(ev, "hf")) { /* We can assume we are offhook if we received a hookflash */ @@ -3232,7 +3582,7 @@ if (sub->next->owner) mgcp_queue_frame(sub->next, &f); ast_mutex_unlock(&sub->next->lock); - if (strstr(p->curtone, "wt") && (ev[0] == 'A')) { + if (strstr(p->curtone, (p->ncs ? "wt1" : "wt")) && (ev[0] == 'A')) { memset(p->curtone, 0, sizeof(p->curtone)); } } else { @@ -3318,6 +3668,10 @@ } if (sscanf(req.verb, "%d", &result) && sscanf(req.identifier, "%d", &ident)) { + if (result < 200) { + ast_log(LOG_DEBUG, "Ignoring provisional response on transaction %d\n", ident); + return 1; + } /* Try to find who this message is for, if it's important */ sub = find_subchannel_and_lock(NULL, ident, &sin); if (sub) { @@ -3374,13 +3728,76 @@ static int *mgcpsock_read_id = NULL; +static int mgcp_prune_realtime_gateway(struct mgcp_gateway *g){ + struct mgcp_endpoint *enext, *e = g->endpoints; + struct mgcp_subchannel *s, *sub; + int i, prune = 1; + + if (g->ha || !g->realtime || ast_mutex_trylock(&g->msgs_lock) || g->msgs) { + ast_mutex_unlock(&g->msgs_lock); + return 0; + } + + while(e) { + ast_mutex_lock(&e->lock); + if(e->dsp || ast_mutex_trylock(&e->rqnt_queue_lock) || ast_mutex_trylock(&e->cmd_queue_lock)) { + prune = 0; + } else if( e->rqnt_queue || e->cmd_queue ) { + prune = 0; + } + s = e->sub; + for (i = 0; (i < MAX_SUBS) && s; i++) { + ast_mutex_lock(&s->lock); + if (!ast_strlen_zero(s->cxident) || s->rtp || ast_mutex_trylock(&s->cx_queue_lock) || s->gate) { + prune = 0; + } else if (s->cx_queue) { + prune = 0; + } + s = s->next; + } + e = e->next; + } + + e = g->endpoints; + + while(e) { + sub = e->sub; + enext = e->next; + for (i = 0; (i < MAX_SUBS) && sub; i++) { + s = sub; + sub = sub->next; + ast_mutex_unlock(&s->lock); + ast_mutex_unlock(&s->cx_queue_lock); + if(prune) { + ast_mutex_destroy(&s->lock); + ast_mutex_destroy(&s->cx_queue_lock); + free(s); + } + } + ast_mutex_unlock(&e->lock); + ast_mutex_unlock(&e->rqnt_queue_lock); + ast_mutex_unlock(&e->cmd_queue_lock); + if(prune) { + ast_mutex_destroy(&e->lock); + ast_mutex_destroy(&e->rqnt_queue_lock); + ast_mutex_destroy(&e->cmd_queue_lock); + free(e); + } + e = enext; + } + if(prune) ast_log(LOG_DEBUG, "***** MGCP REALTIME PRUNE GW: %s\n", g->name); + return prune; +} + static void *do_monitor(void *data) { int res; int reloading; + struct mgcp_gateway *g, *gprev, *gnext; /*struct mgcp_gateway *g;*/ /*struct mgcp_endpoint *e;*/ /*time_t thispass = 0, lastpass = 0;*/ + time_t lastrun = 0; /* Add an I/O event to our UDP socket */ if (mgcpsock > -1) @@ -3440,6 +3857,36 @@ g = g->next; } #endif + /* pruning unused realtime gateways, running in every 60 seconds*/ + if(time(NULL) > (lastrun + 60)) { + ast_mutex_lock(&gatelock); + g = gateways; + gprev = NULL; + while(g) { + gnext = g->next; + if(g->realtime) { + if(mgcp_prune_realtime_gateway(g)) { + if(gprev) { + gprev->next = gnext; + gprev = g; + } else { + gateways = g->next; + } + ast_mutex_unlock(&g->msgs_lock); + ast_mutex_destroy(&g->msgs_lock); + free(g); + } else { + ast_mutex_unlock(&g->msgs_lock); + gprev = g; + } + } else { + gprev = g; + } + g = gnext; + } + ast_mutex_unlock(&gatelock); + lastrun = time(NULL); + } /* Okay, now that we know what to do, release the network lock */ ast_mutex_unlock(&netlock); /* And from now on, we're okay to be killed, so release the monitor lock as well */ @@ -3501,7 +3948,7 @@ format &= capability; if (!format) { ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format); - return NULL; + /*return NULL;*/ } ast_copy_string(tmp, dest, sizeof(tmp)); if (ast_strlen_zero(tmp)) { @@ -3572,6 +4019,7 @@ if (gw) { if (!gw_reload) { gw->expire = -1; + gw->realtime = 0; gw->retransid = -1; /* SC */ ast_mutex_init(&gw->msgs_lock); ast_copy_string(gw->name, cat, sizeof(gw->name)); @@ -3630,6 +4078,12 @@ ast_log(LOG_WARNING, "'%s' is not a valid DTMF mode at line %d\n", v->value, v->lineno); } else if (!strcasecmp(v->name, "nat")) { nat = ast_true(v->value); + } else if (!strcasecmp(v->name, "ncs")) { + ncs = ast_true(v->value); + } else if (!strcasecmp(v->name, "hangupongateremove")) { + hangupongateremove = ast_true(v->value); + } else if (!strcasecmp(v->name, "pktcgatealloc")) { + pktcgatealloc = ast_true(v->value); } else if (!strcasecmp(v->name, "callerid")) { if (!strcasecmp(v->value, "asreceived")) { cid_num[0] = '\0'; @@ -3737,6 +4191,7 @@ e->amaflags = amaflags; e->capability = capability; e->parent = gw; + e->ncs = ncs; e->dtmfmode = dtmfmode; if (!ep_reload && e->sub && e->sub->rtp) e->dtmfmode |= MGCP_DTMF_INBAND; @@ -3771,6 +4226,8 @@ /*stnrcpy(sub->txident, txident, sizeof(sub->txident) - 1);*/ sub->cxmode = MGCP_CX_INACTIVE; sub->nat = nat; + sub->gate = NULL; + sub->sdpsent = 0; sub->next = e->sub; e->sub = sub; } else { @@ -3840,6 +4297,9 @@ e->amaflags = amaflags; e->capability = capability; e->dtmfmode = dtmfmode; + e->ncs = ncs; + e->pktcgatealloc = pktcgatealloc; + e->hangupongateremove = hangupongateremove; e->adsi = adsi; if (!strcasecmp(v->name, "trunk")) e->type = TYPE_TRUNK; @@ -3909,6 +4369,8 @@ gw->endpoints = e; } } + } else if (!strcasecmp(v->name, "name") || !strcasecmp(v->name, "lines")) { + /* just eliminate realtime warnings */ } else ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno); v = v->next; @@ -3962,12 +4424,41 @@ return -1; } +static int mgcp_get_codec(struct ast_channel *chan) +{ + struct mgcp_subchannel *sub = chan->tech_pvt; + struct mgcp_endpoint *p = sub->parent; + return p->capability; +} + static struct ast_rtp_glue mgcp_rtp_glue = { .type = "MGCP", .get_rtp_info = mgcp_get_rtp_peer, .update_peer = mgcp_set_rtp_peer, + .get_codec = mgcp_get_codec, }; + +static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *args, char *buf, size_t buflen) +{ + struct mgcp_subchannel *sub = chan->tech_pvt; + int res = 0; + + /* Sanity check */ + if (!chan || chan->tech != &mgcp_tech) { + ast_log(LOG_ERROR, "This function requires a valid MGCP channel\n"); + return -1; + } + + if (!strcasecmp(args, "ncs")) { + snprintf(buf, buflen, "%s", sub->parent->ncs ? "yes":"no"); + } else { + res = -1; + } + return res; +} + + static void destroy_endpoint(struct mgcp_endpoint *e) { struct mgcp_subchannel *sub = e->sub->next, *s; @@ -3985,6 +4476,12 @@ memset(sub->magic, 0, sizeof(sub->magic)); mgcp_queue_hangup(sub); dump_cmd_queues(NULL, sub); + if(sub->gate) { + sub->gate->tech_pvt = NULL; + sub->gate->got_dq_gi = NULL; + sub->gate->gate_remove = NULL; + sub->gate->gate_open = NULL; + } ast_mutex_unlock(&sub->lock); sub = sub->next; } @@ -4035,7 +4532,7 @@ for (z = NULL, g = gateways; g;) { /* prune endpoints */ for (p = NULL, e = g->endpoints; e; ) { - if (e->delme || g->delme) { + if (!g->realtime && (e->delme || g->delme)) { t = e; e = e->next; if (!p) @@ -4147,6 +4644,12 @@ } else { ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config); } + } else if (!strcasecmp(v->name, "firstdigittimeout")) { + firstdigittimeout = atoi(v->value); + } else if (!strcasecmp(v->name, "gendigittimeout")) { + gendigittimeout = atoi(v->value); + } else if (!strcasecmp(v->name, "matchdigittimeout")) { + matchdigittimeout = atoi(v->value); } v = v->next; } Index: include/asterisk/pktccops.h =================================================================== --- include/asterisk/pktccops.h (revisión: 0) +++ include/asterisk/pktccops.h (revisión: 0) @@ -0,0 +1,47 @@ + +/*! \file + * + * \brief PacketCable COPS + * + * \author Attila Domjan + * + */ + +enum { + GATE_SET, + GATE_INFO, + GATE_SET_HAVE_GATEID, + GATE_DEL +}; + +enum { + GATE_ALLOC_FAILED, + GATE_ALLOC_PROGRESS, + GATE_ALLOCATED, + GATE_CLOSED, + GATE_CLOSED_ERR, + GATE_OPEN, + GATE_DELETED, + GATE_TIMEOUT +}; + +struct cops_gate +{ + AST_LIST_ENTRY(cops_gate) list; + uint32_t gateid; + uint16_t trid; + time_t in_transaction; + uint32_t mta; + int state; + time_t allocated; + time_t checked; + time_t deltimer; + struct cops_cmts *cmts; + int (* got_dq_gi) (struct cops_gate *gate); + int (* gate_remove) (struct cops_gate *gate); + int (* gate_open) (struct cops_gate *gate); + void *tech_pvt; +}; + + +struct cops_gate * pktccops_gate_alloc(int cmd, struct cops_gate *gate, uint32_t mta, uint32_t actcount, float bitrate, uint32_t psize, uint32_t ssip, uint16_t ssport, int (* const got_dq_gi) (struct cops_gate *gate), int (* const gate_remove) (struct cops_gate *gate)); Index: configs/mgcp.conf.sample =================================================================== --- configs/mgcp.conf.sample (revisión: 206803) +++ configs/mgcp.conf.sample (copia de trabajo) @@ -11,6 +11,11 @@ ;cos=3 ; Sets 802.1p priority for signaling packets. ;cos_audio=5 ; Sets 802.1p priority for RTP audio packets. +;---------------------- DIGIT TIMEOUTS ---------------------------- +firstdigittimeout = 30000 ; default 16000 = 16s +gendigittimeout = 10000 ; default 8000 = 8s +matchdigittimeout = 5000 ; defaults 3000 = 3s + ;------------------------------ JITTER BUFFER CONFIGURATION -------------------------- ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of a ; MGCP channel. Defaults to "no". An enabled jitterbuffer will @@ -47,27 +52,27 @@ ;; The MGCP channel supports the following service codes: ;; # - Transfer -;; *67 - Calling Number Delivery Blocking -;; *70 - Cancel Call Waiting -;; *72 - Call Forwarding Activation -;; *73 - Call Forwarding Deactivation -;; *78 - Do Not Disturb Activation -;; *79 - Do Not Disturb Deactivation +;; *67 - Calling Number Delivery Blocking +;; *70 - Cancel Call Waiting +;; *72 - Call Forwarding Activation +;; *73 - Call Forwarding Deactivation +;; *78 - Do Not Disturb Activation +;; *79 - Do Not Disturb Deactivation ;; *8 - Call pick-up ; -; known to work with Swissvoice IP10s -;[192.168.1.20] -;context=local -;host=192.168.1.20 -;callerid = "John Doe" <123> +; known to work with Swissvoice IP10s +;[192.168.1.20] +;context=local +;host=192.168.1.20 +;callerid = "John Doe" <123> ;callgroup=0 ; in the range from 0 to 63 ;pickupgroup=0 ; in the range from 0 to 63 -;nat=no -;threewaycalling=yes +;nat=no +;threewaycalling=yes ;transfer=yes ; transfer requires threewaycalling=yes. Use FLASH to transfer ;callwaiting=yes ; this might be a cause of trouble for ip10s -;cancallforward=yes -;line => aaln/1 +;cancallforward=yes +;line => aaln/1 ; ;[dph100] @@ -90,7 +95,7 @@ ; 'documentation', or 'omit' ;context = local ;host = 192.168.1.20 -;wcardep = aaln/* ; enables wildcard endpoint and sets it to 'aaln/*' +;wcardep = aaln/* ; enables wildcard endpoint and sets it to 'aaln/*' ; another common format is '*' ;callerid = "Duane Cox" <123> ; now lets setup line 1 using per endpoint configuration... ;callwaiting = no @@ -108,3 +113,20 @@ ;transfer = no ;dtmfmode = inband ;line => aaln/2 ; now lets save this config to line2 aka aaln/2 + +;PacketCable +[sbv5121e-mta.test.local] +host = 10.0.1.3 +callwaiting = 1 +canreinvite = 1 +dtmfmode = rfc2833 +amaflags = BILLING +ncs = 1 ; Use NCS 1.0 signalling +pktcgatealloc = 1 ; Allocate DQOS gate on CMTS +hangupongateremove = 1 ; Hangup the channel if the CMTS close the gate +callerid = 3622622225 +accountcode = teszt-3622622225 +line = aaln/1 +callerid = 3622622226 +accountcode = teszt-3622622226 +line = aaln/2 Index: configs/res_pktccops.conf.sample =================================================================== --- configs/res_pktccops.conf.sample (revisión: 0) +++ configs/res_pktccops.conf.sample (revisión: 0) @@ -0,0 +1,15 @@ +[general] +gateinfoperiod => 60 ; default 60s +gatetimeout = 150 ; default 150 +t1 => 250 ; default 250s +t7 => 200 ; default 200s +t8 => 300 ; default 300s +keepalive => 60 ; default 60s + +[teszt] +host => 192.168.0.24 +pool => 10.0.1.0 10.0.1.255 +pool => 10.0.3.0 10.0.3.255 +pool => 10.0.7.0 10.0.8.255 +pool => 10.0.10.0 10.0.11.255 + Index: res/res_pktccops.c =================================================================== --- res/res_pktccops.c (revisión: 0) +++ res/res_pktccops.c (revisión: 0) @@ -0,0 +1,1721 @@ + +/*! \file + * + * \brief PacketCable COPS + * + * \author Attila Domjan + * + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision: 99775 $") + +#define CC_AST_HAS_VERSION_1_6 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "asterisk/file.h" +#include "asterisk/logger.h" +#include "asterisk/channel.h" +#include "asterisk/config.h" +#include "asterisk/options.h" +#include "asterisk/pbx.h" +#include "asterisk/module.h" +#include "asterisk/cli.h" +#include "asterisk/lock.h" +#include "asterisk/pktccops.h" + +#define DEFAULT_COPS_PORT "2126" + +#define COPS_HEADER_SIZE 8 +#define COPS_OBJECT_HEADER_SIZE 4 +#define GATE_SET_OBJ_SIZE 144 +#define GATEID_OBJ_SIZE 8 +#define GATE_INFO_OBJ_SIZE 24 + +#define PKTCCOPS_SCOMMAND_GATE_ALLOC 1 +#define PKTCCOPS_SCOMMAND_GATE_ALLOC_ACK 2 +#define PKTCCOPS_SCOMMAND_GATE_ALLOC_ERR 3 +#define PKTCCOPS_SCOMMAND_GATE_SET 4 +#define PKTCCOPS_SCOMMAND_GATE_SET_ACK 5 +#define PKTCCOPS_SCOMMAND_GATE_SET_ERR 6 +#define PKTCCOPS_SCOMMAND_GATE_INFO 7 +#define PKTCCOPS_SCOMMAND_GATE_INFO_ACK 8 +#define PKTCCOPS_SCOMMAND_GATE_INFO_ERR 9 +#define PKTCCOPS_SCOMMAND_GATE_DELETE 10 +#define PKTCCOPS_SCOMMAND_GATE_DELETE_ACK 11 +#define PKTCCOPS_SCOMMAND_GATE_DELETE_ERR 12 +#define PKTCCOPS_SCOMMAND_GATE_OPEN 13 +#define PKTCCOPS_SCOMMAND_GATE_CLOSE 14 + +AST_MUTEX_DEFINE_STATIC(pktccops_lock); +static pthread_t pktccops_thread = AST_PTHREADT_NULL; +static uint16_t cops_trid = 0; + +struct pktcobj { + uint16_t length; + unsigned char cnum; + unsigned char ctype; + char *contents; + struct pktcobj *next; +}; + +struct copsmsg { + unsigned char verflag; + unsigned char opcode; + uint16_t clienttype; + uint32_t length; + struct pktcobj *object; + char *msg; /* != NULL if not packet cable message received */ +}; + +struct gatespec { + int direction; /* 0-DS, 1-US */ + int protocolid; + int flags; /* 0x00 */ + int sessionclass; /* normal voip: 0x01, high priority voip: 0x02, unspecified: 0x00 */ + uint32_t srcip; + uint32_t dstip; + uint16_t srcp; + uint16_t dstp; + int diffserv; + uint16_t t1; + uint16_t t7; + uint16_t t8; + uint32_t r; /* Token Bucket Rate */ + uint32_t b; /* token Bucket Size */ + uint32_t p; /* Peak Data Rate */ + uint32_t m; /* Minimum Policed Size*/ + uint32_t mm; /* Maximum Policed Size */ + uint32_t rate; + uint32_t s; /* Allowable Jitter*/ +}; + + +struct cops_cmts +{ + AST_LIST_ENTRY(cops_cmts) list; + char name[80]; + char host[80]; + char port[80]; + uint16_t t1; + uint16_t t7; + uint16_t t8; + uint32_t keepalive; + + uint32_t handle; + int state; + time_t contime; + time_t katimer; + int sfd; + int need_delete; +}; + +struct cops_ippool +{ + AST_LIST_ENTRY(cops_ippool) list; + uint32_t start; + uint32_t stop; + struct cops_cmts *cmts; +}; + +static uint16_t t1 = 250; +static uint16_t t7 = 200; +static uint16_t t8 = 300; +static uint32_t keepalive = 60; +static int pktccopsdebug = 0; +static int pktcreload = 0; +static int gateinfoperiod = 60; +static int gatetimeout = 150; + +AST_LIST_HEAD_STATIC(cmts_list, cops_cmts); +AST_LIST_HEAD_STATIC(ippool_list, cops_ippool); +AST_LIST_HEAD_STATIC(gate_list, cops_gate); + +static int pktccops_add_ippool(struct cops_ippool *ippool); +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); +static void pktccops_unregister_ippools(void); +static int load_pktccops_config(void); + +static uint32_t ftoieeef (float n) { + uint32_t res; + memcpy(&res, &n, 4); + return(htonl(res)); +} + +static uint16_t cops_constructgatespec(struct gatespec *gs, char *res) { + if(res == NULL) { + return 0; + } + + *res = (char) gs->direction; + *(res + 1) = (char) gs->protocolid; + *(res + 2) = (char) gs->flags; + *(res + 3) = (char) gs->sessionclass; + + *((uint32_t *) (res + 4)) = gs->srcip; + *((uint32_t *) (res + 8)) = gs->dstip; + + *((uint16_t *) (res + 12)) = gs->srcp; + *((uint16_t *) (res + 14)) = gs->dstp; + + *(res + 16) = (char) gs->diffserv; + *(res + 17) = 0; /* reserved */ + *(res + 18) = 0; /* reserved */ + *(res + 19) = 0; /* reserved */ + + *((uint16_t *) (res + 20)) = gs->t1; + *(res + 22) = 0; /* reserved */ + *(res + 23) = 0; /* reserved */ + + *((uint16_t *) (res + 24)) = gs->t7; + *((uint16_t *) (res + 26)) = gs->t8; + + *((uint32_t *) (res + 28)) = gs->r; + *((uint32_t *) (res + 32)) = gs->b; + *((uint32_t *) (res + 36)) = gs->p; + *((uint32_t *) (res + 40)) = gs->m; + *((uint32_t *) (res + 44)) = gs->mm; + *((uint32_t *) (res + 48)) = gs->rate; + *((uint32_t *) (res + 52)) = gs->s; + return 56; /* length */ +}; + +static uint16_t cops_construct_gate (int cmd, char *p, uint16_t trid, uint32_t mtahost, uint32_t actcount, float rate, uint32_t psizegateid, uint32_t ssip, uint16_t ssport, uint32_t gateid, struct cops_cmts *cmts) { + struct gatespec gs; + int offset = 0; + + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "CMD: %i\n", cmd); + + /* Transaction Identifier 8 octets */ + *(p + offset++) = 0; + *(p + offset++) = 8; /* length */ + *(p + offset++) = 1; /* snum */ + *(p + offset++) = 1; /* stype */ + *((uint16_t *) (p + offset)) = htons(trid); + offset += 2; + *(p + offset++) = 0; + *(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 */ + + /*Subscriper Identifier 8 octets */ + *(p + offset++) = 0; + *(p + offset++) = 8; /* length */ + *(p + offset++) = 2; /* snum */ + *(p + offset++) = 1; /* stype */ + *((uint32_t *) (p + offset)) = htonl(mtahost); + offset += 4; + + if (cmd == GATE_INFO || cmd == GATE_SET_HAVE_GATEID || cmd == GATE_DEL) { + /* Gate ID 8 Octets */ + *(p + offset++) = 0; + *(p + offset++) = 8; /* length */ + *(p + offset++) = 3; /* snum */ + *(p + offset++) = 1; /* stype */ + *((uint32_t *) (p + offset)) = htonl(gateid); + offset += 4; + if (cmd == GATE_INFO || cmd == GATE_DEL) { + return offset; + } + + } + + /* Activity Count 8 octets */ + *(p + offset++) = 0; + *(p + offset++) = 8; /* length */ + *(p + offset++) = 4; /* snum */ + *(p + offset++) = 1; /* stype */ + *((uint32_t *) (p + offset)) = htonl(actcount); + offset += 4; + + + /* Gate Spec 2*60 Octets */ + gs.direction = 0; /* DS */ + gs.protocolid = 17; /* UDP */ + gs.flags = 0; + gs.sessionclass = 1; + gs.srcip = htonl(ssip); + gs.dstip = htonl(mtahost); + gs.srcp = htons(ssport); + gs.dstp = 0; +/* gs.diffserv = 0xa0;*/ + gs.diffserv = 0; + gs.t1 = htons(cmts->t1); + gs.t7 = htons(cmts->t7); + gs.t8 = htons(cmts->t8); + gs.r = ftoieeef(rate); + gs.b = ftoieeef(psizegateid); + gs.p = ftoieeef(rate); + gs.m = htonl((uint32_t) psizegateid); + gs.mm = htonl((uint32_t) psizegateid); + gs.rate = ftoieeef(rate); + gs.s = htonl(800); + + + *(p + offset) = 0; + offset++; + *(p + offset) = 60; /* length */ + offset++; + *(p + offset) = 5; /* snum */ + offset++; + *(p + offset) = 1; /* stype */ + offset++; + offset += cops_constructgatespec(&gs, p + offset); + + + gs.direction = 1; /* US */ + gs.srcip = htonl(mtahost); + gs.dstip = htonl(ssip); + gs.srcp = 0; + gs.dstp = htons(ssport); + *(p + offset) = 0; + offset++; + *(p + offset) = 60; /* length */ + offset++; + *(p + offset) = 5; /* snum */ + offset++; + *(p + offset) = 1; /* stype */ + offset++; + offset += cops_constructgatespec(&gs, p + offset); + + + return(offset); +} + +static int cops_getmsg (int sfd, struct copsmsg *recmsg) { + int len, lent; + char buf[COPS_HEADER_SIZE]; + struct pktcobj *pobject = NULL; + recmsg->msg = NULL; + recmsg->object = NULL; + len = recv(sfd, buf, COPS_HEADER_SIZE, MSG_DONTWAIT); + if (len < COPS_HEADER_SIZE ) + return len; + recmsg->verflag = *buf; + recmsg->opcode = *(buf + 1); + recmsg->clienttype = ntohs(*((uint16_t *) (buf + 2))); + recmsg->length = ntohl(*((uint32_t *) (buf + 4))); + /* Eg KA msg*/ + if(recmsg->clienttype != 0x8008 ) { + recmsg->msg = malloc(recmsg->length - COPS_HEADER_SIZE); + if(!recmsg->msg) + return -1; + lent = recv(sfd, recmsg->msg, recmsg->length - COPS_HEADER_SIZE, MSG_DONTWAIT); + if(lent < recmsg->length - COPS_HEADER_SIZE) + return lent; + len += len; + } else { + /* PacketCable Objects */ + while(len < recmsg->length) { + if(len == COPS_HEADER_SIZE) { + /* 1st round */ + recmsg->object = malloc(sizeof(struct pktcobj)); + if(!recmsg->object) + return -1; + pobject = recmsg->object; + } else { + pobject->next = malloc(sizeof(struct pktcobj)); + if(!pobject->next) + return -1; + pobject = pobject->next; + } + pobject->next = NULL; + lent = recv(sfd, buf, COPS_OBJECT_HEADER_SIZE, MSG_DONTWAIT); + if(lent < COPS_OBJECT_HEADER_SIZE) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "Too short object header len: %i\n", lent); + return lent; + } + len += lent; + pobject->length = ntohs(*((uint16_t *) buf)); + pobject->cnum = *(buf + 2); + pobject->ctype = *(buf + 3); + pobject->contents = malloc(pobject->length - COPS_OBJECT_HEADER_SIZE); + if(!pobject->contents) + return -1; + lent = recv(sfd, pobject->contents, pobject->length - COPS_OBJECT_HEADER_SIZE, MSG_DONTWAIT); + if(lent < pobject->length - COPS_OBJECT_HEADER_SIZE) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "Too short object content len: %i\n", lent); + return lent; + } + len += lent; + } + } + return len; +}; + +static int cops_sendmsg (int sfd, struct copsmsg * sendmsg){ + char *buf; + int bufpos; + struct pktcobj *pobject; + + if(sfd < 0) + return -1; + + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: sending opcode: %i len: %i\n", sendmsg->opcode, sendmsg->length); + if (sendmsg->length < COPS_HEADER_SIZE) { + ast_log(LOG_WARNING, "COPS: invalid msg size!!!\n"); + return -1; + } + buf = malloc((size_t) sendmsg->length); + if(!buf) + return -1; + *buf = sendmsg->verflag ; + *(buf + 1) = sendmsg->opcode; + *((uint16_t *)(buf + 2)) = htons(sendmsg->clienttype); + *((uint32_t *)(buf + 4)) = htonl(sendmsg->length); + + if(sendmsg->msg != NULL) { + memcpy(buf + COPS_HEADER_SIZE, sendmsg->msg, sendmsg->length - COPS_HEADER_SIZE); + } else if(sendmsg->object != NULL) { + bufpos=8; + pobject = sendmsg->object; + while(pobject != NULL) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: Sending Object : cnum: %i ctype %i len: %i\n", pobject->cnum, pobject->ctype, pobject->length); + if(sendmsg->length < bufpos + pobject->length) { + ast_log(LOG_WARNING, "COPS: Invalid msg size len: %i objectlen: %i\n", sendmsg->length, pobject->length); + free(buf); + return -1; + } + *(uint16_t *) (buf + bufpos) = htons(pobject->length); + *(buf + bufpos + 2) = pobject->cnum; + *(buf + bufpos + 3) = pobject->ctype; + if(sendmsg->length < pobject->length + bufpos) { + ast_log(LOG_WARNING, "COPS: Error sum of object len more the msg len %i < %i\n", sendmsg->length, pobject->length + bufpos); + free(buf); + return -1; + } + memcpy((buf + bufpos + 4), pobject->contents, pobject->length - 4); + bufpos += pobject->length; + pobject = pobject->next; + } + } + + errno = 0; + if (send(sfd, buf, sendmsg->length, MSG_NOSIGNAL | MSG_DONTWAIT ) == -1) { + ast_log(LOG_WARNING, "COPS: Send failed errno=%i\n", errno); + free(buf); + return -2; + } + free(buf); + return 0; +} + +static void cops_freemsg (struct copsmsg *p) { + struct pktcobj *pnext; + free(p->msg); + p->msg = NULL; + while(p->object != NULL) { + pnext = p->object->next; + free(p->object->contents); + p->object->contents = NULL; + free(p->object); + p->object = pnext; + } + p->object = NULL; +} + +struct cops_gate * pktccops_gate_alloc(int cmd, struct cops_gate *gate, uint32_t mta, uint32_t actcount, float bitrate, uint32_t psize, uint32_t ssip, uint16_t ssport, int (* const got_dq_gi) (struct cops_gate *gate), int (* const gate_remove) (struct cops_gate *gate)) +{ + while(pktcreload) + usleep(10000); + + if (cmd == GATE_SET_HAVE_GATEID && gate) + { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "------- gate modify gateid 0x%x ssip: 0x%x\n", gate->gateid, ssip); + /* TODO implement it*/ + } + + if((gate = cops_gate_cmd(cmd, NULL, cops_trid++, mta, actcount, bitrate, psize, ssip, ssport, gate))) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: Allocating gate for mta: 0x%x\n", mta); + gate->got_dq_gi = got_dq_gi; + gate->gate_remove = gate_remove; + return(gate); + } else { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: Couldn't allocate gate for mta: 0x%x\n", mta); + return NULL; + } +} + +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) { + struct copsmsg *gateset; + struct cops_gate *new; + struct cops_ippool *ippool; + + + if(cmd == GATE_DEL) { + if(gate == NULL) { + return NULL; + } else { + cmts = gate->cmts; + } + } + + if(!cmts) { + AST_LIST_LOCK(&ippool_list); + AST_LIST_TRAVERSE(&ippool_list, ippool, list) { + if(mta >= ippool->start && mta <= ippool->stop) { + cmts = ippool->cmts; + break; + } + } + AST_LIST_UNLOCK(&ippool_list); + if(!cmts) { + ast_log(LOG_WARNING, "COPS: couldn't find cmts for mta: 0x%x\n", mta); + return NULL; + } + if(cmts->sfd < 0) { + ast_log(LOG_WARNING, "CMTS: %s not connected\n", cmts->name); + return NULL; + } + } + + if(cmd == GATE_SET) { + new = ast_calloc(1, sizeof(*new)); + new->gateid = 0; + new->trid = trid; + new->mta = mta; + new->state = GATE_ALLOC_PROGRESS; + new->checked = time(NULL); + new->allocated = time(NULL); + new->cmts = cmts; + new->got_dq_gi = NULL; + new->gate_remove = NULL; + new->gate_open = NULL; + new->tech_pvt = NULL; + new->deltimer = 0; + AST_LIST_LOCK(&gate_list); + AST_LIST_INSERT_HEAD(&gate_list, new, list); + AST_LIST_UNLOCK(&gate_list); + gate = new; + } else { + if(gate) + gate->trid = trid; + } + + gate->in_transaction = time(NULL); + + gateset = malloc(sizeof(struct copsmsg)); + if(!gateset) { + free(gateset); + return NULL; + } + gateset->msg = NULL; + gateset->verflag = 0x10; + gateset->opcode = 2; /* Decision */ + gateset->clienttype = 0x8008; /* =PacketCable */ + + /* Handle object */ + gateset->object = malloc(sizeof(struct pktcobj)); + if(!gateset->object) { + cops_freemsg(gateset); + free(gateset); + return NULL; + } + gateset->object->length = COPS_OBJECT_HEADER_SIZE + 4; + gateset->object->cnum = 1; /* Handle */ + gateset->object->ctype = 1; /* client */ + gateset->object->contents = malloc(4); + if(!gateset->object->contents) { + cops_freemsg(gateset); + free(gateset); + return NULL; + } + *((uint32_t *) gateset->object->contents) = htonl(cmts->handle); + + /* Context Object */ + gateset->object->next = malloc(sizeof(struct pktcobj)); + if(!gateset->object->next) { + cops_freemsg(gateset); + free(gateset); + return NULL; + } + gateset->object->next->length = COPS_OBJECT_HEADER_SIZE + 4; + gateset->object->next->cnum = 2; /* Context */ + gateset->object->next->ctype = 1; /* Context */ + gateset->object->next->contents = malloc(4); + if(!gateset->object->next->contents) { + cops_freemsg(gateset); + free(gateset); + return NULL; + } + *((uint32_t *) gateset->object->next->contents) = htonl(0x00080000); /* R-Type = 8 configuration request, M-Type = 0 */ + + /* Decision Object: Flags */ + gateset->object->next->next = malloc(sizeof(struct pktcobj)); + if(!gateset->object->next->next) { + cops_freemsg(gateset); + free(gateset); + return NULL; + } + gateset->object->next->next->length = COPS_OBJECT_HEADER_SIZE + 4; + gateset->object->next->next->cnum = 6; /* Decision */ + gateset->object->next->next->ctype = 1; /* Flags */ + gateset->object->next->next->contents = malloc(4); + if(!gateset->object->next->next->contents) { + cops_freemsg(gateset); + free(gateset); + return NULL; + } + *((uint32_t *) gateset->object->next->next->contents) = htonl(0x00010001); /* Install, Trigger Error */ + + /* Decision Object: Data */ + gateset->object->next->next->next = malloc(sizeof(struct pktcobj)); + if(!gateset->object->next->next->next) { + cops_freemsg(gateset); + free(gateset); + return NULL; + } + 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); + gateset->object->next->next->next->cnum = 6; /* Decision */ + gateset->object->next->next->next->ctype = 4; /* Decision Data */ + 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)); + if(!gateset->object->next->next->next->contents) { + cops_freemsg(gateset); + free(gateset); + return NULL; + } + gateset->object->next->next->next->next = NULL; + + gateset->length = COPS_HEADER_SIZE + gateset->object->length + gateset->object->next->length + gateset->object->next->next->length + gateset->object->next->next->next->length; + + if((cmd == GATE_INFO || cmd == GATE_SET_HAVE_GATEID || cmd == GATE_DEL) && gate) { + ast_log(LOG_DEBUG, "Construct gate with gateid: 0x%x\n", gate->gateid); + cops_construct_gate(cmd, gateset->object->next->next->next->contents, trid, mta, actcount, bitrate, psize, ssip, ssport, gate->gateid, cmts); + } else { + ast_log(LOG_DEBUG, "Construct new gate\n"); + cops_construct_gate(cmd, gateset->object->next->next->next->contents, trid, mta, actcount, bitrate, psize, ssip, ssport, 0, cmts); + } + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "send cmd\n"); + cops_sendmsg(cmts->sfd, gateset); + cops_freemsg(gateset); + free(gateset); + return gate; +} + +static int cops_connect(char *host, char *port) { + int s, sfd = -1, flags; + struct addrinfo hints; + struct addrinfo *rp; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = 0; + hints.ai_protocol = 0; + + s = getaddrinfo(host, port, &hints, &result); + if (s != 0) { + ast_log(LOG_WARNING, "COPS: getaddrinfo: %s\n", gai_strerror(s)); + return(-1); + } + + for (rp = result; rp != NULL; rp = rp->ai_next) { + sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if(sfd==-1) ast_log(LOG_WARNING, "Failed socket\n"); + flags = fcntl(sfd, F_GETFL); + fcntl(sfd, F_SETFL, flags | O_NONBLOCK); + connect(sfd, rp->ai_addr, rp->ai_addrlen); + if(sfd==-1) ast_log(LOG_WARNING, "Failed connect\n"); + } + freeaddrinfo(result); + + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "Connecting to cmts: %s:%s\n", host, port); + return(sfd); +} + +#ifdef CC_AST_HAS_VERSION_1_6 + +#define PKTCCOPS_DESTROY_CURRENT_GATE \ + AST_LIST_REMOVE_CURRENT(list); \ + if(gate->gate_remove) \ + gate->gate_remove(gate); \ + free(gate); + +#else + +#define PKTCCOPS_DESTROY_CURRENT_GATE \ + AST_LIST_REMOVE_CURRENT(&gate_list, list); \ + if(gate->gate_remove) \ + gate->gate_remove(gate); \ + free(gate); + +#endif + +static void *do_pktccops (void *data) { + int res, nfds, len; + struct copsmsg *recmsg, *sendmsg; + struct copsmsg recmsgb, sendmsgb; + fd_set rfds; + struct timeval tv; + struct pktcobj *pobject; + struct cops_cmts *cmts; + struct cops_gate *gate; + char *sobjp; + uint16_t snst, sobjlen, scommand, recvtrid, actcount, reason, subreason; + uint32_t gateid, subscrid, pktcerror; + time_t last_exec = 0; + + recmsg = &recmsgb; + sendmsg = &sendmsgb; + + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: thread started\n"); + + for(;;) { + tv.tv_sec = 1; + tv.tv_usec = 0; + FD_ZERO(&rfds); + nfds = 0; + AST_LIST_LOCK(&cmts_list); + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + if(last_exec != time(NULL)) { + if(cmts->state == 2 && cmts->katimer + cmts->keepalive < time(NULL)) { + ast_log(LOG_WARNING, "KA timer (%is) expired cmts: %s\n", cmts->keepalive, cmts->name); + cmts->state = 0; + cmts->katimer = -1; + close(cmts->sfd); + cmts->sfd = -1; + } + } + if(cmts->sfd > 0) { + FD_SET(cmts->sfd, &rfds); + if(cmts->sfd > nfds) nfds = cmts->sfd; + } else { + cmts->sfd = cops_connect(cmts->host, cmts->port); + if(cmts->sfd > 0) { + cmts->state = 1; + if(cmts->sfd > 0) { + FD_SET(cmts->sfd, &rfds); + if(cmts->sfd > nfds) nfds = cmts->sfd; + } + } + } + } + AST_LIST_UNLOCK(&cmts_list); + + if(last_exec != time(NULL)) { + last_exec = time(NULL); + AST_LIST_LOCK(&gate_list); + AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) /* { inside the macro */ + if(gate) { + if(gate->deltimer && gate->deltimer < time(NULL)) { + gate->deltimer = time(NULL) + 5; + gate->trid = cops_trid++; + cops_gate_cmd(GATE_DEL, gate->cmts, gate->trid, 0, 0, 0, 0, 0, 0, gate); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: requested Gate-Del: CMTS: %s gateid: 0x%x\n", (gate->cmts) ? gate->cmts->name : "null", gate->gateid); + } + if(time(NULL) - gate->checked > gatetimeout) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: remove from list GATE, CMTS: %s gateid: 0x%x\n", (gate->cmts) ? gate->cmts->name : "null", gate->gateid); + gate->state = GATE_TIMEOUT; + PKTCCOPS_DESTROY_CURRENT_GATE; + } else if(time(NULL) - gate->checked > gateinfoperiod && (gate->state == GATE_ALLOCATED || gate->state == GATE_OPEN)) { + if(gate->cmts && (!gate->in_transaction || ( gate->in_transaction + 5 ) < time(NULL))) { + gate->trid = cops_trid++; + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: Gate-Info send to CMTS: %s gateid: 0x%x\n", gate->cmts->name, gate->gateid); + cops_gate_cmd(GATE_INFO, gate->cmts, gate->trid, gate->mta, 0, 0, 0, 0, 0, gate); + } + } + } + } + AST_LIST_UNLOCK(&gate_list); + } + + if(pktcreload == 2) + pktcreload = 0; + if((res = select(nfds + 1, &rfds, NULL, NULL, &tv))) { + AST_LIST_LOCK(&cmts_list); + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + if(FD_ISSET(cmts->sfd, &rfds)) { + len = cops_getmsg(cmts->sfd, recmsg); + if(len > 0) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: got from %s:\n Header: versflag=0x%.2x opcode=%i clienttype=0x%.4x msglength=%i\n", + cmts->name, recmsg->verflag, recmsg->opcode, recmsg->clienttype, recmsg->length); + if(recmsg->object != NULL) { + pobject = recmsg->object; + while(pobject != NULL) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 " OBJECT: length=%i cnum=%i ctype=%i\n", pobject->length, pobject->cnum, pobject->ctype); + if(recmsg->opcode == 1 && pobject->cnum == 1 && pobject->ctype == 1 ) { + cmts->handle = ntohl(*((uint32_t *) pobject->contents)); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 " REQ client handle: %i\n", cmts->handle); + cmts->state = 2; + cmts->katimer=time(NULL); + } else if(pobject->cnum == 9 && pobject->ctype == 1) { + sobjp = pobject->contents; + subscrid = 0; + recvtrid = 0; + scommand = 0; + pktcerror = 0; + actcount = 0; + gateid = 0; + reason = 0; + subreason = 0; + while(sobjp < (pobject->contents + pobject->length - 4)) { + sobjlen = ntohs(*((uint16_t *) sobjp)); + snst = ntohs(*((uint16_t *) (sobjp + 2))); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 " S-Num S-type: 0x%.4x len: %i\n", snst, sobjlen); + if(snst == 0x0101 ) { + recvtrid = ntohs(*((uint16_t *) (sobjp + 4))); + scommand = ntohs(*((uint16_t *) (sobjp + 6))); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 " Transaction Identifier command: %i trid %i\n", scommand, recvtrid); + } else if(snst == 0x0201) { + subscrid = ntohl(*((uint32_t *) (sobjp + 4))); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 " Subscriber ID: 0x%.8x\n", subscrid); + } else if(snst == 0x0301) { + gateid = ntohl(*((uint32_t *) (sobjp + 4))); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 " Gate ID: 0x%x 0x%.8x\n", gateid, gateid); + } else if(snst == 0x0401) { + actcount = ntohs(*((uint16_t *) (sobjp + 6))); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 " Activity Count: %i\n", actcount); + } else if(snst == 0x0901) { + pktcerror = ntohl(*((uint32_t *) (sobjp + 4))); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 " PKTC Error: 0x%.8x\n", pktcerror); + } else if(snst == 0x0d01) { + reason = ntohs(*((uint16_t *) (sobjp + 4))); + subreason = ntohs(*((uint16_t *) (sobjp + 6))); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 " Reason: %u Subreason: %u\n", reason, subreason); + } + sobjp += sobjlen; + if(!sobjlen) + break; + } + if(scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE || scommand == PKTCCOPS_SCOMMAND_GATE_OPEN) { + AST_LIST_LOCK(&gate_list); + AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) /* { inside the macro */ + if(gate->cmts == cmts && gate->gateid == gateid) { + if(scommand == PKTCCOPS_SCOMMAND_GATE_CLOSE && gate->state != GATE_CLOSED && gate->state != GATE_CLOSED_ERR ) { + if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "COPS Gate Close Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name); + if (subreason) { + gate->state = GATE_CLOSED_ERR; + PKTCCOPS_DESTROY_CURRENT_GATE; + } else { + gate->state = GATE_CLOSED; + PKTCCOPS_DESTROY_CURRENT_GATE; + } + break; + } else if(scommand == PKTCCOPS_SCOMMAND_GATE_OPEN && gate->state == GATE_ALLOCATED) { + if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "COPS Gate Open Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name); + gate->state = GATE_OPEN; + if(gate->gate_open) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "Calling GATE-OPEN callback function\n"); + gate->gate_open(gate); + gate->gate_open = NULL; + } + break; + } + } + } + AST_LIST_UNLOCK(&gate_list); + } 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) { + AST_LIST_LOCK(&gate_list); + AST_LIST_TRAVERSE_SAFE_BEGIN(&gate_list, gate, list) /* { inside the macro */ + if(gate->cmts == cmts && gate->trid == recvtrid) { + gate->gateid = gateid; + gate->checked = time(NULL); + if(scommand == PKTCCOPS_SCOMMAND_GATE_SET_ACK) { + if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "COPS Gate Set Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name); + gate->state = GATE_ALLOCATED; + if(gate->got_dq_gi) { + gate->got_dq_gi(gate); + gate->got_dq_gi = NULL; + } + } else if(scommand == PKTCCOPS_SCOMMAND_GATE_SET_ERR) { + if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "COPS Gate Set Error TrId: %i ErrorCode: 0x%.8x CMTS: %s\n ", recvtrid, pktcerror, cmts->name); + gate->state = GATE_ALLOC_FAILED; + if(gate->got_dq_gi) { + gate->got_dq_gi(gate); + gate->got_dq_gi = NULL; + } + PKTCCOPS_DESTROY_CURRENT_GATE; + } else if(scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ACK) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS Gate Info Ack Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name); + } else if(scommand == PKTCCOPS_SCOMMAND_GATE_INFO_ERR) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS Gate Info Error Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name); + gate->state = GATE_ALLOC_FAILED; + PKTCCOPS_DESTROY_CURRENT_GATE; + } else if(scommand == PKTCCOPS_SCOMMAND_GATE_DELETE_ACK) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS Gate Deleted Gate ID: 0x%x TrId: %i CMTS: %s\n", gateid, recvtrid, cmts->name); + gate->state = GATE_DELETED; + PKTCCOPS_DESTROY_CURRENT_GATE; + } + gate->in_transaction = 0; + break; + } + } + AST_LIST_UNLOCK(&gate_list); + } + } + pobject = pobject->next; + } + } + + if (recmsg->opcode == 6 && recmsg->object && recmsg->object->cnum == 11 && recmsg->object->ctype == 1) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: Client open %s\n", cmts->name); + sendmsg->msg = NULL; + sendmsg->verflag = 0x10; + sendmsg->opcode = 7; /* Client Accept */ + sendmsg->clienttype = 0x8008; /* =PacketCable */ + sendmsg->length = COPS_HEADER_SIZE + COPS_OBJECT_HEADER_SIZE + 4; + sendmsg->object = malloc(sizeof(struct pktcobj)); + sendmsg->object->length = 4 + COPS_OBJECT_HEADER_SIZE; + sendmsg->object->cnum = 10; /* keppalive timer*/ + sendmsg->object->ctype = 1; + sendmsg->object->contents = malloc(sizeof(uint32_t)); + *((uint32_t *) sendmsg->object->contents) = htonl(cmts->keepalive & 0x0000ffff); + sendmsg->object->next = NULL; + cops_sendmsg(cmts->sfd, sendmsg); + cops_freemsg(sendmsg); + } else if (recmsg->opcode == 9) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: Keepalive Request got echoing back %s\n", cmts->name); + cops_sendmsg(cmts->sfd, recmsg); + cmts->state=2; + cmts->katimer=time(NULL); + } + } + if (len <= 0) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "COPS: lost connection to %s\n", cmts->name); + close(cmts->sfd); + cmts->sfd = -1; + cmts->state = 0; + } + cops_freemsg(recmsg); + } + } + AST_LIST_UNLOCK(&cmts_list); + } + if(pktcreload) { + if(option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "Reloading pktccops...\n"); + AST_LIST_LOCK(&gate_list); + AST_LIST_LOCK(&cmts_list); + pktccops_unregister_ippools(); + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + cmts->need_delete = 1; + } + load_pktccops_config(); + AST_LIST_TRAVERSE_SAFE_BEGIN(&cmts_list, cmts, list) /* { <- in the macro!!! */ + if(cmts && cmts->need_delete) { + AST_LIST_TRAVERSE(&gate_list, gate, list) { + if(gate->cmts == cmts) { + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "Null gate %s\n", gate->cmts->name); + gate->cmts = NULL; + } + gate->in_transaction = 0; + } + AST_LIST_UNLOCK(&gate_list); + if(pktccopsdebug) ast_verbose(VERBOSE_PREFIX_3 "removing cmts: %s\n", cmts->name); + if(cmts->sfd > 0) { + close(cmts->sfd); + } +#ifdef CC_AST_HAS_VERSION_1_6 + AST_LIST_REMOVE_CURRENT(list); +#else + AST_LIST_REMOVE_CURRENT(&cmts_list, list); +#endif + free(cmts); + } + } + AST_LIST_UNLOCK(&cmts_list); + AST_LIST_UNLOCK(&gate_list); + pktcreload = 2; + } + pthread_testcancel(); + } + return NULL; +} + +static int restart_pktc_thread(void) { + if (pktccops_thread == AST_PTHREADT_STOP) + return 0; + if (ast_mutex_lock(&pktccops_lock)) { + ast_log(LOG_WARNING, "Unable to lock pktccops\n"); + return -1; + } + if (pktccops_thread == pthread_self()) { + ast_mutex_unlock(&pktccops_lock); + ast_log(LOG_WARNING, "Cannot kill myself\n"); + return -1; + } + if (pktccops_thread != AST_PTHREADT_NULL) { + /* Wake up the thread */ + pthread_kill(pktccops_thread, SIGURG); + } else { + /* Start a new monitor */ + if (ast_pthread_create_background(&pktccops_thread, NULL, do_pktccops, NULL) < 0) { + ast_mutex_unlock(&pktccops_lock); + ast_log(LOG_ERROR, "Unable to start monitor thread.\n"); + return -1; + } + } + ast_mutex_unlock(&pktccops_lock); + return 0; +} + +static int load_pktccops_config(void) +{ + static char *cfg = "res_pktccops.conf"; + struct ast_config *config; + struct ast_variable *v; + struct cops_cmts *cmts; + struct cops_ippool *new_ippool; + const char *host, *cat, *port; + int sfd, update; + int res = 0; + uint16_t t1_temp, t7_temp, t8_temp; + uint32_t keepalive_temp; + unsigned int a,b,c,d,e,f,g,h; + +#ifdef CC_AST_HAS_VERSION_1_6 + struct ast_flags config_flags = {0}; + config = ast_config_load(cfg, config_flags); +#else + config = ast_config_load(cfg); +#endif + + if (!config) { + ast_log(LOG_WARNING, "Unable to load config file res_ptkccops.conf\n"); + return -1; + } + for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) { + if(!strcmp(cat, "general")) { + for (v = ast_variable_browse(config, cat); v; v = v->next) { + if (!strcasecmp(v->name, "t1")) { + t1 = atoi(v->value); + } else if (!strcasecmp(v->name, "t7")) { + t7 = atoi(v->value); + } else if (!strcasecmp(v->name, "t8")) { + t8 = atoi(v->value); + } else if (!strcasecmp(v->name, "keepalive")) { + keepalive = atoi(v->value); + } else if (!strcasecmp(v->name, "gateinfoperiod")) { + gateinfoperiod = atoi(v->value); + } else if (!strcasecmp(v->name, "gatetimeout")) { + gatetimeout = atoi(v->value); + } else { + ast_log(LOG_WARNING, "Unkown option %s in general section of res_ptkccops.conf\n", v->name); + } + } + } else { + /* Defaults */ + host = NULL; + port = NULL; + sfd = 0; + t1_temp = t1; + t7_temp = t7; + t8_temp = t8; + keepalive_temp = keepalive; + + for (v = ast_variable_browse(config, cat); v; v = v->next) { + if (!strcasecmp(v->name, "host")) { + host = v->value; + } else if (!strcasecmp(v->name, "port")) { + port = v->value; + } else if (!strcasecmp(v->name, "t1")) { + t1_temp = atoi(v->value); + } else if (!strcasecmp(v->name, "t7")) { + t7_temp = atoi(v->value); + } else if (!strcasecmp(v->name, "t8")) { + t8_temp = atoi(v->value); + } else if (!strcasecmp(v->name, "keepalive")) { + keepalive_temp = atoi(v->value); + } else if (!strcasecmp(v->name, "pool")) { + /* we weill parse it in 2nd round */ + } else { + ast_log(LOG_WARNING, "Unkown option %s in res_ptkccops.conf\n", v->name); + } + } + + update = 0; + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + if(!strcmp(cmts->name, cat)) { + update = 1; + break; + } + + } + if(!update) { + cmts = ast_calloc(1, sizeof(*cmts)); + if (!cmts) { + res = -1; + break; + } + AST_LIST_INSERT_HEAD(&cmts_list, cmts, list); + } + if (cat) + ast_copy_string(cmts->name, cat, sizeof(cmts->name)); + if (host) + ast_copy_string(cmts->host, host, sizeof(cmts->host)); + if (port) + ast_copy_string(cmts->port, port, sizeof(cmts->port)); + else + ast_copy_string(cmts->port, DEFAULT_COPS_PORT, sizeof(cmts->port)); + + cmts->t1 = t1_temp; + cmts->t7 = t7_temp; + cmts->t8 = t8_temp; + cmts->keepalive = keepalive_temp; + if(!update) { + cmts->state = 0; + cmts->sfd = -1; + } + cmts->need_delete = 0; + for (v = ast_variable_browse(config, cat); v; v = v->next) { + /* parse ipppol when we have cmts ptr */ + if (!strcasecmp(v->name, "pool")) { + if(sscanf(v->value, "%u.%u.%u.%u %u.%u.%u.%u", &a, &b, &c, &d, &e, &f, &g, &h) == 8) { + new_ippool = ast_calloc(1, sizeof(*new_ippool)); + if (!new_ippool) { + res = -1; + break; + } + new_ippool->start = a << 24 | b << 16 | c << 8 | d; + new_ippool->stop = e << 24 | f << 16 | g << 8 | h; + new_ippool->cmts = cmts; + pktccops_add_ippool(new_ippool); + } else { + ast_log(LOG_WARNING, "Invalid ip pool format in res_ptkccops.conf\n"); + } + } + } + } + } + ast_config_destroy(config); + return res; +} + +#ifdef CC_AST_HAS_VERSION_1_6 + +static char *pktccops_show_cmtses(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + struct cops_cmts *cmts; + char format[] = "%-16s %-24s %-12s %7s\n"; + char statedesc[16]; + int katimer; + + switch(cmd) { + case CLI_INIT: + e->command = "pktccops show cmtses"; + e->usage = + "Usage: pktccops show cmtses\n" + " List PacketCable COPS CMTSes.\n"; + + return NULL; + case CLI_GENERATE: + return NULL; + } + + ast_cli(a->fd, format, "Name ", "Host ", "Status ", "KA timer "); + ast_cli(a->fd, format, "------------", "--------------------", "----------", "-----------"); + AST_LIST_LOCK(&cmts_list); + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + katimer = -1; + if(cmts->state == 2) { + ast_copy_string(statedesc, "Connected", 16); + katimer = (int) (time(NULL) - cmts->katimer); + } else if(cmts->state == 1) { + ast_copy_string(statedesc, "Connecting", 16); + } else { + ast_copy_string(statedesc, "N/A", 16); + } + ast_cli(a->fd, "%-16s %-15s:%-8s %-12s %-7i\n", cmts->name, cmts->host, cmts->port, statedesc, katimer); + } + AST_LIST_UNLOCK(&cmts_list); + return CLI_SUCCESS; +} + +static char *pktccops_show_gates(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + struct cops_gate *gate; + char state_desc[16]; + + switch(cmd) { + case CLI_INIT: + e->command = "pktccops show gates"; + e->usage = + "Usage: pktccops show gates\n" + " List PacketCable COPS GATEs.\n"; + + return NULL; + case CLI_GENERATE: + return NULL; + } + + ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"CMTS", "Gate-Id","MTA", "Status", "AllocTime", "CheckTime"); + ast_cli(a->fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"--------------" ,"----------", "----------", "--------", "--------", "--------\n"); + AST_LIST_LOCK(&cmts_list); + AST_LIST_LOCK(&gate_list); + AST_LIST_TRAVERSE(&gate_list, gate, list) { + if(gate->state == GATE_ALLOC_FAILED) { + ast_copy_string(state_desc, "Failed", 16); + } else if(gate->state == GATE_ALLOC_PROGRESS) { + ast_copy_string(state_desc, "In Progress", 16); + } else if(gate->state == GATE_ALLOCATED) { + ast_copy_string(state_desc, "Allocated", 16); + } else if(gate->state == GATE_CLOSED) { + ast_copy_string(state_desc, "Closed", 16); + } else if(gate->state == GATE_CLOSED_ERR) { + ast_copy_string(state_desc, "ClosedErr", 16); + } else if(gate->state == GATE_OPEN) { + ast_copy_string(state_desc, "Open", 16); + } else if(gate->state == GATE_DELETED) { + ast_copy_string(state_desc, "Deleted", 16); + } else { + ast_copy_string(state_desc, "N/A", 16); + } + + ast_cli(a->fd, "%-16s 0x%.8x 0x%08x %-10s %10i %10i %u\n", (gate->cmts) ? gate->cmts->name : "null" , gate->gateid, gate->mta, + state_desc, (int) (time(NULL) - gate->allocated), (gate->checked) ? (int) (time(NULL) - gate->checked) : 0, (unsigned int) gate->in_transaction); + } + AST_LIST_UNLOCK(&cmts_list); + AST_LIST_UNLOCK(&gate_list); + return CLI_SUCCESS; +} + +static char *pktccops_show_pools(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + struct cops_ippool *ippool; + char format[] = "%-16s %-18s %-7s\n"; + char start[32]; + char stop[32]; + + switch(cmd) { + case CLI_INIT: + e->command = "pktccops show pools"; + e->usage = + "Usage: pktccops show pools\n" + " List PacketCable COPS ip pools of MTAs.\n"; + + return NULL; + case CLI_GENERATE: + return NULL; + } + + ast_cli(a->fd, format, "Start ", "Stop ", "CMTS "); + ast_cli(a->fd, format, "----------", "----------", "--------"); + AST_LIST_LOCK(&ippool_list); + AST_LIST_TRAVERSE(&ippool_list, ippool, list) { + sprintf(start, "%u.%u.%u.%u", ippool->start >> 24, (ippool->start >> 16) & 0x000000ff, (ippool->start >> 8) & 0x000000ff, ippool->start & 0x000000ff); + + sprintf(stop, "%u.%u.%u.%u", ippool->stop >> 24, (ippool->stop >> 16) & 0x000000ff, (ippool->stop >> 8) & 0x000000ff, ippool->stop & 0x000000ff); + ast_cli(a->fd, "%-16s %-18s %-16s\n", start, stop, ippool->cmts->name); + } + AST_LIST_UNLOCK(&ippool_list); + return CLI_SUCCESS; +} + +static char *pktccops_gatedel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + int found = 0; + int trid; + uint32_t gateid; + struct cops_gate *gate; + struct cops_cmts *cmts; + + switch (cmd) { + case CLI_INIT: + e->command = "pktccops gatedel"; + e->usage = + "Usage: pktccops gatedel \n" + " Send Gate-Del to cmts.\n"; + return NULL; + case CLI_GENERATE: + return NULL; + } + + if (a->argc < 4) + return CLI_SHOWUSAGE; + + AST_LIST_LOCK(&cmts_list); + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + if(!strcmp(cmts->name, a->argv[2])) { + ast_cli(a->fd, "Found cmts: %s\n", cmts->name); + found = 1; + break; + } + } + AST_LIST_UNLOCK(&cmts_list); + + if(!found) + return CLI_SHOWUSAGE; + + trid = cops_trid++; + if(!sscanf(a->argv[3], "%x", &gateid)) { + ast_cli(a->fd, "bad gate specification (%s)\n", a->argv[3]); + return CLI_SHOWUSAGE; + } + + found = 0; + AST_LIST_LOCK(&gate_list); + AST_LIST_TRAVERSE(&gate_list, gate, list) { + if(gate->gateid == gateid && gate->cmts == cmts) { + found = 1; + break; + } + } + + if(!found) { + ast_cli(a->fd, "gate not found: %s\n", a->argv[3]); + return CLI_SHOWUSAGE; + } + + AST_LIST_UNLOCK(&gate_list); + cops_gate_cmd(GATE_DEL, cmts, trid, 0, 0, 0, 0, 0, 0, gate); + return CLI_SUCCESS; +} + +static char *pktccops_gateset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + int foundcmts = 0; + int trid; + unsigned int an,bn,cn,dn; + uint32_t mta, ssip; + struct cops_cmts *cmts; + struct cops_gate *gate; + + switch (cmd) { + case CLI_INIT: + e->command = "pktccops gateset"; + e->usage = + "Usage: pktccops gateset \n" + " Send Gate-Set to cmts.\n"; + return NULL; + case CLI_GENERATE: + return NULL; + } + + if (a->argc < 9) + return CLI_SHOWUSAGE; + + if(!strncmp(a->argv[2], "null", sizeof(a->argv[2]))) { + cmts = NULL; + } else { + AST_LIST_LOCK(&cmts_list); + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + if(!strcmp(cmts->name, a->argv[2])) { + ast_cli(a->fd, "Found cmts: %s\n", cmts->name); + foundcmts = 1; + break; + } + } + AST_LIST_UNLOCK(&cmts_list); + if(!foundcmts) { + ast_cli(a->fd, "CMTS not found: %s\n", a->argv[2]); + return CLI_SHOWUSAGE; + } + } + + trid = cops_trid++; + if(sscanf(a->argv[3], "%u.%u.%u.%u", &an, &bn, &cn, &dn) != 4) { + ast_cli(a->fd, "mta specification (%s) not looks like as an ipaddr\n", a->argv[3]); + return CLI_SHOWUSAGE; + } + mta = an << 24 | bn << 16 | cn << 8 | dn; + + if(sscanf(a->argv[7], "%u.%u.%u.%u", &an, &bn, &cn, &dn) != 4) { + ast_cli(a->fd, "ssip specification (%s) not looks like as an ipaddr\n", a->argv[7]); + return CLI_SHOWUSAGE; + } + ssip = an << 24 | bn << 16 | cn << 8 | dn; + + 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); + return CLI_SUCCESS; +} + +static char *pktccops_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + switch (cmd) { + case CLI_INIT: + e->command = "pktccops set debug {on|off}"; + e->usage = + "Usage: pktccops set debug {on|off}\n" + " Turn on/off debuging\n"; + return NULL; + case CLI_GENERATE: + return NULL; + } + + if (a->argc != e->args) + return CLI_SHOWUSAGE; + if (!strncasecmp(a->argv[e->args - 1], "on", 2)) { + pktccopsdebug = 1; + ast_cli(a->fd, "PktcCOPS Debugging Enabled\n"); + } else if (!strncasecmp(a->argv[e->args - 1], "off", 2)) { + pktccopsdebug = 0; + ast_cli(a->fd, "PktcCOPS Debugging Disabled\n"); + } else { + return CLI_SHOWUSAGE; + } + return CLI_SUCCESS; + +} + +static struct ast_cli_entry cli_pktccops[] = { + AST_CLI_DEFINE(pktccops_show_cmtses, "List PacketCable COPS CMTSes"), + AST_CLI_DEFINE(pktccops_show_gates, "List PacketCable COPS GATEs"), + AST_CLI_DEFINE(pktccops_show_pools, "List PacketCable MTA pools"), + AST_CLI_DEFINE(pktccops_gateset, "Send Gate-Set to cmts"), + AST_CLI_DEFINE(pktccops_gatedel, "Send Gate-Det to cmts"), + AST_CLI_DEFINE(pktccops_debug, "Enable/Disable COPS debugging") +}; + +#else + +static int pktccops_show_cmtses_command(int fd, int argc, char **argv) +{ + struct cops_cmts *cmts; + char format[] = "%-16s %-24s %-12s %7s\n"; + char statedesc[16]; + int katimer; + ast_cli(fd, format, "Name ", "Host ", "Status ", "KA timer "); + ast_cli(fd, format, "------------", "--------------------", "----------", "-----------"); + AST_LIST_LOCK(&cmts_list); + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + katimer = -1; + if(cmts->state == 2) { + ast_copy_string(statedesc, "Connected", 16); + katimer = (int) (time(NULL) - cmts->katimer); + } else if(cmts->state == 1) { + ast_copy_string(statedesc, "Connecting", 16); + } else { + ast_copy_string(statedesc, "N/A", 16); + } + ast_cli(fd, "%-16s %-15s:%-8s %-12s %-7i\n", cmts->name, cmts->host, cmts->port, statedesc, katimer); + } + AST_LIST_UNLOCK(&cmts_list); + return 0; +} + +static int pktccops_show_gates_command(int fd, int argc, char **argv) +{ + struct cops_gate *gate; + char state_desc[16]; + ast_cli(fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"CMTS", "Gate-Id","MTA", "Status", "AllocTime", "CheckTime"); + ast_cli(fd, "%-16s %-12s %-12s %-10s %-10s %-10s\n" ,"--------------" ,"----------", "----------", "--------", "--------", "--------\n"); + AST_LIST_LOCK(&cmts_list); + AST_LIST_LOCK(&gate_list); + AST_LIST_TRAVERSE(&gate_list, gate, list) { + if(gate->state == GATE_ALLOC_FAILED) { + ast_copy_string(state_desc, "Failed", 16); + } else if(gate->state == GATE_ALLOC_PROGRESS) { + ast_copy_string(state_desc, "In Progress", 16); + } else if(gate->state == GATE_ALLOCATED) { + ast_copy_string(state_desc, "Allocated", 16); + } else if(gate->state == GATE_CLOSED) { + ast_copy_string(state_desc, "Closed", 16); + } else if(gate->state == GATE_CLOSED_ERR) { + ast_copy_string(state_desc, "ClosedErr", 16); + } else if(gate->state == GATE_OPEN) { + ast_copy_string(state_desc, "Open", 16); + } else if(gate->state == GATE_DELETED) { + ast_copy_string(state_desc, "Deleted", 16); + } else { + ast_copy_string(state_desc, "N/A", 16); + } + + ast_cli(fd, "%-16s 0x%.8x 0x%08x %-10s %10i %10i %u\n", (gate->cmts) ? gate->cmts->name : "null" , gate->gateid, gate->mta, + state_desc, (int) (time(NULL) - gate->allocated), (gate->checked) ? (int) (time(NULL) - gate->checked) : 0, (unsigned int) gate->in_transaction); + } + AST_LIST_UNLOCK(&cmts_list); + AST_LIST_UNLOCK(&gate_list); + return 0; +} + +static int pktccops_show_pools_command(int fd, int argc, char **argv) +{ + struct cops_ippool *ippool; + char format[] = "%-16s %-18s %-7s\n"; + char start[32]; + char stop[32]; + + ast_cli(fd, format, "Start ", "Stop ", "CMTS "); + ast_cli(fd, format, "----------", "----------", "--------"); + AST_LIST_LOCK(&ippool_list); + AST_LIST_TRAVERSE(&ippool_list, ippool, list) { + sprintf(start, "%u.%u.%u.%u", ippool->start >> 24, (ippool->start >> 16) & 0x000000ff, (ippool->start >> 8) & 0x000000ff, ippool->start & 0x000000ff); + + sprintf(stop, "%u.%u.%u.%u", ippool->stop >> 24, (ippool->stop >> 16) & 0x000000ff, (ippool->stop >> 8) & 0x000000ff, ippool->stop & 0x000000ff); + ast_cli(fd, "%-16s %-18s %-16s\n", start, stop, ippool->cmts->name); + } + AST_LIST_UNLOCK(&ippool_list); + return 0; +} + +static int pktccops_gatedel_command(int fd, int argc, char **argv) +{ + int found = 0; + int trid; + uint32_t gateid; + struct cops_gate *gate; + struct cops_cmts *cmts; + + if (argc < 4) + return RESULT_SHOWUSAGE; + + AST_LIST_LOCK(&cmts_list); + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + if(!strcmp(cmts->name, argv[2])) { + ast_cli(fd, "Found cmts: %s\n", cmts->name); + found = 1; + break; + } + } + AST_LIST_UNLOCK(&cmts_list); + + if(!found) + return RESULT_SHOWUSAGE; + + trid = cops_trid++; + if(!sscanf(argv[3], "%x", &gateid)) { + ast_cli(fd, "bad gate specification (%s)\n", argv[3]); + return RESULT_SHOWUSAGE; + } + + found = 0; + AST_LIST_LOCK(&gate_list); + AST_LIST_TRAVERSE(&gate_list, gate, list) { + if(gate->gateid == gateid && gate->cmts == cmts) { + found = 1; + break; + } + } + + if(!found) { + ast_cli(fd, "gate not found: %s\n", argv[3]); + return RESULT_SHOWUSAGE; + } + + AST_LIST_UNLOCK(&gate_list); + cops_gate_cmd(GATE_DEL, cmts, trid, 0, 0, 0, 0, 0, 0, gate); + return 0; +} + +static int pktccops_gateset_command(int fd, int argc, char **argv) +{ + int foundcmts = 0; + int trid; + unsigned int a,b,c,d; + uint32_t mta, ssip; + struct cops_cmts *cmts; + struct cops_gate *gate; + + if (argc < 9) + return RESULT_SHOWUSAGE; + + + if(!strncmp(argv[2], "null", sizeof(argv[2]))) { + cmts = NULL; + } else { + AST_LIST_LOCK(&cmts_list); + AST_LIST_TRAVERSE(&cmts_list, cmts, list) { + if(!strcmp(cmts->name, argv[2])) { + ast_cli(fd, "Found cmts: %s\n", cmts->name); + foundcmts = 1; + break; + } + } + AST_LIST_UNLOCK(&cmts_list); + if(!foundcmts) { + ast_cli(fd, "CMTS not found: %s\n", argv[2]); + return RESULT_SHOWUSAGE; + } + } + + trid = cops_trid++; + if(sscanf(argv[3], "%u.%u.%u.%u", &a, &b, &c, &d) != 4) { + ast_cli(fd, "mta specification (%s) not looks like as an ipaddr\n", argv[3]); + return RESULT_SHOWUSAGE; + } + mta = a << 24 | b << 16 | c << 8 | d; + + if(sscanf(argv[7], "%u.%u.%u.%u", &a, &b, &c, &d) != 4) { + ast_cli(fd, "ssip specification (%s) not looks like as an ipaddr\n", argv[7]); + return RESULT_SHOWUSAGE; + } + ssip = a << 24 | b << 16 | c << 8 | d; + + gate = cops_gate_cmd(GATE_SET, cmts, trid, mta, atoi(argv[4]), atof(argv[5]), atoi(argv[6]), ssip, atoi(argv[8]), NULL); + return 0; +} + +static int pktccops_debug_command(int fd, int argc, char *argv[]) +{ + if (argc != 3) + return RESULT_SHOWUSAGE; + pktccopsdebug = 1; + ast_cli(fd, "PktcCOPS Debugging Enabled\n"); + return RESULT_SUCCESS; +} + +static int pktccops_no_debug_command(int fd, int argc, char *argv[]) +{ + if (argc != 4) + return RESULT_SHOWUSAGE; + pktccopsdebug = 0; + ast_cli(fd, "PktcCOPS Debugging Disabled\n"); + return RESULT_SUCCESS; +} + + +static char pktccops_showcmtses_usage[] = +"Usage: pktccops show cmtses\n" +" List PacketCable COPS CMTSes.\n"; + +static char pktccops_showgates_usage[] = +"Usage: pktccops show gates\n" +" List PacketCable GATES.\n"; + +static char pktccops_showpools_usage[] = +"Usage: pktccops show cmtses\n" +" List PacketCable COPS CMTSes.\n"; + +static char pktccops_gateset_usage[] = +"Usage: pktccops gateset \n" +" Send Gate-Set to cmts.\n"; + +static char pktccops_gatedel_usage[] = +"Usage: pktccops gatedel \n" +" Send Gate-Del to cmts.\n"; + +static char pktccops_debug_usage[] = +"Usage: pktccops set debug\n" +" Turn on debuging\n"; + +static char pktccops_no_debug_usage[] = +"Usage: pktccops set debug off\n" +" Turn off debuging\n"; + +static struct ast_cli_entry cli_pktccops[] = { + { { "pktccops", "show", "cmtses", NULL }, + pktccops_show_cmtses_command, "List PacketCable COPS CMTSes", + pktccops_showcmtses_usage }, + + { { "pktccops", "show", "gates", NULL }, + pktccops_show_gates_command, "List PacketCable COPS GATEs", + pktccops_showgates_usage }, + + { { "pktccops", "show", "pools", NULL }, + pktccops_show_pools_command, "List PacketCable MTA pools", + pktccops_showpools_usage }, + + { { "pktccops", "gateset", NULL }, + pktccops_gateset_command, "Send Gate-Set to cmts", + pktccops_gateset_usage }, + + { { "pktccops", "gatedel", NULL }, + pktccops_gatedel_command, "Send Gate-Del to cmts", + pktccops_gatedel_usage }, + + { { "pktccops", "set", "debug", NULL }, + pktccops_debug_command, "Turn on debuging", + pktccops_debug_usage }, + + { { "pktccops", "set", "debug", "off", NULL }, + pktccops_no_debug_command, "turn off debuging", + pktccops_no_debug_usage }, +}; + +#endif + +static int pktccops_add_ippool(struct cops_ippool *ippool) +{ + if (ippool) { + AST_LIST_LOCK(&ippool_list); + AST_LIST_INSERT_HEAD(&ippool_list, ippool, list); + AST_LIST_UNLOCK(&ippool_list); + return 0; + } else { + ast_log(LOG_WARNING, "Attempted to register NULL ippool?\n"); + return -1; + } +} + +static void pktccops_unregister_cmtses(void) +{ + struct cops_cmts *cmts; + struct cops_gate *gate; + AST_LIST_LOCK(&cmts_list); + while ((cmts = AST_LIST_REMOVE_HEAD(&cmts_list, list))) { + if(cmts->sfd > 0) + close(cmts->sfd); + free(cmts); + } + AST_LIST_UNLOCK(&cmts_list); + + AST_LIST_LOCK(&gate_list); + while ((gate = AST_LIST_REMOVE_HEAD(&gate_list, list))) { + free(gate); + } + AST_LIST_UNLOCK(&gate_list); +} + +static void pktccops_unregister_ippools(void) +{ + struct cops_ippool *ippool; + AST_LIST_LOCK(&ippool_list); + while ((ippool = AST_LIST_REMOVE_HEAD(&ippool_list, list))) { + free(ippool); + } + AST_LIST_UNLOCK(&ippool_list); +} + +static int load_module(void) +{ + int res; + AST_LIST_LOCK(&cmts_list); + res = load_pktccops_config(); + AST_LIST_UNLOCK(&cmts_list); + if(res == -1) + return AST_MODULE_LOAD_DECLINE; + ast_cli_register_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry)); + restart_pktc_thread(); + return 0; +} + +static int unload_module(void) +{ + if (!ast_mutex_lock(&pktccops_lock)) { + if (pktccops_thread && (pktccops_thread != AST_PTHREADT_STOP)) { + pthread_cancel(pktccops_thread); + pthread_kill(pktccops_thread, SIGURG); + pthread_join(pktccops_thread, NULL); + } + pktccops_thread = AST_PTHREADT_STOP; + ast_mutex_unlock(&pktccops_lock); + } else { + ast_log(LOG_WARNING, "Unable to lock the pktccops_thread\n"); + return -1; + } + + ast_cli_unregister_multiple(cli_pktccops, sizeof(cli_pktccops) / sizeof(struct ast_cli_entry)); + pktccops_unregister_cmtses(); + pktccops_unregister_ippools(); + pktccops_thread = AST_PTHREADT_NULL; + return 0; +} + +static int reload_module(void) +{ + /* Prohibit unloading */ + if(pktcreload) { + ast_log(LOG_NOTICE, "Previous reload in progress, please wait!\n"); + return -1; + } + pktcreload = 1; + return 0; +} + +AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "PktcCOPS manager", + .load = load_module, + .unload = unload_module, + .reload = reload_module, + ); Index: res/res_pktccops.exports =================================================================== --- res/res_pktccops.exports (revisión: 0) +++ res/res_pktccops.exports (revisión: 0) @@ -0,0 +1,6 @@ +{ + global: + pktccops_gate_alloc; + local: + *; +};