blob: 998dbc58e0f360ce3fd34abb1c3b60b9b7823917 [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>
29#include <ctype.h>
30
31/* Allocate a new connection identifier. According to RFC3435, they must
Philipp Maierf8bfbe82017-11-23 19:32:31 +010032 * be unique only within the scope of the endpoint. (Caller must provide
33 * memory for id) */
Philipp Maierffd75e42017-11-22 11:44:50 +010034static 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 Maier230e4fc2017-11-28 09:38:45 +010064 LOGP(DLMGCP, LOGL_ERROR, "endpoint:0x%x, unable to generate a unique connectionIdentifier\n",
Philipp Maierffd75e42017-11-22 11:44:50 +010065 ENDPOINT_NUMBER(endp));
66
67 return -1;
68}
Philipp Maier87bd9be2017-08-22 16:35:41 +020069
70/* Reset codec state and free memory */
Philipp Maier77f76d02018-03-16 12:37:49 +010071static void mgcp_rtp_codec_init(struct mgcp_rtp_codec *codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +020072{
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
Philipp Maierc430d192018-03-16 12:11:18 +010086/* Initialize rtp connection struct with default values */
Philipp Maier892dec02018-03-16 12:32:01 +010087static void mgcp_rtp_conn_init(struct mgcp_conn_rtp *conn_rtp, struct mgcp_conn *conn)
Philipp Maier87bd9be2017-08-22 16:35:41 +020088{
Philipp Maier892dec02018-03-16 12:32:01 +010089 struct mgcp_rtp_end *end = &conn_rtp->end;
Philipp Maier87bd9be2017-08-22 16:35:41 +020090
Philipp Maier892dec02018-03-16 12:32:01 +010091 conn_rtp->type = MGCP_RTP_DEFAULT;
92 conn_rtp->osmux.allocated_cid = -1;
93
94 /* backpointer to the generic part of the connection */
95 conn->u.rtp.conn = conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +020096
97 end->rtp.fd = -1;
98 end->rtcp.fd = -1;
Harald Weltea0ac30f2017-12-25 09:52:30 +010099 memset(&end->stats, 0, sizeof(end->stats));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200100 end->rtp_port = end->rtcp_port = 0;
101 talloc_free(end->fmtp_extra);
102 end->fmtp_extra = NULL;
103
104 /* Set default values */
105 end->frames_per_packet = 0; /* unknown */
106 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
107 end->output_enabled = 0;
108
Philipp Maier77f76d02018-03-16 12:37:49 +0100109 mgcp_rtp_codec_init(&end->codec);
110 mgcp_rtp_codec_init(&end->alt_codec);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200111}
112
Philipp Maierd0b470d2018-03-16 12:45:53 +0100113/* Cleanup rtp connection struct */
114static void mgcp_rtp_conn_cleanup(struct mgcp_conn_rtp *conn_rtp)
115{
116 osmux_disable_conn(conn_rtp);
117 osmux_release_cid(conn_rtp);
118 mgcp_free_rtp_port(&conn_rtp->end);
119}
120
Philipp Maier87bd9be2017-08-22 16:35:41 +0200121/*! allocate a new connection list entry.
122 * \param[in] ctx talloc context
123 * \param[in] endp associated endpoint
124 * \param[in] id identification number of the connection
125 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
126 * \returns pointer to allocated connection, NULL on error */
127struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100128 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129{
130 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100131 int rc;
132
Philipp Maier87bd9be2017-08-22 16:35:41 +0200133 /* Do not allow more then two connections */
134 if (llist_count(&endp->conns) >= endp->type->max_conns)
135 return NULL;
136
Philipp Maier87bd9be2017-08-22 16:35:41 +0200137 /* Create new connection and add it to the list */
138 conn = talloc_zero(ctx, struct mgcp_conn);
139 if (!conn)
140 return NULL;
141 conn->endp = endp;
142 conn->type = type;
143 conn->mode = MGCP_CONN_NONE;
144 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100145 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100146 rc = mgcp_alloc_id(endp, conn->id);
147 if (rc < 0) {
148 talloc_free(conn);
149 return NULL;
150 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200151
152 switch (type) {
153 case MGCP_CONN_TYPE_RTP:
Philipp Maier892dec02018-03-16 12:32:01 +0100154 mgcp_rtp_conn_init(&conn->u.rtp, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155 break;
156 default:
157 /* NOTE: This should never be called with an
158 * invalid type, its up to the programmer
159 * to ensure propery types */
160 OSMO_ASSERT(false);
161 }
162
163 llist_add(&conn->entry, &endp->conns);
164
165 return conn;
166}
167
168/*! find a connection by its ID.
169 * \param[in] endp associated endpoint
170 * \param[in] id identification number of the connection
171 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100172struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174 struct mgcp_conn *conn;
175
176 llist_for_each_entry(conn, &endp->conns, entry) {
Philipp Maier01d24a32017-11-21 17:26:09 +0100177 if (strncmp(conn->id, id, sizeof(conn->id)) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200178 return conn;
179 }
180
181 return NULL;
182}
183
184/*! find an RTP connection by its ID.
185 * \param[in] endp associated endpoint
186 * \param[in] id identification number of the connection
187 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100188struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
189 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191 struct mgcp_conn *conn;
192
193 conn = mgcp_conn_get(endp, id);
194 if (!conn)
195 return NULL;
196
197 if (conn->type == MGCP_CONN_TYPE_RTP)
198 return &conn->u.rtp;
199
200 return NULL;
201}
202
203/*! free a connection by its ID.
204 * \param[in] endp associated endpoint
205 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100206void mgcp_conn_free(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 conn = mgcp_conn_get(endp, id);
211 if (!conn)
212 return;
213
Philipp Maierdf5d2192018-01-24 11:39:32 +0100214 /* Run endpoint cleanup action. By this we inform the endpoint about
215 * the removal of the connection and allow it to clean up its inner
216 * state accordingly */
217 if (endp->type->cleanup_cb)
218 endp->type->cleanup_cb(endp, conn);
219
Philipp Maier87bd9be2017-08-22 16:35:41 +0200220 switch (conn->type) {
221 case MGCP_CONN_TYPE_RTP:
Philipp Maierd0b470d2018-03-16 12:45:53 +0100222 mgcp_rtp_conn_cleanup(&conn->u.rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200223 break;
224 default:
225 /* NOTE: This should never be called with an
226 * invalid type, its up to the programmer
227 * to ensure propery types */
228 OSMO_ASSERT(false);
229 }
230
231 llist_del(&conn->entry);
232 talloc_free(conn);
233}
234
235/*! free oldest connection in the list.
236 * \param[in] endp associated endpoint */
237void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
238{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200239 struct mgcp_conn *conn;
240
241 if (llist_empty(&endp->conns))
242 return;
243
244 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
245 if (!conn)
246 return;
247
248 mgcp_conn_free(endp, conn->id);
249}
250
251/*! free all connections at once.
252 * \param[in] endp associated endpoint */
253void mgcp_conn_free_all(struct mgcp_endpoint *endp)
254{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200255 struct mgcp_conn *conn;
256 struct mgcp_conn *conn_tmp;
257
258 /* Drop all items in the list */
259 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
260 mgcp_conn_free(endp, conn->id);
261 }
262
263 return;
264}
265
Harald Welte1d1b98f2017-12-25 10:03:40 +0100266/*! dump basic connection information to human readable string.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200267 * \param[in] conn to dump
Harald Welte1d1b98f2017-12-25 10:03:40 +0100268 * \returns human readable string */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200269char *mgcp_conn_dump(struct mgcp_conn *conn)
270{
Philipp Maier01d24a32017-11-21 17:26:09 +0100271 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200272
273 if (!conn) {
274 snprintf(str, sizeof(str), "(null connection)");
275 return str;
276 }
277
278 switch (conn->type) {
279 case MGCP_CONN_TYPE_RTP:
280 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100281 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200282 "rtp:%u rtcp:%u)",
283 conn->name,
284 conn->id,
285 inet_ntoa(conn->u.rtp.end.addr),
286 ntohs(conn->u.rtp.end.rtp_port),
287 ntohs(conn->u.rtp.end.rtcp_port));
288 break;
289
290 default:
291 /* Should not happen, we should be able to dump
292 * every possible connection type. */
293 snprintf(str, sizeof(str), "(unknown connection type)");
294 break;
295 }
296
297 return str;
298}
299
300/*! find destination connection on a specific endpoint.
301 * \param[in] conn to search a destination for
302 * \returns destination connection, NULL on failure */
303struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
304{
305 struct mgcp_endpoint *endp;
306 struct mgcp_conn *partner_conn;
307 endp = conn->endp;
308
309 /*! NOTE: This simply works by grabbing the first connection that is
310 * not the supplied connection, which is suitable for endpoints that
311 * do not serve more than two connections. */
312
313 llist_for_each_entry(partner_conn, &endp->conns, entry) {
314 if (conn != partner_conn) {
315 return partner_conn;
316 }
317 }
318
319 return NULL;
320}