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>
25 * \ingroup channel_drivers
28 /*! \li \ref chan_mgcp.c uses the configuration file \ref mgcp.conf
29 * \addtogroup configuration_file
32 /*! \page mgcp.conf mgcp.conf
33 * \verbinclude mgcp.conf.sample
37 <use type="module">res_pktccops</use>
38 <support_level>extended</support_level>
43 ASTERISK_REGISTER_FILE()
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
50 #include <sys/signal.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <arpa/inet.h>
58 #include "asterisk/lock.h"
59 #include "asterisk/channel.h"
60 #include "asterisk/config.h"
61 #include "asterisk/module.h"
62 #include "asterisk/pickup.h"
63 #include "asterisk/pbx.h"
64 #include "asterisk/sched.h"
65 #include "asterisk/io.h"
66 #include "asterisk/rtp_engine.h"
67 #include "asterisk/acl.h"
68 #include "asterisk/callerid.h"
69 #include "asterisk/cli.h"
70 #include "asterisk/say.h"
71 #include "asterisk/astdb.h"
72 #include "asterisk/features.h"
73 #include "asterisk/app.h"
74 #include "asterisk/musiconhold.h"
75 #include "asterisk/utils.h"
76 #include "asterisk/netsock2.h"
77 #include "asterisk/causes.h"
78 #include "asterisk/dsp.h"
79 #include "asterisk/devicestate.h"
80 #include "asterisk/stringfields.h"
81 #include "asterisk/abstract_jb.h"
82 #include "asterisk/chanvars.h"
83 #include "asterisk/pktccops.h"
84 #include "asterisk/stasis.h"
85 #include "asterisk/bridge.h"
86 #include "asterisk/features_config.h"
87 #include "asterisk/parking.h"
88 #include "asterisk/stasis_channels.h"
89 #include "asterisk/format_cache.h"
92 * Define to work around buggy dlink MGCP phone firmware which
93 * appears not to know that "rt" is part of the "G" package.
95 /* #define DLINK_BUGGY_FIRMWARE */
98 #define DEFAULT_EXPIRY 120
99 #define MAX_EXPIRY 3600
100 #define DIRECTMEDIA 1
103 #define INADDR_NONE (in_addr_t)(-1)
106 /*! Global jitterbuffer configuration - by default, jb is disabled
107 * \note Values shown here match the defaults shown in mgcp.conf.sample */
108 static struct ast_jb_conf default_jbconf =
112 .resync_threshold = 1000,
116 static struct ast_jb_conf global_jbconf;
118 static const char tdesc[] = "Media Gateway Control Protocol (MGCP)";
119 static const char config[] = "mgcp.conf";
121 #define MGCP_DTMF_RFC2833 (1 << 0)
122 #define MGCP_DTMF_INBAND (1 << 1)
123 #define MGCP_DTMF_HYBRID (1 << 2)
125 #define DEFAULT_MGCP_GW_PORT 2427 /*!< From RFC 2705 */
126 #define DEFAULT_MGCP_CA_PORT 2727 /*!< From RFC 2705 */
127 #define MGCP_MAX_PACKET 1500 /*!< Also from RFC 2543, should sub headers tho */
128 #define DEFAULT_RETRANS 1000 /*!< How frequently to retransmit */
129 #define MAX_RETRANS 5 /*!< Try only 5 times for retransmissions */
131 /*! MGCP rtp stream modes { */
132 #define MGCP_CX_SENDONLY 0
133 #define MGCP_CX_RECVONLY 1
134 #define MGCP_CX_SENDRECV 2
135 #define MGCP_CX_CONF 3
136 #define MGCP_CX_CONFERENCE 3
137 #define MGCP_CX_MUTE 4
138 #define MGCP_CX_INACTIVE 4
141 static const char * const mgcp_cxmodes[] = {
161 static char context[AST_MAX_EXTENSION] = "default";
163 static char language[MAX_LANGUAGE] = "";
164 static char musicclass[MAX_MUSICCLASS] = "";
165 static char parkinglot[AST_MAX_CONTEXT];
166 static char cid_num[AST_MAX_EXTENSION] = "";
167 static char cid_name[AST_MAX_EXTENSION] = "";
169 static int dtmfmode = 0;
172 static int pktcgatealloc = 0;
173 static int hangupongateremove = 0;
175 static ast_group_t cur_callergroup = 0;
176 static ast_group_t cur_pickupgroup = 0;
180 unsigned int tos_audio;
182 unsigned int cos_audio;
183 } qos = { 0, 0, 0, 0 };
185 static int immediate = 0;
187 static int callwaiting = 0;
189 static int callreturn = 0;
191 static int slowsequence = 0;
193 static int threewaycalling = 0;
195 /*! This is for flashhook transfers */
196 static int transfer = 0;
198 static int cancallforward = 0;
200 static int singlepath = 0;
202 static int directmedia = DIRECTMEDIA;
204 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
206 static char mailbox[AST_MAX_MAILBOX_UNIQUEID];
208 static int amaflags = 0;
212 static unsigned int oseq_global = 0;
213 AST_MUTEX_DEFINE_STATIC(oseq_lock);
215 /*! Wait up to 16 seconds for first digit (FXO logic) */
216 static int firstdigittimeout = 16000;
218 /*! How long to wait for following digits (FXO logic) */
219 static int gendigittimeout = 8000;
221 /*! How long to wait for an extra digit, if there is an ambiguous match */
222 static int matchdigittimeout = 3000;
224 /*! Protect the monitoring thread, so only one process can kill or start it, and not
225 when it's doing something critical. */
226 AST_MUTEX_DEFINE_STATIC(netlock);
228 AST_MUTEX_DEFINE_STATIC(monlock);
230 /*! This is the thread for the monitor which checks for input on the channels
231 * which are not currently in use.
233 static pthread_t monitor_thread = AST_PTHREADT_NULL;
235 static int restart_monitor(void);
237 static struct ast_format_cap *global_capability;
238 static int nonCodecCapability = AST_RTP_DTMF;
240 static char ourhost[MAXHOSTNAMELEN];
241 static struct in_addr __ourip;
244 static int mgcpdebug = 0;
246 static struct ast_sched_context *sched;
247 static struct io_context *io;
248 /*! The private structures of the mgcp channels are linked for
249 * selecting outgoing channels
252 #define MGCP_MAX_HEADERS 64
253 #define MGCP_MAX_LINES 64
255 struct mgcp_request {
261 int headers; /*!< MGCP Headers */
262 char *header[MGCP_MAX_HEADERS];
263 int lines; /*!< SDP Content */
264 char *line[MGCP_MAX_LINES];
265 char data[MGCP_MAX_PACKET];
266 int cmd; /*!< int version of verb = command */
267 unsigned int trid; /*!< int version of identifier = transaction id */
268 struct mgcp_request *next; /*!< next in the queue */
271 /*! \brief mgcp_message: MGCP message for queuing up */
272 struct mgcp_message {
273 struct mgcp_endpoint *owner_ep;
274 struct mgcp_subchannel *owner_sub;
276 unsigned long expire;
279 struct mgcp_message *next;
283 #define RESPONSE_TIMEOUT 30 /*!< in seconds */
285 struct mgcp_response {
289 struct mgcp_response *next;
298 struct mgcp_subchannel {
299 /*! subchannel magic string.
300 Needed to prove that any subchannel pointer passed by asterisk
301 really points to a valid subchannel memory area.
302 Ugly.. But serves the purpose for the time being.
304 #define MGCP_SUBCHANNEL_MAGIC "!978!"
308 struct ast_channel *owner;
309 struct mgcp_endpoint *parent;
310 struct ast_rtp_instance *rtp;
311 struct sockaddr_in tmpdest;
312 char txident[80]; /*! \todo FIXME txident is replaced by rqnt_ident in endpoint.
313 This should be obsoleted */
317 struct mgcp_request *cx_queue; /*!< pending CX commands */
318 ast_mutex_t cx_queue_lock; /*!< CX queue lock */
320 int iseq; /*!< Not used? RTP? */
324 struct cops_gate *gate;
325 struct mgcp_subchannel *next; /*!< for out circular linked list */
328 #define MGCP_ONHOOK 1
329 #define MGCP_OFFHOOK 2
334 struct mgcp_endpoint {
337 struct mgcp_subchannel *sub; /*!< Pointer to our current connection, channel and stuff */
338 char accountcode[AST_MAX_ACCOUNT_CODE];
339 char exten[AST_MAX_EXTENSION]; /*!< Extention where to start */
340 char context[AST_MAX_EXTENSION];
341 char language[MAX_LANGUAGE];
342 char cid_num[AST_MAX_EXTENSION]; /*!< Caller*ID number */
343 char cid_name[AST_MAX_EXTENSION]; /*!< Caller*ID name */
344 char lastcallerid[AST_MAX_EXTENSION]; /*!< Last Caller*ID */
345 char dtmf_buf[AST_MAX_EXTENSION]; /*!< place to collect digits be */
346 char call_forward[AST_MAX_EXTENSION]; /*!< Last Caller*ID */
347 char musicclass[MAX_MUSICCLASS];
348 char curtone[80]; /*!< Current tone */
349 char mailbox[AST_MAX_EXTENSION];
350 char parkinglot[AST_MAX_CONTEXT]; /*!< Parkinglot */
351 struct stasis_subscription *mwi_event_sub;
352 ast_group_t callgroup;
353 ast_group_t pickupgroup;
362 int dnd; /* How does this affect callwait? Do we just deny a mgcp_request if we're dnd? */
369 int hangupongateremove;
371 int slowsequence; /*!< MS: Sequence the endpoint as a whole */
373 int iseq; /*!< Not used? */
374 int lastout; /*!< tracking this on the subchannels. Is it needed here? */
375 int needdestroy; /*!< Not used? */
376 struct ast_format_cap *cap;
377 int nonCodecCapability;
379 int msgstate; /*!< voicemail message state */
383 char rqnt_ident[80]; /*!< request identifier */
384 struct mgcp_request *rqnt_queue; /*!< pending RQNT commands */
385 ast_mutex_t rqnt_queue_lock;
386 struct mgcp_request *cmd_queue; /*!< pending commands other than RQNT */
387 ast_mutex_t cmd_queue_lock;
388 int delme; /*!< needed for reload */
389 int needaudit; /*!< needed for reload */
390 struct ast_dsp *dsp; /*!< XXX Should there be a dsp/subchannel? XXX */
391 /* owner is tracked on the subchannels, and the *sub indicates whos in charge */
392 /* struct ast_channel *owner; */
393 /* struct ast_rtp *rtp; */
394 /* struct sockaddr_in tmpdest; */
395 /* message go the the endpoint and not the channel so they stay here */
396 struct ast_variable *chanvars; /*!< Variables to set for channel created by user */
397 struct mgcp_endpoint *next;
398 struct mgcp_gateway *parent;
401 static struct mgcp_gateway {
402 /* A gateway containing one or more endpoints */
404 int isnamedottedip; /*!< is the name FQDN or dotted ip */
405 struct sockaddr_in addr;
406 struct sockaddr_in defaddr;
407 struct in_addr ourip;
409 int expire; /*!< XXX Should we ever expire dynamic registrations? XXX */
410 struct mgcp_endpoint *endpoints;
417 /* Wildcard endpoint name */
419 struct mgcp_message *msgs; /*!< gw msg queue */
420 ast_mutex_t msgs_lock; /*!< queue lock */
421 int retransid; /*!< retrans timer id */
422 int delme; /*!< needed for reload */
424 struct mgcp_response *responses;
425 struct mgcp_gateway *next;
428 AST_MUTEX_DEFINE_STATIC(mgcp_reload_lock);
429 static int mgcp_reloading = 0;
431 /*! \brief gatelock: mutex for gateway/endpoint lists */
432 AST_MUTEX_DEFINE_STATIC(gatelock);
434 static int mgcpsock = -1;
436 static struct sockaddr_in bindaddr;
438 static void mgcp_set_owner(struct mgcp_subchannel *sub, struct ast_channel *chan);
439 static struct ast_frame *mgcp_read(struct ast_channel *ast);
440 static int transmit_response(struct mgcp_subchannel *sub, char *msg, struct mgcp_request *req, char *msgrest);
441 static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone);
442 static int transmit_modify_request(struct mgcp_subchannel *sub);
443 static int transmit_connect(struct mgcp_subchannel *sub);
444 static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername);
445 static int transmit_modify_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp, const struct ast_format_cap *codecs);
446 static int transmit_connection_del(struct mgcp_subchannel *sub);
447 static int transmit_audit_endpoint(struct mgcp_endpoint *p);
448 static void start_rtp(struct mgcp_subchannel *sub);
449 static void handle_response(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
450 int result, unsigned int ident, struct mgcp_request *resp);
451 static void dump_cmd_queues(struct mgcp_endpoint *p, struct mgcp_subchannel *sub);
452 static char *mgcp_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
453 static int reload_config(int reload);
455 static struct ast_channel *mgcp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *dest, int *cause);
456 static int mgcp_call(struct ast_channel *ast, const char *dest, int timeout);
457 static int mgcp_hangup(struct ast_channel *ast);
458 static int mgcp_answer(struct ast_channel *ast);
459 static struct ast_frame *mgcp_read(struct ast_channel *ast);
460 static int mgcp_write(struct ast_channel *ast, struct ast_frame *frame);
461 static int mgcp_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen);
462 static int mgcp_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
463 static int mgcp_senddigit_begin(struct ast_channel *ast, char digit);
464 static int mgcp_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration);
465 static int mgcp_devicestate(const char *data);
466 static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp, char *tone);
467 static int transmit_connect_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp);
468 static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v);
469 static int mgcp_alloc_pktcgate(struct mgcp_subchannel *sub);
470 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen);
471 static struct ast_variable *add_var(const char *buf, struct ast_variable *list);
472 static struct ast_variable *copy_vars(struct ast_variable *src);
474 static struct ast_channel_tech mgcp_tech = {
476 .description = tdesc,
477 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
478 .requester = mgcp_request,
479 .devicestate = mgcp_devicestate,
481 .hangup = mgcp_hangup,
482 .answer = mgcp_answer,
485 .indicate = mgcp_indicate,
487 .send_digit_begin = mgcp_senddigit_begin,
488 .send_digit_end = mgcp_senddigit_end,
489 .func_channel_read = acf_channel_read,
492 static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
494 /* This module does not handle MWI in an event-based manner. However, it
495 * subscribes to MWI for each mailbox that is configured so that the core
496 * knows that we care about it. Then, chan_mgcp will get the MWI from the
497 * event cache instead of checking the mailbox directly. */
500 static int has_voicemail(struct mgcp_endpoint *p)
503 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
505 msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(), p->mailbox);
507 struct ast_mwi_state *mwi_state = stasis_message_data(msg);
508 new_msgs = mwi_state->new_msgs;
510 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
516 static int unalloc_sub(struct mgcp_subchannel *sub)
518 struct mgcp_endpoint *p = sub->parent;
520 ast_log(LOG_WARNING, "Trying to unalloc the real channel %s@%s?!?\n", p->name, p->parent->name);
523 ast_debug(1, "Released sub %d of channel %s@%s\n", sub->id, p->name, p->parent->name);
525 mgcp_set_owner(sub, NULL);
526 if (!ast_strlen_zero(sub->cxident)) {
527 transmit_connection_del(sub);
529 sub->cxident[0] = '\0';
530 sub->callid[0] = '\0';
531 sub->cxmode = MGCP_CX_INACTIVE;
533 sub->alreadygone = 0;
534 memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
536 ast_rtp_instance_destroy(sub->rtp);
539 dump_cmd_queues(NULL, sub);
543 /* modified for new transport mechanism */
544 static int __mgcp_xmit(struct mgcp_gateway *gw, char *data, int len)
547 if (gw->addr.sin_addr.s_addr)
548 res=sendto(mgcpsock, data, len, 0, (struct sockaddr *)&gw->addr, sizeof(struct sockaddr_in));
550 res=sendto(mgcpsock, data, len, 0, (struct sockaddr *)&gw->defaddr, sizeof(struct sockaddr_in));
552 ast_log(LOG_WARNING, "mgcp_xmit returned %d: %s\n", res, strerror(errno));
557 static int resend_response(struct mgcp_subchannel *sub, struct mgcp_response *resp)
559 struct mgcp_endpoint *p = sub->parent;
561 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));
562 res = __mgcp_xmit(p->parent, resp->buf, resp->len);
568 static int send_response(struct mgcp_subchannel *sub, struct mgcp_request *req)
570 struct mgcp_endpoint *p = sub->parent;
572 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));
573 res = __mgcp_xmit(p->parent, req->data, req->len);
579 /* modified for new transport framework */
580 static void dump_queue(struct mgcp_gateway *gw, struct mgcp_endpoint *p)
582 struct mgcp_message *cur, *q = NULL, *w, *prev;
584 ast_mutex_lock(&gw->msgs_lock);
585 for (prev = NULL, cur = gw->msgs; cur; prev = cur, cur = cur->next) {
586 if (!p || cur->owner_ep == p) {
588 prev->next = cur->next;
590 gw->msgs = cur->next;
593 ast_log(LOG_NOTICE, "Removing message from %s transaction %u\n",
594 gw->name, cur->seqno);
605 ast_mutex_unlock(&gw->msgs_lock);
614 static void mgcp_queue_frame(struct mgcp_subchannel *sub, struct ast_frame *f)
618 if (!ast_channel_trylock(sub->owner)) {
619 ast_queue_frame(sub->owner, f);
620 ast_channel_unlock(sub->owner);
623 DEADLOCK_AVOIDANCE(&sub->lock);
631 static void mgcp_queue_hangup(struct mgcp_subchannel *sub)
635 if (!ast_channel_trylock(sub->owner)) {
636 ast_queue_hangup(sub->owner);
637 ast_channel_unlock(sub->owner);
640 DEADLOCK_AVOIDANCE(&sub->lock);
648 static void mgcp_queue_control(struct mgcp_subchannel *sub, int control)
650 struct ast_frame f = { AST_FRAME_CONTROL, { control } };
651 return mgcp_queue_frame(sub, &f);
654 static int retrans_pkt(const void *data)
656 struct mgcp_gateway *gw = (struct mgcp_gateway *)data;
657 struct mgcp_message *cur, *exq = NULL, *w, *prev;
660 /* find out expired msgs */
661 ast_mutex_lock(&gw->msgs_lock);
663 for (prev = NULL, cur = gw->msgs; cur; prev = cur, cur = cur->next) {
664 if (cur->retrans < MAX_RETRANS) {
666 ast_debug(1, "Retransmitting #%d transaction %u on [%s]\n",
667 cur->retrans, cur->seqno, gw->name);
668 __mgcp_xmit(gw, cur->buf, cur->len);
671 prev->next = cur->next;
673 gw->msgs = cur->next;
675 ast_log(LOG_WARNING, "Maximum retries exceeded for transaction %u on [%s]\n",
676 cur->seqno, gw->name);
695 ast_mutex_unlock(&gw->msgs_lock);
699 /* time-out transaction */
700 handle_response(cur->owner_ep, cur->owner_sub, 406, cur->seqno, NULL);
708 /* modified for the new transaction mechanism */
709 static int mgcp_postrequest(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
710 char *data, int len, unsigned int seqno)
712 struct mgcp_message *msg;
713 struct mgcp_message *cur;
714 struct mgcp_gateway *gw;
717 if (!(msg = ast_malloc(sizeof(*msg) + len))) {
720 if (!(gw = ((p && p->parent) ? p->parent : NULL))) {
725 msg->owner_sub = sub;
731 memcpy(msg->buf, data, msg->len);
733 ast_mutex_lock(&gw->msgs_lock);
734 for (cur = gw->msgs; cur && cur->next; cur = cur->next);
742 msg->expire = now.tv_sec * 1000 + now.tv_usec / 1000 + DEFAULT_RETRANS;
744 if (gw->retransid == -1)
745 gw->retransid = ast_sched_add(sched, DEFAULT_RETRANS, retrans_pkt, (void *)gw);
746 ast_mutex_unlock(&gw->msgs_lock);
747 __mgcp_xmit(gw, msg->buf, msg->len);
748 /* XXX Should schedule retransmission XXX */
752 /* modified for new transport */
753 static int send_request(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
754 struct mgcp_request *req, unsigned int seqno)
757 struct mgcp_request **queue, *q, *r, *t;
760 ast_debug(1, "Slow sequence is %d\n", p->slowsequence);
761 if (p->slowsequence) {
762 queue = &p->cmd_queue;
763 l = &p->cmd_queue_lock;
768 queue = &sub->cx_queue;
769 l = &sub->cx_queue_lock;
772 /* delete pending cx cmds */
774 if (!sub->parent->ncs) {
786 queue = &sub->cx_queue;
787 l = &sub->cx_queue_lock;
792 queue = &p->rqnt_queue;
793 l = &p->rqnt_queue_lock;
798 queue = &p->cmd_queue;
799 l = &p->cmd_queue_lock;
805 if (!(r = ast_malloc(sizeof(*r)))) {
806 ast_log(LOG_WARNING, "Cannot post MGCP request: insufficient memory\n");
810 memcpy(r, req, sizeof(*r));
813 ast_debug(1, "Posting Request:\n%s to %s:%d\n", req->data,
814 ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
816 res = mgcp_postrequest(p, sub, req->data, req->len, seqno);
818 ast_debug(1, "Queueing Request:\n%s to %s:%d\n", req->data,
819 ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
822 /* XXX find tail. We could also keep tail in the data struct for faster access */
823 for (t = *queue; t && t->next; t = t->next);
836 static int mgcp_call(struct ast_channel *ast, const char *dest, int timeout)
839 struct mgcp_endpoint *p;
840 struct mgcp_subchannel *sub;
842 const char *distinctive_ring = pbx_builtin_getvar_helper(ast, "ALERT_INFO");
844 ast_debug(3, "MGCP mgcp_call(%s)\n", ast_channel_name(ast));
845 sub = ast_channel_tech_pvt(ast);
848 ast_mutex_lock(&sub->lock);
849 switch (p->hookstate) {
851 if (!ast_strlen_zero(distinctive_ring)) {
852 snprintf(tone, sizeof(tone), "L/wt%s", distinctive_ring);
853 ast_debug(3, "MGCP distinctive callwait %s\n", tone);
855 ast_copy_string(tone, (p->ncs ? "L/wt1" : "L/wt"), sizeof(tone));
856 ast_debug(3, "MGCP normal callwait %s\n", tone);
861 if (!ast_strlen_zero(distinctive_ring)) {
862 snprintf(tone, sizeof(tone), "L/r%s", distinctive_ring);
863 ast_debug(3, "MGCP distinctive ring %s\n", tone);
865 ast_copy_string(tone, "L/rg", sizeof(tone));
866 ast_debug(3, "MGCP default ring\n");
871 if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
872 ast_log(LOG_WARNING, "mgcp_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
873 ast_mutex_unlock(&sub->lock);
879 sub->cxmode = MGCP_CX_RECVONLY;
880 ast_setstate(ast, AST_STATE_RINGING);
881 if (p->type == TYPE_LINE) {
885 transmit_modify_request(sub);
888 if (sub->next->owner && !ast_strlen_zero(sub->next->cxident) && !ast_strlen_zero(sub->next->callid)) {
889 /* try to prevent a callwait from disturbing the other connection */
890 sub->next->cxmode = MGCP_CX_RECVONLY;
891 transmit_modify_request(sub->next);
894 transmit_notify_request_with_callerid(sub, tone,
895 S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, ""),
896 S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, ""));
897 ast_setstate(ast, AST_STATE_RINGING);
899 if (sub->next->owner && !ast_strlen_zero(sub->next->cxident) && !ast_strlen_zero(sub->next->callid)) {
900 /* Put the connection back in sendrecv */
901 sub->next->cxmode = MGCP_CX_SENDRECV;
902 transmit_modify_request(sub->next);
905 ast_log(LOG_NOTICE, "Don't know how to dial on trunks yet\n");
908 ast_mutex_unlock(&sub->lock);
912 static int mgcp_hangup(struct ast_channel *ast)
914 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
915 struct mgcp_endpoint *p = sub->parent;
917 ast_debug(1, "mgcp_hangup(%s)\n", ast_channel_name(ast));
918 if (!ast_channel_tech_pvt(ast)) {
919 ast_debug(1, "Asked to hangup channel not connected\n");
922 if (strcmp(sub->magic, MGCP_SUBCHANNEL_MAGIC)) {
923 ast_debug(1, "Invalid magic. MGCP subchannel freed up already.\n");
926 ast_mutex_lock(&sub->lock);
927 ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s\n", ast_channel_name(ast), p->name, p->parent->name);
929 if ((p->dtmfmode & MGCP_DTMF_INBAND) && p->dsp) {
930 /* check whether other channel is active. */
931 if (!sub->next->owner) {
932 if (p->dtmfmode & MGCP_DTMF_HYBRID) {
933 p->dtmfmode &= ~MGCP_DTMF_INBAND;
935 ast_debug(2, "MGCP free dsp on %s@%s\n", p->name, p->parent->name);
936 ast_dsp_free(p->dsp);
941 mgcp_set_owner(sub, NULL);
943 /* for deleting gate */
944 if (p->pktcgatealloc && sub->gate) {
945 sub->gate->gate_open = NULL;
946 sub->gate->gate_remove = NULL;
947 sub->gate->got_dq_gi = NULL;
948 sub->gate->tech_pvt = NULL;
949 if (sub->gate->state == GATE_ALLOC_PROGRESS || sub->gate->state == GATE_ALLOCATED) {
950 ast_pktccops_gate_alloc(GATE_DEL, sub->gate, 0, 0, 0, 0, 0, 0, NULL, NULL);
952 sub->gate->deltimer = time(NULL) + 5;
957 if (!ast_strlen_zero(sub->cxident)) {
958 transmit_connection_del(sub);
960 sub->cxident[0] = '\0';
961 if ((sub == p->sub) && sub->next->owner) {
962 RAII_VAR(struct ast_channel *, bridged, ast_channel_bridge_peer(sub->next->owner), ast_channel_cleanup);
964 if (p->hookstate == MGCP_OFFHOOK) {
965 if (sub->next->owner && bridged) {
967 transmit_notify_request_with_callerid(p->sub, (p->ncs ? "L/wt1" : "L/wt"),
968 S_COR(ast_channel_caller(bridged)->id.number.valid, ast_channel_caller(bridged)->id.number.str, ""),
969 S_COR(ast_channel_caller(bridged)->id.name.valid, ast_channel_caller(bridged)->id.name.str, ""));
972 /* set our other connection as the primary and swith over to it */
974 p->sub->cxmode = MGCP_CX_RECVONLY;
975 transmit_modify_request(p->sub);
976 if (sub->next->owner && bridged) {
977 transmit_notify_request_with_callerid(p->sub, "L/rg",
978 S_COR(ast_channel_caller(bridged)->id.number.valid, ast_channel_caller(bridged)->id.number.str, ""),
979 S_COR(ast_channel_caller(bridged)->id.name.valid, ast_channel_caller(bridged)->id.name.str, ""));
983 } else if ((sub == p->sub->next) && p->hookstate == MGCP_OFFHOOK) {
984 transmit_notify_request(sub, p->ncs ? "" : "L/v");
985 } else if (p->hookstate == MGCP_OFFHOOK) {
986 transmit_notify_request(sub, "L/ro");
988 transmit_notify_request(sub, "");
991 ast_channel_tech_pvt_set(ast, NULL);
992 sub->alreadygone = 0;
994 sub->cxmode = MGCP_CX_INACTIVE;
995 sub->callid[0] = '\0';
997 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
999 /* Reset temporary destination */
1000 memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
1002 ast_rtp_instance_destroy(sub->rtp);
1006 ast_module_unref(ast_module_info->self);
1008 if ((p->hookstate == MGCP_ONHOOK) && (!sub->next->rtp)) {
1009 p->hidecallerid = 0;
1010 if (p->hascallwaiting && !p->callwaiting) {
1011 ast_verb(3, "Enabling call waiting on %s\n", ast_channel_name(ast));
1012 p->callwaiting = -1;
1014 if (has_voicemail(p)) {
1015 ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s set vmwi(+)\n",
1016 ast_channel_name(ast), p->name, p->parent->name);
1017 transmit_notify_request(sub, "L/vmwi(+)");
1019 ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s set vmwi(-)\n",
1020 ast_channel_name(ast), p->name, p->parent->name);
1021 transmit_notify_request(sub, "L/vmwi(-)");
1024 ast_mutex_unlock(&sub->lock);
1028 static char *handle_mgcp_show_endpoints(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1030 struct mgcp_gateway *mg;
1031 struct mgcp_endpoint *me;
1032 int hasendpoints = 0;
1033 struct ast_variable * v = NULL;
1037 e->command = "mgcp show endpoints";
1039 "Usage: mgcp show endpoints\n"
1040 " Lists all endpoints known to the MGCP (Media Gateway Control Protocol) subsystem.\n";
1047 return CLI_SHOWUSAGE;
1049 ast_mutex_lock(&gatelock);
1050 for (mg = gateways; mg; mg = mg->next) {
1051 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");
1052 for (me = mg->endpoints; me; me = me->next) {
1053 ast_cli(a->fd, " -- '%s@%s in '%s' is %s\n", me->name, mg->name, me->context, me->sub->owner ? "active" : "idle");
1055 ast_cli(a->fd, " Variables:\n");
1056 for (v = me->chanvars ; v ; v = v->next) {
1057 ast_cli(a->fd, " %s = '%s'\n", v->name, v->value);
1062 if (!hasendpoints) {
1063 ast_cli(a->fd, " << No Endpoints Defined >> ");
1066 ast_mutex_unlock(&gatelock);
1070 static char *handle_mgcp_audit_endpoint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1072 struct mgcp_gateway *mg;
1073 struct mgcp_endpoint *me;
1075 char *ename,*gname, *c;
1079 e->command = "mgcp audit endpoint";
1081 "Usage: mgcp audit endpoint <endpointid>\n"
1082 " Lists the capabilities of an endpoint in the MGCP (Media Gateway Control Protocol) subsystem.\n"
1083 " mgcp debug MUST be on to see the results of this command.\n";
1090 return CLI_SHOWUSAGE;
1093 return CLI_SHOWUSAGE;
1094 /* split the name into parts by null */
1095 ename = ast_strdupa(a->argv[3]);
1096 for (gname = ename; *gname; gname++) {
1097 if (*gname == '@') {
1103 if (gname[0] == '[') {
1106 if ((c = strrchr(gname, ']'))) {
1109 ast_mutex_lock(&gatelock);
1110 for (mg = gateways; mg; mg = mg->next) {
1111 if (!strcasecmp(mg->name, gname)) {
1112 for (me = mg->endpoints; me; me = me->next) {
1113 if (!strcasecmp(me->name, ename)) {
1115 transmit_audit_endpoint(me);
1125 ast_cli(a->fd, " << Could not find endpoint >> ");
1127 ast_mutex_unlock(&gatelock);
1131 static char *handle_mgcp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1135 e->command = "mgcp set debug {on|off}";
1137 "Usage: mgcp set debug {on|off}\n"
1138 " Enables/Disables dumping of MGCP packets for debugging purposes\n";
1144 if (a->argc != e->args)
1145 return CLI_SHOWUSAGE;
1147 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
1149 ast_cli(a->fd, "MGCP Debugging Enabled\n");
1150 } else if (!strncasecmp(a->argv[3], "off", 3)) {
1152 ast_cli(a->fd, "MGCP Debugging Disabled\n");
1154 return CLI_SHOWUSAGE;
1159 static struct ast_cli_entry cli_mgcp[] = {
1160 AST_CLI_DEFINE(handle_mgcp_audit_endpoint, "Audit specified MGCP endpoint"),
1161 AST_CLI_DEFINE(handle_mgcp_show_endpoints, "List defined MGCP endpoints"),
1162 AST_CLI_DEFINE(handle_mgcp_set_debug, "Enable/Disable MGCP debugging"),
1163 AST_CLI_DEFINE(mgcp_reload, "Reload MGCP configuration"),
1166 static int mgcp_answer(struct ast_channel *ast)
1169 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1170 struct mgcp_endpoint *p = sub->parent;
1172 ast_mutex_lock(&sub->lock);
1173 sub->cxmode = MGCP_CX_SENDRECV;
1177 transmit_modify_request(sub);
1179 ast_verb(3, "MGCP mgcp_answer(%s) on %s@%s-%d\n",
1180 ast_channel_name(ast), p->name, p->parent->name, sub->id);
1181 if (ast_channel_state(ast) != AST_STATE_UP) {
1182 ast_setstate(ast, AST_STATE_UP);
1183 ast_debug(1, "mgcp_answer(%s)\n", ast_channel_name(ast));
1184 transmit_notify_request(sub, "");
1185 transmit_modify_request(sub);
1187 ast_mutex_unlock(&sub->lock);
1191 static struct ast_frame *mgcp_rtp_read(struct mgcp_subchannel *sub)
1193 /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
1194 struct ast_frame *f;
1196 f = ast_rtp_instance_read(sub->rtp, 0);
1197 /* Don't send RFC2833 if we're not supposed to */
1198 if (f && (f->frametype == AST_FRAME_DTMF) && !(sub->parent->dtmfmode & MGCP_DTMF_RFC2833))
1199 return &ast_null_frame;
1201 /* We already hold the channel lock */
1202 if (f->frametype == AST_FRAME_VOICE) {
1203 if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(sub->owner), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
1204 struct ast_format_cap *caps;
1206 ast_debug(1, "Oooh, format changed to %s\n", ast_format_get_name(f->subclass.format));
1208 caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1210 ast_format_cap_append(caps, f->subclass.format, 0);
1211 ast_channel_nativeformats_set(sub->owner, caps);
1214 return &ast_null_frame;
1217 ast_set_read_format(sub->owner, ast_channel_readformat(sub->owner));
1218 ast_set_write_format(sub->owner, ast_channel_writeformat(sub->owner));
1220 /* Courtesy fearnor aka alex@pilosoft.com */
1221 if ((sub->parent->dtmfmode & MGCP_DTMF_INBAND) && (sub->parent->dsp)) {
1223 ast_log(LOG_NOTICE, "MGCP ast_dsp_process\n");
1225 f = ast_dsp_process(sub->owner, sub->parent->dsp, f);
1232 static void mgcp_set_owner(struct mgcp_subchannel *sub, struct ast_channel *chan)
1236 ast_rtp_instance_set_channel_id(sub->rtp, sub->owner ? ast_channel_uniqueid(chan) : "");
1240 static struct ast_frame *mgcp_read(struct ast_channel *ast)
1242 struct ast_frame *f;
1243 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1244 ast_mutex_lock(&sub->lock);
1245 f = mgcp_rtp_read(sub);
1246 ast_mutex_unlock(&sub->lock);
1250 static int mgcp_write(struct ast_channel *ast, struct ast_frame *frame)
1252 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1255 if (frame->frametype != AST_FRAME_VOICE) {
1256 if (frame->frametype == AST_FRAME_IMAGE)
1259 ast_log(LOG_WARNING, "Can't send %u type frames with MGCP write\n", frame->frametype);
1263 if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
1264 struct ast_str *cap_buf = ast_str_alloca(64);
1266 ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
1267 ast_format_get_name(frame->subclass.format),
1268 ast_format_cap_get_names(ast_channel_nativeformats(ast), &cap_buf),
1269 ast_format_get_name(ast_channel_readformat(ast)),
1270 ast_format_get_name(ast_channel_writeformat(ast)));
1275 ast_mutex_lock(&sub->lock);
1276 if (!sub->sdpsent && sub->gate) {
1277 if (sub->gate->state == GATE_ALLOCATED) {
1278 ast_debug(1, "GATE ALLOCATED, sending sdp\n");
1279 transmit_modify_with_sdp(sub, NULL, 0);
1282 if ((sub->parent->sub == sub) || !sub->parent->singlepath) {
1284 res = ast_rtp_instance_write(sub->rtp, frame);
1287 ast_mutex_unlock(&sub->lock);
1292 static int mgcp_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1294 struct mgcp_subchannel *sub = ast_channel_tech_pvt(newchan);
1296 ast_mutex_lock(&sub->lock);
1297 ast_log(LOG_NOTICE, "mgcp_fixup(%s, %s)\n", ast_channel_name(oldchan), ast_channel_name(newchan));
1298 if (sub->owner != oldchan) {
1299 ast_mutex_unlock(&sub->lock);
1300 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
1303 mgcp_set_owner(sub, newchan);
1304 ast_mutex_unlock(&sub->lock);
1308 static int mgcp_senddigit_begin(struct ast_channel *ast, char digit)
1310 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1311 struct mgcp_endpoint *p = sub->parent;
1314 ast_mutex_lock(&sub->lock);
1315 if (p->dtmfmode & MGCP_DTMF_INBAND || p->dtmfmode & MGCP_DTMF_HYBRID) {
1316 ast_debug(1, "Sending DTMF using inband/hybrid\n");
1317 res = -1; /* Let asterisk play inband indications */
1318 } else if (p->dtmfmode & MGCP_DTMF_RFC2833) {
1319 ast_debug(1, "Sending DTMF using RFC2833\n");
1320 ast_rtp_instance_dtmf_begin(sub->rtp, digit);
1322 ast_log(LOG_ERROR, "Don't know about DTMF_MODE %d\n", p->dtmfmode);
1324 ast_mutex_unlock(&sub->lock);
1329 static int mgcp_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
1331 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1332 struct mgcp_endpoint *p = sub->parent;
1336 ast_mutex_lock(&sub->lock);
1337 if (p->dtmfmode & MGCP_DTMF_INBAND || p->dtmfmode & MGCP_DTMF_HYBRID) {
1338 ast_debug(1, "Stopping DTMF using inband/hybrid\n");
1339 res = -1; /* Tell Asterisk to stop inband indications */
1340 } else if (p->dtmfmode & MGCP_DTMF_RFC2833) {
1341 ast_debug(1, "Stopping DTMF using RFC2833\n");
1342 if (sub->parent->ncs) {
1351 transmit_notify_request(sub, tmp);
1352 ast_rtp_instance_dtmf_end(sub->rtp, digit);
1354 ast_log(LOG_ERROR, "Don't know about DTMF_MODE %d\n", p->dtmfmode);
1356 ast_mutex_unlock(&sub->lock);
1362 * \brief mgcp_devicestate: channel callback for device status monitoring
1363 * \param data tech/resource name of MGCP device to query
1365 * Callback for device state management in channel subsystem
1366 * to obtain device status (up/down) of a specific MGCP endpoint
1368 * \return device status result (from devicestate.h) AST_DEVICE_INVALID (not available) or AST_DEVICE_UNKNOWN (available but unknown state)
1370 static int mgcp_devicestate(const char *data)
1372 struct mgcp_gateway *g;
1373 struct mgcp_endpoint *e = NULL;
1374 char *tmp, *endpt, *gw;
1375 int ret = AST_DEVICE_INVALID;
1377 endpt = ast_strdupa(data);
1378 if ((tmp = strchr(endpt, '@'))) {
1384 ast_mutex_lock(&gatelock);
1385 for (g = gateways; g; g = g->next) {
1386 if (strcasecmp(g->name, gw) == 0) {
1395 for (; e; e = e->next) {
1396 if (strcasecmp(e->name, endpt) == 0) {
1405 * As long as the gateway/endpoint is valid, we'll
1406 * assume that the device is available and its state
1409 ret = AST_DEVICE_UNKNOWN;
1412 ast_mutex_unlock(&gatelock);
1416 static char *control2str(int ind) {
1418 case AST_CONTROL_HANGUP:
1419 return "Other end has hungup";
1420 case AST_CONTROL_RING:
1421 return "Local ring";
1422 case AST_CONTROL_RINGING:
1423 return "Remote end is ringing";
1424 case AST_CONTROL_ANSWER:
1425 return "Remote end has answered";
1426 case AST_CONTROL_BUSY:
1427 return "Remote end is busy";
1428 case AST_CONTROL_TAKEOFFHOOK:
1429 return "Make it go off hook";
1430 case AST_CONTROL_OFFHOOK:
1431 return "Line is off hook";
1432 case AST_CONTROL_CONGESTION:
1433 return "Congestion (circuits busy)";
1434 case AST_CONTROL_FLASH:
1435 return "Flash hook";
1436 case AST_CONTROL_WINK:
1438 case AST_CONTROL_OPTION:
1439 return "Set a low-level option";
1440 case AST_CONTROL_RADIO_KEY:
1442 case AST_CONTROL_RADIO_UNKEY:
1443 return "Un-Key Radio";
1448 static int mgcp_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
1450 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1453 ast_debug(3, "MGCP asked to indicate %d '%s' condition on channel %s\n",
1454 ind, control2str(ind), ast_channel_name(ast));
1455 ast_mutex_lock(&sub->lock);
1457 case AST_CONTROL_RINGING:
1458 #ifdef DLINK_BUGGY_FIRMWARE
1459 transmit_notify_request(sub, "rt");
1461 if (!sub->sdpsent) { /* will hide the inband progress!!! */
1462 transmit_notify_request(sub, sub->parent->ncs ? "L/rt" : "G/rt");
1466 case AST_CONTROL_BUSY:
1467 transmit_notify_request(sub, "L/bz");
1469 case AST_CONTROL_INCOMPLETE:
1470 /* We do not currently support resetting of the Interdigit Timer, so treat
1471 * Incomplete control frames as a congestion response
1473 case AST_CONTROL_CONGESTION:
1474 transmit_notify_request(sub, sub->parent->ncs ? "L/cg" : "G/cg");
1476 case AST_CONTROL_HOLD:
1477 ast_moh_start(ast, data, NULL);
1479 case AST_CONTROL_UNHOLD:
1482 case AST_CONTROL_SRCUPDATE:
1483 ast_rtp_instance_update_source(sub->rtp);
1485 case AST_CONTROL_SRCCHANGE:
1486 ast_rtp_instance_change_source(sub->rtp);
1488 case AST_CONTROL_PROGRESS:
1489 case AST_CONTROL_PROCEEDING:
1490 transmit_modify_request(sub);
1492 transmit_notify_request(sub, "");
1495 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
1497 case AST_CONTROL_PVT_CAUSE_CODE:
1500 ast_mutex_unlock(&sub->lock);
1504 static struct ast_channel *mgcp_new(struct mgcp_subchannel *sub, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
1506 struct ast_format_cap *caps = NULL;
1507 struct ast_channel *tmp;
1508 struct ast_variable *v = NULL;
1509 struct mgcp_endpoint *i = sub->parent;
1510 struct ast_format *tmpfmt;
1512 caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1514 ast_log(LOG_ERROR, "Format capabilities could not be created\n");
1517 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, assignedids, requestor, i->amaflags, "MGCP/%s@%s-%d", i->name, i->parent->name, sub->id);
1519 ast_log(LOG_WARNING, "Channel could not be created\n");
1524 ast_channel_stage_snapshot(tmp);
1525 ast_channel_tech_set(tmp, &mgcp_tech);
1526 if (ast_format_cap_count(i->cap)) {
1527 ast_format_cap_append_from_cap(caps, i->cap, AST_MEDIA_TYPE_UNKNOWN);
1529 ast_format_cap_append_from_cap(caps, global_capability, AST_MEDIA_TYPE_UNKNOWN);
1531 ast_channel_nativeformats_set(tmp, caps);
1534 ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(sub->rtp, 0));
1536 if (i->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID)) {
1537 i->dsp = ast_dsp_new();
1538 ast_dsp_set_features(i->dsp, DSP_FEATURE_DIGIT_DETECT);
1539 /* this is to prevent clipping of dtmf tones during dsp processing */
1540 ast_dsp_set_digitmode(i->dsp, DSP_DIGITMODE_NOQUELCH);
1544 if (state == AST_STATE_RING) {
1545 ast_channel_rings_set(tmp, 1);
1548 tmpfmt = ast_format_cap_get_format(ast_channel_nativeformats(tmp), 0);
1549 ast_channel_set_writeformat(tmp, tmpfmt);
1550 ast_channel_set_rawwriteformat(tmp, tmpfmt);
1551 ast_channel_set_readformat(tmp, tmpfmt);
1552 ast_channel_set_rawreadformat(tmp, tmpfmt);
1553 ao2_ref(tmpfmt, -1);
1554 ast_channel_tech_pvt_set(tmp, sub);
1555 if (!ast_strlen_zero(i->language))
1556 ast_channel_language_set(tmp, i->language);
1557 if (!ast_strlen_zero(i->accountcode))
1558 ast_channel_accountcode_set(tmp, i->accountcode);
1560 ast_channel_amaflags_set(tmp, i->amaflags);
1561 mgcp_set_owner(sub, tmp);
1562 ast_module_ref(ast_module_info->self);
1563 ast_channel_callgroup_set(tmp, i->callgroup);
1564 ast_channel_pickupgroup_set(tmp, i->pickupgroup);
1565 ast_channel_call_forward_set(tmp, i->call_forward);
1566 ast_channel_context_set(tmp, i->context);
1567 ast_channel_exten_set(tmp, i->exten);
1568 /* Don't use ast_set_callerid() here because it will
1569 * generate a needless NewCallerID event */
1570 if (!ast_strlen_zero(i->cid_num)) {
1571 ast_channel_caller(tmp)->ani.number.valid = 1;
1572 ast_channel_caller(tmp)->ani.number.str = ast_strdup(i->cid_num);
1576 ast_channel_adsicpe_set(tmp, AST_ADSI_UNAVAILABLE);
1578 ast_channel_priority_set(tmp, 1);
1580 /* Set channel variables for this call from configuration */
1581 for (v = i->chanvars ; v ; v = v->next) {
1582 char valuebuf[1024];
1583 pbx_builtin_setvar_helper(tmp, v->name, ast_get_encoded_str(v->value, valuebuf, sizeof(valuebuf)));
1587 ast_jb_configure(tmp, &global_jbconf);
1590 ast_channel_stage_snapshot_done(tmp);
1591 ast_channel_unlock(tmp);
1593 if (state != AST_STATE_DOWN) {
1594 if (ast_pbx_start(tmp)) {
1595 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
1600 ast_verb(3, "MGCP mgcp_new(%s) created in state: %s\n",
1601 ast_channel_name(tmp), ast_state2str(state));
1606 static char *get_sdp_by_line(char* line, char *name, int nameLen)
1608 if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=') {
1609 char *r = line + nameLen + 1;
1610 while (*r && (*r < 33)) ++r;
1616 static char *get_sdp(struct mgcp_request *req, char *name)
1619 int len = strlen(name);
1622 for (x = 0; x < req->lines; x++) {
1623 r = get_sdp_by_line(req->line[x], name, len);
1624 if (r[0] != '\0') return r;
1629 static void sdpLineNum_iterator_init(int *iterator)
1634 static char *get_sdp_iterate(int* iterator, struct mgcp_request *req, char *name)
1636 int len = strlen(name);
1638 while (*iterator < req->lines) {
1639 r = get_sdp_by_line(req->line[(*iterator)++], name, len);
1640 if (r[0] != '\0') return r;
1645 static char *__get_header(struct mgcp_request *req, char *name, int *start, char *def)
1648 int len = strlen(name);
1650 for (x = *start; x < req->headers; x++) {
1651 if (!strncasecmp(req->header[x], name, len) &&
1652 (req->header[x][len] == ':')) {
1653 r = req->header[x] + len + 1;
1654 while (*r && (*r < 33)) {
1661 /* Don't return NULL, so get_header is always a valid pointer */
1665 static char *get_header(struct mgcp_request *req, char *name)
1668 return __get_header(req, name, &start, "");
1671 /*! \brief get_csv: (SC:) get comma separated value */
1672 static char *get_csv(char *c, int *len, char **next)
1676 *next = NULL, *len = 0;
1677 if (!c) return NULL;
1679 while (*c && (*c < 33 || *c == ',')) {
1684 while (*c && (*c >= 33 && *c != ',')) {
1690 s = NULL, *next = NULL;
1696 static struct mgcp_gateway *find_realtime_gw(char *name, char *at, struct sockaddr_in *sin)
1698 struct mgcp_gateway *g = NULL;
1699 struct ast_variable *mgcpgwconfig = NULL;
1700 struct ast_variable *gwv, *epname = NULL;
1701 struct mgcp_endpoint *e;
1705 ast_debug(1, "*** find Realtime MGCPGW\n");
1707 if (!(i = ast_check_realtime("mgcpgw")) || !(j = ast_check_realtime("mgcpep"))) {
1711 if (ast_strlen_zero(at)) {
1712 ast_debug(1, "null gw name\n");
1716 if (!(mgcpgwconfig = ast_load_realtime("mgcpgw", "name", at, NULL))) {
1721 * \note This is a fairly odd way of instantiating lines. Instead of each
1722 * line created by virtue of being in the database (and loaded via
1723 * ast_load_realtime_multientry), this code forces a specific order with a
1724 * "lines" entry in the "mgcpgw" record. This has benefits, because as with
1725 * chan_dahdi, values are inherited across definitions. The downside is
1726 * that it's not as clear what the values will be simply by looking at a
1727 * single row in the database, and it's probable that the sanest configuration
1728 * should have the first column in the "mgcpep" table be "clearvars", with a
1729 * static value of "all", if any variables are set at all. It may be worth
1730 * making this assumption explicit in the code in the future, and then just
1731 * using ast_load_realtime_multientry for the "mgcpep" records.
1734 for (gwv = mgcpgwconfig; gwv; gwv = gwv->next) {
1735 if (!strcasecmp(gwv->name, "lines")) {
1736 ast_copy_string(lines, gwv->value, sizeof(lines));
1740 /* Position gwv at the end of the list */
1741 for (gwv = gwv && gwv->next ? gwv : mgcpgwconfig; gwv->next; gwv = gwv->next);
1743 if (!ast_strlen_zero(lines)) {
1744 AST_DECLARE_APP_ARGS(args,
1745 AST_APP_ARG(line)[100];
1747 AST_STANDARD_APP_ARGS(args, lines);
1748 for (i = 0; i < args.argc; i++) {
1749 gwv->next = ast_load_realtime("mgcpep", "name", at, "line", args.line[i], NULL);
1751 /* Remove "line" AND position gwv at the end of the list. */
1752 for (epname = NULL; gwv->next; gwv = gwv->next) {
1753 if (!strcasecmp(gwv->next->name, "line")) {
1754 /* Remove it from the list */
1756 gwv->next = gwv->next->next;
1759 /* Since "line" instantiates the configuration, we have to move it to the end. */
1762 epname->next = NULL;
1767 for (gwv = mgcpgwconfig; gwv; gwv = gwv->next) {
1768 ast_debug(1, "MGCP Realtime var: %s => %s\n", gwv->name, gwv->value);
1772 g = build_gateway(at, mgcpgwconfig);
1773 ast_variables_destroy(mgcpgwconfig);
1779 for (e = g->endpoints; e; e = e->next) {
1780 transmit_audit_endpoint(e);
1787 static struct mgcp_subchannel *find_subchannel_and_lock(char *name, int msgid, struct sockaddr_in *sin)
1789 struct mgcp_endpoint *p = NULL;
1790 struct mgcp_subchannel *sub = NULL;
1791 struct mgcp_gateway *g;
1793 char *at = NULL, *c;
1796 ast_copy_string(tmp, name, sizeof(tmp));
1797 at = strchr(tmp, '@');
1799 ast_log(LOG_NOTICE, "Endpoint '%s' has no at sign!\n", name);
1804 ast_mutex_lock(&gatelock);
1805 if (at && (at[0] == '[')) {
1807 c = strrchr(at, ']');
1812 for (g = gateways ? gateways : find_realtime_gw(name, at, sin); g; g = g->next ? g->next : find_realtime_gw(name, at, sin)) {
1813 if ((!name || !strcasecmp(g->name, at)) &&
1814 (sin || g->addr.sin_addr.s_addr || g->defaddr.sin_addr.s_addr)) {
1815 /* Found the gateway. If it's dynamic, save it's address -- now for the endpoint */
1816 if (sin && g->dynamic && name) {
1817 if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) ||
1818 (g->addr.sin_port != sin->sin_port)) {
1819 memcpy(&g->addr, sin, sizeof(g->addr));
1821 struct ast_sockaddr tmp1, tmp2;
1822 struct sockaddr_in tmp3 = {0,};
1824 tmp3.sin_addr = g->ourip;
1825 ast_sockaddr_from_sin(&tmp1, &g->addr);
1826 ast_sockaddr_from_sin(&tmp2, &tmp3);
1827 if (ast_ouraddrfor(&tmp1, &tmp2)) {
1828 memcpy(&g->ourip, &__ourip, sizeof(g->ourip));
1830 ast_sockaddr_to_sin(&tmp2, &tmp3);
1831 g->ourip = tmp3.sin_addr;
1833 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));
1835 /* not dynamic, check if the name matches */
1837 if (strcasecmp(g->name, at)) {
1840 /* not dynamic, no name, check if the addr matches */
1841 } else if (!name && sin) {
1842 if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) ||
1843 (g->addr.sin_port != sin->sin_port)) {
1849 for (p = g->endpoints; p; p = p->next) {
1850 ast_debug(1, "Searching on %s@%s for subchannel\n", p->name, g->name);
1855 } else if (name && !strcasecmp(p->name, tmp)) {
1856 ast_debug(1, "Coundn't determine subchannel, assuming current master %s@%s-%d\n",
1857 p->name, g->name, p->sub->id);
1864 ast_mutex_lock(&sub->lock);
1869 ast_mutex_unlock(&gatelock);
1873 ast_log(LOG_NOTICE, "Endpoint '%s' not found on gateway '%s'\n", tmp, at);
1875 ast_log(LOG_NOTICE, "Gateway '%s' (and thus its endpoint '%s') does not exist\n", at, tmp);
1882 static void parse(struct mgcp_request *req)
1884 /* Divide fields by NULL's */
1889 /* First header starts immediately */
1893 /* We've got a new header */
1895 ast_debug(3, "Header: %s (%d)\n", req->header[f], (int) strlen(req->header[f]));
1896 if (ast_strlen_zero(req->header[f])) {
1897 /* Line by itself means we're now in content */
1901 if (f >= MGCP_MAX_HEADERS - 1) {
1902 ast_log(LOG_WARNING, "Too many MGCP headers...\n");
1906 req->header[f] = c + 1;
1907 } else if (*c == '\r') {
1908 /* Ignore but eliminate \r's */
1912 /* Check for last header */
1913 if (!ast_strlen_zero(req->header[f])) {
1917 /* Now we process any mime content */
1922 /* We've got a new line */
1924 ast_debug(3, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
1925 if (f >= MGCP_MAX_LINES - 1) {
1926 ast_log(LOG_WARNING, "Too many SDP lines...\n");
1930 req->line[f] = c + 1;
1931 } else if (*c == '\r') {
1932 /* Ignore and eliminate \r's */
1936 /* Check for last line */
1937 if (!ast_strlen_zero(req->line[f])) {
1941 /* Parse up the initial header */
1943 while (*c && *c < 33) c++;
1944 /* First the verb */
1946 while (*c && (*c > 32)) c++;
1950 while (*c && (*c < 33)) c++;
1951 req->identifier = c;
1952 while (*c && (*c > 32)) c++;
1956 while (*c && (*c < 33)) c++;
1958 while (*c && (*c > 32)) c++;
1962 while (*c && (*c < 33)) c++;
1964 while (*c && (*c > 32)) c++;
1965 while (*c && (*c < 33)) c++;
1966 while (*c && (*c > 32)) c++;
1972 ast_debug(1, "Verb: '%s', Identifier: '%s', Endpoint: '%s', Version: '%s'\n",
1973 req->verb, req->identifier, req->endpoint, req->version);
1974 ast_debug(1, "%d headers, %d lines\n", req->headers, req->lines);
1976 ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
1980 static int process_sdp(struct mgcp_subchannel *sub, struct mgcp_request *req)
1988 struct ast_format_cap *peercap;
1989 int peerNonCodecCapability;
1990 struct sockaddr_in sin;
1991 struct ast_sockaddr sin_tmp;
1993 struct ast_hostent ahp; struct hostent *hp;
1994 int codec, codec_count=0;
1996 struct mgcp_endpoint *p = sub->parent;
1997 struct ast_str *global_buf = ast_str_alloca(64);
1998 struct ast_str *peer_buf = ast_str_alloca(64);
1999 struct ast_str *pvt_buf = ast_str_alloca(64);
2001 /* Get codec and RTP info from SDP */
2002 m = get_sdp(req, "m");
2003 c = get_sdp(req, "c");
2004 if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
2005 ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
2008 if (sscanf(c, "IN IP4 %256s", host) != 1) {
2009 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
2012 /* XXX This could block for a long time, and block the main thread! XXX */
2013 hp = ast_gethostbyname(host, &ahp);
2015 ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
2018 if (sscanf(m, "audio %30d RTP/AVP %n", &portno, &len) != 1 || !len) {
2019 ast_log(LOG_WARNING, "Malformed media stream descriptor: %s\n", m);
2022 sin.sin_family = AF_INET;
2023 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
2024 sin.sin_port = htons(portno);
2025 ast_sockaddr_from_sin(&sin_tmp, &sin);
2026 ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
2027 ast_debug(3, "Peer RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
2028 /* Scan through the RTP payload types specified in a "m=" line: */
2029 ast_rtp_codecs_payloads_clear(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp);
2030 codecs = ast_strdupa(m + len);
2031 while (!ast_strlen_zero(codecs)) {
2032 if (sscanf(codecs, "%30d%n", &codec, &len) != 1) {
2036 ast_log(LOG_WARNING, "Error in codec string '%s' at '%s'\n", m, codecs);
2039 ast_rtp_codecs_payloads_set_m_type(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp, codec);
2044 /* Next, scan through each "a=rtpmap:" line, noting each */
2045 /* specified RTP payload type (with corresponding MIME subtype): */
2046 sdpLineNum_iterator_init(&iterator);
2047 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
2048 char* mimeSubtype = ast_strdupa(a); /* ensures we have enough space */
2049 if (sscanf(a, "rtpmap: %30d %127[^/]/", &codec, mimeSubtype) != 2)
2051 /* Note: should really look at the 'freq' and '#chans' params too */
2052 ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp, codec, "audio", mimeSubtype, 0);
2055 /* Now gather all of the codecs that were asked for: */
2056 if (!(peercap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
2059 ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(sub->rtp), peercap, &peerNonCodecCapability);
2060 ast_format_cap_get_compatible(global_capability, peercap, p->cap);
2061 ast_debug(1, "Capabilities: us - %s, them - %s, combined - %s\n",
2062 ast_format_cap_get_names(global_capability, &global_buf),
2063 ast_format_cap_get_names(peercap, &peer_buf),
2064 ast_format_cap_get_names(p->cap, &pvt_buf));
2065 ao2_ref(peercap, -1);
2067 ast_debug(1, "Non-codec capabilities: us - %d, them - %d, combined - %d\n",
2068 nonCodecCapability, peerNonCodecCapability, p->nonCodecCapability);
2069 if (!ast_format_cap_count(p->cap)) {
2070 ast_log(LOG_WARNING, "No compatible codecs!\n");
2076 static int add_header(struct mgcp_request *req, const char *var, const char *value)
2078 if (req->len >= sizeof(req->data) - 4) {
2079 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
2083 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
2086 req->header[req->headers] = req->data + req->len;
2087 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s: %s\r\n", var, value);
2088 req->len += strlen(req->header[req->headers]);
2089 if (req->headers < MGCP_MAX_HEADERS) {
2092 ast_log(LOG_WARNING, "Out of header space\n");
2098 static int add_line(struct mgcp_request *req, char *line)
2100 if (req->len >= sizeof(req->data) - 4) {
2101 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
2105 /* Add extra empty return */
2106 ast_copy_string(req->data + req->len, "\r\n", sizeof(req->data) - req->len);
2107 req->len += strlen(req->data + req->len);
2109 req->line[req->lines] = req->data + req->len;
2110 snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
2111 req->len += strlen(req->line[req->lines]);
2112 if (req->lines < MGCP_MAX_LINES) {
2115 ast_log(LOG_WARNING, "Out of line space\n");
2121 static int init_resp(struct mgcp_request *req, char *resp, struct mgcp_request *orig, char *resprest)
2123 /* Initialize a response */
2124 if (req->headers || req->len) {
2125 ast_log(LOG_WARNING, "Request already initialized?!?\n");
2128 req->header[req->headers] = req->data + req->len;
2129 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %s %s\r\n", resp, orig->identifier, resprest);
2130 req->len += strlen(req->header[req->headers]);
2131 if (req->headers < MGCP_MAX_HEADERS) {
2134 ast_log(LOG_WARNING, "Out of header space\n");
2139 static int init_req(struct mgcp_endpoint *p, struct mgcp_request *req, char *verb, unsigned int oseq)
2141 /* Initialize a response */
2142 if (req->headers || req->len) {
2143 ast_log(LOG_WARNING, "Request already initialized?!?\n");
2146 req->header[req->headers] = req->data + req->len;
2147 /* check if we need brackets around the gw name */
2148 if (p->parent->isnamedottedip) {
2149 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %u %s@[%s] MGCP 1.0%s\r\n", verb, oseq, p->name, p->parent->name, p->ncs ? " NCS 1.0" : "");
2151 + snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %u %s@%s MGCP 1.0%s\r\n", verb, oseq, p->name, p->parent->name, p->ncs ? " NCS 1.0" : "");
2153 req->len += strlen(req->header[req->headers]);
2154 if (req->headers < MGCP_MAX_HEADERS) {
2157 ast_log(LOG_WARNING, "Out of header space\n");
2163 static int respprep(struct mgcp_request *resp, struct mgcp_endpoint *p, char *msg, struct mgcp_request *req, char *msgrest)
2165 memset(resp, 0, sizeof(*resp));
2166 init_resp(resp, msg, req, msgrest);
2170 static int reqprep(struct mgcp_request *req, struct mgcp_endpoint *p, char *verb)
2173 memset(req, 0, sizeof(struct mgcp_request));
2174 ast_mutex_lock(&oseq_lock);
2176 if (oseq_global > 999999999) {
2180 ast_mutex_unlock(&oseq_lock);
2181 init_req(p, req, verb, oseq);
2185 static int transmit_response(struct mgcp_subchannel *sub, char *msg, struct mgcp_request *req, char *msgrest)
2187 struct mgcp_request resp;
2188 struct mgcp_endpoint *p = sub->parent;
2189 struct mgcp_response *mgr;
2195 respprep(&resp, p, msg, req, msgrest);
2196 if (!(mgr = ast_calloc(1, sizeof(*mgr) + resp.len + 1))) {
2197 return send_response(sub, &resp);
2199 /* Store MGCP response in case we have to retransmit */
2200 sscanf(req->identifier, "%30d", &mgr->seqno);
2201 time(&mgr->whensent);
2202 mgr->len = resp.len;
2203 memcpy(mgr->buf, resp.data, resp.len);
2204 mgr->buf[resp.len] = '\0';
2205 mgr->next = p->parent->responses;
2206 p->parent->responses = mgr;
2208 return send_response(sub, &resp);
2212 static int add_sdp(struct mgcp_request *resp, struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp)
2217 struct sockaddr_in sin;
2218 struct ast_sockaddr sin_tmp;
2227 struct sockaddr_in dest = { 0, };
2228 struct ast_sockaddr dest_tmp;
2229 struct mgcp_endpoint *p = sub->parent;
2230 /* XXX We break with the "recommendation" and send our IP, in order that our
2231 peer doesn't have to ast_gethostbyname() us XXX */
2234 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
2237 ast_rtp_instance_get_local_address(sub->rtp, &sin_tmp);
2238 ast_sockaddr_to_sin(&sin_tmp, &sin);
2240 ast_rtp_instance_get_remote_address(sub->rtp, &dest_tmp);
2241 ast_sockaddr_to_sin(&dest_tmp, &dest);
2243 if (sub->tmpdest.sin_addr.s_addr) {
2244 dest.sin_addr = sub->tmpdest.sin_addr;
2245 dest.sin_port = sub->tmpdest.sin_port;
2246 /* Reset temporary destination */
2247 memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
2249 dest.sin_addr = p->parent->ourip;
2250 dest.sin_port = sin.sin_port;
2253 ast_debug(1, "We're at %s port %d\n", ast_inet_ntoa(p->parent->ourip), ntohs(sin.sin_port));
2254 ast_copy_string(v, "v=0\r\n", sizeof(v));
2255 snprintf(o, sizeof(o), "o=root %d %d IN IP4 %s\r\n", (int)getpid(), (int)getpid(), ast_inet_ntoa(dest.sin_addr));
2256 ast_copy_string(s, "s=session\r\n", sizeof(s));
2257 snprintf(c, sizeof(c), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
2258 ast_copy_string(t, "t=0 0\r\n", sizeof(t));
2259 snprintf(m, sizeof(m), "m=audio %d RTP/AVP", ntohs(dest.sin_port));
2261 for (x = 0; x < ast_format_cap_count(p->cap); x++) {
2262 struct ast_format *format = ast_format_cap_get_format(p->cap, x);
2264 if (ast_format_get_type(format) != AST_MEDIA_TYPE_AUDIO) {
2265 ao2_ref(format, -1);
2269 ast_debug(1, "Answering with capability %s\n", ast_format_get_name(format));
2270 codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 1, format, 0);
2272 snprintf(costr, sizeof(costr), " %d", codec);
2273 strncat(m, costr, sizeof(m) - strlen(m) - 1);
2274 snprintf(costr, sizeof(costr), "a=rtpmap:%d %s/8000\r\n", codec, ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
2275 strncat(a, costr, sizeof(a) - strlen(a) - 1);
2278 ao2_ref(format, -1);
2281 for (x = 1LL; x <= AST_RTP_MAX; x <<= 1) {
2282 if (p->nonCodecCapability & x) {
2283 ast_debug(1, "Answering with non-codec capability %d\n", (int) x);
2284 codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 0, NULL, x);
2286 snprintf(costr, sizeof(costr), " %d", codec);
2287 strncat(m, costr, sizeof(m) - strlen(m) - 1);
2288 snprintf(costr, sizeof(costr), "a=rtpmap:%d %s/8000\r\n", codec, ast_rtp_lookup_mime_subtype2(0, NULL, x, 0));
2289 strncat(a, costr, sizeof(a) - strlen(a) - 1);
2290 if (x == AST_RTP_DTMF) {
2291 /* Indicate we support DTMF... Not sure about 16,
2292 but MSN supports it so dang it, we will too... */
2293 snprintf(costr, sizeof costr, "a=fmtp:%d 0-16\r\n", codec);
2294 strncat(a, costr, sizeof(a) - strlen(a) - 1);
2299 strncat(m, "\r\n", sizeof(m) - strlen(m) - 1);
2300 len = strlen(v) + strlen(s) + strlen(o) + strlen(c) + strlen(t) + strlen(m) + strlen(a);
2301 snprintf(costr, sizeof(costr), "%d", len);
2312 static int transmit_modify_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp, const struct ast_format_cap *codecs)
2314 struct mgcp_request resp;
2317 struct mgcp_endpoint *p = sub->parent;
2319 struct ast_sockaddr sub_tmpdest_tmp;
2322 if (ast_strlen_zero(sub->cxident) && rtp) {
2323 /* We don't have a CXident yet, store the destination and
2325 ast_rtp_instance_get_remote_address(rtp, &sub_tmpdest_tmp);
2326 ast_sockaddr_to_sin(&sub_tmpdest_tmp, &sub->tmpdest);
2329 ast_copy_string(local, "e:on, s:off, p:20", sizeof(local));
2331 for (i = 0; i < ast_format_cap_count(p->cap); i++) {
2332 struct ast_format *format = ast_format_cap_get_format(p->cap, i);
2334 if (ast_format_get_type(format) != AST_MEDIA_TYPE_AUDIO) {
2335 ao2_ref(format, -1);
2339 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
2340 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2342 ao2_ref(format, -1);
2346 if (sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) {
2347 snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
2348 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2352 ast_debug(1, "Waiting for opened gate...\n");
2359 oseq = reqprep(&resp, p, "MDCX");
2360 add_header(&resp, "C", sub->callid);
2361 add_header(&resp, "L", local);
2362 add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
2363 /* X header should not be sent. kept for compatibility */
2364 add_header(&resp, "X", sub->txident);
2365 add_header(&resp, "I", sub->cxident);
2366 /*add_header(&resp, "S", "");*/
2367 add_sdp(&resp, sub, rtp);
2368 /* fill in new fields */
2369 resp.cmd = MGCP_CMD_MDCX;
2371 return send_request(p, sub, &resp, oseq);
2374 static int transmit_connect_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp)
2376 struct mgcp_request resp;
2380 struct mgcp_endpoint *p = sub->parent;
2383 ast_debug(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n",
2384 p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2386 ast_copy_string(local, "e:on, s:off, p:20", sizeof(local));
2388 for (i = 0; i < ast_format_cap_count(p->cap); i++) {
2389 struct ast_format *format = ast_format_cap_get_format(p->cap, i);
2391 if (ast_format_get_type(format) != AST_MEDIA_TYPE_AUDIO) {
2392 ao2_ref(format, -1);
2396 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
2397 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2399 ao2_ref(format, -1);
2403 if(sub->gate->state == GATE_ALLOCATED) {
2404 snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
2405 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2409 oseq = reqprep(&resp, p, "CRCX");
2410 add_header(&resp, "C", sub->callid);
2411 add_header(&resp, "L", local);
2412 add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
2413 /* X header should not be sent. kept for compatibility */
2414 add_header(&resp, "X", sub->txident);
2415 /*add_header(&resp, "S", "");*/
2416 add_sdp(&resp, sub, rtp);
2417 /* fill in new fields */
2418 resp.cmd = MGCP_CMD_CRCX;
2420 return send_request(p, sub, &resp, oseq);
2423 static int mgcp_pktcgate_remove(struct cops_gate *gate)
2425 struct mgcp_subchannel *sub = gate->tech_pvt;
2431 ast_mutex_lock(&sub->lock);
2432 ast_debug(1, "Pktc: gate 0x%x deleted\n", gate->gateid);
2433 if (sub->gate->state != GATE_CLOSED && sub->parent->hangupongateremove) {
2436 ast_softhangup(sub->owner, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
2437 ast_channel_unlock(sub->owner);
2442 ast_mutex_unlock(&sub->lock);
2446 static int mgcp_pktcgate_open(struct cops_gate *gate)
2448 struct mgcp_subchannel *sub = gate->tech_pvt;
2452 ast_mutex_lock(&sub->lock);
2453 ast_debug(1, "Pktc: gate 0x%x open\n", gate->gateid);
2454 if (!sub->sdpsent) transmit_modify_with_sdp(sub, NULL, 0);
2455 ast_mutex_unlock(&sub->lock);
2459 static int mgcp_alloc_pktcgate(struct mgcp_subchannel *sub)
2461 struct mgcp_endpoint *p = sub->parent;
2462 sub->gate = ast_pktccops_gate_alloc(GATE_SET, NULL, ntohl(p->parent->addr.sin_addr.s_addr),
2463 8, 128000, 232, 0, 0, NULL, &mgcp_pktcgate_remove);
2468 sub->gate->tech_pvt = sub;
2469 sub->gate->gate_open = &mgcp_pktcgate_open;
2473 static int transmit_connect(struct mgcp_subchannel *sub)
2475 struct mgcp_request resp;
2479 struct ast_format *tmpfmt;
2480 struct mgcp_endpoint *p = sub->parent;
2483 ast_copy_string(local, "p:20, s:off, e:on", sizeof(local));
2485 for (x = 0; x < ast_format_cap_count(p->cap); x++) {
2486 tmpfmt = ast_format_cap_get_format(p->cap, x);
2488 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, tmpfmt, 0, 0));
2489 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2491 ao2_ref(tmpfmt, -1);
2494 ast_debug(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n",
2495 p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2497 oseq = reqprep(&resp, p, "CRCX");
2498 add_header(&resp, "C", sub->callid);
2499 add_header(&resp, "L", local);
2500 add_header(&resp, "M", "inactive");
2501 /* X header should not be sent. kept for compatibility */
2502 add_header(&resp, "X", sub->txident);
2503 /*add_header(&resp, "S", "");*/
2504 /* fill in new fields */
2505 resp.cmd = MGCP_CMD_CRCX;
2507 return send_request(p, sub, &resp, oseq);
2510 static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone)
2512 struct mgcp_request resp;
2513 struct mgcp_endpoint *p = sub->parent;
2516 ast_debug(3, "MGCP Asked to indicate tone: %s on %s@%s-%d in cxmode: %s\n",
2517 tone, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode]);
2518 ast_copy_string(p->curtone, tone, sizeof(p->curtone));
2519 oseq = reqprep(&resp, p, "RQNT");
2520 add_header(&resp, "X", p->rqnt_ident);
2521 switch (p->hookstate) {
2523 add_header(&resp, "R", "L/hd(N)");
2526 add_header_offhook(sub, &resp, tone);
2529 if (!ast_strlen_zero(tone)) {
2530 add_header(&resp, "S", tone);
2532 /* fill in new fields */
2533 resp.cmd = MGCP_CMD_RQNT;
2535 return send_request(p, NULL, &resp, oseq);
2538 static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername)
2540 struct mgcp_request resp;
2543 struct timeval t = ast_tvnow();
2545 struct mgcp_endpoint *p = sub->parent;
2548 ast_localtime(&t, &tm, NULL);
2556 /* Keep track of last callerid for blacklist and callreturn */
2557 ast_copy_string(p->lastcallerid, l, sizeof(p->lastcallerid));
2559 snprintf(tone2, sizeof(tone2), "%s,L/ci(%02d/%02d/%02d/%02d,%s,%s)", tone,
2560 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, l, n);
2561 ast_copy_string(p->curtone, tone, sizeof(p->curtone));
2562 oseq = reqprep(&resp, p, "RQNT");
2563 add_header(&resp, "X", p->rqnt_ident);
2564 switch (p->hookstate) {
2566 add_header(&resp, "R", "L/hd(N)");
2569 add_header_offhook(sub, &resp, tone);
2572 if (!ast_strlen_zero(tone2)) {
2573 add_header(&resp, "S", tone2);
2575 ast_debug(3, "MGCP Asked to indicate tone: %s on %s@%s-%d in cxmode: %s\n",
2576 tone2, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode]);
2577 /* fill in new fields */
2578 resp.cmd = MGCP_CMD_RQNT;
2580 return send_request(p, NULL, &resp, oseq);
2583 static int transmit_modify_request(struct mgcp_subchannel *sub)
2585 struct mgcp_request resp;
2586 struct mgcp_endpoint *p = sub->parent;
2593 if (ast_strlen_zero(sub->cxident)) {
2594 /* We don't have a CXident yet, store the destination and
2598 ast_debug(3, "Modified %s@%s-%d with new mode: %s on callid: %s\n",
2599 p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2601 ast_copy_string(local, "", sizeof(local));
2602 for (i = 0; i < ast_format_cap_count(p->cap); i++) {
2603 struct ast_format *format = ast_format_cap_get_format(p->cap, i);
2605 if (p->ncs && !fc) {
2606 ast_format_cap_remove_by_type(p->cap, AST_MEDIA_TYPE_UNKNOWN);
2607 ast_format_cap_append(p->cap, format, 0); /* sb5120e bug */
2608 ao2_ref(format, -1);
2612 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
2614 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2616 ao2_ref(format, -1);
2619 if (!sub->sdpsent) {
2621 if (sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) {
2622 snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
2623 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2625 /* we still don't have gateid wait */
2631 oseq = reqprep(&resp, p, "MDCX");
2632 add_header(&resp, "C", sub->callid);
2633 if (!sub->sdpsent) {
2634 add_header(&resp, "L", local);
2636 add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
2637 /* X header should not be sent. kept for compatibility */
2638 add_header(&resp, "X", sub->txident);
2639 add_header(&resp, "I", sub->cxident);
2640 switch (sub->parent->hookstate) {
2642 add_header(&resp, "R", "L/hd(N)");
2645 add_header_offhook(sub, &resp, "");
2648 if (!sub->sdpsent) {
2649 add_sdp(&resp, sub, NULL);
2652 /* fill in new fields */
2653 resp.cmd = MGCP_CMD_MDCX;
2655 return send_request(p, sub, &resp, oseq);
2659 static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp, char *tone)
2661 struct mgcp_endpoint *p = sub->parent;
2662 char tone_indicate_end = 0;
2664 /* We also should check the tone to indicate, because it have no sense
2665 to request notify D/[0-9#*] (dtmf keys) if we are sending congestion
2666 tone for example G/cg */
2667 if (p && (!strcasecmp(tone, (p->ncs ? "L/ro" : "G/cg")))) {
2668 tone_indicate_end = 1;
2671 if (p && p->sub && p->sub->owner &&
2672 ast_channel_state(p->sub->owner) >= AST_STATE_RINGING &&
2673 (p->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID))) {
2674 add_header(resp, "R", "L/hu(N),L/hf(N)");
2676 } else if (!tone_indicate_end){
2677 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)"));
2679 ast_debug(1, "We don't want more digits if we will end the call\n");
2680 add_header(resp, "R", "L/hu(N),L/hf(N)");
2687 static int transmit_audit_endpoint(struct mgcp_endpoint *p)
2689 struct mgcp_request resp;
2691 oseq = reqprep(&resp, p, "AUEP");
2692 /* removed unknown param VS */
2693 /*add_header(&resp, "F", "A,R,D,S,X,N,I,T,O,ES,E,MD,M");*/
2694 add_header(&resp, "F", "A");
2695 /* fill in new fields */
2696 resp.cmd = MGCP_CMD_AUEP;
2698 return send_request(p, NULL, &resp, oseq);
2701 static int transmit_connection_del(struct mgcp_subchannel *sub)
2703 struct mgcp_endpoint *p = sub->parent;
2704 struct mgcp_request resp;
2707 ast_debug(3, "Delete connection %s %s@%s-%d with new mode: %s on callid: %s\n",
2708 sub->cxident, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2709 oseq = reqprep(&resp, p, "DLCX");
2710 /* check if call id is avail */
2712 add_header(&resp, "C", sub->callid);
2713 /* X header should not be sent. kept for compatibility */
2714 add_header(&resp, "X", sub->txident);
2715 /* check if cxident is avail */
2716 if (sub->cxident[0])
2717 add_header(&resp, "I", sub->cxident);
2718 /* fill in new fields */
2719 resp.cmd = MGCP_CMD_DLCX;
2721 return send_request(p, sub, &resp, oseq);
2724 static int transmit_connection_del_w_params(struct mgcp_endpoint *p, char *callid, char *cxident)
2726 struct mgcp_request resp;
2729 ast_debug(3, "Delete connection %s %s@%s on callid: %s\n",
2730 cxident ? cxident : "", p->name, p->parent->name, callid ? callid : "");
2731 oseq = reqprep(&resp, p, "DLCX");
2732 /* check if call id is avail */
2733 if (callid && *callid)
2734 add_header(&resp, "C", callid);
2735 /* check if cxident is avail */
2736 if (cxident && *cxident)
2737 add_header(&resp, "I", cxident);
2738 /* fill in new fields */
2739 resp.cmd = MGCP_CMD_DLCX;
2741 return send_request(p, p->sub, &resp, oseq);
2744 /*! \brief dump_cmd_queues: (SC:) cleanup pending commands */
2745 static void dump_cmd_queues(struct mgcp_endpoint *p, struct mgcp_subchannel *sub)
2747 struct mgcp_request *t, *q;
2750 ast_mutex_lock(&p->rqnt_queue_lock);
2751 for (q = p->rqnt_queue; q; t = q->next, ast_free(q), q=t);
2752 p->rqnt_queue = NULL;
2753 ast_mutex_unlock(&p->rqnt_queue_lock);
2755 ast_mutex_lock(&p->cmd_queue_lock);
2756 for (q = p->cmd_queue; q; t = q->next, ast_free(q), q=t);
2757 p->cmd_queue = NULL;
2758 ast_mutex_unlock(&p->cmd_queue_lock);
2760 ast_mutex_lock(&p->sub->cx_queue_lock);
2761 for (q = p->sub->cx_queue; q; t = q->next, ast_free(q), q=t);
2762 p->sub->cx_queue = NULL;
2763 ast_mutex_unlock(&p->sub->cx_queue_lock);
2765 ast_mutex_lock(&p->sub->next->cx_queue_lock);
2766 for (q = p->sub->next->cx_queue; q; t = q->next, ast_free(q), q=t);
2767 p->sub->next->cx_queue = NULL;
2768 ast_mutex_unlock(&p->sub->next->cx_queue_lock);
2770 ast_mutex_lock(&sub->cx_queue_lock);
2771 for (q = sub->cx_queue; q; t = q->next, ast_free(q), q=t);
2772 sub->cx_queue = NULL;
2773 ast_mutex_unlock(&sub->cx_queue_lock);
2778 /*! \brief find_command: (SC:) remove command transaction from queue */
2779 static struct mgcp_request *find_command(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
2780 struct mgcp_request **queue, ast_mutex_t *l, int ident)
2782 struct mgcp_request *prev, *req;
2785 for (prev = NULL, req = *queue; req; prev = req, req = req->next) {
2786 if (req->trid == ident) {
2787 /* remove from queue */
2791 prev->next = req->next;
2793 /* send next pending command */
2795 ast_debug(1, "Posting Queued Request:\n%s to %s:%d\n", (*queue)->data,
2796 ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
2798 mgcp_postrequest(p, sub, (*queue)->data, (*queue)->len, (*queue)->trid);
2803 ast_mutex_unlock(l);
2807 /* modified for new transport mechanism */
2808 static void handle_response(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
2809 int result, unsigned int ident, struct mgcp_request *resp)
2812 struct mgcp_request *req;
2813 struct mgcp_gateway *gw = p->parent;
2816 /* provisional response */
2820 if (p->slowsequence)
2821 req = find_command(p, sub, &p->cmd_queue, &p->cmd_queue_lock, ident);
2823 req = find_command(p, sub, &sub->cx_queue, &sub->cx_queue_lock, ident);
2824 else if (!(req = find_command(p, sub, &p->rqnt_queue, &p->rqnt_queue_lock, ident)))
2825 req = find_command(p, sub, &p->cmd_queue, &p->cmd_queue_lock, ident);
2828 ast_verb(3, "No command found on [%s] for transaction %u. Ignoring...\n",
2833 if (p && (result >= 400) && (result <= 599)) {
2836 p->hookstate = MGCP_OFFHOOK;
2839 p->hookstate = MGCP_ONHOOK;
2842 ast_log(LOG_NOTICE, "Transaction %u timed out\n", ident);
2845 ast_log(LOG_NOTICE, "Transaction %u aborted\n", ident);
2849 if (!sub->cxident[0] && (req->cmd == MGCP_CMD_CRCX)) {
2850 ast_log(LOG_NOTICE, "DLCX for all connections on %s due to error %d\n", gw->name, result);
2851 transmit_connection_del(sub);
2854 ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
2855 result, p->name, p->parent->name, sub ? sub->id:-1);
2856 mgcp_queue_hangup(sub);
2859 if (p->sub->next->owner) {
2860 ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
2861 result, p->name, p->parent->name, sub ? sub->id:-1);
2862 mgcp_queue_hangup(p->sub);
2865 if (p->sub->owner) {
2866 ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
2867 result, p->name, p->parent->name, sub ? sub->id:-1);
2868 mgcp_queue_hangup(p->sub);
2871 dump_cmd_queues(p, NULL);
2877 if (result == 200 && (req->cmd == MGCP_CMD_CRCX || req->cmd == MGCP_CMD_MDCX)) {
2879 transmit_response(sub, "000", resp, "OK");
2880 if (sub->owner && ast_channel_state(sub->owner) == AST_STATE_RINGING) {
2881 ast_queue_control(sub->owner, AST_CONTROL_RINGING);
2885 if (req->cmd == MGCP_CMD_CRCX) {
2886 if ((c = get_header(resp, "I"))) {
2887 if (!ast_strlen_zero(c) && sub) {
2888 /* if we are hanging up do not process this conn. */
2890 if (!ast_strlen_zero(sub->cxident)) {
2891 if (strcasecmp(c, sub->cxident)) {
2892 ast_log(LOG_WARNING, "Subchannel already has a cxident. sub->cxident: %s requested %s\n", sub->cxident, c);
2895 ast_copy_string(sub->cxident, c, sizeof(sub->cxident));
2896 if (sub->tmpdest.sin_addr.s_addr) {
2897 transmit_modify_with_sdp(sub, NULL, 0);
2900 /* XXX delete this one
2901 callid and conn id may already be lost.
2902 so the following del conn may have a side effect of
2903 cleaning up the next subchannel */
2904 transmit_connection_del(sub);
2910 if (req->cmd == MGCP_CMD_AUEP) {
2911 /* check stale connection ids */
2912 if ((c = get_header(resp, "I"))) {
2915 while ((v = get_csv(c, &len, &n))) {
2917 if (strncasecmp(v, p->sub->cxident, len) &&
2918 strncasecmp(v, p->sub->next->cxident, len)) {
2919 /* connection id not found. delete it */
2920 char cxident[80] = "";
2922 if (len > (sizeof(cxident) - 1))
2923 len = sizeof(cxident) - 1;
2924 ast_copy_string(cxident, v, len);
2925 ast_verb(3, "Non existing connection id %s on %s@%s \n",
2926 cxident, p->name, gw->name);
2927 transmit_connection_del_w_params(p, NULL, cxident);
2934 /* Try to determine the hookstate returned from an audit endpoint command */
2935 if ((c = get_header(resp, "ES"))) {
2936 if (!ast_strlen_zero(c)) {
2937 if (strstr(c, "hu")) {
2938 if (p->hookstate != MGCP_ONHOOK) {
2939 /* XXX cleanup if we think we are offhook XXX */
2940 if ((p->sub->owner || p->sub->next->owner ) &&
2941 p->hookstate == MGCP_OFFHOOK)
2942 mgcp_queue_hangup(sub);
2943 p->hookstate = MGCP_ONHOOK;
2945 /* update the requested events according to the new hookstate */
2946 transmit_notify_request(p->sub, "");
2948 ast_verb(3, "Setting hookstate of %s@%s to ONHOOK\n", p->name, gw->name);
2950 } else if (strstr(c, "hd")) {
2951 if (p->hookstate != MGCP_OFFHOOK) {
2952 p->hookstate = MGCP_OFFHOOK;
2954 /* update the requested events according to the new hookstate */
2955 transmit_notify_request(p->sub, "");
2957 ast_verb(3, "Setting hookstate of %s@%s to OFFHOOK\n", p->name, gw->name);
2964 if (resp && resp->lines) {
2965 /* do not process sdp if we are hanging up. this may be a late response */
2966 if (sub && sub->owner) {
2970 process_sdp(sub, resp);
2978 static void start_rtp(struct mgcp_subchannel *sub)
2980 struct ast_sockaddr bindaddr_tmp;
2982 ast_mutex_lock(&sub->lock);
2983 /* check again to be on the safe side */
2985 ast_rtp_instance_destroy(sub->rtp);
2988 /* Allocate the RTP now */
2989 ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
2990 sub->rtp = ast_rtp_instance_new("asterisk", sched, &bindaddr_tmp, NULL);
2991 if (sub->rtp && sub->owner)
2992 ast_channel_set_fd(sub->owner, 0, ast_rtp_instance_fd(sub->rtp, 0));
2994 ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "MGCP RTP");
2995 ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, sub->nat);
2997 /* Make a call*ID */
2998 snprintf(sub->callid, sizeof(sub->callid), "%08lx%s", (unsigned long)ast_random(), sub->txident);
2999 /* Transmit the connection create */
3000 if(!sub->parent->pktcgatealloc) {
3001 transmit_connect_with_sdp(sub, NULL);
3003 transmit_connect(sub);
3005 if(!mgcp_alloc_pktcgate(sub))
3006 mgcp_queue_hangup(sub);
3008 ast_mutex_unlock(&sub->lock);
3011 static void *mgcp_ss(void *data)
3013 struct ast_channel *chan = data;
3014 struct mgcp_subchannel *sub = ast_channel_tech_pvt(chan);
3015 struct mgcp_endpoint *p = sub->parent;
3016 /* char exten[AST_MAX_EXTENSION] = ""; */
3018 int timeout = firstdigittimeout;
3021 int loop_pause = 100;
3022 RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
3023 const char *pickupexten;
3025 len = strlen(p->dtmf_buf);
3027 ast_channel_lock(chan);
3028 pickup_cfg = ast_get_chan_features_pickup_config(chan);
3030 ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
3033 pickupexten = ast_strdupa(pickup_cfg->pickupexten);
3035 ast_channel_unlock(chan);
3037 while (len < AST_MAX_EXTENSION - 1) {
3038 ast_debug(1, "Dtmf buffer '%s' for '%s@%s'\n", p->dtmf_buf, p->name, p->parent->name);
3039 res = 1; /* Assume that we will get a digit */
3040 while (strlen(p->dtmf_buf) == len) {
3041 ast_safe_sleep(chan, loop_pause);
3042 timeout -= loop_pause;
3051 len = strlen(p->dtmf_buf);
3053 if (!ast_ignore_pattern(ast_channel_context(chan), p->dtmf_buf)) {
3054 /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
3055 ast_indicate(chan, -1);
3057 /* XXX Redundant? We should already be playing dialtone */
3058 /*tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALTONE);*/
3059 transmit_notify_request(sub, "L/dl");
3061 if (ast_exists_extension(chan, ast_channel_context(chan), p->dtmf_buf, 1, p->cid_num)) {
3062 if (!res || !ast_matchmore_extension(chan, ast_channel_context(chan), p->dtmf_buf, 1, p->cid_num)) {
3064 /* Record this as the forwarding extension */
3065 ast_copy_string(p->call_forward, p->dtmf_buf, sizeof(p->call_forward));
3066 ast_verb(3, "Setting call forward to '%s' on channel %s\n",
3067 p->call_forward, ast_channel_name(chan));
3068 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
3069 transmit_notify_request(sub, "L/sl");
3073 /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
3074 ast_indicate(chan, -1);
3076 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
3077 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALTONE);*/
3078 transmit_notify_request(sub, "L/dl");
3082 /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
3083 ast_indicate(chan, -1);
3084 ast_channel_lock(chan);
3085 ast_channel_exten_set(chan, p->dtmf_buf);
3086 ast_channel_dialed(chan)->number.str = ast_strdup(p->dtmf_buf);
3087 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
3088 ast_set_callerid(chan,
3089 p->hidecallerid ? "" : p->cid_num,
3090 p->hidecallerid ? "" : p->cid_name,
3091 ast_channel_caller(chan)->ani.number.valid ? NULL : p->cid_num);
3092 ast_setstate(chan, AST_STATE_RING);
3093 ast_channel_unlock(chan);
3094 if (p->dtmfmode & MGCP_DTMF_HYBRID) {