blob: cfd81706e6d5f3f3cfb0bd03acf1ccc7507bb09e [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 */
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
Philipp Maierc430d192018-03-16 12:11:18 +010086/* Initialize rtp connection struct with default values */
87static void mgcp_rtp_conn_init(struct mgcp_conn_rtp *conn)
Philipp Maier87bd9be2017-08-22 16:35:41 +020088{
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;
Harald Weltea0ac30f2017-12-25 09:52:30 +010096 memset(&end->stats, 0, sizeof(end->stats));
Philipp Maier87bd9be2017-08-22 16:35:41 +020097 end->rtp_port = end->rtcp_port = 0;
98 talloc_free(end->fmtp_extra);
99 end->fmtp_extra = NULL;
100
101 /* Set default values */
102 end->frames_per_packet = 0; /* unknown */
103 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
104 end->output_enabled = 0;
105
106 mgcp_rtp_codec_reset(&end->codec);
107 mgcp_rtp_codec_reset(&end->alt_codec);
108}
109
110/*! allocate a new connection list entry.
111 * \param[in] ctx talloc context
112 * \param[in] endp associated endpoint
113 * \param[in] id identification number of the connection
114 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
115 * \returns pointer to allocated connection, NULL on error */
116struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100117 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118{
119 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100120 int rc;
121
Philipp Maier87bd9be2017-08-22 16:35:41 +0200122 /* Do not allow more then two connections */
123 if (llist_count(&endp->conns) >= endp->type->max_conns)
124 return NULL;
125
Philipp Maier87bd9be2017-08-22 16:35:41 +0200126 /* Create new connection and add it to the list */
127 conn = talloc_zero(ctx, struct mgcp_conn);
128 if (!conn)
129 return NULL;
130 conn->endp = endp;
131 conn->type = type;
132 conn->mode = MGCP_CONN_NONE;
133 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200134 conn->u.rtp.conn = conn;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100135 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100136 rc = mgcp_alloc_id(endp, conn->id);
137 if (rc < 0) {
138 talloc_free(conn);
139 return NULL;
140 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200141
142 switch (type) {
143 case MGCP_CONN_TYPE_RTP:
Philipp Maierc430d192018-03-16 12:11:18 +0100144 mgcp_rtp_conn_init(&conn->u.rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145 break;
146 default:
147 /* NOTE: This should never be called with an
148 * invalid type, its up to the programmer
149 * to ensure propery types */
150 OSMO_ASSERT(false);
151 }
152
153 llist_add(&conn->entry, &endp->conns);
154
155 return conn;
156}
157
158/*! find a connection by its ID.
159 * \param[in] endp associated endpoint
160 * \param[in] id identification number of the connection
161 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100162struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200163{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200164 struct mgcp_conn *conn;
165
166 llist_for_each_entry(conn, &endp->conns, entry) {
Philipp Maier01d24a32017-11-21 17:26:09 +0100167 if (strncmp(conn->id, id, sizeof(conn->id)) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168 return conn;
169 }
170
171 return NULL;
172}
173
174/*! find an RTP connection by its ID.
175 * \param[in] endp associated endpoint
176 * \param[in] id identification number of the connection
177 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100178struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
179 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200180{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200181 struct mgcp_conn *conn;
182
183 conn = mgcp_conn_get(endp, id);
184 if (!conn)
185 return NULL;
186
187 if (conn->type == MGCP_CONN_TYPE_RTP)
188 return &conn->u.rtp;
189
190 return NULL;
191}
192
193/*! free a connection by its ID.
194 * \param[in] endp associated endpoint
195 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100196void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200198 struct mgcp_conn *conn;
199
200 conn = mgcp_conn_get(endp, id);
201 if (!conn)
202 return;
203
Philipp Maierdf5d2192018-01-24 11:39:32 +0100204 /* Run endpoint cleanup action. By this we inform the endpoint about
205 * the removal of the connection and allow it to clean up its inner
206 * state accordingly */
207 if (endp->type->cleanup_cb)
208 endp->type->cleanup_cb(endp, conn);
209
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210 switch (conn->type) {
211 case MGCP_CONN_TYPE_RTP:
212 osmux_disable_conn(&conn->u.rtp);
213 osmux_release_cid(&conn->u.rtp);
214 mgcp_free_rtp_port(&conn->u.rtp.end);
215 break;
216 default:
217 /* NOTE: This should never be called with an
218 * invalid type, its up to the programmer
219 * to ensure propery types */
220 OSMO_ASSERT(false);
221 }
222
223 llist_del(&conn->entry);
224 talloc_free(conn);
225}
226
227/*! free oldest connection in the list.
228 * \param[in] endp associated endpoint */
229void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
230{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200231 struct mgcp_conn *conn;
232
233 if (llist_empty(&endp->conns))
234 return;
235
236 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
237 if (!conn)
238 return;
239
240 mgcp_conn_free(endp, conn->id);
241}
242
243/*! free all connections at once.
244 * \param[in] endp associated endpoint */
245void mgcp_conn_free_all(struct mgcp_endpoint *endp)
246{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200247 struct mgcp_conn *conn;
248 struct mgcp_conn *conn_tmp;
249
250 /* Drop all items in the list */
251 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
252 mgcp_conn_free(endp, conn->id);
253 }
254
255 return;
256}
257
Harald Welte1d1b98f2017-12-25 10:03:40 +0100258/*! dump basic connection information to human readable string.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200259 * \param[in] conn to dump
Harald Welte1d1b98f2017-12-25 10:03:40 +0100260 * \returns human readable string */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200261char *mgcp_conn_dump(struct mgcp_conn *conn)
262{
Philipp Maier01d24a32017-11-21 17:26:09 +0100263 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264
265 if (!conn) {
266 snprintf(str, sizeof(str), "(null connection)");
267 return str;
268 }
269
270 switch (conn->type) {
271 case MGCP_CONN_TYPE_RTP:
272 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100273 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200274 "rtp:%u rtcp:%u)",
275 conn->name,
276 conn->id,
277 inet_ntoa(conn->u.rtp.end.addr),
278 ntohs(conn->u.rtp.end.rtp_port),
279 ntohs(conn->u.rtp.end.rtcp_port));
280 break;
281
282 default:
283 /* Should not happen, we should be able to dump
284 * every possible connection type. */
285 snprintf(str, sizeof(str), "(unknown connection type)");
286 break;
287 }
288
289 return str;
290}
291
292/*! find destination connection on a specific endpoint.
293 * \param[in] conn to search a destination for
294 * \returns destination connection, NULL on failure */
295struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
296{
297 struct mgcp_endpoint *endp;
298 struct mgcp_conn *partner_conn;
299 endp = conn->endp;
300
301 /*! NOTE: This simply works by grabbing the first connection that is
302 * not the supplied connection, which is suitable for endpoints that
303 * do not serve more than two connections. */
304
305 llist_for_each_entry(partner_conn, &endp->conns, entry) {
306 if (conn != partner_conn) {
307 return partner_conn;
308 }
309 }
310
311 return NULL;
312}