blob: 9063bec8326fcc8e6b76542b1c8e9f93f9d87451 [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
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
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;
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:
144 mgcp_rtp_conn_reset(&conn->u.rtp);
145 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
204 switch (conn->type) {
205 case MGCP_CONN_TYPE_RTP:
206 osmux_disable_conn(&conn->u.rtp);
207 osmux_release_cid(&conn->u.rtp);
208 mgcp_free_rtp_port(&conn->u.rtp.end);
209 break;
210 default:
211 /* NOTE: This should never be called with an
212 * invalid type, its up to the programmer
213 * to ensure propery types */
214 OSMO_ASSERT(false);
215 }
216
217 llist_del(&conn->entry);
218 talloc_free(conn);
219}
220
221/*! free oldest connection in the list.
222 * \param[in] endp associated endpoint */
223void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
224{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225 struct mgcp_conn *conn;
226
227 if (llist_empty(&endp->conns))
228 return;
229
230 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
231 if (!conn)
232 return;
233
234 mgcp_conn_free(endp, conn->id);
235}
236
237/*! free all connections at once.
238 * \param[in] endp associated endpoint */
239void mgcp_conn_free_all(struct mgcp_endpoint *endp)
240{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241 struct mgcp_conn *conn;
242 struct mgcp_conn *conn_tmp;
243
244 /* Drop all items in the list */
245 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
246 mgcp_conn_free(endp, conn->id);
247 }
248
249 return;
250}
251
252/*! dump basic connection information to human readble string.
253 * \param[in] conn to dump
254 * \returns human readble string */
255char *mgcp_conn_dump(struct mgcp_conn *conn)
256{
Philipp Maier01d24a32017-11-21 17:26:09 +0100257 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200258
259 if (!conn) {
260 snprintf(str, sizeof(str), "(null connection)");
261 return str;
262 }
263
264 switch (conn->type) {
265 case MGCP_CONN_TYPE_RTP:
266 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100267 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200268 "rtp:%u rtcp:%u)",
269 conn->name,
270 conn->id,
271 inet_ntoa(conn->u.rtp.end.addr),
272 ntohs(conn->u.rtp.end.rtp_port),
273 ntohs(conn->u.rtp.end.rtcp_port));
274 break;
275
276 default:
277 /* Should not happen, we should be able to dump
278 * every possible connection type. */
279 snprintf(str, sizeof(str), "(unknown connection type)");
280 break;
281 }
282
283 return str;
284}
285
286/*! find destination connection on a specific endpoint.
287 * \param[in] conn to search a destination for
288 * \returns destination connection, NULL on failure */
289struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
290{
291 struct mgcp_endpoint *endp;
292 struct mgcp_conn *partner_conn;
293 endp = conn->endp;
294
295 /*! NOTE: This simply works by grabbing the first connection that is
296 * not the supplied connection, which is suitable for endpoints that
297 * do not serve more than two connections. */
298
299 llist_for_each_entry(partner_conn, &endp->conns, entry) {
300 if (conn != partner_conn) {
301 return partner_conn;
302 }
303 }
304
305 return NULL;
306}