blob: 910289ed27c949f70fa1bc24212c84cceb16c409 [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;
Philipp Maier704c4f02018-06-07 18:51:31 +0200271 unsigned int pt;
272 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
301 /* Do not allow duplicate payload types */
302 for (i = 0; i < count; i++)
303 if (r->codecs[i] == pt)
304 goto response_parse_failure_pt;
305
306 /* Note: The payload type we store may not necessarly match
307 * the codec types we have defined in enum mgcp_codecs. To
308 * ensure that the end result only contains codec types which
309 * match enum mgcp_codecs, we will go through afterwards and
310 * remap the affected entries with the inrofmation we learn
311 * from rtpmap */
312 r->codecs[count] = pt;
313 count++;
314 }
315
316 r->codecs_len = count;
317
318exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200319 return 0;
320
Philipp Maier704c4f02018-06-07 18:51:31 +0200321response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200322 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200323 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200324 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200325
326response_parse_failure_pt:
327 LOGP(DLMGCP, LOGL_ERROR,
328 "Failed to parse SDP parameter payload types (%s)\n", line);
329 return -EINVAL;
330}
331
332/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
333static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
334{
335 unsigned int pt;
336 char codec_resp[64];
337 unsigned int codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200338
339
Philipp Maier704c4f02018-06-07 18:51:31 +0200340 if (strstr(line, "ptime")) {
341 if (sscanf(line, "a=ptime:%u", &r->ptime) != 1)
342 goto response_parse_failure_ptime;
343 } else if (strstr(line, "rtpmap")) {
344 if (sscanf(line, "a=rtpmap:%d %63s", &pt, codec_resp) == 2) {
345 /* The MGW may assign an own payload type in the
346 * response if the choosen codec falls into the IANA
347 * assigned dynamic payload type range (96-127).
348 * Normally the MGW should obey the 3gpp payload type
349 * assignments, which are fixed, so we likely wont see
350 * anything unexpected here. In order to be sure that
351 * we will now check the codec string and if the result
352 * does not match to what is IANA / 3gpp assigned, we
353 * will create an entry in the ptmap table so we can
354 * lookup later what has been assigned. */
355 codec = map_str_to_codec(codec_resp);
356 if (codec != pt) {
357 if (r->ptmap_len < ARRAY_SIZE(r->ptmap)) {
358 r->ptmap[r->ptmap_len].pt = pt;
359 r->ptmap[r->ptmap_len].codec = codec;
360 r->ptmap_len++;
361 } else
362 goto response_parse_failure_rtpmap;
363 }
364
365 } else
366 goto response_parse_failure_rtpmap;
367 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200368
Philipp Maier704c4f02018-06-07 18:51:31 +0200369 return 0;
370
371response_parse_failure_ptime:
372 LOGP(DLMGCP, LOGL_ERROR,
373 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200374 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200375response_parse_failure_rtpmap:
376 LOGP(DLMGCP, LOGL_ERROR,
377 "Failed to parse SDP parameter, invalid rtpmap (%s)\n", line);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200378 return -EINVAL;
Philipp Maier06da85e2017-10-05 18:49:24 +0200379}
380
381/* Parse a line like "c=IN IP4 10.11.12.13" */
382static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
383{
384 struct in_addr ip_test;
385
386 if (strlen(line) < 16)
387 goto response_parse_failure;
388
389 /* The current implementation strictly supports IPV4 only ! */
390 if (memcmp("c=IN IP4 ", line, 9) != 0)
391 goto response_parse_failure;
392
393 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100394 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200395
396 /* Check IP-Address */
397 if (inet_aton(r->audio_ip, &ip_test) == 0)
398 goto response_parse_failure;
399
400 return 0;
401
402response_parse_failure:
403 LOGP(DLMGCP, LOGL_ERROR,
404 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200405 return -EINVAL;
406}
407
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200408/*! Extract OSMUX CID from an MGCP parameter line (string).
409 * \param[in] line single parameter line from the MGCP message
410 * \returns OSMUX CID, -1 wildcard, -2 on error
411 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
412 */
413static int mgcp_parse_osmux_cid(const char *line)
414{
415 int osmux_cid;
416
417
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200418 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200419 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
420 return -1;
421 }
422
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200423 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200424 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
425 line);
426 return -2;
427 }
428
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200429 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
430 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
431 osmux_cid, OSMUX_CID_MAX);
432 return -2;
433 }
434 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
435
436 return osmux_cid;
437}
438
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100439/* A new section is marked by a double line break, check a few more
440 * patterns as there may be variants */
441static char *mgcp_find_section_end(char *string)
442{
443 char *rc;
444
445 rc = strstr(string, "\n\n");
446 if (rc)
447 return rc;
448
449 rc = strstr(string, "\n\r\n\r");
450 if (rc)
451 return rc;
452
453 rc = strstr(string, "\r\n\r\n");
454 if (rc)
455 return rc;
456
457 return NULL;
458}
459
Philipp Maier275ac972018-01-20 00:58:16 +0100460/*! Parse body (SDP) parameters of the MGCP response
461 * \param[in,out] r Response data
462 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200463int mgcp_response_parse_params(struct mgcp_response *r)
464{
465 char *line;
466 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100467 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100468 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200469 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200470
Philipp Maiere9d645b2018-01-19 23:54:08 +0100471 /* Since this functions performs a destructive parsing, we create a
472 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100473 OSMO_ASSERT(r->body);
474 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100475 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100476
Philipp Maiere9d645b2018-01-19 23:54:08 +0100477 /* Find beginning of the parameter (SDP) section */
478 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100479 if (!data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200480 LOGP(DLMGCP, LOGL_ERROR,
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100481 "MGCP response: cannot find start of SDP parameters\n");
Philipp Maiere9d645b2018-01-19 23:54:08 +0100482 rc = -EINVAL;
483 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200484 }
485
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100486 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
487 * skips any \r and \n characters for free, so we don't need to skip the marker. */
488
Philipp Maiere9d645b2018-01-19 23:54:08 +0100489 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200490 if (!mgcp_line_is_valid(line))
491 return -EINVAL;
492
493 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200494 case 'a':
495 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
496 if (rc)
497 goto exit;
498 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200499 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200500 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200501 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100502 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200503 break;
504 case 'c':
505 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200506 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100507 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200508 break;
509 default:
510 /* skip unhandled parameters */
511 break;
512 }
513 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100514
Philipp Maier704c4f02018-06-07 18:51:31 +0200515 /* See also note in mgcp_parse_audio_port_pt() */
516 for (i = 0; i < r->codecs_len; i++)
517 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
518
Philipp Maiere9d645b2018-01-19 23:54:08 +0100519 rc = 0;
520exit:
521 talloc_free(data);
522 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200523}
524
Philipp Maier55295f72018-01-15 14:00:28 +0100525/* Parse a line like "X: something" */
526static int mgcp_parse_head_param(char *result, unsigned int result_len,
527 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100528{
Philipp Maier55295f72018-01-15 14:00:28 +0100529 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200530 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100531
532 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100533 if (strlen(line) < 4)
534 goto response_parse_failure;
535
Philipp Maier55295f72018-01-15 14:00:28 +0100536 /* Check if the label matches */
537 snprintf(label_string, sizeof(label_string), "%c: ", label);
538 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100539 goto response_parse_failure;
540
Philipp Maier55295f72018-01-15 14:00:28 +0100541 /* Copy payload part of the string to destinations (the label string
542 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200543 rc = osmo_strlcpy(result, line + 3, result_len);
544 if (rc >= result_len) {
545 LOGP(DLMGCP, LOGL_ERROR,
546 "Failed to parse MGCP response (parameter label: %c):"
547 " the received conn ID is too long: %zu, maximum is %u characters\n",
548 label, rc, result_len - 1);
549 return -ENOSPC;
550 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100551 return 0;
552
553response_parse_failure:
554 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100555 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100556 return -EINVAL;
557}
558
559/* Parse MGCP parameters of the response */
560static int parse_head_params(struct mgcp_response *r)
561{
562 char *line;
563 int rc = 0;
564 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100565 char *data;
566 char *data_ptr;
567 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100568
Philipp Maier55295f72018-01-15 14:00:28 +0100569 /* Since this functions performs a destructive parsing, we create a
570 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100571 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100572 OSMO_ASSERT(data);
573 data_ptr = data;
574 osmo_strlcpy(data, r->body, strlen(r->body));
575
576 /* If there is an SDP body attached, prevent for_each_non_empty_line()
577 * into running in there, we are not yet interested in the parameters
578 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200579 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100580 if (data_end)
581 *data_end = '\0';
582
Philipp Maier55295f72018-01-15 14:00:28 +0100583 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200584 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100585 case 'Z':
586 rc = mgcp_parse_head_param(r->head.endpoint,
587 sizeof(r->head.endpoint),
588 'Z', line);
589 if (rc)
590 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100591
592 /* A specific endpoint identifier returned by the MGW
593 * must not contain any wildcard characters */
594 if (strstr(r->head.endpoint, "*") != NULL) {
595 rc = -EINVAL;
596 goto exit;
597 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100598
599 /* A specific endpoint identifier returned by the MGW
600 * must contain an @ character */
601 if (strstr(r->head.endpoint, "@") == NULL) {
602 rc = -EINVAL;
603 goto exit;
604 }
Philipp Maier55295f72018-01-15 14:00:28 +0100605 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100606 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100607 rc = mgcp_parse_head_param(r->head.conn_id,
608 sizeof(r->head.conn_id),
609 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100610 if (rc)
611 goto exit;
612 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200613 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200614 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200615 rc = mgcp_parse_osmux_cid(line);
616 if (rc < 0) {
617 /* -1: we don't want wildcards in response. -2: error */
618 rc = -EINVAL;
619 goto exit;
620 }
621 r->head.x_osmo_osmux_use = true;
622 r->head.x_osmo_osmux_cid = (uint8_t) rc;
623 rc = 0;
624 break;
625 }
626 /* Ignore unknown X-headers */
627 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100628 default:
629 /* skip unhandled parameters */
630 break;
631 }
632 }
633exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100634 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100635 return rc;
636}
637
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200638static struct mgcp_response_pending *mgcp_client_response_pending_get(
639 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100640 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200641{
642 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200643 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100644 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200645 llist_del(&pending->entry);
646 return pending;
647 }
648 }
649 return NULL;
650}
651
652/* Feed an MGCP message into the receive processing.
653 * Parse the head and call any callback registered for the transaction id found
654 * in the MGCP message. This is normally called directly from the internal
655 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
656 * function is published mainly to be able to feed data from the test suite.
657 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200658int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200659{
Philipp Maier1fc69992018-01-31 15:32:55 +0100660 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200661 struct mgcp_response_pending *pending;
662 int rc;
663
Philipp Maier1fc69992018-01-31 15:32:55 +0100664 r = talloc_zero(mgcp, struct mgcp_response);
665 OSMO_ASSERT(r);
666
667 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200668 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100669 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100670 rc = 1;
671 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100672 }
673
Philipp Maier1fc69992018-01-31 15:32:55 +0100674 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100675 if (rc) {
676 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100677 rc = 1;
678 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200679 }
680
Philipp Maier1fc69992018-01-31 15:32:55 +0100681 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200682 if (!pending) {
683 LOGP(DLMGCP, LOGL_ERROR,
684 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100685 r->head.trans_id);
686 rc = -ENOENT;
687 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200688 }
689
Philipp Maier1fc69992018-01-31 15:32:55 +0100690 mgcp_client_handle_response(mgcp, pending, r);
691 rc = 0;
692
693error:
694 talloc_free(r);
695 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200696}
697
698static int mgcp_do_read(struct osmo_fd *fd)
699{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200700 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200701 struct msgb *msg;
702 int ret;
703
704 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
705 if (!msg) {
706 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
707 return -1;
708 }
709
710 ret = read(fd->fd, msg->data, 4096 - 128);
711 if (ret <= 0) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100712 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %s: %d='%s'\n", osmo_sock_get_name2(fd->fd),
713 errno, strerror(errno));
714
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200715 msgb_free(msg);
716 return -1;
717 } else if (ret > 4096 - 128) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100718 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %s: %d\n", osmo_sock_get_name2(fd->fd), ret);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200719 msgb_free(msg);
720 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100721 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200722
723 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200724 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200725 talloc_free(msg);
726 return ret;
727}
728
729static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
730{
731 int ret;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200732
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100733 LOGP(DLMGCP, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
734 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 +0200735
736 ret = write(fd->fd, msg->data, msg->len);
737 if (ret != msg->len)
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100738 LOGP(DLMGCP, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
739 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100740 msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200741 return ret;
742}
743
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200744struct mgcp_client *mgcp_client_init(void *ctx,
745 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200746{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200747 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200748
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200749 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200750
751 INIT_LLIST_HEAD(&mgcp->responses_pending);
752 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
753
754 mgcp->next_trans_id = 1;
755
756 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200757 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200758 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200759 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200760
761 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200762 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200763 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200764 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200765
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100766 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
767 sizeof(mgcp->actual.endpoint_domain_name))
768 >= sizeof(mgcp->actual.endpoint_domain_name)) {
769 LOGP(DLMGCP, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
770 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
771 talloc_free(mgcp);
772 return NULL;
773 }
774 LOGP(DLMGCP, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
775
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200776 return mgcp;
777}
778
Philipp Maier6a26c162018-08-01 16:37:06 +0200779static int init_socket(struct mgcp_client *mgcp)
780{
781 int rc;
782 struct osmo_wqueue *wq;
783 int i;
784
785 wq = &mgcp->wq;
786
787 for (i = 0; i < 100; i++) {
788
789 /* Initalize socket with the currently configured port
790 * number */
791 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
792 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
793 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
794 if (rc > 0)
795 return rc;
796
797 /* If there is a different port than the default port
798 * configured then we assume that the user has choosen
799 * that port conciously and we will not try to resolve
800 * this by silently choosing a different port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200801 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200802 return -EINVAL;
803
804 /* Choose a new port number to try next */
805 LOGP(DLMGCP, LOGL_NOTICE,
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100806 "MGCPGW failed to bind to %s:%u, retrying with port %u\n",
807 mgcp->actual.local_addr, mgcp->actual.local_port, mgcp->actual.local_port + 1);
Philipp Maier6a26c162018-08-01 16:37:06 +0200808 mgcp->actual.local_port++;
809 }
810
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100811 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200812 return -EINVAL;
813}
814
Philipp Maier275ac972018-01-20 00:58:16 +0100815/*! Initalize client connection (opens socket only, no request is sent yet)
816 * \param[in,out] mgcp MGCP client descriptor.
817 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200818int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200819{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200820 struct sockaddr_in addr;
821 struct osmo_wqueue *wq;
822 int rc;
823
824 if (!mgcp) {
825 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
826 return -EINVAL;
827 }
828
829 wq = &mgcp->wq;
830
Philipp Maier6a26c162018-08-01 16:37:06 +0200831 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100832 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200833 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100834 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
835 mgcp->actual.local_addr, mgcp->actual.local_port,
836 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200837 goto error_close_fd;
838 }
839
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200840 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200841 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
842
843 osmo_wqueue_init(wq, 10);
844 wq->bfd.when = BSC_FD_READ;
845 wq->bfd.data = mgcp;
846 wq->read_cb = mgcp_do_read;
847 wq->write_cb = mgcp_do_write;
848
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100849 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200850
851 return 0;
852error_close_fd:
853 close(wq->bfd.fd);
854 wq->bfd.fd = -1;
855 return rc;
856}
857
Philipp Maier275ac972018-01-20 00:58:16 +0100858/*! Get the IP-Aaddress of the associated MGW as string.
859 * \param[in] mgcp MGCP client descriptor.
860 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200861const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200862{
863 return mgcp->actual.remote_addr;
864}
865
Philipp Maier275ac972018-01-20 00:58:16 +0100866/*! Get the IP-Port of the associated MGW.
867 * \param[in] mgcp MGCP client descriptor.
868 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200869uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200870{
871 return mgcp->actual.remote_port;
872}
873
Philipp Maier275ac972018-01-20 00:58:16 +0100874/*! Get the IP-Aaddress of the associated MGW as its numeric representation.
875 * \param[in] mgcp MGCP client descriptor.
876 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200877uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200878{
879 return mgcp->remote_addr;
880}
881
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100882/* To compose endpoint names, usually for CRCX, use this as domain name.
883 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
884const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
885{
886 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
887}
888
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200889static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, char *name)
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100890{
891 static char endpoint[MGCP_ENDPOINT_MAXLEN];
892 int rc;
893
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200894 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100895 if (rc > sizeof(endpoint) - 1) {
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200896 LOGP(DLMGCP, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
897 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100898 return NULL;
899 }
900 if (rc < 1) {
901 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
902 return NULL;
903 }
904 return endpoint;
905}
906
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200907const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
908{
909 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
910}
911
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200912struct mgcp_response_pending * mgcp_client_pending_add(
913 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200914 mgcp_trans_id_t trans_id,
915 mgcp_response_cb_t response_cb,
916 void *priv)
917{
918 struct mgcp_response_pending *pending;
919
920 pending = talloc_zero(mgcp, struct mgcp_response_pending);
921 pending->trans_id = trans_id;
922 pending->response_cb = response_cb;
923 pending->priv = priv;
924 llist_add_tail(&pending->entry, &mgcp->responses_pending);
925
926 return pending;
927}
928
929/* Send the MGCP message in msg to the MGCP GW and handle a response with
930 * response_cb. NOTE: the response_cb still needs to call
931 * mgcp_response_parse_params(response) to get the parsed parameters -- to
932 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100933 * the response_cb is invoked.
934 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
935 * mgcp_client_cancel() needs to be called for this transaction.
936 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200937int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
938 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200939{
940 struct mgcp_response_pending *pending;
941 mgcp_trans_id_t trans_id;
942 int rc;
943
944 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
945 if (!trans_id) {
946 LOGP(DLMGCP, LOGL_ERROR,
947 "Unset transaction id in mgcp send request\n");
948 talloc_free(msg);
949 return -EINVAL;
950 }
951
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200952 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200953
954 if (msgb_l2len(msg) > 4096) {
955 LOGP(DLMGCP, LOGL_ERROR,
956 "Cannot send, MGCP message too large: %u\n",
957 msgb_l2len(msg));
958 msgb_free(msg);
959 rc = -EINVAL;
960 goto mgcp_tx_error;
961 }
962
963 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
964 if (rc) {
965 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
966 msgb_free(msg);
967 goto mgcp_tx_error;
968 } else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100969 LOGP(DLMGCP, LOGL_DEBUG, "Queued %u bytes for MGCP GW\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200970 msgb_l2len(msg));
971 return 0;
972
973mgcp_tx_error:
974 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200975 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200976 return -1;
977}
978
Philipp Maier275ac972018-01-20 00:58:16 +0100979/*! Cancel a pending transaction.
980 * \param[in] mgcp MGCP client descriptor.
981 * \param[in,out] trans_id Transaction id.
982 * \returns 0 on success, -ENOENT on error.
983 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100984 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
985 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
986 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +0100987 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100988int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
989{
990 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
991 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +0100992 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100993 LOGP(DLMGCP, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100994 return -ENOENT;
995 }
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100996 LOGP(DLMGCP, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100997 talloc_free(pending);
998 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +0100999 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1000 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1001 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1002 * before we had a chance to send out the message and it is still going to be sent, we will just
1003 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1004 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001005 */
1006}
1007
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001008static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001009{
1010 /* avoid zero trans_id to distinguish from unset trans_id */
1011 if (!mgcp->next_trans_id)
1012 mgcp->next_trans_id ++;
1013 return mgcp->next_trans_id ++;
1014}
1015
Philipp Maier1dc6be62017-10-05 18:25:37 +02001016#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1017 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001018 MGCP_MSG_PRESENCE_CONN_MODE)
1019#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001020 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001021 MGCP_MSG_PRESENCE_CONN_ID)
1022#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1023#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1024#define MGCP_RSIP_MANDATORY 0 /* none */
1025
Philipp Maier704c4f02018-06-07 18:51:31 +02001026/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1027static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1028{
1029 unsigned int i;
1030 int rc = 0;
1031 const char *codec;
1032 unsigned int pt;
1033
1034 rc += msgb_printf(msg, "L:");
1035
1036 if (mgcp_msg->ptime)
1037 rc += msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
1038
1039 if (mgcp_msg->codecs_len) {
1040 rc += msgb_printf(msg, " a:");
1041 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1042 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001043 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001044
Philipp Maier704c4f02018-06-07 18:51:31 +02001045 /* Note: Use codec descriptors from enum mgcp_codecs
1046 * in mgcp_client only! */
1047 OSMO_ASSERT(codec);
1048 rc += msgb_printf(msg, "%s", extract_codec_name(codec));
1049 if (i < mgcp_msg->codecs_len - 1)
1050 rc += msgb_printf(msg, ";");
1051 }
1052 rc += msgb_printf(msg, ",");
1053 }
1054
1055 rc += msgb_printf(msg, " nt:IN\r\n");
1056
1057 return rc;
1058}
1059
1060/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1061static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1062{
1063 unsigned int i;
1064 int rc = 0;
1065 char local_ip[INET_ADDRSTRLEN];
1066 const char *codec;
1067 unsigned int pt;
1068
1069 /* Add separator to mark the beginning of the SDP block */
1070 rc += msgb_printf(msg, "\r\n");
1071
1072 /* Add SDP protocol version */
1073 rc += msgb_printf(msg, "v=0\r\n");
1074
1075 /* Determine local IP-Address */
1076 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1077 LOGP(DLMGCP, LOGL_ERROR,
1078 "Could not determine local IP-Address!\n");
1079 msgb_free(msg);
1080 return -2;
1081 }
1082
1083 /* Add owner/creator (SDP) */
1084 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
1085 mgcp_msg->call_id, local_ip);
1086
1087 /* Add session name (none) */
1088 rc += msgb_printf(msg, "s=-\r\n");
1089
1090 /* Add RTP address and port */
1091 if (mgcp_msg->audio_port == 0) {
1092 LOGP(DLMGCP, LOGL_ERROR,
1093 "Invalid port number, can not generate MGCP message\n");
1094 msgb_free(msg);
1095 return -2;
1096 }
1097 if (strlen(mgcp_msg->audio_ip) <= 0) {
1098 LOGP(DLMGCP, LOGL_ERROR,
1099 "Empty ip address, can not generate MGCP message\n");
1100 msgb_free(msg);
1101 return -2;
1102 }
1103 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
1104
1105 /* Add time description, active time (SDP) */
1106 rc += msgb_printf(msg, "t=0 0\r\n");
1107
1108 rc += msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
1109 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1110 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1111 rc += msgb_printf(msg, " %u", pt);
1112
1113 }
1114 rc += msgb_printf(msg, "\r\n");
1115
Philipp Maier228e5912019-03-05 13:56:59 +01001116 /* Add optional codec parameters (fmtp) */
1117 if (mgcp_msg->param_present) {
1118 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1119 /* The following is only applicable for AMR */
1120 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1121 continue;
1122 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1123 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
1124 rc += msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
1125 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
1126 rc += msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
1127 }
1128 }
1129
Philipp Maier704c4f02018-06-07 18:51:31 +02001130 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1131 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001132
Philipp Maier704c4f02018-06-07 18:51:31 +02001133 /* Note: Only dynamic payload type from the range 96-127
1134 * require to be explained further via rtpmap. All others
1135 * are implcitly definedby the number in m=audio */
1136 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001137 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001138
1139 /* Note: Use codec descriptors from enum mgcp_codecs
1140 * in mgcp_client only! */
1141 OSMO_ASSERT(codec);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001142
Philipp Maier704c4f02018-06-07 18:51:31 +02001143 rc += msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
1144 }
1145 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001146
Philipp Maier704c4f02018-06-07 18:51:31 +02001147 if (mgcp_msg->ptime)
1148 rc += msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
1149
1150 return rc;
1151}
1152
Philipp Maier275ac972018-01-20 00:58:16 +01001153/*! Generate an MGCP message
1154 * \param[in] mgcp MGCP client descriptor.
1155 * \param[in] mgcp_msg Message description
1156 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001157struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1158{
1159 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1160 uint32_t mandatory_mask;
1161 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1162 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001163 int rc_sdp;
1164 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001165 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001166
1167 msg->l2h = msg->data;
1168 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1169
1170 /* Add command verb */
1171 switch (mgcp_msg->verb) {
1172 case MGCP_VERB_CRCX:
1173 mandatory_mask = MGCP_CRCX_MANDATORY;
1174 rc += msgb_printf(msg, "CRCX %u", trans_id);
1175 break;
1176 case MGCP_VERB_MDCX:
1177 mandatory_mask = MGCP_MDCX_MANDATORY;
1178 rc += msgb_printf(msg, "MDCX %u", trans_id);
1179 break;
1180 case MGCP_VERB_DLCX:
1181 mandatory_mask = MGCP_DLCX_MANDATORY;
1182 rc += msgb_printf(msg, "DLCX %u", trans_id);
1183 break;
1184 case MGCP_VERB_AUEP:
1185 mandatory_mask = MGCP_AUEP_MANDATORY;
1186 rc += msgb_printf(msg, "AUEP %u", trans_id);
1187 break;
1188 case MGCP_VERB_RSIP:
1189 mandatory_mask = MGCP_RSIP_MANDATORY;
1190 rc += msgb_printf(msg, "RSIP %u", trans_id);
1191 break;
1192 default:
1193 LOGP(DLMGCP, LOGL_ERROR,
1194 "Invalid command verb, can not generate MGCP message\n");
1195 msgb_free(msg);
1196 return NULL;
1197 }
1198
1199 /* Check if mandatory fields are missing */
1200 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1201 LOGP(DLMGCP, LOGL_ERROR,
1202 "One or more missing mandatory fields, can not generate MGCP message\n");
1203 msgb_free(msg);
1204 return NULL;
1205 }
1206
1207 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001208 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1209 if (strlen(mgcp_msg->endpoint) <= 0) {
1210 LOGP(DLMGCP, LOGL_ERROR,
1211 "Empty endpoint name, can not generate MGCP message\n");
1212 msgb_free(msg);
1213 return NULL;
1214 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001215
1216 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1217 LOGP(DLMGCP, LOGL_ERROR,
1218 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1219 mgcp_msg->endpoint);
1220 msgb_free(msg);
1221 }
1222
Philipp Maier1dc6be62017-10-05 18:25:37 +02001223 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001224 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001225
1226 /* Add protocol version */
1227 rc += msgb_printf(msg, " MGCP 1.0\r\n");
1228
1229 /* Add call id */
1230 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
1231 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
1232
1233 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001234 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1235 if (strlen(mgcp_msg->conn_id) <= 0) {
1236 LOGP(DLMGCP, LOGL_ERROR,
1237 "Empty connection id, can not generate MGCP message\n");
1238 msgb_free(msg);
1239 return NULL;
1240 }
Philipp Maier01d24a32017-11-21 17:26:09 +01001241 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001242 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001243
Philipp Maier704c4f02018-06-07 18:51:31 +02001244 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1245 * if we do not know this information yet, we fall back to LCO */
1246 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1247 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1248 use_sdp = true;
1249
1250 /* Add local connection options (LCO) */
1251 if (!use_sdp
1252 && (mgcp_msg->verb == MGCP_VERB_CRCX
1253 || mgcp_msg->verb == MGCP_VERB_MDCX))
1254 rc += add_lco(msg, mgcp_msg);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001255
1256 /* Add mode */
1257 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
1258 rc +=
1259 msgb_printf(msg, "M: %s\r\n",
1260 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1261
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001262 /* Add X-Osmo-IGN */
1263 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1264 && (mgcp_msg->x_osmo_ign != 0))
1265 rc +=
1266 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1267 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1268
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001269 /* Add X-Osmo-Osmux */
1270 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001271 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
1272 LOGP(DLMGCP, LOGL_ERROR,
1273 "Wrong Osmux CID %d, can not generate MGCP message\n",
1274 mgcp_msg->x_osmo_osmux_cid);
1275 msgb_free(msg);
1276 return NULL;
1277 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001278 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
1279 rc +=
1280 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1281 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1282 }
1283
1284
Philipp Maier704c4f02018-06-07 18:51:31 +02001285 /* Add session description protocol (SDP) */
1286 if (use_sdp
1287 && (mgcp_msg->verb == MGCP_VERB_CRCX
1288 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1289 rc_sdp = add_sdp(msg, mgcp_msg, mgcp);
1290 if (rc_sdp == -2)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001291 return NULL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001292 else
1293 rc += rc_sdp;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001294 }
1295
1296 if (rc != 0) {
1297 LOGP(DLMGCP, LOGL_ERROR,
1298 "message buffer to small, can not generate MGCP message\n");
1299 msgb_free(msg);
1300 msg = NULL;
1301 }
1302
1303 return msg;
1304}
1305
Philipp Maier275ac972018-01-20 00:58:16 +01001306/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1307 * \param[in] msg message buffer
1308 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001309mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1310{
1311 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1312}
1313
Philipp Maier275ac972018-01-20 00:58:16 +01001314/*! Get the configuration parameters a given MGCP client instance
1315 * \param[in] mgcp MGCP client descriptor.
1316 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001317struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001318{
1319 return &mgcp->actual;
1320}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001321
1322const struct value_string mgcp_client_connection_mode_strs[] = {
1323 { MGCP_CONN_NONE, "none" },
1324 { MGCP_CONN_RECV_SEND, "sendrecv" },
1325 { MGCP_CONN_SEND_ONLY, "sendonly" },
1326 { MGCP_CONN_RECV_ONLY, "recvonly" },
1327 { MGCP_CONN_LOOPBACK, "loopback" },
1328 { 0, NULL }
1329};