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 int has_voicemail(struct mgcp_endpoint *p)
495 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
497 msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(), p->mailbox);
499 struct ast_mwi_state *mwi_state = stasis_message_data(msg);
500 new_msgs = mwi_state->new_msgs;
502 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
508 static int unalloc_sub(struct mgcp_subchannel *sub)
510 struct mgcp_endpoint *p = sub->parent;
512 ast_log(LOG_WARNING, "Trying to unalloc the real channel %s@%s?!?\n", p->name, p->parent->name);
515 ast_debug(1, "Released sub %d of channel %s@%s\n", sub->id, p->name, p->parent->name);
517 mgcp_set_owner(sub, NULL);
518 if (!ast_strlen_zero(sub->cxident)) {
519 transmit_connection_del(sub);
521 sub->cxident[0] = '\0';
522 sub->callid[0] = '\0';
523 sub->cxmode = MGCP_CX_INACTIVE;
525 sub->alreadygone = 0;
526 memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
528 ast_rtp_instance_destroy(sub->rtp);
531 dump_cmd_queues(NULL, sub);
535 /* modified for new transport mechanism */
536 static int __mgcp_xmit(struct mgcp_gateway *gw, char *data, int len)
539 if (gw->addr.sin_addr.s_addr)
540 res=sendto(mgcpsock, data, len, 0, (struct sockaddr *)&gw->addr, sizeof(struct sockaddr_in));
542 res=sendto(mgcpsock, data, len, 0, (struct sockaddr *)&gw->defaddr, sizeof(struct sockaddr_in));
544 ast_log(LOG_WARNING, "mgcp_xmit returned %d: %s\n", res, strerror(errno));
549 static int resend_response(struct mgcp_subchannel *sub, struct mgcp_response *resp)
551 struct mgcp_endpoint *p = sub->parent;
553 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));
554 res = __mgcp_xmit(p->parent, resp->buf, resp->len);
560 static int send_response(struct mgcp_subchannel *sub, struct mgcp_request *req)
562 struct mgcp_endpoint *p = sub->parent;
564 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));
565 res = __mgcp_xmit(p->parent, req->data, req->len);
571 /* modified for new transport framework */
572 static void dump_queue(struct mgcp_gateway *gw, struct mgcp_endpoint *p)
574 struct mgcp_message *cur, *q = NULL, *w, *prev;
576 ast_mutex_lock(&gw->msgs_lock);
577 for (prev = NULL, cur = gw->msgs; cur; prev = cur, cur = cur->next) {
578 if (!p || cur->owner_ep == p) {
580 prev->next = cur->next;
582 gw->msgs = cur->next;
585 ast_log(LOG_NOTICE, "Removing message from %s transaction %u\n",
586 gw->name, cur->seqno);
597 ast_mutex_unlock(&gw->msgs_lock);
606 static void mgcp_queue_frame(struct mgcp_subchannel *sub, struct ast_frame *f)
610 if (!ast_channel_trylock(sub->owner)) {
611 ast_queue_frame(sub->owner, f);
612 ast_channel_unlock(sub->owner);
615 DEADLOCK_AVOIDANCE(&sub->lock);
623 static void mgcp_queue_hangup(struct mgcp_subchannel *sub)
627 if (!ast_channel_trylock(sub->owner)) {
628 ast_queue_hangup(sub->owner);
629 ast_channel_unlock(sub->owner);
632 DEADLOCK_AVOIDANCE(&sub->lock);
640 static void mgcp_queue_control(struct mgcp_subchannel *sub, int control)
642 struct ast_frame f = { AST_FRAME_CONTROL, { control } };
643 return mgcp_queue_frame(sub, &f);
646 static int retrans_pkt(const void *data)
648 struct mgcp_gateway *gw = (struct mgcp_gateway *)data;
649 struct mgcp_message *cur, *exq = NULL, *w, *prev;
652 /* find out expired msgs */
653 ast_mutex_lock(&gw->msgs_lock);
655 for (prev = NULL, cur = gw->msgs; cur; prev = cur, cur = cur->next) {
656 if (cur->retrans < MAX_RETRANS) {
658 ast_debug(1, "Retransmitting #%d transaction %u on [%s]\n",
659 cur->retrans, cur->seqno, gw->name);
660 __mgcp_xmit(gw, cur->buf, cur->len);
663 prev->next = cur->next;
665 gw->msgs = cur->next;
667 ast_log(LOG_WARNING, "Maximum retries exceeded for transaction %u on [%s]\n",
668 cur->seqno, gw->name);
687 ast_mutex_unlock(&gw->msgs_lock);
691 /* time-out transaction */
692 handle_response(cur->owner_ep, cur->owner_sub, 406, cur->seqno, NULL);
700 /* modified for the new transaction mechanism */
701 static int mgcp_postrequest(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
702 char *data, int len, unsigned int seqno)
704 struct mgcp_message *msg;
705 struct mgcp_message *cur;
706 struct mgcp_gateway *gw;
709 if (!(msg = ast_malloc(sizeof(*msg) + len))) {
712 if (!(gw = ((p && p->parent) ? p->parent : NULL))) {
717 msg->owner_sub = sub;
723 memcpy(msg->buf, data, msg->len);
725 ast_mutex_lock(&gw->msgs_lock);
726 for (cur = gw->msgs; cur && cur->next; cur = cur->next);
734 msg->expire = now.tv_sec * 1000 + now.tv_usec / 1000 + DEFAULT_RETRANS;
736 if (gw->retransid == -1)
737 gw->retransid = ast_sched_add(sched, DEFAULT_RETRANS, retrans_pkt, (void *)gw);
738 ast_mutex_unlock(&gw->msgs_lock);
739 __mgcp_xmit(gw, msg->buf, msg->len);
740 /* XXX Should schedule retransmission XXX */
744 /* modified for new transport */
745 static int send_request(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
746 struct mgcp_request *req, unsigned int seqno)
749 struct mgcp_request **queue, *q, *r, *t;
752 ast_debug(1, "Slow sequence is %d\n", p->slowsequence);
753 if (p->slowsequence) {
754 queue = &p->cmd_queue;
755 l = &p->cmd_queue_lock;
760 queue = &sub->cx_queue;
761 l = &sub->cx_queue_lock;
764 /* delete pending cx cmds */
766 if (!sub->parent->ncs) {
778 queue = &sub->cx_queue;
779 l = &sub->cx_queue_lock;
784 queue = &p->rqnt_queue;
785 l = &p->rqnt_queue_lock;
790 queue = &p->cmd_queue;
791 l = &p->cmd_queue_lock;
797 if (!(r = ast_malloc(sizeof(*r)))) {
798 ast_log(LOG_WARNING, "Cannot post MGCP request: insufficient memory\n");
802 memcpy(r, req, sizeof(*r));
805 ast_debug(1, "Posting Request:\n%s to %s:%d\n", req->data,
806 ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
808 res = mgcp_postrequest(p, sub, req->data, req->len, seqno);
810 ast_debug(1, "Queueing Request:\n%s to %s:%d\n", req->data,
811 ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
814 /* XXX find tail. We could also keep tail in the data struct for faster access */
815 for (t = *queue; t && t->next; t = t->next);
828 static int mgcp_call(struct ast_channel *ast, const char *dest, int timeout)
831 struct mgcp_endpoint *p;
832 struct mgcp_subchannel *sub;
834 const char *distinctive_ring = pbx_builtin_getvar_helper(ast, "ALERT_INFO");
836 ast_debug(3, "MGCP mgcp_call(%s)\n", ast_channel_name(ast));
837 sub = ast_channel_tech_pvt(ast);
840 ast_mutex_lock(&sub->lock);
841 switch (p->hookstate) {
843 if (!ast_strlen_zero(distinctive_ring)) {
844 snprintf(tone, sizeof(tone), "L/wt%s", distinctive_ring);
845 ast_debug(3, "MGCP distinctive callwait %s\n", tone);
847 ast_copy_string(tone, (p->ncs ? "L/wt1" : "L/wt"), sizeof(tone));
848 ast_debug(3, "MGCP normal callwait %s\n", tone);
853 if (!ast_strlen_zero(distinctive_ring)) {
854 snprintf(tone, sizeof(tone), "L/r%s", distinctive_ring);
855 ast_debug(3, "MGCP distinctive ring %s\n", tone);
857 ast_copy_string(tone, "L/rg", sizeof(tone));
858 ast_debug(3, "MGCP default ring\n");
863 if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
864 ast_log(LOG_WARNING, "mgcp_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
865 ast_mutex_unlock(&sub->lock);
871 sub->cxmode = MGCP_CX_RECVONLY;
872 ast_setstate(ast, AST_STATE_RINGING);
873 if (p->type == TYPE_LINE) {
877 transmit_modify_request(sub);
880 if (sub->next->owner && !ast_strlen_zero(sub->next->cxident) && !ast_strlen_zero(sub->next->callid)) {
881 /* try to prevent a callwait from disturbing the other connection */
882 sub->next->cxmode = MGCP_CX_RECVONLY;
883 transmit_modify_request(sub->next);
886 transmit_notify_request_with_callerid(sub, tone,
887 S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, ""),
888 S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, ""));
889 ast_setstate(ast, AST_STATE_RINGING);
891 if (sub->next->owner && !ast_strlen_zero(sub->next->cxident) && !ast_strlen_zero(sub->next->callid)) {
892 /* Put the connection back in sendrecv */
893 sub->next->cxmode = MGCP_CX_SENDRECV;
894 transmit_modify_request(sub->next);
897 ast_log(LOG_NOTICE, "Don't know how to dial on trunks yet\n");
900 ast_mutex_unlock(&sub->lock);
904 static int mgcp_hangup(struct ast_channel *ast)
906 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
907 struct mgcp_endpoint *p = sub->parent;
909 ast_debug(1, "mgcp_hangup(%s)\n", ast_channel_name(ast));
910 if (!ast_channel_tech_pvt(ast)) {
911 ast_debug(1, "Asked to hangup channel not connected\n");
914 if (strcmp(sub->magic, MGCP_SUBCHANNEL_MAGIC)) {
915 ast_debug(1, "Invalid magic. MGCP subchannel freed up already.\n");
918 ast_mutex_lock(&sub->lock);
919 ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s\n", ast_channel_name(ast), p->name, p->parent->name);
921 if ((p->dtmfmode & MGCP_DTMF_INBAND) && p->dsp) {
922 /* check whether other channel is active. */
923 if (!sub->next->owner) {
924 if (p->dtmfmode & MGCP_DTMF_HYBRID) {
925 p->dtmfmode &= ~MGCP_DTMF_INBAND;
927 ast_debug(2, "MGCP free dsp on %s@%s\n", p->name, p->parent->name);
928 ast_dsp_free(p->dsp);
933 mgcp_set_owner(sub, NULL);
935 /* for deleting gate */
936 if (p->pktcgatealloc && sub->gate) {
937 sub->gate->gate_open = NULL;
938 sub->gate->gate_remove = NULL;
939 sub->gate->got_dq_gi = NULL;
940 sub->gate->tech_pvt = NULL;
941 if (sub->gate->state == GATE_ALLOC_PROGRESS || sub->gate->state == GATE_ALLOCATED) {
942 ast_pktccops_gate_alloc(GATE_DEL, sub->gate, 0, 0, 0, 0, 0, 0, NULL, NULL);
944 sub->gate->deltimer = time(NULL) + 5;
949 if (!ast_strlen_zero(sub->cxident)) {
950 transmit_connection_del(sub);
952 sub->cxident[0] = '\0';
953 if ((sub == p->sub) && sub->next->owner) {
954 RAII_VAR(struct ast_channel *, bridged, ast_channel_bridge_peer(sub->next->owner), ast_channel_cleanup);
956 if (p->hookstate == MGCP_OFFHOOK) {
957 if (sub->next->owner && bridged) {
959 transmit_notify_request_with_callerid(p->sub, (p->ncs ? "L/wt1" : "L/wt"),
960 S_COR(ast_channel_caller(bridged)->id.number.valid, ast_channel_caller(bridged)->id.number.str, ""),
961 S_COR(ast_channel_caller(bridged)->id.name.valid, ast_channel_caller(bridged)->id.name.str, ""));
964 /* set our other connection as the primary and swith over to it */
966 p->sub->cxmode = MGCP_CX_RECVONLY;
967 transmit_modify_request(p->sub);
968 if (sub->next->owner && bridged) {
969 transmit_notify_request_with_callerid(p->sub, "L/rg",
970 S_COR(ast_channel_caller(bridged)->id.number.valid, ast_channel_caller(bridged)->id.number.str, ""),
971 S_COR(ast_channel_caller(bridged)->id.name.valid, ast_channel_caller(bridged)->id.name.str, ""));
975 } else if ((sub == p->sub->next) && p->hookstate == MGCP_OFFHOOK) {
976 transmit_notify_request(sub, p->ncs ? "" : "L/v");
977 } else if (p->hookstate == MGCP_OFFHOOK) {
978 transmit_notify_request(sub, "L/ro");
980 transmit_notify_request(sub, "");
983 ast_channel_tech_pvt_set(ast, NULL);
984 sub->alreadygone = 0;
986 sub->cxmode = MGCP_CX_INACTIVE;
987 sub->callid[0] = '\0';
989 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
991 /* Reset temporary destination */
992 memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
994 ast_rtp_instance_destroy(sub->rtp);
998 ast_module_unref(ast_module_info->self);
1000 if ((p->hookstate == MGCP_ONHOOK) && (!sub->next->rtp)) {
1001 p->hidecallerid = 0;
1002 if (p->hascallwaiting && !p->callwaiting) {
1003 ast_verb(3, "Enabling call waiting on %s\n", ast_channel_name(ast));
1004 p->callwaiting = -1;
1006 if (has_voicemail(p)) {
1007 ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s set vmwi(+)\n",
1008 ast_channel_name(ast), p->name, p->parent->name);
1009 transmit_notify_request(sub, "L/vmwi(+)");
1011 ast_debug(3, "MGCP mgcp_hangup(%s) on %s@%s set vmwi(-)\n",
1012 ast_channel_name(ast), p->name, p->parent->name);
1013 transmit_notify_request(sub, "L/vmwi(-)");
1016 ast_mutex_unlock(&sub->lock);
1020 static char *handle_mgcp_show_endpoints(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1022 struct mgcp_gateway *mg;
1023 struct mgcp_endpoint *me;
1024 int hasendpoints = 0;
1025 struct ast_variable * v = NULL;
1029 e->command = "mgcp show endpoints";
1031 "Usage: mgcp show endpoints\n"
1032 " Lists all endpoints known to the MGCP (Media Gateway Control Protocol) subsystem.\n";
1039 return CLI_SHOWUSAGE;
1041 ast_mutex_lock(&gatelock);
1042 for (mg = gateways; mg; mg = mg->next) {
1043 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");
1044 for (me = mg->endpoints; me; me = me->next) {
1045 ast_cli(a->fd, " -- '%s@%s in '%s' is %s\n", me->name, mg->name, me->context, me->sub->owner ? "active" : "idle");
1047 ast_cli(a->fd, " Variables:\n");
1048 for (v = me->chanvars ; v ; v = v->next) {
1049 ast_cli(a->fd, " %s = '%s'\n", v->name, v->value);
1054 if (!hasendpoints) {
1055 ast_cli(a->fd, " << No Endpoints Defined >> ");
1058 ast_mutex_unlock(&gatelock);
1062 static char *handle_mgcp_audit_endpoint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1064 struct mgcp_gateway *mg;
1065 struct mgcp_endpoint *me;
1067 char *ename,*gname, *c;
1071 e->command = "mgcp audit endpoint";
1073 "Usage: mgcp audit endpoint <endpointid>\n"
1074 " Lists the capabilities of an endpoint in the MGCP (Media Gateway Control Protocol) subsystem.\n"
1075 " mgcp debug MUST be on to see the results of this command.\n";
1082 return CLI_SHOWUSAGE;
1085 return CLI_SHOWUSAGE;
1086 /* split the name into parts by null */
1087 ename = ast_strdupa(a->argv[3]);
1088 for (gname = ename; *gname; gname++) {
1089 if (*gname == '@') {
1095 if (gname[0] == '[') {
1098 if ((c = strrchr(gname, ']'))) {
1101 ast_mutex_lock(&gatelock);
1102 for (mg = gateways; mg; mg = mg->next) {
1103 if (!strcasecmp(mg->name, gname)) {
1104 for (me = mg->endpoints; me; me = me->next) {
1105 if (!strcasecmp(me->name, ename)) {
1107 transmit_audit_endpoint(me);
1117 ast_cli(a->fd, " << Could not find endpoint >> ");
1119 ast_mutex_unlock(&gatelock);
1123 static char *handle_mgcp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1127 e->command = "mgcp set debug {on|off}";
1129 "Usage: mgcp set debug {on|off}\n"
1130 " Enables/Disables dumping of MGCP packets for debugging purposes\n";
1136 if (a->argc != e->args)
1137 return CLI_SHOWUSAGE;
1139 if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
1141 ast_cli(a->fd, "MGCP Debugging Enabled\n");
1142 } else if (!strncasecmp(a->argv[3], "off", 3)) {
1144 ast_cli(a->fd, "MGCP Debugging Disabled\n");
1146 return CLI_SHOWUSAGE;
1151 static struct ast_cli_entry cli_mgcp[] = {
1152 AST_CLI_DEFINE(handle_mgcp_audit_endpoint, "Audit specified MGCP endpoint"),
1153 AST_CLI_DEFINE(handle_mgcp_show_endpoints, "List defined MGCP endpoints"),
1154 AST_CLI_DEFINE(handle_mgcp_set_debug, "Enable/Disable MGCP debugging"),
1155 AST_CLI_DEFINE(mgcp_reload, "Reload MGCP configuration"),
1158 static int mgcp_answer(struct ast_channel *ast)
1161 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1162 struct mgcp_endpoint *p = sub->parent;
1164 ast_mutex_lock(&sub->lock);
1165 sub->cxmode = MGCP_CX_SENDRECV;
1169 transmit_modify_request(sub);
1171 ast_verb(3, "MGCP mgcp_answer(%s) on %s@%s-%d\n",
1172 ast_channel_name(ast), p->name, p->parent->name, sub->id);
1173 if (ast_channel_state(ast) != AST_STATE_UP) {
1174 ast_setstate(ast, AST_STATE_UP);
1175 ast_debug(1, "mgcp_answer(%s)\n", ast_channel_name(ast));
1176 transmit_notify_request(sub, "");
1177 transmit_modify_request(sub);
1179 ast_mutex_unlock(&sub->lock);
1183 static struct ast_frame *mgcp_rtp_read(struct mgcp_subchannel *sub)
1185 /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
1186 struct ast_frame *f;
1188 f = ast_rtp_instance_read(sub->rtp, 0);
1189 /* Don't send RFC2833 if we're not supposed to */
1190 if (f && (f->frametype == AST_FRAME_DTMF) && !(sub->parent->dtmfmode & MGCP_DTMF_RFC2833))
1191 return &ast_null_frame;
1193 /* We already hold the channel lock */
1194 if (f->frametype == AST_FRAME_VOICE) {
1195 if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(sub->owner), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
1196 struct ast_format_cap *caps;
1198 ast_debug(1, "Oooh, format changed to %s\n", ast_format_get_name(f->subclass.format));
1200 caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1202 ast_format_cap_append(caps, f->subclass.format, 0);
1203 ast_channel_nativeformats_set(sub->owner, caps);
1206 return &ast_null_frame;
1209 ast_set_read_format(sub->owner, ast_channel_readformat(sub->owner));
1210 ast_set_write_format(sub->owner, ast_channel_writeformat(sub->owner));
1212 /* Courtesy fearnor aka alex@pilosoft.com */
1213 if ((sub->parent->dtmfmode & MGCP_DTMF_INBAND) && (sub->parent->dsp)) {
1215 ast_log(LOG_NOTICE, "MGCP ast_dsp_process\n");
1217 f = ast_dsp_process(sub->owner, sub->parent->dsp, f);
1224 static void mgcp_set_owner(struct mgcp_subchannel *sub, struct ast_channel *chan)
1228 ast_rtp_instance_set_channel_id(sub->rtp, sub->owner ? ast_channel_uniqueid(chan) : "");
1232 static struct ast_frame *mgcp_read(struct ast_channel *ast)
1234 struct ast_frame *f;
1235 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1236 ast_mutex_lock(&sub->lock);
1237 f = mgcp_rtp_read(sub);
1238 ast_mutex_unlock(&sub->lock);
1242 static int mgcp_write(struct ast_channel *ast, struct ast_frame *frame)
1244 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1247 if (frame->frametype != AST_FRAME_VOICE) {
1248 if (frame->frametype == AST_FRAME_IMAGE)
1251 ast_log(LOG_WARNING, "Can't send %u type frames with MGCP write\n", frame->frametype);
1255 if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
1256 struct ast_str *cap_buf = ast_str_alloca(64);
1258 ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
1259 ast_format_get_name(frame->subclass.format),
1260 ast_format_cap_get_names(ast_channel_nativeformats(ast), &cap_buf),
1261 ast_format_get_name(ast_channel_readformat(ast)),
1262 ast_format_get_name(ast_channel_writeformat(ast)));
1267 ast_mutex_lock(&sub->lock);
1268 if (!sub->sdpsent && sub->gate) {
1269 if (sub->gate->state == GATE_ALLOCATED) {
1270 ast_debug(1, "GATE ALLOCATED, sending sdp\n");
1271 transmit_modify_with_sdp(sub, NULL, 0);
1274 if ((sub->parent->sub == sub) || !sub->parent->singlepath) {
1276 res = ast_rtp_instance_write(sub->rtp, frame);
1279 ast_mutex_unlock(&sub->lock);
1284 static int mgcp_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1286 struct mgcp_subchannel *sub = ast_channel_tech_pvt(newchan);
1288 ast_mutex_lock(&sub->lock);
1289 ast_log(LOG_NOTICE, "mgcp_fixup(%s, %s)\n", ast_channel_name(oldchan), ast_channel_name(newchan));
1290 if (sub->owner != oldchan) {
1291 ast_mutex_unlock(&sub->lock);
1292 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
1295 mgcp_set_owner(sub, newchan);
1296 ast_mutex_unlock(&sub->lock);
1300 static int mgcp_senddigit_begin(struct ast_channel *ast, char digit)
1302 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1303 struct mgcp_endpoint *p = sub->parent;
1306 ast_mutex_lock(&sub->lock);
1307 if (p->dtmfmode & MGCP_DTMF_INBAND || p->dtmfmode & MGCP_DTMF_HYBRID) {
1308 ast_debug(1, "Sending DTMF using inband/hybrid\n");
1309 res = -1; /* Let asterisk play inband indications */
1310 } else if (p->dtmfmode & MGCP_DTMF_RFC2833) {
1311 ast_debug(1, "Sending DTMF using RFC2833\n");
1312 ast_rtp_instance_dtmf_begin(sub->rtp, digit);
1314 ast_log(LOG_ERROR, "Don't know about DTMF_MODE %d\n", p->dtmfmode);
1316 ast_mutex_unlock(&sub->lock);
1321 static int mgcp_senddigit_end(struct ast_channel *ast, char digit, unsigned int duration)
1323 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1324 struct mgcp_endpoint *p = sub->parent;
1328 ast_mutex_lock(&sub->lock);
1329 if (p->dtmfmode & MGCP_DTMF_INBAND || p->dtmfmode & MGCP_DTMF_HYBRID) {
1330 ast_debug(1, "Stopping DTMF using inband/hybrid\n");
1331 res = -1; /* Tell Asterisk to stop inband indications */
1332 } else if (p->dtmfmode & MGCP_DTMF_RFC2833) {
1333 ast_debug(1, "Stopping DTMF using RFC2833\n");
1334 if (sub->parent->ncs) {
1343 transmit_notify_request(sub, tmp);
1344 ast_rtp_instance_dtmf_end(sub->rtp, digit);
1346 ast_log(LOG_ERROR, "Don't know about DTMF_MODE %d\n", p->dtmfmode);
1348 ast_mutex_unlock(&sub->lock);
1354 * \brief mgcp_devicestate: channel callback for device status monitoring
1355 * \param data tech/resource name of MGCP device to query
1357 * Callback for device state management in channel subsystem
1358 * to obtain device status (up/down) of a specific MGCP endpoint
1360 * \return device status result (from devicestate.h) AST_DEVICE_INVALID (not available) or AST_DEVICE_UNKNOWN (available but unknown state)
1362 static int mgcp_devicestate(const char *data)
1364 struct mgcp_gateway *g;
1365 struct mgcp_endpoint *e = NULL;
1366 char *tmp, *endpt, *gw;
1367 int ret = AST_DEVICE_INVALID;
1369 endpt = ast_strdupa(data);
1370 if ((tmp = strchr(endpt, '@'))) {
1376 ast_mutex_lock(&gatelock);
1377 for (g = gateways; g; g = g->next) {
1378 if (strcasecmp(g->name, gw) == 0) {
1387 for (; e; e = e->next) {
1388 if (strcasecmp(e->name, endpt) == 0) {
1397 * As long as the gateway/endpoint is valid, we'll
1398 * assume that the device is available and its state
1401 ret = AST_DEVICE_UNKNOWN;
1404 ast_mutex_unlock(&gatelock);
1408 static char *control2str(int ind) {
1410 case AST_CONTROL_HANGUP:
1411 return "Other end has hungup";
1412 case AST_CONTROL_RING:
1413 return "Local ring";
1414 case AST_CONTROL_RINGING:
1415 return "Remote end is ringing";
1416 case AST_CONTROL_ANSWER:
1417 return "Remote end has answered";
1418 case AST_CONTROL_BUSY:
1419 return "Remote end is busy";
1420 case AST_CONTROL_TAKEOFFHOOK:
1421 return "Make it go off hook";
1422 case AST_CONTROL_OFFHOOK:
1423 return "Line is off hook";
1424 case AST_CONTROL_CONGESTION:
1425 return "Congestion (circuits busy)";
1426 case AST_CONTROL_FLASH:
1427 return "Flash hook";
1428 case AST_CONTROL_WINK:
1430 case AST_CONTROL_OPTION:
1431 return "Set a low-level option";
1432 case AST_CONTROL_RADIO_KEY:
1434 case AST_CONTROL_RADIO_UNKEY:
1435 return "Un-Key Radio";
1440 static int mgcp_indicate(struct ast_channel *ast, int ind, const void *data, size_t datalen)
1442 struct mgcp_subchannel *sub = ast_channel_tech_pvt(ast);
1445 ast_debug(3, "MGCP asked to indicate %d '%s' condition on channel %s\n",
1446 ind, control2str(ind), ast_channel_name(ast));
1447 ast_mutex_lock(&sub->lock);
1449 case AST_CONTROL_RINGING:
1450 #ifdef DLINK_BUGGY_FIRMWARE
1451 transmit_notify_request(sub, "rt");
1453 if (!sub->sdpsent) { /* will hide the inband progress!!! */
1454 transmit_notify_request(sub, sub->parent->ncs ? "L/rt" : "G/rt");
1458 case AST_CONTROL_BUSY:
1459 transmit_notify_request(sub, "L/bz");
1461 case AST_CONTROL_INCOMPLETE:
1462 /* We do not currently support resetting of the Interdigit Timer, so treat
1463 * Incomplete control frames as a congestion response
1465 case AST_CONTROL_CONGESTION:
1466 transmit_notify_request(sub, sub->parent->ncs ? "L/cg" : "G/cg");
1468 case AST_CONTROL_HOLD:
1469 ast_moh_start(ast, data, NULL);
1471 case AST_CONTROL_UNHOLD:
1474 case AST_CONTROL_SRCUPDATE:
1475 ast_rtp_instance_update_source(sub->rtp);
1477 case AST_CONTROL_SRCCHANGE:
1478 ast_rtp_instance_change_source(sub->rtp);
1480 case AST_CONTROL_PROGRESS:
1481 case AST_CONTROL_PROCEEDING:
1482 transmit_modify_request(sub);
1484 transmit_notify_request(sub, "");
1487 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
1489 case AST_CONTROL_PVT_CAUSE_CODE:
1492 ast_mutex_unlock(&sub->lock);
1496 static struct ast_channel *mgcp_new(struct mgcp_subchannel *sub, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
1498 struct ast_format_cap *caps = NULL;
1499 struct ast_channel *tmp;
1500 struct ast_variable *v = NULL;
1501 struct mgcp_endpoint *i = sub->parent;
1502 struct ast_format *tmpfmt;
1504 caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1506 ast_log(LOG_ERROR, "Format capabilities could not be created\n");
1509 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);
1511 ast_log(LOG_WARNING, "Channel could not be created\n");
1516 ast_channel_stage_snapshot(tmp);
1517 ast_channel_tech_set(tmp, &mgcp_tech);
1518 if (ast_format_cap_count(i->cap)) {
1519 ast_format_cap_append_from_cap(caps, i->cap, AST_MEDIA_TYPE_UNKNOWN);
1521 ast_format_cap_append_from_cap(caps, global_capability, AST_MEDIA_TYPE_UNKNOWN);
1523 ast_channel_nativeformats_set(tmp, caps);
1526 ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(sub->rtp, 0));
1528 if (i->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID)) {
1529 i->dsp = ast_dsp_new();
1530 ast_dsp_set_features(i->dsp, DSP_FEATURE_DIGIT_DETECT);
1531 /* this is to prevent clipping of dtmf tones during dsp processing */
1532 ast_dsp_set_digitmode(i->dsp, DSP_DIGITMODE_NOQUELCH);
1536 if (state == AST_STATE_RING) {
1537 ast_channel_rings_set(tmp, 1);
1540 tmpfmt = ast_format_cap_get_format(ast_channel_nativeformats(tmp), 0);
1541 ast_channel_set_writeformat(tmp, tmpfmt);
1542 ast_channel_set_rawwriteformat(tmp, tmpfmt);
1543 ast_channel_set_readformat(tmp, tmpfmt);
1544 ast_channel_set_rawreadformat(tmp, tmpfmt);
1545 ao2_ref(tmpfmt, -1);
1546 ast_channel_tech_pvt_set(tmp, sub);
1547 if (!ast_strlen_zero(i->language))
1548 ast_channel_language_set(tmp, i->language);
1549 if (!ast_strlen_zero(i->accountcode))
1550 ast_channel_accountcode_set(tmp, i->accountcode);
1552 ast_channel_amaflags_set(tmp, i->amaflags);
1553 mgcp_set_owner(sub, tmp);
1554 ast_module_ref(ast_module_info->self);
1555 ast_channel_callgroup_set(tmp, i->callgroup);
1556 ast_channel_pickupgroup_set(tmp, i->pickupgroup);
1557 ast_channel_call_forward_set(tmp, i->call_forward);
1558 ast_channel_context_set(tmp, i->context);
1559 ast_channel_exten_set(tmp, i->exten);
1560 /* Don't use ast_set_callerid() here because it will
1561 * generate a needless NewCallerID event */
1562 if (!ast_strlen_zero(i->cid_num)) {
1563 ast_channel_caller(tmp)->ani.number.valid = 1;
1564 ast_channel_caller(tmp)->ani.number.str = ast_strdup(i->cid_num);
1568 ast_channel_adsicpe_set(tmp, AST_ADSI_UNAVAILABLE);
1570 ast_channel_priority_set(tmp, 1);
1572 /* Set channel variables for this call from configuration */
1573 for (v = i->chanvars ; v ; v = v->next) {
1574 char valuebuf[1024];
1575 pbx_builtin_setvar_helper(tmp, v->name, ast_get_encoded_str(v->value, valuebuf, sizeof(valuebuf)));
1579 ast_jb_configure(tmp, &global_jbconf);
1582 ast_channel_stage_snapshot_done(tmp);
1583 ast_channel_unlock(tmp);
1585 if (state != AST_STATE_DOWN) {
1586 if (ast_pbx_start(tmp)) {
1587 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
1592 ast_verb(3, "MGCP mgcp_new(%s) created in state: %s\n",
1593 ast_channel_name(tmp), ast_state2str(state));
1598 static char *get_sdp_by_line(char* line, char *name, int nameLen)
1600 if (strncasecmp(line, name, nameLen) == 0 && line[nameLen] == '=') {
1601 char *r = line + nameLen + 1;
1602 while (*r && (*r < 33)) ++r;
1608 static char *get_sdp(struct mgcp_request *req, char *name)
1611 int len = strlen(name);
1614 for (x = 0; x < req->lines; x++) {
1615 r = get_sdp_by_line(req->line[x], name, len);
1616 if (r[0] != '\0') return r;
1621 static void sdpLineNum_iterator_init(int *iterator)
1626 static char *get_sdp_iterate(int* iterator, struct mgcp_request *req, char *name)
1628 int len = strlen(name);
1630 while (*iterator < req->lines) {
1631 r = get_sdp_by_line(req->line[(*iterator)++], name, len);
1632 if (r[0] != '\0') return r;
1637 static char *__get_header(struct mgcp_request *req, char *name, int *start, char *def)
1640 int len = strlen(name);
1642 for (x = *start; x < req->headers; x++) {
1643 if (!strncasecmp(req->header[x], name, len) &&
1644 (req->header[x][len] == ':')) {
1645 r = req->header[x] + len + 1;
1646 while (*r && (*r < 33)) {
1653 /* Don't return NULL, so get_header is always a valid pointer */
1657 static char *get_header(struct mgcp_request *req, char *name)
1660 return __get_header(req, name, &start, "");
1663 /*! \brief get_csv: (SC:) get comma separated value */
1664 static char *get_csv(char *c, int *len, char **next)
1668 *next = NULL, *len = 0;
1669 if (!c) return NULL;
1671 while (*c && (*c < 33 || *c == ',')) {
1676 while (*c && (*c >= 33 && *c != ',')) {
1682 s = NULL, *next = NULL;
1688 static struct mgcp_gateway *find_realtime_gw(char *name, char *at, struct sockaddr_in *sin)
1690 struct mgcp_gateway *g = NULL;
1691 struct ast_variable *mgcpgwconfig = NULL;
1692 struct ast_variable *gwv, *epname = NULL;
1693 struct mgcp_endpoint *e;
1697 ast_debug(1, "*** find Realtime MGCPGW\n");
1699 if (!(i = ast_check_realtime("mgcpgw")) || !(j = ast_check_realtime("mgcpep"))) {
1703 if (ast_strlen_zero(at)) {
1704 ast_debug(1, "null gw name\n");
1708 if (!(mgcpgwconfig = ast_load_realtime("mgcpgw", "name", at, NULL))) {
1713 * \note This is a fairly odd way of instantiating lines. Instead of each
1714 * line created by virtue of being in the database (and loaded via
1715 * ast_load_realtime_multientry), this code forces a specific order with a
1716 * "lines" entry in the "mgcpgw" record. This has benefits, because as with
1717 * chan_dahdi, values are inherited across definitions. The downside is
1718 * that it's not as clear what the values will be simply by looking at a
1719 * single row in the database, and it's probable that the sanest configuration
1720 * should have the first column in the "mgcpep" table be "clearvars", with a
1721 * static value of "all", if any variables are set at all. It may be worth
1722 * making this assumption explicit in the code in the future, and then just
1723 * using ast_load_realtime_multientry for the "mgcpep" records.
1726 for (gwv = mgcpgwconfig; gwv; gwv = gwv->next) {
1727 if (!strcasecmp(gwv->name, "lines")) {
1728 ast_copy_string(lines, gwv->value, sizeof(lines));
1732 /* Position gwv at the end of the list */
1733 for (gwv = gwv && gwv->next ? gwv : mgcpgwconfig; gwv->next; gwv = gwv->next);
1735 if (!ast_strlen_zero(lines)) {
1736 AST_DECLARE_APP_ARGS(args,
1737 AST_APP_ARG(line)[100];
1739 AST_STANDARD_APP_ARGS(args, lines);
1740 for (i = 0; i < args.argc; i++) {
1741 gwv->next = ast_load_realtime("mgcpep", "name", at, "line", args.line[i], NULL);
1743 /* Remove "line" AND position gwv at the end of the list. */
1744 for (epname = NULL; gwv->next; gwv = gwv->next) {
1745 if (!strcasecmp(gwv->next->name, "line")) {
1746 /* Remove it from the list */
1748 gwv->next = gwv->next->next;
1751 /* Since "line" instantiates the configuration, we have to move it to the end. */
1754 epname->next = NULL;
1759 for (gwv = mgcpgwconfig; gwv; gwv = gwv->next) {
1760 ast_debug(1, "MGCP Realtime var: %s => %s\n", gwv->name, gwv->value);
1764 g = build_gateway(at, mgcpgwconfig);
1765 ast_variables_destroy(mgcpgwconfig);
1771 for (e = g->endpoints; e; e = e->next) {
1772 transmit_audit_endpoint(e);
1779 static struct mgcp_subchannel *find_subchannel_and_lock(char *name, int msgid, struct sockaddr_in *sin)
1781 struct mgcp_endpoint *p = NULL;
1782 struct mgcp_subchannel *sub = NULL;
1783 struct mgcp_gateway *g;
1785 char *at = NULL, *c;
1788 ast_copy_string(tmp, name, sizeof(tmp));
1789 at = strchr(tmp, '@');
1791 ast_log(LOG_NOTICE, "Endpoint '%s' has no at sign!\n", name);
1796 ast_mutex_lock(&gatelock);
1797 if (at && (at[0] == '[')) {
1799 c = strrchr(at, ']');
1804 for (g = gateways ? gateways : find_realtime_gw(name, at, sin); g; g = g->next ? g->next : find_realtime_gw(name, at, sin)) {
1805 if ((!name || !strcasecmp(g->name, at)) &&
1806 (sin || g->addr.sin_addr.s_addr || g->defaddr.sin_addr.s_addr)) {
1807 /* Found the gateway. If it's dynamic, save it's address -- now for the endpoint */
1808 if (sin && g->dynamic && name) {
1809 if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) ||
1810 (g->addr.sin_port != sin->sin_port)) {
1811 memcpy(&g->addr, sin, sizeof(g->addr));
1813 struct ast_sockaddr tmp1, tmp2;
1814 struct sockaddr_in tmp3 = {0,};
1816 tmp3.sin_addr = g->ourip;
1817 ast_sockaddr_from_sin(&tmp1, &g->addr);
1818 ast_sockaddr_from_sin(&tmp2, &tmp3);
1819 if (ast_ouraddrfor(&tmp1, &tmp2)) {
1820 memcpy(&g->ourip, &__ourip, sizeof(g->ourip));
1822 ast_sockaddr_to_sin(&tmp2, &tmp3);
1823 g->ourip = tmp3.sin_addr;
1825 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));
1827 /* not dynamic, check if the name matches */
1829 if (strcasecmp(g->name, at)) {
1832 /* not dynamic, no name, check if the addr matches */
1833 } else if (!name && sin) {
1834 if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) ||
1835 (g->addr.sin_port != sin->sin_port)) {
1841 for (p = g->endpoints; p; p = p->next) {
1842 ast_debug(1, "Searching on %s@%s for subchannel\n", p->name, g->name);
1847 } else if (name && !strcasecmp(p->name, tmp)) {
1848 ast_debug(1, "Coundn't determine subchannel, assuming current master %s@%s-%d\n",
1849 p->name, g->name, p->sub->id);
1856 ast_mutex_lock(&sub->lock);
1861 ast_mutex_unlock(&gatelock);
1865 ast_log(LOG_NOTICE, "Endpoint '%s' not found on gateway '%s'\n", tmp, at);
1867 ast_log(LOG_NOTICE, "Gateway '%s' (and thus its endpoint '%s') does not exist\n", at, tmp);
1874 static void parse(struct mgcp_request *req)
1876 /* Divide fields by NULL's */
1881 /* First header starts immediately */
1885 /* We've got a new header */
1887 ast_debug(3, "Header: %s (%d)\n", req->header[f], (int) strlen(req->header[f]));
1888 if (ast_strlen_zero(req->header[f])) {
1889 /* Line by itself means we're now in content */
1893 if (f >= MGCP_MAX_HEADERS - 1) {
1894 ast_log(LOG_WARNING, "Too many MGCP headers...\n");
1898 req->header[f] = c + 1;
1899 } else if (*c == '\r') {
1900 /* Ignore but eliminate \r's */
1904 /* Check for last header */
1905 if (!ast_strlen_zero(req->header[f])) {
1909 /* Now we process any mime content */
1914 /* We've got a new line */
1916 ast_debug(3, "Line: %s (%d)\n", req->line[f], (int) strlen(req->line[f]));
1917 if (f >= MGCP_MAX_LINES - 1) {
1918 ast_log(LOG_WARNING, "Too many SDP lines...\n");
1922 req->line[f] = c + 1;
1923 } else if (*c == '\r') {
1924 /* Ignore and eliminate \r's */
1928 /* Check for last line */
1929 if (!ast_strlen_zero(req->line[f])) {
1933 /* Parse up the initial header */
1935 while (*c && *c < 33) c++;
1936 /* First the verb */
1938 while (*c && (*c > 32)) c++;
1942 while (*c && (*c < 33)) c++;
1943 req->identifier = c;
1944 while (*c && (*c > 32)) c++;
1948 while (*c && (*c < 33)) c++;
1950 while (*c && (*c > 32)) c++;
1954 while (*c && (*c < 33)) c++;
1956 while (*c && (*c > 32)) c++;
1957 while (*c && (*c < 33)) c++;
1958 while (*c && (*c > 32)) c++;
1964 ast_debug(1, "Verb: '%s', Identifier: '%s', Endpoint: '%s', Version: '%s'\n",
1965 req->verb, req->identifier, req->endpoint, req->version);
1966 ast_debug(1, "%d headers, %d lines\n", req->headers, req->lines);
1968 ast_log(LOG_WARNING, "Odd content, extra stuff left over ('%s')\n", c);
1972 static int process_sdp(struct mgcp_subchannel *sub, struct mgcp_request *req)
1980 struct ast_format_cap *peercap;
1981 int peerNonCodecCapability;
1982 struct sockaddr_in sin;
1983 struct ast_sockaddr sin_tmp;
1985 struct ast_hostent ahp; struct hostent *hp;
1986 int codec, codec_count=0;
1988 struct mgcp_endpoint *p = sub->parent;
1989 struct ast_str *global_buf = ast_str_alloca(64);
1990 struct ast_str *peer_buf = ast_str_alloca(64);
1991 struct ast_str *pvt_buf = ast_str_alloca(64);
1993 /* Get codec and RTP info from SDP */
1994 m = get_sdp(req, "m");
1995 c = get_sdp(req, "c");
1996 if (ast_strlen_zero(m) || ast_strlen_zero(c)) {
1997 ast_log(LOG_WARNING, "Insufficient information for SDP (m = '%s', c = '%s')\n", m, c);
2000 if (sscanf(c, "IN IP4 %256s", host) != 1) {
2001 ast_log(LOG_WARNING, "Invalid host in c= line, '%s'\n", c);
2004 /* XXX This could block for a long time, and block the main thread! XXX */
2005 hp = ast_gethostbyname(host, &ahp);
2007 ast_log(LOG_WARNING, "Unable to lookup host in c= line, '%s'\n", c);
2010 if (sscanf(m, "audio %30d RTP/AVP %n", &portno, &len) != 1 || !len) {
2011 ast_log(LOG_WARNING, "Malformed media stream descriptor: %s\n", m);
2014 sin.sin_family = AF_INET;
2015 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
2016 sin.sin_port = htons(portno);
2017 ast_sockaddr_from_sin(&sin_tmp, &sin);
2018 ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
2019 ast_debug(3, "Peer RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
2020 /* Scan through the RTP payload types specified in a "m=" line: */
2021 ast_rtp_codecs_payloads_clear(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp);
2022 codecs = ast_strdupa(m + len);
2023 while (!ast_strlen_zero(codecs)) {
2024 if (sscanf(codecs, "%30d%n", &codec, &len) != 1) {
2028 ast_log(LOG_WARNING, "Error in codec string '%s' at '%s'\n", m, codecs);
2031 ast_rtp_codecs_payloads_set_m_type(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp, codec);
2036 /* Next, scan through each "a=rtpmap:" line, noting each */
2037 /* specified RTP payload type (with corresponding MIME subtype): */
2038 sdpLineNum_iterator_init(&iterator);
2039 while ((a = get_sdp_iterate(&iterator, req, "a"))[0] != '\0') {
2040 char* mimeSubtype = ast_strdupa(a); /* ensures we have enough space */
2041 if (sscanf(a, "rtpmap: %30d %127[^/]/", &codec, mimeSubtype) != 2)
2043 /* Note: should really look at the 'freq' and '#chans' params too */
2044 ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp, codec, "audio", mimeSubtype, 0);
2047 /* Now gather all of the codecs that were asked for: */
2048 if (!(peercap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
2051 ast_rtp_codecs_payload_formats(ast_rtp_instance_get_codecs(sub->rtp), peercap, &peerNonCodecCapability);
2052 ast_format_cap_get_compatible(global_capability, peercap, p->cap);
2053 ast_debug(1, "Capabilities: us - %s, them - %s, combined - %s\n",
2054 ast_format_cap_get_names(global_capability, &global_buf),
2055 ast_format_cap_get_names(peercap, &peer_buf),
2056 ast_format_cap_get_names(p->cap, &pvt_buf));
2057 ao2_ref(peercap, -1);
2059 ast_debug(1, "Non-codec capabilities: us - %d, them - %d, combined - %d\n",
2060 nonCodecCapability, peerNonCodecCapability, p->nonCodecCapability);
2061 if (!ast_format_cap_count(p->cap)) {
2062 ast_log(LOG_WARNING, "No compatible codecs!\n");
2068 static int add_header(struct mgcp_request *req, const char *var, const char *value)
2070 if (req->len >= sizeof(req->data) - 4) {
2071 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
2075 ast_log(LOG_WARNING, "Can't add more headers when lines have been added\n");
2078 req->header[req->headers] = req->data + req->len;
2079 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s: %s\r\n", var, value);
2080 req->len += strlen(req->header[req->headers]);
2081 if (req->headers < MGCP_MAX_HEADERS) {
2084 ast_log(LOG_WARNING, "Out of header space\n");
2090 static int add_line(struct mgcp_request *req, char *line)
2092 if (req->len >= sizeof(req->data) - 4) {
2093 ast_log(LOG_WARNING, "Out of space, can't add anymore\n");
2097 /* Add extra empty return */
2098 ast_copy_string(req->data + req->len, "\r\n", sizeof(req->data) - req->len);
2099 req->len += strlen(req->data + req->len);
2101 req->line[req->lines] = req->data + req->len;
2102 snprintf(req->line[req->lines], sizeof(req->data) - req->len, "%s", line);
2103 req->len += strlen(req->line[req->lines]);
2104 if (req->lines < MGCP_MAX_LINES) {
2107 ast_log(LOG_WARNING, "Out of line space\n");
2113 static int init_resp(struct mgcp_request *req, char *resp, struct mgcp_request *orig, char *resprest)
2115 /* Initialize a response */
2116 if (req->headers || req->len) {
2117 ast_log(LOG_WARNING, "Request already initialized?!?\n");
2120 req->header[req->headers] = req->data + req->len;
2121 snprintf(req->header[req->headers], sizeof(req->data) - req->len, "%s %s %s\r\n", resp, orig->identifier, resprest);
2122 req->len += strlen(req->header[req->headers]);
2123 if (req->headers < MGCP_MAX_HEADERS) {
2126 ast_log(LOG_WARNING, "Out of header space\n");
2131 static int init_req(struct mgcp_endpoint *p, struct mgcp_request *req, char *verb, unsigned int oseq)
2133 /* Initialize a response */
2134 if (req->headers || req->len) {
2135 ast_log(LOG_WARNING, "Request already initialized?!?\n");
2138 req->header[req->headers] = req->data + req->len;
2139 /* check if we need brackets around the gw name */
2140 if (p->parent->isnamedottedip) {
2141 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" : "");
2143 + 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" : "");
2145 req->len += strlen(req->header[req->headers]);
2146 if (req->headers < MGCP_MAX_HEADERS) {
2149 ast_log(LOG_WARNING, "Out of header space\n");
2155 static int respprep(struct mgcp_request *resp, struct mgcp_endpoint *p, char *msg, struct mgcp_request *req, char *msgrest)
2157 memset(resp, 0, sizeof(*resp));
2158 init_resp(resp, msg, req, msgrest);
2162 static int reqprep(struct mgcp_request *req, struct mgcp_endpoint *p, char *verb)
2165 memset(req, 0, sizeof(struct mgcp_request));
2166 ast_mutex_lock(&oseq_lock);
2168 if (oseq_global > 999999999) {
2172 ast_mutex_unlock(&oseq_lock);
2173 init_req(p, req, verb, oseq);
2177 static int transmit_response(struct mgcp_subchannel *sub, char *msg, struct mgcp_request *req, char *msgrest)
2179 struct mgcp_request resp;
2180 struct mgcp_endpoint *p = sub->parent;
2181 struct mgcp_response *mgr;
2187 respprep(&resp, p, msg, req, msgrest);
2188 if (!(mgr = ast_calloc(1, sizeof(*mgr) + resp.len + 1))) {
2189 return send_response(sub, &resp);
2191 /* Store MGCP response in case we have to retransmit */
2192 sscanf(req->identifier, "%30d", &mgr->seqno);
2193 time(&mgr->whensent);
2194 mgr->len = resp.len;
2195 memcpy(mgr->buf, resp.data, resp.len);
2196 mgr->buf[resp.len] = '\0';
2197 mgr->next = p->parent->responses;
2198 p->parent->responses = mgr;
2200 return send_response(sub, &resp);
2204 static int add_sdp(struct mgcp_request *resp, struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp)
2209 struct sockaddr_in sin;
2210 struct ast_sockaddr sin_tmp;
2219 struct sockaddr_in dest = { 0, };
2220 struct ast_sockaddr dest_tmp;
2221 struct mgcp_endpoint *p = sub->parent;
2222 /* XXX We break with the "recommendation" and send our IP, in order that our
2223 peer doesn't have to ast_gethostbyname() us XXX */
2226 ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
2229 ast_rtp_instance_get_local_address(sub->rtp, &sin_tmp);
2230 ast_sockaddr_to_sin(&sin_tmp, &sin);
2232 ast_rtp_instance_get_remote_address(sub->rtp, &dest_tmp);
2233 ast_sockaddr_to_sin(&dest_tmp, &dest);
2235 if (sub->tmpdest.sin_addr.s_addr) {
2236 dest.sin_addr = sub->tmpdest.sin_addr;
2237 dest.sin_port = sub->tmpdest.sin_port;
2238 /* Reset temporary destination */
2239 memset(&sub->tmpdest, 0, sizeof(sub->tmpdest));
2241 dest.sin_addr = p->parent->ourip;
2242 dest.sin_port = sin.sin_port;
2245 ast_debug(1, "We're at %s port %d\n", ast_inet_ntoa(p->parent->ourip), ntohs(sin.sin_port));
2246 ast_copy_string(v, "v=0\r\n", sizeof(v));
2247 snprintf(o, sizeof(o), "o=root %d %d IN IP4 %s\r\n", (int)getpid(), (int)getpid(), ast_inet_ntoa(dest.sin_addr));
2248 ast_copy_string(s, "s=session\r\n", sizeof(s));
2249 snprintf(c, sizeof(c), "c=IN IP4 %s\r\n", ast_inet_ntoa(dest.sin_addr));
2250 ast_copy_string(t, "t=0 0\r\n", sizeof(t));
2251 snprintf(m, sizeof(m), "m=audio %d RTP/AVP", ntohs(dest.sin_port));
2253 for (x = 0; x < ast_format_cap_count(p->cap); x++) {
2254 struct ast_format *format = ast_format_cap_get_format(p->cap, x);
2256 if (ast_format_get_type(format) != AST_MEDIA_TYPE_AUDIO) {
2257 ao2_ref(format, -1);
2261 ast_debug(1, "Answering with capability %s\n", ast_format_get_name(format));
2262 codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 1, format, 0);
2264 snprintf(costr, sizeof(costr), " %d", codec);
2265 strncat(m, costr, sizeof(m) - strlen(m) - 1);
2266 snprintf(costr, sizeof(costr), "a=rtpmap:%d %s/8000\r\n", codec, ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
2267 strncat(a, costr, sizeof(a) - strlen(a) - 1);
2270 ao2_ref(format, -1);
2273 for (x = 1LL; x <= AST_RTP_MAX; x <<= 1) {
2274 if (p->nonCodecCapability & x) {
2275 ast_debug(1, "Answering with non-codec capability %d\n", (int) x);
2276 codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(sub->rtp), 0, NULL, x);
2278 snprintf(costr, sizeof(costr), " %d", codec);
2279 strncat(m, costr, sizeof(m) - strlen(m) - 1);
2280 snprintf(costr, sizeof(costr), "a=rtpmap:%d %s/8000\r\n", codec, ast_rtp_lookup_mime_subtype2(0, NULL, x, 0));
2281 strncat(a, costr, sizeof(a) - strlen(a) - 1);
2282 if (x == AST_RTP_DTMF) {
2283 /* Indicate we support DTMF... Not sure about 16,
2284 but MSN supports it so dang it, we will too... */
2285 snprintf(costr, sizeof costr, "a=fmtp:%d 0-16\r\n", codec);
2286 strncat(a, costr, sizeof(a) - strlen(a) - 1);
2291 strncat(m, "\r\n", sizeof(m) - strlen(m) - 1);
2292 len = strlen(v) + strlen(s) + strlen(o) + strlen(c) + strlen(t) + strlen(m) + strlen(a);
2293 snprintf(costr, sizeof(costr), "%d", len);
2304 static int transmit_modify_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp, const struct ast_format_cap *codecs)
2306 struct mgcp_request resp;
2309 struct mgcp_endpoint *p = sub->parent;
2311 struct ast_sockaddr sub_tmpdest_tmp;
2314 if (ast_strlen_zero(sub->cxident) && rtp) {
2315 /* We don't have a CXident yet, store the destination and
2317 ast_rtp_instance_get_remote_address(rtp, &sub_tmpdest_tmp);
2318 ast_sockaddr_to_sin(&sub_tmpdest_tmp, &sub->tmpdest);
2321 ast_copy_string(local, "e:on, s:off, p:20", sizeof(local));
2323 for (i = 0; i < ast_format_cap_count(p->cap); i++) {
2324 struct ast_format *format = ast_format_cap_get_format(p->cap, i);
2326 if (ast_format_get_type(format) != AST_MEDIA_TYPE_AUDIO) {
2327 ao2_ref(format, -1);
2331 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
2332 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2334 ao2_ref(format, -1);
2338 if (sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) {
2339 snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
2340 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2344 ast_debug(1, "Waiting for opened gate...\n");
2351 oseq = reqprep(&resp, p, "MDCX");
2352 add_header(&resp, "C", sub->callid);
2353 add_header(&resp, "L", local);
2354 add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
2355 /* X header should not be sent. kept for compatibility */
2356 add_header(&resp, "X", sub->txident);
2357 add_header(&resp, "I", sub->cxident);
2358 /*add_header(&resp, "S", "");*/
2359 add_sdp(&resp, sub, rtp);
2360 /* fill in new fields */
2361 resp.cmd = MGCP_CMD_MDCX;
2363 return send_request(p, sub, &resp, oseq);
2366 static int transmit_connect_with_sdp(struct mgcp_subchannel *sub, struct ast_rtp_instance *rtp)
2368 struct mgcp_request resp;
2372 struct mgcp_endpoint *p = sub->parent;
2375 ast_debug(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n",
2376 p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2378 ast_copy_string(local, "e:on, s:off, p:20", sizeof(local));
2380 for (i = 0; i < ast_format_cap_count(p->cap); i++) {
2381 struct ast_format *format = ast_format_cap_get_format(p->cap, i);
2383 if (ast_format_get_type(format) != AST_MEDIA_TYPE_AUDIO) {
2384 ao2_ref(format, -1);
2388 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
2389 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2391 ao2_ref(format, -1);
2395 if(sub->gate->state == GATE_ALLOCATED) {
2396 snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
2397 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2401 oseq = reqprep(&resp, p, "CRCX");
2402 add_header(&resp, "C", sub->callid);
2403 add_header(&resp, "L", local);
2404 add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
2405 /* X header should not be sent. kept for compatibility */
2406 add_header(&resp, "X", sub->txident);
2407 /*add_header(&resp, "S", "");*/
2408 add_sdp(&resp, sub, rtp);
2409 /* fill in new fields */
2410 resp.cmd = MGCP_CMD_CRCX;
2412 return send_request(p, sub, &resp, oseq);
2415 static int mgcp_pktcgate_remove(struct cops_gate *gate)
2417 struct mgcp_subchannel *sub = gate->tech_pvt;
2423 ast_mutex_lock(&sub->lock);
2424 ast_debug(1, "Pktc: gate 0x%x deleted\n", gate->gateid);
2425 if (sub->gate->state != GATE_CLOSED && sub->parent->hangupongateremove) {
2428 ast_softhangup(sub->owner, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
2429 ast_channel_unlock(sub->owner);
2434 ast_mutex_unlock(&sub->lock);
2438 static int mgcp_pktcgate_open(struct cops_gate *gate)
2440 struct mgcp_subchannel *sub = gate->tech_pvt;
2444 ast_mutex_lock(&sub->lock);
2445 ast_debug(1, "Pktc: gate 0x%x open\n", gate->gateid);
2446 if (!sub->sdpsent) transmit_modify_with_sdp(sub, NULL, 0);
2447 ast_mutex_unlock(&sub->lock);
2451 static int mgcp_alloc_pktcgate(struct mgcp_subchannel *sub)
2453 struct mgcp_endpoint *p = sub->parent;
2454 sub->gate = ast_pktccops_gate_alloc(GATE_SET, NULL, ntohl(p->parent->addr.sin_addr.s_addr),
2455 8, 128000, 232, 0, 0, NULL, &mgcp_pktcgate_remove);
2460 sub->gate->tech_pvt = sub;
2461 sub->gate->gate_open = &mgcp_pktcgate_open;
2465 static int transmit_connect(struct mgcp_subchannel *sub)
2467 struct mgcp_request resp;
2471 struct ast_format *tmpfmt;
2472 struct mgcp_endpoint *p = sub->parent;
2475 ast_copy_string(local, "p:20, s:off, e:on", sizeof(local));
2477 for (x = 0; x < ast_format_cap_count(p->cap); x++) {
2478 tmpfmt = ast_format_cap_get_format(p->cap, x);
2480 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, tmpfmt, 0, 0));
2481 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2483 ao2_ref(tmpfmt, -1);
2486 ast_debug(3, "Creating connection for %s@%s-%d in cxmode: %s callid: %s\n",
2487 p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2489 oseq = reqprep(&resp, p, "CRCX");
2490 add_header(&resp, "C", sub->callid);
2491 add_header(&resp, "L", local);
2492 add_header(&resp, "M", "inactive");
2493 /* X header should not be sent. kept for compatibility */
2494 add_header(&resp, "X", sub->txident);
2495 /*add_header(&resp, "S", "");*/
2496 /* fill in new fields */
2497 resp.cmd = MGCP_CMD_CRCX;
2499 return send_request(p, sub, &resp, oseq);
2502 static int transmit_notify_request(struct mgcp_subchannel *sub, char *tone)
2504 struct mgcp_request resp;
2505 struct mgcp_endpoint *p = sub->parent;
2508 ast_debug(3, "MGCP Asked to indicate tone: %s on %s@%s-%d in cxmode: %s\n",
2509 tone, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode]);
2510 ast_copy_string(p->curtone, tone, sizeof(p->curtone));
2511 oseq = reqprep(&resp, p, "RQNT");
2512 add_header(&resp, "X", p->rqnt_ident);
2513 switch (p->hookstate) {
2515 add_header(&resp, "R", "L/hd(N)");
2518 add_header_offhook(sub, &resp, tone);
2521 if (!ast_strlen_zero(tone)) {
2522 add_header(&resp, "S", tone);
2524 /* fill in new fields */
2525 resp.cmd = MGCP_CMD_RQNT;
2527 return send_request(p, NULL, &resp, oseq);
2530 static int transmit_notify_request_with_callerid(struct mgcp_subchannel *sub, char *tone, char *callernum, char *callername)
2532 struct mgcp_request resp;
2535 struct timeval t = ast_tvnow();
2537 struct mgcp_endpoint *p = sub->parent;
2540 ast_localtime(&t, &tm, NULL);
2548 /* Keep track of last callerid for blacklist and callreturn */
2549 ast_copy_string(p->lastcallerid, l, sizeof(p->lastcallerid));
2551 snprintf(tone2, sizeof(tone2), "%s,L/ci(%02d/%02d/%02d/%02d,%s,%s)", tone,
2552 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, l, n);
2553 ast_copy_string(p->curtone, tone, sizeof(p->curtone));
2554 oseq = reqprep(&resp, p, "RQNT");
2555 add_header(&resp, "X", p->rqnt_ident);
2556 switch (p->hookstate) {
2558 add_header(&resp, "R", "L/hd(N)");
2561 add_header_offhook(sub, &resp, tone);
2564 if (!ast_strlen_zero(tone2)) {
2565 add_header(&resp, "S", tone2);
2567 ast_debug(3, "MGCP Asked to indicate tone: %s on %s@%s-%d in cxmode: %s\n",
2568 tone2, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode]);
2569 /* fill in new fields */
2570 resp.cmd = MGCP_CMD_RQNT;
2572 return send_request(p, NULL, &resp, oseq);
2575 static int transmit_modify_request(struct mgcp_subchannel *sub)
2577 struct mgcp_request resp;
2578 struct mgcp_endpoint *p = sub->parent;
2585 if (ast_strlen_zero(sub->cxident)) {
2586 /* We don't have a CXident yet, store the destination and
2590 ast_debug(3, "Modified %s@%s-%d with new mode: %s on callid: %s\n",
2591 p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2593 ast_copy_string(local, "", sizeof(local));
2594 for (i = 0; i < ast_format_cap_count(p->cap); i++) {
2595 struct ast_format *format = ast_format_cap_get_format(p->cap, i);
2597 if (p->ncs && !fc) {
2598 ast_format_cap_remove_by_type(p->cap, AST_MEDIA_TYPE_UNKNOWN);
2599 ast_format_cap_append(p->cap, format, 0); /* sb5120e bug */
2600 ao2_ref(format, -1);
2604 snprintf(tmp, sizeof(tmp), ", a:%s", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
2606 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2608 ao2_ref(format, -1);
2611 if (!sub->sdpsent) {
2613 if (sub->gate->state == GATE_ALLOCATED || sub->gate->state == GATE_OPEN) {
2614 snprintf(tmp, sizeof(tmp), ", dq-gi:%x", sub->gate->gateid);
2615 strncat(local, tmp, sizeof(local) - strlen(local) - 1);
2617 /* we still don't have gateid wait */
2623 oseq = reqprep(&resp, p, "MDCX");
2624 add_header(&resp, "C", sub->callid);
2625 if (!sub->sdpsent) {
2626 add_header(&resp, "L", local);
2628 add_header(&resp, "M", mgcp_cxmodes[sub->cxmode]);
2629 /* X header should not be sent. kept for compatibility */
2630 add_header(&resp, "X", sub->txident);
2631 add_header(&resp, "I", sub->cxident);
2632 switch (sub->parent->hookstate) {
2634 add_header(&resp, "R", "L/hd(N)");
2637 add_header_offhook(sub, &resp, "");
2640 if (!sub->sdpsent) {
2641 add_sdp(&resp, sub, NULL);
2644 /* fill in new fields */
2645 resp.cmd = MGCP_CMD_MDCX;
2647 return send_request(p, sub, &resp, oseq);
2651 static void add_header_offhook(struct mgcp_subchannel *sub, struct mgcp_request *resp, char *tone)
2653 struct mgcp_endpoint *p = sub->parent;
2654 char tone_indicate_end = 0;
2656 /* We also should check the tone to indicate, because it have no sense
2657 to request notify D/[0-9#*] (dtmf keys) if we are sending congestion
2658 tone for example G/cg */
2659 if (p && (!strcasecmp(tone, (p->ncs ? "L/ro" : "G/cg")))) {
2660 tone_indicate_end = 1;
2663 if (p && p->sub && p->sub->owner &&
2664 ast_channel_state(p->sub->owner) >= AST_STATE_RINGING &&
2665 (p->dtmfmode & (MGCP_DTMF_INBAND | MGCP_DTMF_HYBRID))) {
2666 add_header(resp, "R", "L/hu(N),L/hf(N)");
2668 } else if (!tone_indicate_end){
2669 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)"));
2671 ast_debug(1, "We don't want more digits if we will end the call\n");
2672 add_header(resp, "R", "L/hu(N),L/hf(N)");
2679 static int transmit_audit_endpoint(struct mgcp_endpoint *p)
2681 struct mgcp_request resp;
2683 oseq = reqprep(&resp, p, "AUEP");
2684 /* removed unknown param VS */
2685 /*add_header(&resp, "F", "A,R,D,S,X,N,I,T,O,ES,E,MD,M");*/
2686 add_header(&resp, "F", "A");
2687 /* fill in new fields */
2688 resp.cmd = MGCP_CMD_AUEP;
2690 return send_request(p, NULL, &resp, oseq);
2693 static int transmit_connection_del(struct mgcp_subchannel *sub)
2695 struct mgcp_endpoint *p = sub->parent;
2696 struct mgcp_request resp;
2699 ast_debug(3, "Delete connection %s %s@%s-%d with new mode: %s on callid: %s\n",
2700 sub->cxident, p->name, p->parent->name, sub->id, mgcp_cxmodes[sub->cxmode], sub->callid);
2701 oseq = reqprep(&resp, p, "DLCX");
2702 /* check if call id is avail */
2704 add_header(&resp, "C", sub->callid);
2705 /* X header should not be sent. kept for compatibility */
2706 add_header(&resp, "X", sub->txident);
2707 /* check if cxident is avail */
2708 if (sub->cxident[0])
2709 add_header(&resp, "I", sub->cxident);
2710 /* fill in new fields */
2711 resp.cmd = MGCP_CMD_DLCX;
2713 return send_request(p, sub, &resp, oseq);
2716 static int transmit_connection_del_w_params(struct mgcp_endpoint *p, char *callid, char *cxident)
2718 struct mgcp_request resp;
2721 ast_debug(3, "Delete connection %s %s@%s on callid: %s\n",
2722 cxident ? cxident : "", p->name, p->parent->name, callid ? callid : "");
2723 oseq = reqprep(&resp, p, "DLCX");
2724 /* check if call id is avail */
2725 if (callid && *callid)
2726 add_header(&resp, "C", callid);
2727 /* check if cxident is avail */
2728 if (cxident && *cxident)
2729 add_header(&resp, "I", cxident);
2730 /* fill in new fields */
2731 resp.cmd = MGCP_CMD_DLCX;
2733 return send_request(p, p->sub, &resp, oseq);
2736 /*! \brief dump_cmd_queues: (SC:) cleanup pending commands */
2737 static void dump_cmd_queues(struct mgcp_endpoint *p, struct mgcp_subchannel *sub)
2739 struct mgcp_request *t, *q;
2742 ast_mutex_lock(&p->rqnt_queue_lock);
2743 for (q = p->rqnt_queue; q; t = q->next, ast_free(q), q=t);
2744 p->rqnt_queue = NULL;
2745 ast_mutex_unlock(&p->rqnt_queue_lock);
2747 ast_mutex_lock(&p->cmd_queue_lock);
2748 for (q = p->cmd_queue; q; t = q->next, ast_free(q), q=t);
2749 p->cmd_queue = NULL;
2750 ast_mutex_unlock(&p->cmd_queue_lock);
2752 ast_mutex_lock(&p->sub->cx_queue_lock);
2753 for (q = p->sub->cx_queue; q; t = q->next, ast_free(q), q=t);
2754 p->sub->cx_queue = NULL;
2755 ast_mutex_unlock(&p->sub->cx_queue_lock);
2757 ast_mutex_lock(&p->sub->next->cx_queue_lock);
2758 for (q = p->sub->next->cx_queue; q; t = q->next, ast_free(q), q=t);
2759 p->sub->next->cx_queue = NULL;
2760 ast_mutex_unlock(&p->sub->next->cx_queue_lock);
2762 ast_mutex_lock(&sub->cx_queue_lock);
2763 for (q = sub->cx_queue; q; t = q->next, ast_free(q), q=t);
2764 sub->cx_queue = NULL;
2765 ast_mutex_unlock(&sub->cx_queue_lock);
2770 /*! \brief find_command: (SC:) remove command transaction from queue */
2771 static struct mgcp_request *find_command(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
2772 struct mgcp_request **queue, ast_mutex_t *l, int ident)
2774 struct mgcp_request *prev, *req;
2777 for (prev = NULL, req = *queue; req; prev = req, req = req->next) {
2778 if (req->trid == ident) {
2779 /* remove from queue */
2783 prev->next = req->next;
2785 /* send next pending command */
2787 ast_debug(1, "Posting Queued Request:\n%s to %s:%d\n", (*queue)->data,
2788 ast_inet_ntoa(p->parent->addr.sin_addr), ntohs(p->parent->addr.sin_port));
2790 mgcp_postrequest(p, sub, (*queue)->data, (*queue)->len, (*queue)->trid);
2795 ast_mutex_unlock(l);
2799 /* modified for new transport mechanism */
2800 static void handle_response(struct mgcp_endpoint *p, struct mgcp_subchannel *sub,
2801 int result, unsigned int ident, struct mgcp_request *resp)
2804 struct mgcp_request *req;
2805 struct mgcp_gateway *gw = p->parent;
2808 /* provisional response */
2812 if (p->slowsequence)
2813 req = find_command(p, sub, &p->cmd_queue, &p->cmd_queue_lock, ident);
2815 req = find_command(p, sub, &sub->cx_queue, &sub->cx_queue_lock, ident);
2816 else if (!(req = find_command(p, sub, &p->rqnt_queue, &p->rqnt_queue_lock, ident)))
2817 req = find_command(p, sub, &p->cmd_queue, &p->cmd_queue_lock, ident);
2820 ast_verb(3, "No command found on [%s] for transaction %u. Ignoring...\n",
2825 if (p && (result >= 400) && (result <= 599)) {
2828 p->hookstate = MGCP_OFFHOOK;
2831 p->hookstate = MGCP_ONHOOK;
2834 ast_log(LOG_NOTICE, "Transaction %u timed out\n", ident);
2837 ast_log(LOG_NOTICE, "Transaction %u aborted\n", ident);
2841 if (!sub->cxident[0] && (req->cmd == MGCP_CMD_CRCX)) {
2842 ast_log(LOG_NOTICE, "DLCX for all connections on %s due to error %d\n", gw->name, result);
2843 transmit_connection_del(sub);
2846 ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
2847 result, p->name, p->parent->name, sub ? sub->id:-1);
2848 mgcp_queue_hangup(sub);
2851 if (p->sub->next->owner) {
2852 ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
2853 result, p->name, p->parent->name, sub ? sub->id:-1);
2854 mgcp_queue_hangup(p->sub);
2857 if (p->sub->owner) {
2858 ast_log(LOG_NOTICE, "Terminating on result %d from %s@%s-%d\n",
2859 result, p->name, p->parent->name, sub ? sub->id:-1);
2860 mgcp_queue_hangup(p->sub);
2863 dump_cmd_queues(p, NULL);
2869 if (result == 200 && (req->cmd == MGCP_CMD_CRCX || req->cmd == MGCP_CMD_MDCX)) {
2871 transmit_response(sub, "000", resp, "OK");
2872 if (sub->owner && ast_channel_state(sub->owner) == AST_STATE_RINGING) {
2873 ast_queue_control(sub->owner, AST_CONTROL_RINGING);
2877 if (req->cmd == MGCP_CMD_CRCX) {
2878 if ((c = get_header(resp, "I"))) {
2879 if (!ast_strlen_zero(c) && sub) {
2880 /* if we are hanging up do not process this conn. */
2882 if (!ast_strlen_zero(sub->cxident)) {
2883 if (strcasecmp(c, sub->cxident)) {
2884 ast_log(LOG_WARNING, "Subchannel already has a cxident. sub->cxident: %s requested %s\n", sub->cxident, c);
2887 ast_copy_string(sub->cxident, c, sizeof(sub->cxident));
2888 if (sub->tmpdest.sin_addr.s_addr) {
2889 transmit_modify_with_sdp(sub, NULL, 0);
2892 /* XXX delete this one
2893 callid and conn id may already be lost.
2894 so the following del conn may have a side effect of
2895 cleaning up the next subchannel */
2896 transmit_connection_del(sub);
2902 if (req->cmd == MGCP_CMD_AUEP) {
2903 /* check stale connection ids */
2904 if ((c = get_header(resp, "I"))) {
2907 while ((v = get_csv(c, &len, &n))) {
2909 if (strncasecmp(v, p->sub->cxident, len) &&
2910 strncasecmp(v, p->sub->next->cxident, len)) {
2911 /* connection id not found. delete it */
2912 char cxident[80] = "";
2914 if (len > (sizeof(cxident) - 1))
2915 len = sizeof(cxident) - 1;
2916 ast_copy_string(cxident, v, len);
2917 ast_verb(3, "Non existing connection id %s on %s@%s \n",
2918 cxident, p->name, gw->name);
2919 transmit_connection_del_w_params(p, NULL, cxident);
2926 /* Try to determine the hookstate returned from an audit endpoint command */
2927 if ((c = get_header(resp, "ES"))) {
2928 if (!ast_strlen_zero(c)) {
2929 if (strstr(c, "hu")) {
2930 if (p->hookstate != MGCP_ONHOOK) {
2931 /* XXX cleanup if we think we are offhook XXX */
2932 if ((p->sub->owner || p->sub->next->owner ) &&
2933 p->hookstate == MGCP_OFFHOOK)
2934 mgcp_queue_hangup(sub);
2935 p->hookstate = MGCP_ONHOOK;
2937 /* update the requested events according to the new hookstate */
2938 transmit_notify_request(p->sub, "");
2940 ast_verb(3, "Setting hookstate of %s@%s to ONHOOK\n", p->name, gw->name);
2942 } else if (strstr(c, "hd")) {
2943 if (p->hookstate != MGCP_OFFHOOK) {
2944 p->hookstate = MGCP_OFFHOOK;
2946 /* update the requested events according to the new hookstate */
2947 transmit_notify_request(p->sub, "");
2949 ast_verb(3, "Setting hookstate of %s@%s to OFFHOOK\n", p->name, gw->name);
2956 if (resp && resp->lines) {
2957 /* do not process sdp if we are hanging up. this may be a late response */
2958 if (sub && sub->owner) {
2962 process_sdp(sub, resp);
2970 static void start_rtp(struct mgcp_subchannel *sub)
2972 struct ast_sockaddr bindaddr_tmp;
2974 ast_mutex_lock(&sub->lock);
2975 /* check again to be on the safe side */
2977 ast_rtp_instance_destroy(sub->rtp);
2980 /* Allocate the RTP now */
2981 ast_sockaddr_from_sin(&bindaddr_tmp, &bindaddr);
2982 sub->rtp = ast_rtp_instance_new("asterisk", sched, &bindaddr_tmp, NULL);
2983 if (sub->rtp && sub->owner)
2984 ast_channel_set_fd(sub->owner, 0, ast_rtp_instance_fd(sub->rtp, 0));
2986 ast_rtp_instance_set_qos(sub->rtp, qos.tos_audio, qos.cos_audio, "MGCP RTP");
2987 ast_rtp_instance_set_prop(sub->rtp, AST_RTP_PROPERTY_NAT, sub->nat);
2989 /* Make a call*ID */
2990 snprintf(sub->callid, sizeof(sub->callid), "%08lx%s", (unsigned long)ast_random(), sub->txident);
2991 /* Transmit the connection create */
2992 if(!sub->parent->pktcgatealloc) {
2993 transmit_connect_with_sdp(sub, NULL);
2995 transmit_connect(sub);
2997 if(!mgcp_alloc_pktcgate(sub))
2998 mgcp_queue_hangup(sub);
3000 ast_mutex_unlock(&sub->lock);
3003 static void *mgcp_ss(void *data)
3005 struct ast_channel *chan = data;
3006 struct mgcp_subchannel *sub = ast_channel_tech_pvt(chan);
3007 struct mgcp_endpoint *p = sub->parent;
3008 /* char exten[AST_MAX_EXTENSION] = ""; */
3010 int timeout = firstdigittimeout;
3013 int loop_pause = 100;
3014 RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
3015 const char *pickupexten;
3017 len = strlen(p->dtmf_buf);
3019 ast_channel_lock(chan);
3020 pickup_cfg = ast_get_chan_features_pickup_config(chan);
3022 ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
3025 pickupexten = ast_strdupa(pickup_cfg->pickupexten);
3027 ast_channel_unlock(chan);
3029 while (len < AST_MAX_EXTENSION - 1) {
3030 ast_debug(1, "Dtmf buffer '%s' for '%s@%s'\n", p->dtmf_buf, p->name, p->parent->name);
3031 res = 1; /* Assume that we will get a digit */
3032 while (strlen(p->dtmf_buf) == len) {
3033 ast_safe_sleep(chan, loop_pause);
3034 timeout -= loop_pause;
3043 len = strlen(p->dtmf_buf);
3045 if (!ast_ignore_pattern(ast_channel_context(chan), p->dtmf_buf)) {
3046 /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
3047 ast_indicate(chan, -1);
3049 /* XXX Redundant? We should already be playing dialtone */
3050 /*tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALTONE);*/
3051 transmit_notify_request(sub, "L/dl");
3053 if (ast_exists_extension(chan, ast_channel_context(chan), p->dtmf_buf, 1, p->cid_num)) {
3054 if (!res || !ast_matchmore_extension(chan, ast_channel_context(chan), p->dtmf_buf, 1, p->cid_num)) {
3056 /* Record this as the forwarding extension */
3057 ast_copy_string(p->call_forward, p->dtmf_buf, sizeof(p->call_forward));
3058 ast_verb(3, "Setting call forward to '%s' on channel %s\n",
3059 p->call_forward, ast_channel_name(chan));
3060 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALRECALL);*/
3061 transmit_notify_request(sub, "L/sl");
3065 /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
3066 ast_indicate(chan, -1);
3068 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
3069 /*res = tone_zone_play_tone(p->subs[index].zfd, DAHDI_TONE_DIALTONE);*/
3070 transmit_notify_request(sub, "L/dl");
3074 /*res = tone_zone_play_tone(p->subs[index].zfd, -1);*/
3075 ast_indicate(chan, -1);
3076 ast_channel_lock(chan);
3077 ast_channel_exten_set(chan, p->dtmf_buf);
3078 ast_channel_dialed(chan)->number.str = ast_strdup(p->dtmf_buf);
3079 memset(p->dtmf_buf, 0, sizeof(p->dtmf_buf));
3080 ast_set_callerid(chan,
3081 p->hidecallerid ? "" : p->cid_num,
3082 p->hidecallerid ? "" : p->cid_name,
3083 ast_channel_caller(chan)->ani.number.valid ? NULL : p->cid_num);
3084 ast_setstate(chan, AST_STATE_RING);
3085 ast_channel_unlock(chan);
3086 if (p->dtmfmode & MGCP_DTMF_HYBRID) {
3087 p->dtmfmode |= MGCP_DTMF_INBAND;
3088 ast_indicate(chan, -1);
3090 res = ast_pbx_run(chan);