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