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> |
Eric | 55fdfc2 | 2021-08-13 00:14:18 +0200 | [diff] [blame] | 31 | #include <pthread.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 32 | |
| 33 | #include <osmocom/core/msgb.h> |
| 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/select.h> |
| 36 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 37 | #include <osmocom/mgcp/mgcp.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 38 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 39 | #include <osmocom/mgcp/osmux.h> |
| 40 | #include <osmocom/mgcp/mgcp_network.h> |
| 41 | #include <osmocom/mgcp/mgcp_protocol.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 42 | #include <osmocom/mgcp/mgcp_stat.h> |
| 43 | #include <osmocom/mgcp/mgcp_msg.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 44 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 45 | #include <osmocom/mgcp/mgcp_trunk.h> |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 46 | #include <osmocom/mgcp/mgcp_sdp.h> |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 47 | #include <osmocom/mgcp/mgcp_codec.h> |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 48 | #include <osmocom/mgcp/mgcp_conn.h> |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 49 | #include <osmocom/mgcp/mgcp_iuup.h> |
Pau Espin Pedrol | 8e0d101 | 2022-10-06 10:29:11 +0200 | [diff] [blame] | 50 | #include <osmocom/mgcp/debug.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 51 | |
Philipp Maier | 39889e4 | 2021-08-04 17:42:57 +0200 | [diff] [blame] | 52 | /* Contains the last successfully resolved endpoint name. This variable is used |
| 53 | * for the unit-tests to verify that the endpoint was correctly resolved. */ |
| 54 | static char debug_last_endpoint_name[MGCP_ENDPOINT_MAXLEN]; |
| 55 | |
| 56 | /* Called from unit-tests only */ |
| 57 | char *mgcp_debug_get_last_endpoint_name(void) |
| 58 | { |
| 59 | return debug_last_endpoint_name; |
| 60 | } |
| 61 | |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 62 | /* A combination of LOGPENDP and LOGPTRUNK that automatically falls back to |
| 63 | * LOGPTRUNK when the endp parameter is NULL */ |
| 64 | #define LOGPEPTR(endp, trunk, cat, level, fmt, args...) \ |
| 65 | do { \ |
| 66 | if (endp) \ |
| 67 | LOGPENDP(endp, cat, level, fmt, ## args); \ |
| 68 | else \ |
| 69 | LOGPTRUNK(trunk, cat, level, fmt, ## args); \ |
| 70 | } while (0) |
| 71 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 72 | /* Request data passed to the request handler */ |
| 73 | struct mgcp_request_data { |
| 74 | /* request name (e.g. "MDCX") */ |
| 75 | char name[4+1]; |
| 76 | |
| 77 | /* parsing results from the MGCP header (trans id, endpoint name ...) */ |
| 78 | struct mgcp_parse_data *pdata; |
| 79 | |
| 80 | /* pointer to endpoint resource (may be NULL for wildcarded requests) */ |
| 81 | struct mgcp_endpoint *endp; |
| 82 | |
| 83 | /* pointer to trunk resource */ |
| 84 | struct mgcp_trunk *trunk; |
| 85 | |
| 86 | /* set to true when the request has been classified as wildcarded */ |
| 87 | bool wildcarded; |
| 88 | |
| 89 | /* contains cause code in case of problems during endp/trunk resolution */ |
| 90 | int mgcp_cause; |
| 91 | }; |
| 92 | |
Philipp Maier | 33d97f7 | 2021-07-14 14:53:45 +0200 | [diff] [blame] | 93 | /* Request handler specification, here we specify an array with function |
| 94 | * pointers to the various MGCP requests implemented below */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 95 | struct mgcp_request { |
Philipp Maier | 33d97f7 | 2021-07-14 14:53:45 +0200 | [diff] [blame] | 96 | /* request name (e.g. "MDCX") */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 97 | char *name; |
Philipp Maier | 33d97f7 | 2021-07-14 14:53:45 +0200 | [diff] [blame] | 98 | |
| 99 | /* function pointer to the request handler */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 100 | struct msgb *(*handle_request)(struct mgcp_request_data *data); |
| 101 | |
Philipp Maier | 33d97f7 | 2021-07-14 14:53:45 +0200 | [diff] [blame] | 102 | /* a human readable name that describes the request */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 103 | char *debug_name; |
| 104 | }; |
| 105 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 106 | static struct msgb *handle_audit_endpoint(struct mgcp_request_data *data); |
| 107 | static struct msgb *handle_create_con(struct mgcp_request_data *data); |
| 108 | static struct msgb *handle_delete_con(struct mgcp_request_data *data); |
| 109 | static struct msgb *handle_modify_con(struct mgcp_request_data *data); |
| 110 | static struct msgb *handle_rsip(struct mgcp_request_data *data); |
| 111 | static struct msgb *handle_noti_req(struct mgcp_request_data *data); |
Philipp Maier | 33d97f7 | 2021-07-14 14:53:45 +0200 | [diff] [blame] | 112 | static const struct mgcp_request mgcp_requests[] = { |
Eric | ee6958c | 2021-09-14 18:36:53 +0200 | [diff] [blame] | 113 | { .name = "AUEP", .handle_request = handle_audit_endpoint, .debug_name = "AuditEndpoint" }, |
| 114 | { |
| 115 | .name = "CRCX", |
| 116 | .handle_request = handle_create_con, |
| 117 | .debug_name = "CreateConnection", |
| 118 | }, |
| 119 | { |
| 120 | .name = "DLCX", |
| 121 | .handle_request = handle_delete_con, |
| 122 | .debug_name = "DeleteConnection", |
| 123 | }, |
| 124 | { |
| 125 | .name = "MDCX", |
| 126 | .handle_request = handle_modify_con, |
| 127 | .debug_name = "ModifiyConnection", |
| 128 | }, |
| 129 | { |
| 130 | .name = "RQNT", |
| 131 | .handle_request = handle_noti_req, |
| 132 | .debug_name = "NotificationRequest", |
| 133 | }, |
Philipp Maier | 33d97f7 | 2021-07-14 14:53:45 +0200 | [diff] [blame] | 134 | |
| 135 | /* SPEC extension */ |
Eric | ee6958c | 2021-09-14 18:36:53 +0200 | [diff] [blame] | 136 | { |
| 137 | .name = "RSIP", |
| 138 | .handle_request = handle_rsip, |
| 139 | .debug_name = "ReSetInProgress", |
| 140 | }, |
Philipp Maier | 33d97f7 | 2021-07-14 14:53:45 +0200 | [diff] [blame] | 141 | }; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 142 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 143 | /* Initalize transcoder */ |
| 144 | static int setup_rtp_processing(struct mgcp_endpoint *endp, |
| 145 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 146 | { |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 147 | struct mgcp_config *cfg = endp->trunk->cfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 148 | struct mgcp_conn_rtp *conn_src = NULL; |
| 149 | struct mgcp_conn_rtp *conn_dst = conn; |
| 150 | struct mgcp_conn *_conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 151 | |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 152 | switch (conn->type) { |
| 153 | case MGCP_RTP_DEFAULT: |
Pau Espin Pedrol | 9d939b6 | 2022-10-03 16:59:20 +0200 | [diff] [blame] | 154 | case MGCP_RTP_OSMUX: |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 155 | case MGCP_RTP_IUUP: |
| 156 | break; |
| 157 | default: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 158 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 159 | "RTP-setup: Endpoint is not configured as RTP default, stopping here!\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 163 | if (conn->conn->mode == MGCP_CONN_LOOPBACK) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 164 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 165 | "RTP-setup: Endpoint is in loopback mode, stopping here!\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* Find the "sister" connection */ |
| 170 | llist_for_each_entry(_conn, &endp->conns, entry) { |
| 171 | if (_conn->id != conn->conn->id) { |
| 172 | conn_src = &_conn->u.rtp; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
Philipp Maier | acc1035 | 2018-07-19 18:07:57 +0200 | [diff] [blame] | 177 | return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 180 | /* Helper function to allocate some memory for responses and retransmissions */ |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 181 | static struct msgb *mgcp_msgb_alloc(void *ctx) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 182 | { |
| 183 | struct msgb *msg; |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 184 | msg = msgb_alloc_headroom_c(ctx, 4096, 128, "MGCP msg"); |
| 185 | |
| 186 | if (!msg) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 187 | LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n"); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 190 | |
| 191 | return msg; |
| 192 | } |
| 193 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 194 | /* Helper function for do_retransmission() and create_resp() */ |
Eric | 958f5e7 | 2021-08-03 23:00:17 +0200 | [diff] [blame] | 195 | static struct msgb *create_retransmission_response(const struct mgcp_endpoint *endp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 196 | { |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 197 | struct msgb *msg = mgcp_msgb_alloc(endp->trunk); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 198 | if (!msg) |
| 199 | return NULL; |
| 200 | |
| 201 | msg->l2h = msgb_put(msg, strlen(endp->last_response)); |
| 202 | memcpy(msg->l2h, endp->last_response, msgb_l2len(msg)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 203 | mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 204 | return msg; |
| 205 | } |
| 206 | |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 207 | static struct msgb *create_resp(void *msgctx, struct mgcp_endpoint *endp, int code, const char *txt, const char *msg, |
| 208 | const char *trans, const char *param, const char *sdp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 209 | { |
| 210 | int len; |
| 211 | struct msgb *res; |
| 212 | |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 213 | OSMO_ASSERT(msgctx != 0); |
| 214 | res = mgcp_msgb_alloc(msgctx); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 215 | if (!res) |
| 216 | return NULL; |
| 217 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 218 | len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s", |
| 219 | code, trans, txt, param ? param : "", sdp ? sdp : ""); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 220 | if (len < 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 221 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 222 | msgb_free(res); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | res->l2h = msgb_put(res, len); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 227 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 228 | mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * Remember the last transmission per endpoint. |
| 232 | */ |
| 233 | if (endp) { |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 234 | struct mgcp_trunk *trunk = endp->trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 235 | talloc_free(endp->last_response); |
| 236 | talloc_free(endp->last_trans); |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 237 | endp->last_trans = talloc_strdup(trunk->endpoints, trans); |
| 238 | endp->last_response = talloc_strndup(trunk->endpoints, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 239 | (const char *)res->l2h, |
| 240 | msgb_l2len(res)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return res; |
| 244 | } |
| 245 | |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 246 | static struct msgb *create_ok_resp_with_param(void *msgctx, struct mgcp_endpoint *endp, int code, const char *msg, |
| 247 | const char *trans, const char *param) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 248 | { |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 249 | return create_resp(msgctx, endp, code, " OK", msg, trans, param, NULL); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 252 | static struct msgb *create_ok_response(void *msgctx, struct mgcp_endpoint *endp, int code, const char *msg, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 253 | const char *trans) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 254 | { |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 255 | return create_ok_resp_with_param(msgctx, endp, code, msg, trans, NULL); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 258 | static struct msgb *create_err_response(void *msgctx, struct mgcp_endpoint *endp, int code, const char *msg, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 259 | const char *trans) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 260 | { |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 261 | return create_resp(msgctx, endp, code, " FAIL", msg, trans, NULL, NULL); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 264 | /* Format MGCP response string (with SDP attached) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 265 | static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 266 | struct mgcp_conn_rtp *conn, |
| 267 | const char *msg, |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 268 | const char *trans_id, |
Philipp Maier | 41d5920 | 2021-07-20 15:49:00 +0200 | [diff] [blame] | 269 | bool add_epname, |
| 270 | bool add_conn_id) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 271 | { |
Pau Espin Pedrol | 71d42e7 | 2020-09-03 14:20:07 +0200 | [diff] [blame] | 272 | /* cfg->local_ip allows overwritting the announced IP address with |
| 273 | * regards to the one we actually bind to. Useful in behind-NAT |
| 274 | * scenarios. |
| 275 | * TODO: we may want to define another local_ip_osmux var to |
| 276 | * us for OSMUX connections. Perhaps adding a new internal API to get it |
| 277 | * based on conn type. |
| 278 | */ |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 279 | const char *addr = strlen(endp->trunk->cfg->local_ip) ? endp->trunk->cfg->local_ip : conn->end.local_addr; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 280 | struct msgb *sdp; |
| 281 | int rc; |
| 282 | struct msgb *result; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 283 | |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 284 | sdp = msgb_alloc_headroom_c(endp->trunk, 4096, 128, "sdp record"); |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 285 | if (!sdp) |
| 286 | return NULL; |
| 287 | |
Philipp Maier | 41d5920 | 2021-07-20 15:49:00 +0200 | [diff] [blame] | 288 | /* Attach optional endpoint name */ |
| 289 | if (add_epname) { |
| 290 | rc = msgb_printf(sdp, "Z: %s\r\n", endp->name); |
| 291 | if (rc < 0) |
| 292 | goto error; |
| 293 | } |
| 294 | |
| 295 | /* Attach optional connection id */ |
| 296 | if (add_conn_id) { |
| 297 | rc = msgb_printf(sdp, "I: %s\r\n", conn->conn->id); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 298 | if (rc < 0) |
| 299 | goto error; |
| 300 | } |
| 301 | |
Philipp Maier | 3cbfb8a | 2018-01-22 11:39:59 +0100 | [diff] [blame] | 302 | /* Attach optional OSMUX parameters */ |
Pau Espin Pedrol | c63f15a | 2019-05-10 16:52:08 +0200 | [diff] [blame] | 303 | if (mgcp_conn_rtp_is_osmux(conn)) { |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 304 | rc = msgb_printf(sdp, MGCP_X_OSMO_OSMUX_HEADER " %u\r\n", conn->osmux.local_cid); |
Philipp Maier | 3cbfb8a | 2018-01-22 11:39:59 +0100 | [diff] [blame] | 305 | if (rc < 0) |
| 306 | goto error; |
| 307 | } |
| 308 | |
| 309 | /* Attach line break to separate the parameters from the SDP block */ |
Philipp Maier | c3cfae2 | 2018-01-22 12:03:03 +0100 | [diff] [blame] | 310 | rc = msgb_printf(sdp, "\r\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 311 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 312 | rc = mgcp_write_response_sdp(endp, conn, sdp, addr); |
| 313 | if (rc < 0) |
| 314 | goto error; |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 315 | result = create_resp(endp->trunk, endp, 200, " OK", msg, trans_id, NULL, (char *)sdp->data); |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 316 | msgb_free(sdp); |
| 317 | return result; |
| 318 | error: |
| 319 | msgb_free(sdp); |
| 320 | return NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 323 | /* Send out dummy packet to keep the connection open, if the connection is an |
| 324 | * osmux connection, send the dummy packet via OSMUX */ |
| 325 | 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] | 326 | { |
Pau Espin Pedrol | 2ca4882 | 2022-10-06 10:58:10 +0200 | [diff] [blame] | 327 | /* Avoid sending dummy packet if the remote address was not yet |
| 328 | * configured through CRCX/MDCX: */ |
| 329 | if (!mgcp_rtp_end_remote_addr_available(&conn->end)) |
| 330 | return; |
| 331 | |
Pau Espin Pedrol | abb9d47 | 2022-10-04 12:09:14 +0200 | [diff] [blame] | 332 | if (mgcp_conn_rtp_is_osmux(conn)) |
Pau Espin Pedrol | 887c93e | 2022-10-06 12:36:03 +0200 | [diff] [blame] | 333 | osmux_send_dummy(conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 334 | else |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 335 | mgcp_send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 338 | /* handle incoming messages: |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 339 | * - this can be a command (four letters, space, transaction id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 340 | * - or a response (three numbers, space, transaction id) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 341 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) |
| 342 | { |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 343 | struct rate_ctr_group *rate_ctrs = cfg->ratectr.mgcp_general_ctr_group; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 344 | struct mgcp_parse_data pdata; |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 345 | struct mgcp_request_data rq; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 346 | int rc, i, code, handled = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 347 | struct msgb *resp = NULL; |
| 348 | char *data; |
| 349 | |
Philipp Maier | 39889e4 | 2021-08-04 17:42:57 +0200 | [diff] [blame] | 350 | debug_last_endpoint_name[0] = '\0'; |
| 351 | |
Alexander Chemeris | 6386600 | 2020-05-05 17:18:40 +0300 | [diff] [blame] | 352 | /* Count all messages, even incorect ones */ |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 353 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_TOTAL)); |
Alexander Chemeris | 6386600 | 2020-05-05 17:18:40 +0300 | [diff] [blame] | 354 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 355 | if (msgb_l2len(msg) < 4) { |
| 356 | LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 357 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_MSG_PARSE)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 358 | return NULL; |
| 359 | } |
| 360 | |
Alexander Chemeris | 6386600 | 2020-05-05 17:18:40 +0300 | [diff] [blame] | 361 | if (mgcp_msg_terminate_nul(msg)) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 362 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_MSG_PARSE)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 363 | return NULL; |
Alexander Chemeris | 6386600 | 2020-05-05 17:18:40 +0300 | [diff] [blame] | 364 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 365 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 366 | mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 367 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 368 | /* attempt to treat it as a response */ |
| 369 | if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 370 | LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 371 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_MSG_PARSE)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
| 374 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 375 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 376 | /* Parse message, extract endpoint name and transaction identifier and request name etc. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 377 | memset(&pdata, 0, sizeof(pdata)); |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 378 | memset(&rq, 0, sizeof(rq)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 379 | pdata.cfg = cfg; |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 380 | memcpy(rq.name, (const char *)&msg->l2h[0], sizeof(rq.name)-1); |
| 381 | msg->l3h = &msg->l2h[4]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 382 | data = mgcp_strline((char *)msg->l3h, &pdata.save); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 383 | rc = mgcp_parse_header(&pdata, data); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 384 | if (rc < 0) { |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 385 | LOGP(DLMGCP, LOGL_ERROR, "%s: failed to parse MCGP message\n", rq.name); |
| 386 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_MSG_PARSE)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 387 | return create_err_response(cfg, NULL, -rc, rq.name, "000000"); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 390 | /* Locate endpoint and trunk, if no endpoint can be located try at least to identify the trunk. */ |
| 391 | rq.pdata = &pdata; |
| 392 | rq.wildcarded = mgcp_endp_is_wildcarded(pdata.epname); |
| 393 | rq.endp = mgcp_endp_by_name(&rc, pdata.epname, pdata.cfg); |
| 394 | rq.mgcp_cause = rc; |
| 395 | if (!rq.endp) { |
| 396 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_NO_ENDPOINT)); |
| 397 | if (rq.wildcarded) { |
| 398 | /* If we are unable to find the endpoint we still may be able to identify the trunk. Some |
| 399 | * request handlers will still be able to perform a useful action if the request refers to |
| 400 | * the whole trunk (wildcarded request). */ |
| 401 | LOGP(DLMGCP, LOGL_NOTICE, |
| 402 | "%s: cannot find endpoint \"%s\", cause=%d -- trying to identify trunk...\n", rq.name, |
| 403 | pdata.epname, -rq.mgcp_cause); |
| 404 | rq.trunk = mgcp_trunk_by_name(pdata.cfg, pdata.epname); |
| 405 | if (!rq.trunk) { |
| 406 | LOGP(DLMGCP, LOGL_ERROR, "%s: failed to identify trunk for endpoint \"%s\" -- abort\n", |
| 407 | rq.name, pdata.epname); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 408 | return create_err_response(cfg, NULL, -rq.mgcp_cause, rq.name, pdata.trans); |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 409 | } |
| 410 | } else { |
| 411 | /* If the endpoint name suggests that the request refers to a specific endpoint, then the |
| 412 | * request cannot be handled and we must stop early. */ |
| 413 | LOGP(DLMGCP, LOGL_NOTICE, |
| 414 | "%s: cannot find endpoint \"%s\", cause=%d -- abort\n", rq.name, |
| 415 | pdata.epname, -rq.mgcp_cause); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 416 | return create_err_response(cfg, NULL, -rq.mgcp_cause, rq.name, pdata.trans); |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 417 | } |
| 418 | } else { |
Philipp Maier | 39889e4 | 2021-08-04 17:42:57 +0200 | [diff] [blame] | 419 | osmo_strlcpy(debug_last_endpoint_name, rq.endp->name, sizeof(debug_last_endpoint_name)); |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 420 | rq.trunk = rq.endp->trunk; |
| 421 | rq.mgcp_cause = 0; |
| 422 | |
| 423 | /* Check if we have to retransmit a response from a previous transaction */ |
| 424 | if (pdata.trans && rq.endp->last_trans && strcmp(rq.endp->last_trans, pdata.trans) == 0) { |
| 425 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_RETRANSMITTED)); |
Eric | 958f5e7 | 2021-08-03 23:00:17 +0200 | [diff] [blame] | 426 | return create_retransmission_response(rq.endp); |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | /* Find an appropriate handler for the current request and execute it */ |
| 431 | for (i = 0; i < ARRAY_SIZE(mgcp_requests); i++) { |
| 432 | if (strcmp(mgcp_requests[i].name, rq.name) == 0) { |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 433 | /* Execute request handler */ |
| 434 | if (rq.endp) |
| 435 | LOGP(DLMGCP, LOGL_INFO, |
| 436 | "%s: executing request handler \"%s\" for endpoint resource \"%s\"\n", rq.name, |
| 437 | mgcp_requests[i].debug_name, rq.endp->name); |
| 438 | else |
| 439 | LOGP(DLMGCP, LOGL_INFO, |
| 440 | "%s: executing request handler \"%s\" for trunk resource of endpoint \"%s\"\n", |
| 441 | rq.name, mgcp_requests[i].debug_name, pdata.epname); |
| 442 | resp = mgcp_requests[i].handle_request(&rq); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 443 | handled = 1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | } |
| 447 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 448 | /* Check if the MGCP request was handled and increment rate counters accordingly. */ |
Alexander Chemeris | 6386600 | 2020-05-05 17:18:40 +0300 | [diff] [blame] | 449 | if (handled) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 450 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_HANDLED)); |
Alexander Chemeris | 6386600 | 2020-05-05 17:18:40 +0300 | [diff] [blame] | 451 | } else { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 452 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_UNHANDLED)); |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 453 | LOGP(DLMGCP, LOGL_ERROR, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]); |
Alexander Chemeris | 6386600 | 2020-05-05 17:18:40 +0300 | [diff] [blame] | 454 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 455 | |
| 456 | return resp; |
| 457 | } |
| 458 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 459 | /* AUEP command handler, processes the received command */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 460 | static struct msgb *handle_audit_endpoint(struct mgcp_request_data *rq) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 461 | { |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 462 | LOGPENDP(rq->endp, DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n"); |
Eric | ee6958c | 2021-09-14 18:36:53 +0200 | [diff] [blame] | 463 | if (!rq->endp || !mgcp_endp_avail(rq->endp)) { |
| 464 | LOGPENDP(rq->endp, DLMGCP, LOGL_ERROR, "AUEP: selected endpoint not available!\n"); |
| 465 | return create_err_response(rq->trunk, NULL, 501, "AUEP", rq->pdata->trans); |
| 466 | } |
| 467 | |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 468 | return create_ok_response(rq->trunk, rq->endp, 200, "AUEP", rq->pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 471 | /* 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] | 472 | * counter that points on the next free port. Since we have a pointer |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 473 | * to the next free port, binding should in work on the first attempt in |
Pau Espin Pedrol | fc80673 | 2019-04-23 00:18:43 +0200 | [diff] [blame] | 474 | * general. In case of failure the next port is tried until the whole port |
| 475 | * range is tried once. */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 476 | 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] | 477 | { |
| 478 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 479 | struct mgcp_port_range *range; |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 480 | unsigned int tries; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 481 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 482 | OSMO_ASSERT(conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 483 | |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 484 | range = &endp->trunk->cfg->net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 485 | |
Eric | 55fdfc2 | 2021-08-13 00:14:18 +0200 | [diff] [blame] | 486 | pthread_mutex_lock(&range->lock); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 487 | /* attempt to find a port */ |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 488 | tries = (range->range_end - range->range_start) / 2; |
| 489 | for (i = 0; i < tries; ++i) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 490 | int rc; |
| 491 | |
| 492 | if (range->last_port >= range->range_end) |
| 493 | range->last_port = range->range_start; |
| 494 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 495 | rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 496 | |
| 497 | range->last_port += 2; |
| 498 | if (rc == 0) { |
Eric | 55fdfc2 | 2021-08-13 00:14:18 +0200 | [diff] [blame] | 499 | pthread_mutex_unlock(&range->lock); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | } |
Eric | 55fdfc2 | 2021-08-13 00:14:18 +0200 | [diff] [blame] | 504 | pthread_mutex_unlock(&range->lock); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 505 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 506 | "Allocating a RTP/RTCP port failed %u times.\n", |
| 507 | tries); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 508 | return -1; |
| 509 | } |
| 510 | |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 511 | /*! Helper function for check_local_cx_options() to get a pointer of the next |
| 512 | * lco option identifier |
| 513 | * \param[in] lco string |
| 514 | * \returns pointer to the beginning of the LCO identifier, NULL on failure */ |
| 515 | char *get_lco_identifier(const char *options) |
| 516 | { |
| 517 | char *ptr; |
| 518 | unsigned int count = 0; |
| 519 | |
| 520 | /* Jump to the end of the lco identifier */ |
| 521 | ptr = strstr(options, ":"); |
| 522 | if (!ptr) |
| 523 | return NULL; |
| 524 | |
| 525 | /* Walk backwards until the pointer points to the beginning of the |
| 526 | * lco identifier. We know that we stand at the beginning when we |
| 527 | * are either at the beginning of the memory or see a space or |
| 528 | * comma. (this is tolerant, it will accept a:10, b:11 as well as |
| 529 | * a:10,b:11) */ |
| 530 | while (1) { |
| 531 | /* Endless loop protection */ |
| 532 | if (count > 10000) |
| 533 | return NULL; |
| 534 | else if (ptr < options || *ptr == ' ' || *ptr == ',') { |
| 535 | ptr++; |
| 536 | break; |
| 537 | } |
| 538 | ptr--; |
| 539 | count++; |
| 540 | } |
| 541 | |
| 542 | /* Check if we got any result */ |
| 543 | if (*ptr == ':') |
| 544 | return NULL; |
| 545 | |
| 546 | return ptr; |
| 547 | } |
| 548 | |
| 549 | /*! Check the LCO option. This function checks for multiple appearence of LCO |
| 550 | * options, which is illegal |
| 551 | * \param[in] ctx talloc context |
| 552 | * \param[in] lco string |
| 553 | * \returns 0 on success, -1 on failure */ |
| 554 | int check_local_cx_options(void *ctx, const char *options) |
| 555 | { |
| 556 | int i; |
| 557 | char *options_copy; |
| 558 | char *lco_identifier; |
| 559 | char *lco_identifier_end; |
| 560 | char *next_lco_identifier; |
| 561 | |
| 562 | char **lco_seen; |
| 563 | unsigned int lco_seen_n = 0; |
| 564 | |
| 565 | if (!options) |
| 566 | return -1; |
| 567 | |
| 568 | lco_seen = |
| 569 | (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *)); |
| 570 | options_copy = talloc_strdup(ctx, options); |
| 571 | lco_identifier = options_copy; |
| 572 | |
| 573 | do { |
| 574 | /* Move the lco_identifier pointer to the beginning of the |
| 575 | * current lco option identifier */ |
| 576 | lco_identifier = get_lco_identifier(lco_identifier); |
| 577 | if (!lco_identifier) |
| 578 | goto error; |
| 579 | |
| 580 | /* Look ahead to the next LCO option early, since we |
| 581 | * will parse destructively */ |
| 582 | next_lco_identifier = strstr(lco_identifier + 1, ","); |
| 583 | |
| 584 | /* Pinch off the end of the lco field identifier name |
| 585 | * and see if we still got something, also check if |
| 586 | * there is some value after the colon. */ |
| 587 | lco_identifier_end = strstr(lco_identifier, ":"); |
| 588 | if (!lco_identifier_end) |
| 589 | goto error; |
| 590 | if (*(lco_identifier_end + 1) == ' ' |
| 591 | || *(lco_identifier_end + 1) == ',' |
| 592 | || *(lco_identifier_end + 1) == '\0') |
| 593 | goto error; |
| 594 | *lco_identifier_end = '\0'; |
| 595 | if (strlen(lco_identifier) == 0) |
| 596 | goto error; |
| 597 | |
| 598 | /* Check if we have already seen the current field identifier |
| 599 | * before. If yes, we must bail, an LCO must only appear once |
| 600 | * in the LCO string */ |
| 601 | for (i = 0; i < lco_seen_n; i++) { |
Pau Espin Pedrol | 7eb6f2c | 2019-06-26 13:00:52 +0200 | [diff] [blame] | 602 | if (strcasecmp(lco_seen[i], lco_identifier) == 0) |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 603 | goto error; |
| 604 | } |
| 605 | lco_seen[lco_seen_n] = lco_identifier; |
| 606 | lco_seen_n++; |
| 607 | |
| 608 | /* The first identifier must always be found at the beginnning |
| 609 | * of the LCO string */ |
| 610 | if (lco_seen[0] != options_copy) |
| 611 | goto error; |
| 612 | |
| 613 | /* Go to the next lco option */ |
| 614 | lco_identifier = next_lco_identifier; |
| 615 | } while (lco_identifier); |
| 616 | |
| 617 | talloc_free(lco_seen); |
| 618 | talloc_free(options_copy); |
| 619 | return 0; |
| 620 | error: |
| 621 | talloc_free(lco_seen); |
| 622 | talloc_free(options_copy); |
| 623 | return -1; |
| 624 | } |
| 625 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 626 | /* Set the LCO from a string (see RFC 3435). |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 627 | * 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] | 628 | * like an empty string, the 'string' field is never NULL after this function |
| 629 | * has been called. */ |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 630 | static int set_local_cx_options(void *ctx, struct mgcp_lco *lco, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 631 | const char *options) |
| 632 | { |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 633 | char *lco_id; |
Philipp Maier | 8dbc9ed | 2018-10-26 14:50:25 +0200 | [diff] [blame] | 634 | char codec[17]; |
Pau Espin Pedrol | 1953986 | 2020-09-21 11:43:41 +0200 | [diff] [blame] | 635 | char nt[17]; |
Pau Espin Pedrol | 83fd8a5 | 2019-06-26 12:55:26 +0200 | [diff] [blame] | 636 | int len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 637 | |
Philipp Maier | 604410c | 2018-06-06 10:02:16 +0200 | [diff] [blame] | 638 | if (!options) |
| 639 | return 0; |
| 640 | if (strlen(options) == 0) |
| 641 | return 0; |
| 642 | |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 643 | /* Make sure the encoding of the LCO is consistant before we proceed */ |
| 644 | if (check_local_cx_options(ctx, options) != 0) { |
| 645 | LOGP(DLMGCP, LOGL_ERROR, |
| 646 | "local CX options: Internal inconsistency in Local Connection Options!\n"); |
| 647 | return 524; |
| 648 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 649 | |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 650 | talloc_free(lco->string); |
| 651 | lco->string = talloc_strdup(ctx, options); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 652 | |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 653 | lco_id = lco->string; |
| 654 | while ((lco_id = get_lco_identifier(lco_id))) { |
| 655 | switch (tolower(lco_id[0])) { |
| 656 | case 'p': |
| 657 | if (sscanf(lco_id + 1, ":%d-%d", |
| 658 | &lco->pkt_period_min, &lco->pkt_period_max) == 1) |
| 659 | lco->pkt_period_max = lco->pkt_period_min; |
| 660 | break; |
| 661 | case 'a': |
Pau Espin Pedrol | 1dc2dce | 2020-09-21 11:25:15 +0200 | [diff] [blame] | 662 | /* FIXME: LCO also supports the negotiation of more than one codec. |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 663 | * (e.g. a:PCMU;G726-32) But this implementation only supports a single |
| 664 | * codec only. */ |
| 665 | if (sscanf(lco_id + 1, ":%16[^,]", codec) == 1) { |
| 666 | talloc_free(lco->codec); |
Pau Espin Pedrol | 83fd8a5 | 2019-06-26 12:55:26 +0200 | [diff] [blame] | 667 | /* MGCP header is case insensive, and we'll need |
| 668 | codec in uppercase when using it later: */ |
| 669 | len = strlen(codec); |
| 670 | lco->codec = talloc_size(ctx, len + 1); |
| 671 | osmo_str_toupper_buf(lco->codec, len + 1, codec); |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 672 | } |
| 673 | break; |
Pau Espin Pedrol | 1953986 | 2020-09-21 11:43:41 +0200 | [diff] [blame] | 674 | case 'n': |
| 675 | if (lco_id[1] == 't' && sscanf(lco_id + 2, ":%16[^,]", nt) == 1) |
| 676 | break; |
| 677 | /* else: fall throught to print notice log */ |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 678 | default: |
| 679 | LOGP(DLMGCP, LOGL_NOTICE, |
| 680 | "LCO: unhandled option: '%c'/%d in \"%s\"\n", |
| 681 | *lco_id, *lco_id, lco->string); |
| 682 | break; |
| 683 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 684 | |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 685 | lco_id = strchr(lco_id, ','); |
| 686 | if (!lco_id) |
| 687 | break; |
Philipp Maier | 604410c | 2018-06-06 10:02:16 +0200 | [diff] [blame] | 688 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 689 | |
| 690 | LOGP(DLMGCP, LOGL_DEBUG, |
| 691 | "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n", |
| 692 | lco->pkt_period_max, lco->codec); |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 693 | |
| 694 | /* Check if the packetization fits the 20ms raster */ |
| 695 | if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) { |
| 696 | LOGP(DLMGCP, LOGL_ERROR, |
| 697 | "local CX options: packetization interval is not a multiple of 20ms!\n"); |
| 698 | return 535; |
| 699 | } |
| 700 | |
| 701 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change, |
| 705 | struct mgcp_rtp_end *rtp) |
| 706 | { |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 707 | struct mgcp_trunk *trunk = endp->trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 708 | |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 709 | int patch_ssrc = expect_ssrc_change && trunk->force_constant_ssrc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 710 | |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 711 | rtp->force_aligned_timing = trunk->force_aligned_timing; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 712 | rtp->force_constant_ssrc = patch_ssrc ? 1 : 0; |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 713 | rtp->rfc5993_hr_convert = trunk->rfc5993_hr_convert; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 714 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 715 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, |
| 716 | "Configuring RTP endpoint: local port %d%s%s\n", |
Pau Espin Pedrol | 5ffd127 | 2022-10-04 13:45:48 +0200 | [diff] [blame] | 717 | osmo_sockaddr_port(&rtp->addr.u.sa), |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 718 | rtp->force_aligned_timing ? ", force constant timing" : "", |
| 719 | rtp->force_constant_ssrc ? ", force constant ssrc" : ""); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Pau Espin Pedrol | 8358c4b | 2021-07-07 12:41:38 +0200 | [diff] [blame] | 722 | uint32_t mgcp_rtp_packet_duration(const struct mgcp_endpoint *endp, |
| 723 | const struct mgcp_rtp_end *rtp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 724 | { |
| 725 | int f = 0; |
| 726 | |
| 727 | /* Get the number of frames per channel and packet */ |
| 728 | if (rtp->frames_per_packet) |
| 729 | f = rtp->frames_per_packet; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 730 | else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) { |
| 731 | int den = 1000 * rtp->codec->frame_duration_num; |
| 732 | f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den + |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 733 | den / 2) |
| 734 | / den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 737 | return rtp->codec->rate * f * rtp->codec->frame_duration_num / |
| 738 | rtp->codec->frame_duration_den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Pau Espin Pedrol | 9fb8ddf | 2019-05-08 15:35:36 +0200 | [diff] [blame] | 741 | /*! Initializes osmux socket if not yet initialized. Parses Osmux CID from MGCP line. |
| 742 | * \param[in] endp Endpoint willing to initialize osmux |
| 743 | * \param[in] line Line X-Osmux from MGCP header msg to parse |
| 744 | * \returns OSMUX CID, -1 for wildcard, -2 on parse error, -3 on osmux initalize error |
| 745 | */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 746 | static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line) |
| 747 | { |
Pau Espin Pedrol | 36413c0 | 2022-10-12 17:58:17 +0200 | [diff] [blame] | 748 | if (!endp->trunk->cfg->osmux.initialized) { |
Pau Espin Pedrol | c1ad7fd | 2022-10-06 10:32:07 +0200 | [diff] [blame] | 749 | if (osmux_init(endp->trunk) < 0) { |
Pau Espin Pedrol | 8e0d101 | 2022-10-06 10:29:11 +0200 | [diff] [blame] | 750 | LOGPENDP(endp, DOSMUX, LOGL_ERROR, "Cannot init OSMUX\n"); |
Pau Espin Pedrol | 9fb8ddf | 2019-05-08 15:35:36 +0200 | [diff] [blame] | 751 | return -3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 752 | } |
Pau Espin Pedrol | 8e0d101 | 2022-10-06 10:29:11 +0200 | [diff] [blame] | 753 | LOGPENDP(endp, DOSMUX, LOGL_NOTICE, "OSMUX socket has been set up\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | return mgcp_parse_osmux_cid(line); |
| 757 | } |
| 758 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 759 | /* Process codec information contained in CRCX/MDCX */ |
| 760 | static int handle_codec_info(struct mgcp_conn_rtp *conn, |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 761 | struct mgcp_request_data *rq, int have_sdp, bool crcx) |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 762 | { |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 763 | struct mgcp_endpoint *endp = rq->endp; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 764 | int rc; |
| 765 | char *cmd; |
| 766 | |
| 767 | if (crcx) |
| 768 | cmd = "CRCX"; |
| 769 | else |
| 770 | cmd = "MDCX"; |
| 771 | |
| 772 | /* Collect codec information */ |
| 773 | if (have_sdp) { |
| 774 | /* If we have SDP, we ignore the local connection options and |
| 775 | * use only the SDP information. */ |
| 776 | mgcp_codec_reset_all(conn); |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 777 | rc = mgcp_parse_sdp_data(endp, conn, rq->pdata); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 778 | if (rc != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 779 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 780 | "%s: sdp not parseable\n", cmd); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 781 | |
| 782 | /* See also RFC 3661: Protocol error */ |
| 783 | return 510; |
| 784 | } |
| 785 | } else if (endp->local_options.codec) { |
| 786 | /* When no SDP is available, we use the codec information from |
| 787 | * the local connection options (if present) */ |
| 788 | mgcp_codec_reset_all(conn); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 789 | rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec, NULL); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 790 | if (rc != 0) |
| 791 | goto error; |
| 792 | } |
| 793 | |
| 794 | /* Make sure we always set a sane default codec */ |
| 795 | if (conn->end.codecs_assigned == 0) { |
| 796 | /* When SDP and/or LCO did not supply any codec information, |
| 797 | * than it makes sense to pick a sane default: (payload-type 0, |
| 798 | * PCMU), see also: OS#2658 */ |
| 799 | mgcp_codec_reset_all(conn); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 800 | rc = mgcp_codec_add(conn, 0, NULL, NULL); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 801 | if (rc != 0) |
| 802 | goto error; |
| 803 | } |
| 804 | |
| 805 | /* Make codec decision */ |
| 806 | if (mgcp_codec_decide(conn) != 0) |
| 807 | goto error; |
| 808 | |
| 809 | return 0; |
| 810 | |
| 811 | error: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 812 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 813 | "%s: codec negotiation failure\n", cmd); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 814 | |
| 815 | /* See also RFC 3661: Codec negotiation failure */ |
| 816 | return 534; |
| 817 | } |
| 818 | |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 819 | static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line) |
| 820 | { |
| 821 | char *saveptr = NULL; |
| 822 | |
Pau Espin Pedrol | 6049a63 | 2020-09-21 11:03:21 +0200 | [diff] [blame] | 823 | if (strncasecmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER))) |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 824 | return false; |
| 825 | line += strlen(MGCP_X_OSMO_IGN_HEADER); |
| 826 | |
| 827 | while (1) { |
| 828 | char *token = strtok_r(line, " ", &saveptr); |
| 829 | line = NULL; |
| 830 | if (!token) |
| 831 | break; |
| 832 | |
Pau Espin Pedrol | 6e26c70 | 2019-06-26 13:09:39 +0200 | [diff] [blame] | 833 | if (!strcasecmp(token, "C")) |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 834 | endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID; |
| 835 | else |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 836 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "received unknown X-Osmo-IGN item '%s'\n", token); |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | return true; |
| 840 | } |
| 841 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 842 | /* CRCX command handler, processes the received command */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 843 | static struct msgb *handle_create_con(struct mgcp_request_data *rq) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 844 | { |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 845 | struct mgcp_parse_data *pdata = rq->pdata; |
| 846 | struct mgcp_trunk *trunk = rq->trunk; |
| 847 | struct mgcp_endpoint *endp = rq->endp; |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 848 | struct rate_ctr_group *rate_ctrs = trunk->ratectr.mgcp_crcx_ctr_group; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 849 | int error_code = 400; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 850 | const char *local_options = NULL; |
| 851 | const char *callid = NULL; |
| 852 | const char *mode = NULL; |
| 853 | char *line; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 854 | int have_sdp = 0, remote_osmux_cid = -2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 855 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 856 | struct mgcp_conn *_conn = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 857 | char conn_name[512]; |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 858 | int rc; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 859 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 860 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n"); |
Philipp Maier | 246233d | 2020-08-18 15:15:24 +0200 | [diff] [blame] | 861 | |
Eric | ee6958c | 2021-09-14 18:36:53 +0200 | [diff] [blame] | 862 | /* we must have a free ep */ |
| 863 | if (!endp) { |
| 864 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_AVAIL)); |
| 865 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "CRCX: no free endpoints available!\n"); |
| 866 | return create_err_response(rq->trunk, NULL, 403, "CRCX", pdata->trans); |
| 867 | } |
| 868 | |
Philipp Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 869 | if (!mgcp_endp_avail(endp)) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 870 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_AVAIL)); |
Philipp Maier | a910a81 | 2020-08-18 15:13:33 +0200 | [diff] [blame] | 871 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 872 | "CRCX: selected endpoint not available!\n"); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 873 | return create_err_response(rq->trunk, NULL, 501, "CRCX", pdata->trans); |
Philipp Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 874 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 875 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 876 | /* parse CallID C: and LocalParameters L: */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 877 | for_each_line(line, pdata->save) { |
Philipp Maier | 036612b | 2021-07-19 17:47:49 +0200 | [diff] [blame] | 878 | if (!mgcp_check_param(endp, trunk, line)) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 879 | continue; |
| 880 | |
Pau Espin Pedrol | 0c6c3c1 | 2019-06-25 17:18:12 +0200 | [diff] [blame] | 881 | switch (toupper(line[0])) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 882 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 883 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 884 | break; |
| 885 | case 'C': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 886 | callid = (const char *)line + 3; |
| 887 | break; |
| 888 | case 'I': |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 889 | /* It is illegal to send a connection identifier |
| 890 | * together with a CRCX, the MGW will assign the |
| 891 | * connection identifier by itself on CRCX */ |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 892 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BAD_ACTION)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 893 | return create_err_response(rq->trunk, NULL, 523, "CRCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 894 | break; |
| 895 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 896 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 897 | break; |
| 898 | case 'X': |
Pau Espin Pedrol | c1bf469 | 2019-05-14 16:23:24 +0200 | [diff] [blame] | 899 | if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) { |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 900 | /* If osmux is disabled, just skip setting it up */ |
Pau Espin Pedrol | 36413c0 | 2022-10-12 17:58:17 +0200 | [diff] [blame] | 901 | if (rq->endp->trunk->cfg->osmux.usage == OSMUX_USAGE_OFF) |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 902 | break; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 903 | remote_osmux_cid = mgcp_osmux_setup(endp, line); |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 904 | break; |
| 905 | } |
| 906 | |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 907 | if (parse_x_osmo_ign(endp, line)) |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 908 | break; |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 909 | |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 910 | /* Ignore unknown X-headers */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 911 | break; |
| 912 | case '\0': |
| 913 | have_sdp = 1; |
| 914 | goto mgcp_header_done; |
| 915 | default: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 916 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 917 | "CRCX: unhandled option: '%c'/%d\n", *line, *line); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 918 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_UNHANDLED_PARAM)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 919 | return create_err_response(rq->trunk, NULL, 539, "CRCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 920 | break; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | mgcp_header_done: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 925 | /* Check parameters */ |
| 926 | if (!callid) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 927 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 928 | "CRCX: insufficient parameters, missing callid\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 929 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_MISSING_CALLID)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 930 | return create_err_response(endp, endp, 516, "CRCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 933 | if (!mode) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 934 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 935 | "CRCX: insufficient parameters, missing mode\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 936 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_INVALID_MODE)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 937 | return create_err_response(endp, endp, 517, "CRCX", pdata->trans); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 938 | } |
| 939 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 940 | /* Check if we are able to accept the creation of another connection */ |
| 941 | if (llist_count(&endp->conns) >= endp->type->max_conns) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 942 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 943 | "CRCX: endpoint full, max. %i connections allowed!\n", |
| 944 | endp->type->max_conns); |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 945 | if (trunk->force_realloc) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 946 | /* There is no more room for a connection, make some |
| 947 | * room by blindly tossing the oldest of the two two |
| 948 | * connections */ |
| 949 | mgcp_conn_free_oldest(endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 950 | } else { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 951 | /* There is no more room for a connection, leave |
| 952 | * everything as it is and return with an error */ |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 953 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_LIMIT_EXCEEDED)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 954 | return create_err_response(endp, endp, 540, "CRCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 958 | /* Check if this endpoint already serves a call, if so, check if the |
| 959 | * callids match up so that we are sure that this is our call */ |
| 960 | if (endp->callid && mgcp_verify_call_id(endp, callid)) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 961 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 962 | "CRCX: already seized by other call (%s)\n", |
| 963 | endp->callid); |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 964 | if (trunk->force_realloc) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 965 | /* This is not our call, toss everything by releasing |
| 966 | * the entire endpoint. (rude!) */ |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 967 | mgcp_endp_release(endp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 968 | else { |
| 969 | /* This is not our call, leave everything as it is and |
| 970 | * return with an error. */ |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 971 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_UNKNOWN_CALLID)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 972 | return create_err_response(endp, endp, 400, "CRCX", pdata->trans); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 976 | if (!endp->callid) { |
| 977 | /* Claim endpoint resources. This will also set the callid, |
| 978 | * creating additional connections will only be possible if |
| 979 | * the callid matches up (see above). */ |
| 980 | rc = mgcp_endp_claim(endp, callid); |
| 981 | if (rc != 0) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 982 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_CLAIM)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 983 | return create_err_response(endp, endp, 502, "CRCX", pdata->trans); |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 984 | } |
| 985 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 986 | |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 987 | snprintf(conn_name, sizeof(conn_name), "%s", callid); |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 988 | _conn = mgcp_conn_alloc(trunk->endpoints, endp, MGCP_CONN_TYPE_RTP, conn_name); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 989 | if (!_conn) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 990 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 991 | "CRCX: unable to allocate RTP connection\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 992 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_ALLOC_CONN)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 993 | goto error2; |
| 994 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 995 | } |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 996 | |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 997 | conn = mgcp_conn_get_rtp(endp, _conn->id); |
| 998 | OSMO_ASSERT(conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 999 | |
| 1000 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 1001 | error_code = 517; |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1002 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_INVALID_MODE)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1003 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 1006 | /* If X-Osmux (remote CID) was received (-1 is wilcard), alloc next avail CID as local CID */ |
| 1007 | if (remote_osmux_cid >= -1) { |
Pau Espin Pedrol | d48a811 | 2022-09-27 12:37:53 +0200 | [diff] [blame] | 1008 | if (osmux_init_conn(conn) < 0) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1009 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_NO_OSMUX)); |
Pau Espin Pedrol | 2b89617 | 2019-04-24 13:47:23 +0200 | [diff] [blame] | 1010 | goto error2; |
| 1011 | } |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 1012 | if (remote_osmux_cid >= 0) { |
| 1013 | conn->osmux.remote_cid_present = true; |
| 1014 | conn->osmux.remote_cid = remote_osmux_cid; |
| 1015 | } |
Pau Espin Pedrol | 36413c0 | 2022-10-12 17:58:17 +0200 | [diff] [blame] | 1016 | } else if (endp->trunk->cfg->osmux.usage == OSMUX_USAGE_ONLY) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1017 | LOGPCONN(_conn, DLMGCP, LOGL_ERROR, |
| 1018 | "CRCX: osmux only and no osmux offered\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1019 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_NO_OSMUX)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1020 | goto error2; |
| 1021 | } |
| 1022 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1023 | /* Set local connection options, if present */ |
| 1024 | if (local_options) { |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1025 | rc = set_local_cx_options(trunk->endpoints, |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1026 | &endp->local_options, local_options); |
| 1027 | if (rc != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1028 | LOGPCONN(_conn, DLMGCP, LOGL_ERROR, |
Oliver Smith | 169d50e | 2023-01-24 13:12:54 +0100 | [diff] [blame] | 1029 | "CRCX: invalid local connection options!\n"); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1030 | error_code = rc; |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1031 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1032 | goto error2; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | /* Handle codec information and decide for a suitable codec */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1037 | rc = handle_codec_info(conn, rq, have_sdp, true); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1038 | mgcp_codec_summary(conn); |
| 1039 | if (rc) { |
| 1040 | error_code = rc; |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1041 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_CODEC_NEGOTIATION)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1042 | goto error2; |
| 1043 | } |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 1044 | /* Upgrade the conn type RTP_DEFAULT->RTP_IUUP if needed based on requested codec: */ |
| 1045 | /* TODO: "codec" probably needs to be moved from endp to conn */ |
| 1046 | if (conn->type == MGCP_RTP_DEFAULT && strcmp(conn->end.codec->subtype_name, "VND.3GPP.IUFP") == 0) { |
| 1047 | rc = mgcp_conn_iuup_init(conn); |
| 1048 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1049 | |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1050 | conn->end.fmtp_extra = talloc_strdup(trunk->endpoints, |
| 1051 | trunk->audio_fmtp_extra); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1052 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1053 | if (pdata->cfg->force_ptime) { |
| 1054 | conn->end.packet_duration_ms = pdata->cfg->force_ptime; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1055 | conn->end.force_output_ptime = 1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1056 | } |
| 1057 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 1058 | mgcp_rtp_end_config(endp, 0, &conn->end); |
| 1059 | |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1060 | /* check connection mode setting */ |
| 1061 | if (conn->conn->mode != MGCP_CONN_LOOPBACK |
| 1062 | && conn->conn->mode != MGCP_CONN_RECV_ONLY |
Pau Espin Pedrol | 5ffd127 | 2022-10-04 13:45:48 +0200 | [diff] [blame] | 1063 | && osmo_sockaddr_port(&conn->end.addr.u.sa) == 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1064 | LOGPCONN(_conn, DLMGCP, LOGL_ERROR, |
| 1065 | "CRCX: selected connection mode type requires an opposite end!\n"); |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1066 | error_code = 527; |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1067 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC)); |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1068 | goto error2; |
| 1069 | } |
| 1070 | |
Pau Espin Pedrol | 71d42e7 | 2020-09-03 14:20:07 +0200 | [diff] [blame] | 1071 | /* Find a local address for conn based on policy and initial SDP remote |
| 1072 | information, then find a free port for it */ |
Pau Espin Pedrol | 70c03f5 | 2022-10-04 16:49:41 +0200 | [diff] [blame] | 1073 | if (mgcp_get_local_addr(conn->end.local_addr, conn) < 0) { |
| 1074 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BIND_PORT)); |
| 1075 | goto error2; |
| 1076 | } |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 1077 | if (allocate_port(endp, conn) != 0) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1078 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BIND_PORT)); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 1079 | goto error2; |
| 1080 | } |
| 1081 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1082 | if (setup_rtp_processing(endp, conn) != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1083 | LOGPCONN(_conn, DLMGCP, LOGL_ERROR, |
| 1084 | "CRCX: could not start RTP processing!\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1085 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_START_RTP)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1086 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1087 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1088 | |
Pau Espin Pedrol | 833281d | 2022-10-06 17:41:22 +0200 | [diff] [blame] | 1089 | /* Notify Osmux conn that CRCX was received */ |
| 1090 | if (mgcp_conn_rtp_is_osmux(conn)) { |
| 1091 | if (conn_osmux_event_rx_crcx_mdcx(conn) < 0) { |
| 1092 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, "CRCX: Osmux handling failed!\n"); |
| 1093 | goto error2; |
| 1094 | } |
| 1095 | } |
| 1096 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1097 | LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, |
| 1098 | "CRCX: Creating connection: port: %u\n", conn->end.local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1099 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1100 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1101 | OSMO_ASSERT(trunk->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
Pau Espin Pedrol | ca280a1 | 2021-07-06 18:15:35 +0200 | [diff] [blame] | 1102 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY && |
Pau Espin Pedrol | ca280a1 | 2021-07-06 18:15:35 +0200 | [diff] [blame] | 1103 | trunk->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1104 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1105 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1106 | LOGPCONN(_conn, DLMGCP, LOGL_NOTICE, |
| 1107 | "CRCX: connection successfully created\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1108 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_SUCCESS)); |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 1109 | mgcp_endp_update(endp); |
Philipp Maier | 41d5920 | 2021-07-20 15:49:00 +0200 | [diff] [blame] | 1110 | |
| 1111 | /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */ |
| 1112 | bool add_epname = rq->wildcarded && trunk->trunk_type == MGCP_TRUNK_VIRTUAL; |
| 1113 | return create_response_with_sdp(endp, conn, "CRCX", pdata->trans, add_epname, true); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1114 | error2: |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 1115 | mgcp_endp_release(endp); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1116 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1117 | "CRCX: unable to create connection\n"); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1118 | return create_err_response(endp, endp, error_code, "CRCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1119 | } |
| 1120 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1121 | /* MDCX command handler, processes the received command */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1122 | static struct msgb *handle_modify_con(struct mgcp_request_data *rq) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1123 | { |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1124 | struct mgcp_parse_data *pdata = rq->pdata; |
| 1125 | struct mgcp_trunk *trunk = rq->trunk; |
| 1126 | struct mgcp_endpoint *endp = rq->endp; |
| 1127 | struct rate_ctr_group *rate_ctrs = trunk->ratectr.mgcp_mdcx_ctr_group; |
Pau Espin Pedrol | fbbe8f2 | 2020-09-03 14:49:56 +0200 | [diff] [blame] | 1128 | char new_local_addr[INET6_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1129 | int error_code = 500; |
| 1130 | int silent = 0; |
| 1131 | int have_sdp = 0; |
| 1132 | char *line; |
| 1133 | const char *local_options = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1134 | const char *mode = NULL; |
| 1135 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1136 | const char *conn_id = NULL; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 1137 | int remote_osmux_cid = -2; |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 1138 | int rc; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1139 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1140 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1141 | |
Philipp Maier | 5656fbf | 2018-02-02 14:41:58 +0100 | [diff] [blame] | 1142 | /* Prohibit wildcarded requests */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1143 | if (rq->wildcarded) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1144 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1145 | "MDCX: wildcarded endpoint names not supported.\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1146 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_WILDCARD)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1147 | return create_err_response(rq->trunk, endp, 507, "MDCX", pdata->trans); |
Philipp Maier | 5656fbf | 2018-02-02 14:41:58 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Eric | ee6958c | 2021-09-14 18:36:53 +0200 | [diff] [blame] | 1150 | if (!endp || !mgcp_endp_avail(endp)) { |
| 1151 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_AVAIL)); |
| 1152 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "MDCX: selected endpoint not available!\n"); |
| 1153 | return create_err_response(rq->trunk, NULL, 501, "MDCX", pdata->trans); |
| 1154 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1155 | if (llist_count(&endp->conns) <= 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1156 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1157 | "MDCX: endpoint is not holding a connection.\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1158 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_NO_CONN)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1159 | return create_err_response(endp, endp, 400, "MDCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1160 | } |
| 1161 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1162 | for_each_line(line, pdata->save) { |
Philipp Maier | 036612b | 2021-07-19 17:47:49 +0200 | [diff] [blame] | 1163 | if (!mgcp_check_param(endp, trunk, line)) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1164 | continue; |
| 1165 | |
Pau Espin Pedrol | 0c6c3c1 | 2019-06-25 17:18:12 +0200 | [diff] [blame] | 1166 | switch (toupper(line[0])) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1167 | case 'C': |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1168 | if (mgcp_verify_call_id(endp, line + 3) != 0) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1169 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_INVALID_CALLID)); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1170 | error_code = 516; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1171 | goto error3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1172 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1173 | break; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1174 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1175 | conn_id = (const char *)line + 3; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1176 | if ((error_code = mgcp_verify_ci(endp, conn_id))) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1177 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_INVALID_CONNID)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1178 | goto error3; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1179 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1180 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1181 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1182 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1183 | break; |
| 1184 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1185 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1186 | break; |
| 1187 | case 'Z': |
Pau Espin Pedrol | 9b508f6 | 2019-06-26 13:11:22 +0200 | [diff] [blame] | 1188 | silent = strcasecmp("noanswer", line + 3) == 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1189 | break; |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1190 | case 'X': |
Pau Espin Pedrol | c1bf469 | 2019-05-14 16:23:24 +0200 | [diff] [blame] | 1191 | if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) { |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1192 | /* If osmux is disabled, just skip setting it up */ |
Pau Espin Pedrol | 36413c0 | 2022-10-12 17:58:17 +0200 | [diff] [blame] | 1193 | if (endp->trunk->cfg->osmux.usage == OSMUX_USAGE_OFF) |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1194 | break; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 1195 | remote_osmux_cid = mgcp_osmux_setup(endp, line); |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1196 | break; |
| 1197 | } |
| 1198 | /* Ignore unknown X-headers */ |
| 1199 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1200 | case '\0': |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1201 | have_sdp = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1202 | goto mgcp_header_done; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1203 | break; |
| 1204 | default: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1205 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1206 | "MDCX: Unhandled MGCP option: '%c'/%d\n", |
| 1207 | line[0], line[0]); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1208 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_UNHANDLED_PARAM)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1209 | return create_err_response(rq->trunk, NULL, 539, "MDCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1210 | break; |
| 1211 | } |
| 1212 | } |
| 1213 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1214 | mgcp_header_done: |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1215 | if (!conn_id) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1216 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1217 | "MDCX: insufficient parameters, missing ci (connectionIdentifier)\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1218 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_NO_CONNID)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1219 | return create_err_response(endp, endp, 515, "MDCX", pdata->trans); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | conn = mgcp_conn_get_rtp(endp, conn_id); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1223 | if (!conn) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1224 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_CONN_NOT_FOUND)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1225 | return create_err_response(endp, endp, 400, "MDCX", pdata->trans); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1226 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1227 | |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 1228 | mgcp_conn_watchdog_kick(conn->conn); |
| 1229 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1230 | if (mode) { |
| 1231 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1232 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_INVALID_MODE)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1233 | error_code = 517; |
| 1234 | goto error3; |
| 1235 | } |
| 1236 | } else |
Pau Espin Pedrol | 209eb9f | 2019-04-24 12:03:04 +0200 | [diff] [blame] | 1237 | conn->conn->mode = conn->conn->mode_orig; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1238 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1239 | /* Set local connection options, if present */ |
| 1240 | if (local_options) { |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1241 | rc = set_local_cx_options(trunk->endpoints, |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1242 | &endp->local_options, local_options); |
| 1243 | if (rc != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1244 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1245 | "MDCX: invalid local connection options!\n"); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1246 | error_code = rc; |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1247 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS)); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1248 | goto error3; |
| 1249 | } |
| 1250 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1251 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1252 | /* Handle codec information and decide for a suitable codec */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1253 | rc = handle_codec_info(conn, rq, have_sdp, false); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1254 | mgcp_codec_summary(conn); |
| 1255 | if (rc) { |
Philipp Maier | af07f66 | 2018-02-02 11:34:02 +0100 | [diff] [blame] | 1256 | error_code = rc; |
| 1257 | goto error3; |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 1258 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1259 | |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1260 | /* check connection mode setting */ |
| 1261 | if (conn->conn->mode != MGCP_CONN_LOOPBACK |
| 1262 | && conn->conn->mode != MGCP_CONN_RECV_ONLY |
Pau Espin Pedrol | d6769ea | 2021-07-06 19:44:27 +0200 | [diff] [blame] | 1263 | && !mgcp_rtp_end_remote_addr_available(&conn->end)) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1264 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1265 | "MDCX: selected connection mode type requires an opposite end!\n"); |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1266 | error_code = 527; |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1267 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC)); |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1268 | goto error3; |
| 1269 | } |
| 1270 | |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1271 | if (mgcp_conn_rtp_is_osmux(conn)) { |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 1272 | OSMO_ASSERT(conn->osmux.local_cid_allocated); |
| 1273 | if (remote_osmux_cid < -1) { |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1274 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1275 | "MDCX: Failed to parse Osmux CID!\n"); |
| 1276 | goto error3; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 1277 | } else if (remote_osmux_cid == -1) { |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1278 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1279 | "MDCX: wilcard in MDCX is not supported!\n"); |
| 1280 | goto error3; |
Pau Espin Pedrol | 2177919 | 2022-09-23 16:46:33 +0200 | [diff] [blame] | 1281 | } else if (conn->osmux.remote_cid_present && |
Pau Espin Pedrol | 833281d | 2022-10-06 17:41:22 +0200 | [diff] [blame] | 1282 | remote_osmux_cid != conn->osmux.remote_cid) { |
| 1283 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1284 | "MDCX: changing already allocated CID is not supported!\n"); |
| 1285 | goto error3; |
| 1286 | } else { |
| 1287 | conn->osmux.remote_cid_present = true; |
| 1288 | conn->osmux.remote_cid = remote_osmux_cid; |
| 1289 | if (conn_osmux_event_rx_crcx_mdcx(conn) < 0) { |
| 1290 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1291 | "MDCX: Osmux handling failed!\n"); |
| 1292 | goto error3; |
| 1293 | } |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1294 | } |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1295 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1296 | |
Pau Espin Pedrol | fbbe8f2 | 2020-09-03 14:49:56 +0200 | [diff] [blame] | 1297 | /* MDCX may have provided a new remote address, which means we may need |
| 1298 | to update our announced IP addr and re-bind our local end. This can |
| 1299 | happen for instance if MGW initially provided an IPv4 during CRCX |
| 1300 | ACK, and now MDCX tells us the remote has an IPv6 address. */ |
Pau Espin Pedrol | 70c03f5 | 2022-10-04 16:49:41 +0200 | [diff] [blame] | 1301 | if (mgcp_get_local_addr(new_local_addr, conn) < 0) { |
| 1302 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BIND_PORT)); |
| 1303 | goto error3; |
| 1304 | } |
Pau Espin Pedrol | fbbe8f2 | 2020-09-03 14:49:56 +0200 | [diff] [blame] | 1305 | if (strcmp(new_local_addr, conn->end.local_addr)) { |
| 1306 | osmo_strlcpy(conn->end.local_addr, new_local_addr, sizeof(conn->end.local_addr)); |
| 1307 | mgcp_free_rtp_port(&conn->end); |
| 1308 | if (allocate_port(endp, conn) != 0) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1309 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BIND_PORT)); |
Pau Espin Pedrol | fbbe8f2 | 2020-09-03 14:49:56 +0200 | [diff] [blame] | 1310 | goto error3; |
| 1311 | } |
| 1312 | } |
| 1313 | |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1314 | if (setup_rtp_processing(endp, conn) != 0) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1315 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_START_RTP)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1316 | goto error3; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1317 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1318 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1319 | mgcp_rtp_end_config(endp, 1, &conn->end); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1320 | |
| 1321 | /* modify */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1322 | LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, |
| 1323 | "MDCX: modified conn:%s\n", mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1324 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1325 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1326 | OSMO_ASSERT(trunk->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
Pau Espin Pedrol | ca280a1 | 2021-07-06 18:15:35 +0200 | [diff] [blame] | 1327 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY && |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1328 | trunk->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1329 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1330 | |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1331 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_SUCCESS)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1332 | if (silent) |
| 1333 | goto out_silent; |
| 1334 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1335 | LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE, |
| 1336 | "MDCX: connection successfully modified\n"); |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 1337 | mgcp_endp_update(endp); |
Philipp Maier | 41d5920 | 2021-07-20 15:49:00 +0200 | [diff] [blame] | 1338 | return create_response_with_sdp(endp, conn, "MDCX", pdata->trans, false, false); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1339 | error3: |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1340 | return create_err_response(endp, endp, error_code, "MDCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1341 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1342 | out_silent: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1343 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "MDCX: silent exit\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1344 | return NULL; |
| 1345 | } |
| 1346 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1347 | /* DLCX command handler, processes the received command */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1348 | static struct msgb *handle_delete_con(struct mgcp_request_data *rq) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1349 | { |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1350 | struct mgcp_parse_data *pdata = rq->pdata; |
| 1351 | struct mgcp_trunk *trunk = rq->trunk; |
| 1352 | struct mgcp_endpoint *endp = rq->endp; |
| 1353 | struct rate_ctr_group *rate_ctrs = trunk->ratectr.mgcp_dlcx_ctr_group; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1354 | int error_code = 400; |
| 1355 | int silent = 0; |
| 1356 | char *line; |
| 1357 | char stats[1048]; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1358 | const char *conn_id = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1359 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1360 | unsigned int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1361 | |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1362 | /* NOTE: In this handler we can not take it for granted that the endp |
| 1363 | * pointer will be populated, however a trunk is always guaranteed. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1364 | |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1365 | LOGPEPTR(endp, trunk, DLMGCP, LOGL_NOTICE, "DLCX: deleting connection(s) ...\n"); |
| 1366 | |
| 1367 | if (endp && !mgcp_endp_avail(endp)) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1368 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_AVAIL)); |
Philipp Maier | a910a81 | 2020-08-18 15:13:33 +0200 | [diff] [blame] | 1369 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1370 | "DLCX: selected endpoint not available!\n"); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1371 | return create_err_response(rq->trunk, NULL, 501, "DLCX", pdata->trans); |
Philipp Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1374 | if (endp && !rq->wildcarded && llist_empty(&endp->conns)) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1375 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1376 | "DLCX: endpoint is not holding a connection.\n"); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1377 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_NO_CONN)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1378 | return create_err_response(endp, endp, 515, "DLCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1379 | } |
| 1380 | |
Eric | ee6958c | 2021-09-14 18:36:53 +0200 | [diff] [blame] | 1381 | /* Handle wildcarded DLCX that refers to the whole trunk. This means |
| 1382 | * that we walk over all endpoints on the trunk in order to drop all |
| 1383 | * connections on the trunk. (see also RFC3435 Annex F.7) */ |
| 1384 | if (rq->wildcarded) { |
| 1385 | int num_conns = 0; |
| 1386 | for (i = 0; i < trunk->number_endpoints; i++) { |
| 1387 | num_conns += llist_count(&trunk->endpoints[i]->conns); |
| 1388 | mgcp_endp_release(trunk->endpoints[i]); |
| 1389 | } |
| 1390 | rate_ctr_add(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_SUCCESS), num_conns); |
| 1391 | return create_ok_response(trunk, NULL, 200, "DLCX", pdata->trans); |
| 1392 | } |
| 1393 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1394 | for_each_line(line, pdata->save) { |
Philipp Maier | 036612b | 2021-07-19 17:47:49 +0200 | [diff] [blame] | 1395 | if (!mgcp_check_param(endp, trunk, line)) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1396 | continue; |
| 1397 | |
Pau Espin Pedrol | 0c6c3c1 | 2019-06-25 17:18:12 +0200 | [diff] [blame] | 1398 | switch (toupper(line[0])) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1399 | case 'C': |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1400 | /* If we have no endpoint, but a call id in the request, |
| 1401 | then this request cannot be handled */ |
| 1402 | if (!endp) { |
| 1403 | LOGPTRUNK(trunk, DLMGCP, LOGL_NOTICE, |
| 1404 | "cannot handle requests with call-id (C) without endpoint -- abort!"); |
| 1405 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_UNHANDLED_PARAM)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1406 | return create_err_response(rq->trunk, NULL, 539, "DLCX", pdata->trans); |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1407 | } |
| 1408 | |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1409 | if (mgcp_verify_call_id(endp, line + 3) != 0) { |
| 1410 | error_code = 516; |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1411 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_INVALID_CALLID)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1412 | goto error3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1413 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1414 | break; |
| 1415 | case 'I': |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1416 | /* If we have no endpoint, but a connection id in the request, |
| 1417 | then this request cannot be handled */ |
| 1418 | if (!endp) { |
| 1419 | LOGPTRUNK(trunk, DLMGCP, LOGL_NOTICE, |
| 1420 | "cannot handle requests with conn-id (I) without endpoint -- abort!"); |
| 1421 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_UNHANDLED_PARAM)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1422 | return create_err_response(rq->trunk, NULL, 539, "DLCX", pdata->trans); |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1425 | conn_id = (const char *)line + 3; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1426 | if ((error_code = mgcp_verify_ci(endp, conn_id))) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1427 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_INVALID_CONNID)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1428 | goto error3; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1429 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1430 | break; |
| 1431 | case 'Z': |
Pau Espin Pedrol | 9b508f6 | 2019-06-26 13:11:22 +0200 | [diff] [blame] | 1432 | silent = strcasecmp("noanswer", line + 3) == 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1433 | break; |
| 1434 | default: |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1435 | LOGPEPTR(endp, trunk, DLMGCP, LOGL_NOTICE, "DLCX: Unhandled MGCP option: '%c'/%d\n", |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1436 | line[0], line[0]); |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1437 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_UNHANDLED_PARAM)); |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1438 | return create_err_response(rq->trunk, NULL, 539, "DLCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1439 | break; |
| 1440 | } |
| 1441 | } |
| 1442 | |
Philipp Maier | ce18705 | 2021-07-23 10:58:19 +0200 | [diff] [blame] | 1443 | /* The logic does not permit to go past this point without having the |
| 1444 | * the endp pointer populated. */ |
| 1445 | OSMO_ASSERT(endp); |
| 1446 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1447 | /* When no connection id is supplied, we will interpret this as a |
Philipp Maier | f486e74 | 2021-07-19 14:56:29 +0200 | [diff] [blame] | 1448 | * wildcarded DLCX that refers to the selected endpoint. This means |
| 1449 | * that we drop all connections on that specific endpoint at once. |
| 1450 | * (See also RFC3435 Section F.7) */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1451 | if (!conn_id) { |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1452 | int num_conns = llist_count(&endp->conns); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1453 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1454 | "DLCX: missing ci (connectionIdentifier), will remove all connections (%d total) at once\n", |
| 1455 | num_conns); |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1456 | |
| 1457 | if (num_conns > 0) |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1458 | rate_ctr_add(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_SUCCESS), num_conns); |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1459 | |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 1460 | mgcp_endp_release(endp); |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1461 | |
| 1462 | /* Note: In this case we do not return any statistics, |
| 1463 | * as we assume that the client is not interested in |
| 1464 | * this case. */ |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1465 | return create_ok_response(endp, endp, 200, "DLCX", pdata->trans); |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1468 | /* Find the connection */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1469 | conn = mgcp_conn_get_rtp(endp, conn_id); |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1470 | if (!conn) { |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1471 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_INVALID_CONNID)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1472 | goto error3; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1473 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1474 | /* save the statistics of the current connection */ |
| 1475 | mgcp_format_stats(stats, sizeof(stats), conn->conn); |
| 1476 | |
| 1477 | /* delete connection */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1478 | LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, "DLCX: deleting conn:%s\n", |
| 1479 | mgcp_conn_dump(conn->conn)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1480 | mgcp_conn_free(endp, conn_id); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1481 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1482 | "DLCX: connection successfully deleted\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1483 | |
| 1484 | /* When all connections are closed, the endpoint will be released |
| 1485 | * in order to be ready to be used by another call. */ |
| 1486 | if (llist_count(&endp->conns) <= 0) { |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 1487 | mgcp_endp_release(endp); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1488 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: endpoint released\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1489 | } |
| 1490 | |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 1491 | rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_SUCCESS)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1492 | if (silent) |
| 1493 | goto out_silent; |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1494 | return create_ok_resp_with_param(endp, endp, 250, "DLCX", pdata->trans, stats); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1495 | |
| 1496 | error3: |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1497 | return create_err_response(endp, endp, error_code, "DLCX", pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1498 | |
| 1499 | out_silent: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1500 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: silent exit\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1501 | return NULL; |
| 1502 | } |
| 1503 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1504 | /* RSIP command handler, processes the received command */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1505 | static struct msgb *handle_rsip(struct mgcp_request_data *rq) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1506 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1507 | /* TODO: Also implement the resetting of a specific endpoint |
| 1508 | * to make mgcp_send_reset_ep() work. Currently this will call |
| 1509 | * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1 |
| 1510 | * to make read_call_agent() reset all endpoints when called |
| 1511 | * next time. In order to selectively reset endpoints some |
| 1512 | * mechanism to distinguish which endpoint shall be resetted |
| 1513 | * is needed */ |
| 1514 | |
| 1515 | LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n"); |
| 1516 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1517 | if (rq->pdata->cfg->reset_cb) |
| 1518 | rq->pdata->cfg->reset_cb(rq->endp->trunk); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1519 | return NULL; |
| 1520 | } |
| 1521 | |
| 1522 | static char extract_tone(const char *line) |
| 1523 | { |
| 1524 | const char *str = strstr(line, "D/"); |
| 1525 | if (!str) |
| 1526 | return CHAR_MAX; |
| 1527 | |
| 1528 | return str[2]; |
| 1529 | } |
| 1530 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1531 | /* This can request like DTMF detection and forward, fax detection... it |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1532 | * 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] | 1533 | * do this right now. */ |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1534 | static struct msgb *handle_noti_req(struct mgcp_request_data *rq) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1535 | { |
| 1536 | int res = 0; |
| 1537 | char *line; |
| 1538 | char tone = CHAR_MAX; |
| 1539 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1540 | LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n"); |
| 1541 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1542 | for_each_line(line, rq->pdata->save) { |
Pau Espin Pedrol | 0c6c3c1 | 2019-06-25 17:18:12 +0200 | [diff] [blame] | 1543 | switch (toupper(line[0])) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1544 | case 'S': |
| 1545 | tone = extract_tone(line); |
| 1546 | break; |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | /* we didn't see a signal request with a tone */ |
| 1551 | if (tone == CHAR_MAX) |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1552 | return create_ok_response(rq->endp, rq->endp, 200, "RQNT", rq->pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1553 | |
Philipp Maier | 8dc3597 | 2021-07-14 11:20:16 +0200 | [diff] [blame] | 1554 | if (rq->pdata->cfg->rqnt_cb) |
| 1555 | res = rq->pdata->cfg->rqnt_cb(rq->endp, tone); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1556 | |
Eric | fbcf4a6 | 2021-09-09 18:02:31 +0200 | [diff] [blame] | 1557 | return res == 0 ? create_ok_response(rq->endp, rq->endp, 200, "RQNT", rq->pdata->trans) : |
| 1558 | create_err_response(rq->endp, rq->endp, res, "RQNT", rq->pdata->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1561 | /* Connection keepalive timer, will take care that dummy packets are send |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 1562 | * regularly, so that NAT connections stay open */ |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1563 | static void mgcp_keepalive_timer_cb(void *_trunk) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1564 | { |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1565 | struct mgcp_trunk *trunk = _trunk; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1566 | struct mgcp_conn *conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1567 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1568 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1569 | LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n", |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1570 | trunk->trunk_nr); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1571 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1572 | /* Do not accept invalid configuration values |
| 1573 | * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and |
| 1574 | * values greater 0 */ |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1575 | OSMO_ASSERT(trunk->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1576 | |
| 1577 | /* The dummy packet functionality has been disabled, we will exit |
| 1578 | * immediately, no further timer is scheduled, which means we will no |
| 1579 | * longer send dummy packets even when we did before */ |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1580 | if (trunk->keepalive_interval == MGCP_KEEPALIVE_NEVER) |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1581 | return; |
| 1582 | |
| 1583 | /* In cases where only one dummy packet is sent, we do not need |
| 1584 | * the timer since the functions that handle the CRCX and MDCX are |
| 1585 | * triggering the sending of the dummy packet. So we behave like in |
| 1586 | * the MGCP_KEEPALIVE_NEVER case */ |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1587 | if (trunk->keepalive_interval == MGCP_KEEPALIVE_ONCE) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1588 | return; |
| 1589 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1590 | /* Send walk over all endpoints and send out dummy packets through |
| 1591 | * every connection present on each endpoint */ |
Philipp Maier | 4131a65 | 2021-07-07 14:04:34 +0200 | [diff] [blame] | 1592 | for (i = 0; i < trunk->number_endpoints; ++i) { |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 1593 | struct mgcp_endpoint *endp = trunk->endpoints[i]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1594 | llist_for_each_entry(conn, &endp->conns, entry) { |
Pau Espin Pedrol | ca280a1 | 2021-07-06 18:15:35 +0200 | [diff] [blame] | 1595 | if (conn->type == MGCP_CONN_TYPE_RTP && |
Pau Espin Pedrol | 2ca4882 | 2022-10-06 10:58:10 +0200 | [diff] [blame] | 1596 | conn->mode == MGCP_CONN_RECV_ONLY) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1597 | send_dummy(endp, &conn->u.rtp); |
| 1598 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1599 | } |
| 1600 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1601 | /* Schedule the keepalive timer for the next round */ |
| 1602 | LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n", |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1603 | trunk->trunk_nr); |
| 1604 | osmo_timer_schedule(&trunk->keepalive_timer, trunk->keepalive_interval, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1605 | 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1606 | } |
| 1607 | |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1608 | void mgcp_trunk_set_keepalive(struct mgcp_trunk *trunk, int interval) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1609 | { |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1610 | trunk->keepalive_interval = interval; |
| 1611 | osmo_timer_setup(&trunk->keepalive_timer, mgcp_keepalive_timer_cb, trunk); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1612 | |
| 1613 | if (interval <= 0) |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1614 | osmo_timer_del(&trunk->keepalive_timer); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1615 | else |
Philipp Maier | 14b27a8 | 2020-06-02 20:15:30 +0200 | [diff] [blame] | 1616 | osmo_timer_schedule(&trunk->keepalive_timer, |
| 1617 | trunk->keepalive_interval, 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1618 | } |
| 1619 | |
Philipp Maier | 38533ba | 2021-07-29 17:38:34 +0200 | [diff] [blame] | 1620 | /* Free config, this function is automatically called by talloc_free when the configuration is freed. */ |
| 1621 | static int config_free_talloc_destructor(struct mgcp_config *cfg) |
| 1622 | { |
| 1623 | mgcp_ratectr_global_free(cfg); |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1627 | /*! allocate configuration with default values. |
| 1628 | * (called once at startup by main function) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1629 | struct mgcp_config *mgcp_config_alloc(void) |
| 1630 | { |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 1631 | /* FIXME: This is unrelated to the protocol, put this in some |
| 1632 | * appropiate place! */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1633 | struct mgcp_config *cfg; |
| 1634 | |
| 1635 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 1636 | if (!cfg) { |
| 1637 | LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 1638 | return NULL; |
| 1639 | } |
| 1640 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1641 | osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain)); |
| 1642 | |
Eric | 55fdfc2 | 2021-08-13 00:14:18 +0200 | [diff] [blame] | 1643 | cfg->net_ports.lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1644 | cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START; |
| 1645 | cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END; |
| 1646 | cfg->net_ports.last_port = cfg->net_ports.range_start; |
| 1647 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1648 | cfg->source_port = 2427; |
Eric | 2764bdb | 2021-08-23 22:11:47 +0200 | [diff] [blame] | 1649 | osmo_strlcpy(cfg->source_addr, "0.0.0.0", sizeof(cfg->source_addr)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1650 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1651 | cfg->rtp_processing_cb = &mgcp_rtp_processing_default; |
| 1652 | cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default; |
| 1653 | |
| 1654 | cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default; |
| 1655 | |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 1656 | INIT_LLIST_HEAD(&cfg->trunks); |
| 1657 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 1658 | /* Allocate virtual trunk */ |
Philipp Maier | d19de2e | 2020-06-03 13:55:33 +0200 | [diff] [blame] | 1659 | if (!mgcp_trunk_alloc(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID)) { |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 1660 | talloc_free(cfg); |
| 1661 | return NULL; |
| 1662 | } |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 1663 | |
Philipp Maier | a065e63 | 2021-07-09 13:22:42 +0200 | [diff] [blame] | 1664 | mgcp_ratectr_global_alloc(cfg); |
Philipp Maier | 38533ba | 2021-07-29 17:38:34 +0200 | [diff] [blame] | 1665 | talloc_set_destructor(cfg, config_free_talloc_destructor); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1666 | |
| 1667 | return cfg; |
| 1668 | } |
| 1669 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1670 | static int send_agent(struct mgcp_config *cfg, const char *buf, int len) |
| 1671 | { |
| 1672 | return write(cfg->gw_fd.bfd.fd, buf, len); |
| 1673 | } |
| 1674 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1675 | /*! Reset all endpoints by sending RSIP message to self. |
| 1676 | * (called by VTY) |
| 1677 | * \param[in] endp trunk endpoint |
| 1678 | * \param[in] endpoint number |
| 1679 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1680 | int mgcp_send_reset_all(struct mgcp_config *cfg) |
| 1681 | { |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1682 | char buf[MGCP_ENDPOINT_MAXLEN + 128]; |
| 1683 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1684 | int rc; |
| 1685 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1686 | len = snprintf(buf, sizeof(buf), |
| 1687 | "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain); |
| 1688 | if (len < 0) |
| 1689 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1690 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1691 | rc = send_agent(cfg, buf, len); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1692 | if (rc <= 0) |
| 1693 | return -1; |
| 1694 | |
| 1695 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1696 | } |
| 1697 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1698 | /*! Reset a single endpoint by sending RSIP message to self. |
| 1699 | * (called by VTY) |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 1700 | * \param[in] endp to reset |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1701 | * \returns 0 on success, -1 on error */ |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 1702 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1703 | { |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1704 | char buf[MGCP_ENDPOINT_MAXLEN + 128]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1705 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1706 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1707 | |
| 1708 | len = snprintf(buf, sizeof(buf), |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 1709 | "RSIP 39 %s MGCP 1.0\r\n", endp->name); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1710 | if (len < 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1711 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1712 | |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 1713 | rc = send_agent(endp->trunk->cfg, buf, len); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1714 | if (rc <= 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1715 | return -1; |
| 1716 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1717 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1718 | } |