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