Holger Hans Peter Freyther | 26143c8 | 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 | 6f38c06 | 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 | 26143c8 | 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 | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 34 | #include <osmocore/msgb.h> |
| 35 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 36 | #include <openbsc/gsm_data.h> |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 37 | #include <osmocore/select.h> |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 38 | #include <openbsc/mgcp.h> |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 39 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | 26143c8 | 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 | 26143c8 | 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 | 6f38c06 | 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 | 26143c8 | 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 | 6f38c06 | 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 | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 101 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 102 | static int generate_call_id(struct mgcp_config *cfg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 103 | { |
| 104 | int i; |
| 105 | |
| 106 | /* use the call id */ |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 107 | ++cfg->last_call_id; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 108 | |
| 109 | /* handle wrap around */ |
Holger Hans Peter Freyther | 6f38c06 | 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 | 26143c8 | 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 | 6f38c06 | 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 | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 118 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 119 | return cfg->last_call_id; |
Holger Hans Peter Freyther | 26143c8 | 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 | 26143c8 | 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 | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 140 | static struct msgb *mgcp_msgb_alloc(void) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 141 | { |
Holger Hans Peter Freyther | ad4e339 | 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 | 94c9b64 | 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 | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 151 | const char *data) |
| 152 | { |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 153 | int len; |
Holger Hans Peter Freyther | ad4e339 | 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 | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 159 | |
| 160 | if (data) { |
Holger Hans Peter Freyther | ad4e339 | 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 | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 162 | } else { |
Holger Hans Peter Freyther | ad4e339 | 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 | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 164 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 165 | |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 166 | res->l2h = msgb_put(res, len); |
Holger Hans Peter Freyther | 40faed8 | 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 | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 168 | return res; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Holger Hans Peter Freyther | 60d02a3 | 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 | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 172 | { |
Holger Hans Peter Freyther | 94c9b64 | 2010-02-21 10:50:46 +0100 | [diff] [blame] | 173 | return mgcp_create_response_with_data(code, msg, trans, NULL); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Holger Hans Peter Freyther | 60d02a3 | 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 | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 178 | { |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 179 | const char *addr = endp->cfg->local_ip; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 180 | char sdp_record[4096]; |
| 181 | |
| 182 | if (!addr) |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 183 | addr = endp->cfg->source_addr; |
Holger Hans Peter Freyther | 26143c8 | 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 | 001dc61 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 192 | endp->bts_payload_type, endp->bts_payload_type, |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 193 | endp->cfg->audio_name); |
Holger Hans Peter Freyther | 94c9b64 | 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 | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 197 | /* |
| 198 | * handle incoming messages: |
| 199 | * - this can be a command (four letters, space, transaction id) |
| 200 | * - or a response (three numbers, space, transaction id) |
| 201 | */ |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 202 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 203 | { |
| 204 | int code; |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 205 | struct msgb *resp = NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 206 | |
| 207 | if (msg->len < 4) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 208 | LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len); |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 209 | return NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* attempt to treat it as a response */ |
| 213 | if (sscanf((const char *)&msg->data[0], "%3d %*s", &code) == 1) { |
Holger Hans Peter Freyther | 40faed8 | 2010-02-26 13:30:57 +0100 | [diff] [blame] | 214 | LOGP(DMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 215 | } else { |
| 216 | int i, handled = 0; |
| 217 | msg->l3h = &msg->l2h[4]; |
| 218 | for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) |
| 219 | if (strncmp(mgcp_requests[i].name, (const char *) &msg->data[0], 4) == 0) { |
| 220 | handled = 1; |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 221 | resp = mgcp_requests[i].handle_request(cfg, msg); |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 222 | break; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 223 | } |
| 224 | if (!handled) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 225 | LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->data[0]); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 229 | return resp; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* string tokenizer for the poor */ |
| 233 | static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length) |
| 234 | { |
| 235 | int i, found = 0; |
| 236 | |
| 237 | int whitespace = 1; |
| 238 | for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) { |
| 239 | /* if we have a space we found an end */ |
| 240 | if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') { |
| 241 | if (!whitespace) { |
| 242 | ++found; |
| 243 | whitespace = 1; |
| 244 | ptrs->length = i - ptrs->start - 1; |
| 245 | ++ptrs; |
| 246 | --ptrs_length; |
| 247 | } else { |
| 248 | /* skip any number of whitespace */ |
| 249 | } |
| 250 | |
| 251 | /* line end... stop */ |
| 252 | if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') |
| 253 | break; |
| 254 | } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') { |
| 255 | /* line end, be done */ |
| 256 | break; |
| 257 | } else if (whitespace) { |
| 258 | whitespace = 0; |
| 259 | ptrs->start = i; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (ptrs_length == 0) |
| 264 | return -1; |
| 265 | return found; |
| 266 | } |
| 267 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 268 | static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 269 | { |
| 270 | char *endptr = NULL; |
| 271 | unsigned int gw = INT_MAX; |
| 272 | |
| 273 | gw = strtoul(mgcp, &endptr, 16); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 274 | if (gw == 0 || gw >= cfg->number_endpoints || strcmp(endptr, "@mgw") != 0) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 275 | LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 276 | return NULL; |
| 277 | } |
| 278 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 279 | return &cfg->endpoints[gw]; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 282 | static int analyze_header(struct mgcp_config *cfg, struct msgb *msg, |
| 283 | struct mgcp_msg_ptr *ptr, int size, |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 284 | const char **transaction_id, struct mgcp_endpoint **endp) |
| 285 | { |
| 286 | int found; |
| 287 | |
Holger Hans Peter Freyther | 275c044 | 2010-02-26 13:32:55 +0100 | [diff] [blame] | 288 | *transaction_id = "000000"; |
| 289 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 290 | if (size < 3) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 291 | LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n"); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 292 | return -1; |
| 293 | } |
| 294 | |
| 295 | found = find_msg_pointers(msg, ptr, size); |
| 296 | |
Holger Hans Peter Freyther | 275c044 | 2010-02-26 13:32:55 +0100 | [diff] [blame] | 297 | if (found <= 3) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 298 | LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * replace the space with \0. the main method gurantess that |
| 304 | * we still have + 1 for null termination |
| 305 | */ |
| 306 | msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0'; |
| 307 | msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0'; |
| 308 | msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0'; |
| 309 | msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0'; |
| 310 | |
| 311 | if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0 |
| 312 | || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 313 | LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 314 | (const char *)&msg->l3h[ptr[3].start], |
| 315 | (const char *)&msg->l3h[ptr[2].start]); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | *transaction_id = (const char *)&msg->l3h[ptr[0].start]; |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 320 | *endp = find_endpoint(cfg, (const char *)&msg->l3h[ptr[1].start]); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 321 | return *endp == NULL; |
| 322 | } |
| 323 | |
| 324 | static int verify_call_id(const struct mgcp_endpoint *endp, |
| 325 | const char *callid) |
| 326 | { |
| 327 | if (strcmp(endp->callid, callid) != 0) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 328 | LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 329 | ENDPOINT_NUMBER(endp), endp->callid, callid); |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int verify_ci(const struct mgcp_endpoint *endp, |
| 337 | const char *ci) |
| 338 | { |
| 339 | if (atoi(ci) != endp->ci) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 340 | LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 341 | ENDPOINT_NUMBER(endp), endp->ci, ci); |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 348 | static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 349 | { |
| 350 | struct mgcp_msg_ptr data_ptrs[6]; |
| 351 | int found, response; |
| 352 | const char *trans_id; |
| 353 | struct mgcp_endpoint *endp; |
| 354 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 355 | found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 356 | if (found != 0) |
| 357 | response = 500; |
| 358 | else |
| 359 | response = 200; |
| 360 | |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 361 | return create_response(response, "AUEP", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static int parse_conn_mode(const char* msg, int *conn_mode) |
| 365 | { |
| 366 | int ret = 0; |
| 367 | if (strcmp(msg, "recvonly") == 0) |
| 368 | *conn_mode = MGCP_CONN_RECV_ONLY; |
| 369 | else if (strcmp(msg, "sendrecv") == 0) |
| 370 | *conn_mode = MGCP_CONN_RECV_SEND; |
| 371 | else { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 372 | LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 373 | ret = -1; |
| 374 | } |
| 375 | |
| 376 | return ret; |
| 377 | } |
| 378 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 379 | static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 380 | { |
| 381 | struct mgcp_msg_ptr data_ptrs[6]; |
| 382 | int found, i, line_start; |
| 383 | const char *trans_id; |
| 384 | struct mgcp_endpoint *endp; |
| 385 | int error_code = 500; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 386 | int port; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 387 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 388 | found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 389 | if (found != 0) |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 390 | return create_response(500, "CRCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 391 | |
| 392 | if (endp->ci != CI_UNUSED) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 393 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 394 | return create_response(500, "CRCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* parse CallID C: and LocalParameters L: */ |
| 398 | MSG_TOKENIZE_START |
| 399 | switch (msg->l3h[line_start]) { |
| 400 | case 'L': |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 401 | endp->local_options = talloc_strdup(cfg->endpoints, |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 402 | (const char *)&msg->l3h[line_start + 3]); |
| 403 | break; |
| 404 | case 'C': |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 405 | endp->callid = talloc_strdup(cfg->endpoints, |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 406 | (const char *)&msg->l3h[line_start + 3]); |
| 407 | break; |
| 408 | case 'M': |
| 409 | if (parse_conn_mode((const char *)&msg->l3h[line_start + 3], |
| 410 | &endp->conn_mode) != 0) { |
| 411 | error_code = 517; |
| 412 | goto error2; |
| 413 | } |
| 414 | break; |
| 415 | default: |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 416 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 417 | msg->l3h[line_start], msg->l3h[line_start], |
| 418 | ENDPOINT_NUMBER(endp)); |
| 419 | break; |
| 420 | } |
| 421 | MSG_TOKENIZE_END |
| 422 | |
| 423 | /* initialize */ |
| 424 | endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0; |
| 425 | |
| 426 | /* set to zero until we get the info */ |
| 427 | memset(&endp->remote, 0, sizeof(endp->remote)); |
| 428 | |
| 429 | /* bind to the port now */ |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 430 | port = rtp_calculate_port(ENDPOINT_NUMBER(endp), cfg->rtp_base_port); |
| 431 | if (cfg->early_bind) |
| 432 | endp->rtp_port = port; |
| 433 | else if (mgcp_bind_rtp_port(endp, port) != 0) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 434 | goto error2; |
| 435 | |
| 436 | /* assign a local call identifier or fail */ |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 437 | endp->ci = generate_call_id(cfg); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 438 | if (endp->ci == CI_UNUSED) |
| 439 | goto error2; |
| 440 | |
Holger Hans Peter Freyther | 001dc61 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 441 | endp->bts_payload_type = cfg->audio_payload; |
| 442 | |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 443 | /* policy CB */ |
| 444 | if (cfg->policy_cb) { |
| 445 | switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, trans_id)) { |
| 446 | case MGCP_POLICY_REJECT: |
| 447 | LOGP(DMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n", |
| 448 | ENDPOINT_NUMBER(endp)); |
| 449 | mgcp_free_endp(endp); |
| 450 | return create_response(500, "CRCX", trans_id); |
| 451 | break; |
| 452 | case MGCP_POLICY_DEFER: |
| 453 | /* stop processing */ |
| 454 | return NULL; |
| 455 | break; |
| 456 | case MGCP_POLICY_CONT: |
| 457 | /* just continue */ |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 462 | LOGP(DMGCP, LOGL_NOTICE, "Creating endpoint on: 0x%x CI: %u port: %u\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 463 | ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 464 | if (cfg->change_cb) |
| 465 | cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, endp->rtp_port); |
Holger Hans Peter Freyther | 25374bd | 2010-02-03 09:54:43 +0100 | [diff] [blame] | 466 | |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 467 | return create_response_with_sdp(endp, "CRCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 468 | error: |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 469 | LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 470 | hexdump(msg->l3h, msgb_l3len(msg)), |
| 471 | ENDPOINT_NUMBER(endp), line_start, i); |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 472 | return create_response(error_code, "CRCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 473 | |
| 474 | error2: |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 475 | LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 476 | return create_response(error_code, "CRCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 479 | static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 480 | { |
| 481 | struct mgcp_msg_ptr data_ptrs[6]; |
| 482 | int found, i, line_start; |
| 483 | const char *trans_id; |
| 484 | struct mgcp_endpoint *endp; |
| 485 | int error_code = 500; |
| 486 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 487 | found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 488 | if (found != 0) |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 489 | return create_response(error_code, "MDCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 490 | |
| 491 | if (endp->ci == CI_UNUSED) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 492 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 493 | return create_response(error_code, "MDCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | MSG_TOKENIZE_START |
| 497 | switch (msg->l3h[line_start]) { |
| 498 | case 'C': { |
| 499 | if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 500 | goto error3; |
| 501 | break; |
| 502 | } |
| 503 | case 'I': { |
| 504 | if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 505 | goto error3; |
| 506 | break; |
| 507 | } |
| 508 | case 'L': |
| 509 | /* skip */ |
| 510 | break; |
| 511 | case 'M': |
| 512 | if (parse_conn_mode((const char *)&msg->l3h[line_start + 3], |
| 513 | &endp->conn_mode) != 0) { |
| 514 | error_code = 517; |
| 515 | goto error3; |
| 516 | } |
| 517 | break; |
| 518 | case '\0': |
| 519 | /* SDP file begins */ |
| 520 | break; |
| 521 | case 'a': |
| 522 | case 'o': |
| 523 | case 's': |
| 524 | case 't': |
| 525 | case 'v': |
| 526 | /* skip these SDP attributes */ |
| 527 | break; |
| 528 | case 'm': { |
| 529 | int port; |
Holger Hans Peter Freyther | 001dc61 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 530 | int payload; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 531 | const char *param = (const char *)&msg->l3h[line_start]; |
| 532 | |
Holger Hans Peter Freyther | 001dc61 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 533 | if (sscanf(param, "m=audio %d RTP/AVP %d", &port, &payload) == 2) { |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 534 | endp->net_rtp = htons(port); |
| 535 | endp->net_rtcp = htons(port + 1); |
Holger Hans Peter Freyther | 001dc61 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 536 | endp->net_payload_type = payload; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 537 | } |
| 538 | break; |
| 539 | } |
| 540 | case 'c': { |
| 541 | char ipv4[16]; |
| 542 | const char *param = (const char *)&msg->l3h[line_start]; |
| 543 | |
| 544 | if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) { |
| 545 | inet_aton(ipv4, &endp->remote); |
| 546 | } |
| 547 | break; |
| 548 | } |
| 549 | default: |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 550 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 551 | msg->l3h[line_start], msg->l3h[line_start], |
| 552 | ENDPOINT_NUMBER(endp)); |
| 553 | break; |
| 554 | } |
| 555 | MSG_TOKENIZE_END |
| 556 | |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 557 | /* policy CB */ |
| 558 | if (cfg->policy_cb) { |
| 559 | switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, trans_id)) { |
| 560 | case MGCP_POLICY_REJECT: |
| 561 | LOGP(DMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n", |
| 562 | ENDPOINT_NUMBER(endp)); |
| 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 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 575 | /* modify */ |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 576 | LOGP(DMGCP, LOGL_NOTICE, "Modified endpoint on: 0x%x Server: %s:%u\n", |
Holger Hans Peter Freyther | 40faed8 | 2010-02-26 13:30:57 +0100 | [diff] [blame] | 577 | ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), ntohs(endp->net_rtp)); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 578 | if (cfg->change_cb) |
| 579 | cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, endp->rtp_port); |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 580 | return create_response_with_sdp(endp, "MDCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 581 | |
| 582 | error: |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 583 | LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 584 | hexdump(msg->l3h, msgb_l3len(msg)), |
| 585 | ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]); |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 586 | return create_response(error_code, "MDCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 587 | |
| 588 | error3: |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 589 | return create_response(error_code, "MDCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 592 | static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 593 | { |
| 594 | struct mgcp_msg_ptr data_ptrs[6]; |
| 595 | int found, i, line_start; |
| 596 | const char *trans_id; |
| 597 | struct mgcp_endpoint *endp; |
| 598 | int error_code = 500; |
| 599 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 600 | found = analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 601 | if (found != 0) |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 602 | return create_response(error_code, "DLCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 603 | |
| 604 | if (endp->ci == CI_UNUSED) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 605 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 606 | return create_response(error_code, "DLCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | MSG_TOKENIZE_START |
| 610 | switch (msg->l3h[line_start]) { |
| 611 | case 'C': { |
| 612 | if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 613 | goto error3; |
| 614 | break; |
| 615 | } |
| 616 | case 'I': { |
| 617 | if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0) |
| 618 | goto error3; |
| 619 | break; |
| 620 | } |
| 621 | default: |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 622 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 623 | msg->l3h[line_start], msg->l3h[line_start], |
| 624 | ENDPOINT_NUMBER(endp)); |
| 625 | break; |
| 626 | } |
| 627 | MSG_TOKENIZE_END |
| 628 | |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 629 | /* policy CB */ |
| 630 | if (cfg->policy_cb) { |
| 631 | switch (cfg->policy_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, trans_id)) { |
| 632 | case MGCP_POLICY_REJECT: |
| 633 | LOGP(DMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n", |
| 634 | ENDPOINT_NUMBER(endp)); |
| 635 | return create_response(500, "DLCX", trans_id); |
| 636 | break; |
| 637 | case MGCP_POLICY_DEFER: |
| 638 | /* stop processing */ |
| 639 | return NULL; |
| 640 | break; |
| 641 | case MGCP_POLICY_CONT: |
| 642 | /* just continue */ |
| 643 | break; |
| 644 | } |
| 645 | } |
| 646 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 647 | /* free the connection */ |
Holger Hans Peter Freyther | 64dcbe2 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 648 | mgcp_free_endp(endp); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 649 | if (cfg->change_cb) |
| 650 | cfg->change_cb(cfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, endp->rtp_port); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 651 | |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 652 | return create_response(250, "DLCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 653 | |
| 654 | error: |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 655 | LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 656 | hexdump(msg->l3h, msgb_l3len(msg)), |
| 657 | ENDPOINT_NUMBER(endp), line_start, i); |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 658 | return create_response(error_code, "DLCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 659 | |
| 660 | error3: |
Holger Hans Peter Freyther | 60d02a3 | 2010-02-21 01:44:08 +0100 | [diff] [blame] | 661 | return create_response(error_code, "DLCX", trans_id); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 662 | } |
| 663 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 664 | struct mgcp_config *mgcp_config_alloc(void) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 665 | { |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 666 | struct mgcp_config *cfg; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 667 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 668 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 669 | if (!cfg) { |
| 670 | LOGP(DMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 671 | return NULL; |
| 672 | } |
| 673 | |
| 674 | cfg->source_port = 2427; |
| 675 | cfg->source_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 676 | cfg->audio_name = talloc_strdup(cfg, "GSM-EFR/8000"); |
| 677 | cfg->audio_payload = 97; |
| 678 | cfg->rtp_base_port = RTP_PORT_DEFAULT; |
| 679 | |
| 680 | return cfg; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 683 | int mgcp_endpoints_allocate(struct mgcp_config *cfg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 684 | { |
| 685 | int i; |
| 686 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 687 | /* Initialize all endpoints */ |
| 688 | cfg->endpoints = _talloc_zero_array(cfg, |
| 689 | sizeof(struct mgcp_endpoint), |
| 690 | cfg->number_endpoints, "endpoints"); |
| 691 | if (!cfg->endpoints) |
| 692 | return -1; |
| 693 | |
| 694 | for (i = 0; i < cfg->number_endpoints; ++i) { |
| 695 | cfg->endpoints[i].local_rtp.fd = -1; |
| 696 | cfg->endpoints[i].local_rtcp.fd = -1; |
| 697 | cfg->endpoints[i].ci = CI_UNUSED; |
| 698 | cfg->endpoints[i].cfg = cfg; |
Holger Hans Peter Freyther | 001dc61 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 699 | cfg->endpoints[i].net_payload_type = -1; |
| 700 | cfg->endpoints[i].bts_payload_type = -1; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 703 | return 0; |
| 704 | } |
Holger Hans Peter Freyther | 64dcbe2 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 705 | |
| 706 | void mgcp_free_endp(struct mgcp_endpoint *endp) |
| 707 | { |
| 708 | LOGP(DMGCP, LOGL_NOTICE, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp)); |
| 709 | endp->ci= CI_UNUSED; |
| 710 | |
| 711 | if (endp->callid) { |
| 712 | talloc_free(endp->callid); |
| 713 | endp->callid = NULL; |
| 714 | } |
| 715 | |
| 716 | if (endp->local_options) { |
| 717 | talloc_free(endp->local_options); |
Holger Hans Peter Freyther | a5ec926 | 2010-03-01 18:52:04 +0100 | [diff] [blame] | 718 | endp->local_options = NULL; |
Holger Hans Peter Freyther | 64dcbe2 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | if (!endp->cfg->early_bind) { |
| 722 | bsc_unregister_fd(&endp->local_rtp); |
| 723 | bsc_unregister_fd(&endp->local_rtcp); |
| 724 | } |
| 725 | |
| 726 | endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0; |
| 727 | endp->net_payload_type = endp->bts_payload_type = -1; |
| 728 | } |