blob: 43bddf4a5a30890408780d476afd614698c4acbd [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 Freyther7bdc6372010-02-20 21:21:02 +01005 * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2010 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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <ctype.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <time.h>
30#include <limits.h>
31#include <unistd.h>
32
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010033#include <openbsc/debug.h>
34#include <openbsc/msgb.h>
35#include <openbsc/talloc.h>
36#include <openbsc/gsm_data.h>
37#include <openbsc/select.h>
38#include <openbsc/mgcp.h>
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010039#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010040
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010041enum mgcp_connection_mode {
42 MGCP_CONN_NONE = 0,
43 MGCP_CONN_RECV_ONLY = 1,
44 MGCP_CONN_SEND_ONLY = 2,
45 MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY,
46};
47
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010048/**
49 * Macro for tokenizing MGCP messages and SDP in one go.
50 *
51 */
52#define MSG_TOKENIZE_START \
53 line_start = 0; \
54 for (i = 0; i < msgb_l3len(msg); ++i) { \
55 /* we have a line end */ \
56 if (msg->l3h[i] == '\n') { \
57 /* skip the first line */ \
58 if (line_start == 0) { \
59 line_start = i + 1; \
60 continue; \
61 } \
62 \
63 /* check if we have a proper param */ \
64 if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \
65 } else if (i - line_start > 2 \
66 && islower(msg->l3h[line_start]) \
67 && msg->l3h[line_start + 1] == '=') { \
68 } else if (i - line_start < 3 \
69 || msg->l3h[line_start + 1] != ':' \
70 || msg->l3h[line_start + 2] != ' ') \
71 goto error; \
72 \
73 msg->l3h[i] = '\0'; \
74 if (msg->l3h[i-1] == '\r') \
75 msg->l3h[i-1] = '\0';
76
77#define MSG_TOKENIZE_END \
78 line_start = i + 1; \
79 } \
80 }
81
82
83struct mgcp_msg_ptr {
84 unsigned int start;
85 unsigned int length;
86};
87
88struct mgcp_request {
89 char *name;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010090 struct msgb *(*handle_request) (struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010091 char *debug_name;
92};
93
94#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
95 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
96
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010097static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg);
98static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg);
99static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg);
100static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100101
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100102static int generate_call_id(struct mgcp_config *cfg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100103{
104 int i;
105
106 /* use the call id */
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100107 ++cfg->last_call_id;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100108
109 /* handle wrap around */
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100110 if (cfg->last_call_id == CI_UNUSED)
111 ++cfg->last_call_id;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100112
113 /* callstack can only be of size number_of_endpoints */
114 /* verify that the call id is free, e.g. in case of overrun */
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100115 for (i = 1; i < cfg->number_endpoints; ++i)
116 if (cfg->endpoints[i].ci == cfg->last_call_id)
117 return generate_call_id(cfg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100118
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100119 return cfg->last_call_id;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100120}
121
122/* FIXIME/TODO: need to have a list of pending transactions and check that */
123static unsigned int generate_transaction_id()
124{
125 return abs(rand());
126}
127
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100128/*
129 * array of function pointers for handling various
130 * messages. In the future this might be binary sorted
131 * for performance reasons.
132 */
133static const struct mgcp_request mgcp_requests [] = {
134 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
135 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
136 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
137 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
138};
139
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100140static struct msgb *mgcp_msgb_alloc(void)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100141{
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100142 struct msgb *msg;
143 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
144 if (!msg)
145 LOGP(DMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
146
147 return msg;
148}
149
Holger Hans Peter Freyther6414a0c2010-02-21 10:50:46 +0100150struct msgb *mgcp_create_response_with_data(int code, const char *msg, const char *trans,
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100151 const char *data)
152{
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100153 int len;
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100154 struct msgb *res;
155
156 res = mgcp_msgb_alloc();
157 if (!res)
158 return NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100159
160 if (data) {
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100161 len = snprintf((char *) res->data, 2048, "%d %s\n%s", code, trans, data);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100162 } else {
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100163 len = snprintf((char *) res->data, 2048, "%d %s\n", code, trans);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100164 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100165
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100166 res->l2h = msgb_put(res, len);
Holger Hans Peter Freyther63f2db22010-02-26 13:30:57 +0100167 LOGP(DMGCP, LOGL_DEBUG, "Sending response: code: %d for '%s'\n", code, res->l2h);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100168 return res;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100169}
170
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100171static struct msgb *create_response(int code, const char *msg, const char *trans)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100172{
Holger Hans Peter Freyther6414a0c2010-02-21 10:50:46 +0100173 return mgcp_create_response_with_data(code, msg, trans, NULL);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100174}
175
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100176static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
177 const char *msg, const char *trans_id)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100178{
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100179 const char *addr = endp->cfg->local_ip;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100180 char sdp_record[4096];
181
182 if (!addr)
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100183 addr = endp->cfg->source_addr;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100184
185 snprintf(sdp_record, sizeof(sdp_record) - 1,
186 "I: %d\n\n"
187 "v=0\r\n"
188 "c=IN IP4 %s\r\n"
189 "m=audio %d RTP/AVP %d\r\n"
190 "a=rtpmap:%d %s\r\n",
191 endp->ci, addr, endp->rtp_port,
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100192 endp->cfg->audio_payload, endp->cfg->audio_payload,
193 endp->cfg->audio_name);
Holger Hans Peter Freyther6414a0c2010-02-21 10:50:46 +0100194 return mgcp_create_response_with_data(200, msg, trans_id, sdp_record);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100195}
196
197/* send a static record */
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100198struct msgb *mgcp_create_rsip(void)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100199{
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100200 struct msgb *msg;
201 int len;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100202
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100203 msg = mgcp_msgb_alloc();
204 if (!msg)
205 return NULL;
206
207 len = snprintf((char *) msg->data, 2048,
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100208 "RSIP %u *@mgw MGCP 1.0\n"
209 "RM: restart\n", generate_transaction_id());
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100210 msg->l2h = msgb_put(msg, len);
211 return msg;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100212}
213
214/*
215 * handle incoming messages:
216 * - this can be a command (four letters, space, transaction id)
217 * - or a response (three numbers, space, transaction id)
218 */
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100219struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100220{
221 int code;
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100222 struct msgb *resp = NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100223
224 if (msg->len < 4) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100225 LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100226 return NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100227 }
228
229 /* attempt to treat it as a response */
230 if (sscanf((const char *)&msg->data[0], "%3d %*s", &code) == 1) {
Holger Hans Peter Freyther63f2db22010-02-26 13:30:57 +0100231 LOGP(DMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100232 } else {
233 int i, handled = 0;
234 msg->l3h = &msg->l2h[4];
235 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
236 if (strncmp(mgcp_requests[i].name, (const char *) &msg->data[0], 4) == 0) {
237 handled = 1;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100238 resp = mgcp_requests[i].handle_request(cfg, msg);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100239 break;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100240 }
241 if (!handled) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100242 LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->data[0]);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100243 }
244 }
245
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100246 return resp;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100247}
248
249/* string tokenizer for the poor */
250static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
251{
252 int i, found = 0;
253
254 int whitespace = 1;
255 for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
256 /* if we have a space we found an end */
257 if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
258 if (!whitespace) {
259 ++found;
260 whitespace = 1;
261 ptrs->length = i - ptrs->start - 1;
262 ++ptrs;
263 --ptrs_length;
264 } else {
265 /* skip any number of whitespace */
266 }
267
268 /* line end... stop */
269 if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
270 break;
271 } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
272 /* line end, be done */
273 break;
274 } else if (whitespace) {
275 whitespace = 0;
276 ptrs->start = i;
277 }
278 }
279
280 if (ptrs_length == 0)
281 return -1;
282 return found;
283}
284
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100285static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100286{
287 char *endptr = NULL;
288 unsigned int gw = INT_MAX;
289
290 gw = strtoul(mgcp, &endptr, 16);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100291 if (gw == 0 || gw >= cfg->number_endpoints || strcmp(endptr, "@mgw") != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100292 LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100293 return NULL;
294 }
295
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100296 return &cfg->endpoints[gw];
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100297}
298
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100299static int analyze_header(struct mgcp_config *cfg, struct msgb *msg,
300 struct mgcp_msg_ptr *ptr, int size,
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100301 const char **transaction_id, struct mgcp_endpoint **endp)
302{
303 int found;
304
305 if (size < 3) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100306 LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100307 return -1;
308 }
309
310 found = find_msg_pointers(msg, ptr, size);
311
312 if (found < 3) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100313 LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100314 return -1;
315 }
316
317 /*
318 * replace the space with \0. the main method gurantess that
319 * we still have + 1 for null termination
320 */
321 msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
322 msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
323 msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
324 msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
325
326 if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
327 || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100328 LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100329 (const char *)&msg->l3h[ptr[3].start],
330 (const char *)&msg->l3h[ptr[2].start]);
331 return -1;
332 }
333
334 *transaction_id = (const char *)&msg->l3h[ptr[0].start];
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100335 *endp = find_endpoint(cfg, (const char *)&msg->l3h[ptr[1].start]);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100336 return *endp == NULL;
337}
338
339static int verify_call_id(const struct mgcp_endpoint *endp,
340 const char *callid)
341{
342 if (strcmp(endp->callid, callid) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100343 LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100344 ENDPOINT_NUMBER(endp), endp->callid, callid);
345 return -1;
346 }
347
348 return 0;
349}
350
351static int verify_ci(const struct mgcp_endpoint *endp,
352 const char *ci)
353{
354 if (atoi(ci) != endp->ci) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100355 LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100356 ENDPOINT_NUMBER(endp), endp->ci, ci);
357 return -1;
358 }
359
360 return 0;
361}
362
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100363static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100364{
365 struct mgcp_msg_ptr data_ptrs[6];
366 int found, response;
367 const char *trans_id;
368 struct mgcp_endpoint *endp;
369
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100370 found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100371 if (found != 0)
372 response = 500;
373 else
374 response = 200;
375
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100376 return create_response(response, "AUEP", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100377}
378
379static int parse_conn_mode(const char* msg, int *conn_mode)
380{
381 int ret = 0;
382 if (strcmp(msg, "recvonly") == 0)
383 *conn_mode = MGCP_CONN_RECV_ONLY;
384 else if (strcmp(msg, "sendrecv") == 0)
385 *conn_mode = MGCP_CONN_RECV_SEND;
386 else {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100387 LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100388 ret = -1;
389 }
390
391 return ret;
392}
393
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100394static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100395{
396 struct mgcp_msg_ptr data_ptrs[6];
397 int found, i, line_start;
398 const char *trans_id;
399 struct mgcp_endpoint *endp;
400 int error_code = 500;
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100401 int port;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100402
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100403 found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100404 if (found != 0)
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100405 return create_response(500, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100406
407 if (endp->ci != CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100408 LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100409 return create_response(500, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100410 }
411
412 /* parse CallID C: and LocalParameters L: */
413 MSG_TOKENIZE_START
414 switch (msg->l3h[line_start]) {
415 case 'L':
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100416 endp->local_options = talloc_strdup(cfg->endpoints,
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100417 (const char *)&msg->l3h[line_start + 3]);
418 break;
419 case 'C':
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100420 endp->callid = talloc_strdup(cfg->endpoints,
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100421 (const char *)&msg->l3h[line_start + 3]);
422 break;
423 case 'M':
424 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
425 &endp->conn_mode) != 0) {
426 error_code = 517;
427 goto error2;
428 }
429 break;
430 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100431 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100432 msg->l3h[line_start], msg->l3h[line_start],
433 ENDPOINT_NUMBER(endp));
434 break;
435 }
436 MSG_TOKENIZE_END
437
438 /* initialize */
439 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
440
441 /* set to zero until we get the info */
442 memset(&endp->remote, 0, sizeof(endp->remote));
443
444 /* bind to the port now */
Holger Hans Peter Freyther1b0ea972010-02-21 11:11:04 +0100445 port = rtp_calculate_port(ENDPOINT_NUMBER(endp), cfg->rtp_base_port);
446 if (cfg->early_bind)
447 endp->rtp_port = port;
448 else if (mgcp_bind_rtp_port(endp, port) != 0)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100449 goto error2;
450
451 /* assign a local call identifier or fail */
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100452 endp->ci = generate_call_id(cfg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100453 if (endp->ci == CI_UNUSED)
454 goto error2;
455
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100456 LOGP(DMGCP, LOGL_NOTICE, "Creating endpoint on: 0x%x CI: %u port: %u\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100457 ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100458 if (cfg->change_cb)
459 cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, endp->rtp_port);
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +0100460
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100461 return create_response_with_sdp(endp, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100462error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100463 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100464 hexdump(msg->l3h, msgb_l3len(msg)),
465 ENDPOINT_NUMBER(endp), line_start, i);
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100466 return create_response(error_code, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100467
468error2:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100469 LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100470 return create_response(error_code, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100471}
472
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100473static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100474{
475 struct mgcp_msg_ptr data_ptrs[6];
476 int found, i, line_start;
477 const char *trans_id;
478 struct mgcp_endpoint *endp;
479 int error_code = 500;
480
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100481 found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100482 if (found != 0)
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100483 return create_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100484
485 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100486 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100487 return create_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100488 }
489
490 MSG_TOKENIZE_START
491 switch (msg->l3h[line_start]) {
492 case 'C': {
493 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
494 goto error3;
495 break;
496 }
497 case 'I': {
498 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
499 goto error3;
500 break;
501 }
502 case 'L':
503 /* skip */
504 break;
505 case 'M':
506 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
507 &endp->conn_mode) != 0) {
508 error_code = 517;
509 goto error3;
510 }
511 break;
512 case '\0':
513 /* SDP file begins */
514 break;
515 case 'a':
516 case 'o':
517 case 's':
518 case 't':
519 case 'v':
520 /* skip these SDP attributes */
521 break;
522 case 'm': {
523 int port;
524 const char *param = (const char *)&msg->l3h[line_start];
525
526 if (sscanf(param, "m=audio %d RTP/AVP %*d", &port) == 1) {
527 endp->net_rtp = htons(port);
528 endp->net_rtcp = htons(port + 1);
529 }
530 break;
531 }
532 case 'c': {
533 char ipv4[16];
534 const char *param = (const char *)&msg->l3h[line_start];
535
536 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
537 inet_aton(ipv4, &endp->remote);
538 }
539 break;
540 }
541 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100542 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100543 msg->l3h[line_start], msg->l3h[line_start],
544 ENDPOINT_NUMBER(endp));
545 break;
546 }
547 MSG_TOKENIZE_END
548
549 /* modify */
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100550 LOGP(DMGCP, LOGL_NOTICE, "Modified endpoint on: 0x%x Server: %s:%u\n",
Holger Hans Peter Freyther63f2db22010-02-26 13:30:57 +0100551 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), ntohs(endp->net_rtp));
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100552 if (cfg->change_cb)
553 cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, endp->rtp_port);
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100554 return create_response_with_sdp(endp, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100555
556error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100557 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100558 hexdump(msg->l3h, msgb_l3len(msg)),
559 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100560 return create_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100561
562error3:
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100563 return create_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100564}
565
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100566static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100567{
568 struct mgcp_msg_ptr data_ptrs[6];
569 int found, i, line_start;
570 const char *trans_id;
571 struct mgcp_endpoint *endp;
572 int error_code = 500;
573
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100574 found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100575 if (found != 0)
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100576 return create_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100577
578 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100579 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100580 return create_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100581 }
582
583 MSG_TOKENIZE_START
584 switch (msg->l3h[line_start]) {
585 case 'C': {
586 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
587 goto error3;
588 break;
589 }
590 case 'I': {
591 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
592 goto error3;
593 break;
594 }
595 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100596 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100597 msg->l3h[line_start], msg->l3h[line_start],
598 ENDPOINT_NUMBER(endp));
599 break;
600 }
601 MSG_TOKENIZE_END
602
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100603 /* free the connection */
Holger Hans Peter Freyther154b9552010-02-26 13:27:51 +0100604 mgcp_free_endp(endp);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100605 if (cfg->change_cb)
606 cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, endp->rtp_port);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100607
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100608 return create_response(250, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100609
610error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100611 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100612 hexdump(msg->l3h, msgb_l3len(msg)),
613 ENDPOINT_NUMBER(endp), line_start, i);
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100614 return create_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100615
616error3:
Holger Hans Peter Freyther8d188ed2010-02-21 01:44:08 +0100617 return create_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100618}
619
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100620struct mgcp_config *mgcp_config_alloc(void)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100621{
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100622 struct mgcp_config *cfg;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100623
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100624 cfg = talloc_zero(NULL, struct mgcp_config);
625 if (!cfg) {
626 LOGP(DMGCP, LOGL_FATAL, "Failed to allocate config.\n");
627 return NULL;
628 }
629
630 cfg->source_port = 2427;
631 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
632 cfg->audio_name = talloc_strdup(cfg, "GSM-EFR/8000");
633 cfg->audio_payload = 97;
634 cfg->rtp_base_port = RTP_PORT_DEFAULT;
635
636 return cfg;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100637}
638
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100639int mgcp_endpoints_allocate(struct mgcp_config *cfg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100640{
641 int i;
642
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100643 /* Initialize all endpoints */
644 cfg->endpoints = _talloc_zero_array(cfg,
645 sizeof(struct mgcp_endpoint),
646 cfg->number_endpoints, "endpoints");
647 if (!cfg->endpoints)
648 return -1;
649
650 for (i = 0; i < cfg->number_endpoints; ++i) {
651 cfg->endpoints[i].local_rtp.fd = -1;
652 cfg->endpoints[i].local_rtcp.fd = -1;
653 cfg->endpoints[i].ci = CI_UNUSED;
654 cfg->endpoints[i].cfg = cfg;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100655 }
656
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100657 return 0;
658}
Holger Hans Peter Freyther154b9552010-02-26 13:27:51 +0100659
660void mgcp_free_endp(struct mgcp_endpoint *endp)
661{
662 LOGP(DMGCP, LOGL_NOTICE, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
663 endp->ci= CI_UNUSED;
664
665 if (endp->callid) {
666 talloc_free(endp->callid);
667 endp->callid = NULL;
668 }
669
670 if (endp->local_options) {
671 talloc_free(endp->local_options);
672 endp->callid = NULL;
673 }
674
675 if (!endp->cfg->early_bind) {
676 bsc_unregister_fd(&endp->local_rtp);
677 bsc_unregister_fd(&endp->local_rtcp);
678 }
679
680 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
681 endp->net_payload_type = endp->bts_payload_type = -1;
682}