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