Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 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 Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <ctype.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <time.h> |
| 28 | #include <limits.h> |
| 29 | #include <unistd.h> |
| 30 | #include <errno.h> |
| 31 | |
| 32 | #include <osmocom/core/msgb.h> |
| 33 | #include <osmocom/core/talloc.h> |
| 34 | #include <osmocom/core/select.h> |
| 35 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 36 | #include <osmocom/mgcp/mgcp.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 37 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 38 | #include <osmocom/mgcp/mgcp_internal.h> |
| 39 | #include <osmocom/mgcp/mgcp_stat.h> |
| 40 | #include <osmocom/mgcp/mgcp_msg.h> |
| 41 | #include <osmocom/mgcp/mgcp_ep.h> |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 42 | #include <osmocom/mgcp/mgcp_sdp.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 43 | |
| 44 | struct mgcp_request { |
| 45 | char *name; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 46 | struct msgb *(*handle_request) (struct mgcp_parse_data * data); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 47 | char *debug_name; |
| 48 | }; |
| 49 | |
| 50 | #define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \ |
| 51 | { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME }, |
| 52 | |
| 53 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data); |
| 54 | static struct msgb *handle_create_con(struct mgcp_parse_data *data); |
| 55 | static struct msgb *handle_delete_con(struct mgcp_parse_data *data); |
| 56 | static struct msgb *handle_modify_con(struct mgcp_parse_data *data); |
| 57 | static struct msgb *handle_rsip(struct mgcp_parse_data *data); |
| 58 | static struct msgb *handle_noti_req(struct mgcp_parse_data *data); |
| 59 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 60 | /* Initalize transcoder */ |
| 61 | static int setup_rtp_processing(struct mgcp_endpoint *endp, |
| 62 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 63 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 64 | struct mgcp_config *cfg = endp->cfg; |
| 65 | struct mgcp_conn_rtp *conn_src = NULL; |
| 66 | struct mgcp_conn_rtp *conn_dst = conn; |
| 67 | struct mgcp_conn *_conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 68 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 69 | if (conn->type != MGCP_RTP_DEFAULT) { |
| 70 | LOGP(DLMGCP, LOGL_NOTICE, |
| 71 | "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n", |
| 72 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 76 | if (conn->conn->mode == MGCP_CONN_LOOPBACK) { |
| 77 | LOGP(DLMGCP, LOGL_NOTICE, |
| 78 | "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n", |
| 79 | ENDPOINT_NUMBER(endp)); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /* Find the "sister" connection */ |
| 84 | llist_for_each_entry(_conn, &endp->conns, entry) { |
| 85 | if (_conn->id != conn->conn->id) { |
| 86 | conn_src = &_conn->u.rtp; |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return cfg->setup_rtp_processing_cb(endp, &conn_dst->end, |
| 92 | &conn_src->end); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 95 | /* array of function pointers for handling various |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 96 | * messages. In the future this might be binary sorted |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 97 | * for performance reasons. */ |
| 98 | static const struct mgcp_request mgcp_requests[] = { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 99 | MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint") |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 100 | MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection") |
| 101 | MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection") |
| 102 | MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection") |
| 103 | MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest") |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 104 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 105 | /* SPEC extension */ |
| 106 | MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress") |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 109 | /* Helper function to allocate some memory for responses and retransmissions */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 110 | static struct msgb *mgcp_msgb_alloc(void) |
| 111 | { |
| 112 | struct msgb *msg; |
| 113 | msg = msgb_alloc_headroom(4096, 128, "MGCP msg"); |
| 114 | if (!msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 115 | LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 116 | |
| 117 | return msg; |
| 118 | } |
| 119 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 120 | /* Helper function for do_retransmission() and create_resp() */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 121 | static struct msgb *do_retransmission(const struct mgcp_endpoint *endp) |
| 122 | { |
| 123 | struct msgb *msg = mgcp_msgb_alloc(); |
| 124 | if (!msg) |
| 125 | return NULL; |
| 126 | |
| 127 | msg->l2h = msgb_put(msg, strlen(endp->last_response)); |
| 128 | memcpy(msg->l2h, endp->last_response, msgb_l2len(msg)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 129 | mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 130 | return msg; |
| 131 | } |
| 132 | |
| 133 | static struct msgb *create_resp(struct mgcp_endpoint *endp, int code, |
| 134 | const char *txt, const char *msg, |
| 135 | const char *trans, const char *param, |
| 136 | const char *sdp) |
| 137 | { |
| 138 | int len; |
| 139 | struct msgb *res; |
| 140 | |
| 141 | res = mgcp_msgb_alloc(); |
| 142 | if (!res) |
| 143 | return NULL; |
| 144 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 145 | len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s", |
| 146 | code, trans, txt, param ? param : "", sdp ? sdp : ""); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 147 | if (len < 0) { |
| 148 | LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n"); |
| 149 | msgb_free(res); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | res->l2h = msgb_put(res, len); |
| 154 | LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 155 | mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * Remember the last transmission per endpoint. |
| 159 | */ |
| 160 | if (endp) { |
| 161 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 162 | talloc_free(endp->last_response); |
| 163 | talloc_free(endp->last_trans); |
| 164 | endp->last_trans = talloc_strdup(tcfg->endpoints, trans); |
| 165 | endp->last_response = talloc_strndup(tcfg->endpoints, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 166 | (const char *)res->l2h, |
| 167 | msgb_l2len(res)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return res; |
| 171 | } |
| 172 | |
| 173 | static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 174 | int code, const char *msg, |
| 175 | const char *trans, |
| 176 | const char *param) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 177 | { |
| 178 | return create_resp(endp, code, " OK", msg, trans, param, NULL); |
| 179 | } |
| 180 | |
| 181 | static struct msgb *create_ok_response(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 182 | int code, const char *msg, |
| 183 | const char *trans) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 184 | { |
| 185 | return create_ok_resp_with_param(endp, code, msg, trans, NULL); |
| 186 | } |
| 187 | |
| 188 | static struct msgb *create_err_response(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 189 | int code, const char *msg, |
| 190 | const char *trans) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 191 | { |
| 192 | return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL); |
| 193 | } |
| 194 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 195 | /* Format MGCP response string (with SDP attached) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 196 | static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 197 | struct mgcp_conn_rtp *conn, |
| 198 | const char *msg, |
| 199 | const char *trans_id) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 200 | { |
| 201 | const char *addr = endp->cfg->local_ip; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 202 | struct msgb *sdp; |
| 203 | int rc; |
| 204 | struct msgb *result; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 205 | char osmux_extension[strlen("\nX-Osmux: 255") + 1]; |
| 206 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 207 | sdp = msgb_alloc_headroom(4096, 128, "sdp record"); |
| 208 | if (!sdp) |
| 209 | return NULL; |
| 210 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 211 | if (!addr) |
| 212 | addr = mgcp_net_src_addr(endp); |
| 213 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 214 | if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) { |
| 215 | sprintf(osmux_extension, "\nX-Osmux: %u", conn->osmux.cid); |
| 216 | conn->osmux.state = OSMUX_STATE_ACTIVATING; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 217 | } else { |
| 218 | osmux_extension[0] = '\0'; |
| 219 | } |
| 220 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 221 | rc = msgb_printf(sdp, "I: %u%s\n\n", conn->conn->id, osmux_extension); |
| 222 | if (rc < 0) |
| 223 | goto error; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 224 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 225 | rc = mgcp_write_response_sdp(endp, conn, sdp, addr); |
| 226 | if (rc < 0) |
| 227 | goto error; |
| 228 | result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data); |
| 229 | msgb_free(sdp); |
| 230 | return result; |
| 231 | error: |
| 232 | msgb_free(sdp); |
| 233 | return NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 236 | /* Send out dummy packet to keep the connection open, if the connection is an |
| 237 | * osmux connection, send the dummy packet via OSMUX */ |
| 238 | static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 239 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 240 | if (conn->osmux.state != OSMUX_STATE_DISABLED) |
| 241 | osmux_send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 242 | else |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 243 | mgcp_send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 246 | /* handle incoming messages: |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 247 | * - this can be a command (four letters, space, transaction id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 248 | * - or a response (three numbers, space, transaction id) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 249 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) |
| 250 | { |
| 251 | struct mgcp_parse_data pdata; |
| 252 | int i, code, handled = 0; |
| 253 | struct msgb *resp = NULL; |
| 254 | char *data; |
| 255 | |
| 256 | if (msgb_l2len(msg) < 4) { |
| 257 | LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len); |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | if (mgcp_msg_terminate_nul(msg)) |
| 262 | return NULL; |
| 263 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 264 | mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 265 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 266 | /* attempt to treat it as a response */ |
| 267 | if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 268 | LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | msg->l3h = &msg->l2h[4]; |
| 273 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 274 | /* |
| 275 | * Check for a duplicate message and respond. |
| 276 | */ |
| 277 | memset(&pdata, 0, sizeof(pdata)); |
| 278 | pdata.cfg = cfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 279 | data = mgcp_strline((char *)msg->l3h, &pdata.save); |
| 280 | pdata.found = mgcp_parse_header(&pdata, data); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 281 | if (pdata.endp && pdata.trans |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 282 | && pdata.endp->last_trans |
| 283 | && strcmp(pdata.endp->last_trans, pdata.trans) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 284 | return do_retransmission(pdata.endp); |
| 285 | } |
| 286 | |
| 287 | for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 288 | if (strncmp |
| 289 | (mgcp_requests[i].name, (const char *)&msg->l2h[0], |
| 290 | 4) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 291 | handled = 1; |
| 292 | resp = mgcp_requests[i].handle_request(&pdata); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (!handled) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 298 | LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", |
| 299 | &msg->l2h[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 300 | |
| 301 | return resp; |
| 302 | } |
| 303 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 304 | /* AUEP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 305 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p) |
| 306 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 307 | LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n"); |
| 308 | |
| 309 | if (p->found != 0) { |
| 310 | LOGP(DLMGCP, LOGL_ERROR, |
| 311 | "AUEP: failed to find the endpoint.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 312 | return create_err_response(NULL, 500, "AUEP", p->trans); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 313 | } else |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 314 | return create_ok_response(p->endp, 200, "AUEP", p->trans); |
| 315 | } |
| 316 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 317 | /* Try to find a free port by attemting to bind on it. Also handle the |
| 318 | * counter that points on the next free port. Since we have a pointer |
| 319 | * to the next free port, binding should work on the first attemt, |
| 320 | * neverless, try at least the next 200 ports before giving up */ |
| 321 | static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 322 | { |
| 323 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 324 | struct mgcp_rtp_end *end; |
| 325 | struct mgcp_port_range *range; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 326 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 327 | OSMO_ASSERT(conn); |
| 328 | end = &conn->end; |
| 329 | OSMO_ASSERT(end); |
| 330 | |
| 331 | range = &endp->cfg->net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 332 | |
| 333 | /* attempt to find a port */ |
| 334 | for (i = 0; i < 200; ++i) { |
| 335 | int rc; |
| 336 | |
| 337 | if (range->last_port >= range->range_end) |
| 338 | range->last_port = range->range_start; |
| 339 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 340 | rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 341 | |
| 342 | range->last_port += 2; |
| 343 | if (rc == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | } |
| 348 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 349 | LOGP(DLMGCP, LOGL_ERROR, |
| 350 | "Allocating a RTP/RTCP port failed 200 times 0x%x.\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 351 | ENDPOINT_NUMBER(endp)); |
| 352 | return -1; |
| 353 | } |
| 354 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 355 | /* Set the LCO from a string (see RFC 3435). |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 356 | * The string is stored in the 'string' field. A NULL string is handled excatlyy |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 357 | * like an empty string, the 'string' field is never NULL after this function |
| 358 | * has been called. */ |
| 359 | static void set_local_cx_options(void *ctx, struct mgcp_lco *lco, |
| 360 | const char *options) |
| 361 | { |
| 362 | char *p_opt, *a_opt; |
| 363 | char codec[9]; |
| 364 | |
| 365 | talloc_free(lco->string); |
| 366 | talloc_free(lco->codec); |
| 367 | lco->codec = NULL; |
| 368 | lco->pkt_period_min = lco->pkt_period_max = 0; |
| 369 | lco->string = talloc_strdup(ctx, options ? options : ""); |
| 370 | |
| 371 | p_opt = strstr(lco->string, "p:"); |
| 372 | if (p_opt && sscanf(p_opt, "p:%d-%d", |
| 373 | &lco->pkt_period_min, &lco->pkt_period_max) == 1) |
| 374 | lco->pkt_period_max = lco->pkt_period_min; |
| 375 | |
| 376 | a_opt = strstr(lco->string, "a:"); |
| 377 | if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1) |
| 378 | lco->codec = talloc_strdup(ctx, codec); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 379 | |
| 380 | LOGP(DLMGCP, LOGL_DEBUG, |
| 381 | "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n", |
| 382 | lco->pkt_period_max, lco->codec); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change, |
| 386 | struct mgcp_rtp_end *rtp) |
| 387 | { |
| 388 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 389 | |
| 390 | int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc; |
| 391 | |
| 392 | rtp->force_aligned_timing = tcfg->force_aligned_timing; |
| 393 | rtp->force_constant_ssrc = patch_ssrc ? 1 : 0; |
| 394 | |
| 395 | LOGP(DLMGCP, LOGL_DEBUG, |
| 396 | "Configuring RTP endpoint: local port %d%s%s\n", |
| 397 | ntohs(rtp->rtp_port), |
| 398 | rtp->force_aligned_timing ? ", force constant timing" : "", |
| 399 | rtp->force_constant_ssrc ? ", force constant ssrc" : ""); |
| 400 | } |
| 401 | |
| 402 | uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp, |
| 403 | struct mgcp_rtp_end *rtp) |
| 404 | { |
| 405 | int f = 0; |
| 406 | |
| 407 | /* Get the number of frames per channel and packet */ |
| 408 | if (rtp->frames_per_packet) |
| 409 | f = rtp->frames_per_packet; |
| 410 | else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) { |
| 411 | int den = 1000 * rtp->codec.frame_duration_num; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 412 | f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den + |
| 413 | den / 2) |
| 414 | / den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 417 | return rtp->codec.rate * f * rtp->codec.frame_duration_num / |
| 418 | rtp->codec.frame_duration_den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line) |
| 422 | { |
| 423 | if (!endp->cfg->osmux_init) { |
| 424 | if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) { |
| 425 | LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n"); |
| 426 | return -1; |
| 427 | } |
| 428 | LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n"); |
| 429 | } |
| 430 | |
| 431 | return mgcp_parse_osmux_cid(line); |
| 432 | } |
| 433 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 434 | /* CRCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 435 | static struct msgb *handle_create_con(struct mgcp_parse_data *p) |
| 436 | { |
| 437 | struct mgcp_trunk_config *tcfg; |
| 438 | struct mgcp_endpoint *endp = p->endp; |
| 439 | int error_code = 400; |
| 440 | |
| 441 | const char *local_options = NULL; |
| 442 | const char *callid = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 443 | const char *ci = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 444 | const char *mode = NULL; |
| 445 | char *line; |
| 446 | int have_sdp = 0, osmux_cid = -1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 447 | struct mgcp_conn_rtp *conn = NULL; |
| 448 | uint32_t conn_id; |
| 449 | char conn_name[512]; |
| 450 | |
| 451 | LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 452 | |
| 453 | if (p->found != 0) |
| 454 | return create_err_response(NULL, 510, "CRCX", p->trans); |
| 455 | |
| 456 | /* parse CallID C: and LocalParameters L: */ |
| 457 | for_each_line(line, p->save) { |
| 458 | if (!mgcp_check_param(endp, line)) |
| 459 | continue; |
| 460 | |
| 461 | switch (line[0]) { |
| 462 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 463 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 464 | break; |
| 465 | case 'C': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 466 | callid = (const char *)line + 3; |
| 467 | break; |
| 468 | case 'I': |
| 469 | ci = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 470 | break; |
| 471 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 472 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 473 | break; |
| 474 | case 'X': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 475 | /* If osmoux is disabled, just skip setting it up */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 476 | if (!p->endp->cfg->osmux) |
| 477 | break; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 478 | if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == |
| 479 | 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 480 | osmux_cid = mgcp_osmux_setup(endp, line); |
| 481 | break; |
| 482 | case '\0': |
| 483 | have_sdp = 1; |
| 484 | goto mgcp_header_done; |
| 485 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 486 | LOGP(DLMGCP, LOGL_NOTICE, |
| 487 | "CRCX: endpoint:%x unhandled option: '%c'/%d\n", |
| 488 | ENDPOINT_NUMBER(endp), *line, *line); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 489 | break; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | mgcp_header_done: |
| 494 | tcfg = p->endp->tcfg; |
| 495 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 496 | /* Check parameters */ |
| 497 | if (!callid) { |
| 498 | LOGP(DLMGCP, LOGL_ERROR, |
| 499 | "CRCX: endpoint:%x insufficient parameters, missing callid\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 500 | ENDPOINT_NUMBER(endp)); |
| 501 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 502 | } |
| 503 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 504 | if (!mode) { |
| 505 | LOGP(DLMGCP, LOGL_ERROR, |
| 506 | "CRCX: endpoint:%x insufficient parameters, missing mode\n", |
| 507 | ENDPOINT_NUMBER(endp)); |
| 508 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 509 | } |
| 510 | |
| 511 | if (!ci) { |
| 512 | LOGP(DLMGCP, LOGL_ERROR, |
| 513 | "CRCX: endpoint:%x insufficient parameters, missing connection id\n", |
| 514 | ENDPOINT_NUMBER(endp)); |
| 515 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 516 | } |
| 517 | |
| 518 | /* Check if we are able to accept the creation of another connection */ |
| 519 | if (llist_count(&endp->conns) >= endp->type->max_conns) { |
| 520 | LOGP(DLMGCP, LOGL_ERROR, |
| 521 | "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n", |
| 522 | endp->type->max_conns, ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 523 | if (tcfg->force_realloc) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 524 | /* There is no more room for a connection, make some |
| 525 | * room by blindly tossing the oldest of the two two |
| 526 | * connections */ |
| 527 | mgcp_conn_free_oldest(endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 528 | } else { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 529 | /* There is no more room for a connection, leave |
| 530 | * everything as it is and return with an error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 531 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 532 | } |
| 533 | } |
| 534 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 535 | /* Check if this endpoint already serves a call, if so, check if the |
| 536 | * callids match up so that we are sure that this is our call */ |
| 537 | if (endp->callid && mgcp_verify_call_id(endp, callid)) { |
| 538 | LOGP(DLMGCP, LOGL_ERROR, |
| 539 | "CRCX: endpoint:%x allready seized by other call (%s)\n", |
| 540 | ENDPOINT_NUMBER(endp), endp->callid); |
| 541 | if (tcfg->force_realloc) |
| 542 | /* This is not our call, toss everything by releasing |
| 543 | * the entire endpoint. (rude!) */ |
| 544 | mgcp_release_endp(endp); |
| 545 | else { |
| 546 | /* This is not our call, leave everything as it is and |
| 547 | * return with an error. */ |
| 548 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /* Set the callid, creation of another connection will only be possible |
| 553 | * when the callid matches up. (Connections are distinuished by their |
| 554 | * connection ids) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 555 | endp->callid = talloc_strdup(tcfg->endpoints, callid); |
| 556 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 557 | /* Extract audio codec information */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 558 | set_local_cx_options(endp->tcfg->endpoints, &endp->local_options, |
| 559 | local_options); |
| 560 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 561 | if (mgcp_parse_ci(&conn_id, ci)) { |
| 562 | LOGP(DLMGCP, LOGL_ERROR, |
| 563 | "CRCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n", |
| 564 | ENDPOINT_NUMBER(endp)); |
| 565 | return create_err_response(endp, 400, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 568 | /* Only accept another connection when the connection ID is different. */ |
| 569 | if (mgcp_conn_get_rtp(endp, conn_id)) { |
| 570 | LOGP(DLMGCP, LOGL_ERROR, |
| 571 | "CRCX: endpoint:%x there is already a connection with id %u present!\n", |
| 572 | conn_id, ENDPOINT_NUMBER(endp)); |
| 573 | if (tcfg->force_realloc) { |
| 574 | /* Ignore the existing connection by just freeing it */ |
| 575 | mgcp_conn_free(endp, conn_id); |
| 576 | } else { |
| 577 | /* There is already a connection with that ID present, |
| 578 | * leave everything as it is and return with an error. */ |
| 579 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 580 | } |
| 581 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 582 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 583 | snprintf(conn_name, sizeof(conn_name), "%s-%u", callid, conn_id); |
| 584 | mgcp_conn_alloc(NULL, endp, conn_id, MGCP_CONN_TYPE_RTP, |
| 585 | conn_name); |
| 586 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 587 | if (!conn) { |
| 588 | LOGP(DLMGCP, LOGL_ERROR, |
| 589 | "CRCX: endpoint:%x unable to allocate RTP connection\n", |
| 590 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 591 | goto error2; |
| 592 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 596 | error_code = 517; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 597 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | mgcp_rtp_end_config(endp, 0, &conn->end); |
| 601 | |
| 602 | if (allocate_port(endp, conn) != 0) { |
| 603 | goto error2; |
| 604 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 605 | |
| 606 | /* Annotate Osmux circuit ID and set it to negotiating state until this |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 607 | * is fully set up from the dummy load. */ |
| 608 | conn->osmux.state = OSMUX_STATE_DISABLED; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 609 | if (osmux_cid >= 0) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 610 | conn->osmux.cid = osmux_cid; |
| 611 | conn->osmux.state = OSMUX_STATE_NEGOTIATING; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 612 | } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) { |
| 613 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 614 | "CRCX: endpoint:%x osmux only and no osmux offered\n", |
| 615 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 616 | goto error2; |
| 617 | } |
| 618 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 619 | /* set up RTP media parameters */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 620 | if (have_sdp) |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 621 | mgcp_parse_sdp_data(endp, conn, p); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 622 | else if (endp->local_options.codec) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 623 | mgcp_set_audio_info(p->cfg, &conn->end.codec, |
| 624 | PTYPE_UNDEFINED, endp->local_options.codec); |
| 625 | conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints, |
| 626 | tcfg->audio_fmtp_extra); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 627 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 628 | if (p->cfg->force_ptime) { |
| 629 | conn->end.packet_duration_ms = p->cfg->force_ptime; |
| 630 | conn->end.force_output_ptime = 1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 633 | if (setup_rtp_processing(endp, conn) != 0) { |
| 634 | LOGP(DLMGCP, LOGL_ERROR, |
| 635 | "CRCX: endpoint:%x could not start RTP processing!\n", |
| 636 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 637 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 638 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 639 | |
| 640 | /* policy CB */ |
| 641 | if (p->cfg->policy_cb) { |
| 642 | int rc; |
| 643 | rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 644 | MGCP_ENDP_CRCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 645 | switch (rc) { |
| 646 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 647 | LOGP(DLMGCP, LOGL_NOTICE, |
| 648 | "CRCX: endpoint:%x CRCX rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 649 | ENDPOINT_NUMBER(endp)); |
| 650 | mgcp_release_endp(endp); |
| 651 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 652 | break; |
| 653 | case MGCP_POLICY_DEFER: |
| 654 | /* stop processing */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 655 | return NULL; |
| 656 | break; |
| 657 | case MGCP_POLICY_CONT: |
| 658 | /* just continue */ |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 663 | LOGP(DLMGCP, LOGL_DEBUG, |
| 664 | "CRCX: endpoint:%x Creating connection: CI: %u port: %u\n", |
| 665 | ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 666 | if (p->cfg->change_cb) |
| 667 | p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX); |
| 668 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 669 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY |
| 670 | && tcfg->keepalive_interval != 0) { |
| 671 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 674 | LOGP(DLMGCP, LOGL_NOTICE, |
| 675 | "CRCX: endpoint:%x connection successfully created\n", |
| 676 | ENDPOINT_NUMBER(endp)); |
| 677 | return create_response_with_sdp(endp, conn, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 678 | error2: |
| 679 | mgcp_release_endp(endp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 680 | LOGP(DLMGCP, LOGL_NOTICE, |
| 681 | "CRCX: endpoint:%x unable to create connection resource error\n", |
| 682 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 683 | return create_err_response(endp, error_code, "CRCX", p->trans); |
| 684 | } |
| 685 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 686 | /* MDCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 687 | static struct msgb *handle_modify_con(struct mgcp_parse_data *p) |
| 688 | { |
| 689 | struct mgcp_endpoint *endp = p->endp; |
| 690 | int error_code = 500; |
| 691 | int silent = 0; |
| 692 | int have_sdp = 0; |
| 693 | char *line; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 694 | const char *ci = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 695 | const char *local_options = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 696 | const char *mode = NULL; |
| 697 | struct mgcp_conn_rtp *conn = NULL; |
| 698 | uint32_t conn_id; |
| 699 | |
| 700 | LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 701 | |
| 702 | if (p->found != 0) |
| 703 | return create_err_response(NULL, 510, "MDCX", p->trans); |
| 704 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 705 | if (llist_count(&endp->conns) <= 0) { |
| 706 | LOGP(DLMGCP, LOGL_ERROR, |
| 707 | "MDCX: endpoint:%x endpoint is not holding a connection.\n", |
| 708 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 709 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 710 | } |
| 711 | |
| 712 | for_each_line(line, p->save) { |
| 713 | if (!mgcp_check_param(endp, line)) |
| 714 | continue; |
| 715 | |
| 716 | switch (line[0]) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 717 | case 'C': |
| 718 | if (mgcp_verify_call_id(endp, line + 3) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 719 | goto error3; |
| 720 | break; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 721 | case 'I': |
| 722 | ci = (const char *)line + 3; |
| 723 | if (mgcp_verify_ci(endp, ci) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 724 | goto error3; |
| 725 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 726 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 727 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 728 | break; |
| 729 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 730 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 731 | break; |
| 732 | case 'Z': |
| 733 | silent = strcmp("noanswer", line + 3) == 0; |
| 734 | break; |
| 735 | case '\0': |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 736 | have_sdp = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 737 | goto mgcp_header_done; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 738 | break; |
| 739 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 740 | LOGP(DLMGCP, LOGL_NOTICE, |
| 741 | "MDCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n", |
| 742 | ENDPOINT_NUMBER(endp), line[0], line[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 743 | break; |
| 744 | } |
| 745 | } |
| 746 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 747 | mgcp_header_done: |
| 748 | if (mgcp_parse_ci(&conn_id, ci)) { |
| 749 | LOGP(DLMGCP, LOGL_ERROR, |
| 750 | "MDCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n", |
| 751 | ENDPOINT_NUMBER(endp)); |
| 752 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 753 | } |
| 754 | |
| 755 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 756 | if (!conn) |
| 757 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 758 | |
| 759 | if (mode) { |
| 760 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 761 | error_code = 517; |
| 762 | goto error3; |
| 763 | } |
| 764 | } else |
| 765 | conn->conn->mode = conn->conn->mode_orig; |
| 766 | |
| 767 | if (have_sdp) |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 768 | mgcp_parse_sdp_data(endp, conn, p); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 769 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 770 | set_local_cx_options(endp->tcfg->endpoints, &endp->local_options, |
| 771 | local_options); |
| 772 | |
| 773 | if (!have_sdp && endp->local_options.codec) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 774 | mgcp_set_audio_info(p->cfg, &conn->end.codec, |
| 775 | PTYPE_UNDEFINED, endp->local_options.codec); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 776 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 777 | if (setup_rtp_processing(endp, conn) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 778 | goto error3; |
| 779 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 780 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 781 | /* policy CB */ |
| 782 | if (p->cfg->policy_cb) { |
| 783 | int rc; |
| 784 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 785 | MGCP_ENDP_MDCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 786 | switch (rc) { |
| 787 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 788 | LOGP(DLMGCP, LOGL_NOTICE, |
| 789 | "MDCX: endpoint:%x rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 790 | ENDPOINT_NUMBER(endp)); |
| 791 | if (silent) |
| 792 | goto out_silent; |
| 793 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 794 | break; |
| 795 | case MGCP_POLICY_DEFER: |
| 796 | /* stop processing */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 797 | LOGP(DLMGCP, LOGL_DEBUG, |
| 798 | "MDCX: endpoint:%x defered by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 799 | ENDPOINT_NUMBER(endp)); |
| 800 | return NULL; |
| 801 | break; |
| 802 | case MGCP_POLICY_CONT: |
| 803 | /* just continue */ |
| 804 | break; |
| 805 | } |
| 806 | } |
| 807 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 808 | mgcp_rtp_end_config(endp, 1, &conn->end); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 809 | |
| 810 | /* modify */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 811 | LOGP(DLMGCP, LOGL_DEBUG, |
| 812 | "MDCX: endpoint:%x modified conn:%s\n", |
| 813 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 814 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 815 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 816 | MGCP_ENDP_MDCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 817 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 818 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY && |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 819 | endp->tcfg->keepalive_interval != 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 820 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 821 | |
| 822 | if (silent) |
| 823 | goto out_silent; |
| 824 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 825 | LOGP(DLMGCP, LOGL_NOTICE, |
| 826 | "MDCX: endpoint:%x connection successfully modified\n", |
| 827 | ENDPOINT_NUMBER(endp)); |
| 828 | return create_response_with_sdp(endp, conn, "MDCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 829 | error3: |
| 830 | return create_err_response(endp, error_code, "MDCX", p->trans); |
| 831 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 832 | out_silent: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 833 | LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:%x silent exit\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 834 | ENDPOINT_NUMBER(endp)); |
| 835 | return NULL; |
| 836 | } |
| 837 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 838 | /* DLCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 839 | static struct msgb *handle_delete_con(struct mgcp_parse_data *p) |
| 840 | { |
| 841 | struct mgcp_endpoint *endp = p->endp; |
| 842 | int error_code = 400; |
| 843 | int silent = 0; |
| 844 | char *line; |
| 845 | char stats[1048]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 846 | const char *ci = NULL; |
| 847 | struct mgcp_conn_rtp *conn = NULL; |
| 848 | uint32_t conn_id; |
| 849 | |
| 850 | LOGP(DLMGCP, LOGL_NOTICE, |
| 851 | "DLCX: endpoint:%x deleting connection ...\n", |
| 852 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 853 | |
| 854 | if (p->found != 0) |
| 855 | return create_err_response(NULL, error_code, "DLCX", p->trans); |
| 856 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 857 | if (llist_count(&endp->conns) <= 0) { |
| 858 | LOGP(DLMGCP, LOGL_ERROR, |
| 859 | "DLCX: endpoint:%x endpoint is not holding a connection.\n", |
| 860 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 861 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 862 | } |
| 863 | |
| 864 | for_each_line(line, p->save) { |
| 865 | if (!mgcp_check_param(endp, line)) |
| 866 | continue; |
| 867 | |
| 868 | switch (line[0]) { |
| 869 | case 'C': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 870 | if (mgcp_verify_call_id(endp, line + 3) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 871 | goto error3; |
| 872 | break; |
| 873 | case 'I': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 874 | ci = (const char *)line + 3; |
| 875 | if (mgcp_verify_ci(endp, ci) != 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 876 | goto error3; |
| 877 | break; |
| 878 | case 'Z': |
| 879 | silent = strcmp("noanswer", line + 3) == 0; |
| 880 | break; |
| 881 | default: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 882 | LOGP(DLMGCP, LOGL_NOTICE, |
| 883 | "DLCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n", |
| 884 | ENDPOINT_NUMBER(endp), line[0], line[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 885 | break; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | /* policy CB */ |
| 890 | if (p->cfg->policy_cb) { |
| 891 | int rc; |
| 892 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 893 | MGCP_ENDP_DLCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 894 | switch (rc) { |
| 895 | case MGCP_POLICY_REJECT: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 896 | LOGP(DLMGCP, LOGL_NOTICE, |
| 897 | "DLCX: endpoint:%x rejected by policy\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 898 | ENDPOINT_NUMBER(endp)); |
| 899 | if (silent) |
| 900 | goto out_silent; |
| 901 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 902 | break; |
| 903 | case MGCP_POLICY_DEFER: |
| 904 | /* stop processing */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 905 | return NULL; |
| 906 | break; |
| 907 | case MGCP_POLICY_CONT: |
| 908 | /* just continue */ |
| 909 | break; |
| 910 | } |
| 911 | } |
| 912 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame^] | 913 | /* When no connection id is supplied, we will interpret this as a |
| 914 | * wildcarded DLCX and drop all connections at once. (See also |
| 915 | * RFC3435 Section F.7) */ |
| 916 | if (!ci) { |
| 917 | LOGP(DLMGCP, LOGL_NOTICE, |
| 918 | "DLCX: endpoint:%x missing ci (connectionIdentifier), will remove all connections at once\n", |
| 919 | ENDPOINT_NUMBER(endp)); |
| 920 | |
| 921 | mgcp_release_endp(endp); |
| 922 | |
| 923 | /* Note: In this case we do not return any statistics, |
| 924 | * as we assume that the client is not interested in |
| 925 | * this case. */ |
| 926 | return create_ok_response(endp, 200, "DLCX", p->trans); |
| 927 | } |
| 928 | |
| 929 | /* Parse the connection id */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 930 | if (mgcp_parse_ci(&conn_id, ci)) { |
| 931 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame^] | 932 | "DLCX: endpoint:%x insufficient parameters, invalid ci (connectionIdentifier)\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 933 | ENDPOINT_NUMBER(endp)); |
| 934 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 935 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 936 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame^] | 937 | /* Find the connection */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 938 | conn = mgcp_conn_get_rtp(endp, conn_id); |
| 939 | if (!conn) |
| 940 | goto error3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 941 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 942 | /* save the statistics of the current connection */ |
| 943 | mgcp_format_stats(stats, sizeof(stats), conn->conn); |
| 944 | |
| 945 | /* delete connection */ |
| 946 | LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x deleting conn:%s\n", |
| 947 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn)); |
| 948 | mgcp_conn_free(endp, conn_id); |
| 949 | LOGP(DLMGCP, LOGL_NOTICE, |
| 950 | "DLCX: endpoint:%x connection successfully deleted\n", |
| 951 | ENDPOINT_NUMBER(endp)); |
| 952 | |
| 953 | /* When all connections are closed, the endpoint will be released |
| 954 | * in order to be ready to be used by another call. */ |
| 955 | if (llist_count(&endp->conns) <= 0) { |
| 956 | mgcp_release_endp(endp); |
| 957 | LOGP(DLMGCP, LOGL_DEBUG, |
| 958 | "DLCX: endpoint:%x endpoint released\n", |
| 959 | ENDPOINT_NUMBER(endp)); |
| 960 | } |
| 961 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 962 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 963 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 964 | MGCP_ENDP_DLCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 965 | |
| 966 | if (silent) |
| 967 | goto out_silent; |
| 968 | return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats); |
| 969 | |
| 970 | error3: |
| 971 | return create_err_response(endp, error_code, "DLCX", p->trans); |
| 972 | |
| 973 | out_silent: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 974 | LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x silent exit\n", |
| 975 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 976 | return NULL; |
| 977 | } |
| 978 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 979 | /* RSIP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 980 | static struct msgb *handle_rsip(struct mgcp_parse_data *p) |
| 981 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 982 | /* TODO: Also implement the resetting of a specific endpoint |
| 983 | * to make mgcp_send_reset_ep() work. Currently this will call |
| 984 | * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1 |
| 985 | * to make read_call_agent() reset all endpoints when called |
| 986 | * next time. In order to selectively reset endpoints some |
| 987 | * mechanism to distinguish which endpoint shall be resetted |
| 988 | * is needed */ |
| 989 | |
| 990 | LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n"); |
| 991 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 992 | if (p->found != 0) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 993 | LOGP(DLMGCP, LOGL_ERROR, |
| 994 | "RSIP: failed to find the endpoint.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 995 | return NULL; |
| 996 | } |
| 997 | |
| 998 | if (p->cfg->reset_cb) |
| 999 | p->cfg->reset_cb(p->endp->tcfg); |
| 1000 | return NULL; |
| 1001 | } |
| 1002 | |
| 1003 | static char extract_tone(const char *line) |
| 1004 | { |
| 1005 | const char *str = strstr(line, "D/"); |
| 1006 | if (!str) |
| 1007 | return CHAR_MAX; |
| 1008 | |
| 1009 | return str[2]; |
| 1010 | } |
| 1011 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1012 | /* This can request like DTMF detection and forward, fax detection... it |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1013 | * can also request when the notification should be send and such. We don't |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1014 | * do this right now. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1015 | static struct msgb *handle_noti_req(struct mgcp_parse_data *p) |
| 1016 | { |
| 1017 | int res = 0; |
| 1018 | char *line; |
| 1019 | char tone = CHAR_MAX; |
| 1020 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1021 | LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n"); |
| 1022 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1023 | if (p->found != 0) |
| 1024 | return create_err_response(NULL, 400, "RQNT", p->trans); |
| 1025 | |
| 1026 | for_each_line(line, p->save) { |
| 1027 | switch (line[0]) { |
| 1028 | case 'S': |
| 1029 | tone = extract_tone(line); |
| 1030 | break; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /* we didn't see a signal request with a tone */ |
| 1035 | if (tone == CHAR_MAX) |
| 1036 | return create_ok_response(p->endp, 200, "RQNT", p->trans); |
| 1037 | |
| 1038 | if (p->cfg->rqnt_cb) |
| 1039 | res = p->cfg->rqnt_cb(p->endp, tone); |
| 1040 | |
| 1041 | return res == 0 ? |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1042 | create_ok_response(p->endp, 200, "RQNT", p->trans) : |
| 1043 | create_err_response(p->endp, res, "RQNT", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1046 | /* Connection keepalive timer, will take care that dummy packets are send |
| 1047 | * regulary, so that NAT connections stay open */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1048 | static void mgcp_keepalive_timer_cb(void *_tcfg) |
| 1049 | { |
| 1050 | struct mgcp_trunk_config *tcfg = _tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1051 | struct mgcp_conn *conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1052 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1053 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1054 | LOGP(DLMGCP, LOGL_DEBUG, "Triggered trunk %d keepalive timer.\n", |
| 1055 | tcfg->trunk_nr); |
| 1056 | |
| 1057 | if (tcfg->keepalive_interval <= 0) |
| 1058 | return; |
| 1059 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1060 | /* Send walk over all endpoints and send out dummy packets through |
| 1061 | * every connection present on each endpoint */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1062 | for (i = 1; i < tcfg->number_endpoints; ++i) { |
| 1063 | struct mgcp_endpoint *endp = &tcfg->endpoints[i]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1064 | llist_for_each_entry(conn, &endp->conns, entry) { |
| 1065 | if (conn->mode == MGCP_CONN_RECV_ONLY) |
| 1066 | send_dummy(endp, &conn->u.rtp); |
| 1067 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | LOGP(DLMGCP, LOGL_DEBUG, "Rescheduling trunk %d keepalive timer.\n", |
| 1071 | tcfg->trunk_nr); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1072 | osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval, |
| 1073 | 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval) |
| 1077 | { |
| 1078 | tcfg->keepalive_interval = interval; |
| 1079 | osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg); |
| 1080 | |
| 1081 | if (interval <= 0) |
| 1082 | osmo_timer_del(&tcfg->keepalive_timer); |
| 1083 | else |
| 1084 | osmo_timer_schedule(&tcfg->keepalive_timer, |
| 1085 | tcfg->keepalive_interval, 0); |
| 1086 | } |
| 1087 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1088 | /*! allocate configuration with default values. |
| 1089 | * (called once at startup by main function) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1090 | struct mgcp_config *mgcp_config_alloc(void) |
| 1091 | { |
| 1092 | struct mgcp_config *cfg; |
| 1093 | |
| 1094 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 1095 | if (!cfg) { |
| 1096 | LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 1097 | return NULL; |
| 1098 | } |
| 1099 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1100 | cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START; |
| 1101 | cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END; |
| 1102 | cfg->net_ports.last_port = cfg->net_ports.range_start; |
| 1103 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1104 | cfg->source_port = 2427; |
| 1105 | cfg->source_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1106 | cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1107 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1108 | cfg->rtp_processing_cb = &mgcp_rtp_processing_default; |
| 1109 | cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default; |
| 1110 | |
| 1111 | cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default; |
| 1112 | |
| 1113 | /* default trunk handling */ |
| 1114 | cfg->trunk.cfg = cfg; |
| 1115 | cfg->trunk.trunk_nr = 0; |
| 1116 | cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL; |
| 1117 | cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1118 | cfg->trunk.audio_payload = 126; |
| 1119 | cfg->trunk.audio_send_ptime = 1; |
| 1120 | cfg->trunk.audio_send_name = 1; |
| 1121 | cfg->trunk.omit_rtcp = 0; |
| 1122 | mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE); |
| 1123 | |
| 1124 | INIT_LLIST_HEAD(&cfg->trunks); |
| 1125 | |
| 1126 | return cfg; |
| 1127 | } |
| 1128 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1129 | /*! allocate configuration with default values. |
| 1130 | * (called once at startup by VTY) |
| 1131 | * \param[in] cfg mgcp configuration |
| 1132 | * \param[in] nr trunk number |
| 1133 | * \returns pointer to allocated trunk configuration */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1134 | struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr) |
| 1135 | { |
| 1136 | struct mgcp_trunk_config *trunk; |
| 1137 | |
| 1138 | trunk = talloc_zero(cfg, struct mgcp_trunk_config); |
| 1139 | if (!trunk) { |
| 1140 | LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n"); |
| 1141 | return NULL; |
| 1142 | } |
| 1143 | |
| 1144 | trunk->cfg = cfg; |
| 1145 | trunk->trunk_type = MGCP_TRUNK_E1; |
| 1146 | trunk->trunk_nr = nr; |
| 1147 | trunk->audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1148 | trunk->audio_payload = 126; |
| 1149 | trunk->audio_send_ptime = 1; |
| 1150 | trunk->audio_send_name = 1; |
| 1151 | trunk->number_endpoints = 33; |
| 1152 | trunk->omit_rtcp = 0; |
| 1153 | mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE); |
| 1154 | llist_add_tail(&trunk->entry, &cfg->trunks); |
| 1155 | return trunk; |
| 1156 | } |
| 1157 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1158 | /*! get trunk configuration by trunk number (index). |
| 1159 | * \param[in] cfg mgcp configuration |
| 1160 | * \param[in] index trunk number |
| 1161 | * \returns pointer to trunk configuration, NULL on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1162 | struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index) |
| 1163 | { |
| 1164 | struct mgcp_trunk_config *trunk; |
| 1165 | |
| 1166 | llist_for_each_entry(trunk, &cfg->trunks, entry) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1167 | if (trunk->trunk_nr == index) |
| 1168 | return trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1169 | |
| 1170 | return NULL; |
| 1171 | } |
| 1172 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1173 | /*! allocate endpoints and set default values. |
| 1174 | * (called once at startup by VTY) |
| 1175 | * \param[in] tcfg trunk configuration |
| 1176 | * \returns 0 on success, -1 on failure */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1177 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg) |
| 1178 | { |
| 1179 | int i; |
| 1180 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1181 | tcfg->endpoints = _talloc_zero_array(tcfg->cfg, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1182 | sizeof(struct mgcp_endpoint), |
| 1183 | tcfg->number_endpoints, |
| 1184 | "endpoints"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1185 | if (!tcfg->endpoints) |
| 1186 | return -1; |
| 1187 | |
| 1188 | for (i = 0; i < tcfg->number_endpoints; ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1189 | INIT_LLIST_HEAD(&tcfg->endpoints[i].conns); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1190 | tcfg->endpoints[i].cfg = tcfg->cfg; |
| 1191 | tcfg->endpoints[i].tcfg = tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1192 | |
| 1193 | /* NOTE: Currently all endpoints are of type RTP, this will |
| 1194 | * change when new variations are implemented */ |
| 1195 | tcfg->endpoints[i].type = &ep_typeset.rtp; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1201 | /*! relase endpoint, all open connections are closed. |
| 1202 | * \param[in] endp endpoint to release */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1203 | void mgcp_release_endp(struct mgcp_endpoint *endp) |
| 1204 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1205 | LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:%x\n", |
| 1206 | ENDPOINT_NUMBER(endp)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1207 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1208 | /* Normally this function should only be called wehen |
| 1209 | * all connections have been removed already. In case |
| 1210 | * that there are still connections open (e.g. when |
| 1211 | * RSIP is executed), free them all at once. */ |
| 1212 | mgcp_conn_free_all(endp); |
| 1213 | |
| 1214 | /* Reset endpoint parameters and states */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1215 | talloc_free(endp->callid); |
| 1216 | endp->callid = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1217 | talloc_free(endp->local_options.string); |
| 1218 | endp->local_options.string = NULL; |
| 1219 | talloc_free(endp->local_options.codec); |
| 1220 | endp->local_options.codec = NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | static int send_agent(struct mgcp_config *cfg, const char *buf, int len) |
| 1224 | { |
| 1225 | return write(cfg->gw_fd.bfd.fd, buf, len); |
| 1226 | } |
| 1227 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1228 | /*! Reset all endpoints by sending RSIP message to self. |
| 1229 | * (called by VTY) |
| 1230 | * \param[in] endp trunk endpoint |
| 1231 | * \param[in] endpoint number |
| 1232 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1233 | int mgcp_send_reset_all(struct mgcp_config *cfg) |
| 1234 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1235 | int rc; |
| 1236 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1237 | static const char mgcp_reset[] = { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1238 | "RSIP 1 *@mgw MGCP 1.0\r\n" |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1239 | }; |
| 1240 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1241 | rc = send_agent(cfg, mgcp_reset, sizeof mgcp_reset - 1); |
| 1242 | if (rc <= 0) |
| 1243 | return -1; |
| 1244 | |
| 1245 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1246 | } |
| 1247 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1248 | /*! Reset a single endpoint by sending RSIP message to self. |
| 1249 | * (called by VTY) |
| 1250 | * \param[in] endp trunk endpoint |
| 1251 | * \param[in] endpoint number |
| 1252 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1253 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint) |
| 1254 | { |
| 1255 | char buf[128]; |
| 1256 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1257 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1258 | |
| 1259 | len = snprintf(buf, sizeof(buf), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1260 | "RSIP 39 %x@mgw MGCP 1.0\r\n", endpoint); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1261 | if (len < 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1262 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1263 | |
| 1264 | buf[sizeof(buf) - 1] = '\0'; |
| 1265 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1266 | rc = send_agent(endp->cfg, buf, len); |
| 1267 | if (rc <= 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1268 | return -1; |
| 1269 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1270 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1271 | } |