blob: 005504997ff425a53102aae406e53838a62ea142 [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 Maierffd75e42017-11-22 11:44:50 +010028#include <osmocom/gsm/gsm_utils.h>
Philipp Maier7181cc12018-03-28 16:20:14 +020029#include <osmocom/core/rate_ctr.h>
Philipp Maierffd75e42017-11-22 11:44:50 +010030#include <ctype.h>
31
Philipp Maier7181cc12018-03-28 16:20:14 +020032const static struct rate_ctr_desc rate_ctr_desc[] = {
33 {
34 .name = "in_stream_err_ts_ctr",
35 .description = "inbound rtp-stream timestamp errors",
36 },{
37 .name = "out_stream_err_ts_ctr",
38 .description = "outbound rtp-stream timestamp errors",
39 }
40};
41
42const static struct rate_ctr_group_desc rate_ctr_group_desc = {
43 .group_name_prefix = "conn_rtp",
44 .group_description = "rtp connection statistics",
45 .class_id = 1,
46 .num_ctr = 2,
47 .ctr_desc = rate_ctr_desc
48};
49
50
Philipp Maierffd75e42017-11-22 11:44:50 +010051/* Allocate a new connection identifier. According to RFC3435, they must
Philipp Maierf8bfbe82017-11-23 19:32:31 +010052 * be unique only within the scope of the endpoint. (Caller must provide
53 * memory for id) */
Philipp Maierffd75e42017-11-22 11:44:50 +010054static int mgcp_alloc_id(struct mgcp_endpoint *endp, char *id)
55{
56 int i;
57 int k;
58 int rc;
59 uint8_t id_bin[16];
60 char *id_hex;
61
62 /* Generate a connection id that is unique for the current endpoint.
63 * Technically a counter would be sufficient, but in order to
64 * be able to find a specific connection in large logfiles and to
65 * prevent unintentional connections we assign the connection
66 * identifiers randomly from a reasonable large number space */
67 for (i = 0; i < 32; i++) {
68 rc = osmo_get_rand_id(id_bin, sizeof(id_bin));
69 if (rc < 0)
70 return rc;
71
72 id_hex = osmo_hexdump_nospc(id_bin, sizeof(id_bin));
73 for (k = 0; k < strlen(id_hex); k++)
74 id_hex[k] = toupper(id_hex[k]);
75
76 /* ensure that the generated conn_id is unique
77 * for this endpoint */
78 if (!mgcp_conn_get_rtp(endp, id_hex)) {
79 osmo_strlcpy(id, id_hex, MGCP_CONN_ID_LENGTH);
80 return 0;
81 }
82 }
83
Philipp Maier230e4fc2017-11-28 09:38:45 +010084 LOGP(DLMGCP, LOGL_ERROR, "endpoint:0x%x, unable to generate a unique connectionIdentifier\n",
Philipp Maierffd75e42017-11-22 11:44:50 +010085 ENDPOINT_NUMBER(endp));
86
87 return -1;
88}
Philipp Maier87bd9be2017-08-22 16:35:41 +020089
90/* Reset codec state and free memory */
Philipp Maier77f76d02018-03-16 12:37:49 +010091static void mgcp_rtp_codec_init(struct mgcp_rtp_codec *codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +020092{
93 codec->payload_type = -1;
94 codec->subtype_name = NULL;
95 codec->audio_name = NULL;
96 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
97 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
98 codec->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
99 codec->channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
100
101 /* see also mgcp_sdp.c, mgcp_set_audio_info() */
102 talloc_free(codec->subtype_name);
103 talloc_free(codec->audio_name);
104}
105
Philipp Maierc430d192018-03-16 12:11:18 +0100106/* Initialize rtp connection struct with default values */
Philipp Maier892dec02018-03-16 12:32:01 +0100107static void mgcp_rtp_conn_init(struct mgcp_conn_rtp *conn_rtp, struct mgcp_conn *conn)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200108{
Philipp Maier892dec02018-03-16 12:32:01 +0100109 struct mgcp_rtp_end *end = &conn_rtp->end;
Philipp Maier7181cc12018-03-28 16:20:14 +0200110 /* FIXME: Each new rate counter group requires an unique index. At the
111 * moment we generate this index using this counter, but perhaps there
112 * is a more concious way to assign the indexes. */
113 static unsigned int rate_ctr_index = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200114
Philipp Maier892dec02018-03-16 12:32:01 +0100115 conn_rtp->type = MGCP_RTP_DEFAULT;
116 conn_rtp->osmux.allocated_cid = -1;
117
118 /* backpointer to the generic part of the connection */
119 conn->u.rtp.conn = conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200120
121 end->rtp.fd = -1;
122 end->rtcp.fd = -1;
Harald Weltea0ac30f2017-12-25 09:52:30 +0100123 memset(&end->stats, 0, sizeof(end->stats));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200124 end->rtp_port = end->rtcp_port = 0;
125 talloc_free(end->fmtp_extra);
126 end->fmtp_extra = NULL;
127
128 /* Set default values */
129 end->frames_per_packet = 0; /* unknown */
130 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
131 end->output_enabled = 0;
132
Philipp Maier77f76d02018-03-16 12:37:49 +0100133 mgcp_rtp_codec_init(&end->codec);
134 mgcp_rtp_codec_init(&end->alt_codec);
Philipp Maier7181cc12018-03-28 16:20:14 +0200135
136 conn_rtp->rate_ctr_group =
137 rate_ctr_group_alloc(conn, &rate_ctr_group_desc,
138 rate_ctr_index);
139 conn_rtp->state.in_stream.err_ts_ctr =
140 &conn_rtp->rate_ctr_group->ctr[0];
141 conn_rtp->state.out_stream.err_ts_ctr =
142 &conn_rtp->rate_ctr_group->ctr[1];
143 rate_ctr_index++;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200144}
145
Philipp Maierd0b470d2018-03-16 12:45:53 +0100146/* Cleanup rtp connection struct */
147static void mgcp_rtp_conn_cleanup(struct mgcp_conn_rtp *conn_rtp)
148{
149 osmux_disable_conn(conn_rtp);
150 osmux_release_cid(conn_rtp);
151 mgcp_free_rtp_port(&conn_rtp->end);
Philipp Maier7181cc12018-03-28 16:20:14 +0200152 rate_ctr_group_free(conn_rtp->rate_ctr_group);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100153}
154
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155/*! allocate a new connection list entry.
156 * \param[in] ctx talloc context
157 * \param[in] endp associated endpoint
158 * \param[in] id identification number of the connection
159 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
160 * \returns pointer to allocated connection, NULL on error */
161struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100162 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200163{
164 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100165 int rc;
166
Philipp Maier87bd9be2017-08-22 16:35:41 +0200167 /* Do not allow more then two connections */
168 if (llist_count(&endp->conns) >= endp->type->max_conns)
169 return NULL;
170
Philipp Maier87bd9be2017-08-22 16:35:41 +0200171 /* Create new connection and add it to the list */
172 conn = talloc_zero(ctx, struct mgcp_conn);
173 if (!conn)
174 return NULL;
175 conn->endp = endp;
176 conn->type = type;
177 conn->mode = MGCP_CONN_NONE;
178 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100179 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100180 rc = mgcp_alloc_id(endp, conn->id);
181 if (rc < 0) {
182 talloc_free(conn);
183 return NULL;
184 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200185
186 switch (type) {
187 case MGCP_CONN_TYPE_RTP:
Philipp Maier892dec02018-03-16 12:32:01 +0100188 mgcp_rtp_conn_init(&conn->u.rtp, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189 break;
190 default:
191 /* NOTE: This should never be called with an
192 * invalid type, its up to the programmer
193 * to ensure propery types */
194 OSMO_ASSERT(false);
195 }
196
197 llist_add(&conn->entry, &endp->conns);
198
199 return conn;
200}
201
202/*! find a 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 *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200208 struct mgcp_conn *conn;
209
210 llist_for_each_entry(conn, &endp->conns, entry) {
Philipp Maier01d24a32017-11-21 17:26:09 +0100211 if (strncmp(conn->id, id, sizeof(conn->id)) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212 return conn;
213 }
214
215 return NULL;
216}
217
218/*! find an RTP connection by its ID.
219 * \param[in] endp associated endpoint
220 * \param[in] id identification number of the connection
221 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100222struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
223 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200224{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225 struct mgcp_conn *conn;
226
227 conn = mgcp_conn_get(endp, id);
228 if (!conn)
229 return NULL;
230
231 if (conn->type == MGCP_CONN_TYPE_RTP)
232 return &conn->u.rtp;
233
234 return NULL;
235}
236
237/*! free a connection by its ID.
238 * \param[in] endp associated endpoint
239 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100240void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200242 struct mgcp_conn *conn;
243
244 conn = mgcp_conn_get(endp, id);
245 if (!conn)
246 return;
247
Philipp Maierdf5d2192018-01-24 11:39:32 +0100248 /* Run endpoint cleanup action. By this we inform the endpoint about
249 * the removal of the connection and allow it to clean up its inner
250 * state accordingly */
251 if (endp->type->cleanup_cb)
252 endp->type->cleanup_cb(endp, conn);
253
Philipp Maier87bd9be2017-08-22 16:35:41 +0200254 switch (conn->type) {
255 case MGCP_CONN_TYPE_RTP:
Philipp Maierd0b470d2018-03-16 12:45:53 +0100256 mgcp_rtp_conn_cleanup(&conn->u.rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200257 break;
258 default:
259 /* NOTE: This should never be called with an
260 * invalid type, its up to the programmer
261 * to ensure propery types */
262 OSMO_ASSERT(false);
263 }
264
265 llist_del(&conn->entry);
266 talloc_free(conn);
267}
268
269/*! free oldest connection in the list.
270 * \param[in] endp associated endpoint */
271void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
272{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200273 struct mgcp_conn *conn;
274
275 if (llist_empty(&endp->conns))
276 return;
277
278 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
279 if (!conn)
280 return;
281
282 mgcp_conn_free(endp, conn->id);
283}
284
285/*! free all connections at once.
286 * \param[in] endp associated endpoint */
287void mgcp_conn_free_all(struct mgcp_endpoint *endp)
288{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200289 struct mgcp_conn *conn;
290 struct mgcp_conn *conn_tmp;
291
292 /* Drop all items in the list */
293 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
294 mgcp_conn_free(endp, conn->id);
295 }
296
297 return;
298}
299
Harald Welte1d1b98f2017-12-25 10:03:40 +0100300/*! dump basic connection information to human readable string.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200301 * \param[in] conn to dump
Harald Welte1d1b98f2017-12-25 10:03:40 +0100302 * \returns human readable string */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200303char *mgcp_conn_dump(struct mgcp_conn *conn)
304{
Philipp Maier01d24a32017-11-21 17:26:09 +0100305 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200306
307 if (!conn) {
308 snprintf(str, sizeof(str), "(null connection)");
309 return str;
310 }
311
312 switch (conn->type) {
313 case MGCP_CONN_TYPE_RTP:
314 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100315 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200316 "rtp:%u rtcp:%u)",
317 conn->name,
318 conn->id,
319 inet_ntoa(conn->u.rtp.end.addr),
320 ntohs(conn->u.rtp.end.rtp_port),
321 ntohs(conn->u.rtp.end.rtcp_port));
322 break;
323
324 default:
325 /* Should not happen, we should be able to dump
326 * every possible connection type. */
327 snprintf(str, sizeof(str), "(unknown connection type)");
328 break;
329 }
330
331 return str;
332}
333
334/*! find destination connection on a specific endpoint.
335 * \param[in] conn to search a destination for
336 * \returns destination connection, NULL on failure */
337struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
338{
339 struct mgcp_endpoint *endp;
340 struct mgcp_conn *partner_conn;
341 endp = conn->endp;
342
343 /*! NOTE: This simply works by grabbing the first connection that is
344 * not the supplied connection, which is suitable for endpoints that
345 * do not serve more than two connections. */
346
347 llist_for_each_entry(partner_conn, &endp->conns, entry) {
348 if (conn != partner_conn) {
349 return partner_conn;
350 }
351 }
352
353 return NULL;
354}