blob: 5823e2bab9095c74eb1beab528f1538e0542cbe3 [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001/* mgcp_utils - common functions to setup an MGCP connection
2 */
3/* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte220f4872018-02-04 09:04:16 +01007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
Neels Hofmeyre9920f22017-07-10 15:07:22 +02009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte220f4872018-02-04 09:04:16 +010014 * GNU General Public License for more details.
Neels Hofmeyre9920f22017-07-10 15:07:22 +020015 *
Harald Welte220f4872018-02-04 09:04:16 +010016 * You should have received a copy of the GNU General Public License
Neels Hofmeyre9920f22017-07-10 15:07:22 +020017 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <osmocom/core/linuxlist.h>
22#include <osmocom/core/select.h>
23#include <osmocom/core/write_queue.h>
24#include <osmocom/core/msgb.h>
25#include <osmocom/core/logging.h>
Philipp Maier1dc6be62017-10-05 18:25:37 +020026#include <osmocom/core/byteswap.h>
Harald Welte8890dfa2017-11-17 15:09:30 +010027#include <osmocom/core/socket.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020028
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020029#include <osmocom/mgcp_client/mgcp_client.h>
30#include <osmocom/mgcp_client/mgcp_client_internal.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020031
32#include <netinet/in.h>
33#include <arpa/inet.h>
34
35#include <errno.h>
36#include <unistd.h>
37#include <string.h>
Pau Espin Pedrol166077e2019-06-26 12:21:38 +020038#include <ctype.h>
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +020039#include <stdlib.h>
40#include <limits.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020041
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +020042#ifndef OSMUX_CID_MAX
43#define OSMUX_CID_MAX 255 /* FIXME: use OSMUX_CID_MAX from libosmo-netif? */
44#endif
45
Philipp Maier704c4f02018-06-07 18:51:31 +020046/* Codec descripton for dynamic payload types (SDP) */
Neels Hofmeyr47642f22019-02-27 05:56:53 +010047const struct value_string osmo_mgcpc_codec_names[] = {
Philipp Maier704c4f02018-06-07 18:51:31 +020048 { CODEC_PCMU_8000_1, "PCMU/8000/1" },
49 { CODEC_GSM_8000_1, "GSM/8000/1" },
50 { CODEC_PCMA_8000_1, "PCMA/8000/1" },
51 { CODEC_G729_8000_1, "G729/8000/1" },
52 { CODEC_GSMEFR_8000_1, "GSM-EFR/8000/1" },
53 { CODEC_GSMHR_8000_1, "GSM-HR-08/8000/1" },
54 { CODEC_AMR_8000_1, "AMR/8000/1" },
55 { CODEC_AMRWB_16000_1, "AMR-WB/16000/1" },
56 { 0, NULL },
57};
58
59/* Get encoding name from a full codec string e,g.
60 * ("CODEC/8000/2" => returns "CODEC") */
61static char *extract_codec_name(const char *str)
62{
63 static char buf[64];
64 unsigned int i;
65
66 if (!str)
67 return NULL;
68
69 /* FIXME osmo_strlcpy */
70 osmo_strlcpy(buf, str, sizeof(buf));
71
72 for (i = 0; i < strlen(buf); i++) {
73 if (buf[i] == '/')
74 buf[i] = '\0';
75 }
76
77 return buf;
78}
79
80/*! Map a string to a codec.
81 * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM")
82 * \returns codec that corresponds to the given string representation. */
83enum mgcp_codecs map_str_to_codec(const char *str)
84{
85 unsigned int i;
86 char *codec_name;
87 char str_buf[64];
88
89 osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf));
90
Neels Hofmeyr47642f22019-02-27 05:56:53 +010091 for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) {
92 codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str);
Philipp Maier704c4f02018-06-07 18:51:31 +020093 if (!codec_name)
94 continue;
95 if (strcmp(codec_name, str_buf) == 0)
Neels Hofmeyr47642f22019-02-27 05:56:53 +010096 return osmo_mgcpc_codec_names[i].value;
Philipp Maier704c4f02018-06-07 18:51:31 +020097 }
98
99 return -1;
100}
101
102/* Check the ptmap for illegal mappings */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200103static int check_ptmap(const struct ptmap *ptmap)
Philipp Maier704c4f02018-06-07 18:51:31 +0200104{
105 /* Check if there are mappings that leave the IANA assigned dynamic
106 * payload type range. Under normal conditions such mappings should
107 * not occur */
108
109 /* Its ok to have a 1:1 mapping in the statically defined
110 * range, this won't hurt */
111 if (ptmap->codec == ptmap->pt)
112 return 0;
113
114 if (ptmap->codec < 96 || ptmap->codec > 127)
115 goto error;
116 if (ptmap->pt < 96 || ptmap->pt > 127)
117 goto error;
118
119 return 0;
120error:
121 LOGP(DLMGCP, LOGL_ERROR,
122 "ptmap contains illegal mapping: codec=%u maps to pt=%u\n",
123 ptmap->codec, ptmap->pt);
124 return -1;
125}
126
127/*! Map a codec to a payload type.
128 * \ptmap[in] payload pointer to payload type map with specified payload types.
129 * \ptmap[in] ptmap_len length of the payload type map.
130 * \ptmap[in] codec the codec for which the payload type should be looked up.
131 * \returns assigned payload type */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200132unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len,
Philipp Maier704c4f02018-06-07 18:51:31 +0200133 enum mgcp_codecs codec)
134{
135 unsigned int i;
136
137 /*! Note: If the payload type map is empty or the codec is not found
138 * in the map, then a 1:1 mapping is performed. If the codec falls
139 * into the statically defined range or if the mapping table isself
140 * tries to map to the statically defined range, then the mapping
141 * is also ignored and a 1:1 mapping is performed instead. */
142
143 /* we may return the codec directly since enum mgcp_codecs directly
144 * corresponds to the statićally assigned payload types */
145 if (codec < 96 || codec > 127)
146 return codec;
147
148 for (i = 0; i < ptmap_len; i++) {
149 /* Skip illegal map entries */
150 if (check_ptmap(ptmap) == 0 && ptmap->codec == codec)
151 return ptmap->pt;
152 ptmap++;
153 }
154
155 /* If nothing is found, do not perform any mapping */
156 return codec;
157}
158
159/*! Map a payload type to a codec.
160 * \ptmap[in] payload pointer to payload type map with specified payload types.
161 * \ptmap[in] ptmap_len length of the payload type map.
162 * \ptmap[in] payload type for which the codec should be looked up.
163 * \returns codec that corresponds to the specified payload type */
164enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len,
165 unsigned int pt)
166{
167 unsigned int i;
168
169 /*! Note: If the payload type map is empty or the payload type is not
170 * found in the map, then a 1:1 mapping is performed. If the payload
171 * type falls into the statically defined range or if the mapping
172 * table isself tries to map to the statically defined range, then
173 * the mapping is also ignored and a 1:1 mapping is performed
174 * instead. */
175
176 /* See also note in map_codec_to_pt() */
177 if (pt < 96 || pt > 127)
178 return pt;
179
180 for (i = 0; i < ptmap_len; i++) {
181 if (check_ptmap(ptmap) == 0 && ptmap->pt == pt)
182 return ptmap->codec;
183 ptmap++;
184 }
185
186 /* If nothing is found, do not perform any mapping */
187 return pt;
188}
189
Philipp Maier275ac972018-01-20 00:58:16 +0100190/*! Initalize MGCP client configuration struct with default values.
191 * \param[out] conf Client configuration.*/
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200192void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200193{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200194 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
195 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200196 .local_addr = NULL,
197 .local_port = -1,
198 .remote_addr = NULL,
199 .remote_port = -1,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200200 };
201}
202
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200203static void mgcp_client_handle_response(struct mgcp_client *mgcp,
204 struct mgcp_response_pending *pending,
205 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200206{
207 if (!pending) {
208 LOGP(DLMGCP, LOGL_ERROR,
209 "Cannot handle NULL response\n");
210 return;
211 }
212 if (pending->response_cb)
213 pending->response_cb(response, pending->priv);
214 else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100215 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200216 talloc_free(pending);
217}
218
219static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
220{
221 int comment_pos;
222 char *end;
223
224 if (mgcp_msg_terminate_nul(msg))
225 goto response_parse_failure;
226
227 r->body = (char *)msg->data;
228
Harald Welte9bf7c532017-11-17 14:14:31 +0100229 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200230 &r->head.response_code, &r->head.trans_id,
231 &comment_pos) != 2)
232 goto response_parse_failure;
233
Philipp Maierabe8c892018-01-20 00:15:12 +0100234 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200235 end = strchr(r->head.comment, '\r');
236 if (!end)
237 goto response_parse_failure;
238 /* Mark the end of the comment */
239 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200240 return 0;
241
242response_parse_failure:
243 LOGP(DLMGCP, LOGL_ERROR,
244 "Failed to parse MGCP response header\n");
245 return -EINVAL;
246}
247
248/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
249static bool mgcp_line_is_valid(const char *line)
250{
251 const size_t line_len = strlen(line);
252 if (line[0] == '\0')
253 return true;
254
255 if (line_len < 2
256 || line[1] != '=') {
257 LOGP(DLMGCP, LOGL_ERROR,
258 "Wrong MGCP option format: '%s'\n",
259 line);
260 return false;
261 }
262
263 return true;
264}
265
Philipp Maier704c4f02018-06-07 18:51:31 +0200266/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
267static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200268{
Philipp Maier704c4f02018-06-07 18:51:31 +0200269 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200270 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200271 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200272 unsigned int count = 0;
273 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200274
Philipp Maier704c4f02018-06-07 18:51:31 +0200275 /* Extract port information */
276 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
277 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100278 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200279 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100280
Philipp Maier704c4f02018-06-07 18:51:31 +0200281 /* Extract payload types */
282 line = strstr(line, "RTP/AVP ");
283 if (!line)
284 goto exit;
285
286 pt_str = strtok(line, " ");
287 while (1) {
288 /* Do not allow excessive payload types */
289 if (count > ARRAY_SIZE(r->codecs))
290 goto response_parse_failure_pt;
291
292 pt_str = strtok(NULL, " ");
293 if (!pt_str)
294 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200295 errno = 0;
296 pt = strtoul(pt_str, &pt_end, 0);
297 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
298 pt_str == pt_end)
299 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200300
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200301 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
302 goto response_parse_failure_pt;
303
Philipp Maier704c4f02018-06-07 18:51:31 +0200304 /* Do not allow duplicate payload types */
305 for (i = 0; i < count; i++)
306 if (r->codecs[i] == pt)
307 goto response_parse_failure_pt;
308
309 /* Note: The payload type we store may not necessarly match
310 * the codec types we have defined in enum mgcp_codecs. To
311 * ensure that the end result only contains codec types which
312 * match enum mgcp_codecs, we will go through afterwards and
313 * remap the affected entries with the inrofmation we learn
314 * from rtpmap */
315 r->codecs[count] = pt;
316 count++;
317 }
318
319 r->codecs_len = count;
320
321exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200322 return 0;
323
Philipp Maier704c4f02018-06-07 18:51:31 +0200324response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200325 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200326 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200327 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200328
329response_parse_failure_pt:
330 LOGP(DLMGCP, LOGL_ERROR,
331 "Failed to parse SDP parameter payload types (%s)\n", line);
332 return -EINVAL;
333}
334
335/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
336static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
337{
338 unsigned int pt;
339 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200340 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200341
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200342#define A_PTIME "a=ptime:"
343#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200344
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200345 if (osmo_str_startswith(line, A_PTIME)) {
346 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
347 LOGP(DLMGCP, LOGL_ERROR,
348 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
349 return -EINVAL;
350 }
351 } else if (osmo_str_startswith(line, A_RTPMAP)) {
352 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
353 LOGP(DLMGCP, LOGL_ERROR,
354 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
355 return -EINVAL;
356 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200357 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
358 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
359 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200360 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200361 codec = map_str_to_codec(codec_resp);
362 r->ptmap[r->ptmap_len].pt = pt;
363 r->ptmap[r->ptmap_len].codec = codec;
364 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200365 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200366
Philipp Maier704c4f02018-06-07 18:51:31 +0200367 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200368}
369
370/* Parse a line like "c=IN IP4 10.11.12.13" */
371static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
372{
373 struct in_addr ip_test;
374
375 if (strlen(line) < 16)
376 goto response_parse_failure;
377
378 /* The current implementation strictly supports IPV4 only ! */
379 if (memcmp("c=IN IP4 ", line, 9) != 0)
380 goto response_parse_failure;
381
382 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100383 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200384
385 /* Check IP-Address */
386 if (inet_aton(r->audio_ip, &ip_test) == 0)
387 goto response_parse_failure;
388
389 return 0;
390
391response_parse_failure:
392 LOGP(DLMGCP, LOGL_ERROR,
393 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200394 return -EINVAL;
395}
396
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200397/*! Extract OSMUX CID from an MGCP parameter line (string).
398 * \param[in] line single parameter line from the MGCP message
399 * \returns OSMUX CID, -1 wildcard, -2 on error
400 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
401 */
402static int mgcp_parse_osmux_cid(const char *line)
403{
404 int osmux_cid;
405
406
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200407 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200408 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
409 return -1;
410 }
411
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200412 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200413 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
414 line);
415 return -2;
416 }
417
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200418 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
419 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
420 osmux_cid, OSMUX_CID_MAX);
421 return -2;
422 }
423 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
424
425 return osmux_cid;
426}
427
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100428/* A new section is marked by a double line break, check a few more
429 * patterns as there may be variants */
430static char *mgcp_find_section_end(char *string)
431{
432 char *rc;
433
434 rc = strstr(string, "\n\n");
435 if (rc)
436 return rc;
437
438 rc = strstr(string, "\n\r\n\r");
439 if (rc)
440 return rc;
441
442 rc = strstr(string, "\r\n\r\n");
443 if (rc)
444 return rc;
445
446 return NULL;
447}
448
Philipp Maier275ac972018-01-20 00:58:16 +0100449/*! Parse body (SDP) parameters of the MGCP response
450 * \param[in,out] r Response data
451 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200452int mgcp_response_parse_params(struct mgcp_response *r)
453{
454 char *line;
455 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100456 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100457 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200458 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200459
Philipp Maiere9d645b2018-01-19 23:54:08 +0100460 /* Since this functions performs a destructive parsing, we create a
461 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100462 OSMO_ASSERT(r->body);
463 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100464 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100465
Philipp Maiere9d645b2018-01-19 23:54:08 +0100466 /* Find beginning of the parameter (SDP) section */
467 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100468 if (!data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200469 LOGP(DLMGCP, LOGL_ERROR,
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100470 "MGCP response: cannot find start of SDP parameters\n");
Philipp Maiere9d645b2018-01-19 23:54:08 +0100471 rc = -EINVAL;
472 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200473 }
474
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100475 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
476 * skips any \r and \n characters for free, so we don't need to skip the marker. */
477
Philipp Maiere9d645b2018-01-19 23:54:08 +0100478 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200479 if (!mgcp_line_is_valid(line))
480 return -EINVAL;
481
482 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200483 case 'a':
484 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
485 if (rc)
486 goto exit;
487 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200488 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200489 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200490 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100491 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200492 break;
493 case 'c':
494 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200495 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100496 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200497 break;
498 default:
499 /* skip unhandled parameters */
500 break;
501 }
502 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100503
Philipp Maier704c4f02018-06-07 18:51:31 +0200504 /* See also note in mgcp_parse_audio_port_pt() */
505 for (i = 0; i < r->codecs_len; i++)
506 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
507
Philipp Maiere9d645b2018-01-19 23:54:08 +0100508 rc = 0;
509exit:
510 talloc_free(data);
511 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200512}
513
Philipp Maier55295f72018-01-15 14:00:28 +0100514/* Parse a line like "X: something" */
515static int mgcp_parse_head_param(char *result, unsigned int result_len,
516 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100517{
Philipp Maier55295f72018-01-15 14:00:28 +0100518 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200519 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100520
521 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100522 if (strlen(line) < 4)
523 goto response_parse_failure;
524
Philipp Maier55295f72018-01-15 14:00:28 +0100525 /* Check if the label matches */
526 snprintf(label_string, sizeof(label_string), "%c: ", label);
527 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100528 goto response_parse_failure;
529
Philipp Maier55295f72018-01-15 14:00:28 +0100530 /* Copy payload part of the string to destinations (the label string
531 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200532 rc = osmo_strlcpy(result, line + 3, result_len);
533 if (rc >= result_len) {
534 LOGP(DLMGCP, LOGL_ERROR,
535 "Failed to parse MGCP response (parameter label: %c):"
536 " the received conn ID is too long: %zu, maximum is %u characters\n",
537 label, rc, result_len - 1);
538 return -ENOSPC;
539 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100540 return 0;
541
542response_parse_failure:
543 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100544 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100545 return -EINVAL;
546}
547
548/* Parse MGCP parameters of the response */
549static int parse_head_params(struct mgcp_response *r)
550{
551 char *line;
552 int rc = 0;
553 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100554 char *data;
555 char *data_ptr;
556 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100557
Philipp Maier55295f72018-01-15 14:00:28 +0100558 /* Since this functions performs a destructive parsing, we create a
559 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100560 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100561 OSMO_ASSERT(data);
562 data_ptr = data;
563 osmo_strlcpy(data, r->body, strlen(r->body));
564
565 /* If there is an SDP body attached, prevent for_each_non_empty_line()
566 * into running in there, we are not yet interested in the parameters
567 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200568 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100569 if (data_end)
570 *data_end = '\0';
571
Philipp Maier55295f72018-01-15 14:00:28 +0100572 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200573 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100574 case 'Z':
575 rc = mgcp_parse_head_param(r->head.endpoint,
576 sizeof(r->head.endpoint),
577 'Z', line);
578 if (rc)
579 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100580
581 /* A specific endpoint identifier returned by the MGW
582 * must not contain any wildcard characters */
583 if (strstr(r->head.endpoint, "*") != NULL) {
584 rc = -EINVAL;
585 goto exit;
586 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100587
588 /* A specific endpoint identifier returned by the MGW
589 * must contain an @ character */
590 if (strstr(r->head.endpoint, "@") == NULL) {
591 rc = -EINVAL;
592 goto exit;
593 }
Philipp Maier55295f72018-01-15 14:00:28 +0100594 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100595 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100596 rc = mgcp_parse_head_param(r->head.conn_id,
597 sizeof(r->head.conn_id),
598 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100599 if (rc)
600 goto exit;
601 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200602 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200603 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200604 rc = mgcp_parse_osmux_cid(line);
605 if (rc < 0) {
606 /* -1: we don't want wildcards in response. -2: error */
607 rc = -EINVAL;
608 goto exit;
609 }
610 r->head.x_osmo_osmux_use = true;
611 r->head.x_osmo_osmux_cid = (uint8_t) rc;
612 rc = 0;
613 break;
614 }
615 /* Ignore unknown X-headers */
616 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100617 default:
618 /* skip unhandled parameters */
619 break;
620 }
621 }
622exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100623 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100624 return rc;
625}
626
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200627static struct mgcp_response_pending *mgcp_client_response_pending_get(
628 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100629 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200630{
631 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200632 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100633 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200634 llist_del(&pending->entry);
635 return pending;
636 }
637 }
638 return NULL;
639}
640
641/* Feed an MGCP message into the receive processing.
642 * Parse the head and call any callback registered for the transaction id found
643 * in the MGCP message. This is normally called directly from the internal
644 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
645 * function is published mainly to be able to feed data from the test suite.
646 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200647int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200648{
Philipp Maier1fc69992018-01-31 15:32:55 +0100649 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200650 struct mgcp_response_pending *pending;
651 int rc;
652
Philipp Maier1fc69992018-01-31 15:32:55 +0100653 r = talloc_zero(mgcp, struct mgcp_response);
654 OSMO_ASSERT(r);
655
656 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200657 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100658 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100659 rc = 1;
660 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100661 }
662
Philipp Maier1fc69992018-01-31 15:32:55 +0100663 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100664 if (rc) {
665 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100666 rc = 1;
667 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200668 }
669
Philipp Maier1fc69992018-01-31 15:32:55 +0100670 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200671 if (!pending) {
672 LOGP(DLMGCP, LOGL_ERROR,
673 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100674 r->head.trans_id);
675 rc = -ENOENT;
676 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200677 }
678
Philipp Maier1fc69992018-01-31 15:32:55 +0100679 mgcp_client_handle_response(mgcp, pending, r);
680 rc = 0;
681
682error:
683 talloc_free(r);
684 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200685}
686
687static int mgcp_do_read(struct osmo_fd *fd)
688{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200689 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200690 struct msgb *msg;
691 int ret;
692
693 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
694 if (!msg) {
695 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
696 return -1;
697 }
698
699 ret = read(fd->fd, msg->data, 4096 - 128);
700 if (ret <= 0) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100701 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %s: %d='%s'\n", osmo_sock_get_name2(fd->fd),
702 errno, strerror(errno));
703
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200704 msgb_free(msg);
705 return -1;
706 } else if (ret > 4096 - 128) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100707 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %s: %d\n", osmo_sock_get_name2(fd->fd), ret);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200708 msgb_free(msg);
709 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100710 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200711
712 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200713 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200714 talloc_free(msg);
715 return ret;
716}
717
718static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
719{
720 int ret;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200721
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100722 LOGP(DLMGCP, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
723 osmo_sock_get_name2(fd->fd), msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200724
725 ret = write(fd->fd, msg->data, msg->len);
726 if (ret != msg->len)
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100727 LOGP(DLMGCP, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
728 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100729 msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200730 return ret;
731}
732
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200733struct mgcp_client *mgcp_client_init(void *ctx,
734 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200735{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200736 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200737
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200738 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200739
740 INIT_LLIST_HEAD(&mgcp->responses_pending);
741 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
742
743 mgcp->next_trans_id = 1;
744
745 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200746 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200747 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200748 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200749
750 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200751 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200752 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200753 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200754
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100755 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
756 sizeof(mgcp->actual.endpoint_domain_name))
757 >= sizeof(mgcp->actual.endpoint_domain_name)) {
758 LOGP(DLMGCP, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
759 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
760 talloc_free(mgcp);
761 return NULL;
762 }
763 LOGP(DLMGCP, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
764
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200765 return mgcp;
766}
767
Philipp Maier6a26c162018-08-01 16:37:06 +0200768static int init_socket(struct mgcp_client *mgcp)
769{
770 int rc;
771 struct osmo_wqueue *wq;
772 int i;
773
774 wq = &mgcp->wq;
775
776 for (i = 0; i < 100; i++) {
777
778 /* Initalize socket with the currently configured port
779 * number */
780 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
781 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
782 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
783 if (rc > 0)
784 return rc;
785
786 /* If there is a different port than the default port
787 * configured then we assume that the user has choosen
788 * that port conciously and we will not try to resolve
789 * this by silently choosing a different port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200790 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200791 return -EINVAL;
792
793 /* Choose a new port number to try next */
794 LOGP(DLMGCP, LOGL_NOTICE,
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100795 "MGCPGW failed to bind to %s:%u, retrying with port %u\n",
796 mgcp->actual.local_addr, mgcp->actual.local_port, mgcp->actual.local_port + 1);
Philipp Maier6a26c162018-08-01 16:37:06 +0200797 mgcp->actual.local_port++;
798 }
799
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100800 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200801 return -EINVAL;
802}
803
Philipp Maier275ac972018-01-20 00:58:16 +0100804/*! Initalize client connection (opens socket only, no request is sent yet)
805 * \param[in,out] mgcp MGCP client descriptor.
806 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200807int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200808{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200809 struct sockaddr_in addr;
810 struct osmo_wqueue *wq;
811 int rc;
812
813 if (!mgcp) {
814 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
815 return -EINVAL;
816 }
817
818 wq = &mgcp->wq;
819
Philipp Maier6a26c162018-08-01 16:37:06 +0200820 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100821 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200822 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100823 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
824 mgcp->actual.local_addr, mgcp->actual.local_port,
825 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200826 goto error_close_fd;
827 }
828
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200829 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200830 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
831
832 osmo_wqueue_init(wq, 10);
833 wq->bfd.when = BSC_FD_READ;
834 wq->bfd.data = mgcp;
835 wq->read_cb = mgcp_do_read;
836 wq->write_cb = mgcp_do_write;
837
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100838 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200839
840 return 0;
841error_close_fd:
842 close(wq->bfd.fd);
843 wq->bfd.fd = -1;
844 return rc;
845}
846
Philipp Maier275ac972018-01-20 00:58:16 +0100847/*! Get the IP-Aaddress of the associated MGW as string.
848 * \param[in] mgcp MGCP client descriptor.
849 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200850const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200851{
852 return mgcp->actual.remote_addr;
853}
854
Philipp Maier275ac972018-01-20 00:58:16 +0100855/*! Get the IP-Port of the associated MGW.
856 * \param[in] mgcp MGCP client descriptor.
857 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200858uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200859{
860 return mgcp->actual.remote_port;
861}
862
Philipp Maier275ac972018-01-20 00:58:16 +0100863/*! Get the IP-Aaddress of the associated MGW as its numeric representation.
864 * \param[in] mgcp MGCP client descriptor.
865 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200866uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200867{
868 return mgcp->remote_addr;
869}
870
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100871/* To compose endpoint names, usually for CRCX, use this as domain name.
872 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
873const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
874{
875 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
876}
877
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200878static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, char *name)
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100879{
880 static char endpoint[MGCP_ENDPOINT_MAXLEN];
881 int rc;
882
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200883 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100884 if (rc > sizeof(endpoint) - 1) {
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200885 LOGP(DLMGCP, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
886 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100887 return NULL;
888 }
889 if (rc < 1) {
890 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
891 return NULL;
892 }
893 return endpoint;
894}
895
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200896const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
897{
898 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
899}
900
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200901struct mgcp_response_pending * mgcp_client_pending_add(
902 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200903 mgcp_trans_id_t trans_id,
904 mgcp_response_cb_t response_cb,
905 void *priv)
906{
907 struct mgcp_response_pending *pending;
908
909 pending = talloc_zero(mgcp, struct mgcp_response_pending);
910 pending->trans_id = trans_id;
911 pending->response_cb = response_cb;
912 pending->priv = priv;
913 llist_add_tail(&pending->entry, &mgcp->responses_pending);
914
915 return pending;
916}
917
918/* Send the MGCP message in msg to the MGCP GW and handle a response with
919 * response_cb. NOTE: the response_cb still needs to call
920 * mgcp_response_parse_params(response) to get the parsed parameters -- to
921 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100922 * the response_cb is invoked.
923 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
924 * mgcp_client_cancel() needs to be called for this transaction.
925 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200926int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
927 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200928{
929 struct mgcp_response_pending *pending;
930 mgcp_trans_id_t trans_id;
931 int rc;
932
933 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
934 if (!trans_id) {
935 LOGP(DLMGCP, LOGL_ERROR,
936 "Unset transaction id in mgcp send request\n");
937 talloc_free(msg);
938 return -EINVAL;
939 }
940
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200941 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200942
943 if (msgb_l2len(msg) > 4096) {
944 LOGP(DLMGCP, LOGL_ERROR,
945 "Cannot send, MGCP message too large: %u\n",
946 msgb_l2len(msg));
947 msgb_free(msg);
948 rc = -EINVAL;
949 goto mgcp_tx_error;
950 }
951
952 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
953 if (rc) {
954 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
955 msgb_free(msg);
956 goto mgcp_tx_error;
957 } else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100958 LOGP(DLMGCP, LOGL_DEBUG, "Queued %u bytes for MGCP GW\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200959 msgb_l2len(msg));
960 return 0;
961
962mgcp_tx_error:
963 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200964 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200965 return -1;
966}
967
Philipp Maier275ac972018-01-20 00:58:16 +0100968/*! Cancel a pending transaction.
969 * \param[in] mgcp MGCP client descriptor.
970 * \param[in,out] trans_id Transaction id.
971 * \returns 0 on success, -ENOENT on error.
972 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100973 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
974 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
975 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +0100976 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100977int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
978{
979 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
980 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +0100981 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100982 LOGP(DLMGCP, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100983 return -ENOENT;
984 }
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100985 LOGP(DLMGCP, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100986 talloc_free(pending);
987 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +0100988 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
989 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
990 * per se is broken and the program should notice so by a full wqueue. Even if this was called
991 * before we had a chance to send out the message and it is still going to be sent, we will just
992 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
993 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100994 */
995}
996
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200997static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200998{
999 /* avoid zero trans_id to distinguish from unset trans_id */
1000 if (!mgcp->next_trans_id)
1001 mgcp->next_trans_id ++;
1002 return mgcp->next_trans_id ++;
1003}
1004
Philipp Maier1dc6be62017-10-05 18:25:37 +02001005#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1006 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001007 MGCP_MSG_PRESENCE_CONN_MODE)
1008#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001009 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001010 MGCP_MSG_PRESENCE_CONN_ID)
1011#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1012#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1013#define MGCP_RSIP_MANDATORY 0 /* none */
1014
Philipp Maier704c4f02018-06-07 18:51:31 +02001015/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1016static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1017{
1018 unsigned int i;
1019 int rc = 0;
1020 const char *codec;
1021 unsigned int pt;
1022
1023 rc += msgb_printf(msg, "L:");
1024
1025 if (mgcp_msg->ptime)
1026 rc += msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
1027
1028 if (mgcp_msg->codecs_len) {
1029 rc += msgb_printf(msg, " a:");
1030 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1031 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001032 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001033
Philipp Maier704c4f02018-06-07 18:51:31 +02001034 /* Note: Use codec descriptors from enum mgcp_codecs
1035 * in mgcp_client only! */
1036 OSMO_ASSERT(codec);
1037 rc += msgb_printf(msg, "%s", extract_codec_name(codec));
1038 if (i < mgcp_msg->codecs_len - 1)
1039 rc += msgb_printf(msg, ";");
1040 }
1041 rc += msgb_printf(msg, ",");
1042 }
1043
1044 rc += msgb_printf(msg, " nt:IN\r\n");
1045
1046 return rc;
1047}
1048
1049/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1050static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1051{
1052 unsigned int i;
1053 int rc = 0;
1054 char local_ip[INET_ADDRSTRLEN];
1055 const char *codec;
1056 unsigned int pt;
1057
1058 /* Add separator to mark the beginning of the SDP block */
1059 rc += msgb_printf(msg, "\r\n");
1060
1061 /* Add SDP protocol version */
1062 rc += msgb_printf(msg, "v=0\r\n");
1063
1064 /* Determine local IP-Address */
1065 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1066 LOGP(DLMGCP, LOGL_ERROR,
1067 "Could not determine local IP-Address!\n");
1068 msgb_free(msg);
1069 return -2;
1070 }
1071
1072 /* Add owner/creator (SDP) */
1073 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
1074 mgcp_msg->call_id, local_ip);
1075
1076 /* Add session name (none) */
1077 rc += msgb_printf(msg, "s=-\r\n");
1078
1079 /* Add RTP address and port */
1080 if (mgcp_msg->audio_port == 0) {
1081 LOGP(DLMGCP, LOGL_ERROR,
1082 "Invalid port number, can not generate MGCP message\n");
1083 msgb_free(msg);
1084 return -2;
1085 }
1086 if (strlen(mgcp_msg->audio_ip) <= 0) {
1087 LOGP(DLMGCP, LOGL_ERROR,
1088 "Empty ip address, can not generate MGCP message\n");
1089 msgb_free(msg);
1090 return -2;
1091 }
1092 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
1093
1094 /* Add time description, active time (SDP) */
1095 rc += msgb_printf(msg, "t=0 0\r\n");
1096
1097 rc += msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
1098 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1099 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1100 rc += msgb_printf(msg, " %u", pt);
1101
1102 }
1103 rc += msgb_printf(msg, "\r\n");
1104
Philipp Maier228e5912019-03-05 13:56:59 +01001105 /* Add optional codec parameters (fmtp) */
1106 if (mgcp_msg->param_present) {
1107 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1108 /* The following is only applicable for AMR */
1109 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1110 continue;
1111 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1112 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
1113 rc += msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
1114 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
1115 rc += msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
1116 }
1117 }
1118
Philipp Maier704c4f02018-06-07 18:51:31 +02001119 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1120 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001121
Philipp Maier704c4f02018-06-07 18:51:31 +02001122 /* Note: Only dynamic payload type from the range 96-127
1123 * require to be explained further via rtpmap. All others
1124 * are implcitly definedby the number in m=audio */
1125 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001126 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001127
1128 /* Note: Use codec descriptors from enum mgcp_codecs
1129 * in mgcp_client only! */
1130 OSMO_ASSERT(codec);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001131
Philipp Maier704c4f02018-06-07 18:51:31 +02001132 rc += msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
1133 }
1134 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001135
Philipp Maier704c4f02018-06-07 18:51:31 +02001136 if (mgcp_msg->ptime)
1137 rc += msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
1138
1139 return rc;
1140}
1141
Philipp Maier275ac972018-01-20 00:58:16 +01001142/*! Generate an MGCP message
1143 * \param[in] mgcp MGCP client descriptor.
1144 * \param[in] mgcp_msg Message description
1145 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001146struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1147{
1148 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1149 uint32_t mandatory_mask;
1150 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1151 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001152 int rc_sdp;
1153 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001154 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001155
1156 msg->l2h = msg->data;
1157 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1158
1159 /* Add command verb */
1160 switch (mgcp_msg->verb) {
1161 case MGCP_VERB_CRCX:
1162 mandatory_mask = MGCP_CRCX_MANDATORY;
1163 rc += msgb_printf(msg, "CRCX %u", trans_id);
1164 break;
1165 case MGCP_VERB_MDCX:
1166 mandatory_mask = MGCP_MDCX_MANDATORY;
1167 rc += msgb_printf(msg, "MDCX %u", trans_id);
1168 break;
1169 case MGCP_VERB_DLCX:
1170 mandatory_mask = MGCP_DLCX_MANDATORY;
1171 rc += msgb_printf(msg, "DLCX %u", trans_id);
1172 break;
1173 case MGCP_VERB_AUEP:
1174 mandatory_mask = MGCP_AUEP_MANDATORY;
1175 rc += msgb_printf(msg, "AUEP %u", trans_id);
1176 break;
1177 case MGCP_VERB_RSIP:
1178 mandatory_mask = MGCP_RSIP_MANDATORY;
1179 rc += msgb_printf(msg, "RSIP %u", trans_id);
1180 break;
1181 default:
1182 LOGP(DLMGCP, LOGL_ERROR,
1183 "Invalid command verb, can not generate MGCP message\n");
1184 msgb_free(msg);
1185 return NULL;
1186 }
1187
1188 /* Check if mandatory fields are missing */
1189 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1190 LOGP(DLMGCP, LOGL_ERROR,
1191 "One or more missing mandatory fields, can not generate MGCP message\n");
1192 msgb_free(msg);
1193 return NULL;
1194 }
1195
1196 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001197 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1198 if (strlen(mgcp_msg->endpoint) <= 0) {
1199 LOGP(DLMGCP, LOGL_ERROR,
1200 "Empty endpoint name, can not generate MGCP message\n");
1201 msgb_free(msg);
1202 return NULL;
1203 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001204
1205 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1206 LOGP(DLMGCP, LOGL_ERROR,
1207 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1208 mgcp_msg->endpoint);
1209 msgb_free(msg);
1210 }
1211
Philipp Maier1dc6be62017-10-05 18:25:37 +02001212 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001213 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001214
1215 /* Add protocol version */
1216 rc += msgb_printf(msg, " MGCP 1.0\r\n");
1217
1218 /* Add call id */
1219 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
1220 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
1221
1222 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001223 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1224 if (strlen(mgcp_msg->conn_id) <= 0) {
1225 LOGP(DLMGCP, LOGL_ERROR,
1226 "Empty connection id, can not generate MGCP message\n");
1227 msgb_free(msg);
1228 return NULL;
1229 }
Philipp Maier01d24a32017-11-21 17:26:09 +01001230 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001231 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001232
Philipp Maier704c4f02018-06-07 18:51:31 +02001233 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1234 * if we do not know this information yet, we fall back to LCO */
1235 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1236 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1237 use_sdp = true;
1238
1239 /* Add local connection options (LCO) */
1240 if (!use_sdp
1241 && (mgcp_msg->verb == MGCP_VERB_CRCX
1242 || mgcp_msg->verb == MGCP_VERB_MDCX))
1243 rc += add_lco(msg, mgcp_msg);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001244
1245 /* Add mode */
1246 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
1247 rc +=
1248 msgb_printf(msg, "M: %s\r\n",
1249 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1250
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001251 /* Add X-Osmo-IGN */
1252 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1253 && (mgcp_msg->x_osmo_ign != 0))
1254 rc +=
1255 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1256 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1257
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001258 /* Add X-Osmo-Osmux */
1259 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001260 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
1261 LOGP(DLMGCP, LOGL_ERROR,
1262 "Wrong Osmux CID %d, can not generate MGCP message\n",
1263 mgcp_msg->x_osmo_osmux_cid);
1264 msgb_free(msg);
1265 return NULL;
1266 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001267 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
1268 rc +=
1269 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1270 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1271 }
1272
1273
Philipp Maier704c4f02018-06-07 18:51:31 +02001274 /* Add session description protocol (SDP) */
1275 if (use_sdp
1276 && (mgcp_msg->verb == MGCP_VERB_CRCX
1277 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1278 rc_sdp = add_sdp(msg, mgcp_msg, mgcp);
1279 if (rc_sdp == -2)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001280 return NULL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001281 else
1282 rc += rc_sdp;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001283 }
1284
1285 if (rc != 0) {
1286 LOGP(DLMGCP, LOGL_ERROR,
1287 "message buffer to small, can not generate MGCP message\n");
1288 msgb_free(msg);
1289 msg = NULL;
1290 }
1291
1292 return msg;
1293}
1294
Philipp Maier275ac972018-01-20 00:58:16 +01001295/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1296 * \param[in] msg message buffer
1297 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001298mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1299{
1300 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1301}
1302
Philipp Maier275ac972018-01-20 00:58:16 +01001303/*! Get the configuration parameters a given MGCP client instance
1304 * \param[in] mgcp MGCP client descriptor.
1305 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001306struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001307{
1308 return &mgcp->actual;
1309}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001310
1311const struct value_string mgcp_client_connection_mode_strs[] = {
1312 { MGCP_CONN_NONE, "none" },
1313 { MGCP_CONN_RECV_SEND, "sendrecv" },
1314 { MGCP_CONN_SEND_ONLY, "sendonly" },
1315 { MGCP_CONN_RECV_ONLY, "recvonly" },
1316 { MGCP_CONN_LOOPBACK, "loopback" },
1317 { 0, NULL }
1318};