Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2010 by On-Waves |
| 7 | * 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 Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 33 | #include <cellmgr_debug.h> |
| 34 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 35 | #include <mgcp/mgcp.h> |
| 36 | #include <mgcp/mgcp_internal.h> |
| 37 | |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 38 | #include <osmocore/msgb.h> |
| 39 | #include <osmocore/talloc.h> |
| 40 | #include <osmocore/select.h> |
| 41 | #include <osmocore/utils.h> |
| 42 | |
| 43 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 44 | /** |
| 45 | * Macro for tokenizing MGCP messages and SDP in one go. |
| 46 | * |
| 47 | */ |
| 48 | #define MSG_TOKENIZE_START \ |
| 49 | line_start = 0; \ |
| 50 | for (i = 0; i < msgb_l3len(msg); ++i) { \ |
| 51 | /* we have a line end */ \ |
| 52 | if (msg->l3h[i] == '\n') { \ |
| 53 | /* skip the first line */ \ |
| 54 | if (line_start == 0) { \ |
| 55 | line_start = i + 1; \ |
| 56 | continue; \ |
| 57 | } \ |
| 58 | \ |
| 59 | /* check if we have a proper param */ \ |
| 60 | if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \ |
| 61 | } else if (i - line_start > 2 \ |
| 62 | && islower(msg->l3h[line_start]) \ |
| 63 | && msg->l3h[line_start + 1] == '=') { \ |
| 64 | } else if (i - line_start < 3 \ |
| 65 | || msg->l3h[line_start + 1] != ':' \ |
| 66 | || msg->l3h[line_start + 2] != ' ') \ |
| 67 | goto error; \ |
| 68 | \ |
| 69 | msg->l3h[i] = '\0'; \ |
| 70 | if (msg->l3h[i-1] == '\r') \ |
| 71 | msg->l3h[i-1] = '\0'; |
| 72 | |
| 73 | #define MSG_TOKENIZE_END \ |
| 74 | line_start = i + 1; \ |
| 75 | } \ |
| 76 | } |
| 77 | |
| 78 | |
| 79 | struct mgcp_request { |
| 80 | char *name; |
| 81 | struct msgb *(*handle_request) (struct mgcp_config *cfg, struct msgb *msg); |
| 82 | char *debug_name; |
| 83 | }; |
| 84 | |
| 85 | #define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \ |
| 86 | { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME }, |
| 87 | |
| 88 | static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg); |
| 89 | static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg); |
| 90 | static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg); |
| 91 | static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg); |
| 92 | static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg); |
| 93 | |
| 94 | static int generate_call_id(struct mgcp_config *cfg) |
| 95 | { |
| 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 | */ |
| 119 | static 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") |
| 124 | |
| 125 | /* SPEC extension */ |
| 126 | MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress") |
| 127 | }; |
| 128 | |
| 129 | static struct msgb *mgcp_msgb_alloc(void) |
| 130 | { |
| 131 | struct msgb *msg; |
| 132 | msg = msgb_alloc_headroom(4096, 128, "MGCP msg"); |
| 133 | if (!msg) |
| 134 | LOGP(DMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n"); |
| 135 | |
| 136 | return msg; |
| 137 | } |
| 138 | |
| 139 | struct msgb *mgcp_create_response_with_data(int code, const char *msg, const char *trans, |
| 140 | const char *data) |
| 141 | { |
| 142 | int len; |
| 143 | struct msgb *res; |
| 144 | |
| 145 | res = mgcp_msgb_alloc(); |
| 146 | if (!res) |
| 147 | return NULL; |
| 148 | |
| 149 | if (data) { |
| 150 | len = snprintf((char *) res->data, 2048, "%d %s\n%s", code, trans, data); |
| 151 | } else { |
| 152 | len = snprintf((char *) res->data, 2048, "%d %s\n", code, trans); |
| 153 | } |
| 154 | |
| 155 | res->l2h = msgb_put(res, len); |
| 156 | LOGP(DMGCP, LOGL_DEBUG, "Sending response: code: %d for '%s'\n", code, res->l2h); |
| 157 | return res; |
| 158 | } |
| 159 | |
| 160 | static struct msgb *create_response(int code, const char *msg, const char *trans) |
| 161 | { |
| 162 | return mgcp_create_response_with_data(code, msg, trans, NULL); |
| 163 | } |
| 164 | |
| 165 | static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, |
| 166 | const char *msg, const char *trans_id) |
| 167 | { |
| 168 | const char *addr = endp->cfg->local_ip; |
| 169 | char sdp_record[4096]; |
| 170 | |
| 171 | if (!addr) |
| 172 | addr = endp->cfg->source_addr; |
| 173 | |
| 174 | snprintf(sdp_record, sizeof(sdp_record) - 1, |
| 175 | "I: %d\n\n" |
| 176 | "v=0\r\n" |
| 177 | "c=IN IP4 %s\r\n" |
| 178 | "m=audio %d RTP/AVP %d\r\n" |
| 179 | "a=rtpmap:%d %s\r\n", |
| 180 | endp->ci, addr, endp->rtp_port, |
| 181 | endp->bts_payload_type, endp->bts_payload_type, |
| 182 | endp->cfg->audio_name); |
| 183 | return mgcp_create_response_with_data(200, msg, trans_id, sdp_record); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * handle incoming messages: |
| 188 | * - this can be a command (four letters, space, transaction id) |
| 189 | * - or a response (three numbers, space, transaction id) |
| 190 | */ |
| 191 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) |
| 192 | { |
| 193 | int code; |
| 194 | struct msgb *resp = NULL; |
| 195 | |
| 196 | if (msgb_l2len(msg) < 4) { |
| 197 | LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | /* attempt to treat it as a response */ |
| 202 | if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) { |
| 203 | LOGP(DMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); |
| 204 | } else { |
| 205 | int i, handled = 0; |
| 206 | msg->l3h = &msg->l2h[4]; |
| 207 | for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) |
| 208 | if (strncmp(mgcp_requests[i].name, (const char *) &msg->l2h[0], 4) == 0) { |
| 209 | handled = 1; |
| 210 | resp = mgcp_requests[i].handle_request(cfg, msg); |
| 211 | break; |
| 212 | } |
| 213 | if (!handled) { |
| 214 | LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return resp; |
| 219 | } |
| 220 | |
| 221 | /* string tokenizer for the poor */ |
| 222 | static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length) |
| 223 | { |
| 224 | int i, found = 0; |
| 225 | |
| 226 | int whitespace = 1; |
| 227 | for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) { |
| 228 | /* if we have a space we found an end */ |
| 229 | if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') { |
| 230 | if (!whitespace) { |
| 231 | ++found; |
| 232 | whitespace = 1; |
| 233 | ptrs->length = i - ptrs->start - 1; |
| 234 | ++ptrs; |
| 235 | --ptrs_length; |
| 236 | } else { |
| 237 | /* skip any number of whitespace */ |
| 238 | } |
| 239 | |
| 240 | /* line end... stop */ |
| 241 | if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') |
| 242 | break; |
| 243 | } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') { |
| 244 | /* line end, be done */ |
| 245 | break; |
| 246 | } else if (whitespace) { |
| 247 | whitespace = 0; |
| 248 | ptrs->start = i; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if (ptrs_length == 0) |
| 253 | return -1; |
| 254 | return found; |
| 255 | } |
| 256 | |
| 257 | static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp) |
| 258 | { |
| 259 | char *endptr = NULL; |
| 260 | unsigned int gw = INT_MAX; |
| 261 | |
| 262 | gw = strtoul(mgcp, &endptr, 16); |
| 263 | if (gw == 0 || gw >= cfg->number_endpoints || strcmp(endptr, "@mgw") != 0) { |
| 264 | LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp); |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | return &cfg->endpoints[gw]; |
| 269 | } |
| 270 | |
| 271 | int mgcp_analyze_header(struct mgcp_config *cfg, struct msgb *msg, |
| 272 | struct mgcp_msg_ptr *ptr, int size, |
| 273 | const char **transaction_id, struct mgcp_endpoint **endp) |
| 274 | { |
| 275 | int found; |
| 276 | |
| 277 | *transaction_id = "000000"; |
| 278 | |
| 279 | if (size < 3) { |
| 280 | LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n"); |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | found = find_msg_pointers(msg, ptr, size); |
| 285 | |
| 286 | if (found <= 3) { |
| 287 | LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found); |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * replace the space with \0. the main method gurantess that |
| 293 | * we still have + 1 for null termination |
| 294 | */ |
| 295 | msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0'; |
| 296 | msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0'; |
| 297 | msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0'; |
| 298 | msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0'; |
| 299 | |
| 300 | if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0 |
| 301 | || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) { |
| 302 | LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n", |
| 303 | (const char *)&msg->l3h[ptr[3].start], |
| 304 | (const char *)&msg->l3h[ptr[2].start]); |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | *transaction_id = (const char *)&msg->l3h[ptr[0].start]; |
| 309 | if (endp) { |
| 310 | *endp = find_endpoint(cfg, (const char *)&msg->l3h[ptr[1].start]); |
| 311 | return *endp == NULL; |
| 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int verify_call_id(const struct mgcp_endpoint *endp, |
| 317 | const char *callid) |
| 318 | { |
| 319 | if (strcmp(endp->callid, callid) != 0) { |
| 320 | LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n", |
| 321 | ENDPOINT_NUMBER(endp), endp->callid, callid); |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int verify_ci(const struct mgcp_endpoint *endp, |
| 329 | const char *ci) |
| 330 | { |
| 331 | if (atoi(ci) != endp->ci) { |
| 332 | LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n", |
| 333 | ENDPOINT_NUMBER(endp), endp->ci, ci); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg) |
| 341 | { |
| 342 | struct mgcp_msg_ptr data_ptrs[6]; |
| 343 | int found, response; |
| 344 | const char *trans_id; |
| 345 | struct mgcp_endpoint *endp; |
| 346 | |
| 347 | found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
| 348 | if (found != 0) |
| 349 | response = 500; |
| 350 | else |
| 351 | response = 200; |
| 352 | |
| 353 | return create_response(response, "AUEP", trans_id); |
| 354 | } |
| 355 | |
| 356 | static int parse_conn_mode(const char* msg, int *conn_mode) |
| 357 | { |
| 358 | int ret = 0; |
| 359 | if (strcmp(msg, "recvonly") == 0) |
| 360 | *conn_mode = MGCP_CONN_RECV_ONLY; |
| 361 | else if (strcmp(msg, "sendrecv") == 0) |
| 362 | *conn_mode = MGCP_CONN_RECV_SEND; |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame] | 363 | else if (strcmp(msg, "loopback") == 0) |
| 364 | *conn_mode = MGCP_CONN_LOOPBACK; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 365 | else { |
| 366 | LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg); |
| 367 | ret = -1; |
| 368 | } |
| 369 | |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg) |
| 374 | { |
| 375 | struct mgcp_msg_ptr data_ptrs[6]; |
| 376 | int found, i, line_start; |
| 377 | const char *trans_id; |
| 378 | struct mgcp_endpoint *endp; |
| 379 | int error_code = 500; |
| 380 | int port; |
| 381 | |
| 382 | found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
| 383 | if (found != 0) |
| 384 | return create_response(500, "CRCX", trans_id); |
| 385 | |
| 386 | if (endp->ci != CI_UNUSED) { |
| 387 | if (cfg->force_realloc) { |
| 388 | LOGP(DMGCP, LOGL_NOTICE, "Endpoint 0x%x already allocated. Forcing realloc.\n", |
| 389 | ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame] | 390 | mgcp_free_endp(endp); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 391 | } else { |
| 392 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n", |
| 393 | ENDPOINT_NUMBER(endp)); |
| 394 | return create_response(500, "CRCX", trans_id); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /* parse CallID C: and LocalParameters L: */ |
| 399 | MSG_TOKENIZE_START |
| 400 | switch (msg->l3h[line_start]) { |
| 401 | case 'L': |
| 402 | endp->local_options = talloc_strdup(cfg->endpoints, |
| 403 | (const char *)&msg->l3h[line_start + 3]); |
| 404 | break; |
| 405 | case 'C': |
| 406 | endp->callid = talloc_strdup(cfg->endpoints, |
| 407 | (const char *)&msg->l3h[line_start + 3]); |
| 408 | break; |
| 409 | case 'M': |
| 410 | if (parse_conn_mode((const char *)&msg->l3h[line_start + 3], |
| 411 | &endp->conn_mode) != 0) { |
| 412 | error_code = 517; |
| 413 | goto error2; |
| 414 | } |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame] | 415 | |
| 416 | endp->orig_mode = endp->conn_mode; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 417 | break; |
| 418 | default: |
| 419 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
| 420 | msg->l3h[line_start], msg->l3h[line_start], |
| 421 | ENDPOINT_NUMBER(endp)); |
| 422 | break; |
| 423 | } |
| 424 | MSG_TOKENIZE_END |
| 425 | |
| 426 | /* initialize */ |
| 427 | endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0; |
| 428 | |
| 429 | /* set to zero until we get the info */ |
| 430 | memset(&endp->remote, 0, sizeof(endp->remote)); |
| 431 | |
| 432 | /* bind to the port now */ |
| 433 | port = rtp_calculate_port(ENDPOINT_NUMBER(endp), cfg->rtp_base_port); |
| 434 | if (cfg->early_bind) |
| 435 | endp->rtp_port = port; |
| 436 | else if (mgcp_bind_rtp_port(endp, port) != 0) |
| 437 | goto error2; |
| 438 | |
| 439 | /* assign a local call identifier or fail */ |
| 440 | endp->ci = generate_call_id(cfg); |
| 441 | if (endp->ci == CI_UNUSED) |
| 442 | goto error2; |
| 443 | |
| 444 | endp->bts_payload_type = cfg->audio_payload; |
| 445 | |
| 446 | /* policy CB */ |
| 447 | if (cfg->policy_cb) { |
| 448 | switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, trans_id)) { |
| 449 | case MGCP_POLICY_REJECT: |
| 450 | LOGP(DMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n", |
| 451 | ENDPOINT_NUMBER(endp)); |
| 452 | mgcp_free_endp(endp); |
| 453 | return create_response(500, "CRCX", trans_id); |
| 454 | break; |
| 455 | case MGCP_POLICY_DEFER: |
| 456 | /* stop processing */ |
| 457 | return NULL; |
| 458 | break; |
| 459 | case MGCP_POLICY_CONT: |
| 460 | /* just continue */ |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | LOGP(DMGCP, LOGL_NOTICE, "Creating endpoint on: 0x%x CI: %u port: %u\n", |
| 466 | ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port); |
| 467 | if (cfg->change_cb) |
| 468 | cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, endp->rtp_port); |
| 469 | |
| 470 | return create_response_with_sdp(endp, "CRCX", trans_id); |
| 471 | error: |
| 472 | LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n", |
| 473 | hexdump(msg->l3h, msgb_l3len(msg)), |
| 474 | ENDPOINT_NUMBER(endp), line_start, i); |
| 475 | return create_response(error_code, "CRCX", trans_id); |
| 476 | |
| 477 | error2: |
| 478 | LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp)); |
| 479 | return create_response(error_code, "CRCX", trans_id); |
| 480 | } |
| 481 | |
| 482 | static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg) |
| 483 | { |
| 484 | struct mgcp_msg_ptr data_ptrs[6]; |
| 485 | int found, i, line_start; |
| 486 | const char *trans_id; |
| 487 | struct mgcp_endpoint *endp; |
| 488 | int error_code = 500; |
| 489 | int silent = 0; |
| 490 | |
| 491 | found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
| 492 | if (found != 0) |
| 493 | return create_response(error_code, "MDCX", trans_id); |
| 494 | |
| 495 | if (endp->ci == CI_UNUSED) { |
| 496 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp)); |
| 497 | return create_response(error_code, "MDCX", trans_id); |
| 498 | } |
| 499 | |
| 500 | MSG_TOKENIZE_START |
| 501 | switch (msg->l3h[line_start]) { |
| 502 | case 'C': { |
| 503 | if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 504 | goto error3; |
| 505 | break; |
| 506 | } |
| 507 | case 'I': { |
| 508 | if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 509 | goto error3; |
| 510 | break; |
| 511 | } |
| 512 | case 'L': |
| 513 | /* skip */ |
| 514 | break; |
| 515 | case 'M': |
| 516 | if (parse_conn_mode((const char *)&msg->l3h[line_start + 3], |
| 517 | &endp->conn_mode) != 0) { |
| 518 | error_code = 517; |
| 519 | goto error3; |
| 520 | } |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame] | 521 | endp->orig_mode = endp->conn_mode; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 522 | break; |
| 523 | case 'Z': |
| 524 | silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0; |
| 525 | break; |
| 526 | case '\0': |
| 527 | /* SDP file begins */ |
| 528 | break; |
| 529 | case 'a': |
| 530 | case 'o': |
| 531 | case 's': |
| 532 | case 't': |
| 533 | case 'v': |
| 534 | /* skip these SDP attributes */ |
| 535 | break; |
| 536 | case 'm': { |
| 537 | int port; |
| 538 | int payload; |
| 539 | const char *param = (const char *)&msg->l3h[line_start]; |
| 540 | |
| 541 | if (sscanf(param, "m=audio %d RTP/AVP %d", &port, &payload) == 2) { |
| 542 | endp->net_rtp = htons(port); |
| 543 | endp->net_rtcp = htons(port + 1); |
| 544 | endp->net_payload_type = payload; |
| 545 | } |
| 546 | break; |
| 547 | } |
| 548 | case 'c': { |
| 549 | char ipv4[16]; |
| 550 | const char *param = (const char *)&msg->l3h[line_start]; |
| 551 | |
| 552 | if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) { |
| 553 | inet_aton(ipv4, &endp->remote); |
| 554 | } |
| 555 | break; |
| 556 | } |
| 557 | default: |
| 558 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
| 559 | msg->l3h[line_start], msg->l3h[line_start], |
| 560 | ENDPOINT_NUMBER(endp)); |
| 561 | break; |
| 562 | } |
| 563 | MSG_TOKENIZE_END |
| 564 | |
| 565 | /* policy CB */ |
| 566 | if (cfg->policy_cb) { |
| 567 | switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, trans_id)) { |
| 568 | case MGCP_POLICY_REJECT: |
| 569 | LOGP(DMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n", |
| 570 | ENDPOINT_NUMBER(endp)); |
| 571 | if (silent) |
| 572 | goto out_silent; |
| 573 | return create_response(500, "MDCX", trans_id); |
| 574 | break; |
| 575 | case MGCP_POLICY_DEFER: |
| 576 | /* stop processing */ |
| 577 | return NULL; |
| 578 | break; |
| 579 | case MGCP_POLICY_CONT: |
| 580 | /* just continue */ |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /* modify */ |
| 586 | LOGP(DMGCP, LOGL_NOTICE, "Modified endpoint on: 0x%x Server: %s:%u\n", |
| 587 | ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), ntohs(endp->net_rtp)); |
| 588 | if (cfg->change_cb) |
| 589 | cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, endp->rtp_port); |
| 590 | if (silent) |
| 591 | goto out_silent; |
| 592 | |
| 593 | return create_response_with_sdp(endp, "MDCX", trans_id); |
| 594 | |
| 595 | error: |
| 596 | LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n", |
| 597 | hexdump(msg->l3h, msgb_l3len(msg)), |
| 598 | ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]); |
| 599 | return create_response(error_code, "MDCX", trans_id); |
| 600 | |
| 601 | error3: |
| 602 | return create_response(error_code, "MDCX", trans_id); |
| 603 | |
| 604 | |
| 605 | out_silent: |
| 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg) |
| 610 | { |
| 611 | struct mgcp_msg_ptr data_ptrs[6]; |
| 612 | int found, i, line_start; |
| 613 | const char *trans_id; |
| 614 | struct mgcp_endpoint *endp; |
| 615 | int error_code = 500; |
| 616 | int silent = 0; |
| 617 | |
| 618 | found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
| 619 | if (found != 0) |
| 620 | return create_response(error_code, "DLCX", trans_id); |
| 621 | |
| 622 | if (endp->ci == CI_UNUSED) { |
| 623 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp)); |
| 624 | return create_response(error_code, "DLCX", trans_id); |
| 625 | } |
| 626 | |
| 627 | MSG_TOKENIZE_START |
| 628 | switch (msg->l3h[line_start]) { |
| 629 | case 'C': { |
| 630 | if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 631 | goto error3; |
| 632 | break; |
| 633 | } |
| 634 | case 'I': { |
| 635 | if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 636 | goto error3; |
| 637 | break; |
| 638 | case 'Z': |
| 639 | silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0; |
| 640 | break; |
| 641 | } |
| 642 | default: |
| 643 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
| 644 | msg->l3h[line_start], msg->l3h[line_start], |
| 645 | ENDPOINT_NUMBER(endp)); |
| 646 | break; |
| 647 | } |
| 648 | MSG_TOKENIZE_END |
| 649 | |
| 650 | /* policy CB */ |
| 651 | if (cfg->policy_cb) { |
| 652 | switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, trans_id)) { |
| 653 | case MGCP_POLICY_REJECT: |
| 654 | LOGP(DMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n", |
| 655 | ENDPOINT_NUMBER(endp)); |
| 656 | if (silent) |
| 657 | goto out_silent; |
| 658 | return create_response(500, "DLCX", trans_id); |
| 659 | break; |
| 660 | case MGCP_POLICY_DEFER: |
| 661 | /* stop processing */ |
| 662 | return NULL; |
| 663 | break; |
| 664 | case MGCP_POLICY_CONT: |
| 665 | /* just continue */ |
| 666 | break; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /* free the connection */ |
| 671 | LOGP(DMGCP, LOGL_NOTICE, "Deleted endpoint on: 0x%x Server: %s:%u\n", |
| 672 | ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), ntohs(endp->net_rtp)); |
| 673 | mgcp_free_endp(endp); |
| 674 | if (cfg->change_cb) |
| 675 | cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, endp->rtp_port); |
| 676 | |
| 677 | if (silent) |
| 678 | goto out_silent; |
| 679 | return create_response(250, "DLCX", trans_id); |
| 680 | |
| 681 | error: |
| 682 | LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n", |
| 683 | hexdump(msg->l3h, msgb_l3len(msg)), |
| 684 | ENDPOINT_NUMBER(endp), line_start, i); |
| 685 | return create_response(error_code, "DLCX", trans_id); |
| 686 | |
| 687 | error3: |
| 688 | return create_response(error_code, "DLCX", trans_id); |
| 689 | |
| 690 | out_silent: |
| 691 | return NULL; |
| 692 | } |
| 693 | |
| 694 | static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg) |
| 695 | { |
| 696 | if (cfg->reset_cb) |
| 697 | cfg->reset_cb(cfg); |
| 698 | return NULL; |
| 699 | } |
| 700 | |
| 701 | struct mgcp_config *mgcp_config_alloc(void) |
| 702 | { |
| 703 | struct mgcp_config *cfg; |
| 704 | |
| 705 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 706 | if (!cfg) { |
| 707 | LOGP(DMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 708 | return NULL; |
| 709 | } |
| 710 | |
| 711 | cfg->source_port = 2427; |
| 712 | cfg->source_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 713 | cfg->audio_name = talloc_strdup(cfg, "GSM-EFR/8000"); |
| 714 | cfg->audio_payload = 97; |
| 715 | cfg->rtp_base_port = RTP_PORT_DEFAULT; |
| 716 | |
| 717 | return cfg; |
| 718 | } |
| 719 | |
| 720 | int mgcp_endpoints_allocate(struct mgcp_config *cfg) |
| 721 | { |
| 722 | int i; |
| 723 | |
| 724 | /* Initialize all endpoints */ |
| 725 | cfg->endpoints = _talloc_zero_array(cfg, |
| 726 | sizeof(struct mgcp_endpoint), |
| 727 | cfg->number_endpoints, "endpoints"); |
| 728 | if (!cfg->endpoints) |
| 729 | return -1; |
| 730 | |
| 731 | for (i = 0; i < cfg->number_endpoints; ++i) { |
| 732 | cfg->endpoints[i].local_rtp.fd = -1; |
| 733 | cfg->endpoints[i].local_rtcp.fd = -1; |
| 734 | cfg->endpoints[i].ci = CI_UNUSED; |
| 735 | cfg->endpoints[i].cfg = cfg; |
| 736 | cfg->endpoints[i].net_payload_type = -1; |
| 737 | cfg->endpoints[i].bts_payload_type = -1; |
| 738 | } |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | void mgcp_free_endp(struct mgcp_endpoint *endp) |
| 744 | { |
| 745 | LOGP(DMGCP, LOGL_DEBUG, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame] | 746 | endp->ci = CI_UNUSED; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 747 | |
| 748 | if (endp->callid) { |
| 749 | talloc_free(endp->callid); |
| 750 | endp->callid = NULL; |
| 751 | } |
| 752 | |
| 753 | if (endp->local_options) { |
| 754 | talloc_free(endp->local_options); |
| 755 | endp->local_options = NULL; |
| 756 | } |
| 757 | |
| 758 | if (!endp->cfg->early_bind) { |
| 759 | bsc_unregister_fd(&endp->local_rtp); |
| 760 | bsc_unregister_fd(&endp->local_rtcp); |
| 761 | } |
| 762 | |
| 763 | endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0; |
| 764 | endp->net_payload_type = endp->bts_payload_type = -1; |
| 765 | endp->in_bts = endp->in_remote = 0; |
| 766 | memset(&endp->remote, 0, sizeof(endp->remote)); |
| 767 | memset(&endp->bts, 0, sizeof(endp->bts)); |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame] | 768 | |
| 769 | memset(&endp->net_state, 0, sizeof(endp->net_state)); |
| 770 | memset(&endp->bts_state, 0, sizeof(endp->bts_state)); |
| 771 | |
| 772 | endp->conn_mode = endp->orig_mode = MGCP_CONN_NONE; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 773 | } |