blob: 949121398919de2190c6f87b0fa207e1293a9bc5 [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 */
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
109 mgcp_rtp_codec_reset(&end->codec);
110 mgcp_rtp_codec_reset(&end->alt_codec);
111}
112
113/*! allocate a new connection list entry.
114 * \param[in] ctx talloc context
115 * \param[in] endp associated endpoint
116 * \param[in] id identification number of the connection
117 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
118 * \returns pointer to allocated connection, NULL on error */
119struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100120 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200121{
122 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100123 int rc;
124
Philipp Maier87bd9be2017-08-22 16:35:41 +0200125 /* Do not allow more then two connections */
126 if (llist_count(&endp->conns) >= endp->type->max_conns)
127 return NULL;
128
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129 /* Create new connection and add it to the list */
130 conn = talloc_zero(ctx, struct mgcp_conn);
131 if (!conn)
132 return NULL;
133 conn->endp = endp;
134 conn->type = type;
135 conn->mode = MGCP_CONN_NONE;
136 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100137 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100138 rc = mgcp_alloc_id(endp, conn->id);
139 if (rc < 0) {
140 talloc_free(conn);
141 return NULL;
142 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143
144 switch (type) {
145 case MGCP_CONN_TYPE_RTP:
Philipp Maier892dec02018-03-16 12:32:01 +0100146 mgcp_rtp_conn_init(&conn->u.rtp, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200147 break;
148 default:
149 /* NOTE: This should never be called with an
150 * invalid type, its up to the programmer
151 * to ensure propery types */
152 OSMO_ASSERT(false);
153 }
154
155 llist_add(&conn->entry, &endp->conns);
156
157 return conn;
158}
159
160/*! find a connection by its ID.
161 * \param[in] endp associated endpoint
162 * \param[in] id identification number of the connection
163 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100164struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200166 struct mgcp_conn *conn;
167
168 llist_for_each_entry(conn, &endp->conns, entry) {
Philipp Maier01d24a32017-11-21 17:26:09 +0100169 if (strncmp(conn->id, id, sizeof(conn->id)) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200170 return conn;
171 }
172
173 return NULL;
174}
175
176/*! find an RTP connection by its ID.
177 * \param[in] endp associated endpoint
178 * \param[in] id identification number of the connection
179 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100180struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
181 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200183 struct mgcp_conn *conn;
184
185 conn = mgcp_conn_get(endp, id);
186 if (!conn)
187 return NULL;
188
189 if (conn->type == MGCP_CONN_TYPE_RTP)
190 return &conn->u.rtp;
191
192 return NULL;
193}
194
195/*! free a connection by its ID.
196 * \param[in] endp associated endpoint
197 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100198void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200199{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200200 struct mgcp_conn *conn;
201
202 conn = mgcp_conn_get(endp, id);
203 if (!conn)
204 return;
205
Philipp Maierdf5d2192018-01-24 11:39:32 +0100206 /* Run endpoint cleanup action. By this we inform the endpoint about
207 * the removal of the connection and allow it to clean up its inner
208 * state accordingly */
209 if (endp->type->cleanup_cb)
210 endp->type->cleanup_cb(endp, conn);
211
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212 switch (conn->type) {
213 case MGCP_CONN_TYPE_RTP:
214 osmux_disable_conn(&conn->u.rtp);
215 osmux_release_cid(&conn->u.rtp);
216 mgcp_free_rtp_port(&conn->u.rtp.end);
217 break;
218 default:
219 /* NOTE: This should never be called with an
220 * invalid type, its up to the programmer
221 * to ensure propery types */
222 OSMO_ASSERT(false);
223 }
224
225 llist_del(&conn->entry);
226 talloc_free(conn);
227}
228
229/*! free oldest connection in the list.
230 * \param[in] endp associated endpoint */
231void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
232{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 struct mgcp_conn *conn;
234
235 if (llist_empty(&endp->conns))
236 return;
237
238 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
239 if (!conn)
240 return;
241
242 mgcp_conn_free(endp, conn->id);
243}
244
245/*! free all connections at once.
246 * \param[in] endp associated endpoint */
247void mgcp_conn_free_all(struct mgcp_endpoint *endp)
248{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200249 struct mgcp_conn *conn;
250 struct mgcp_conn *conn_tmp;
251
252 /* Drop all items in the list */
253 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
254 mgcp_conn_free(endp, conn->id);
255 }
256
257 return;
258}
259
Harald Welte1d1b98f2017-12-25 10:03:40 +0100260/*! dump basic connection information to human readable string.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200261 * \param[in] conn to dump
Harald Welte1d1b98f2017-12-25 10:03:40 +0100262 * \returns human readable string */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200263char *mgcp_conn_dump(struct mgcp_conn *conn)
264{
Philipp Maier01d24a32017-11-21 17:26:09 +0100265 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200266
267 if (!conn) {
268 snprintf(str, sizeof(str), "(null connection)");
269 return str;
270 }
271
272 switch (conn->type) {
273 case MGCP_CONN_TYPE_RTP:
274 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100275 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200276 "rtp:%u rtcp:%u)",
277 conn->name,
278 conn->id,
279 inet_ntoa(conn->u.rtp.end.addr),
280 ntohs(conn->u.rtp.end.rtp_port),
281 ntohs(conn->u.rtp.end.rtcp_port));
282 break;
283
284 default:
285 /* Should not happen, we should be able to dump
286 * every possible connection type. */
287 snprintf(str, sizeof(str), "(unknown connection type)");
288 break;
289 }
290
291 return str;
292}
293
294/*! find destination connection on a specific endpoint.
295 * \param[in] conn to search a destination for
296 * \returns destination connection, NULL on failure */
297struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
298{
299 struct mgcp_endpoint *endp;
300 struct mgcp_conn *partner_conn;
301 endp = conn->endp;
302
303 /*! NOTE: This simply works by grabbing the first connection that is
304 * not the supplied connection, which is suitable for endpoints that
305 * do not serve more than two connections. */
306
307 llist_for_each_entry(partner_conn, &endp->conns, entry) {
308 if (conn != partner_conn) {
309 return partner_conn;
310 }
311 }
312
313 return NULL;
314}