Index: channels/chan_mgcp.c =================================================================== --- channels/chan_mgcp.c (revisiĆ³n: 206632) +++ 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: configs/mgcp.conf.sample =================================================================== --- configs/mgcp.conf.sample (revisiĆ³n: 206632) +++ 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