2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Implementation of Media Gateway Control Protocol
23 * \author Mark Spencer <markster@digium.com>
26 * \arg \ref Config_mgcp
27 * \arg \ref res_pktccops
29 * \ingroup channel_drivers
33 <depend>res_pktccops</depend>
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
45 #include <sys/signal.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <arpa/inet.h>
53 #include "asterisk/lock.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/config.h"
56 #include "asterisk/module.h"
57 #include "asterisk/pbx.h"
58 #include "asterisk/sched.h"
59 #include "asterisk/io.h"
60 #include "asterisk/rtp_engine.h"
61 #include "asterisk/acl.h"
62 #include "asterisk/callerid.h"
63 #include "asterisk/cli.h"
64 #include "asterisk/say.h"
65 #include "asterisk/cdr.h"
66 #include "asterisk/astdb.h"
67 #include "asterisk/features.h"
68 #include "asterisk/app.h"
69 #include "asterisk/musiconhold.h"
70 #include "asterisk/utils.h"
71 #include "asterisk/netsock.h"
72 #include "asterisk/causes.h"
73 #include "asterisk/dsp.h"
74 #include "asterisk/devicestate.h"
75 #include "asterisk/stringfields.h"
76 #include "asterisk/abstract_jb.h"
77 #include "asterisk/event.h"
78 #include "asterisk/chanvars.h"
79 #include "asterisk/pktccops.h"
82 * Define to work around buggy dlink MGCP phone firmware which
83 * appears not to know that "rt" is part of the "G" package.
85 /* #define DLINK_BUGGY_FIRMWARE */
88 #define DEFAULT_EXPIRY 120
89 #define MAX_EXPIRY 3600
93 #define INADDR_NONE (in_addr_t)(-1)
96 /*! Global jitterbuffer configuration - by default, jb is disabled */
97 static struct ast_jb_conf default_jbconf =
101 .resync_threshold = -1,
105 static struct ast_jb_conf global_jbconf;
107 static const char tdesc[] = "Media Gateway Control Protocol (MGCP)";
108 static const char config[] = "mgcp.conf";
110 #define MGCP_DTMF_RFC2833 (1 << 0)
111 #define MGCP_DTMF_INBAND (1 << 1)
112 #define MGCP_DTMF_HYBRID (1 << 2)
114 #define DEFAULT_MGCP_GW_PORT 2427 /*!< From RFC 2705 */
115 #define DEFAULT_MGCP_CA_PORT 2727 /*!< From RFC 2705 */
116 #define MGCP_MAX_PACKET 1500 /*!< Also from RFC 2543, should sub headers tho */
117 #define DEFAULT_RETRANS 1000 /*!< How frequently to retransmit */
118 #define MAX_RETRANS 5 /*!< Try only 5 times for retransmissions */
120 /*! MGCP rtp stream modes { */
121 #define MGCP_CX_SENDONLY 0
122 #define MGCP_CX_RECVONLY 1
123 #define MGCP_CX_SENDRECV 2
124 #define MGCP_CX_CONF 3
125 #define MGCP_CX_CONFERENCE 3
126 #define MGCP_CX_MUTE 4
127 #define MGCP_CX_INACTIVE 4
130 static const char * const mgcp_cxmodes[] = {
150 static char context[AST_MAX_EXTENSION] = "default";
152 static char language[MAX_LANGUAGE] = "";
153 static char musicclass[MAX_MUSICCLASS] = "";
154 static char parkinglot[AST_MAX_CONTEXT];
155 static char cid_num[AST_MAX_EXTENSION] = "";
156 static char cid_name[AST_MAX_EXTENSION] = "";
158 static int dtmfmode = 0;
161 static int pktcgatealloc = 0;
162 static int hangupongateremove = 0;
164 static ast_group_t cur_callergroup = 0;
165 static ast_group_t cur_pickupgroup = 0;
169 unsigned int tos_audio;
171 unsigned int cos_audio;
172 } qos = { 0, 0, 0, 0 };
174 static int immediate = 0;
176 static int callwaiting = 0;
178 static int callreturn = 0;
180 static int slowsequence = 0;
182 static int threewaycalling = 0;
184 /*! This is for flashhook transfers */
185 static int transfer = 0;
187 static int cancallforward = 0;
189 static int singlepath = 0;
191 static int directmedia = DIRECTMEDIA;
193 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
195 static char mailbox[AST_MAX_EXTENSION];
197 static int amaflags = 0;
201 static unsigned int oseq;
203 /*! Wait up to 16 seconds for first digit (FXO logic) */
204 static int firstdigittimeout = 16000;
206 /*! How long to wait for following digits (FXO logic) */
207 static int gendigittimeout = 8000;
209 /*! How long to wait for an extra digit, if there is an ambiguous match */
210 static int matchdigittimeout = 3000;
212 /*! Protect the monitoring thread, so only one process can kill or start it, and not
213 when it's doing something critical. */
214 AST_MUTEX_DEFINE_STATIC(netlock);
216 AST_MUTEX_DEFINE_STATIC(monlock);
218 /*! This is the thread for the monitor which checks for input on the channels
219 * which are not currently in use.
221 static pthread_t monitor_thread = AST_PTHREADT_NULL;
223 static int restart_monitor(void);
225 static format_t capability = AST_FORMAT_ULAW;
226 static int nonCodecCapability = AST_RTP_DTMF;
228 static char ourhost[MAXHOSTNAMELEN];
229 static struct in_addr __ourip;
232 static int mgcpdebug = 0;
234 static struct sched_context *sched;
235 static struct io_context *io;
236 /*! The private structures of the mgcp channels are linked for
237 * selecting outgoing channels
240 #define MGCP_MAX_HEADERS 64
241 #define MGCP_MAX_LINES 64
243 struct mgcp_request {
249 int headers; /*!< MGCP Headers */
250 char *header[MGCP_MAX_HEADERS];
251 int lines; /*!< SDP Content */
252 char *line[MGCP_MAX_LINES];
253 char data[MGCP_MAX_PACKET];
254 int cmd; /*!< int version of verb = command */
255 unsigned int trid; /*!< int version of identifier = transaction id */
256 struct mgcp_request *next; /*!< next in the queue */
259 /*! \brief mgcp_message: MGCP message for queuing up */
260 struct mgcp_message {
261 struct mgcp_endpoint *owner_ep;
262 struct mgcp_subchannel *owner_sub;
264 unsigned long expire;
267 struct mgcp_message *next;
271 #define RESPONSE_TIMEOUT 30 /*!< in seconds */
273 struct mgcp_response {
277 struct mgcp_response *next;
286 struct mgcp_subchannel {
287 /*! subchannel magic string.
288 Needed to prove that any subchannel pointer passed by asterisk
289 really points to a valid subchannel memory area.
290 Ugly.. But serves the purpose for the time being.
292 #define MGCP_SUBCHANNEL_MAGIC "!978!"
296 struct ast_channel *owner;
297 struct mgcp_endpoint *parent;
298 struct ast_rtp_instance *rtp;
299 struct sockaddr_in tmpdest;
300 char txident[80]; /*! \todo FIXME txident is replaced by rqnt_ident in endpoint.
301 This should be obsoleted */
305 struct mgcp_request *cx_queue; /*!< pending CX commands */
306 ast_mutex_t cx_queue_lock; /*!< CX queue lock */
308 int iseq; /*!< Not used? RTP? */
312 struct cops_gate *gate;
313 struct mgcp_subchannel *next; /*!< for out circular linked list */
316 #define MGCP_ONHOOK 1
317 #define MGCP_OFFHOOK 2
322 struct mgcp_endpoint {
325 struct mgcp_subchannel *sub; /*!< Pointer to our current connection, channel and stuff */
326 char accountcode[AST_MAX_ACCOUNT_CODE];
327 char exten[AST_MAX_EXTENSION]; /*!< Extention where to start */
328 char context[AST_MAX_EXTENSION];
329 char language[MAX_LANGUAGE];
330 char cid_num[AST_MAX_EXTENSION]; /*!< Caller*ID number */
331 char cid_name[AST_MAX_EXTENSION]; /*!< Caller*ID name */
332 char lastcallerid[AST_MAX_EXTENSION]; /*!< Last Caller*ID */
333 char dtmf_buf[AST_MAX_EXTENSION]; /*!< place to collect digits be */
334 char call_forward[AST_MAX_EXTENSION]; /*!< Last Caller*ID */
335 char musicclass[MAX_MUSICCLASS];
336 char curtone[80]; /*!< Current tone */
337 char mailbox[AST_MAX_EXTENSION];
338 char parkinglot[AST_MAX_CONTEXT]; /*!< Parkinglot */
339 struct ast_event_sub *mwi_event_sub;
340 ast_group_t callgroup;
341 ast_group_t pickupgroup;
350 int dnd; /* How does this affect callwait? Do we just deny a mgcp_request if we're dnd? */
357 int hangupongateremove;
359 int slowsequence; /*!< MS: Sequence the endpoint as a whole */
361 int iseq; /*!< Not used? */
362 int lastout; /*!< tracking this on the subchannels. Is it needed here? */
363 int needdestroy; /*!< Not used? */
365 int nonCodecCapability;
367 int msgstate; /*!< voicemail message state */
371 char rqnt_ident[80]; /*!< request identifier */
372 struct mgcp_request *rqnt_queue; /*!< pending RQNT commands */
373 ast_mutex_t rqnt_queue_lock;
374 struct mgcp_request *cmd_queue; /*!< pending commands other than RQNT */
375 ast_mutex_t cmd_queue_lock;
376 int delme; /*!< needed for reload */
377 int needaudit; /*!< needed for reload */
378 struct ast_dsp *dsp; /*!< XXX Should there be a dsp/subchannel? XXX */
379 /* owner is tracked on the subchannels, and the *sub indicates whos in charge */
380 /* struct ast_channel *owner; */
381 /* struct ast_rtp *rtp; */
382 /* struct sockaddr_in tmpdest; */
383 /* message go the the endpoint and not the channel so they stay here */
384 struct ast_variable *chanvars; /*!< Variables to set for channel created by user */
385 struct mgcp_endpoint *next;
386 struct mgcp_gateway *parent;
389 static struct mgcp_gateway {
390 /* A gateway containing one or more endpoints */
392 int isnamedottedip; /*!< is the name FQDN or dotted ip */
393 struct sockaddr_in addr;
394 struct sockaddr_in defaddr;
395 struct in_addr ourip;
397 int expire; /*!< XXX Should we ever expire dynamic registrations? XXX */
398 struct mgcp_endpoint *endpoints;
405 /* Wildcard endpoint name */
407 struct mgcp_message *msgs; /*!< gw msg queue */
408 ast_mutex_t msgs_lock; /*!< queue lock */
409 int retransid; /*!< retrans timer id */
410 int delme; /*!< needed for reload */
412 struct mgcp_response *responses;
413 struct mgcp_gateway *next;
416 AST_MUTEX_DEFINE_STATIC(mgcp_reload_lock);
417 static int mgcp_reloading = 0;
419 /*! \brief gatelock: mutex for gateway/endpoint lists */
420 AST_MUTEX_DEFINE_STATIC(gatelock);
422 static int mgcpsock = -1;
424 static struct sockaddr_in bindaddr;
426 static struct ast_frame *mgcp_read(struct ast_channel *ast);
427 static int transmit_response(struct mgcp_subchannel *sub, char *msg, struct mgcp_request *req, char *msgrest);
428 static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone);
429 static int transmit_modify_request(struct mgcp_subchannel *sub);
430 static int transmit_connect(struct mgcp_subchannel *sub);
431 static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername);
432 static int transmit_modify_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp, format_t codecs);
433 static int transmit_connection_del(struct mgcp_subchannel *sub);
434 static int transmit_audit_endpoint(struct mgcp_endpoint *p);
435 static void start_rtp(struct mgcp_subchannel *sub);
436 static void handle_response(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
437 int result, unsigned int ident, struct mgcp_request *resp);
438 static void dump_cmd_queues(struct mgcp_endpoint *p, struct mgcp_subchannel *sub);
439 static char *mgcp_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
440 static int reload_config(int reload);
442 static struct ast_channel *mgcp_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
443 static int mgcp_call(struct ast_channel *ast, char *dest, int timeout);
444 static int mgcp_hangup(struct ast_channel *ast);
445 static int mgcp_answer(struct ast_channel *ast);
446 static struct ast_frame *mgcp_read(struct ast_channel *ast);
447 static int mgcp_write(struct ast_channel *ast, struct ast_frame *frame);
448 static int mgcp_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
449 static int mgcp_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
450 static int mgcp_senddigit_begin(struct ast_channel *ast, char digit);
451 static int mgcp_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
452 static int mgcp_devicestate(void *data);
453 static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp, char *tone);
454 static int transmit_connect_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp);
455 static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v);
456 static int mgcp_alloc_pktcgate(struct mgcp_subchannel *sub);
457 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen);
458 static struct ast_variable *add_var(const char *buf, struct ast_variable *list);
459 static struct ast_variable *copy_vars(struct ast_variable *src);
461 static const struct ast_channel_tech mgcp_tech = {
463 .description = tdesc,
464 .capabilities = AST_FORMAT_ULAW | AST_FORMAT_ALAW,
465 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
466 .requester = mgcp_request,
467 .devicestate = mgcp_devicestate,
469 .hangup = mgcp_hangup,
470 .answer = mgcp_answer,
473 .indicate = mgcp_indicate,
475 .send_digit_begin = mgcp_senddigit_begin,
476 .send_digit_end = mgcp_senddigit_end,
477 .bridge = ast_rtp_instance_bridge,
478 .func_channel_read = acf_channel_read,
481 static void mwi_event_cb(const struct ast_event *event, void *userdata)
483 /* This module does not handle MWI in an event-based manner. However, it
484 * subscribes to MWI for each mailbox that is configured so that the core
485 * knows that we care about it. Then, chan_mgcp will get the MWI from the
486 * event cache instead of checking the mailbox directly. */
489 static int has_voicemail(struct mgcp_endpoint *p)
492 struct ast_event *event;
495 cntx = mbox = ast_strdupa(p->mailbox);
497 if (ast_strlen_zero(cntx))
500 event = ast_event_get_cached(AST_EVENT_MWI,
501 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mbox,
502 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, cntx,
506 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
507 ast_event_destroy(event);
509 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
514 static int unalloc_sub(struct mgcp_subchannel *sub)
516 struct mgcp_endpoint *p = sub->parent;
518 ast_log(LOG_WARNING, "Trying to unalloc the real channel %s@%s?!?\n", p->name, p->parent->name);
521 ast_debug(1, "Released sub %d of channel %s@%s\n", sub->id, p->name, p->parent->name);
524 if (!ast_strlen_zero(sub->cxident)) {
525 transmit_connection_del(sub);
527 sub->cxident[0] = '\0';
528 sub->callid[0] = '\0';
529 sub->cxmode = MGCP_CX_INACTIVE;
531 sub->alreadygone = 0;
532 memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
534 ast_rtp_instance_destroy(sub->rtp);
537 dump_cmd_queues(NULL, sub);
541 /* modified for new transport mechanism */
542 static int __mgcp_xmit(struct mgcp_gateway *gw, char *data, int len)
545 if (gw->addr.sin_addr.s_addr)
546 res=sendto(mgcpsock, data, len, 0, (struct sockaddr *)&gw->addr, sizeof(struct sockaddr_in));
548 res=sendto(mgcpsock, data, len, 0, (struct sockaddr *)&gw->defaddr, sizeof(struct sockaddr_in));
550 ast_log(LOG_WARNING, "mgcp_xmit returned %d: %s\n", res, strerror(errno));
555 static int resend_response(struct mgcp_subchannel *sub, struct mgcp_response *resp)
557 struct mgcp_endpoint *p = sub->parent;
559 ast_debug(1, "Retransmitting:\n%s\n to %s:%d\n", resp->buf, ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
560 res = __mgcp_xmit(p->parent, resp->buf, resp->len);
566 static int send_response(struct mgcp_subchannel *sub, struct mgcp_request *req)
568 struct mgcp_endpoint *p = sub->parent;
570 ast_debug(1, "Transmitting:\n%s\n to %s:%d\n", req->data, ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
571 res = __mgcp_xmit(p->parent, req->data, req->len);
577 /* modified for new transport framework */
578 static void dump_queue(struct mgcp_gateway *gw, struct mgcp_endpoint *p)
580 struct mgcp_message *cur, *q = NULL, *w, *prev;
582 ast_mutex_lock(&gw->msgs_lock);
583 for (prev = NULL, cur = gw->msgs; cur; prev = cur, cur = cur->next) {
584 if (!p || cur->owner_ep == p) {
586 prev->next = cur->next;
588 gw->msgs = cur->next;
591 ast_log(LOG_NOTICE, "Removing message from %s transaction %u\n",
592 gw->name, cur->seqno);
603 ast_mutex_unlock(&gw->msgs_lock);
612 static void mgcp_queue_frame(struct mgcp_subchannel *sub, struct ast_frame *f)
616 if (!ast_channel_trylock(sub->owner)) {
617 ast_queue_frame(sub->owner, f);
618 ast_channel_unlock(sub->owner);
621 DEADLOCK_AVOIDANCE(&sub->lock);
629 static void mgcp_queue_hangup(struct mgcp_subchannel *sub)
633 if (!ast_channel_trylock(sub->owner)) {
634 ast_queue_hangup(sub->owner);
635 ast_channel_unlock(sub->owner);
638 DEADLOCK_AVOIDANCE(&sub->lock);
646 static void mgcp_queue_control(struct mgcp_subchannel *sub, int control)
648 struct ast_frame f = { AST_FRAME_CONTROL, { control } };
649 return mgcp_queue_frame(sub, &f);
652 static int retrans_pkt(const void *data)
654 struct mgcp_gateway *gw = (struct mgcp_gateway *)data;
655 struct mgcp_message *cur, *exq = NULL, *w, *prev;
658 /* find out expired msgs */
659 ast_mutex_lock(&gw->msgs_lock);
661 for (prev = NULL, cur = gw->msgs; cur; prev = cur, cur = cur->next) {
662 if (cur->retrans < MAX_RETRANS) {
664 ast_debug(1, "Retransmitting #%d transaction %u on [%s]\n",
665 cur->retrans, cur->seqno, gw->name);
666 __mgcp_xmit(gw, cur->buf, cur->len);
669 prev->next = cur->next;
671 gw->msgs = cur->next;
673 ast_log(LOG_WARNING, "Maximum retries exceeded for transaction %u on [%s]\n",
674 cur->seqno, gw->name);
693 ast_mutex_unlock(&gw->msgs_lock);
697 /* time-out transaction */
698 handle_response(cur->owner_ep, cur->owner_sub, 406, cur->seqno, NULL);
706 /* modified for the new transaction mechanism */
707 static int mgcp_postrequest(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
708 char *data, int len, unsigned int seqno)
710 struct mgcp_message *msg;
711 struct mgcp_message *cur;
712 struct mgcp_gateway *gw;
715 if (!(msg = ast_malloc(sizeof(*msg) + len))) {
718 if (!(gw = ((p && p->parent) ? p->parent : NULL))) {
723 msg->owner_sub = sub;
729 memcpy(msg->buf, data, msg->len);
731 ast_mutex_lock(&gw->msgs_lock);
732 for (cur = gw->msgs; cur && cur->next; cur = cur->next);
740 msg->expire = now.tv_sec * 1000 + now.tv_usec / 1000 + DEFAULT_RETRANS;
742 if (gw->retransid == -1)
743 gw->retransid = ast_sched_add(sched, DEFAULT_RETRANS, retrans_pkt, (void *)gw);
744 ast_mutex_unlock(&gw->msgs_lock);
745 __mgcp_xmit(gw, msg->buf, msg->len);
746 /* XXX Should schedule retransmission XXX */
750 /* modified for new transport */
751 static int send_request(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
752 struct mgcp_request *req, unsigned int seqno)
755 struct mgcp_request **queue, *q, *r, *t;
758 ast_debug(1, "Slow sequence is %d\n", p->slowsequence);
759 if (p->slowsequence) {
760 queue = &p->cmd_queue;
761 l = &p->cmd_queue_lock;
766 queue = &sub->cx_queue;
767 l = &sub->cx_queue_lock;
770 /* delete pending cx cmds */
772 if (!sub->parent->ncs) {
784 queue = &sub->cx_queue;
785 l = &sub->cx_queue_lock;
790 queue = &p->rqnt_queue;
791 l = &p->rqnt_queue_lock;
796 queue = &p->cmd_queue;
797 l = &p->cmd_queue_lock;
803 if (!(r = ast_malloc(sizeof(*r)))) {
804 ast_log(LOG_WARNING, "Cannot post MGCP request: insufficient memory\n");
808 memcpy(r, req, sizeof(*r));
811 ast_debug(1, "Posting Request:\n%s to %s:%d\n", req->data,
812 ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
814 res = mgcp_postrequest(p, sub, req->data, req->len, seqno);
816 ast_debug(1, "Queueing Request:\n%s to %s:%d\n", req->data,
817 ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
820 /* XXX find tail. We could also keep tail in the data struct for faster access */
821 for (t = *queue; t && t->next; t = t->next);
834 static int mgcp_call(struct ast_channel *ast, char *dest, int timeout)
837 struct mgcp_endpoint *p;
838 struct mgcp_subchannel *sub;
840 const char *distinctive_ring = NULL;
841 struct varshead *headp;
842 struct ast_var_t *current;
844 ast_debug(3, "MGCP mgcp_call(%s)\n", ast->name);
847 headp = &ast->varshead;
848 AST_LIST_TRAVERSE(headp,current,entries) {
849 /* Check whether there is an ALERT_INFO variable */
850 if (strcasecmp(ast_var_name(current),"ALERT_INFO") == 0) {
851 distinctive_ring = ast_var_value(current);
855 ast_mutex_lock(&sub->lock);
856 switch (p->hookstate) {
858 if (!ast_strlen_zero(distinctive_ring)) {
859 snprintf(tone, sizeof(tone), "L/wt%s", distinctive_ring);
860 ast_debug(3, "MGCP distinctive callwait %s\n", tone);
862 ast_copy_string(tone, (p->ncs ? "L/wt1" : "L/wt"), sizeof(tone));
863 ast_debug(3, "MGCP normal callwait %s\n", tone);
868 if (!ast_strlen_zero(distinctive_ring)) {
869 snprintf(tone, sizeof(tone), "L/r%s", distinctive_ring);
870 ast_debug(3, "MGCP distinctive ring %s\n", tone);
872 ast_copy_string(tone, "L/rg", sizeof(tone));
873 ast_debug(3, "MGCP default ring\n");
878 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
879 ast_log(LOG_WARNING, "mgcp_call called on %s, neither down nor reserved\n", ast->name);
880 ast_mutex_unlock(&sub->lock);
886 sub->cxmode = MGCP_CX_RECVONLY;
887 ast_setstate(ast, AST_STATE_RINGING);
888 if (p->type == TYPE_LINE) {
892 transmit_modify_request(sub);
895 if (sub->next->owner && !ast_strlen_zero(sub->next->cxident) && !ast_strlen_zero(sub->next->callid)) {
896 /* try to prevent a callwait from disturbing the other connection */
897 sub->next->cxmode = MGCP_CX_RECVONLY;
898 transmit_modify_request(sub->next);
901 transmit_notify_request_with_callerid(sub, tone, ast->connected.id.number, ast->connected.id.name);
902 ast_setstate(ast, AST_STATE_RINGING);
904 if (sub->next->owner && !ast_strlen_zero(sub->next->cxident) && !ast_strlen_zero(sub->next->callid)) {
905 /* Put the connection back in sendrecv */
906 sub->next->cxmode = MGCP_CX_SENDRECV;
907 transmit_modify_request(sub->next);
910 ast_log(LOG_NOTICE, "Don't know how to dial on trunks yet\n");
913 ast_mutex_unlock(&sub->lock);
917 static int mgcp_hangup(struct ast_channel *ast)
919 struct mgcp_subchannel *sub = ast->tech_pvt;
920 struct mgcp_endpoint *p = sub->parent;
922 ast_debug(1, "mgcp_hangup(%s)\n", ast->name);
923 if (!ast->tech_pvt) {
924 ast_debug(1, "Asked to hangup channel not connected\n");
927 if (strcmp(sub->magic, MGCP_SUBCHANNEL_MAGIC)) {
928 ast_debug(1, "Invalid magic. MGCP subchannel freed up already.\n");
931 ast_mutex_lock(&sub->lock);
932 ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s\n", ast->name, p->name, p->parent->name);
934 if ((p->dtmfmode & MGCP_DTMF_INBAND) && p->dsp) {
935 /* check whether other channel is active. */
936 if (!sub->next->owner) {
937 if (p->dtmfmode & MGCP_DTMF_HYBRID) {
938 p->dtmfmode &= ~MGCP_DTMF_INBAND;
940 ast_debug(2, "MGCP free dsp on %s@%s\n", p->name, p->parent->name);
941 ast_dsp_free(p->dsp);
948 /* for deleting gate */
949 if (p->pktcgatealloc && sub->gate) {
950 sub->gate->gate_open = NULL;
951 sub->gate->gate_remove = NULL;
952 sub->gate->got_dq_gi = NULL;
953 sub->gate->tech_pvt = NULL;
954 if (sub->gate->state == GATE_ALLOC_PROGRESS || sub->gate->state == GATE_ALLOCATED) {
955 ast_pktccops_gate_alloc(GATE_DEL, sub->gate, 0, 0, 0, 0, 0, 0, NULL, NULL);
957 sub->gate->deltimer = time(NULL) + 5;
962 if (!ast_strlen_zero(sub->cxident)) {
963 transmit_connection_del(sub);
965 sub->cxident[0] = '\0';
966 if ((sub == p->sub) && sub->next->owner) {
967 if (p->hookstate == MGCP_OFFHOOK) {
968 if (sub->next->owner && ast_bridged_channel(sub->next->owner)) {
970 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);
973 /* set our other connection as the primary and swith over to it */
975 p->sub->cxmode = MGCP_CX_RECVONLY;
976 transmit_modify_request(p->sub);
977 if (sub->next->owner && ast_bridged_channel(sub->next->owner)) {
978 transmit_notify_request_with_callerid(p->sub, "L/rg", ast_bridged_channel(sub->next->owner)->cid.cid_num, ast_bridged_channel(sub->next->owner)->cid.cid_name);
982 } else if ((sub == p->sub->next) && p->hookstate == MGCP_OFFHOOK) {
983 transmit_notify_request(sub, p->ncs ? "" : "L/v");
984 } else if (p->hookstate == MGCP_OFFHOOK) {
985 transmit_notify_request(sub, "L/ro");
987 transmit_notify_request(sub, "");
990 ast->tech_pvt = NULL;
991 sub->alreadygone = 0;
993 sub->cxmode = MGCP_CX_INACTIVE;
994 sub->callid[0] = '\0';
996 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
998 /* Reset temporary destination */
999 memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
1001 ast_rtp_instance_destroy(sub->rtp);
1005 ast_module_unref(ast_module_info->self);
1007 if ((p->hookstate == MGCP_ONHOOK) && (!sub->next->rtp)) {
1008 p->hidecallerid = 0;
1009 if (p->hascallwaiting && !p->callwaiting) {
1010 ast_verb(3, "Enabling call waiting on %s\n", ast->name);
1011 p->callwaiting = -1;
1013 if (has_voicemail(p)) {
1014 ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s set vmwi(+)\n",
1015 ast->name, p->name, p->parent->name);
1016 transmit_notify_request(sub, "L/vmwi(+)");
1018 ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s set vmwi(-)\n",
1019 ast->name, p->name, p->parent->name);
1020 transmit_notify_request(sub, "L/vmwi(-)");
1023 ast_mutex_unlock(&sub->lock);
1027 static char *handle_mgcp_show_endpoints(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1029 struct mgcp_gateway *mg;
1030 struct mgcp_endpoint *me;
1031 int hasendpoints = 0;
1032 struct ast_variable * v = NULL;
1036 e->command = "mgcp show endpoints";
1038 "Usage: mgcp show endpoints\n"
1039 " Lists all endpoints known to the MGCP (Media Gateway Control Protocol) subsystem.\n";
1046 return CLI_SHOWUSAGE;
1048 ast_mutex_lock(&gatelock);
1049 for (mg = gateways; mg; mg = mg->next) {
1050 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");
1051 for (me = mg->endpoints; me; me = me->next) {
1052 ast_cli(a->fd, " -- '%s@%s in '%s' is %s\n", me->name, mg->name, me->context, me->sub->owner ? "active" : "idle");
1054 ast_cli(a->fd, " Variables:\n");
1055 for (v = me->chanvars ; v ; v = v->next) {
1056 ast_cli(a->fd, " %s = '%s'\n", v->name, v->value);
1061 if (!hasendpoints) {
1062 ast_cli(a->fd, " << No Endpoints Defined >> ");
1065 ast_mutex_unlock(&gatelock);
1069 static char *handle_mgcp_audit_endpoint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1071 struct mgcp_gateway *mg;
1072 struct mgcp_endpoint *me;
1074 char *ename,*gname, *c;
1078 e->command = "mgcp audit endpoint";
1080 "Usage: mgcp audit endpoint <endpointid>\n"
1081 " Lists the capabilities of an endpoint in the MGCP (Media Gateway Control Protocol) subsystem.\n"
1082 " mgcp debug MUST be on to see the results of this command.\n";
1089 return CLI_SHOWUSAGE;
1092 return CLI_SHOWUSAGE;
1093 /* split the name into parts by null */
1094 ename = ast_strdupa(a->argv[3]);
1095 for (gname = ename; *gname; gname++) {
1096 if (*gname == '@') {
1102 if (gname[0] == '[') {
1105 if ((c = strrchr(gname, ']'))) {
1108 ast_mutex_lock(&gatelock);
1109 for (mg = gateways; mg; mg = mg->next) {
1110 if (!strcasecmp(mg->name, gname)) {
1111 for (me = mg->endpoints; me; me = me->next) {
1112 if (!strcasecmp(me->name, ename)) {
1114 transmit_audit_endpoint(me);
1124 ast_cli(a->fd, " << Could not find endpoint >> ");
1126 ast_mutex_unlock(&gatelock);
1130 static char *handle_mgcp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1134 e->command = "mgcp set debug {on|off}";
1136 "Usage: mgcp set debug {on|off}\n"
1137 " Enables/Disables dumping of MGCP packets for debugging purposes\n";
1143 if (a->argc != e->args)
1144 return CLI_SHOWUSAGE;
1146 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
1148 ast_cli(a->fd, "MGCP Debugging Enabled\n");
1149 } else if (!strncasecmp(a->argv[3], "off", 3)) {
1151 ast_cli(a->fd, "MGCP Debugging Disabled\n");
1153 return CLI_SHOWUSAGE;
1158 static struct ast_cli_entry cli_mgcp[] = {
1159 AST_CLI_DEFINE(handle_mgcp_audit_endpoint, "Audit specified MGCP endpoint"),
1160 AST_CLI_DEFINE(handle_mgcp_show_endpoints, "List defined MGCP endpoints"),
1161 AST_CLI_DEFINE(handle_mgcp_set_debug, "Enable/Disable MGCP debugging"),
1162 AST_CLI_DEFINE(mgcp_reload, "Reload MGCP configuration"),
1165 static int mgcp_answer(struct ast_channel *ast)
1168 struct mgcp_subchannel *sub = ast->tech_pvt;
1169 struct mgcp_endpoint *p = sub->parent;
1171 ast_mutex_lock(&sub->lock);
1172 sub->cxmode = MGCP_CX_SENDRECV;
1176 transmit_modify_request(sub);
1178 ast_verb(3, "MGCP mgcp_answer(%s) on %s@%s-%d\n",
1179 ast->name, p->name, p->parent->name, sub->id);
1180 if (ast->_state != AST_STATE_UP) {
1181 ast_setstate(ast, AST_STATE_UP);
1182 ast_debug(1, "mgcp_answer(%s)\n", ast->name);
1183 transmit_notify_request(sub, "");
1184 transmit_modify_request(sub);
1186 ast_mutex_unlock(&sub->lock);
1190 static struct ast_frame *mgcp_rtp_read(struct mgcp_subchannel *sub)
1192 /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
1193 struct ast_frame *f;
1195 f = ast_rtp_instance_read(sub->rtp, 0);
1196 /* Don't send RFC2833 if we're not supposed to */
1197 if (f && (f->frametype == AST_FRAME_DTMF) && !(sub->parent->dtmfmode & MGCP_DTMF_RFC2833))
1198 return &ast_null_frame;
1200 /* We already hold the channel lock */
1201 if (f->frametype == AST_FRAME_VOICE) {
1202 if (f->subclass.codec != sub->owner->nativeformats) {
1203 ast_debug(1, "Oooh, format changed to %s\n", ast_getformatname(f->subclass.codec));
1204 sub->owner->nativeformats = f->subclass.codec;
1205 ast_set_read_format(sub->owner, sub->owner->readformat);
1206 ast_set_write_format(sub->owner, sub->owner->writeformat);
1208 /* Courtesy fearnor aka alex@pilosoft.com */
1209 if ((sub->parent->dtmfmode & MGCP_DTMF_INBAND) && (sub->parent->dsp)) {
1211 ast_log(LOG_NOTICE, "MGCP ast_dsp_process\n");
1213 f = ast_dsp_process(sub->owner, sub->parent->dsp, f);
1221 static struct ast_frame *mgcp_read(struct ast_channel *ast)
1223 struct ast_frame *f;
1224 struct mgcp_subchannel *sub = ast->tech_pvt;
1225 ast_mutex_lock(&sub->lock);
1226 f = mgcp_rtp_read(sub);
1227 ast_mutex_unlock(&sub->lock);
1231 static int mgcp_write(struct ast_channel *ast, struct ast_frame *frame)
1233 struct mgcp_subchannel *sub = ast->tech_pvt;
1237 if (frame->frametype != AST_FRAME_VOICE) {
1238 if (frame->frametype == AST_FRAME_IMAGE)
1241 ast_log(LOG_WARNING, "Can't send %d type frames with MGCP write\n", frame->frametype);
1245 if (!(frame->subclass.codec & ast->nativeformats)) {
1246 ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
1247 ast_getformatname(frame->subclass.codec),
1248 ast_getformatname_multiple(buf, sizeof(buf), ast->nativeformats),
1249 ast_getformatname(ast->readformat),
1250 ast_getformatname(ast->writeformat));
1255 ast_mutex_lock(&sub->lock);
1256 if (!sub->sdpsent && sub->gate) {
1257 if (sub->gate->state == GATE_ALLOCATED) {
1258 ast_debug(1, "GATE ALLOCATED, sending sdp\n");
1259 transmit_modify_with_sdp(sub, NULL, 0);
1262 if ((sub->parent->sub == sub) || !sub->parent->singlepath) {
1264 res = ast_rtp_instance_write(sub->rtp, frame);
1267 ast_mutex_unlock(&sub->lock);
1272 static int mgcp_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1274 struct mgcp_subchannel *sub = newchan->tech_pvt;
1276 ast_mutex_lock(&sub->lock);
1277 ast_log(LOG_NOTICE, "mgcp_fixup(%s, %s)\n", oldchan->name, newchan->name);
1278 if (sub->owner != oldchan) {
1279 ast_mutex_unlock(&sub->lock);
1280 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
1283 sub->owner = newchan;
1284 ast_mutex_unlock(&sub->lock);
1288 static int mgcp_senddigit_begin(struct ast_channel *ast, char digit)
1290 struct mgcp_subchannel *sub = ast->tech_pvt;
1291 struct mgcp_endpoint *p = sub->parent;
1294 ast_mutex_lock(&sub->lock);
1295 if (p->dtmfmode & MGCP_DTMF_INBAND || p->dtmfmode & MGCP_DTMF_HYBRID) {
1296 ast_debug(1, "Sending DTMF using inband/hybrid\n");
1297 res = -1; /* Let asterisk play inband indications */
1298 } else if (p->dtmfmode & MGCP_DTMF_RFC2833) {
1299 ast_debug(1, "Sending DTMF using RFC2833");
1300 ast_rtp_instance_dtmf_begin(sub->rtp, digit);
1302 ast_log(LOG_ERROR, "Don't know about DTMF_MODE %d\n", p->dtmfmode);
1304 ast_mutex_unlock(&sub->lock);
1309 static int mgcp_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
1311 struct mgcp_subchannel *sub = ast->tech_pvt;
1312 struct mgcp_endpoint *p = sub->parent;
1316 ast_mutex_lock(&sub->lock);
1317 if (p->dtmfmode & MGCP_DTMF_INBAND || p->dtmfmode & MGCP_DTMF_HYBRID) {
1318 ast_debug(1, "Stopping DTMF using inband/hybrid\n");
1319 res = -1; /* Tell Asterisk to stop inband indications */
1320 } else if (p->dtmfmode & MGCP_DTMF_RFC2833) {
1321 ast_debug(1, "Stopping DTMF using RFC2833\n");
1322 if (sub->parent->ncs) {
1331 transmit_notify_request(sub, tmp);
1332 ast_rtp_instance_dtmf_end(sub->rtp, digit);
1334 ast_log(LOG_ERROR, "Don't know about DTMF_MODE %d\n", p->dtmfmode);
1336 ast_mutex_unlock(&sub->lock);
1342 * \brief mgcp_devicestate: channel callback for device status monitoring
1343 * \param data tech/resource name of MGCP device to query
1345 * Callback for device state management in channel subsystem
1346 * to obtain device status (up/down) of a specific MGCP endpoint
1348 * \return device status result (from devicestate.h) AST_DEVICE_INVALID (not available) or AST_DEVICE_UNKNOWN (available but unknown state)
1350 static int mgcp_devicestate(void *data)
1352 struct mgcp_gateway *g;
1353 struct mgcp_endpoint *e = NULL;
1354 char *tmp, *endpt, *gw;
1355 int ret = AST_DEVICE_INVALID;
1357 endpt = ast_strdupa(data);
1358 if ((tmp = strchr(endpt, '@'))) {
1364 ast_mutex_lock(&gatelock);
1365 for (g = gateways; g; g = g->next) {
1366 if (strcasecmp(g->name, gw) == 0) {
1375 for (; e; e = e->next) {
1376 if (strcasecmp(e->name, endpt) == 0) {
1385 * As long as the gateway/endpoint is valid, we'll
1386 * assume that the device is available and its state
1389 ret = AST_DEVICE_UNKNOWN;
1392 ast_mutex_unlock(&gatelock);
1396 static char *control2str(int ind) {
1398 case AST_CONTROL_HANGUP:
1399 return "Other end has hungup";
1400 case AST_CONTROL_RING:
1401 return "Local ring";
1402 case AST_CONTROL_RINGING:
1403 return "Remote end is ringing";
1404 case AST_CONTROL_ANSWER:
1405 return "Remote end has answered";
1406 case AST_CONTROL_BUSY:
1407 return "Remote end is busy";
1408 case AST_CONTROL_TAKEOFFHOOK:
1409 return "Make it go off hook";
1410 case AST_CONTROL_OFFHOOK:
1411 return "Line is off hook";
1412 case AST_CONTROL_CONGESTION:
1413 return "Congestion (circuits busy)";
1414 case AST_CONTROL_FLASH:
1415 return "Flash hook";
1416 case AST_CONTROL_WINK:
1418 case AST_CONTROL_OPTION:
1419 return "Set a low-level option";
1420 case AST_CONTROL_RADIO_KEY:
1422 case AST_CONTROL_RADIO_UNKEY:
1423 return "Un-Key Radio";
1428 static int mgcp_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
1430 struct mgcp_subchannel *sub = ast->tech_pvt;
1433 ast_debug(3, "MGCP asked to indicate %d '%s' condition on channel %s\n",
1434 ind, control2str(ind), ast->name);
1435 ast_mutex_lock(&sub->lock);
1437 case AST_CONTROL_RINGING:
1438 #ifdef DLINK_BUGGY_FIRMWARE
1439 transmit_notify_request(sub, "rt");
1441 if (!sub->sdpsent) { /* will hide the inband progress!!! */
1442 transmit_notify_request(sub, sub->parent->ncs ? "L/rt" : "G/rt");
1446 case AST_CONTROL_BUSY:
1447 transmit_notify_request(sub, "L/bz");
1449 case AST_CONTROL_CONGESTION:
1450 transmit_notify_request(sub, sub->parent->ncs ? "L/cg" : "G/cg");
1452 case AST_CONTROL_HOLD:
1453 ast_moh_start(ast, data, NULL);
1455 case AST_CONTROL_UNHOLD:
1458 case AST_CONTROL_SRCUPDATE:
1459 ast_rtp_instance_update_source(sub->rtp);
1461 case AST_CONTROL_SRCCHANGE:
1462 ast_rtp_instance_change_source(sub->rtp);
1464 case AST_CONTROL_PROGRESS:
1465 case AST_CONTROL_PROCEEDING:
1466 transmit_modify_request(sub);
1468 transmit_notify_request(sub, "");
1471 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
1474 ast_mutex_unlock(&sub->lock);
1478 static struct ast_channel *mgcp_new(struct mgcp_subchannel *sub, int state, const char *linkedid)
1480 struct ast_channel *tmp;
1481 struct ast_variable *v = NULL;
1482 struct mgcp_endpoint *i = sub->parent;
1485 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, linkedid, i->accountcode, i->exten, i->context, i->amaflags, "MGCP/%s@%s-%d", i->name, i->parent->name, sub->id);
1487 tmp->tech = &mgcp_tech;
1488 tmp->nativeformats = i->capability;
1489 if (!tmp->nativeformats) {
1490 tmp->nativeformats = capability;
1492 fmt = ast_best_codec(tmp->nativeformats);
1494 ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(sub->rtp, 0));
1496 if (i->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID)) {
1497 i->dsp = ast_dsp_new();
1498 ast_dsp_set_features(i->dsp, DSP_FEATURE_DIGIT_DETECT);
1499 /* this is to prevent clipping of dtmf tones during dsp processing */
1500 ast_dsp_set_digitmode(i->dsp, DSP_DIGITMODE_NOQUELCH);
1504 if (state == AST_STATE_RING)
1506 tmp->writeformat = fmt;
1507 tmp->rawwriteformat = fmt;
1508 tmp->readformat = fmt;
1509 tmp->rawreadformat = fmt;
1510 tmp->tech_pvt = sub;
1511 if (!ast_strlen_zero(i->language))
1512 ast_string_field_set(tmp, language, i->language);
1513 if (!ast_strlen_zero(i->accountcode))
1514 ast_string_field_set(tmp, accountcode, i->accountcode);
1516 tmp->amaflags = i->amaflags;
1518 ast_module_ref(ast_module_info->self);
1519 tmp->callgroup = i->callgroup;
1520 tmp->pickupgroup = i->pickupgroup;
1521 ast_string_field_set(tmp, call_forward, i->call_forward);
1522 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
1523 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
1525 /* Don't use ast_set_callerid() here because it will
1526 * generate a needless NewCallerID event */
1527 tmp->cid.cid_ani = ast_strdup(i->cid_num);
1530 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
1534 /* Set channel variables for this call from configuration */
1535 for (v = i->chanvars ; v ; v = v->next) {
1536 char valuebuf[1024];
1537 pbx_builtin_setvar_helper(tmp, v->name, ast_get_encoded_str(v->value, valuebuf, sizeof(valuebuf)));
1541 ast_jb_configure(tmp, &global_jbconf);
1543 if (state != AST_STATE_DOWN) {
1544 if (ast_pbx_start(tmp)) {
1545 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
1550 ast_verb(3, "MGCP mgcp_new(%s) created in state: %s\n",
1551 tmp->name, ast_state2str(state));
1553 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1558 static char *get_sdp_by_line(char* line, char *name, int nameLen)
1560 if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=') {
1561 char *r = line + nameLen + 1;
1562 while (*r && (*r < 33)) ++r;
1568 static char *get_sdp(struct mgcp_request *req, char *name)
1571 int len = strlen(name);
1574 for (x = 0; x < req->lines; x++) {
1575 r = get_sdp_by_line(req->line[x], name, len);
1576 if (r[0] != '\0') return r;
1581 static void sdpLineNum_iterator_init(int *iterator)
1586 static char *get_sdp_iterate(int* iterator, struct mgcp_request *req, char *name)
1588 int len = strlen(name);
1590 while (*iterator < req->lines) {
1591 r = get_sdp_by_line(req->line[(*iterator)++], name, len);
1592 if (r[0] != '\0') return r;
1597 static char *__get_header(struct mgcp_request *req, char *name, int *start, char *def)
1600 int len = strlen(name);
1602 for (x = *start; x < req->headers; x++) {
1603 if (!strncasecmp(req->header[x], name, len) &&
1604 (req->header[x][len] == ':')) {
1605 r = req->header[x] + len + 1;
1606 while (*r && (*r < 33)) {
1613 /* Don't return NULL, so get_header is always a valid pointer */
1617 static char *get_header(struct mgcp_request *req, char *name)
1620 return __get_header(req, name, &start, "");
1623 /*! \brief get_csv: (SC:) get comma separated value */
1624 static char *get_csv(char *c, int *len, char **next)
1628 *next = NULL, *len = 0;
1629 if (!c) return NULL;
1631 while (*c && (*c < 33 || *c == ',')) {
1636 while (*c && (*c >= 33 && *c != ',')) {
1642 s = NULL, *next = NULL;
1648 static struct mgcp_gateway *find_realtime_gw(char *name, char *at, struct sockaddr_in *sin)
1650 struct mgcp_gateway *g = NULL;
1651 struct ast_variable *mgcpgwconfig = NULL;
1652 struct ast_variable *mgcpepconfig = NULL;
1653 struct ast_variable *gwv, *epname = NULL;
1654 struct mgcp_endpoint *e;
1655 char *c = NULL, *line;
1659 ast_debug(1, "*** find Realtime MGCPGW\n");
1661 if (!(i = ast_check_realtime("mgcpgw")) || !(j = ast_check_realtime("mgcpep"))) {
1665 if (ast_strlen_zero(at)) {
1666 ast_debug(1, "null gw name\n");
1670 if (!(mgcpgwconfig = ast_load_realtime("mgcpgw", "name", at, NULL))) {
1675 * \note This is a fairly odd way of instantiating lines. Instead of each
1676 * line created by virtue of being in the database (and loaded via
1677 * ast_load_realtime_multientry), this code forces a specific order with a
1678 * "lines" entry in the "mgcpgw" record. This has benefits, because as with
1679 * chan_dahdi, values are inherited across definitions. The downside is
1680 * that it's not as clear what the values will be simply by looking at a
1681 * single row in the database, and it's probable that the sanest configuration
1682 * should have the first column in the "mgcpep" table be "clearvars", with a
1683 * static value of "all", if any variables are set at all. It may be worth
1684 * making this assumption explicit in the code in the future, and then just
1685 * using ast_load_realtime_multientry for the "mgcpep" records.
1688 for (gwv = mgcpgwconfig; gwv; gwv = gwv->next) {
1689 if (!strcasecmp(gwv->name, "lines")) {
1690 ast_copy_string(lines, gwv->value, sizeof(lines));
1694 /* Position gwv at the end of the list */
1695 for (gwv = gwv && gwv->next ? gwv : mgcpgwconfig; gwv->next; gwv = gwv->next);
1697 if (!ast_strlen_zero(lines)) {
1698 AST_DECLARE_APP_ARGS(args,
1699 AST_APP_ARG(line)[100];
1701 AST_STANDARD_APP_ARGS(args, lines);
1702 for (i = 0; i < args.argc; i++) {
1703 gwv->next = ast_load_realtime("mgcpep", "name", at, "line", args.line[i], NULL);
1705 /* Remove "line" AND position gwv at the end of the list. */
1706 for (epname = NULL; gwv->next; gwv = gwv->next) {
1707 if (!strcasecmp(gwv->next->name, "line")) {
1708 /* Remove it from the list */
1710 gwv->next = gwv->next->next;
1713 /* Since "line" instantiates the configuration, we have to move it to the end. */
1716 epname->next = NULL;
1721 for (gwv = mgcpgwconfig; gwv; gwv = gwv->next) {
1722 ast_debug(1, "MGCP Realtime var: %s => %s\n", gwv->name, gwv->value);
1726 g = build_gateway(at, mgcpgwconfig);
1727 ast_variables_destroy(mgcpgwconfig);
1733 for (e = g->endpoints; e; e = e->next) {
1734 transmit_audit_endpoint(e);
1741 static struct mgcp_subchannel *find_subchannel_and_lock(char *name, int msgid, struct sockaddr_in *sin)
1743 struct mgcp_endpoint *p = NULL;
1744 struct mgcp_subchannel *sub = NULL;
1745 struct mgcp_gateway *g;
1747 char *at = NULL, *c;
1750 ast_copy_string(tmp, name, sizeof(tmp));
1751 at = strchr(tmp, '@');
1753 ast_log(LOG_NOTICE, "Endpoint '%s' has no at sign!\n", name);
1758 ast_mutex_lock(&gatelock);
1759 if (at && (at[0] == '[')) {
1761 c = strrchr(at, ']');
1766 for (g = gateways ? gateways : find_realtime_gw(name, at, sin); g; g = g->next ? g->next : find_realtime_gw(name, at, sin)) {
1767 if ((!name || !strcasecmp(g->name, at)) &&
1768 (sin || g->addr.sin_addr.s_addr || g->defaddr.sin_addr.s_addr)) {
1769 /* Found the gateway. If it's dynamic, save it's address -- now for the endpoint */
1770 if (sin && g->dynamic && name) {
1771 if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) ||
1772 (g->addr.sin_port != sin->sin_port)) {
1773 memcpy(&g->addr, sin, sizeof(g->addr));
1774 if (ast_ouraddrfor(&g->addr.sin_addr, &g->ourip))
1775 memcpy(&g->ourip, &__ourip, sizeof(g->ourip));
1776 ast_verb(3, "Registered MGCP gateway '%s' at %s port %d\n", g->name, ast_inet_ntoa(g->addr.sin_addr), ntohs(g->addr.sin_port));
1778 /* not dynamic, check if the name matches */
1780 if (strcasecmp(g->name, at)) {
1784 /* not dynamic, no name, check if the addr matches */
1785 } else if (!name && sin) {
1786 if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) ||
1787 (g->addr.sin_port != sin->sin_port)) {
1789 g = find_realtime_gw(name, at, sin);
1797 for (p = g->endpoints; p; p = p->next) {
1798 ast_debug(1, "Searching on %s@%s for subchannel\n", p->name, g->name);
1803 } else if (name && !strcasecmp(p->name, tmp)) {
1804 ast_debug(1, "Coundn't determine subchannel, assuming current master %s@%s-%d\n",
1805 p->name, g->name, p->sub->id);
1812 ast_mutex_lock(&sub->lock);
1817 ast_mutex_unlock(&gatelock);
1821 ast_log(LOG_NOTICE, "Endpoint '%s' not found on gateway '%s'\n", tmp, at);
1823 ast_log(LOG_NOTICE, "Gateway '%s' (and thus its endpoint '%s') does not exist\n", at, tmp);
1830 static void parse(struct mgcp_request *req)
1832 /* Divide fields by NULL's */
1837 /* First header starts immediately */
1841 /* We've got a new header */
1843 ast_debug(3, "Header: %s (%d)\n", req->header[f], (int) strlen(req->header[f]));
1844 if (ast_strlen_zero(req->header[f])) {
1845 /* Line by itself means we're now in content */
1849 if (f >= MGCP_MAX_HEADERS - 1) {
1850 ast_log(LOG_WARNING, "Too many MGCP headers...\n");
1854 req->header[f] = c + 1;
1855 } else if (*c == '\r') {
1856 /* Ignore but eliminate \r's */
1860 /* Check for last header */
1861 if (!ast_strlen_zero(req->header[f])) {
1865 /* Now we process any mime content */
1870 /* We've got a new line */
1872 ast_debug(3, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
1873 if (f >= MGCP_MAX_LINES - 1) {
1874 ast_log(LOG_WARNING, "Too many SDP lines...\n");
1878 req->line[f] = c + 1;
1879 } else if (*c == '\r') {
1880 /* Ignore and eliminate \r's */
1884 /* Check for last line */
1885 if (!ast_strlen_zero(req->line[f])) {
1889 /* Parse up the initial header */
1891 while (*c && *c < 33) c++;
1892 /* First the verb */
1894 while (*c && (*c > 32)) c++;
1898 while (*c && (*c < 33)) c++;
1899 req->identifier = c;
1900 while (*c && (*c > 32)) c++;
1904 while (*c && (*c < 33)) c++;
1906 while (*c && (*c > 32)) c++;
1910 while (*c && (*c < 33)) c++;
1912 while (*c && (*c > 32)) c++;
1913 while (*c && (*c < 33)) c++;
1914 while (*c && (*c > 32)) c++;
1920 ast_debug(1, "Verb: '%s', Identifier: '%s', Endpoint: '%s', Version: '%s'\n",
1921 req->verb, req->identifier, req->endpoint, req->version);
1922 ast_debug(1, "%d headers, %d lines\n", req->headers, req->lines);
1924 ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
1928 static int process_sdp(struct mgcp_subchannel *sub, struct mgcp_request *req)
1936 format_t peercapability;
1937 int peerNonCodecCapability;
1938 struct sockaddr_in sin;
1940 struct ast_hostent ahp; struct hostent *hp;
1941 int codec, codec_count=0;
1943 struct mgcp_endpoint *p = sub->parent;
1944 char tmp1[256], tmp2[256], tmp3[256];
1946 /* Get codec and RTP info from SDP */
1947 m = get_sdp(req, "m");
1948 c = get_sdp(req, "c");
1949 if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
1950 ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
1953 if (sscanf(c, "IN IP4 %256s", host) != 1) {
1954 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
1957 /* XXX This could block for a long time, and block the main thread! XXX */
1958 hp = ast_gethostbyname(host, &ahp);
1960 ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
1963 if (sscanf(m, "audio %30d RTP/AVP %n", &portno, &len) != 1) {
1964 ast_log(LOG_WARNING, "Unable to determine port number for RTP in '%s'\n", m);
1967 sin.sin_family = AF_INET;
1968 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
1969 sin.sin_port = htons(portno);
1970 ast_rtp_instance_set_remote_address(sub->rtp, &sin);
1971 ast_debug(3, "Peer RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
1972 /* Scan through the RTP payload types specified in a "m=" line: */
1973 ast_rtp_codecs_payloads_clear(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp);
1974 codecs = ast_strdupa(m + len);
1975 while (!ast_strlen_zero(codecs)) {
1976 if (sscanf(codecs, "%30d%n", &codec, &len) != 1) {
1980 ast_log(LOG_WARNING, "Error in codec string '%s' at '%s'\n", m, codecs);
1983 ast_rtp_codecs_payloads_set_m_type(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp, codec);
1988 /* Next, scan through each "a=rtpmap:" line, noting each */
1989 /* specified RTP payload type (with corresponding MIME subtype): */
1990 sdpLineNum_iterator_init(&iterator);
1991 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
1992 char* mimeSubtype = ast_strdupa(a); /* ensures we have enough space */
1993 if (sscanf(a, "rtpmap: %30u %127[^/]/", &codec, mimeSubtype) != 2)
1995 /* Note: should really look at the 'freq' and '#chans' params too */
1996 ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp, codec, "audio", mimeSubtype, 0);
1999 /* Now gather all of the codecs that were asked for: */
2000 ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(sub->rtp), &peercapability, &peerNonCodecCapability);
2001 p->capability = capability & peercapability;
2002 ast_debug(1, "Capabilities: us - %s, them - %s, combined - %s\n",
2003 ast_getformatname_multiple(tmp1, sizeof(tmp1), capability),
2004 ast_getformatname_multiple(tmp2, sizeof(tmp2), peercapability),
2005 ast_getformatname_multiple(tmp3, sizeof(tmp3), p->capability));
2006 ast_debug(1, "Non-codec capabilities: us - %d, them - %d, combined - %d\n",
2007 nonCodecCapability, peerNonCodecCapability, p->nonCodecCapability);
2008 if (!p->capability) {
2009 ast_log(LOG_WARNING, "No compatible codecs!\n");
2015 static int add_header(struct mgcp_request *req, const char *var, const char *value)
2017 if (req->len >= sizeof(req->data) - 4) {
2018 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
2022 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
2025 req->header[req->headers] = req->data + req->len;
2026 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s: %s\r\n", var, value);
2027 req->len += strlen(req->header[req->headers]);
2028 if (req->headers < MGCP_MAX_HEADERS) {
2031 ast_log(LOG_WARNING, "Out of header space\n");
2037 static int add_line(struct mgcp_request *req, char *line)
2039 if (req->len >= sizeof(req->data) - 4) {
2040 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
2044 /* Add extra empty return */
2045 ast_copy_string(req->data + req->len, "\r\n", sizeof(req->data) - req->len);
2046 req->len += strlen(req->data + req->len);
2048 req->line[req->lines] = req->data + req->len;
2049 snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
2050 req->len += strlen(req->line[req->lines]);
2051 if (req->lines < MGCP_MAX_LINES) {
2054 ast_log(LOG_WARNING, "Out of line space\n");
2060 static int init_resp(struct mgcp_request *req, char *resp, struct mgcp_request *orig, char *resprest)
2062 /* Initialize a response */
2063 if (req->headers || req->len) {
2064 ast_log(LOG_WARNING, "Request already initialized?!?\n");
2067 req->header[req->headers] = req->data + req->len;
2068 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %s %s\r\n", resp, orig->identifier, resprest);
2069 req->len += strlen(req->header[req->headers]);
2070 if (req->headers < MGCP_MAX_HEADERS) {
2073 ast_log(LOG_WARNING, "Out of header space\n");
2078 static int init_req(struct mgcp_endpoint *p, struct mgcp_request *req, char *verb)
2080 /* Initialize a response */
2081 if (req->headers || req->len) {
2082 ast_log(LOG_WARNING, "Request already initialized?!?\n");
2085 req->header[req->headers] = req->data + req->len;
2086 /* check if we need brackets around the gw name */
2087 if (p->parent->isnamedottedip) {
2088 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" : "");
2090 + 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" : "");
2092 req->len += strlen(req->header[req->headers]);
2093 if (req->headers < MGCP_MAX_HEADERS) {
2096 ast_log(LOG_WARNING, "Out of header space\n");
2102 static int respprep(struct mgcp_request *resp, struct mgcp_endpoint *p, char *msg, struct mgcp_request *req, char *msgrest)
2104 memset(resp, 0, sizeof(*resp));
2105 init_resp(resp, msg, req, msgrest);
2109 static int reqprep(struct mgcp_request *req, struct mgcp_endpoint *p, char *verb)
2111 memset(req, 0, sizeof(struct mgcp_request));
2113 if (oseq > 999999999) {
2116 init_req(p, req, verb);
2120 static int transmit_response(struct mgcp_subchannel *sub, char *msg, struct mgcp_request *req, char *msgrest)
2122 struct mgcp_request resp;
2123 struct mgcp_endpoint *p = sub->parent;
2124 struct mgcp_response *mgr;
2130 respprep(&resp, p, msg, req, msgrest);
2131 if (!(mgr = ast_calloc(1, sizeof(*mgr) + resp.len + 1))) {
2132 return send_response(sub, &resp);
2134 /* Store MGCP response in case we have to retransmit */
2135 sscanf(req->identifier, "%30d", &mgr->seqno);
2136 time(&mgr->whensent);
2137 mgr->len = resp.len;
2138 memcpy(mgr->buf, resp.data, resp.len);
2139 mgr->buf[resp.len] = '\0';
2140 mgr->next = p->parent->responses;
2141 p->parent->responses = mgr;
2143 return send_response(sub, &resp);
2147 static int add_sdp(struct mgcp_request *resp, struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp)
2152 struct sockaddr_in sin;
2161 struct sockaddr_in dest = { 0, };
2162 struct mgcp_endpoint *p = sub->parent;
2163 /* XXX We break with the "recommendation" and send our IP, in order that our
2164 peer doesn't have to ast_gethostbyname() us XXX */
2167 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
2170 ast_rtp_instance_get_local_address(sub->rtp, &sin);
2172 ast_rtp_instance_get_remote_address(sub->rtp, &dest);
2174 if (sub->tmpdest.sin_addr.s_addr) {
2175 dest.sin_addr = sub->tmpdest.sin_addr;
2176 dest.sin_port = sub->tmpdest.sin_port;
2177 /* Reset temporary destination */
2178 memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
2180 dest.sin_addr = p->parent->ourip;
2181 dest.sin_port = sin.sin_port;
2184 ast_debug(1, "We're at %s port %d\n", ast_inet_ntoa(p->parent->ourip), ntohs(sin.sin_port));
2185 ast_copy_string(v, "v=0\r\n", sizeof(v));
2186 snprintf(o, sizeof(o), "o=root %d %d IN IP4 %s\r\n", (int)getpid(), (int)getpid(), ast_inet_ntoa(dest.sin_addr));
2187 ast_copy_string(s, "s=session\r\n", sizeof(s));
2188 snprintf(c, sizeof(c), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
2189 ast_copy_string(t, "t=0 0\r\n", sizeof(t));
2190 snprintf(m, sizeof(m), "m=audio %d RTP/AVP", ntohs(dest.sin_port));
2191 for (x = 1LL; x <= AST_FORMAT_AUDIO_MASK; x <<= 1) {
2192 if (!(x & AST_FORMAT_AUDIO_MASK)) {
2193 /* Audio is now discontiguous */
2196 if (p->capability & x) {
2197 ast_debug(1, "Answering with capability %s\n", ast_getformatname(x));
2198 codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 1, x);
2200 snprintf(costr, sizeof(costr), " %d", codec);
2201 strncat(m, costr, sizeof(m) - strlen(m) - 1);
2202 snprintf(costr, sizeof(costr), "a=rtpmap:%d %s/8000\r\n", codec, ast_rtp_lookup_mime_subtype2(1, x, 0));
2203 strncat(a, costr, sizeof(a) - strlen(a) - 1);
2207 for (x = 1LL; x <= AST_RTP_MAX; x <<= 1) {
2208 if (p->nonCodecCapability & x) {
2209 ast_debug(1, "Answering with non-codec capability %d\n", (int) x);
2210 codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 0, x);
2212 snprintf(costr, sizeof(costr), " %d", codec);
2213 strncat(m, costr, sizeof(m) - strlen(m) - 1);
2214 snprintf(costr, sizeof(costr), "a=rtpmap:%d %s/8000\r\n", codec, ast_rtp_lookup_mime_subtype2(0, x, 0));
2215 strncat(a, costr, sizeof(a) - strlen(a) - 1);
2216 if (x == AST_RTP_DTMF) {
2217 /* Indicate we support DTMF... Not sure about 16,
2218 but MSN supports it so dang it, we will too... */
2219 snprintf(costr, sizeof costr, "a=fmtp:%d 0-16\r\n", codec);
2220 strncat(a, costr, sizeof(a) - strlen(a) - 1);
2225 strncat(m, "\r\n", sizeof(m) - strlen(m) - 1);
2226 len = strlen(v) + strlen(s) + strlen(o) + strlen(c) + strlen(t) + strlen(m) + strlen(a);
2227 snprintf(costr, sizeof(costr), "%d", len);
2238 static int transmit_modify_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp, format_t codecs)
2240 struct mgcp_request resp;
2243 struct mgcp_endpoint *p = sub->parent;
2246 if (ast_strlen_zero(sub->cxident) && rtp) {
2247 /* We don't have a CXident yet, store the destination and
2249 ast_rtp_instance_get_remote_address(rtp, &sub->tmpdest);
2252 ast_copy_string(local, "e:on, s:off, p:20", sizeof(local));
2253 for (x = 1; x <= AST_FORMAT_AUDIO_MASK; x <<= 1) {
2254 if (!(x & AST_FORMAT_AUDIO_MASK)) {
2255 /* No longer contiguous */
2258 if (p->capability & x) {
2259 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, x, 0));
2260 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2265 if (sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) {
2266 snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
2267 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2271 ast_debug(1, "Waiting for opened gate...\n");
2278 reqprep(&resp, p, "MDCX");
2279 add_header(&resp, "C", sub->callid);
2280 add_header(&resp, "L", local);
2281 add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
2282 /* X header should not be sent. kept for compatibility */
2283 add_header(&resp, "X", sub->txident);
2284 add_header(&resp, "I", sub->cxident);
2285 /*add_header(&resp, "S", "");*/
2286 add_sdp(&resp, sub, rtp);
2287 /* fill in new fields */
2288 resp.cmd = MGCP_CMD_MDCX;
2290 return send_request(p, sub, &resp, oseq);
2293 static int transmit_connect_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp)
2295 struct mgcp_request resp;
2299 struct mgcp_endpoint *p = sub->parent;
2301 ast_debug(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n",
2302 p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2304 ast_copy_string(local, "e:on, s:off, p:20", sizeof(local));
2306 for (x = 1; x <= AST_FORMAT_AUDIO_MASK; x <<= 1) {
2307 if (!(x & AST_FORMAT_AUDIO_MASK)) {
2308 /* No longer contiguous */
2311 if (p->capability & x) {
2312 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, x, 0));
2313 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2318 if(sub->gate->state == GATE_ALLOCATED) {
2319 snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
2320 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2324 reqprep(&resp, p, "CRCX");
2325 add_header(&resp, "C", sub->callid);
2326 add_header(&resp, "L", local);
2327 add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
2328 /* X header should not be sent. kept for compatibility */
2329 add_header(&resp, "X", sub->txident);
2330 /*add_header(&resp, "S", "");*/
2331 add_sdp(&resp, sub, rtp);
2332 /* fill in new fields */
2333 resp.cmd = MGCP_CMD_CRCX;
2335 return send_request(p, sub, &resp, oseq);
2338 static int mgcp_pktcgate_remove(struct cops_gate *gate)
2340 struct mgcp_subchannel *sub = gate->tech_pvt;
2346 ast_mutex_lock(&sub->lock);
2347 ast_debug(1, "Pktc: gate 0x%x deleted\n", gate->gateid);
2348 if (sub->gate->state != GATE_CLOSED && sub->parent->hangupongateremove) {
2351 ast_softhangup(sub->owner, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
2352 ast_channel_unlock(sub->owner);
2357 ast_mutex_unlock(&sub->lock);
2361 static int mgcp_pktcgate_open(struct cops_gate *gate)
2363 struct mgcp_subchannel *sub = gate->tech_pvt;
2367 ast_mutex_lock(&sub->lock);
2368 ast_debug(1, "Pktc: gate 0x%x open\n", gate->gateid);
2369 if (!sub->sdpsent) transmit_modify_with_sdp(sub, NULL, 0);
2370 ast_mutex_unlock(&sub->lock);
2374 static int mgcp_alloc_pktcgate(struct mgcp_subchannel *sub)
2376 struct mgcp_endpoint *p = sub->parent;
2377 sub->gate = ast_pktccops_gate_alloc(GATE_SET, NULL, ntohl(p->parent->addr.sin_addr.s_addr),
2378 8, 128000, 232, 0, 0, NULL, &mgcp_pktcgate_remove);
2383 sub->gate->tech_pvt = sub;
2384 sub->gate->gate_open = &mgcp_pktcgate_open;
2388 static int transmit_connect(struct mgcp_subchannel *sub)
2390 struct mgcp_request resp;
2394 struct mgcp_endpoint *p = sub->parent;
2396 ast_copy_string(local, "p:20, s:off, e:on", sizeof(local));
2398 for (x = 1LL; x <= AST_FORMAT_AUDIO_MASK; x <<= 1) {
2399 if (p->capability & x) {
2400 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, x, 0));
2401 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2405 ast_debug(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n",
2406 p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2408 reqprep(&resp, p, "CRCX");
2409 add_header(&resp, "C", sub->callid);
2410 add_header(&resp, "L", local);
2411 add_header(&resp, "M", "inactive");
2412 /* X header should not be sent. kept for compatibility */
2413 add_header(&resp, "X", sub->txident);
2414 /*add_header(&resp, "S", "");*/
2415 /* fill in new fields */
2416 resp.cmd = MGCP_CMD_CRCX;
2418 return send_request(p, sub, &resp, oseq);
2421 static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone)
2423 struct mgcp_request resp;
2424 struct mgcp_endpoint *p = sub->parent;
2426 ast_debug(3, "MGCP Asked to indicate tone: %s on %s@%s-%d in cxmode: %s\n",
2427 tone, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode]);
2428 ast_copy_string(p->curtone, tone, sizeof(p->curtone));
2429 reqprep(&resp, p, "RQNT");
2430 add_header(&resp, "X", p->rqnt_ident);
2431 switch (p->hookstate) {
2433 add_header(&resp, "R", "L/hd(N)");
2436 add_header_offhook(sub, &resp, tone);
2439 if (!ast_strlen_zero(tone)) {
2440 add_header(&resp, "S", tone);
2442 /* fill in new fields */
2443 resp.cmd = MGCP_CMD_RQNT;
2445 return send_request(p, NULL, &resp, oseq);
2448 static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername)
2450 struct mgcp_request resp;
2453 struct timeval t = ast_tvnow();
2455 struct mgcp_endpoint *p = sub->parent;
2457 ast_localtime(&t, &tm, NULL);
2465 /* Keep track of last callerid for blacklist and callreturn */
2466 ast_copy_string(p->lastcallerid, l, sizeof(p->lastcallerid));
2468 snprintf(tone2, sizeof(tone2), "%s,L/ci(%02d/%02d/%02d/%02d,%s,%s)", tone,
2469 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, l, n);
2470 ast_copy_string(p->curtone, tone, sizeof(p->curtone));
2471 reqprep(&resp, p, "RQNT");
2472 add_header(&resp, "X", p->rqnt_ident);
2473 switch (p->hookstate) {
2475 add_header(&resp, "R", "L/hd(N)");
2478 add_header_offhook(sub, &resp, tone);
2481 if (!ast_strlen_zero(tone2)) {
2482 add_header(&resp, "S", tone2);
2484 ast_debug(3, "MGCP Asked to indicate tone: %s on %s@%s-%d in cxmode: %s\n",
2485 tone2, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode]);
2486 /* fill in new fields */
2487 resp.cmd = MGCP_CMD_RQNT;
2489 return send_request(p, NULL, &resp, oseq);
2492 static int transmit_modify_request(struct mgcp_subchannel *sub)
2494 struct mgcp_request resp;
2495 struct mgcp_endpoint *p = sub->parent;
2501 if (ast_strlen_zero(sub->cxident)) {
2502 /* We don't have a CXident yet, store the destination and
2506 ast_debug(3, "Modified %s@%s-%d with new mode: %s on callid: %s\n",
2507 p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2509 ast_copy_string(local, "", sizeof(local));
2510 for (x = 1; x <= AST_FORMAT_AUDIO_MASK; x <<= 1) {
2511 if (p->capability & x) {
2512 if (p->ncs && !fc) {
2513 p->capability = x; /* sb5120e bug */
2517 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, x, 0));
2519 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2523 if (!sub->sdpsent) {
2525 if (sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) {
2526 snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
2527 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2529 /* we still dont have gateid wait */
2535 reqprep(&resp, p, "MDCX");
2536 add_header(&resp, "C", sub->callid);
2537 if (!sub->sdpsent) {
2538 add_header(&resp, "L", local);
2540 add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
2541 /* X header should not be sent. kept for compatibility */
2542 add_header(&resp, "X", sub->txident);
2543 add_header(&resp, "I", sub->cxident);
2544 switch (sub->parent->hookstate) {
2546 add_header(&resp, "R", "L/hd(N)");
2549 add_header_offhook(sub, &resp, "");
2552 if (!sub->sdpsent) {
2553 add_sdp(&resp, sub, NULL);
2556 /* fill in new fields */
2557 resp.cmd = MGCP_CMD_MDCX;
2559 return send_request(p, sub, &resp, oseq);
2563 static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp, char *tone)
2565 struct mgcp_endpoint *p = sub->parent;
2566 char tone_indicate_end = 0;
2568 /* We also should check the tone to indicate, because it have no sense
2569 to request notify D/[0-9#*] (dtmf keys) if we are sending congestion
2570 tone for example G/cg */
2571 if (p && (!strcasecmp(tone, (p->ncs ? "L/ro" : "G/cg")))) {
2572 tone_indicate_end = 1;
2575 if (p && p->sub && p->sub->owner &&
2576 p->sub->owner->_state >= AST_STATE_RINGING &&
2577 (p->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID))) {
2578 add_header(resp, "R", "L/hu(N),L/hf(N)");
2580 } else if (!tone_indicate_end){
2581 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)"));
2583 ast_debug(1, "We don't want more digits if we will end the call\n");
2584 add_header(resp, "R", "L/hu(N),L/hf(N)");
2591 static int transmit_audit_endpoint(struct mgcp_endpoint *p)
2593 struct mgcp_request resp;
2594 reqprep(&resp, p, "AUEP");
2595 /* removed unknown param VS */
2596 /*add_header(&resp, "F", "A,R,D,S,X,N,I,T,O,ES,E,MD,M");*/
2597 add_header(&resp, "F", "A");
2598 /* fill in new fields */
2599 resp.cmd = MGCP_CMD_AUEP;
2601 return send_request(p, NULL, &resp, oseq);
2604 static int transmit_connection_del(struct mgcp_subchannel *sub)
2606 struct mgcp_endpoint *p = sub->parent;
2607 struct mgcp_request resp;
2609 ast_debug(3, "Delete connection %s %s@%s-%d with new mode: %s on callid: %s\n",
2610 sub->cxident, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2611 reqprep(&resp, p, "DLCX");
2612 /* check if call id is avail */
2614 add_header(&resp, "C", sub->callid);
2615 /* X header should not be sent. kept for compatibility */
2616 add_header(&resp, "X", sub->txident);
2617 /* check if cxident is avail */
2618 if (sub->cxident[0])
2619 add_header(&resp, "I", sub->cxident);
2620 /* fill in new fields */
2621 resp.cmd = MGCP_CMD_DLCX;
2623 return send_request(p, sub, &resp, oseq);
2626 static int transmit_connection_del_w_params(struct mgcp_endpoint *p, char *callid, char *cxident)
2628 struct mgcp_request resp;
2630 ast_debug(3, "Delete connection %s %s@%s on callid: %s\n",
2631 cxident ? cxident : "", p->name, p->parent->name, callid ? callid : "");
2632 reqprep(&resp, p, "DLCX");
2633 /* check if call id is avail */
2634 if (callid && *callid)
2635 add_header(&resp, "C", callid);
2636 /* check if cxident is avail */
2637 if (cxident && *cxident)
2638 add_header(&resp, "I", cxident);
2639 /* fill in new fields */
2640 resp.cmd = MGCP_CMD_DLCX;
2642 return send_request(p, p->sub, &resp, oseq);
2645 /*! \brief dump_cmd_queues: (SC:) cleanup pending commands */
2646 static void dump_cmd_queues(struct mgcp_endpoint *p, struct mgcp_subchannel *sub)
2648 struct mgcp_request *t, *q;
2651 ast_mutex_lock(&p->rqnt_queue_lock);
2652 for (q = p->rqnt_queue; q; t = q->next, ast_free(q), q=t);
2653 p->rqnt_queue = NULL;
2654 ast_mutex_unlock(&p->rqnt_queue_lock);
2656 ast_mutex_lock(&p->cmd_queue_lock);
2657 for (q = p->cmd_queue; q; t = q->next, ast_free(q), q=t);
2658 p->cmd_queue = NULL;
2659 ast_mutex_unlock(&p->cmd_queue_lock);
2661 ast_mutex_lock(&p->sub->cx_queue_lock);
2662 for (q = p->sub->cx_queue; q; t = q->next, ast_free(q), q=t);
2663 p->sub->cx_queue = NULL;
2664 ast_mutex_unlock(&p->sub->cx_queue_lock);
2666 ast_mutex_lock(&p->sub->next->cx_queue_lock);
2667 for (q = p->sub->next->cx_queue; q; t = q->next, ast_free(q), q=t);
2668 p->sub->next->cx_queue = NULL;
2669 ast_mutex_unlock(&p->sub->next->cx_queue_lock);
2671 ast_mutex_lock(&sub->cx_queue_lock);
2672 for (q = sub->cx_queue; q; t = q->next, ast_free(q), q=t);
2673 sub->cx_queue = NULL;
2674 ast_mutex_unlock(&sub->cx_queue_lock);
2679 /*! \brief find_command: (SC:) remove command transaction from queue */
2680 static struct mgcp_request *find_command(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
2681 struct mgcp_request **queue, ast_mutex_t *l, int ident)
2683 struct mgcp_request *prev, *req;
2686 for (prev = NULL, req = *queue; req; prev = req, req = req->next) {
2687 if (req->trid == ident) {
2688 /* remove from queue */
2692 prev->next = req->next;
2694 /* send next pending command */
2696 ast_debug(1, "Posting Queued Request:\n%s to %s:%d\n", (*queue)->data,
2697 ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
2699 mgcp_postrequest(p, sub, (*queue)->data, (*queue)->len, (*queue)->trid);
2704 ast_mutex_unlock(l);
2708 /* modified for new transport mechanism */
2709 static void handle_response(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
2710 int result, unsigned int ident, struct mgcp_request *resp)
2713 struct mgcp_request *req;
2714 struct mgcp_gateway *gw = p->parent;
2717 /* provisional response */
2721 if (p->slowsequence)
2722 req = find_command(p, sub, &p->cmd_queue, &p->cmd_queue_lock, ident);
2724 req = find_command(p, sub, &sub->cx_queue, &sub->cx_queue_lock, ident);
2725 else if (!(req = find_command(p, sub, &p->rqnt_queue, &p->rqnt_queue_lock, ident)))
2726 req = find_command(p, sub, &p->cmd_queue, &p->cmd_queue_lock, ident);
2729 ast_verb(3, "No command found on [%s] for transaction %d. Ignoring...\n",
2734 if (p && (result >= 400) && (result <= 599)) {
2737 p->hookstate = MGCP_OFFHOOK;
2740 p->hookstate = MGCP_ONHOOK;
2743 ast_log(LOG_NOTICE, "Transaction %d timed out\n", ident);
2746 ast_log(LOG_NOTICE, "Transaction %d aborted\n", ident);
2750 if (!sub->cxident[0] && (req->cmd == MGCP_CMD_CRCX)) {
2751 ast_log(LOG_NOTICE, "DLCX for all connections on %s due to error %d\n", gw->name, result);
2752 transmit_connection_del(sub);
2755 ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
2756 result, p->name, p->parent->name, sub ? sub->id:-1);
2757 mgcp_queue_hangup(sub);
2760 if (p->sub->next->owner) {
2761 ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
2762 result, p->name, p->parent->name, sub ? sub->id:-1);
2763 mgcp_queue_hangup(p->sub);
2766 if (p->sub->owner) {
2767 ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
2768 result, p->name, p->parent->name, sub ? sub->id:-1);
2769 mgcp_queue_hangup(p->sub);
2772 dump_cmd_queues(p, NULL);
2778 if (result == 200 && (req->cmd == MGCP_CMD_CRCX || req->cmd == MGCP_CMD_MDCX)) {
2780 transmit_response(sub, "000", resp, "OK");
2781 if (sub->owner && sub->owner->_state == AST_STATE_RINGING) {
2782 ast_queue_control(sub->owner, AST_CONTROL_RINGING);
2786 if (req->cmd == MGCP_CMD_CRCX) {
2787 if ((c = get_header(resp, "I"))) {
2788 if (!ast_strlen_zero(c) && sub) {
2789 /* if we are hanging up do not process this conn. */
2791 if (!ast_strlen_zero(sub->cxident)) {
2792 if (strcasecmp(c, sub->cxident)) {
2793 ast_log(LOG_WARNING, "Subchannel already has a cxident. sub->cxident: %s requested %s\n", sub->cxident, c);
2796 ast_copy_string(sub->cxident, c, sizeof(sub->cxident));
2797 if (sub->tmpdest.sin_addr.s_addr) {
2798 transmit_modify_with_sdp(sub, NULL, 0);
2801 /* XXX delete this one
2802 callid and conn id may already be lost.
2803 so the following del conn may have a side effect of
2804 cleaning up the next subchannel */
2805 transmit_connection_del(sub);
2811 if (req->cmd == MGCP_CMD_AUEP) {
2812 /* check stale connection ids */
2813 if ((c = get_header(resp, "I"))) {
2816 while ((v = get_csv(c, &len, &n))) {
2818 if (strncasecmp(v, p->sub->cxident, len) &&
2819 strncasecmp(v, p->sub->next->cxident, len)) {
2820 /* connection id not found. delete it */
2821 char cxident[80] = "";
2823 if (len > (sizeof(cxident) - 1))
2824 len = sizeof(cxident) - 1;
2825 ast_copy_string(cxident, v, len);
2826 ast_verb(3, "Non existing connection id %s on %s@%s \n",
2827 cxident, p->name, gw->name);
2828 transmit_connection_del_w_params(p, NULL, cxident);
2835 /* Try to determine the hookstate returned from an audit endpoint command */
2836 if ((c = get_header(resp, "ES"))) {
2837 if (!ast_strlen_zero(c)) {
2838 if (strstr(c, "hu")) {
2839 if (p->hookstate != MGCP_ONHOOK) {
2840 /* XXX cleanup if we think we are offhook XXX */
2841 if ((p->sub->owner || p->sub->next->owner ) &&
2842 p->hookstate == MGCP_OFFHOOK)
2843 mgcp_queue_hangup(sub);
2844 p->hookstate = MGCP_ONHOOK;
2846 /* update the requested events according to the new hookstate */
2847 transmit_notify_request(p->sub, "");
2849 ast_verb(3, "Setting hookstate of %s@%s to ONHOOK\n", p->name, gw->name);
2851 } else if (strstr(c, "hd")) {
2852 if (p->hookstate != MGCP_OFFHOOK) {
2853 p->hookstate = MGCP_OFFHOOK;
2855 /* update the requested events according to the new hookstate */
2856 transmit_notify_request(p->sub, "");
2858 ast_verb(3, "Setting hookstate of %s@%s to OFFHOOK\n", p->name, gw->name);
2865 if (resp && resp->lines) {
2866 /* do not process sdp if we are hanging up. this may be a late response */
2867 if (sub && sub->owner) {
2871 process_sdp(sub, resp);
2879 static void start_rtp(struct mgcp_subchannel *sub)
2881 ast_mutex_lock(&sub->lock);
2882 /* check again to be on the safe side */
2884 ast_rtp_instance_destroy(sub->rtp);
2887 /* Allocate the RTP now */
2888 sub->rtp = ast_rtp_instance_new("asterisk", sched, &bindaddr, NULL);
2889 if (sub->rtp && sub->owner)
2890 ast_channel_set_fd(sub->owner, 0, ast_rtp_instance_fd(sub->rtp, 0));
2892 ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "MGCP RTP");
2893 ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, sub->nat);
2895 /* Make a call*ID */
2896 snprintf(sub->callid, sizeof(sub->callid), "%08lx%s", ast_random(), sub->txident);
2897 /* Transmit the connection create */
2898 if(!sub->parent->pktcgatealloc) {
2899 transmit_connect_with_sdp(sub, NULL);
2901 transmit_connect(sub);
2903 if(!mgcp_alloc_pktcgate(sub))
2904 mgcp_queue_hangup(sub);
2906 ast_mutex_unlock(&sub->lock);
2909 static void *mgcp_ss(void *data)
2911 struct ast_channel *chan = data;
2912 struct mgcp_subchannel *sub = chan->tech_pvt;
2913 struct mgcp_endpoint *p = sub->parent;
2914 /* char exten[AST_MAX_EXTENSION] = ""; */
2916 int timeout = firstdigittimeout;
2919 int loop_pause = 100;
2921 len = strlen(p->dtmf_buf);
2923 while (len < AST_MAX_EXTENSION - 1) {
2924 ast_debug(1, "Dtmf buffer '%s' for '%s@%s'\n", p->dtmf_buf, p->name, p->parent->name);
2925 res = 1; /* Assume that we will get a digit */
2926 while (strlen(p->dtmf_buf) == len) {
2927 ast_safe_sleep(chan, loop_pause);
2928 timeout -= loop_pause;
2937 len = strlen(p->dtmf_buf);
2939 if (!ast_ignore_pattern(chan->context, p->dtmf_buf)) {
2940 /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
2941 ast_indicate(chan, -1);
2943 /* XXX Redundant? We should already be playing dialtone */
2944 /*tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALTONE);*/
2945 transmit_notify_request(sub, "L/dl");
2947 if (ast_exists_extension(chan, chan->context, p->dtmf_buf, 1, p->cid_num)) {
2948 if (!res || !ast_matchmore_extension(chan, chan->context, p->dtmf_buf, 1, p->cid_num)) {
2950 /* Record this as the forwarding extension */
2951 ast_copy_string(p->call_forward, p->dtmf_buf, sizeof(p->call_forward));
2952 ast_verb(3, "Setting call forward to '%s' on channel %s\n",
2953 p->call_forward, chan->name);
2954 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
2955 transmit_notify_request(sub, "L/sl");
2959 /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
2960 ast_indicate(chan, -1);
2962 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
2963 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALTONE);*/
2964 transmit_notify_request(sub, "L/dl");
2968 /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
2969 ast_indicate(chan, -1);
2970 ast_copy_string(chan->exten, p->dtmf_buf, sizeof(chan->exten));
2971 chan->cid.cid_dnid = ast_strdup(p->dtmf_buf);
2972 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
2973 ast_set_callerid(chan,
2974 p->hidecallerid ? "" : p->cid_num,
2975 p->hidecallerid ? "" : p->cid_name,
2976 chan->cid.cid_ani ? NULL : p->cid_num);
2977 ast_setstate(chan, AST_STATE_RING);
2978 /*dahdi_enable_ec(p);*/
2979 if (p->dtmfmode & MGCP_DTMF_HYBRID) {
2980 p->dtmfmode |= MGCP_DTMF_INBAND;
2981 ast_indicate(chan, -1);
2983 res = ast_pbx_run(chan);
2985 ast_log(LOG_WARNING, "PBX exited non-zero\n");
2986 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_CONGESTION);*/
2987 /*transmit_notify_request(p, "nbz", 1);*/
2988 transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg");
2993 /* It's a match, but they just typed a digit, and there is an ambiguous match,
2994 so just set the timeout to matchdigittimeout and wait some more */
2995 timeout = matchdigittimeout;
2997 } else if (res == 0) {
2998 ast_debug(1, "not enough digits (and no ambiguous match)...\n");
2999 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_CONGESTION);*/
3000 transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg");
3001 /*dahdi_wait_event(p->subs[index].zfd);*/
3003 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
3005 } else if (p->hascallwaiting && p->callwaiting && !strcmp(p->dtmf_buf, "*70")) {
3006 ast_verb(3, "Disabling call waiting on %s\n", chan->name);
3007 /* Disable call waiting if enabled */
3009 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
3010 transmit_notify_request(sub, "L/sl");
3012 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
3013 timeout = firstdigittimeout;
3014 } else if (!strcmp(p->dtmf_buf,ast_pickup_ext())) {
3015 /* Scan all channels and see if any there
3016 * ringing channqels with that have call groups
3017 * that equal this channels pickup group
3019 if (ast_pickup_call(chan)) {
3020 ast_log(LOG_WARNING, "No call pickup possible...\n");
3021 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_CONGESTION);*/
3022 transmit_notify_request(sub, p->ncs ? "L/cg" : "G/cg");
3024 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
3027 } else if (!p->hidecallerid && !strcmp(p->dtmf_buf, "*67")) {
3028 ast_verb(3, "Disabling Caller*ID on %s\n", chan->name);
3029 /* Disable Caller*ID if enabled */
3030 p->hidecallerid = 1;
3031 ast_set_callerid(chan, "", "", NULL);
3032 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
3033 transmit_notify_request(sub, "L/sl");
3035 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
3036 timeout = firstdigittimeout;
3037 } else if (p->callreturn && !strcmp(p->dtmf_buf, "*69")) {
3039 if (!ast_strlen_zero(p->lastcallerid)) {
3040 res = ast_say_digit_str(chan, p->lastcallerid, "", chan->language);
3043 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
3044 transmit_notify_request(sub, "L/sl");
3046 } else if (!strcmp(p->dtmf_buf, "*78")) {
3047 /* Do not disturb */
3048 ast_verb(3, "Enabled DND on channel %s\n", chan->name);
3049 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
3050 transmit_notify_request(sub, "L/sl");
3053 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
3055 } else if (!strcmp(p->dtmf_buf, "*79")) {
3056 /* Do not disturb */
3057 ast_verb(3, "Disabled DND on channel %s\n", chan->name);
3058 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
3059 transmit_notify_request(sub, "L/sl");