blob: 31713cb13555104e1575bc21be8158eee09d241f [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 Maier87bd9be2017-08-22 16:35:41 +020028
29/* Reset codec state and free memory */
30static void mgcp_rtp_codec_reset(struct mgcp_rtp_codec *codec)
31{
32 codec->payload_type = -1;
33 codec->subtype_name = NULL;
34 codec->audio_name = NULL;
35 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
36 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
37 codec->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
38 codec->channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
39
40 /* see also mgcp_sdp.c, mgcp_set_audio_info() */
41 talloc_free(codec->subtype_name);
42 talloc_free(codec->audio_name);
43}
44
45/* Reset states, free memory, set defaults and reset codec state */
46static void mgcp_rtp_conn_reset(struct mgcp_conn_rtp *conn)
47{
48 struct mgcp_rtp_end *end = &conn->end;
49
50 conn->type = MGCP_RTP_DEFAULT;
51 conn->osmux.allocated_cid = -1;
52
53 end->rtp.fd = -1;
54 end->rtcp.fd = -1;
55 end->local_port = 0;
56 end->packets_rx = 0;
57 end->octets_rx = 0;
58 end->packets_tx = 0;
59 end->octets_tx = 0;
60 end->dropped_packets = 0;
61 end->rtp_port = end->rtcp_port = 0;
62 talloc_free(end->fmtp_extra);
63 end->fmtp_extra = NULL;
64
65 /* Set default values */
66 end->frames_per_packet = 0; /* unknown */
67 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
68 end->output_enabled = 0;
69
70 mgcp_rtp_codec_reset(&end->codec);
71 mgcp_rtp_codec_reset(&end->alt_codec);
72}
73
74/*! allocate a new connection list entry.
75 * \param[in] ctx talloc context
76 * \param[in] endp associated endpoint
77 * \param[in] id identification number of the connection
78 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
79 * \returns pointer to allocated connection, NULL on error */
80struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maier01d24a32017-11-21 17:26:09 +010081 const char *id, enum mgcp_conn_type type,
Philipp Maier87bd9be2017-08-22 16:35:41 +020082 char *name)
83{
84 struct mgcp_conn *conn;
85 OSMO_ASSERT(endp);
86 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
87 OSMO_ASSERT(strlen(name) < sizeof(conn->name));
88
Philipp Maier01d24a32017-11-21 17:26:09 +010089 /* Id is a mandatory parameter */
90 if (!id)
91 return NULL;
92
93 /* Prevent over long id strings */
94 if (strlen(id) >= MGCP_CONN_ID_LENGTH)
95 return NULL;
96
Philipp Maier87bd9be2017-08-22 16:35:41 +020097 /* Do not allow more then two connections */
98 if (llist_count(&endp->conns) >= endp->type->max_conns)
99 return NULL;
100
101 /* Prevent duplicate connection IDs */
102 if (mgcp_conn_get(endp, id))
103 return NULL;
104
105 /* Create new connection and add it to the list */
106 conn = talloc_zero(ctx, struct mgcp_conn);
107 if (!conn)
108 return NULL;
109 conn->endp = endp;
110 conn->type = type;
111 conn->mode = MGCP_CONN_NONE;
112 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200113 conn->u.rtp.conn = conn;
114 strcpy(conn->name, name);
Philipp Maier01d24a32017-11-21 17:26:09 +0100115 osmo_strlcpy(conn->id, id, sizeof(conn->id));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200116
117 switch (type) {
118 case MGCP_CONN_TYPE_RTP:
119 mgcp_rtp_conn_reset(&conn->u.rtp);
120 break;
121 default:
122 /* NOTE: This should never be called with an
123 * invalid type, its up to the programmer
124 * to ensure propery types */
125 OSMO_ASSERT(false);
126 }
127
128 llist_add(&conn->entry, &endp->conns);
129
130 return conn;
131}
132
133/*! find a connection by its ID.
134 * \param[in] endp associated endpoint
135 * \param[in] id identification number of the connection
136 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100137struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200138{
139 OSMO_ASSERT(endp);
Philipp Maier01d24a32017-11-21 17:26:09 +0100140 OSMO_ASSERT(id);
141 OSMO_ASSERT(strlen(id) < MGCP_CONN_ID_LENGTH);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200142 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
143
144 struct mgcp_conn *conn;
145
146 llist_for_each_entry(conn, &endp->conns, entry) {
Philipp Maier01d24a32017-11-21 17:26:09 +0100147 if (strncmp(conn->id, id, sizeof(conn->id)) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148 return conn;
149 }
150
151 return NULL;
152}
153
154/*! find an RTP connection by its ID.
155 * \param[in] endp associated endpoint
156 * \param[in] id identification number of the connection
157 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100158struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
159 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160{
161 OSMO_ASSERT(endp);
Philipp Maier01d24a32017-11-21 17:26:09 +0100162 OSMO_ASSERT(id);
163 OSMO_ASSERT(strlen(id) < MGCP_CONN_ID_LENGTH);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200164 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
165
166 struct mgcp_conn *conn;
167
168 conn = mgcp_conn_get(endp, id);
169 if (!conn)
170 return NULL;
171
172 if (conn->type == MGCP_CONN_TYPE_RTP)
173 return &conn->u.rtp;
174
175 return NULL;
176}
177
178/*! free a connection by its ID.
179 * \param[in] endp associated endpoint
180 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100181void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182{
183 OSMO_ASSERT(endp);
Philipp Maier01d24a32017-11-21 17:26:09 +0100184 OSMO_ASSERT(id);
185 OSMO_ASSERT(strlen(id) < MGCP_CONN_ID_LENGTH);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200186 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
187
188 struct mgcp_conn *conn;
189
190 conn = mgcp_conn_get(endp, id);
191 if (!conn)
192 return;
193
194 switch (conn->type) {
195 case MGCP_CONN_TYPE_RTP:
196 osmux_disable_conn(&conn->u.rtp);
197 osmux_release_cid(&conn->u.rtp);
198 mgcp_free_rtp_port(&conn->u.rtp.end);
199 break;
200 default:
201 /* NOTE: This should never be called with an
202 * invalid type, its up to the programmer
203 * to ensure propery types */
204 OSMO_ASSERT(false);
205 }
206
207 llist_del(&conn->entry);
208 talloc_free(conn);
209}
210
211/*! free oldest connection in the list.
212 * \param[in] endp associated endpoint */
213void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
214{
215 OSMO_ASSERT(endp);
216 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
217
218 struct mgcp_conn *conn;
219
220 if (llist_empty(&endp->conns))
221 return;
222
223 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
224 if (!conn)
225 return;
226
227 mgcp_conn_free(endp, conn->id);
228}
229
230/*! free all connections at once.
231 * \param[in] endp associated endpoint */
232void mgcp_conn_free_all(struct mgcp_endpoint *endp)
233{
234 OSMO_ASSERT(endp);
235 OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
236
237 struct mgcp_conn *conn;
238 struct mgcp_conn *conn_tmp;
239
240 /* Drop all items in the list */
241 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
242 mgcp_conn_free(endp, conn->id);
243 }
244
245 return;
246}
247
248/*! dump basic connection information to human readble string.
249 * \param[in] conn to dump
250 * \returns human readble string */
251char *mgcp_conn_dump(struct mgcp_conn *conn)
252{
Philipp Maier01d24a32017-11-21 17:26:09 +0100253 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200254
255 if (!conn) {
256 snprintf(str, sizeof(str), "(null connection)");
257 return str;
258 }
259
260 switch (conn->type) {
261 case MGCP_CONN_TYPE_RTP:
262 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100263 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264 "rtp:%u rtcp:%u)",
265 conn->name,
266 conn->id,
267 inet_ntoa(conn->u.rtp.end.addr),
268 ntohs(conn->u.rtp.end.rtp_port),
269 ntohs(conn->u.rtp.end.rtcp_port));
270 break;
271
272 default:
273 /* Should not happen, we should be able to dump
274 * every possible connection type. */
275 snprintf(str, sizeof(str), "(unknown connection type)");
276 break;
277 }
278
279 return str;
280}
281
282/*! find destination connection on a specific endpoint.
283 * \param[in] conn to search a destination for
284 * \returns destination connection, NULL on failure */
285struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
286{
287 struct mgcp_endpoint *endp;
288 struct mgcp_conn *partner_conn;
289 endp = conn->endp;
290
291 /*! NOTE: This simply works by grabbing the first connection that is
292 * not the supplied connection, which is suitable for endpoints that
293 * do not serve more than two connections. */
294
295 llist_for_each_entry(partner_conn, &endp->conns, entry) {
296 if (conn != partner_conn) {
297 return partner_conn;
298 }
299 }
300
301 return NULL;
302}