blob: dcfc2ff9de3043e72cc02edb20ae59737373580a [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 Maierfde78ad2017-10-13 12:05:37 +020027#include <osmocom/mgcp/mgcp_ep.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
64 LOGP(DLMGCP, LOGL_ERROR, "endpoint:%x, unable to generate a unique connectionIdentifier\n",
65 ENDPOINT_NUMBER(endp));
66
67 return -1;
68}
Philipp Maier87bd9be2017-08-22 16:35:41 +020069
70/* Reset codec state and free memory */
71static void mgcp_rtp_codec_reset(struct mgcp_rtp_codec *codec)
72{
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
86/* Reset states, free memory, set defaults and reset codec state */
87static void mgcp_rtp_conn_reset(struct mgcp_conn_rtp *conn)
88{
89 struct mgcp_rtp_end *end = &conn->end;
90
91 conn->type = MGCP_RTP_DEFAULT;
92 conn->osmux.allocated_cid = -1;
93
94 end->rtp.fd = -1;
95 end->rtcp.fd = -1;
96 end->local_port = 0;
97 end->packets_rx = 0;
98 end->octets_rx = 0;
99 end->packets_tx = 0;
100 end->octets_tx = 0;
101 end->dropped_packets = 0;
102 end->rtp_port = end->rtcp_port = 0;
103 talloc_free(end->fmtp_extra);
104 end->fmtp_extra = NULL;
105
106 /* Set default values */
107 end->frames_per_packet = 0; /* unknown */
108 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
109 end->output_enabled = 0;
110
111 mgcp_rtp_codec_reset(&end->codec);
112 mgcp_rtp_codec_reset(&end->alt_codec);
113}
114
115/*! allocate a new connection list entry.
116 * \param[in] ctx talloc context
117 * \param[in] endp associated endpoint
118 * \param[in] id identification number of the connection
119 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
120 * \returns pointer to allocated connection, NULL on error */
121struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100122 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123{
124 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100125 int rc;
126
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127 OSMO_ASSERT(endp);
128 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
129 OSMO_ASSERT(strlen(name) < sizeof(conn->name));
130
131 /* Do not allow more then two connections */
132 if (llist_count(&endp->conns) >= endp->type->max_conns)
133 return NULL;
134
Philipp Maier87bd9be2017-08-22 16:35:41 +0200135 /* Create new connection and add it to the list */
136 conn = talloc_zero(ctx, struct mgcp_conn);
137 if (!conn)
138 return NULL;
139 conn->endp = endp;
140 conn->type = type;
141 conn->mode = MGCP_CONN_NONE;
142 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143 conn->u.rtp.conn = conn;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100144 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100145 rc = mgcp_alloc_id(endp, conn->id);
146 if (rc < 0) {
147 talloc_free(conn);
148 return NULL;
149 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200150
151 switch (type) {
152 case MGCP_CONN_TYPE_RTP:
153 mgcp_rtp_conn_reset(&conn->u.rtp);
154 break;
155 default:
156 /* NOTE: This should never be called with an
157 * invalid type, its up to the programmer
158 * to ensure propery types */
159 OSMO_ASSERT(false);
160 }
161
162 llist_add(&conn->entry, &endp->conns);
163
164 return conn;
165}
166
167/*! find a connection by its ID.
168 * \param[in] endp associated endpoint
169 * \param[in] id identification number of the connection
170 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100171struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172{
173 OSMO_ASSERT(endp);
Philipp Maier01d24a32017-11-21 17:26:09 +0100174 OSMO_ASSERT(id);
175 OSMO_ASSERT(strlen(id) < MGCP_CONN_ID_LENGTH);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
177
178 struct mgcp_conn *conn;
179
180 llist_for_each_entry(conn, &endp->conns, entry) {
Philipp Maier01d24a32017-11-21 17:26:09 +0100181 if (strncmp(conn->id, id, sizeof(conn->id)) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182 return conn;
183 }
184
185 return NULL;
186}
187
188/*! find an RTP connection by its ID.
189 * \param[in] endp associated endpoint
190 * \param[in] id identification number of the connection
191 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100192struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
193 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200194{
195 OSMO_ASSERT(endp);
Philipp Maier01d24a32017-11-21 17:26:09 +0100196 OSMO_ASSERT(id);
197 OSMO_ASSERT(strlen(id) < MGCP_CONN_ID_LENGTH);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200198 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
199
200 struct mgcp_conn *conn;
201
202 conn = mgcp_conn_get(endp, id);
203 if (!conn)
204 return NULL;
205
206 if (conn->type == MGCP_CONN_TYPE_RTP)
207 return &conn->u.rtp;
208
209 return NULL;
210}
211
212/*! free a connection by its ID.
213 * \param[in] endp associated endpoint
214 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100215void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216{
217 OSMO_ASSERT(endp);
Philipp Maier01d24a32017-11-21 17:26:09 +0100218 OSMO_ASSERT(id);
219 OSMO_ASSERT(strlen(id) < MGCP_CONN_ID_LENGTH);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200220 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
221
222 struct mgcp_conn *conn;
223
224 conn = mgcp_conn_get(endp, id);
225 if (!conn)
226 return;
227
228 switch (conn->type) {
229 case MGCP_CONN_TYPE_RTP:
230 osmux_disable_conn(&conn->u.rtp);
231 osmux_release_cid(&conn->u.rtp);
232 mgcp_free_rtp_port(&conn->u.rtp.end);
233 break;
234 default:
235 /* NOTE: This should never be called with an
236 * invalid type, its up to the programmer
237 * to ensure propery types */
238 OSMO_ASSERT(false);
239 }
240
241 llist_del(&conn->entry);
242 talloc_free(conn);
243}
244
245/*! free oldest connection in the list.
246 * \param[in] endp associated endpoint */
247void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
248{
249 OSMO_ASSERT(endp);
250 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
251
252 struct mgcp_conn *conn;
253
254 if (llist_empty(&endp->conns))
255 return;
256
257 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
258 if (!conn)
259 return;
260
261 mgcp_conn_free(endp, conn->id);
262}
263
264/*! free all connections at once.
265 * \param[in] endp associated endpoint */
266void mgcp_conn_free_all(struct mgcp_endpoint *endp)
267{
268 OSMO_ASSERT(endp);
269 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
270
271 struct mgcp_conn *conn;
272 struct mgcp_conn *conn_tmp;
273
274 /* Drop all items in the list */
275 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
276 mgcp_conn_free(endp, conn->id);
277 }
278
279 return;
280}
281
282/*! dump basic connection information to human readble string.
283 * \param[in] conn to dump
284 * \returns human readble string */
285char *mgcp_conn_dump(struct mgcp_conn *conn)
286{
Philipp Maier01d24a32017-11-21 17:26:09 +0100287 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200288
289 if (!conn) {
290 snprintf(str, sizeof(str), "(null connection)");
291 return str;
292 }
293
294 switch (conn->type) {
295 case MGCP_CONN_TYPE_RTP:
296 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100297 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200298 "rtp:%u rtcp:%u)",
299 conn->name,
300 conn->id,
301 inet_ntoa(conn->u.rtp.end.addr),
302 ntohs(conn->u.rtp.end.rtp_port),
303 ntohs(conn->u.rtp.end.rtcp_port));
304 break;
305
306 default:
307 /* Should not happen, we should be able to dump
308 * every possible connection type. */
309 snprintf(str, sizeof(str), "(unknown connection type)");
310 break;
311 }
312
313 return str;
314}
315
316/*! find destination connection on a specific endpoint.
317 * \param[in] conn to search a destination for
318 * \returns destination connection, NULL on failure */
319struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
320{
321 struct mgcp_endpoint *endp;
322 struct mgcp_conn *partner_conn;
323 endp = conn->endp;
324
325 /*! NOTE: This simply works by grabbing the first connection that is
326 * not the supplied connection, which is suitable for endpoints that
327 * do not serve more than two connections. */
328
329 llist_for_each_entry(partner_conn, &endp->conns, entry) {
330 if (conn != partner_conn) {
331 return partner_conn;
332 }
333 }
334
335 return NULL;
336}