blob: 62a3a66c7cc8c61d4675a61b07ef1741183e5fdf [file] [log] [blame]
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
Holger Hans Peter Freyther44f848b2011-01-25 23:41:30 +01005 * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2011 by On-Waves
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08007 * All Rights Reserved
8 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +01009 * This program is free software: you can redistribute it and/or modify
10 * 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 Freyther97f66e22010-07-28 03:32:52 +080012 * (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
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080018 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +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 Freyther97f66e22010-07-28 03:32:52 +080021 *
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
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080032#include <cellmgr_debug.h>
33
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080034#include <mgcp/mgcp.h>
35#include <mgcp/mgcp_internal.h>
36
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080037#include <osmocore/msgb.h>
38#include <osmocore/talloc.h>
39#include <osmocore/select.h>
40#include <osmocore/utils.h>
41
42
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080043/**
44 * Macro for tokenizing MGCP messages and SDP in one go.
45 *
46 */
47#define MSG_TOKENIZE_START \
48 line_start = 0; \
49 for (i = 0; i < msgb_l3len(msg); ++i) { \
50 /* we have a line end */ \
51 if (msg->l3h[i] == '\n') { \
52 /* skip the first line */ \
53 if (line_start == 0) { \
54 line_start = i + 1; \
55 continue; \
56 } \
57 \
58 /* check if we have a proper param */ \
59 if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \
60 } else if (i - line_start > 2 \
61 && islower(msg->l3h[line_start]) \
62 && msg->l3h[line_start + 1] == '=') { \
63 } else if (i - line_start < 3 \
64 || msg->l3h[line_start + 1] != ':' \
65 || msg->l3h[line_start + 2] != ' ') \
66 goto error; \
67 \
68 msg->l3h[i] = '\0'; \
69 if (msg->l3h[i-1] == '\r') \
70 msg->l3h[i-1] = '\0';
71
72#define MSG_TOKENIZE_END \
73 line_start = i + 1; \
74 } \
75 }
76
77
78struct mgcp_request {
79 char *name;
80 struct msgb *(*handle_request) (struct mgcp_config *cfg, struct msgb *msg);
81 char *debug_name;
82};
83
84#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
85 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
86
87static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg);
88static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg);
89static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg);
90static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg);
91static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freyther44f848b2011-01-25 23:41:30 +010092static struct msgb *handle_noti_req(struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080093
Holger Hans Peter Freyther5b084012010-08-08 07:51:51 +080094static uint32_t generate_call_id(struct mgcp_config *cfg)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080095{
96 int i;
97
98 /* use the call id */
99 ++cfg->last_call_id;
100
101 /* handle wrap around */
102 if (cfg->last_call_id == CI_UNUSED)
103 ++cfg->last_call_id;
104
105 /* callstack can only be of size number_of_endpoints */
106 /* verify that the call id is free, e.g. in case of overrun */
107 for (i = 1; i < cfg->number_endpoints; ++i)
108 if (cfg->endpoints[i].ci == cfg->last_call_id)
109 return generate_call_id(cfg);
110
111 return cfg->last_call_id;
112}
113
114/*
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 Freyther44f848b2011-01-25 23:41:30 +0100124 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800125
126 /* SPEC extension */
127 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
128};
129
130static struct msgb *mgcp_msgb_alloc(void)
131{
132 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 Freyther7e0936e2011-01-06 19:48: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 Freyther97f66e22010-07-28 03:32:52 +0800143{
144 int len;
145 struct msgb *res;
146
147 res = mgcp_msgb_alloc();
148 if (!res)
149 return NULL;
150
151 if (data) {
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100152 len = snprintf((char *) res->data, 2048, "%d %s%s\r\n%s", code, trans, txt, data);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800153 } else {
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100154 len = snprintf((char *) res->data, 2048, "%d %s%s\r\n", code, trans, txt);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800155 }
156
157 res->l2h = msgb_put(res, len);
158 LOGP(DMGCP, LOGL_DEBUG, "Sending response: code: %d for '%s'\n", code, res->l2h);
159 return res;
160}
161
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100162static struct msgb *create_ok_response(int code, const char *msg, const char *trans)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800163{
Holger Hans Peter Freyther7e0936e2011-01-06 19:48: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 Freyther97f66e22010-07-28 03:32:52 +0800170}
171
172static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
173 const char *msg, const char *trans_id)
174{
175 const char *addr = endp->cfg->local_ip;
176 char sdp_record[4096];
177
178 if (!addr)
179 addr = endp->cfg->source_addr;
180
181 snprintf(sdp_record, sizeof(sdp_record) - 1,
Holger Hans Peter Freyther5b084012010-08-08 07:51:51 +0800182 "I: %u\n\n"
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800183 "v=0\r\n"
184 "c=IN IP4 %s\r\n"
185 "m=audio %d RTP/AVP %d\r\n"
186 "a=rtpmap:%d %s\r\n",
187 endp->ci, addr, endp->rtp_port,
188 endp->bts_payload_type, endp->bts_payload_type,
189 endp->cfg->audio_name);
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100190 return mgcp_create_response_with_data(200, " OK", msg, trans_id, sdp_record);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800191}
192
193/*
194 * handle incoming messages:
195 * - this can be a command (four letters, space, transaction id)
196 * - or a response (three numbers, space, transaction id)
197 */
198struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
199{
200 int code;
201 struct msgb *resp = NULL;
202
203 if (msgb_l2len(msg) < 4) {
204 LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len);
205 return NULL;
206 }
207
208 /* attempt to treat it as a response */
209 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
210 LOGP(DMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
211 } else {
212 int i, handled = 0;
213 msg->l3h = &msg->l2h[4];
214 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
215 if (strncmp(mgcp_requests[i].name, (const char *) &msg->l2h[0], 4) == 0) {
216 handled = 1;
217 resp = mgcp_requests[i].handle_request(cfg, msg);
218 break;
219 }
220 if (!handled) {
221 LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]);
222 }
223 }
224
225 return resp;
226}
227
228/* string tokenizer for the poor */
229static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
230{
231 int i, found = 0;
232
233 int whitespace = 1;
234 for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
235 /* if we have a space we found an end */
236 if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
237 if (!whitespace) {
238 ++found;
239 whitespace = 1;
240 ptrs->length = i - ptrs->start - 1;
241 ++ptrs;
242 --ptrs_length;
243 } else {
244 /* skip any number of whitespace */
245 }
246
247 /* line end... stop */
248 if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
249 break;
250 } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
251 /* line end, be done */
252 break;
253 } else if (whitespace) {
254 whitespace = 0;
255 ptrs->start = i;
256 }
257 }
258
259 if (ptrs_length == 0)
260 return -1;
261 return found;
262}
263
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100264/**
265 * We have a null terminated string with the endpoint name here. We only
266 * support two kinds. Simple ones as seen on the BSC level and the ones
267 * seen on the trunk side.
268 */
269static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg,
270 const char *mgcp)
271{
272 char *rest = NULL;
273 int trunk, endp, mgcp_endp;
274
275 trunk = strtoul(mgcp + 6, &rest, 10);
Holger Hans Peter Freytherea853042011-01-07 11:38:00 +0100276 if (rest == NULL || rest[0] != '/' || trunk < 1) {
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100277 LOGP(DMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
278 return NULL;
279 }
280
281 endp = strtoul(rest + 1, &rest, 10);
282 if (rest == NULL || rest[0] != '@') {
Holger Hans Peter Freytherea853042011-01-07 11:38:00 +0100283 LOGP(DMGCP, LOGL_ERROR, "Wrong endpoint name '%s'\n", mgcp);
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100284 return NULL;
285 }
286
287 /* signalling is on timeslot 1 */
288 if (endp == 1)
289 return NULL;
290
Holger Hans Peter Freytherea853042011-01-07 11:38:00 +0100291 mgcp_endp = mgcp_timeslot_to_endpoint(trunk - 1, endp);
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100292 if (mgcp_endp < 1 || mgcp_endp >= cfg->number_endpoints) {
293 LOGP(DMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n", mgcp);
Holger Hans Peter Freytherea853042011-01-07 11:38:00 +0100294 return NULL;
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100295 }
296
297 return &cfg->endpoints[mgcp_endp];
298}
299
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800300static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp)
301{
302 char *endptr = NULL;
303 unsigned int gw = INT_MAX;
304
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100305 if (strncmp(mgcp, "ds/e1", 5) == 0) {
306 return find_e1_endpoint(cfg, mgcp);
307 } else {
308 gw = strtoul(mgcp, &endptr, 16);
309 if (gw > 0 && gw < cfg->number_endpoints && strcmp(endptr, "@mgw") == 0)
310 return &cfg->endpoints[gw];
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800311 }
312
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100313 LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp);
314 return NULL;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800315}
316
317int mgcp_analyze_header(struct mgcp_config *cfg, struct msgb *msg,
318 struct mgcp_msg_ptr *ptr, int size,
319 const char **transaction_id, struct mgcp_endpoint **endp)
320{
321 int found;
322
323 *transaction_id = "000000";
324
325 if (size < 3) {
326 LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n");
327 return -1;
328 }
329
330 found = find_msg_pointers(msg, ptr, size);
331
332 if (found <= 3) {
333 LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found);
334 return -1;
335 }
336
337 /*
338 * replace the space with \0. the main method gurantess that
339 * we still have + 1 for null termination
340 */
341 msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
342 msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
343 msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
344 msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
345
346 if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
347 || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
348 LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n",
349 (const char *)&msg->l3h[ptr[3].start],
350 (const char *)&msg->l3h[ptr[2].start]);
351 return -1;
352 }
353
354 *transaction_id = (const char *)&msg->l3h[ptr[0].start];
355 if (endp) {
356 *endp = find_endpoint(cfg, (const char *)&msg->l3h[ptr[1].start]);
357 return *endp == NULL;
358 }
359 return 0;
360}
361
362static int verify_call_id(const struct mgcp_endpoint *endp,
363 const char *callid)
364{
365 if (strcmp(endp->callid, callid) != 0) {
366 LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
367 ENDPOINT_NUMBER(endp), endp->callid, callid);
368 return -1;
369 }
370
371 return 0;
372}
373
374static int verify_ci(const struct mgcp_endpoint *endp,
Holger Hans Peter Freyther5b084012010-08-08 07:51:51 +0800375 const char *_ci)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800376{
Holger Hans Peter Freyther5b084012010-08-08 07:51:51 +0800377 uint32_t ci = strtoul(_ci, NULL, 10);
378
379 if (ci != endp->ci) {
380 LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %u != %s\n",
381 ENDPOINT_NUMBER(endp), endp->ci, _ci);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800382 return -1;
383 }
384
385 return 0;
386}
387
388static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg)
389{
390 struct mgcp_msg_ptr data_ptrs[6];
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100391 int found;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800392 const char *trans_id;
393 struct mgcp_endpoint *endp;
394
395 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
396 if (found != 0)
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100397 return create_err_response(500, "AUEP", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800398 else
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100399 return create_ok_response(200, "AUEP", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800400}
401
402static int parse_conn_mode(const char* msg, int *conn_mode)
403{
404 int ret = 0;
405 if (strcmp(msg, "recvonly") == 0)
406 *conn_mode = MGCP_CONN_RECV_ONLY;
407 else if (strcmp(msg, "sendrecv") == 0)
408 *conn_mode = MGCP_CONN_RECV_SEND;
Holger Hans Peter Freytherb988caa2010-09-18 20:23:05 +0800409 else if (strcmp(msg, "sendonly") == 0)
410 *conn_mode = MGCP_CONN_SEND_ONLY;
Holger Hans Peter Freytherd86208d2010-08-04 06:11:27 +0800411 else if (strcmp(msg, "loopback") == 0)
412 *conn_mode = MGCP_CONN_LOOPBACK;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800413 else {
414 LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg);
415 ret = -1;
416 }
417
418 return ret;
419}
420
421static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg)
422{
423 struct mgcp_msg_ptr data_ptrs[6];
424 int found, i, line_start;
425 const char *trans_id;
426 struct mgcp_endpoint *endp;
427 int error_code = 500;
428 int port;
429
430 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
431 if (found != 0)
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100432 return create_err_response(510, "CRCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800433
434 if (endp->ci != CI_UNUSED) {
435 if (cfg->force_realloc) {
436 LOGP(DMGCP, LOGL_NOTICE, "Endpoint 0x%x already allocated. Forcing realloc.\n",
437 ENDPOINT_NUMBER(endp));
Holger Hans Peter Freytherd86208d2010-08-04 06:11:27 +0800438 mgcp_free_endp(endp);
Holger Hans Peter Freythere40bc382010-09-18 03:11:00 +0800439 if (cfg->realloc_cb)
440 cfg->realloc_cb(cfg, ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800441 } else {
442 LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n",
443 ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100444 return create_err_response(400, "CRCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800445 }
446 }
447
448 /* parse CallID C: and LocalParameters L: */
449 MSG_TOKENIZE_START
450 switch (msg->l3h[line_start]) {
451 case 'L':
452 endp->local_options = talloc_strdup(cfg->endpoints,
453 (const char *)&msg->l3h[line_start + 3]);
454 break;
455 case 'C':
456 endp->callid = talloc_strdup(cfg->endpoints,
457 (const char *)&msg->l3h[line_start + 3]);
458 break;
459 case 'M':
460 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
461 &endp->conn_mode) != 0) {
462 error_code = 517;
463 goto error2;
464 }
Holger Hans Peter Freytherd86208d2010-08-04 06:11:27 +0800465
466 endp->orig_mode = endp->conn_mode;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800467 break;
468 default:
469 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
470 msg->l3h[line_start], msg->l3h[line_start],
471 ENDPOINT_NUMBER(endp));
472 break;
473 }
474 MSG_TOKENIZE_END
475
476 /* initialize */
477 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
478
479 /* set to zero until we get the info */
480 memset(&endp->remote, 0, sizeof(endp->remote));
481
482 /* bind to the port now */
483 port = rtp_calculate_port(ENDPOINT_NUMBER(endp), cfg->rtp_base_port);
484 if (cfg->early_bind)
485 endp->rtp_port = port;
486 else if (mgcp_bind_rtp_port(endp, port) != 0)
487 goto error2;
488
489 /* assign a local call identifier or fail */
490 endp->ci = generate_call_id(cfg);
491 if (endp->ci == CI_UNUSED)
492 goto error2;
493
494 endp->bts_payload_type = cfg->audio_payload;
495
496 /* policy CB */
497 if (cfg->policy_cb) {
498 switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, trans_id)) {
499 case MGCP_POLICY_REJECT:
500 LOGP(DMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n",
501 ENDPOINT_NUMBER(endp));
502 mgcp_free_endp(endp);
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100503 return create_err_response(400, "CRCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800504 break;
505 case MGCP_POLICY_DEFER:
506 /* stop processing */
507 return NULL;
508 break;
509 case MGCP_POLICY_CONT:
510 /* just continue */
511 break;
512 }
513 }
514
515 LOGP(DMGCP, LOGL_NOTICE, "Creating endpoint on: 0x%x CI: %u port: %u\n",
516 ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
517 if (cfg->change_cb)
518 cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, endp->rtp_port);
519
520 return create_response_with_sdp(endp, "CRCX", trans_id);
521error:
522 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
523 hexdump(msg->l3h, msgb_l3len(msg)),
524 ENDPOINT_NUMBER(endp), line_start, i);
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100525 return create_err_response(error_code, "CRCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800526
527error2:
528 LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100529 return create_err_response(error_code, "CRCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800530}
531
532static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg)
533{
534 struct mgcp_msg_ptr data_ptrs[6];
535 int found, i, line_start;
536 const char *trans_id;
537 struct mgcp_endpoint *endp;
538 int error_code = 500;
539 int silent = 0;
540
541 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
542 if (found != 0)
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100543 return create_err_response(510, "MDCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800544
545 if (endp->ci == CI_UNUSED) {
546 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100547 return create_err_response(400, "MDCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800548 }
549
550 MSG_TOKENIZE_START
551 switch (msg->l3h[line_start]) {
552 case 'C': {
553 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
554 goto error3;
555 break;
556 }
557 case 'I': {
558 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
559 goto error3;
560 break;
561 }
562 case 'L':
563 /* skip */
564 break;
565 case 'M':
566 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
567 &endp->conn_mode) != 0) {
568 error_code = 517;
569 goto error3;
570 }
Holger Hans Peter Freytherd86208d2010-08-04 06:11:27 +0800571 endp->orig_mode = endp->conn_mode;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800572 break;
573 case 'Z':
574 silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0;
575 break;
576 case '\0':
577 /* SDP file begins */
578 break;
579 case 'a':
580 case 'o':
581 case 's':
582 case 't':
583 case 'v':
584 /* skip these SDP attributes */
585 break;
586 case 'm': {
587 int port;
588 int payload;
589 const char *param = (const char *)&msg->l3h[line_start];
590
591 if (sscanf(param, "m=audio %d RTP/AVP %d", &port, &payload) == 2) {
592 endp->net_rtp = htons(port);
593 endp->net_rtcp = htons(port + 1);
594 endp->net_payload_type = payload;
595 }
596 break;
597 }
598 case 'c': {
599 char ipv4[16];
600 const char *param = (const char *)&msg->l3h[line_start];
601
602 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
603 inet_aton(ipv4, &endp->remote);
604 }
605 break;
606 }
607 default:
608 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
609 msg->l3h[line_start], msg->l3h[line_start],
610 ENDPOINT_NUMBER(endp));
611 break;
612 }
613 MSG_TOKENIZE_END
614
615 /* policy CB */
616 if (cfg->policy_cb) {
617 switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, trans_id)) {
618 case MGCP_POLICY_REJECT:
619 LOGP(DMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n",
620 ENDPOINT_NUMBER(endp));
621 if (silent)
622 goto out_silent;
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100623 return create_err_response(400, "MDCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800624 break;
625 case MGCP_POLICY_DEFER:
626 /* stop processing */
627 return NULL;
628 break;
629 case MGCP_POLICY_CONT:
630 /* just continue */
631 break;
632 }
633 }
634
635 /* modify */
636 LOGP(DMGCP, LOGL_NOTICE, "Modified endpoint on: 0x%x Server: %s:%u\n",
637 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), ntohs(endp->net_rtp));
638 if (cfg->change_cb)
639 cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, endp->rtp_port);
640 if (silent)
641 goto out_silent;
642
643 return create_response_with_sdp(endp, "MDCX", trans_id);
644
645error:
646 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
647 hexdump(msg->l3h, msgb_l3len(msg)),
648 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100649 return create_err_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800650
651error3:
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100652 return create_err_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800653
654
655out_silent:
656 return NULL;
657}
658
659static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg)
660{
661 struct mgcp_msg_ptr data_ptrs[6];
662 int found, i, line_start;
663 const char *trans_id;
664 struct mgcp_endpoint *endp;
665 int error_code = 500;
666 int silent = 0;
667
668 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
669 if (found != 0)
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100670 return create_err_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800671
672 if (endp->ci == CI_UNUSED) {
673 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100674 return create_err_response(400, "DLCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800675 }
676
677 MSG_TOKENIZE_START
678 switch (msg->l3h[line_start]) {
679 case 'C': {
680 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
681 goto error3;
682 break;
683 }
684 case 'I': {
685 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
686 goto error3;
687 break;
688 case 'Z':
689 silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0;
690 break;
691 }
692 default:
693 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
694 msg->l3h[line_start], msg->l3h[line_start],
695 ENDPOINT_NUMBER(endp));
696 break;
697 }
698 MSG_TOKENIZE_END
699
700 /* policy CB */
701 if (cfg->policy_cb) {
702 switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, trans_id)) {
703 case MGCP_POLICY_REJECT:
704 LOGP(DMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n",
705 ENDPOINT_NUMBER(endp));
706 if (silent)
707 goto out_silent;
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100708 return create_err_response(400, "DLCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800709 break;
710 case MGCP_POLICY_DEFER:
711 /* stop processing */
712 return NULL;
713 break;
714 case MGCP_POLICY_CONT:
715 /* just continue */
716 break;
717 }
718 }
719
720 /* free the connection */
721 LOGP(DMGCP, LOGL_NOTICE, "Deleted endpoint on: 0x%x Server: %s:%u\n",
722 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), ntohs(endp->net_rtp));
723 mgcp_free_endp(endp);
724 if (cfg->change_cb)
725 cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, endp->rtp_port);
726
727 if (silent)
728 goto out_silent;
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100729 return create_ok_response(250, "DLCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800730
731error:
732 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
733 hexdump(msg->l3h, msgb_l3len(msg)),
734 ENDPOINT_NUMBER(endp), line_start, i);
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100735 return create_err_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800736
737error3:
Holger Hans Peter Freyther7e0936e2011-01-06 19:48:55 +0100738 return create_err_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800739
740out_silent:
741 return NULL;
742}
743
744static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg)
745{
746 if (cfg->reset_cb)
747 cfg->reset_cb(cfg);
748 return NULL;
749}
750
Holger Hans Peter Freyther44f848b2011-01-25 23:41:30 +0100751/*
752 * This can request like DTMF detection and forward, fax detection... it
753 * can also request when the notification should be send and such. We don't
754 * do this right now.
755 */
756static struct msgb *handle_noti_req(struct mgcp_config *cfg, struct msgb *msg)
757{
758 struct mgcp_msg_ptr data_ptrs[6];
759 const char *trans_id;
760 struct mgcp_endpoint *endp;
761 int found;
762
763 found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
764 if (found != 0)
765 return create_err_response(400, "RQNT", trans_id);
766
767 if (endp->ci == CI_UNUSED) {
768 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
769 return create_err_response(400, "RQNT", trans_id);
770 }
771 return create_ok_response(200, "RQNT", trans_id);
772}
773
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800774struct mgcp_config *mgcp_config_alloc(void)
775{
776 struct mgcp_config *cfg;
777
778 cfg = talloc_zero(NULL, struct mgcp_config);
779 if (!cfg) {
780 LOGP(DMGCP, LOGL_FATAL, "Failed to allocate config.\n");
781 return NULL;
782 }
783
784 cfg->source_port = 2427;
785 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
786 cfg->audio_name = talloc_strdup(cfg, "GSM-EFR/8000");
787 cfg->audio_payload = 97;
788 cfg->rtp_base_port = RTP_PORT_DEFAULT;
789
790 return cfg;
791}
792
793int mgcp_endpoints_allocate(struct mgcp_config *cfg)
794{
795 int i;
796
797 /* Initialize all endpoints */
798 cfg->endpoints = _talloc_zero_array(cfg,
799 sizeof(struct mgcp_endpoint),
800 cfg->number_endpoints, "endpoints");
801 if (!cfg->endpoints)
802 return -1;
803
804 for (i = 0; i < cfg->number_endpoints; ++i) {
805 cfg->endpoints[i].local_rtp.fd = -1;
806 cfg->endpoints[i].local_rtcp.fd = -1;
807 cfg->endpoints[i].ci = CI_UNUSED;
808 cfg->endpoints[i].cfg = cfg;
809 cfg->endpoints[i].net_payload_type = -1;
810 cfg->endpoints[i].bts_payload_type = -1;
811 }
812
813 return 0;
814}
815
816void mgcp_free_endp(struct mgcp_endpoint *endp)
817{
818 LOGP(DMGCP, LOGL_DEBUG, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freytherd86208d2010-08-04 06:11:27 +0800819 endp->ci = CI_UNUSED;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800820
821 if (endp->callid) {
822 talloc_free(endp->callid);
823 endp->callid = NULL;
824 }
825
826 if (endp->local_options) {
827 talloc_free(endp->local_options);
828 endp->local_options = NULL;
829 }
830
831 if (!endp->cfg->early_bind) {
832 bsc_unregister_fd(&endp->local_rtp);
833 bsc_unregister_fd(&endp->local_rtcp);
834 }
835
836 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
837 endp->net_payload_type = endp->bts_payload_type = -1;
838 endp->in_bts = endp->in_remote = 0;
839 memset(&endp->remote, 0, sizeof(endp->remote));
840 memset(&endp->bts, 0, sizeof(endp->bts));
Holger Hans Peter Freytherd86208d2010-08-04 06:11:27 +0800841
842 memset(&endp->net_state, 0, sizeof(endp->net_state));
843 memset(&endp->bts_state, 0, sizeof(endp->bts_state));
844
845 endp->conn_mode = endp->orig_mode = MGCP_CONN_NONE;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800846}