Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1 | /* Message connection list handling */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * Author: Philipp Maier |
| 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 | |
Eric | a94c56e | 2021-09-07 00:10:31 +0200 | [diff] [blame] | 24 | #include <stdatomic.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 25 | #include <osmocom/mgcp/mgcp_conn.h> |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 26 | #include <osmocom/mgcp/mgcp_network.h> |
| 27 | #include <osmocom/mgcp/mgcp_protocol.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 28 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 29 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 30 | #include <osmocom/mgcp/mgcp_trunk.h> |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 31 | #include <osmocom/mgcp/mgcp_sdp.h> |
| 32 | #include <osmocom/mgcp/mgcp_codec.h> |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 33 | #include <osmocom/mgcp/mgcp_iuup.h> |
| 34 | |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 35 | #include <osmocom/gsm/gsm_utils.h> |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 36 | #include <osmocom/core/rate_ctr.h> |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 37 | #include <osmocom/core/timer.h> |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 38 | #include <ctype.h> |
| 39 | |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 40 | const static struct rate_ctr_group_desc rate_ctr_group_desc = { |
| 41 | .group_name_prefix = "conn_rtp", |
| 42 | .group_description = "rtp connection statistics", |
| 43 | .class_id = 1, |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 44 | .num_ctr = ARRAY_SIZE(mgcp_conn_rate_ctr_desc), |
| 45 | .ctr_desc = mgcp_conn_rate_ctr_desc |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 49 | /* Allocate a new connection identifier. According to RFC3435, they must |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 50 | * be unique only within the scope of the endpoint. (Caller must provide |
| 51 | * memory for id) */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 52 | static int mgcp_alloc_id(struct mgcp_endpoint *endp, char *id) |
| 53 | { |
Neels Hofmeyr | a729db6 | 2018-08-28 16:20:51 +0200 | [diff] [blame] | 54 | #define MGCP_CONN_ID_GEN_LEN 8 |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 55 | int i; |
| 56 | int k; |
| 57 | int rc; |
Neels Hofmeyr | a729db6 | 2018-08-28 16:20:51 +0200 | [diff] [blame] | 58 | uint8_t id_bin[MGCP_CONN_ID_GEN_LEN / 2]; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 59 | char *id_hex; |
| 60 | |
| 61 | /* Generate a connection id that is unique for the current endpoint. |
| 62 | * Technically a counter would be sufficient, but in order to |
| 63 | * be able to find a specific connection in large logfiles and to |
| 64 | * prevent unintentional connections we assign the connection |
| 65 | * identifiers randomly from a reasonable large number space */ |
| 66 | for (i = 0; i < 32; i++) { |
| 67 | rc = osmo_get_rand_id(id_bin, sizeof(id_bin)); |
| 68 | if (rc < 0) |
| 69 | return rc; |
| 70 | |
| 71 | id_hex = osmo_hexdump_nospc(id_bin, sizeof(id_bin)); |
| 72 | for (k = 0; k < strlen(id_hex); k++) |
| 73 | id_hex[k] = toupper(id_hex[k]); |
| 74 | |
| 75 | /* ensure that the generated conn_id is unique |
| 76 | * for this endpoint */ |
| 77 | if (!mgcp_conn_get_rtp(endp, id_hex)) { |
Neels Hofmeyr | 55e0dcf | 2018-09-03 21:36:56 +0200 | [diff] [blame] | 78 | osmo_strlcpy(id, id_hex, MGCP_CONN_ID_MAXLEN); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | } |
| 82 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 83 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "unable to generate a unique connectionIdentifier\n"); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 84 | |
| 85 | return -1; |
| 86 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 87 | |
Philipp Maier | c430d19 | 2018-03-16 12:11:18 +0100 | [diff] [blame] | 88 | /* Initialize rtp connection struct with default values */ |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 89 | static int mgcp_rtp_conn_init(struct mgcp_conn_rtp *conn_rtp, struct mgcp_conn *conn) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 90 | { |
Philipp Maier | 892dec0 | 2018-03-16 12:32:01 +0100 | [diff] [blame] | 91 | struct mgcp_rtp_end *end = &conn_rtp->end; |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 92 | /* FIXME: Each new rate counter group requires an unique index. At the |
| 93 | * moment we generate this index using this counter, but perhaps there |
| 94 | * is a more concious way to assign the indexes. */ |
Eric | a94c56e | 2021-09-07 00:10:31 +0200 | [diff] [blame] | 95 | static atomic_uint rate_ctr_index = 0; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 96 | |
Philipp Maier | 892dec0 | 2018-03-16 12:32:01 +0100 | [diff] [blame] | 97 | conn_rtp->type = MGCP_RTP_DEFAULT; |
Pau Espin Pedrol | 8de58e7 | 2019-04-24 13:33:46 +0200 | [diff] [blame] | 98 | conn_rtp->osmux.cid_allocated = false; |
| 99 | conn_rtp->osmux.cid = 0; |
Philipp Maier | 892dec0 | 2018-03-16 12:32:01 +0100 | [diff] [blame] | 100 | |
| 101 | /* backpointer to the generic part of the connection */ |
| 102 | conn->u.rtp.conn = conn; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 103 | |
| 104 | end->rtp.fd = -1; |
| 105 | end->rtcp.fd = -1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 106 | end->rtp_port = end->rtcp_port = 0; |
| 107 | talloc_free(end->fmtp_extra); |
| 108 | end->fmtp_extra = NULL; |
| 109 | |
| 110 | /* Set default values */ |
| 111 | end->frames_per_packet = 0; /* unknown */ |
| 112 | end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS; |
Pau Espin Pedrol | 2c40164 | 2021-12-24 14:48:26 +0100 | [diff] [blame] | 113 | end->output_enabled = false; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 114 | end->maximum_packet_time = -1; |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 115 | |
Eric | a94c56e | 2021-09-07 00:10:31 +0200 | [diff] [blame] | 116 | conn_rtp->rate_ctr_group = rate_ctr_group_alloc(conn, &rate_ctr_group_desc, rate_ctr_index++); |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 117 | if (!conn_rtp->rate_ctr_group) |
| 118 | return -1; |
| 119 | |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 120 | conn_rtp->state.in_stream.err_ts_ctr = rate_ctr_group_get_ctr(conn_rtp->rate_ctr_group, IN_STREAM_ERR_TSTMP_CTR); |
| 121 | conn_rtp->state.out_stream.err_ts_ctr = rate_ctr_group_get_ctr(conn_rtp->rate_ctr_group, OUT_STREAM_ERR_TSTMP_CTR); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 122 | |
| 123 | /* Make sure codec table is reset */ |
| 124 | mgcp_codec_reset_all(conn_rtp); |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 125 | |
| 126 | return 0; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Philipp Maier | d0b470d | 2018-03-16 12:45:53 +0100 | [diff] [blame] | 129 | /* Cleanup rtp connection struct */ |
| 130 | static void mgcp_rtp_conn_cleanup(struct mgcp_conn_rtp *conn_rtp) |
| 131 | { |
Pau Espin Pedrol | 85978da | 2019-05-17 14:38:19 +0200 | [diff] [blame] | 132 | if (mgcp_conn_rtp_is_osmux(conn_rtp)) |
| 133 | conn_osmux_disable(conn_rtp); |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 134 | if (mgcp_conn_rtp_is_iuup(conn_rtp)) |
| 135 | mgcp_conn_iuup_cleanup(conn_rtp); |
Philipp Maier | d0b470d | 2018-03-16 12:45:53 +0100 | [diff] [blame] | 136 | mgcp_free_rtp_port(&conn_rtp->end); |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 137 | rate_ctr_group_free(conn_rtp->rate_ctr_group); |
Neels Hofmeyr | 401b740 | 2019-08-08 22:14:08 +0200 | [diff] [blame] | 138 | mgcp_codec_reset_all(conn_rtp); |
Philipp Maier | d0b470d | 2018-03-16 12:45:53 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 141 | void mgcp_conn_watchdog_cb(void *data) |
| 142 | { |
| 143 | struct mgcp_conn *conn = data; |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 144 | LOGPCONN(conn, DLMGCP, LOGL_ERROR, "connection timed out!\n"); |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 145 | mgcp_conn_free(conn->endp, conn->id); |
| 146 | } |
| 147 | |
| 148 | void mgcp_conn_watchdog_kick(struct mgcp_conn *conn) |
| 149 | { |
Eric | fbf78d1 | 2021-08-23 22:31:39 +0200 | [diff] [blame] | 150 | int timeout = conn->endp->trunk->cfg->conn_timeout; |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 151 | if (!timeout) |
| 152 | return; |
| 153 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 154 | LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "watchdog kicked\n"); |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 155 | osmo_timer_schedule(&conn->watchdog, timeout, 0); |
| 156 | } |
| 157 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 158 | /*! allocate a new connection list entry. |
| 159 | * \param[in] ctx talloc context |
| 160 | * \param[in] endp associated endpoint |
| 161 | * \param[in] id identification number of the connection |
| 162 | * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP) |
| 163 | * \returns pointer to allocated connection, NULL on error */ |
| 164 | struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp, |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 165 | enum mgcp_conn_type type, char *name) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 166 | { |
| 167 | struct mgcp_conn *conn; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 168 | int rc; |
| 169 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 170 | /* Do not allow more then two connections */ |
| 171 | if (llist_count(&endp->conns) >= endp->type->max_conns) |
| 172 | return NULL; |
| 173 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 174 | /* Create new connection and add it to the list */ |
| 175 | conn = talloc_zero(ctx, struct mgcp_conn); |
| 176 | if (!conn) |
| 177 | return NULL; |
| 178 | conn->endp = endp; |
| 179 | conn->type = type; |
| 180 | conn->mode = MGCP_CONN_NONE; |
| 181 | conn->mode_orig = MGCP_CONN_NONE; |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 182 | osmo_strlcpy(conn->name, name, sizeof(conn->name)); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 183 | rc = mgcp_alloc_id(endp, conn->id); |
| 184 | if (rc < 0) { |
| 185 | talloc_free(conn); |
| 186 | return NULL; |
| 187 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 188 | |
| 189 | switch (type) { |
| 190 | case MGCP_CONN_TYPE_RTP: |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 191 | if (mgcp_rtp_conn_init(&conn->u.rtp, conn) < 0) { |
| 192 | talloc_free(conn); |
| 193 | return NULL; |
| 194 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 195 | break; |
| 196 | default: |
| 197 | /* NOTE: This should never be called with an |
| 198 | * invalid type, its up to the programmer |
| 199 | * to ensure propery types */ |
| 200 | OSMO_ASSERT(false); |
| 201 | } |
| 202 | |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 203 | /* Initialize watchdog */ |
| 204 | osmo_timer_setup(&conn->watchdog, mgcp_conn_watchdog_cb, conn); |
| 205 | mgcp_conn_watchdog_kick(conn); |
Pau Espin Pedrol | 6d0a59a | 2020-09-08 16:50:22 +0200 | [diff] [blame] | 206 | mgcp_endp_add_conn(endp, conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 207 | |
| 208 | return conn; |
| 209 | } |
| 210 | |
| 211 | /*! find a connection by its ID. |
| 212 | * \param[in] endp associated endpoint |
| 213 | * \param[in] id identification number of the connection |
| 214 | * \returns pointer to allocated connection, NULL if not found */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 215 | struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 216 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 217 | struct mgcp_conn *conn; |
Neels Hofmeyr | 6531726 | 2018-09-03 22:11:05 +0200 | [diff] [blame] | 218 | const char *id_upper; |
Neels Hofmeyr | a77eade | 2018-08-29 02:30:39 +0200 | [diff] [blame] | 219 | const char *conn_id; |
Neels Hofmeyr | 6531726 | 2018-09-03 22:11:05 +0200 | [diff] [blame] | 220 | |
| 221 | if (!id || !*id) |
| 222 | return NULL; |
| 223 | |
Neels Hofmeyr | a77eade | 2018-08-29 02:30:39 +0200 | [diff] [blame] | 224 | /* Ignore leading zeros in needle */ |
| 225 | while (*id == '0') |
| 226 | id++; |
| 227 | |
Neels Hofmeyr | 6531726 | 2018-09-03 22:11:05 +0200 | [diff] [blame] | 228 | /* Use uppercase to compare identifiers, to avoid mismatches: RFC3435 2.1.3.2 "Names of |
| 229 | * Connections" defines the id as a hex string, so clients may return lower case hex even though |
| 230 | * we sent upper case hex in the CRCX response. */ |
| 231 | id_upper = osmo_str_toupper(id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 232 | |
| 233 | llist_for_each_entry(conn, &endp->conns, entry) { |
Neels Hofmeyr | a77eade | 2018-08-29 02:30:39 +0200 | [diff] [blame] | 234 | /* Ignore leading zeros in haystack */ |
| 235 | for (conn_id=conn->id; *conn_id == '0'; conn_id++); |
| 236 | |
| 237 | if (strcmp(conn_id, id_upper) == 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 238 | return conn; |
| 239 | } |
| 240 | |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | /*! find an RTP connection by its ID. |
| 245 | * \param[in] endp associated endpoint |
| 246 | * \param[in] id identification number of the connection |
| 247 | * \returns pointer to allocated connection, NULL if not found */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 248 | struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp, |
| 249 | const char *id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 250 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 251 | struct mgcp_conn *conn; |
| 252 | |
| 253 | conn = mgcp_conn_get(endp, id); |
| 254 | if (!conn) |
| 255 | return NULL; |
| 256 | |
| 257 | if (conn->type == MGCP_CONN_TYPE_RTP) |
| 258 | return &conn->u.rtp; |
| 259 | |
| 260 | return NULL; |
| 261 | } |
| 262 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 263 | static void aggregate_rtp_conn_stats(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn_rtp) |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 264 | { |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 265 | struct rate_ctr_group *all_stats = endp->trunk->ratectr.all_rtp_conn_stats; |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 266 | struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group; |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 267 | |
| 268 | if (all_stats == NULL || conn_stats == NULL) |
| 269 | return; |
| 270 | |
| 271 | /* Compared to per-connection RTP statistics, aggregated RTP statistics |
| 272 | * contain one additional rate couter item (RTP_NUM_CONNECTIONS). |
| 273 | * All other counters in both counter groups correspond to each other. */ |
| 274 | OSMO_ASSERT(conn_stats->desc->num_ctr + 1 == all_stats->desc->num_ctr); |
| 275 | |
Harald Welte | a48ff4a | 2020-03-08 14:45:08 +0100 | [diff] [blame] | 276 | /* all other counters are [now] updated in real-time */ |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 277 | rate_ctr_add(rate_ctr_group_get_ctr(all_stats, IN_STREAM_ERR_TSTMP_CTR), |
| 278 | rate_ctr_group_get_ctr(conn_stats, IN_STREAM_ERR_TSTMP_CTR)->current); |
| 279 | rate_ctr_add(rate_ctr_group_get_ctr(all_stats, OUT_STREAM_ERR_TSTMP_CTR), |
| 280 | rate_ctr_group_get_ctr(conn_stats, OUT_STREAM_ERR_TSTMP_CTR)->current); |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 281 | |
Pau Espin Pedrol | 907744e | 2021-06-04 17:57:34 +0200 | [diff] [blame] | 282 | rate_ctr_inc(rate_ctr_group_get_ctr(all_stats, RTP_NUM_CONNECTIONS)); |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 285 | /*! free a connection by its ID. |
| 286 | * \param[in] endp associated endpoint |
| 287 | * \param[in] id identification number of the connection */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 288 | void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 289 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 290 | struct mgcp_conn *conn; |
| 291 | |
| 292 | conn = mgcp_conn_get(endp, id); |
| 293 | if (!conn) |
| 294 | return; |
| 295 | |
| 296 | switch (conn->type) { |
| 297 | case MGCP_CONN_TYPE_RTP: |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 298 | aggregate_rtp_conn_stats(endp, &conn->u.rtp); |
Philipp Maier | d0b470d | 2018-03-16 12:45:53 +0100 | [diff] [blame] | 299 | mgcp_rtp_conn_cleanup(&conn->u.rtp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 300 | break; |
| 301 | default: |
| 302 | /* NOTE: This should never be called with an |
| 303 | * invalid type, its up to the programmer |
| 304 | * to ensure propery types */ |
| 305 | OSMO_ASSERT(false); |
| 306 | } |
| 307 | |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 308 | osmo_timer_del(&conn->watchdog); |
Pau Espin Pedrol | 6d0a59a | 2020-09-08 16:50:22 +0200 | [diff] [blame] | 309 | mgcp_endp_remove_conn(endp, conn); |
| 310 | /* WARN: endp may have be freed after call to mgcp_endp_remove_conn */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 311 | talloc_free(conn); |
| 312 | } |
| 313 | |
| 314 | /*! free oldest connection in the list. |
| 315 | * \param[in] endp associated endpoint */ |
| 316 | void mgcp_conn_free_oldest(struct mgcp_endpoint *endp) |
| 317 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 318 | struct mgcp_conn *conn; |
| 319 | |
| 320 | if (llist_empty(&endp->conns)) |
| 321 | return; |
| 322 | |
| 323 | conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry); |
| 324 | if (!conn) |
| 325 | return; |
| 326 | |
| 327 | mgcp_conn_free(endp, conn->id); |
| 328 | } |
| 329 | |
| 330 | /*! free all connections at once. |
| 331 | * \param[in] endp associated endpoint */ |
Eric | 7c0fe31 | 2021-11-09 21:45:44 +0100 | [diff] [blame] | 332 | #if defined(__has_attribute) |
| 333 | #if __has_attribute(no_sanitize) |
| 334 | __attribute__((no_sanitize("undefined"))) /* ubsan detects a misaligned load */ |
| 335 | #endif |
| 336 | #endif |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 337 | void mgcp_conn_free_all(struct mgcp_endpoint *endp) |
| 338 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 339 | struct mgcp_conn *conn; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 340 | |
Eric | fdbefde | 2021-09-06 19:49:24 +0200 | [diff] [blame] | 341 | /* Drop all items in the list, might be consecutive! */ |
| 342 | while ((conn = llist_first_entry_or_null(&endp->conns, struct mgcp_conn, entry))) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 343 | mgcp_conn_free(endp, conn->id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 344 | |
| 345 | return; |
| 346 | } |
| 347 | |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 348 | /*! dump basic connection information to human readable string. |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 349 | * \param[in] conn to dump |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 350 | * \returns human readable string */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 351 | char *mgcp_conn_dump(struct mgcp_conn *conn) |
| 352 | { |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 353 | static char str[sizeof(conn->name)+sizeof(conn->id)+256]; |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 354 | char ipbuf[INET6_ADDRSTRLEN]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 355 | |
| 356 | if (!conn) { |
| 357 | snprintf(str, sizeof(str), "(null connection)"); |
| 358 | return str; |
| 359 | } |
| 360 | |
| 361 | switch (conn->type) { |
| 362 | case MGCP_CONN_TYPE_RTP: |
| 363 | /* Dump RTP connection */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 364 | snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 365 | "rtp:%u rtcp:%u)", |
| 366 | conn->name, |
| 367 | conn->id, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 368 | osmo_sockaddr_ntop(&conn->u.rtp.end.addr.u.sa, ipbuf), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 369 | ntohs(conn->u.rtp.end.rtp_port), |
| 370 | ntohs(conn->u.rtp.end.rtcp_port)); |
| 371 | break; |
| 372 | |
| 373 | default: |
| 374 | /* Should not happen, we should be able to dump |
| 375 | * every possible connection type. */ |
| 376 | snprintf(str, sizeof(str), "(unknown connection type)"); |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | return str; |
| 381 | } |
| 382 | |
| 383 | /*! find destination connection on a specific endpoint. |
| 384 | * \param[in] conn to search a destination for |
| 385 | * \returns destination connection, NULL on failure */ |
| 386 | struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn) |
| 387 | { |
| 388 | struct mgcp_endpoint *endp; |
| 389 | struct mgcp_conn *partner_conn; |
| 390 | endp = conn->endp; |
| 391 | |
| 392 | /*! NOTE: This simply works by grabbing the first connection that is |
| 393 | * not the supplied connection, which is suitable for endpoints that |
| 394 | * do not serve more than two connections. */ |
| 395 | |
| 396 | llist_for_each_entry(partner_conn, &endp->conns, entry) { |
| 397 | if (conn != partner_conn) { |
| 398 | return partner_conn; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | return NULL; |
| 403 | } |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 404 | |
| 405 | /*! get oldest connection in the list. |
| 406 | * \param[in] endp associated endpoint */ |
| 407 | struct mgcp_conn *mgcp_conn_get_oldest(struct mgcp_endpoint *endp) |
| 408 | { |
| 409 | if (llist_empty(&endp->conns)) |
| 410 | return NULL; |
| 411 | |
| 412 | return llist_last_entry(&endp->conns, struct mgcp_conn, entry); |
| 413 | } |