Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 5 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2010 by On-Waves |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 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 | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 34 | #include <osmocore/msgb.h> |
| 35 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 36 | #include <openbsc/gsm_data.h> |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 37 | #include <osmocore/select.h> |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 38 | #include <openbsc/mgcp.h> |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 39 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 41 | enum 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 Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 48 | /** |
| 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 | |
| 83 | struct mgcp_msg_ptr { |
| 84 | unsigned int start; |
| 85 | unsigned int length; |
| 86 | }; |
| 87 | |
| 88 | struct mgcp_request { |
| 89 | char *name; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 90 | struct msgb *(*handle_request) (struct mgcp_config *cfg, struct msgb *msg); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 91 | 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 Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 97 | static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg); |
| 98 | static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg); |
| 99 | static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg); |
| 100 | static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 101 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 102 | static int generate_call_id(struct mgcp_config *cfg) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 103 | { |
| 104 | int i; |
| 105 | |
| 106 | /* use the call id */ |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 107 | ++cfg->last_call_id; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 108 | |
| 109 | /* handle wrap around */ |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 110 | if (cfg->last_call_id == CI_UNUSED) |
| 111 | ++cfg->last_call_id; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 112 | |
| 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 Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 115 | 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 Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 118 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 119 | return cfg->last_call_id; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* FIXIME/TODO: need to have a list of pending transactions and check that */ |
| 123 | static unsigned int generate_transaction_id() |
| 124 | { |
| 125 | return abs(rand()); |
| 126 | } |
| 127 | |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 128 | /* |
| 129 | * array of function pointers for handling various |
| 130 | * messages. In the future this might be binary sorted |
| 131 | * for performance reasons. |
| 132 | */ |
| 133 | static 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 Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 140 | static struct msgb *mgcp_msgb_alloc(void) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 141 | { |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 142 | 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 Freyther | 6414a0c | 2010-02-21 10:50:46 +0100 | [diff] [blame] | 150 | struct msgb *mgcp_create_response_with_data(int code, const char *msg, const char *trans, |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 151 | const char *data) |
| 152 | { |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 153 | int len; |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 154 | struct msgb *res; |
| 155 | |
| 156 | res = mgcp_msgb_alloc(); |
| 157 | if (!res) |
| 158 | return NULL; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 159 | |
| 160 | if (data) { |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 161 | len = snprintf((char *) res->data, 2048, "%d %s\n%s", code, trans, data); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 162 | } else { |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 163 | len = snprintf((char *) res->data, 2048, "%d %s\n", code, trans); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 164 | } |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 165 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 166 | res->l2h = msgb_put(res, len); |
Holger Hans Peter Freyther | 63f2db2 | 2010-02-26 13:30:57 +0100 | [diff] [blame] | 167 | LOGP(DMGCP, LOGL_DEBUG, "Sending response: code: %d for '%s'\n", code, res->l2h); |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 168 | return res; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 171 | static struct msgb *create_response(int code, const char *msg, const char *trans) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 172 | { |
Holger Hans Peter Freyther | 6414a0c | 2010-02-21 10:50:46 +0100 | [diff] [blame] | 173 | return mgcp_create_response_with_data(code, msg, trans, NULL); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 176 | static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, |
| 177 | const char *msg, const char *trans_id) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 178 | { |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 179 | const char *addr = endp->cfg->local_ip; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 180 | char sdp_record[4096]; |
| 181 | |
| 182 | if (!addr) |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 183 | addr = endp->cfg->source_addr; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 184 | |
| 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 Freyther | ef6bb25 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 192 | endp->bts_payload_type, endp->bts_payload_type, |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 193 | endp->cfg->audio_name); |
Holger Hans Peter Freyther | 6414a0c | 2010-02-21 10:50:46 +0100 | [diff] [blame] | 194 | return mgcp_create_response_with_data(200, msg, trans_id, sdp_record); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* send a static record */ |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 198 | struct msgb *mgcp_create_rsip(void) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 199 | { |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 200 | struct msgb *msg; |
| 201 | int len; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 202 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 203 | msg = mgcp_msgb_alloc(); |
| 204 | if (!msg) |
| 205 | return NULL; |
| 206 | |
| 207 | len = snprintf((char *) msg->data, 2048, |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 208 | "RSIP %u *@mgw MGCP 1.0\n" |
| 209 | "RM: restart\n", generate_transaction_id()); |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 210 | msg->l2h = msgb_put(msg, len); |
| 211 | return msg; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 212 | } |
| 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 Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 219 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 220 | { |
| 221 | int code; |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 222 | struct msgb *resp = NULL; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 223 | |
| 224 | if (msg->len < 4) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 225 | LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len); |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 226 | return NULL; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* attempt to treat it as a response */ |
| 230 | if (sscanf((const char *)&msg->data[0], "%3d %*s", &code) == 1) { |
Holger Hans Peter Freyther | 63f2db2 | 2010-02-26 13:30:57 +0100 | [diff] [blame] | 231 | LOGP(DMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 232 | } 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 Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 238 | resp = mgcp_requests[i].handle_request(cfg, msg); |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 239 | break; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 240 | } |
| 241 | if (!handled) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 242 | LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->data[0]); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 246 | return resp; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* string tokenizer for the poor */ |
| 250 | static 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 Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 285 | static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 286 | { |
| 287 | char *endptr = NULL; |
| 288 | unsigned int gw = INT_MAX; |
| 289 | |
| 290 | gw = strtoul(mgcp, &endptr, 16); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 291 | if (gw == 0 || gw >= cfg->number_endpoints || strcmp(endptr, "@mgw") != 0) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 292 | LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 293 | return NULL; |
| 294 | } |
| 295 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 296 | return &cfg->endpoints[gw]; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 299 | static int analyze_header(struct mgcp_config *cfg, struct msgb *msg, |
| 300 | struct mgcp_msg_ptr *ptr, int size, |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 301 | const char **transaction_id, struct mgcp_endpoint **endp) |
| 302 | { |
| 303 | int found; |
| 304 | |
Holger Hans Peter Freyther | a820c5f | 2010-02-26 13:32:55 +0100 | [diff] [blame] | 305 | *transaction_id = "000000"; |
| 306 | |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 307 | if (size < 3) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 308 | LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n"); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | found = find_msg_pointers(msg, ptr, size); |
| 313 | |
Holger Hans Peter Freyther | a820c5f | 2010-02-26 13:32:55 +0100 | [diff] [blame] | 314 | if (found <= 3) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 315 | LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * replace the space with \0. the main method gurantess that |
| 321 | * we still have + 1 for null termination |
| 322 | */ |
| 323 | msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0'; |
| 324 | msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0'; |
| 325 | msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0'; |
| 326 | msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0'; |
| 327 | |
| 328 | if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0 |
| 329 | || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 330 | LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 331 | (const char *)&msg->l3h[ptr[3].start], |
| 332 | (const char *)&msg->l3h[ptr[2].start]); |
| 333 | return -1; |
| 334 | } |
| 335 | |
| 336 | *transaction_id = (const char *)&msg->l3h[ptr[0].start]; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 337 | *endp = find_endpoint(cfg, (const char *)&msg->l3h[ptr[1].start]); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 338 | return *endp == NULL; |
| 339 | } |
| 340 | |
| 341 | static int verify_call_id(const struct mgcp_endpoint *endp, |
| 342 | const char *callid) |
| 343 | { |
| 344 | if (strcmp(endp->callid, callid) != 0) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 345 | LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 346 | ENDPOINT_NUMBER(endp), endp->callid, callid); |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int verify_ci(const struct mgcp_endpoint *endp, |
| 354 | const char *ci) |
| 355 | { |
| 356 | if (atoi(ci) != endp->ci) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 357 | LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 358 | ENDPOINT_NUMBER(endp), endp->ci, ci); |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 365 | static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 366 | { |
| 367 | struct mgcp_msg_ptr data_ptrs[6]; |
| 368 | int found, response; |
| 369 | const char *trans_id; |
| 370 | struct mgcp_endpoint *endp; |
| 371 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 372 | found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 373 | if (found != 0) |
| 374 | response = 500; |
| 375 | else |
| 376 | response = 200; |
| 377 | |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 378 | return create_response(response, "AUEP", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static int parse_conn_mode(const char* msg, int *conn_mode) |
| 382 | { |
| 383 | int ret = 0; |
| 384 | if (strcmp(msg, "recvonly") == 0) |
| 385 | *conn_mode = MGCP_CONN_RECV_ONLY; |
| 386 | else if (strcmp(msg, "sendrecv") == 0) |
| 387 | *conn_mode = MGCP_CONN_RECV_SEND; |
| 388 | else { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 389 | LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 390 | ret = -1; |
| 391 | } |
| 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 396 | static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 397 | { |
| 398 | struct mgcp_msg_ptr data_ptrs[6]; |
| 399 | int found, i, line_start; |
| 400 | const char *trans_id; |
| 401 | struct mgcp_endpoint *endp; |
| 402 | int error_code = 500; |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 403 | int port; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 404 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 405 | found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 406 | if (found != 0) |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 407 | return create_response(500, "CRCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 408 | |
| 409 | if (endp->ci != CI_UNUSED) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 410 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 411 | return create_response(500, "CRCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* parse CallID C: and LocalParameters L: */ |
| 415 | MSG_TOKENIZE_START |
| 416 | switch (msg->l3h[line_start]) { |
| 417 | case 'L': |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 418 | endp->local_options = talloc_strdup(cfg->endpoints, |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 419 | (const char *)&msg->l3h[line_start + 3]); |
| 420 | break; |
| 421 | case 'C': |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 422 | endp->callid = talloc_strdup(cfg->endpoints, |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 423 | (const char *)&msg->l3h[line_start + 3]); |
| 424 | break; |
| 425 | case 'M': |
| 426 | if (parse_conn_mode((const char *)&msg->l3h[line_start + 3], |
| 427 | &endp->conn_mode) != 0) { |
| 428 | error_code = 517; |
| 429 | goto error2; |
| 430 | } |
| 431 | break; |
| 432 | default: |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 433 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 434 | msg->l3h[line_start], msg->l3h[line_start], |
| 435 | ENDPOINT_NUMBER(endp)); |
| 436 | break; |
| 437 | } |
| 438 | MSG_TOKENIZE_END |
| 439 | |
| 440 | /* initialize */ |
| 441 | endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0; |
| 442 | |
| 443 | /* set to zero until we get the info */ |
| 444 | memset(&endp->remote, 0, sizeof(endp->remote)); |
| 445 | |
| 446 | /* bind to the port now */ |
Holger Hans Peter Freyther | 1b0ea97 | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 447 | port = rtp_calculate_port(ENDPOINT_NUMBER(endp), cfg->rtp_base_port); |
| 448 | if (cfg->early_bind) |
| 449 | endp->rtp_port = port; |
| 450 | else if (mgcp_bind_rtp_port(endp, port) != 0) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 451 | goto error2; |
| 452 | |
| 453 | /* assign a local call identifier or fail */ |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 454 | endp->ci = generate_call_id(cfg); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 455 | if (endp->ci == CI_UNUSED) |
| 456 | goto error2; |
| 457 | |
Holger Hans Peter Freyther | ef6bb25 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 458 | endp->bts_payload_type = cfg->audio_payload; |
| 459 | |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 460 | /* policy CB */ |
| 461 | if (cfg->policy_cb) { |
| 462 | switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, trans_id)) { |
| 463 | case MGCP_POLICY_REJECT: |
| 464 | LOGP(DMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n", |
| 465 | ENDPOINT_NUMBER(endp)); |
| 466 | mgcp_free_endp(endp); |
| 467 | return create_response(500, "CRCX", trans_id); |
| 468 | break; |
| 469 | case MGCP_POLICY_DEFER: |
| 470 | /* stop processing */ |
| 471 | return NULL; |
| 472 | break; |
| 473 | case MGCP_POLICY_CONT: |
| 474 | /* just continue */ |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 479 | LOGP(DMGCP, LOGL_NOTICE, "Creating endpoint on: 0x%x CI: %u port: %u\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 480 | ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 481 | if (cfg->change_cb) |
| 482 | cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, endp->rtp_port); |
Holger Hans Peter Freyther | 77f7afe | 2010-02-03 09:54:43 +0100 | [diff] [blame] | 483 | |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 484 | return create_response_with_sdp(endp, "CRCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 485 | error: |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 486 | LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 487 | hexdump(msg->l3h, msgb_l3len(msg)), |
| 488 | ENDPOINT_NUMBER(endp), line_start, i); |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 489 | return create_response(error_code, "CRCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 490 | |
| 491 | error2: |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 492 | LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 493 | return create_response(error_code, "CRCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 496 | static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 497 | { |
| 498 | struct mgcp_msg_ptr data_ptrs[6]; |
| 499 | int found, i, line_start; |
| 500 | const char *trans_id; |
| 501 | struct mgcp_endpoint *endp; |
| 502 | int error_code = 500; |
| 503 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 504 | found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 505 | if (found != 0) |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 506 | return create_response(error_code, "MDCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 507 | |
| 508 | if (endp->ci == CI_UNUSED) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 509 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 510 | return create_response(error_code, "MDCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | MSG_TOKENIZE_START |
| 514 | switch (msg->l3h[line_start]) { |
| 515 | case 'C': { |
| 516 | if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 517 | goto error3; |
| 518 | break; |
| 519 | } |
| 520 | case 'I': { |
| 521 | if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 522 | goto error3; |
| 523 | break; |
| 524 | } |
| 525 | case 'L': |
| 526 | /* skip */ |
| 527 | break; |
| 528 | case 'M': |
| 529 | if (parse_conn_mode((const char *)&msg->l3h[line_start + 3], |
| 530 | &endp->conn_mode) != 0) { |
| 531 | error_code = 517; |
| 532 | goto error3; |
| 533 | } |
| 534 | break; |
| 535 | case '\0': |
| 536 | /* SDP file begins */ |
| 537 | break; |
| 538 | case 'a': |
| 539 | case 'o': |
| 540 | case 's': |
| 541 | case 't': |
| 542 | case 'v': |
| 543 | /* skip these SDP attributes */ |
| 544 | break; |
| 545 | case 'm': { |
| 546 | int port; |
Holger Hans Peter Freyther | ef6bb25 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 547 | int payload; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 548 | const char *param = (const char *)&msg->l3h[line_start]; |
| 549 | |
Holger Hans Peter Freyther | ef6bb25 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 550 | if (sscanf(param, "m=audio %d RTP/AVP %d", &port, &payload) == 2) { |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 551 | endp->net_rtp = htons(port); |
| 552 | endp->net_rtcp = htons(port + 1); |
Holger Hans Peter Freyther | ef6bb25 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 553 | endp->net_payload_type = payload; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 554 | } |
| 555 | break; |
| 556 | } |
| 557 | case 'c': { |
| 558 | char ipv4[16]; |
| 559 | const char *param = (const char *)&msg->l3h[line_start]; |
| 560 | |
| 561 | if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) { |
| 562 | inet_aton(ipv4, &endp->remote); |
| 563 | } |
| 564 | break; |
| 565 | } |
| 566 | default: |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 567 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 568 | msg->l3h[line_start], msg->l3h[line_start], |
| 569 | ENDPOINT_NUMBER(endp)); |
| 570 | break; |
| 571 | } |
| 572 | MSG_TOKENIZE_END |
| 573 | |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 574 | /* policy CB */ |
| 575 | if (cfg->policy_cb) { |
| 576 | switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, trans_id)) { |
| 577 | case MGCP_POLICY_REJECT: |
| 578 | LOGP(DMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n", |
| 579 | ENDPOINT_NUMBER(endp)); |
| 580 | return create_response(500, "MDCX", trans_id); |
| 581 | break; |
| 582 | case MGCP_POLICY_DEFER: |
| 583 | /* stop processing */ |
| 584 | return NULL; |
| 585 | break; |
| 586 | case MGCP_POLICY_CONT: |
| 587 | /* just continue */ |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 592 | /* modify */ |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 593 | LOGP(DMGCP, LOGL_NOTICE, "Modified endpoint on: 0x%x Server: %s:%u\n", |
Holger Hans Peter Freyther | 63f2db2 | 2010-02-26 13:30:57 +0100 | [diff] [blame] | 594 | ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), ntohs(endp->net_rtp)); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 595 | if (cfg->change_cb) |
| 596 | cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, endp->rtp_port); |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 597 | return create_response_with_sdp(endp, "MDCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 598 | |
| 599 | error: |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 600 | LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 601 | hexdump(msg->l3h, msgb_l3len(msg)), |
| 602 | ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]); |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 603 | return create_response(error_code, "MDCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 604 | |
| 605 | error3: |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 606 | return create_response(error_code, "MDCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 607 | } |
| 608 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 609 | static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 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 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 617 | found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 618 | if (found != 0) |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 619 | return create_response(error_code, "DLCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 620 | |
| 621 | if (endp->ci == CI_UNUSED) { |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 622 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 623 | return create_response(error_code, "DLCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | MSG_TOKENIZE_START |
| 627 | switch (msg->l3h[line_start]) { |
| 628 | case 'C': { |
| 629 | if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 630 | goto error3; |
| 631 | break; |
| 632 | } |
| 633 | case 'I': { |
| 634 | if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 635 | goto error3; |
| 636 | break; |
| 637 | } |
| 638 | default: |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 639 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 640 | msg->l3h[line_start], msg->l3h[line_start], |
| 641 | ENDPOINT_NUMBER(endp)); |
| 642 | break; |
| 643 | } |
| 644 | MSG_TOKENIZE_END |
| 645 | |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 646 | /* policy CB */ |
| 647 | if (cfg->policy_cb) { |
| 648 | switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, trans_id)) { |
| 649 | case MGCP_POLICY_REJECT: |
| 650 | LOGP(DMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n", |
| 651 | ENDPOINT_NUMBER(endp)); |
| 652 | return create_response(500, "DLCX", trans_id); |
| 653 | break; |
| 654 | case MGCP_POLICY_DEFER: |
| 655 | /* stop processing */ |
| 656 | return NULL; |
| 657 | break; |
| 658 | case MGCP_POLICY_CONT: |
| 659 | /* just continue */ |
| 660 | break; |
| 661 | } |
| 662 | } |
| 663 | |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 664 | /* free the connection */ |
Holger Hans Peter Freyther | 154b955 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 665 | mgcp_free_endp(endp); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 666 | if (cfg->change_cb) |
| 667 | cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, endp->rtp_port); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 668 | |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 669 | return create_response(250, "DLCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 670 | |
| 671 | error: |
Holger Hans Peter Freyther | 2ada71d | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 672 | LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n", |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 673 | hexdump(msg->l3h, msgb_l3len(msg)), |
| 674 | ENDPOINT_NUMBER(endp), line_start, i); |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 675 | return create_response(error_code, "DLCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 676 | |
| 677 | error3: |
Holger Hans Peter Freyther | 8d188ed | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 678 | return create_response(error_code, "DLCX", trans_id); |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 681 | struct mgcp_config *mgcp_config_alloc(void) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 682 | { |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 683 | struct mgcp_config *cfg; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 684 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 685 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 686 | if (!cfg) { |
| 687 | LOGP(DMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 688 | return NULL; |
| 689 | } |
| 690 | |
| 691 | cfg->source_port = 2427; |
| 692 | cfg->source_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 693 | cfg->audio_name = talloc_strdup(cfg, "GSM-EFR/8000"); |
| 694 | cfg->audio_payload = 97; |
| 695 | cfg->rtp_base_port = RTP_PORT_DEFAULT; |
| 696 | |
| 697 | return cfg; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 700 | int mgcp_endpoints_allocate(struct mgcp_config *cfg) |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 701 | { |
| 702 | int i; |
| 703 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 704 | /* Initialize all endpoints */ |
| 705 | cfg->endpoints = _talloc_zero_array(cfg, |
| 706 | sizeof(struct mgcp_endpoint), |
| 707 | cfg->number_endpoints, "endpoints"); |
| 708 | if (!cfg->endpoints) |
| 709 | return -1; |
| 710 | |
| 711 | for (i = 0; i < cfg->number_endpoints; ++i) { |
| 712 | cfg->endpoints[i].local_rtp.fd = -1; |
| 713 | cfg->endpoints[i].local_rtcp.fd = -1; |
| 714 | cfg->endpoints[i].ci = CI_UNUSED; |
| 715 | cfg->endpoints[i].cfg = cfg; |
Holger Hans Peter Freyther | ef6bb25 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 716 | cfg->endpoints[i].net_payload_type = -1; |
| 717 | cfg->endpoints[i].bts_payload_type = -1; |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 720 | return 0; |
| 721 | } |
Holger Hans Peter Freyther | 154b955 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 722 | |
| 723 | void mgcp_free_endp(struct mgcp_endpoint *endp) |
| 724 | { |
| 725 | LOGP(DMGCP, LOGL_NOTICE, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp)); |
| 726 | endp->ci= CI_UNUSED; |
| 727 | |
| 728 | if (endp->callid) { |
| 729 | talloc_free(endp->callid); |
| 730 | endp->callid = NULL; |
| 731 | } |
| 732 | |
| 733 | if (endp->local_options) { |
| 734 | talloc_free(endp->local_options); |
| 735 | endp->callid = NULL; |
| 736 | } |
| 737 | |
| 738 | if (!endp->cfg->early_bind) { |
| 739 | bsc_unregister_fd(&endp->local_rtp); |
| 740 | bsc_unregister_fd(&endp->local_rtcp); |
| 741 | } |
| 742 | |
| 743 | endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0; |
| 744 | endp->net_payload_type = endp->bts_payload_type = -1; |
| 745 | } |