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