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 | |
| 24 | #include <osmocom/mgcp/mgcp_conn.h> |
| 25 | #include <osmocom/mgcp/mgcp_internal.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 26 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 27 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 28 | #include <osmocom/mgcp/mgcp_sdp.h> |
| 29 | #include <osmocom/mgcp/mgcp_codec.h> |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm_utils.h> |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 31 | #include <osmocom/core/rate_ctr.h> |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 32 | #include <ctype.h> |
| 33 | |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 34 | static const struct rate_ctr_desc rate_ctr_desc[] = { |
| 35 | [IN_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:in", "Inbound rtp-stream timestamp errors."}, |
| 36 | [OUT_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:out", "Outbound rtp-stream timestamp errors."}, |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 37 | [RTP_PACKETS_RX_CTR] = {"rtp:packets_rx", "Inbound rtp packets."}, |
| 38 | [RTP_OCTETS_RX_CTR] = {"rtp:octets_rx", "Inbound rtp octets."}, |
| 39 | [RTP_PACKETS_TX_CTR] = {"rtp:packets_tx", "Outbound rtp packets."}, |
| 40 | [RTP_OCTETS_TX_CTR] = {"rtp:octets_rx", "Outbound rtp octets."}, |
| 41 | [RTP_DROPPED_PACKETS_CTR] = {"rtp:dropped", "dropped rtp packets."} |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 44 | const static struct rate_ctr_group_desc rate_ctr_group_desc = { |
| 45 | .group_name_prefix = "conn_rtp", |
| 46 | .group_description = "rtp connection statistics", |
| 47 | .class_id = 1, |
Philipp Maier | cede2a4 | 2018-07-03 14:14:21 +0200 | [diff] [blame] | 48 | .num_ctr = ARRAY_SIZE(rate_ctr_desc), |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 49 | .ctr_desc = rate_ctr_desc |
| 50 | }; |
| 51 | |
| 52 | |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 53 | /* Allocate a new connection identifier. According to RFC3435, they must |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 54 | * be unique only within the scope of the endpoint. (Caller must provide |
| 55 | * memory for id) */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 56 | static int mgcp_alloc_id(struct mgcp_endpoint *endp, char *id) |
| 57 | { |
| 58 | int i; |
| 59 | int k; |
| 60 | int rc; |
| 61 | uint8_t id_bin[16]; |
| 62 | char *id_hex; |
| 63 | |
| 64 | /* Generate a connection id that is unique for the current endpoint. |
| 65 | * Technically a counter would be sufficient, but in order to |
| 66 | * be able to find a specific connection in large logfiles and to |
| 67 | * prevent unintentional connections we assign the connection |
| 68 | * identifiers randomly from a reasonable large number space */ |
| 69 | for (i = 0; i < 32; i++) { |
| 70 | rc = osmo_get_rand_id(id_bin, sizeof(id_bin)); |
| 71 | if (rc < 0) |
| 72 | return rc; |
| 73 | |
| 74 | id_hex = osmo_hexdump_nospc(id_bin, sizeof(id_bin)); |
| 75 | for (k = 0; k < strlen(id_hex); k++) |
| 76 | id_hex[k] = toupper(id_hex[k]); |
| 77 | |
| 78 | /* ensure that the generated conn_id is unique |
| 79 | * for this endpoint */ |
| 80 | if (!mgcp_conn_get_rtp(endp, id_hex)) { |
| 81 | osmo_strlcpy(id, id_hex, MGCP_CONN_ID_LENGTH); |
| 82 | return 0; |
| 83 | } |
| 84 | } |
| 85 | |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 86 | LOGP(DLMGCP, LOGL_ERROR, "endpoint:0x%x, unable to generate a unique connectionIdentifier\n", |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 87 | ENDPOINT_NUMBER(endp)); |
| 88 | |
| 89 | return -1; |
| 90 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 91 | |
Philipp Maier | c430d19 | 2018-03-16 12:11:18 +0100 | [diff] [blame] | 92 | /* Initialize rtp connection struct with default values */ |
Philipp Maier | 892dec0 | 2018-03-16 12:32:01 +0100 | [diff] [blame] | 93 | static void 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] | 94 | { |
Philipp Maier | 892dec0 | 2018-03-16 12:32:01 +0100 | [diff] [blame] | 95 | struct mgcp_rtp_end *end = &conn_rtp->end; |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 96 | /* FIXME: Each new rate counter group requires an unique index. At the |
| 97 | * moment we generate this index using this counter, but perhaps there |
| 98 | * is a more concious way to assign the indexes. */ |
| 99 | static unsigned int rate_ctr_index = 0; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 100 | |
Philipp Maier | 892dec0 | 2018-03-16 12:32:01 +0100 | [diff] [blame] | 101 | conn_rtp->type = MGCP_RTP_DEFAULT; |
| 102 | conn_rtp->osmux.allocated_cid = -1; |
| 103 | |
| 104 | /* backpointer to the generic part of the connection */ |
| 105 | conn->u.rtp.conn = conn; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 106 | |
| 107 | end->rtp.fd = -1; |
| 108 | end->rtcp.fd = -1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 109 | end->rtp_port = end->rtcp_port = 0; |
| 110 | talloc_free(end->fmtp_extra); |
| 111 | end->fmtp_extra = NULL; |
| 112 | |
| 113 | /* Set default values */ |
| 114 | end->frames_per_packet = 0; /* unknown */ |
| 115 | end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS; |
| 116 | end->output_enabled = 0; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 117 | end->maximum_packet_time = -1; |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 118 | |
| 119 | conn_rtp->rate_ctr_group = rate_ctr_group_alloc(conn, &rate_ctr_group_desc, rate_ctr_index); |
| 120 | conn_rtp->state.in_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[IN_STREAM_ERR_TSTMP_CTR]; |
| 121 | conn_rtp->state.out_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[OUT_STREAM_ERR_TSTMP_CTR]; |
| 122 | rate_ctr_index++; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 123 | |
| 124 | /* Make sure codec table is reset */ |
| 125 | mgcp_codec_reset_all(conn_rtp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Philipp Maier | d0b470d | 2018-03-16 12:45:53 +0100 | [diff] [blame] | 128 | /* Cleanup rtp connection struct */ |
| 129 | static void mgcp_rtp_conn_cleanup(struct mgcp_conn_rtp *conn_rtp) |
| 130 | { |
| 131 | osmux_disable_conn(conn_rtp); |
| 132 | osmux_release_cid(conn_rtp); |
| 133 | mgcp_free_rtp_port(&conn_rtp->end); |
Philipp Maier | 9e1d164 | 2018-05-09 16:26:34 +0200 | [diff] [blame] | 134 | rate_ctr_group_free(conn_rtp->rate_ctr_group); |
Philipp Maier | d0b470d | 2018-03-16 12:45:53 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 137 | /*! allocate a new connection list entry. |
| 138 | * \param[in] ctx talloc context |
| 139 | * \param[in] endp associated endpoint |
| 140 | * \param[in] id identification number of the connection |
| 141 | * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP) |
| 142 | * \returns pointer to allocated connection, NULL on error */ |
| 143 | struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp, |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 144 | enum mgcp_conn_type type, char *name) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 145 | { |
| 146 | struct mgcp_conn *conn; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 147 | int rc; |
| 148 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 149 | /* Do not allow more then two connections */ |
| 150 | if (llist_count(&endp->conns) >= endp->type->max_conns) |
| 151 | return NULL; |
| 152 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 153 | /* Create new connection and add it to the list */ |
| 154 | conn = talloc_zero(ctx, struct mgcp_conn); |
| 155 | if (!conn) |
| 156 | return NULL; |
| 157 | conn->endp = endp; |
| 158 | conn->type = type; |
| 159 | conn->mode = MGCP_CONN_NONE; |
| 160 | conn->mode_orig = MGCP_CONN_NONE; |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 161 | osmo_strlcpy(conn->name, name, sizeof(conn->name)); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 162 | rc = mgcp_alloc_id(endp, conn->id); |
| 163 | if (rc < 0) { |
| 164 | talloc_free(conn); |
| 165 | return NULL; |
| 166 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 167 | |
| 168 | switch (type) { |
| 169 | case MGCP_CONN_TYPE_RTP: |
Philipp Maier | 892dec0 | 2018-03-16 12:32:01 +0100 | [diff] [blame] | 170 | mgcp_rtp_conn_init(&conn->u.rtp, conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 171 | break; |
| 172 | default: |
| 173 | /* NOTE: This should never be called with an |
| 174 | * invalid type, its up to the programmer |
| 175 | * to ensure propery types */ |
| 176 | OSMO_ASSERT(false); |
| 177 | } |
| 178 | |
| 179 | llist_add(&conn->entry, &endp->conns); |
| 180 | |
| 181 | return conn; |
| 182 | } |
| 183 | |
| 184 | /*! find a connection by its ID. |
| 185 | * \param[in] endp associated endpoint |
| 186 | * \param[in] id identification number of the connection |
| 187 | * \returns pointer to allocated connection, NULL if not found */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 188 | 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] | 189 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 190 | struct mgcp_conn *conn; |
| 191 | |
| 192 | llist_for_each_entry(conn, &endp->conns, entry) { |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 193 | if (strncmp(conn->id, id, sizeof(conn->id)) == 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 194 | return conn; |
| 195 | } |
| 196 | |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | /*! find an RTP connection by its ID. |
| 201 | * \param[in] endp associated endpoint |
| 202 | * \param[in] id identification number of the connection |
| 203 | * \returns pointer to allocated connection, NULL if not found */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 204 | struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp, |
| 205 | const char *id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 206 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 207 | struct mgcp_conn *conn; |
| 208 | |
| 209 | conn = mgcp_conn_get(endp, id); |
| 210 | if (!conn) |
| 211 | return NULL; |
| 212 | |
| 213 | if (conn->type == MGCP_CONN_TYPE_RTP) |
| 214 | return &conn->u.rtp; |
| 215 | |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | /*! free a connection by its ID. |
| 220 | * \param[in] endp associated endpoint |
| 221 | * \param[in] id identification number of the connection */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 222 | void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 223 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 224 | struct mgcp_conn *conn; |
| 225 | |
| 226 | conn = mgcp_conn_get(endp, id); |
| 227 | if (!conn) |
| 228 | return; |
| 229 | |
Philipp Maier | df5d219 | 2018-01-24 11:39:32 +0100 | [diff] [blame] | 230 | /* Run endpoint cleanup action. By this we inform the endpoint about |
| 231 | * the removal of the connection and allow it to clean up its inner |
| 232 | * state accordingly */ |
| 233 | if (endp->type->cleanup_cb) |
| 234 | endp->type->cleanup_cb(endp, conn); |
| 235 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 236 | switch (conn->type) { |
| 237 | case MGCP_CONN_TYPE_RTP: |
Philipp Maier | d0b470d | 2018-03-16 12:45:53 +0100 | [diff] [blame] | 238 | mgcp_rtp_conn_cleanup(&conn->u.rtp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 239 | break; |
| 240 | default: |
| 241 | /* NOTE: This should never be called with an |
| 242 | * invalid type, its up to the programmer |
| 243 | * to ensure propery types */ |
| 244 | OSMO_ASSERT(false); |
| 245 | } |
| 246 | |
| 247 | llist_del(&conn->entry); |
| 248 | talloc_free(conn); |
| 249 | } |
| 250 | |
| 251 | /*! free oldest connection in the list. |
| 252 | * \param[in] endp associated endpoint */ |
| 253 | void mgcp_conn_free_oldest(struct mgcp_endpoint *endp) |
| 254 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 255 | struct mgcp_conn *conn; |
| 256 | |
| 257 | if (llist_empty(&endp->conns)) |
| 258 | return; |
| 259 | |
| 260 | conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry); |
| 261 | if (!conn) |
| 262 | return; |
| 263 | |
| 264 | mgcp_conn_free(endp, conn->id); |
| 265 | } |
| 266 | |
| 267 | /*! free all connections at once. |
| 268 | * \param[in] endp associated endpoint */ |
| 269 | void mgcp_conn_free_all(struct mgcp_endpoint *endp) |
| 270 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 271 | struct mgcp_conn *conn; |
| 272 | struct mgcp_conn *conn_tmp; |
| 273 | |
| 274 | /* Drop all items in the list */ |
| 275 | llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) { |
| 276 | mgcp_conn_free(endp, conn->id); |
| 277 | } |
| 278 | |
| 279 | return; |
| 280 | } |
| 281 | |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 282 | /*! dump basic connection information to human readable string. |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 283 | * \param[in] conn to dump |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 284 | * \returns human readable string */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 285 | char *mgcp_conn_dump(struct mgcp_conn *conn) |
| 286 | { |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 287 | static char str[sizeof(conn->name)+sizeof(conn->id)+256]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 288 | |
| 289 | if (!conn) { |
| 290 | snprintf(str, sizeof(str), "(null connection)"); |
| 291 | return str; |
| 292 | } |
| 293 | |
| 294 | switch (conn->type) { |
| 295 | case MGCP_CONN_TYPE_RTP: |
| 296 | /* Dump RTP connection */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 297 | snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, " |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 298 | "rtp:%u rtcp:%u)", |
| 299 | conn->name, |
| 300 | conn->id, |
| 301 | inet_ntoa(conn->u.rtp.end.addr), |
| 302 | ntohs(conn->u.rtp.end.rtp_port), |
| 303 | ntohs(conn->u.rtp.end.rtcp_port)); |
| 304 | break; |
| 305 | |
| 306 | default: |
| 307 | /* Should not happen, we should be able to dump |
| 308 | * every possible connection type. */ |
| 309 | snprintf(str, sizeof(str), "(unknown connection type)"); |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | return str; |
| 314 | } |
| 315 | |
| 316 | /*! find destination connection on a specific endpoint. |
| 317 | * \param[in] conn to search a destination for |
| 318 | * \returns destination connection, NULL on failure */ |
| 319 | struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn) |
| 320 | { |
| 321 | struct mgcp_endpoint *endp; |
| 322 | struct mgcp_conn *partner_conn; |
| 323 | endp = conn->endp; |
| 324 | |
| 325 | /*! NOTE: This simply works by grabbing the first connection that is |
| 326 | * not the supplied connection, which is suitable for endpoints that |
| 327 | * do not serve more than two connections. */ |
| 328 | |
| 329 | llist_for_each_entry(partner_conn, &endp->conns, entry) { |
| 330 | if (conn != partner_conn) { |
| 331 | return partner_conn; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return NULL; |
| 336 | } |