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