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