blob: 0918b8b28c4842e33ca129b8e6ec40608b2d9b32 [file] [log] [blame]
Philipp Maier87bd9be2017-08-22 16:35:41 +02001/* 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 Hofmeyr67793542017-09-08 04:25:16 +020026#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010027#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020028#include <osmocom/mgcp/mgcp_sdp.h>
29#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierffd75e42017-11-22 11:44:50 +010030#include <osmocom/gsm/gsm_utils.h>
Philipp Maier9e1d1642018-05-09 16:26:34 +020031#include <osmocom/core/rate_ctr.h>
Philipp Maierffd75e42017-11-22 11:44:50 +010032#include <ctype.h>
33
Philipp Maier9e1d1642018-05-09 16:26:34 +020034static 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 Maiercede2a42018-07-03 14:14:21 +020037 [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 Maier9e1d1642018-05-09 16:26:34 +020042};
43
Philipp Maier9e1d1642018-05-09 16:26:34 +020044const 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 Maiercede2a42018-07-03 14:14:21 +020048 .num_ctr = ARRAY_SIZE(rate_ctr_desc),
Philipp Maier9e1d1642018-05-09 16:26:34 +020049 .ctr_desc = rate_ctr_desc
50};
51
52
Philipp Maierffd75e42017-11-22 11:44:50 +010053/* Allocate a new connection identifier. According to RFC3435, they must
Philipp Maierf8bfbe82017-11-23 19:32:31 +010054 * be unique only within the scope of the endpoint. (Caller must provide
55 * memory for id) */
Philipp Maierffd75e42017-11-22 11:44:50 +010056static int mgcp_alloc_id(struct mgcp_endpoint *endp, char *id)
57{
Neels Hofmeyra729db62018-08-28 16:20:51 +020058#define MGCP_CONN_ID_GEN_LEN 8
Philipp Maierffd75e42017-11-22 11:44:50 +010059 int i;
60 int k;
61 int rc;
Neels Hofmeyra729db62018-08-28 16:20:51 +020062 uint8_t id_bin[MGCP_CONN_ID_GEN_LEN / 2];
Philipp Maierffd75e42017-11-22 11:44:50 +010063 char *id_hex;
64
65 /* Generate a connection id that is unique for the current endpoint.
66 * Technically a counter would be sufficient, but in order to
67 * be able to find a specific connection in large logfiles and to
68 * prevent unintentional connections we assign the connection
69 * identifiers randomly from a reasonable large number space */
70 for (i = 0; i < 32; i++) {
71 rc = osmo_get_rand_id(id_bin, sizeof(id_bin));
72 if (rc < 0)
73 return rc;
74
75 id_hex = osmo_hexdump_nospc(id_bin, sizeof(id_bin));
76 for (k = 0; k < strlen(id_hex); k++)
77 id_hex[k] = toupper(id_hex[k]);
78
79 /* ensure that the generated conn_id is unique
80 * for this endpoint */
81 if (!mgcp_conn_get_rtp(endp, id_hex)) {
Neels Hofmeyr55e0dcf2018-09-03 21:36:56 +020082 osmo_strlcpy(id, id_hex, MGCP_CONN_ID_MAXLEN);
Philipp Maierffd75e42017-11-22 11:44:50 +010083 return 0;
84 }
85 }
86
Philipp Maier230e4fc2017-11-28 09:38:45 +010087 LOGP(DLMGCP, LOGL_ERROR, "endpoint:0x%x, unable to generate a unique connectionIdentifier\n",
Philipp Maierffd75e42017-11-22 11:44:50 +010088 ENDPOINT_NUMBER(endp));
89
90 return -1;
91}
Philipp Maier87bd9be2017-08-22 16:35:41 +020092
Philipp Maierc430d192018-03-16 12:11:18 +010093/* Initialize rtp connection struct with default values */
Philipp Maier892dec02018-03-16 12:32:01 +010094static void mgcp_rtp_conn_init(struct mgcp_conn_rtp *conn_rtp, struct mgcp_conn *conn)
Philipp Maier87bd9be2017-08-22 16:35:41 +020095{
Philipp Maier892dec02018-03-16 12:32:01 +010096 struct mgcp_rtp_end *end = &conn_rtp->end;
Philipp Maier9e1d1642018-05-09 16:26:34 +020097 /* FIXME: Each new rate counter group requires an unique index. At the
98 * moment we generate this index using this counter, but perhaps there
99 * is a more concious way to assign the indexes. */
100 static unsigned int rate_ctr_index = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200101
Philipp Maier892dec02018-03-16 12:32:01 +0100102 conn_rtp->type = MGCP_RTP_DEFAULT;
103 conn_rtp->osmux.allocated_cid = -1;
104
105 /* backpointer to the generic part of the connection */
106 conn->u.rtp.conn = conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200107
108 end->rtp.fd = -1;
109 end->rtcp.fd = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200110 end->rtp_port = end->rtcp_port = 0;
111 talloc_free(end->fmtp_extra);
112 end->fmtp_extra = NULL;
113
114 /* Set default values */
115 end->frames_per_packet = 0; /* unknown */
116 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
117 end->output_enabled = 0;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200118 end->maximum_packet_time = -1;
Philipp Maier9e1d1642018-05-09 16:26:34 +0200119
120 conn_rtp->rate_ctr_group = rate_ctr_group_alloc(conn, &rate_ctr_group_desc, rate_ctr_index);
121 conn_rtp->state.in_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[IN_STREAM_ERR_TSTMP_CTR];
122 conn_rtp->state.out_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[OUT_STREAM_ERR_TSTMP_CTR];
123 rate_ctr_index++;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200124
125 /* Make sure codec table is reset */
126 mgcp_codec_reset_all(conn_rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127}
128
Philipp Maierd0b470d2018-03-16 12:45:53 +0100129/* Cleanup rtp connection struct */
130static void mgcp_rtp_conn_cleanup(struct mgcp_conn_rtp *conn_rtp)
131{
132 osmux_disable_conn(conn_rtp);
133 osmux_release_cid(conn_rtp);
134 mgcp_free_rtp_port(&conn_rtp->end);
Philipp Maier9e1d1642018-05-09 16:26:34 +0200135 rate_ctr_group_free(conn_rtp->rate_ctr_group);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100136}
137
Philipp Maier87bd9be2017-08-22 16:35:41 +0200138/*! allocate a new connection list entry.
139 * \param[in] ctx talloc context
140 * \param[in] endp associated endpoint
141 * \param[in] id identification number of the connection
142 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
143 * \returns pointer to allocated connection, NULL on error */
144struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100145 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200146{
147 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100148 int rc;
149
Philipp Maier87bd9be2017-08-22 16:35:41 +0200150 /* Do not allow more then two connections */
151 if (llist_count(&endp->conns) >= endp->type->max_conns)
152 return NULL;
153
Philipp Maier87bd9be2017-08-22 16:35:41 +0200154 /* Create new connection and add it to the list */
155 conn = talloc_zero(ctx, struct mgcp_conn);
156 if (!conn)
157 return NULL;
158 conn->endp = endp;
159 conn->type = type;
160 conn->mode = MGCP_CONN_NONE;
161 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100162 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100163 rc = mgcp_alloc_id(endp, conn->id);
164 if (rc < 0) {
165 talloc_free(conn);
166 return NULL;
167 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168
169 switch (type) {
170 case MGCP_CONN_TYPE_RTP:
Philipp Maier892dec02018-03-16 12:32:01 +0100171 mgcp_rtp_conn_init(&conn->u.rtp, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172 break;
173 default:
174 /* NOTE: This should never be called with an
175 * invalid type, its up to the programmer
176 * to ensure propery types */
177 OSMO_ASSERT(false);
178 }
179
180 llist_add(&conn->entry, &endp->conns);
181
182 return conn;
183}
184
185/*! find a connection by its ID.
186 * \param[in] endp associated endpoint
187 * \param[in] id identification number of the connection
188 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100189struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191 struct mgcp_conn *conn;
Neels Hofmeyr65317262018-09-03 22:11:05 +0200192 const char *id_upper;
193
194 if (!id || !*id)
195 return NULL;
196
197 /* Use uppercase to compare identifiers, to avoid mismatches: RFC3435 2.1.3.2 "Names of
198 * Connections" defines the id as a hex string, so clients may return lower case hex even though
199 * we sent upper case hex in the CRCX response. */
200 id_upper = osmo_str_toupper(id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201
202 llist_for_each_entry(conn, &endp->conns, entry) {
Neels Hofmeyr65317262018-09-03 22:11:05 +0200203 if (strcmp(conn->id, id_upper) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200204 return conn;
205 }
206
207 return NULL;
208}
209
210/*! find an RTP connection by its ID.
211 * \param[in] endp associated endpoint
212 * \param[in] id identification number of the connection
213 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100214struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
215 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 struct mgcp_conn *conn;
218
219 conn = mgcp_conn_get(endp, id);
220 if (!conn)
221 return NULL;
222
223 if (conn->type == MGCP_CONN_TYPE_RTP)
224 return &conn->u.rtp;
225
226 return NULL;
227}
228
229/*! free a connection by its ID.
230 * \param[in] endp associated endpoint
231 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100232void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200234 struct mgcp_conn *conn;
235
236 conn = mgcp_conn_get(endp, id);
237 if (!conn)
238 return;
239
Philipp Maierdf5d2192018-01-24 11:39:32 +0100240 /* Run endpoint cleanup action. By this we inform the endpoint about
241 * the removal of the connection and allow it to clean up its inner
242 * state accordingly */
243 if (endp->type->cleanup_cb)
244 endp->type->cleanup_cb(endp, conn);
245
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246 switch (conn->type) {
247 case MGCP_CONN_TYPE_RTP:
Philipp Maierd0b470d2018-03-16 12:45:53 +0100248 mgcp_rtp_conn_cleanup(&conn->u.rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200249 break;
250 default:
251 /* NOTE: This should never be called with an
252 * invalid type, its up to the programmer
253 * to ensure propery types */
254 OSMO_ASSERT(false);
255 }
256
257 llist_del(&conn->entry);
258 talloc_free(conn);
259}
260
261/*! free oldest connection in the list.
262 * \param[in] endp associated endpoint */
263void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
264{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200265 struct mgcp_conn *conn;
266
267 if (llist_empty(&endp->conns))
268 return;
269
270 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
271 if (!conn)
272 return;
273
274 mgcp_conn_free(endp, conn->id);
275}
276
277/*! free all connections at once.
278 * \param[in] endp associated endpoint */
279void mgcp_conn_free_all(struct mgcp_endpoint *endp)
280{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 struct mgcp_conn *conn;
282 struct mgcp_conn *conn_tmp;
283
284 /* Drop all items in the list */
285 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
286 mgcp_conn_free(endp, conn->id);
287 }
288
289 return;
290}
291
Harald Welte1d1b98f2017-12-25 10:03:40 +0100292/*! dump basic connection information to human readable string.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200293 * \param[in] conn to dump
Harald Welte1d1b98f2017-12-25 10:03:40 +0100294 * \returns human readable string */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200295char *mgcp_conn_dump(struct mgcp_conn *conn)
296{
Philipp Maier01d24a32017-11-21 17:26:09 +0100297 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200298
299 if (!conn) {
300 snprintf(str, sizeof(str), "(null connection)");
301 return str;
302 }
303
304 switch (conn->type) {
305 case MGCP_CONN_TYPE_RTP:
306 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100307 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200308 "rtp:%u rtcp:%u)",
309 conn->name,
310 conn->id,
311 inet_ntoa(conn->u.rtp.end.addr),
312 ntohs(conn->u.rtp.end.rtp_port),
313 ntohs(conn->u.rtp.end.rtcp_port));
314 break;
315
316 default:
317 /* Should not happen, we should be able to dump
318 * every possible connection type. */
319 snprintf(str, sizeof(str), "(unknown connection type)");
320 break;
321 }
322
323 return str;
324}
325
326/*! find destination connection on a specific endpoint.
327 * \param[in] conn to search a destination for
328 * \returns destination connection, NULL on failure */
329struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
330{
331 struct mgcp_endpoint *endp;
332 struct mgcp_conn *partner_conn;
333 endp = conn->endp;
334
335 /*! NOTE: This simply works by grabbing the first connection that is
336 * not the supplied connection, which is suitable for endpoints that
337 * do not serve more than two connections. */
338
339 llist_for_each_entry(partner_conn, &endp->conns, entry) {
340 if (conn != partner_conn) {
341 return partner_conn;
342 }
343 }
344
345 return NULL;
346}