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