blob: e49559c1155f8f0f3596feaa244ead2e1c886447 [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 +020034enum {
35 IN_STREAM_ERR_TSTMP_CTR,
36 OUT_STREAM_ERR_TSTMP_CTR,
37};
38
39static const struct rate_ctr_desc rate_ctr_desc[] = {
40 [IN_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:in", "Inbound rtp-stream timestamp errors."},
41 [OUT_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:out", "Outbound rtp-stream timestamp errors."},
42};
43
44
45const static struct rate_ctr_group_desc rate_ctr_group_desc = {
46 .group_name_prefix = "conn_rtp",
47 .group_description = "rtp connection statistics",
48 .class_id = 1,
49 .num_ctr = 2,
50 .ctr_desc = rate_ctr_desc
51};
52
53
Philipp Maierffd75e42017-11-22 11:44:50 +010054/* Allocate a new connection identifier. According to RFC3435, they must
Philipp Maierf8bfbe82017-11-23 19:32:31 +010055 * be unique only within the scope of the endpoint. (Caller must provide
56 * memory for id) */
Philipp Maierffd75e42017-11-22 11:44:50 +010057static int mgcp_alloc_id(struct mgcp_endpoint *endp, char *id)
58{
59 int i;
60 int k;
61 int rc;
62 uint8_t id_bin[16];
63 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)) {
82 osmo_strlcpy(id, id_hex, MGCP_CONN_ID_LENGTH);
83 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;
Harald Weltea0ac30f2017-12-25 09:52:30 +0100110 memset(&end->stats, 0, sizeof(end->stats));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200111 end->rtp_port = end->rtcp_port = 0;
112 talloc_free(end->fmtp_extra);
113 end->fmtp_extra = NULL;
114
115 /* Set default values */
116 end->frames_per_packet = 0; /* unknown */
117 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
118 end->output_enabled = 0;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200119 end->maximum_packet_time = -1;
Philipp Maier9e1d1642018-05-09 16:26:34 +0200120
121 conn_rtp->rate_ctr_group = rate_ctr_group_alloc(conn, &rate_ctr_group_desc, rate_ctr_index);
122 conn_rtp->state.in_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[IN_STREAM_ERR_TSTMP_CTR];
123 conn_rtp->state.out_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[OUT_STREAM_ERR_TSTMP_CTR];
124 rate_ctr_index++;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200125
126 /* Make sure codec table is reset */
127 mgcp_codec_reset_all(conn_rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200128}
129
Philipp Maierd0b470d2018-03-16 12:45:53 +0100130/* Cleanup rtp connection struct */
131static void mgcp_rtp_conn_cleanup(struct mgcp_conn_rtp *conn_rtp)
132{
133 osmux_disable_conn(conn_rtp);
134 osmux_release_cid(conn_rtp);
135 mgcp_free_rtp_port(&conn_rtp->end);
Philipp Maier9e1d1642018-05-09 16:26:34 +0200136 rate_ctr_group_free(conn_rtp->rate_ctr_group);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100137}
138
Philipp Maier87bd9be2017-08-22 16:35:41 +0200139/*! allocate a new connection list entry.
140 * \param[in] ctx talloc context
141 * \param[in] endp associated endpoint
142 * \param[in] id identification number of the connection
143 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
144 * \returns pointer to allocated connection, NULL on error */
145struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100146 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200147{
148 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100149 int rc;
150
Philipp Maier87bd9be2017-08-22 16:35:41 +0200151 /* Do not allow more then two connections */
152 if (llist_count(&endp->conns) >= endp->type->max_conns)
153 return NULL;
154
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155 /* Create new connection and add it to the list */
156 conn = talloc_zero(ctx, struct mgcp_conn);
157 if (!conn)
158 return NULL;
159 conn->endp = endp;
160 conn->type = type;
161 conn->mode = MGCP_CONN_NONE;
162 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100163 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100164 rc = mgcp_alloc_id(endp, conn->id);
165 if (rc < 0) {
166 talloc_free(conn);
167 return NULL;
168 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200169
170 switch (type) {
171 case MGCP_CONN_TYPE_RTP:
Philipp Maier892dec02018-03-16 12:32:01 +0100172 mgcp_rtp_conn_init(&conn->u.rtp, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173 break;
174 default:
175 /* NOTE: This should never be called with an
176 * invalid type, its up to the programmer
177 * to ensure propery types */
178 OSMO_ASSERT(false);
179 }
180
181 llist_add(&conn->entry, &endp->conns);
182
183 return conn;
184}
185
186/*! find a connection by its ID.
187 * \param[in] endp associated endpoint
188 * \param[in] id identification number of the connection
189 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100190struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200192 struct mgcp_conn *conn;
193
194 llist_for_each_entry(conn, &endp->conns, entry) {
Philipp Maier01d24a32017-11-21 17:26:09 +0100195 if (strncmp(conn->id, id, sizeof(conn->id)) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200196 return conn;
197 }
198
199 return NULL;
200}
201
202/*! find an RTP connection by its ID.
203 * \param[in] endp associated endpoint
204 * \param[in] id identification number of the connection
205 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100206struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
207 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200209 struct mgcp_conn *conn;
210
211 conn = mgcp_conn_get(endp, id);
212 if (!conn)
213 return NULL;
214
215 if (conn->type == MGCP_CONN_TYPE_RTP)
216 return &conn->u.rtp;
217
218 return NULL;
219}
220
221/*! free a connection by its ID.
222 * \param[in] endp associated endpoint
223 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100224void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200226 struct mgcp_conn *conn;
227
228 conn = mgcp_conn_get(endp, id);
229 if (!conn)
230 return;
231
Philipp Maierdf5d2192018-01-24 11:39:32 +0100232 /* Run endpoint cleanup action. By this we inform the endpoint about
233 * the removal of the connection and allow it to clean up its inner
234 * state accordingly */
235 if (endp->type->cleanup_cb)
236 endp->type->cleanup_cb(endp, conn);
237
Philipp Maier87bd9be2017-08-22 16:35:41 +0200238 switch (conn->type) {
239 case MGCP_CONN_TYPE_RTP:
Philipp Maierd0b470d2018-03-16 12:45:53 +0100240 mgcp_rtp_conn_cleanup(&conn->u.rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241 break;
242 default:
243 /* NOTE: This should never be called with an
244 * invalid type, its up to the programmer
245 * to ensure propery types */
246 OSMO_ASSERT(false);
247 }
248
249 llist_del(&conn->entry);
250 talloc_free(conn);
251}
252
253/*! free oldest connection in the list.
254 * \param[in] endp associated endpoint */
255void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
256{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257 struct mgcp_conn *conn;
258
259 if (llist_empty(&endp->conns))
260 return;
261
262 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
263 if (!conn)
264 return;
265
266 mgcp_conn_free(endp, conn->id);
267}
268
269/*! free all connections at once.
270 * \param[in] endp associated endpoint */
271void mgcp_conn_free_all(struct mgcp_endpoint *endp)
272{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200273 struct mgcp_conn *conn;
274 struct mgcp_conn *conn_tmp;
275
276 /* Drop all items in the list */
277 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
278 mgcp_conn_free(endp, conn->id);
279 }
280
281 return;
282}
283
Harald Welte1d1b98f2017-12-25 10:03:40 +0100284/*! dump basic connection information to human readable string.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200285 * \param[in] conn to dump
Harald Welte1d1b98f2017-12-25 10:03:40 +0100286 * \returns human readable string */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200287char *mgcp_conn_dump(struct mgcp_conn *conn)
288{
Philipp Maier01d24a32017-11-21 17:26:09 +0100289 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200290
291 if (!conn) {
292 snprintf(str, sizeof(str), "(null connection)");
293 return str;
294 }
295
296 switch (conn->type) {
297 case MGCP_CONN_TYPE_RTP:
298 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100299 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200300 "rtp:%u rtcp:%u)",
301 conn->name,
302 conn->id,
303 inet_ntoa(conn->u.rtp.end.addr),
304 ntohs(conn->u.rtp.end.rtp_port),
305 ntohs(conn->u.rtp.end.rtcp_port));
306 break;
307
308 default:
309 /* Should not happen, we should be able to dump
310 * every possible connection type. */
311 snprintf(str, sizeof(str), "(unknown connection type)");
312 break;
313 }
314
315 return str;
316}
317
318/*! find destination connection on a specific endpoint.
319 * \param[in] conn to search a destination for
320 * \returns destination connection, NULL on failure */
321struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
322{
323 struct mgcp_endpoint *endp;
324 struct mgcp_conn *partner_conn;
325 endp = conn->endp;
326
327 /*! NOTE: This simply works by grabbing the first connection that is
328 * not the supplied connection, which is suitable for endpoints that
329 * do not serve more than two connections. */
330
331 llist_for_each_entry(partner_conn, &endp->conns, entry) {
332 if (conn != partner_conn) {
333 return partner_conn;
334 }
335 }
336
337 return NULL;
338}