blob: 607e230ffacb8216a56c168c509d2cd4c90655e2 [file] [log] [blame]
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +01005 * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2011 by On-Waves
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010021 *
22 */
23
24#include <ctype.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <time.h>
29#include <limits.h>
30#include <unistd.h>
31
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/core/msgb.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/select.h>
Holger Hans Peter Freyther16b7f5c2011-02-28 22:06:47 +010035
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010036#include <openbsc/mgcp.h>
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010037#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010038
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010039/**
40 * Macro for tokenizing MGCP messages and SDP in one go.
41 *
42 */
43#define MSG_TOKENIZE_START \
44 line_start = 0; \
45 for (i = 0; i < msgb_l3len(msg); ++i) { \
46 /* we have a line end */ \
47 if (msg->l3h[i] == '\n') { \
48 /* skip the first line */ \
49 if (line_start == 0) { \
50 line_start = i + 1; \
51 continue; \
52 } \
53 \
54 /* check if we have a proper param */ \
55 if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \
56 } else if (i - line_start > 2 \
57 && islower(msg->l3h[line_start]) \
58 && msg->l3h[line_start + 1] == '=') { \
59 } else if (i - line_start < 3 \
60 || msg->l3h[line_start + 1] != ':' \
61 || msg->l3h[line_start + 2] != ' ') \
62 goto error; \
63 \
64 msg->l3h[i] = '\0'; \
65 if (msg->l3h[i-1] == '\r') \
66 msg->l3h[i-1] = '\0';
67
68#define MSG_TOKENIZE_END \
69 line_start = i + 1; \
70 } \
71 }
72
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +080073static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010074
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010075struct mgcp_request {
76 char *name;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010077 struct msgb *(*handle_request) (struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010078 char *debug_name;
79};
80
81#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
82 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
83
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010084static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg);
85static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg);
86static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg);
87static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freyther9bdcc9c2010-03-31 06:39:35 +020088static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freyther6e94d6d2011-01-25 23:33:54 +010089static struct msgb *handle_noti_req(struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010090
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +080091static void create_transcoder(struct mgcp_endpoint *endp);
92static void delete_transcoder(struct mgcp_endpoint *endp);
93
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +080094static uint32_t generate_call_id(struct mgcp_config *cfg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010095{
96 int i;
97
98 /* use the call id */
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010099 ++cfg->last_call_id;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100100
101 /* handle wrap around */
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100102 if (cfg->last_call_id == CI_UNUSED)
103 ++cfg->last_call_id;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100104
105 /* callstack can only be of size number_of_endpoints */
106 /* verify that the call id is free, e.g. in case of overrun */
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100107 for (i = 1; i < cfg->trunk.number_endpoints; ++i)
108 if (cfg->trunk.endpoints[i].ci == cfg->last_call_id)
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100109 return generate_call_id(cfg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100110
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100111 return cfg->last_call_id;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100112}
113
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100114/*
115 * array of function pointers for handling various
116 * messages. In the future this might be binary sorted
117 * for performance reasons.
118 */
119static const struct mgcp_request mgcp_requests [] = {
120 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
121 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
122 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
123 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
Holger Hans Peter Freyther6e94d6d2011-01-25 23:33:54 +0100124 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Holger Hans Peter Freyther9bdcc9c2010-03-31 06:39:35 +0200125
126 /* SPEC extension */
127 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100128};
129
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100130static struct msgb *mgcp_msgb_alloc(void)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100131{
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100132 struct msgb *msg;
133 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
134 if (!msg)
135 LOGP(DMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
136
137 return msg;
138}
139
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100140struct msgb *mgcp_create_response_with_data(int code, const char *txt,
141 const char *msg, const char *trans,
142 const char *data)
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100143{
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100144 int len;
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100145 struct msgb *res;
146
147 res = mgcp_msgb_alloc();
148 if (!res)
149 return NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100150
151 if (data) {
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100152 len = snprintf((char *) res->data, 2048, "%d %s%s\r\n%s", code, trans, txt, data);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100153 } else {
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100154 len = snprintf((char *) res->data, 2048, "%d %s%s\r\n", code, trans, txt);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100155 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100156
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100157 res->l2h = msgb_put(res, len);
Holger Hans Peter Freyther63f2db22010-02-26 13:30:57 +0100158 LOGP(DMGCP, LOGL_DEBUG, "Sending response: code: %d for '%s'\n", code, res->l2h);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100159 return res;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100160}
161
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100162static struct msgb *create_ok_response(int code, const char *msg, const char *trans)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100163{
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100164 return mgcp_create_response_with_data(code, " OK", msg, trans, NULL);
165}
166
167static struct msgb *create_err_response(int code, const char *msg, const char *trans)
168{
169 return mgcp_create_response_with_data(code, " FAIL", msg, trans, NULL);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100170}
171
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100172static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
173 const char *msg, const char *trans_id)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100174{
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100175 const char *addr = endp->cfg->local_ip;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100176 char sdp_record[4096];
177
178 if (!addr)
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100179 addr = endp->cfg->source_addr;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100180
181 snprintf(sdp_record, sizeof(sdp_record) - 1,
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800182 "I: %u\n\n"
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100183 "v=0\r\n"
Holger Hans Peter Freytherc7e49d32011-07-20 20:43:15 +0200184 "o=- %u 23 IN IP4 %s\r\n"
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100185 "c=IN IP4 %s\r\n"
Holger Hans Peter Freytherc7e49d32011-07-20 20:43:15 +0200186 "t=0 0\r\n"
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100187 "m=audio %d RTP/AVP %d\r\n"
188 "a=rtpmap:%d %s\r\n",
Holger Hans Peter Freytherc7e49d32011-07-20 20:43:15 +0200189 endp->ci, endp->ci, addr, addr,
190 endp->net_end.local_port, endp->bts_end.payload_type,
191 endp->bts_end.payload_type, endp->tcfg->audio_name);
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100192 return mgcp_create_response_with_data(200, " OK", msg, trans_id, sdp_record);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100193}
194
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100195/*
196 * handle incoming messages:
197 * - this can be a command (four letters, space, transaction id)
198 * - or a response (three numbers, space, transaction id)
199 */
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100200struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100201{
202 int code;
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100203 struct msgb *resp = NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100204
Holger Hans Peter Freytherf36a11a2010-03-31 13:26:46 +0200205 if (msgb_l2len(msg) < 4) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100206 LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100207 return NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100208 }
209
210 /* attempt to treat it as a response */
Holger Hans Peter Freytherf36a11a2010-03-31 13:26:46 +0200211 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Holger Hans Peter Freyther63f2db22010-02-26 13:30:57 +0100212 LOGP(DMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100213 } else {
214 int i, handled = 0;
215 msg->l3h = &msg->l2h[4];
216 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
Holger Hans Peter Freytherf36a11a2010-03-31 13:26:46 +0200217 if (strncmp(mgcp_requests[i].name, (const char *) &msg->l2h[0], 4) == 0) {
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100218 handled = 1;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100219 resp = mgcp_requests[i].handle_request(cfg, msg);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100220 break;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100221 }
222 if (!handled) {
Holger Hans Peter Freytherf36a11a2010-03-31 13:26:46 +0200223 LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100224 }
225 }
226
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100227 return resp;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100228}
229
230/* string tokenizer for the poor */
231static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
232{
233 int i, found = 0;
234
235 int whitespace = 1;
236 for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
237 /* if we have a space we found an end */
238 if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
239 if (!whitespace) {
240 ++found;
241 whitespace = 1;
242 ptrs->length = i - ptrs->start - 1;
243 ++ptrs;
244 --ptrs_length;
245 } else {
246 /* skip any number of whitespace */
247 }
248
249 /* line end... stop */
250 if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
251 break;
252 } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
253 /* line end, be done */
254 break;
255 } else if (whitespace) {
256 whitespace = 0;
257 ptrs->start = i;
258 }
259 }
260
261 if (ptrs_length == 0)
262 return -1;
263 return found;
264}
265
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100266/**
267 * We have a null terminated string with the endpoint name here. We only
268 * support two kinds. Simple ones as seen on the BSC level and the ones
269 * seen on the trunk side.
270 */
271static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg,
272 const char *mgcp)
273{
274 char *rest = NULL;
Holger Hans Peter Freyther74e61112011-02-28 14:13:47 +0100275 struct mgcp_trunk_config *tcfg;
276 int trunk, endp;
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100277
278 trunk = strtoul(mgcp + 6, &rest, 10);
Holger Hans Peter Freytherf43f2fc2011-01-07 11:30:21 +0100279 if (rest == NULL || rest[0] != '/' || trunk < 1) {
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100280 LOGP(DMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
281 return NULL;
282 }
283
284 endp = strtoul(rest + 1, &rest, 10);
285 if (rest == NULL || rest[0] != '@') {
Holger Hans Peter Freytherf43f2fc2011-01-07 11:30:21 +0100286 LOGP(DMGCP, LOGL_ERROR, "Wrong endpoint name '%s'\n", mgcp);
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100287 return NULL;
288 }
289
290 /* signalling is on timeslot 1 */
291 if (endp == 1)
292 return NULL;
293
Holger Hans Peter Freyther74e61112011-02-28 14:13:47 +0100294 tcfg = mgcp_trunk_num(cfg, trunk);
295 if (!tcfg) {
296 LOGP(DMGCP, LOGL_ERROR, "The trunk %d is not declared.\n", trunk);
297 return NULL;
298 }
299
300 if (!tcfg->endpoints) {
301 LOGP(DMGCP, LOGL_ERROR, "Endpoints of trunk %d not allocated.\n", trunk);
302 return NULL;
303 }
304
305 if (endp < 1 || endp >= tcfg->number_endpoints) {
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100306 LOGP(DMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n", mgcp);
Holger Hans Peter Freyther45c21842011-01-07 11:36:54 +0100307 return NULL;
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100308 }
309
Holger Hans Peter Freyther74e61112011-02-28 14:13:47 +0100310 return &tcfg->endpoints[endp];
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100311}
312
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100313static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100314{
315 char *endptr = NULL;
316 unsigned int gw = INT_MAX;
317
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100318 if (strncmp(mgcp, "ds/e1", 5) == 0) {
319 return find_e1_endpoint(cfg, mgcp);
320 } else {
321 gw = strtoul(mgcp, &endptr, 16);
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100322 if (gw > 0 && gw < cfg->trunk.number_endpoints && strcmp(endptr, "@mgw") == 0)
323 return &cfg->trunk.endpoints[gw];
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100324 }
325
Holger Hans Peter Freyther9f239a22011-01-06 19:32:52 +0100326 LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp);
327 return NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100328}
329
Holger Hans Peter Freytherf2f15912010-04-01 03:27:04 +0200330int mgcp_analyze_header(struct mgcp_config *cfg, struct msgb *msg,
331 struct mgcp_msg_ptr *ptr, int size,
332 const char **transaction_id, struct mgcp_endpoint **endp)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100333{
334 int found;
335
Holger Hans Peter Freythera820c5f2010-02-26 13:32:55 +0100336 *transaction_id = "000000";
337
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100338 if (size < 3) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100339 LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100340 return -1;
341 }
342
343 found = find_msg_pointers(msg, ptr, size);
344
Holger Hans Peter Freythera820c5f2010-02-26 13:32:55 +0100345 if (found <= 3) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100346 LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100347 return -1;
348 }
349
350 /*
351 * replace the space with \0. the main method gurantess that
352 * we still have + 1 for null termination
353 */
354 msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
355 msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
356 msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
357 msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
358
359 if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
360 || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100361 LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100362 (const char *)&msg->l3h[ptr[3].start],
363 (const char *)&msg->l3h[ptr[2].start]);
364 return -1;
365 }
366
367 *transaction_id = (const char *)&msg->l3h[ptr[0].start];
Holger Hans Peter Freytherf2f15912010-04-01 03:27:04 +0200368 if (endp) {
369 *endp = find_endpoint(cfg, (const char *)&msg->l3h[ptr[1].start]);
370 return *endp == NULL;
371 }
372 return 0;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100373}
374
375static int verify_call_id(const struct mgcp_endpoint *endp,
376 const char *callid)
377{
378 if (strcmp(endp->callid, callid) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100379 LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100380 ENDPOINT_NUMBER(endp), endp->callid, callid);
381 return -1;
382 }
383
384 return 0;
385}
386
387static int verify_ci(const struct mgcp_endpoint *endp,
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800388 const char *_ci)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100389{
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800390 uint32_t ci = strtoul(_ci, NULL, 10);
391
392 if (ci != endp->ci) {
393 LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %u != %s\n",
394 ENDPOINT_NUMBER(endp), endp->ci, _ci);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100395 return -1;
396 }
397
398 return 0;
399}
400
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100401static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100402{
403 struct mgcp_msg_ptr data_ptrs[6];
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100404 int found;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100405 const char *trans_id;
406 struct mgcp_endpoint *endp;
407
Holger Hans Peter Freytherf2f15912010-04-01 03:27:04 +0200408 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100409 if (found != 0)
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100410 return create_err_response(500, "AUEP", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100411 else
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100412 return create_ok_response(200, "AUEP", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100413}
414
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800415static int parse_conn_mode(const char *msg, int *conn_mode)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100416{
417 int ret = 0;
418 if (strcmp(msg, "recvonly") == 0)
419 *conn_mode = MGCP_CONN_RECV_ONLY;
420 else if (strcmp(msg, "sendrecv") == 0)
421 *conn_mode = MGCP_CONN_RECV_SEND;
Holger Hans Peter Freythere02860a2010-09-18 20:21:44 +0800422 else if (strcmp(msg, "sendonly") == 0)
423 *conn_mode = MGCP_CONN_SEND_ONLY;
Holger Hans Peter Freyther98a38772010-08-03 02:27:21 +0800424 else if (strcmp(msg, "loopback") == 0)
425 *conn_mode = MGCP_CONN_LOOPBACK;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100426 else {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100427 LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100428 ret = -1;
429 }
430
431 return ret;
432}
433
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800434static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_rtp_end *end,
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800435 struct mgcp_port_range *range,
436 int (*alloc)(struct mgcp_endpoint *endp, int port))
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800437{
438 int i;
439
440 if (range->mode == PORT_ALLOC_STATIC) {
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800441 end->local_alloc = PORT_ALLOC_STATIC;
442 return 0;
443 }
444
445 /* attempt to find a port */
446 for (i = 0; i < 200; ++i) {
447 int rc;
448
449 if (range->last_port >= range->range_end)
450 range->last_port = range->range_start;
451
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800452 rc = alloc(endp, range->last_port);
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800453
454 range->last_port += 2;
455 if (rc == 0) {
456 end->local_alloc = PORT_ALLOC_DYNAMIC;
457 return 0;
458 }
459
460 }
461
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800462 LOGP(DMGCP, LOGL_ERROR, "Allocating a RTP/RTCP port failed 200 times 0x%x.\n",
463 ENDPOINT_NUMBER(endp));
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800464 return -1;
465}
466
467static int allocate_ports(struct mgcp_endpoint *endp)
468{
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800469 if (allocate_port(endp, &endp->net_end, &endp->cfg->net_ports,
470 mgcp_bind_net_rtp_port) != 0)
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800471 return -1;
472
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800473 if (allocate_port(endp, &endp->bts_end, &endp->cfg->bts_ports,
474 mgcp_bind_bts_rtp_port) != 0) {
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800475 mgcp_rtp_end_reset(&endp->net_end);
476 return -1;
477 }
478
Holger Hans Peter Freyther69906872011-02-28 14:51:48 +0100479 if (endp->cfg->transcoder_ip && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
Holger Hans Peter Freyther21262332010-11-01 20:53:31 +0100480 if (allocate_port(endp, &endp->trans_net,
Holger Hans Peter Freytherb54048f2010-11-01 19:57:50 +0100481 &endp->cfg->transcoder_ports,
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100482 mgcp_bind_trans_net_rtp_port) != 0) {
Holger Hans Peter Freytherb54048f2010-11-01 19:57:50 +0100483 mgcp_rtp_end_reset(&endp->net_end);
484 mgcp_rtp_end_reset(&endp->bts_end);
485 return -1;
486 }
487
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +0100488 if (allocate_port(endp, &endp->trans_bts,
489 &endp->cfg->transcoder_ports,
490 mgcp_bind_trans_bts_rtp_port) != 0) {
491 mgcp_rtp_end_reset(&endp->net_end);
492 mgcp_rtp_end_reset(&endp->bts_end);
493 mgcp_rtp_end_reset(&endp->trans_net);
494 return -1;
495 }
496
Holger Hans Peter Freytherb54048f2010-11-01 19:57:50 +0100497 /* remember that we have set up transcoding */
498 endp->is_transcoded = 1;
Holger Hans Peter Freyther218f8562010-09-18 02:30:02 +0800499 }
500
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800501 return 0;
502}
503
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100504static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100505{
506 struct mgcp_msg_ptr data_ptrs[6];
507 int found, i, line_start;
508 const char *trans_id;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100509 struct mgcp_trunk_config *tcfg;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100510 struct mgcp_endpoint *endp;
Holger Hans Peter Freyther9e9392d2010-08-08 16:24:48 +0800511 int error_code = 400;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100512
Holger Hans Peter Freyther3488c3d2011-07-20 20:44:54 +0200513 const char *local_options = NULL;
514 const char *callid = NULL;
515 const char *mode = NULL;
516
517
Holger Hans Peter Freytherf2f15912010-04-01 03:27:04 +0200518 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100519 if (found != 0)
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100520 return create_err_response(510, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100521
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100522 tcfg = endp->tcfg;
523
Holger Hans Peter Freyther3488c3d2011-07-20 20:44:54 +0200524 /* parse CallID C: and LocalParameters L: */
525 MSG_TOKENIZE_START
526 switch (msg->l3h[line_start]) {
527 case 'L':
528 local_options = (const char *) &msg->l3h[line_start + 3];
529 break;
530 case 'C':
531 callid = (const char *) &msg->l3h[line_start + 3];
532 break;
533 case 'M':
534 mode = (const char *) & msg->l3h[line_start + 3];
535 break;
536 default:
537 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
538 msg->l3h[line_start], msg->l3h[line_start],
539 ENDPOINT_NUMBER(endp));
540 break;
541 }
542 MSG_TOKENIZE_END
543
544 /* Check required data */
545 if (!callid || !mode) {
546 LOGP(DMGCP, LOGL_ERROR, "Missing callid and mode in CRCX on 0x%x\n",
547 ENDPOINT_NUMBER(endp));
548 return create_err_response(400, "CRCX", trans_id);
549 }
550
551 /* this appears to be a retransmission, maybe check trans id */
552 if (endp->allocated &&
553 memcmp(endp->callid, callid, strlen(endp->callid)) == 0)
554 return create_response_with_sdp(endp, "CRCX", trans_id);
555
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +0800556 if (endp->allocated) {
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100557 if (tcfg->force_realloc) {
Holger Hans Peter Freyther408cc4a2010-04-07 10:51:27 +0200558 LOGP(DMGCP, LOGL_NOTICE, "Endpoint 0x%x already allocated. Forcing realloc.\n",
559 ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther32d4e502010-04-24 21:02:48 +0800560 mgcp_free_endp(endp);
Holger Hans Peter Freyther869e38e2010-08-06 17:54:27 +0800561 if (cfg->realloc_cb)
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100562 cfg->realloc_cb(tcfg, ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther408cc4a2010-04-07 10:51:27 +0200563 } else {
564 LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n",
565 ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100566 return create_err_response(400, "CRCX", trans_id);
Holger Hans Peter Freyther408cc4a2010-04-07 10:51:27 +0200567 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100568 }
569
Holger Hans Peter Freyther3488c3d2011-07-20 20:44:54 +0200570 /* copy some parameters */
571 endp->callid = talloc_strdup(tcfg->endpoints, callid);
572
573 if (local_options)
574 endp->local_options = talloc_strdup(tcfg->endpoints, local_options);
575
576 if (parse_conn_mode(mode, &endp->conn_mode) != 0) {
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100577 error_code = 517;
578 goto error2;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100579 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100580
581 /* initialize */
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800582 endp->net_end.rtp_port = endp->net_end.rtcp_port = endp->bts_end.rtp_port = endp->bts_end.rtcp_port = 0;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100583
584 /* set to zero until we get the info */
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800585 memset(&endp->net_end.addr, 0, sizeof(endp->net_end.addr));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100586
587 /* bind to the port now */
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800588 if (allocate_ports(endp) != 0)
589 goto error2;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100590
591 /* assign a local call identifier or fail */
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100592 endp->ci = generate_call_id(cfg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100593 if (endp->ci == CI_UNUSED)
594 goto error2;
595
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +0800596 endp->allocated = 1;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100597 endp->bts_end.payload_type = tcfg->audio_payload;
Holger Hans Peter Freytheref6bb252010-02-26 13:41:22 +0100598
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100599 /* policy CB */
600 if (cfg->policy_cb) {
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100601 switch (cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, trans_id)) {
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100602 case MGCP_POLICY_REJECT:
603 LOGP(DMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n",
604 ENDPOINT_NUMBER(endp));
605 mgcp_free_endp(endp);
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100606 return create_err_response(400, "CRCX", trans_id);
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100607 break;
608 case MGCP_POLICY_DEFER:
609 /* stop processing */
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +0800610 create_transcoder(endp);
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100611 return NULL;
612 break;
613 case MGCP_POLICY_CONT:
614 /* just continue */
615 break;
616 }
617 }
618
Holger Hans Peter Freythere378cb12010-08-06 20:22:23 +0800619 LOGP(DMGCP, LOGL_DEBUG, "Creating endpoint on: 0x%x CI: %u port: %u/%u\n",
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800620 ENDPOINT_NUMBER(endp), endp->ci,
621 endp->net_end.local_port, endp->bts_end.local_port);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100622 if (cfg->change_cb)
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100623 cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +0100624
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +0800625 create_transcoder(endp);
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100626 return create_response_with_sdp(endp, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100627error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100628 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200629 osmo_hexdump(msg->l3h, msgb_l3len(msg)),
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100630 ENDPOINT_NUMBER(endp), line_start, i);
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100631 return create_err_response(error_code, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100632
633error2:
Holger Hans Peter Freyther414bf402010-08-06 07:05:13 +0800634 mgcp_free_endp(endp);
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100635 LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100636 return create_err_response(error_code, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100637}
638
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100639static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100640{
641 struct mgcp_msg_ptr data_ptrs[6];
642 int found, i, line_start;
643 const char *trans_id;
644 struct mgcp_endpoint *endp;
645 int error_code = 500;
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800646 int silent = 0;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100647
Holger Hans Peter Freytherf2f15912010-04-01 03:27:04 +0200648 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100649 if (found != 0)
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100650 return create_err_response(510, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100651
652 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100653 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100654 return create_err_response(400, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100655 }
656
657 MSG_TOKENIZE_START
658 switch (msg->l3h[line_start]) {
659 case 'C': {
660 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
661 goto error3;
662 break;
663 }
664 case 'I': {
665 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
666 goto error3;
667 break;
668 }
669 case 'L':
670 /* skip */
671 break;
672 case 'M':
673 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
674 &endp->conn_mode) != 0) {
675 error_code = 517;
676 goto error3;
677 }
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800678 endp->orig_mode = endp->conn_mode;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100679 break;
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800680 case 'Z':
681 silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0;
682 break;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100683 case '\0':
684 /* SDP file begins */
685 break;
686 case 'a':
687 case 'o':
688 case 's':
689 case 't':
690 case 'v':
691 /* skip these SDP attributes */
692 break;
693 case 'm': {
694 int port;
Holger Hans Peter Freytheref6bb252010-02-26 13:41:22 +0100695 int payload;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100696 const char *param = (const char *)&msg->l3h[line_start];
697
Holger Hans Peter Freytheref6bb252010-02-26 13:41:22 +0100698 if (sscanf(param, "m=audio %d RTP/AVP %d", &port, &payload) == 2) {
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800699 endp->net_end.rtp_port = htons(port);
700 endp->net_end.rtcp_port = htons(port + 1);
701 endp->net_end.payload_type = payload;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100702 }
703 break;
704 }
705 case 'c': {
706 char ipv4[16];
707 const char *param = (const char *)&msg->l3h[line_start];
708
709 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800710 inet_aton(ipv4, &endp->net_end.addr);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100711 }
712 break;
713 }
714 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100715 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100716 msg->l3h[line_start], msg->l3h[line_start],
717 ENDPOINT_NUMBER(endp));
718 break;
719 }
720 MSG_TOKENIZE_END
721
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100722 /* policy CB */
723 if (cfg->policy_cb) {
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100724 switch (cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, trans_id)) {
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100725 case MGCP_POLICY_REJECT:
726 LOGP(DMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n",
727 ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800728 if (silent)
729 goto out_silent;
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100730 return create_err_response(400, "MDCX", trans_id);
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100731 break;
732 case MGCP_POLICY_DEFER:
733 /* stop processing */
734 return NULL;
735 break;
736 case MGCP_POLICY_CONT:
737 /* just continue */
738 break;
739 }
740 }
741
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100742 /* modify */
Holger Hans Peter Freythere378cb12010-08-06 20:22:23 +0800743 LOGP(DMGCP, LOGL_DEBUG, "Modified endpoint on: 0x%x Server: %s:%u\n",
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800744 ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port));
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100745 if (cfg->change_cb)
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100746 cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX);
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800747 if (silent)
748 goto out_silent;
749
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100750 return create_response_with_sdp(endp, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100751
752error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100753 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200754 osmo_hexdump(msg->l3h, msgb_l3len(msg)),
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100755 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100756 return create_err_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100757
758error3:
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100759 return create_err_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800760
761
762out_silent:
763 return NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100764}
765
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100766static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100767{
768 struct mgcp_msg_ptr data_ptrs[6];
769 int found, i, line_start;
770 const char *trans_id;
771 struct mgcp_endpoint *endp;
Holger Hans Peter Freyther9e9392d2010-08-08 16:24:48 +0800772 int error_code = 400;
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800773 int silent = 0;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100774
Holger Hans Peter Freytherf2f15912010-04-01 03:27:04 +0200775 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100776 if (found != 0)
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100777 return create_err_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100778
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +0800779 if (!endp->allocated) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100780 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100781 return create_err_response(400, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100782 }
783
784 MSG_TOKENIZE_START
785 switch (msg->l3h[line_start]) {
786 case 'C': {
787 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
788 goto error3;
789 break;
790 }
791 case 'I': {
792 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
793 goto error3;
794 break;
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800795 case 'Z':
796 silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0;
797 break;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100798 }
799 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100800 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100801 msg->l3h[line_start], msg->l3h[line_start],
802 ENDPOINT_NUMBER(endp));
803 break;
804 }
805 MSG_TOKENIZE_END
806
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100807 /* policy CB */
808 if (cfg->policy_cb) {
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100809 switch (cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, trans_id)) {
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100810 case MGCP_POLICY_REJECT:
811 LOGP(DMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n",
812 ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800813 if (silent)
814 goto out_silent;
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100815 return create_err_response(400, "DLCX", trans_id);
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100816 break;
817 case MGCP_POLICY_DEFER:
818 /* stop processing */
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +0800819 delete_transcoder(endp);
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100820 return NULL;
821 break;
822 case MGCP_POLICY_CONT:
823 /* just continue */
824 break;
825 }
826 }
827
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100828 /* free the connection */
Holger Hans Peter Freythere378cb12010-08-06 20:22:23 +0800829 LOGP(DMGCP, LOGL_DEBUG, "Deleted endpoint on: 0x%x Server: %s:%u\n",
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800830 ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port));
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +0800831
832 delete_transcoder(endp);
Holger Hans Peter Freyther154b9552010-02-26 13:27:51 +0100833 mgcp_free_endp(endp);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100834 if (cfg->change_cb)
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100835 cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100836
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800837 if (silent)
838 goto out_silent;
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100839 return create_ok_response(250, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100840
841error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100842 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200843 osmo_hexdump(msg->l3h, msgb_l3len(msg)),
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100844 ENDPOINT_NUMBER(endp), line_start, i);
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100845 return create_err_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100846
847error3:
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100848 return create_err_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere3d16bb2010-04-22 11:58:13 +0800849
850out_silent:
851 return NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100852}
853
Holger Hans Peter Freyther9bdcc9c2010-03-31 06:39:35 +0200854static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg)
855{
Holger Hans Peter Freyther74db7742011-06-10 00:04:55 +0200856 struct mgcp_msg_ptr data_ptrs[6];
857 const char *trans_id;
858 struct mgcp_endpoint *endp;
859 int found;
860
861 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs),
862 &trans_id, &endp);
863 if (found != 0) {
864 LOGP(DMGCP, LOGL_ERROR, "Failed to find the endpoint.\n");
865 return NULL;
866 }
867
Holger Hans Peter Freyther9bdcc9c2010-03-31 06:39:35 +0200868 if (cfg->reset_cb)
Holger Hans Peter Freyther74db7742011-06-10 00:04:55 +0200869 cfg->reset_cb(endp->tcfg);
Holger Hans Peter Freyther9bdcc9c2010-03-31 06:39:35 +0200870 return NULL;
871}
872
Holger Hans Peter Freyther6e94d6d2011-01-25 23:33:54 +0100873/*
874 * This can request like DTMF detection and forward, fax detection... it
875 * can also request when the notification should be send and such. We don't
876 * do this right now.
877 */
878static struct msgb *handle_noti_req(struct mgcp_config *cfg, struct msgb *msg)
879{
880 struct mgcp_msg_ptr data_ptrs[6];
881 const char *trans_id;
882 struct mgcp_endpoint *endp;
883 int found;
884
885 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
886 if (found != 0)
887 return create_err_response(400, "RQNT", trans_id);
888
889 if (!endp->allocated) {
890 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
891 return create_err_response(400, "RQNT", trans_id);
892 }
893 return create_ok_response(200, "RQNT", trans_id);
894}
895
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100896struct mgcp_config *mgcp_config_alloc(void)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100897{
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100898 struct mgcp_config *cfg;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100899
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100900 cfg = talloc_zero(NULL, struct mgcp_config);
901 if (!cfg) {
902 LOGP(DMGCP, LOGL_FATAL, "Failed to allocate config.\n");
903 return NULL;
904 }
905
906 cfg->source_port = 2427;
907 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100908
Holger Hans Peter Freytherb98ba722010-09-19 04:21:39 +0800909 cfg->transcoder_remote_base = 4000;
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +0800910
911 cfg->bts_ports.base_port = RTP_PORT_DEFAULT;
912 cfg->net_ports.base_port = RTP_PORT_NET_DEFAULT;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100913
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100914 /* default trunk handling */
915 cfg->trunk.cfg = cfg;
916 cfg->trunk.trunk_nr = 0;
917 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
918 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
919 cfg->trunk.audio_payload = 126;
920
Holger Hans Peter Freyther0e939fe2011-02-28 12:11:02 +0100921 INIT_LLIST_HEAD(&cfg->trunks);
922
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100923 return cfg;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100924}
925
Holger Hans Peter Freyther0e939fe2011-02-28 12:11:02 +0100926struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
927{
928 struct mgcp_trunk_config *trunk;
929
930 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
931 if (!trunk) {
932 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate.\n");
933 return NULL;
934 }
935
936 trunk->cfg = cfg;
937 trunk->trunk_type = MGCP_TRUNK_E1;
938 trunk->trunk_nr = nr;
939 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
940 trunk->audio_payload = 126;
941 trunk->number_endpoints = 33;
942 llist_add_tail(&trunk->entry, &cfg->trunks);
943 return trunk;
944}
945
946struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
947{
948 struct mgcp_trunk_config *trunk;
949
950 llist_for_each_entry(trunk, &cfg->trunks, entry)
951 if (trunk->trunk_nr == index)
952 return trunk;
953
954 return NULL;
955}
956
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800957static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end)
958{
Holger Hans Peter Freythere97155a2010-11-02 19:06:13 +0100959 if (end->local_alloc == PORT_ALLOC_DYNAMIC) {
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800960 mgcp_free_rtp_port(end);
Holger Hans Peter Freythere97155a2010-11-02 19:06:13 +0100961 end->local_port = 0;
962 }
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800963
Holger Hans Peter Freytherc4921272010-08-05 03:37:22 +0800964 end->packets = 0;
965 memset(&end->addr, 0, sizeof(end->addr));
Holger Hans Peter Freythere97155a2010-11-02 19:06:13 +0100966 end->rtp_port = end->rtcp_port = 0;
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800967 end->payload_type = -1;
Holger Hans Peter Freytherf138f912010-08-05 08:08:17 +0800968 end->local_alloc = -1;
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800969}
970
Holger Hans Peter Freytherc4921272010-08-05 03:37:22 +0800971static void mgcp_rtp_end_init(struct mgcp_rtp_end *end)
972{
973 mgcp_rtp_end_reset(end);
974 end->rtp.fd = -1;
975 end->rtcp.fd = -1;
976}
977
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +0100978int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100979{
980 int i;
981
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100982 /* Initialize all endpoints */
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +0100983 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100984 sizeof(struct mgcp_endpoint),
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100985 tcfg->number_endpoints, "endpoints");
986 if (!tcfg->endpoints)
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100987 return -1;
988
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100989 for (i = 0; i < tcfg->number_endpoints; ++i) {
990 tcfg->endpoints[i].ci = CI_UNUSED;
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +0100991 tcfg->endpoints[i].cfg = tcfg->cfg;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100992 tcfg->endpoints[i].tcfg = tcfg;
993 mgcp_rtp_end_init(&tcfg->endpoints[i].net_end);
994 mgcp_rtp_end_init(&tcfg->endpoints[i].bts_end);
995 mgcp_rtp_end_init(&tcfg->endpoints[i].trans_net);
996 mgcp_rtp_end_init(&tcfg->endpoints[i].trans_bts);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100997 }
998
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100999 return 0;
1000}
Holger Hans Peter Freyther154b9552010-02-26 13:27:51 +01001001
1002void mgcp_free_endp(struct mgcp_endpoint *endp)
1003{
Holger Hans Peter Freytherc77efdf2010-03-31 12:31:09 +02001004 LOGP(DMGCP, LOGL_DEBUG, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther89976e82010-08-03 15:01:43 +00001005 endp->ci = CI_UNUSED;
Holger Hans Peter Freyther39a97e22010-08-06 18:03:11 +08001006 endp->allocated = 0;
Holger Hans Peter Freyther154b9552010-02-26 13:27:51 +01001007
1008 if (endp->callid) {
1009 talloc_free(endp->callid);
1010 endp->callid = NULL;
1011 }
1012
1013 if (endp->local_options) {
1014 talloc_free(endp->local_options);
Holger Hans Peter Freyther88c6eea2010-03-01 18:52:04 +01001015 endp->local_options = NULL;
Holger Hans Peter Freyther154b9552010-02-26 13:27:51 +01001016 }
1017
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +08001018 mgcp_rtp_end_reset(&endp->bts_end);
1019 mgcp_rtp_end_reset(&endp->net_end);
Holger Hans Peter Freyther21262332010-11-01 20:53:31 +01001020 mgcp_rtp_end_reset(&endp->trans_net);
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +01001021 mgcp_rtp_end_reset(&endp->trans_bts);
Holger Hans Peter Freytherb54048f2010-11-01 19:57:50 +01001022 endp->is_transcoded = 0;
Holger Hans Peter Freyther380b8712010-07-29 02:38:39 +08001023
Holger Hans Peter Freyther31868922010-08-03 11:59:04 +00001024 memset(&endp->net_state, 0, sizeof(endp->net_state));
1025 memset(&endp->bts_state, 0, sizeof(endp->bts_state));
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +08001026
1027 endp->conn_mode = endp->orig_mode = MGCP_CONN_NONE;
Holger Hans Peter Freyther6357a8e2010-08-05 12:07:00 +00001028 endp->allow_patch = 0;
Holger Hans Peter Freyther260d6ed2010-08-06 01:12:21 +08001029
1030 memset(&endp->taps, 0, sizeof(endp->taps));
Holger Hans Peter Freyther154b9552010-02-26 13:27:51 +01001031}
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001032
Holger Hans Peter Freyther557b1ab2010-09-19 04:39:55 +08001033static int send_trans(struct mgcp_config *cfg, const char *buf, int len)
1034{
1035 struct sockaddr_in addr;
1036
1037 memset(&addr, 0, sizeof(addr));
1038 addr.sin_family = AF_INET;
1039 addr.sin_addr = cfg->transcoder_in;
1040 addr.sin_port = htons(2427);
1041 return sendto(cfg->gw_fd.bfd.fd, buf, len, 0,
1042 (struct sockaddr *) &addr, sizeof(addr));
1043}
1044
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001045static void send_msg(struct mgcp_endpoint *endp, int endpoint, int port,
1046 const char *msg, const char *mode)
1047{
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001048 char buf[2096];
1049 int len;
1050
1051 /* hardcoded to AMR right now, we do not know the real type at this point */
1052 len = snprintf(buf, sizeof(buf),
1053 "%s 42 %x@mgw MGCP 1.0\r\n"
1054 "C: 4256\r\n"
1055 "M: %s\r\n"
1056 "\r\n"
1057 "c=IN IP4 %s\r\n"
1058 "m=audio %d RTP/AVP %d\r\n"
1059 "a=rtpmap:%d %s\r\n",
1060 msg, endpoint, mode, endp->cfg->source_addr,
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +01001061 port, endp->tcfg->audio_payload,
1062 endp->tcfg->audio_payload, endp->tcfg->audio_name);
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001063
1064 if (len < 0)
1065 return;
1066
1067 buf[sizeof(buf) - 1] = '\0';
1068
Holger Hans Peter Freyther557b1ab2010-09-19 04:39:55 +08001069 send_trans(endp->cfg, buf, len);
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001070}
1071
1072static void send_dlcx(struct mgcp_endpoint *endp, int endpoint)
1073{
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001074 char buf[2096];
1075 int len;
1076
1077 len = snprintf(buf, sizeof(buf),
1078 "DLCX 43 %x@mgw MGCP 1.0\r\n"
1079 "C: 4256\r\n"
1080 , endpoint);
1081
1082 if (len < 0)
1083 return;
1084
1085 buf[sizeof(buf) - 1] = '\0';
1086
Holger Hans Peter Freyther557b1ab2010-09-19 04:39:55 +08001087 send_trans(endp->cfg, buf, len);
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001088}
1089
Harald Welte6a857052012-01-27 00:41:39 +01001090static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1091{
1092 return write(cfg->gw_fd.bfd.fd, buf, len);
1093}
1094
1095int mgcp_send_reset_all(struct mgcp_config *cfg)
1096{
1097 static const char mgcp_reset[] = {
1098 "RSIP 1 *@mgw MGCP 1.0\r\n"
1099 };
1100
1101 return send_agent(cfg, mgcp_reset, sizeof mgcp_reset -1);
1102}
1103
1104int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1105{
1106 char buf[128];
1107 int len;
1108
1109 len = snprintf(buf, sizeof(buf),
1110 "RSIP 39 %x@mgw MGCP 1.0\r\n"
1111 , endpoint);
1112 if (len < 0)
1113 return len;
1114
1115 buf[sizeof(buf) - 1] = '\0';
1116
1117 return send_agent(endp->cfg, buf, len);
1118}
1119
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001120static void create_transcoder(struct mgcp_endpoint *endp)
1121{
1122 int port;
1123 int in_endp = ENDPOINT_NUMBER(endp);
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +01001124 int out_endp = endp_back_channel(in_endp);
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001125
Holger Hans Peter Freytherb54048f2010-11-01 19:57:50 +01001126 if (!endp->is_transcoded)
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001127 return;
1128
Holger Hans Peter Freyther8b19dee2010-11-01 22:38:25 +01001129 send_msg(endp, in_endp, endp->trans_bts.local_port, "CRCX", "sendrecv");
1130 send_msg(endp, in_endp, endp->trans_bts.local_port, "MDCX", "sendrecv");
Holger Hans Peter Freyther21262332010-11-01 20:53:31 +01001131 send_msg(endp, out_endp, endp->trans_net.local_port, "CRCX", "sendrecv");
1132 send_msg(endp, out_endp, endp->trans_net.local_port, "MDCX", "sendrecv");
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001133
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +01001134 port = rtp_calculate_port(in_endp, endp->cfg->transcoder_remote_base);
1135 endp->trans_bts.rtp_port = htons(port);
1136 endp->trans_bts.rtcp_port = htons(port + 1);
1137
Holger Hans Peter Freytherb98ba722010-09-19 04:21:39 +08001138 port = rtp_calculate_port(out_endp, endp->cfg->transcoder_remote_base);
Holger Hans Peter Freyther21262332010-11-01 20:53:31 +01001139 endp->trans_net.rtp_port = htons(port);
1140 endp->trans_net.rtcp_port = htons(port + 1);
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001141}
1142
1143static void delete_transcoder(struct mgcp_endpoint *endp)
1144{
1145 int in_endp = ENDPOINT_NUMBER(endp);
Holger Hans Peter Freytherbd7b3c52010-11-01 21:04:54 +01001146 int out_endp = endp_back_channel(in_endp);
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001147
Holger Hans Peter Freytherb54048f2010-11-01 19:57:50 +01001148 if (!endp->is_transcoded)
Holger Hans Peter Freyther3b5e3c42010-09-18 06:35:29 +08001149 return;
1150
1151 send_dlcx(endp, in_endp);
1152 send_dlcx(endp, out_endp);
1153}
Holger Hans Peter Freytherf2eedff2010-09-19 04:36:07 +08001154
1155int mgcp_reset_transcoder(struct mgcp_config *cfg)
1156{
Holger Hans Peter Freytherf2eedff2010-09-19 04:36:07 +08001157 if (!cfg->transcoder_ip)
Holger Hans Peter Freythercf1c8772010-09-24 04:41:28 +08001158 return 0;
Holger Hans Peter Freytherf2eedff2010-09-19 04:36:07 +08001159
1160 static const char mgcp_reset[] = {
1161 "RSIP 1 13@mgw MGCP 1.0\r\n"
1162 };
1163
Holger Hans Peter Freyther557b1ab2010-09-19 04:39:55 +08001164 return send_trans(cfg, mgcp_reset, sizeof mgcp_reset -1);
Holger Hans Peter Freytherf2eedff2010-09-19 04:36:07 +08001165}