blob: a823b0c829c0a4a07788992e7668f8418323eca7 [file] [log] [blame]
Neels Hofmeyr87203f22017-10-31 01:15:45 +01001/* MGCP client interface to quagga VTY */
Neels Hofmeyre9920f22017-07-10 15:07:22 +02002/* (C) 2016 by sysmocom s.m.f.c. GmbH <info@sysmocom.de>
3 * Based on OpenBSC interface to quagga VTY (libmsc/vty_interface_layer3.c)
4 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2009-2011 by Holger Hans Peter Freyther
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte220f4872018-02-04 09:04:16 +01009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
Neels Hofmeyre9920f22017-07-10 15:07:22 +020011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte220f4872018-02-04 09:04:16 +010016 * GNU General Public License for more details.
Neels Hofmeyre9920f22017-07-10 15:07:22 +020017 *
Harald Welte220f4872018-02-04 09:04:16 +010018 * You should have received a copy of the GNU General Public License
Neels Hofmeyre9920f22017-07-10 15:07:22 +020019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <inttypes.h>
24#include <stdlib.h>
25#include <talloc.h>
26
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020027#include <osmocom/vty/vty.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020028#include <osmocom/vty/command.h>
Stefan Sperling29248252018-02-22 18:09:28 +010029#include <osmocom/vty/misc.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020030#include <osmocom/core/utils.h>
31
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020032#include <osmocom/mgcp_client/mgcp_client.h>
Philipp Maier3f2c15f2021-07-22 11:53:07 +020033#include <osmocom/mgcp_client/mgcp_client_internal.h>
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020034#include <osmocom/mgcp_client/mgcp_client_pool_internal.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020035
Neels Hofmeyre6d8e912018-08-23 16:36:48 +020036#define MGW_STR MGCP_CLIENT_MGW_STR
Neels Hofmeyre9920f22017-07-10 15:07:22 +020037
Pau Espin Pedrolf6333022022-10-13 13:20:10 +020038/* Only common (non-pooled) VTY commands will use this talloc context. All
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020039 * pooled VTY commands will use the pool (global_mgcp_client_pool) as
40 * talloc context. */
41static void *global_mgcp_client_ctx = NULL;
42
43/* MGCP Client configuration used with mgcp_client_vty_init(). (This pointer
44 * points to user provided memory, so it cannot be used as talloc context.) */
45static struct mgcp_client_conf *global_mgcp_client_conf = NULL;
46
47/* Pointer to the MGCP pool that is managed by mgcp_client_pool_vty_init() */
48static struct mgcp_client_pool *global_mgcp_client_pool = NULL;
49
50struct mgcp_client_conf *get_mgcp_client_config(struct vty *vty)
51{
52 if (global_mgcp_client_pool && vty->node == global_mgcp_client_pool->vty_node->node)
53 return vty->index;
54 else
55 return global_mgcp_client_conf;
56}
Neels Hofmeyre9920f22017-07-10 15:07:22 +020057
Neels Hofmeyr87203f22017-10-31 01:15:45 +010058DEFUN(cfg_mgw_local_ip, cfg_mgw_local_ip_cmd,
Pau Espin Pedrol531470a2020-08-31 17:06:55 +020059 "mgw local-ip " VTY_IPV46_CMD,
Neels Hofmeyr54dd4b32017-10-31 01:18:33 +010060 MGW_STR "local bind to connect to MGW from\n"
Pau Espin Pedrol531470a2020-08-31 17:06:55 +020061 "local bind IPv4 address\n"
62 "local bind IPv6 address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020063{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020064 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
65
Neels Hofmeyrc3132fd2018-12-19 00:30:34 +010066 osmo_talloc_replace_string(global_mgcp_client_ctx,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020067 (char **)&conf->local_addr,
Neels Hofmeyrc3132fd2018-12-19 00:30:34 +010068 argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020069 return CMD_SUCCESS;
70}
Neels Hofmeyr87203f22017-10-31 01:15:45 +010071ALIAS_DEPRECATED(cfg_mgw_local_ip, cfg_mgcpgw_local_ip_cmd,
72 "mgcpgw local-ip A.B.C.D",
73 MGW_STR "local bind to connect to MGCP gateway with\n"
74 "local bind IP address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020075
Neels Hofmeyr87203f22017-10-31 01:15:45 +010076DEFUN(cfg_mgw_local_port, cfg_mgw_local_port_cmd,
77 "mgw local-port <0-65535>",
Neels Hofmeyr54dd4b32017-10-31 01:18:33 +010078 MGW_STR "local port to connect to MGW from\n"
Neels Hofmeyre9920f22017-07-10 15:07:22 +020079 "local bind port\n")
80{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020081 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
82
83 conf->local_port = atoi(argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020084 return CMD_SUCCESS;
85}
Neels Hofmeyr87203f22017-10-31 01:15:45 +010086ALIAS_DEPRECATED(cfg_mgw_local_port, cfg_mgcpgw_local_port_cmd,
87 "mgcpgw local-port <0-65535>",
88 MGW_STR "local bind to connect to MGCP gateway with\n"
89 "local bind port\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020090
Neels Hofmeyr87203f22017-10-31 01:15:45 +010091DEFUN(cfg_mgw_remote_ip, cfg_mgw_remote_ip_cmd,
Pau Espin Pedrol531470a2020-08-31 17:06:55 +020092 "mgw remote-ip " VTY_IPV46_CMD,
Neels Hofmeyr54dd4b32017-10-31 01:18:33 +010093 MGW_STR "remote IP address to reach the MGW at\n"
Pau Espin Pedrol531470a2020-08-31 17:06:55 +020094 "remote IPv4 address\n"
95 "remote IPv6 address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020096{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020097 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
98
Neels Hofmeyrc3132fd2018-12-19 00:30:34 +010099 osmo_talloc_replace_string(global_mgcp_client_ctx,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200100 (char **)&conf->remote_addr, argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200101 return CMD_SUCCESS;
102}
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100103ALIAS_DEPRECATED(cfg_mgw_remote_ip, cfg_mgcpgw_remote_ip_cmd,
104 "mgcpgw remote-ip A.B.C.D",
105 MGW_STR "remote bind to connect to MGCP gateway with\n"
106 "remote bind IP address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200107
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100108DEFUN(cfg_mgw_remote_port, cfg_mgw_remote_port_cmd,
109 "mgw remote-port <0-65535>",
Neels Hofmeyr54dd4b32017-10-31 01:18:33 +0100110 MGW_STR "remote port to reach the MGW at\n"
111 "remote port\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200112{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200113 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
114
115 conf->remote_port = atoi(argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200116 return CMD_SUCCESS;
117}
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100118ALIAS_DEPRECATED(cfg_mgw_remote_port, cfg_mgcpgw_remote_port_cmd,
119 "mgcpgw remote-port <0-65535>",
120 MGW_STR "remote bind to connect to MGCP gateway with\n"
121 "remote bind port\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200122
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100123DEFUN_DEPRECATED(cfg_mgw_endpoint_range, cfg_mgw_endpoint_range_cmd,
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100124 "mgw endpoint-range <1-65534> <1-65534>",
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100125 MGW_STR "DEPRECATED: the endpoint range cannot be defined by the client\n"
126 "-\n" "-\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200127{
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100128 vty_out(vty, "Please do not use legacy config 'mgw endpoint-range'"
129 " (the range can no longer be defined by the MGCP client)%s",
130 VTY_NEWLINE);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200131 return CMD_SUCCESS;
132}
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100133ALIAS_DEPRECATED(cfg_mgw_endpoint_range, cfg_mgcpgw_endpoint_range_cmd,
134 "mgcpgw endpoint-range <1-65534> <1-65534>",
135 MGW_STR "usable range of endpoint identifiers\n"
136 "set first useable endpoint identifier\n"
137 "set the last useable endpoint identifier\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200138
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000139#define BTS_START_STR "First UDP port allocated for the BTS side\n"
140#define UDP_PORT_STR "UDP Port number\n"
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100141DEFUN_DEPRECATED(cfg_mgw_rtp_bts_base_port,
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000142 cfg_mgw_rtp_bts_base_port_cmd,
143 "mgw bts-base <0-65534>",
144 MGW_STR
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100145 "DEPRECATED: there is no explicit BTS side in current osmo-mgw\n" "-\n")
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000146{
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100147 vty_out(vty, "Please do not use legacy config 'mgw bts-base'"
148 " (there is no explicit BTS side in an MGW anymore)%s",
149 VTY_NEWLINE);
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000150 return CMD_SUCCESS;
151}
152ALIAS_DEPRECATED(cfg_mgw_rtp_bts_base_port,
153 cfg_mgcpgw_rtp_bts_base_port_cmd,
154 "mgcpgw bts-base <0-65534>",
155 MGW_STR
156 BTS_START_STR
157 UDP_PORT_STR)
158
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100159DEFUN(cfg_mgw_endpoint_domain_name,
160 cfg_mgw_endpoint_domain_name_cmd,
161 "mgw endpoint-domain NAME",
162 MGW_STR "Set the domain name to send in MGCP messages, e.g. the part 'foo' in 'rtpbridge/*@foo'.\n"
163 "Domain name, should be alphanumeric.\n")
164{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200165 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
166
167 if (osmo_strlcpy(conf->endpoint_domain_name, argv[0], sizeof(conf->endpoint_domain_name))
168 >= sizeof(conf->endpoint_domain_name)) {
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100169 vty_out(vty, "%% Error: 'mgw endpoint-domain' name too long, max length is %zu: '%s'%s",
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200170 sizeof(conf->endpoint_domain_name) - 1, argv[0], VTY_NEWLINE);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100171 return CMD_WARNING;
172 }
173 return CMD_SUCCESS;
174}
175
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200176DEFUN(cfg_mgw_reset_ep_name,
177 cfg_mgw_reset_ep_name_cmd,
178 "mgw reset-endpoint NAME",
179 MGW_STR "Add an endpoint name that should be reset (DLCX) on connect to the reset-endpoint list,"
180 "e.g. 'rtpbridge/*'\n"
181 "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n")
182{
183 int rc;
184 struct reset_ep *reset_ep;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200185 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200186
187 /* stop when the address is already in the list */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200188 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200189 if (strcmp(argv[0], reset_ep->name) == 0) {
190 vty_out(vty, "%% duplicate endpoint name configured ('%s')%s", argv[0], VTY_NEWLINE);
191 return CMD_WARNING;
192 }
193 }
194
195 /* the domain name is not part of the actual endpoint name */
196 if (strchr(argv[0], '@')) {
197 vty_out(vty, "%% the endpoint name must be given without domain name ('%s')%s",
198 argv[0], VTY_NEWLINE);
199 return CMD_WARNING;
200 }
201
202 reset_ep = talloc_zero(global_mgcp_client_ctx, struct reset_ep);
203 OSMO_ASSERT(reset_ep);
204
205 rc = osmo_strlcpy(reset_ep->name, argv[0], sizeof(reset_ep->name));
206 if (rc >= sizeof(reset_ep->name)) {
207 vty_out(vty, "%% Error: 'mgw reset-endpoint' name too long, max length is %zu: '%s'%s",
208 sizeof(reset_ep->name) - 1, argv[0], VTY_NEWLINE);
209 talloc_free(reset_ep);
210 return CMD_WARNING;
211 }
212
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200213 llist_add_tail(&reset_ep->list, &conf->reset_epnames);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200214
215 return CMD_SUCCESS;
216}
217
218DEFUN(cfg_mgw_no_reset_ep_name,
219 cfg_mgw_no_reset_ep_name_cmd,
220 "no mgw reset-endpoint NAME",
Oliver Smith29e671f2021-08-18 08:44:47 +0200221 NO_STR MGW_STR "remove an endpoint name from the reset-endpoint list, e.g. 'rtpbridge/*'\n"
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200222 "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n")
223{
224 struct reset_ep *reset_ep;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200225 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200226
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200227 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200228 if (strcmp(argv[0], reset_ep->name) == 0) {
229 llist_del(&reset_ep->list);
230 talloc_free(reset_ep);
231 return CMD_SUCCESS;
232 }
233 }
234
235 vty_out(vty, "%% no such endpoint name configured ('%s')%s", argv[0], VTY_NEWLINE);
236 return CMD_WARNING;
237}
238
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200239static int config_write(struct vty *vty, const char *indent, struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200240{
241 const char *addr;
242 int port;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200243 struct reset_ep *reset_ep;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200244
Philipp Maierdf9192e2021-09-03 17:50:51 +0200245 if (conf->description)
246 vty_out(vty, "%sdescription %s%s", indent, conf->description, VTY_NEWLINE);
247
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200248 addr = conf->local_addr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200249 if (addr)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100250 vty_out(vty, "%smgw local-ip %s%s", indent, addr,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200251 VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200252 port = conf->local_port;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200253 if (port >= 0)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100254 vty_out(vty, "%smgw local-port %u%s", indent,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200255 (uint16_t)port, VTY_NEWLINE);
256
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200257 addr = conf->remote_addr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200258 if (addr)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100259 vty_out(vty, "%smgw remote-ip %s%s", indent, addr,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200260 VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200261 port = conf->remote_port;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200262 if (port >= 0)
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100263 vty_out(vty, "%smgw remote-port %u%s", indent,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200264 (uint16_t)port, VTY_NEWLINE);
265
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200266 if (conf->endpoint_domain_name[0])
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100267 vty_out(vty, "%smgw endpoint-domain %s%s", indent,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200268 conf->endpoint_domain_name, VTY_NEWLINE);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100269
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200270 llist_for_each_entry(reset_ep, &conf->reset_epnames, list)
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200271 vty_out(vty, "%smgw reset-endpoint %s%s", indent, reset_ep->name, VTY_NEWLINE);
272
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200273 return CMD_SUCCESS;
274}
275
Philipp Maierd6a7e172021-08-03 16:03:27 +0200276/*! Write out MGCP client config to VTY.
277 * \param[in] vty VTY to which we should print.
278 * \param[in] string used for indentation (e.g. " ").
279 * \returns CMD_SUCCESS on success, CMD_WARNING on error */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200280int mgcp_client_config_write(struct vty *vty, const char *indent)
281{
282 return config_write(vty, indent, global_mgcp_client_conf);
283}
284
285static void vty_init_common(void *talloc_ctx, int node)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200286{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200287 global_mgcp_client_ctx = talloc_ctx;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200288
Vadim Yanitskiy3ba40952020-10-04 16:42:04 +0700289 install_lib_element(node, &cfg_mgw_local_ip_cmd);
290 install_lib_element(node, &cfg_mgw_local_port_cmd);
291 install_lib_element(node, &cfg_mgw_remote_ip_cmd);
292 install_lib_element(node, &cfg_mgw_remote_port_cmd);
293 install_lib_element(node, &cfg_mgw_endpoint_range_cmd);
294 install_lib_element(node, &cfg_mgw_rtp_bts_base_port_cmd);
295 install_lib_element(node, &cfg_mgw_endpoint_domain_name_cmd);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200296 install_lib_element(node, &cfg_mgw_reset_ep_name_cmd);
297 install_lib_element(node, &cfg_mgw_no_reset_ep_name_cmd);
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100298
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200299 osmo_fsm_vty_add_cmds();
300}
301
Philipp Maierd6a7e172021-08-03 16:03:27 +0200302/*! Set up MGCP client VTY
303 * (called once at startup by the application process).
304 * \param[in] talloc_ctx talloc context to be used by the VTY for allocating memory.
305 * \param[in] node identifier of the node on which the VTY commands should be installed.
306 * \param[in] conf user provided memory to to store the MGCP client configuration data. */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200307void mgcp_client_vty_init(void *talloc_ctx, int node, struct mgcp_client_conf *conf)
308{
309 global_mgcp_client_conf = conf;
310
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100311 /* deprecated 'mgcpgw' commands */
Vadim Yanitskiy3ba40952020-10-04 16:42:04 +0700312 install_lib_element(node, &cfg_mgcpgw_local_ip_cmd);
313 install_lib_element(node, &cfg_mgcpgw_local_port_cmd);
314 install_lib_element(node, &cfg_mgcpgw_remote_ip_cmd);
315 install_lib_element(node, &cfg_mgcpgw_remote_port_cmd);
316 install_lib_element(node, &cfg_mgcpgw_endpoint_range_cmd);
317 install_lib_element(node, &cfg_mgcpgw_rtp_bts_base_port_cmd);
Stefan Sperling29248252018-02-22 18:09:28 +0100318
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200319 vty_init_common(talloc_ctx, node);
320}
321
322static int config_write_pool(struct vty *vty)
323{
324 struct mgcp_client_pool *pool = global_mgcp_client_pool;
325 struct mgcp_client_pool_member *pool_member;
326 unsigned int indent_buf_len = strlen(pool->vty_indent) + 1 + 1;
327 char *indent = talloc_zero_size(vty, indent_buf_len);
328
329 snprintf(indent, indent_buf_len, "%s ", pool->vty_indent);
330
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200331 llist_for_each_entry(pool_member, &pool->member_list, list) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200332 vty_out(vty, "%smgw %u%s", pool->vty_indent, pool_member->nr, VTY_NEWLINE);
333 config_write(vty, indent, &pool_member->conf);
334 }
335
336 talloc_free(indent);
337 return CMD_SUCCESS;
338}
339
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200340DEFUN_ATTR(cfg_mgw,
Philipp Maierd55be052021-08-19 14:58:31 +0200341 cfg_mgw_cmd, "mgw <0-255>", "Select a MGCP client config to setup\n" "reference number\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200342{
343 int nr = atoi(argv[0]);
344 struct mgcp_client_pool_member *pool_member;
345
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200346 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200347 if (!pool_member) {
Pau Espin Pedrol6ddc79b2022-10-13 17:03:42 +0200348 pool_member = mgcp_client_pool_member_alloc(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200349 OSMO_ASSERT(pool_member);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200350 }
351
352 vty->index = &pool_member->conf;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200353 vty->index_sub = &pool_member->conf.description;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200354 vty->node = global_mgcp_client_pool->vty_node->node;
355
356 return CMD_SUCCESS;
357}
358
359DEFUN_ATTR(cfg_no_mgw,
360 cfg_no_mgw_cmd,
Philipp Maier74e83d32021-08-20 09:57:27 +0200361 "no mgw <0-255>", NO_STR "Select a MGCP client config to remove\n" "reference number\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200362{
363 int nr = atoi(argv[0]);
364 struct mgcp_client_pool_member *pool_member;
365
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200366 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200367 if (!pool_member) {
368 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
369 return CMD_WARNING;
370 }
371
372 /* Make sure that there are no ongoing calls */
373 if (pool_member->refcount > 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200374 vty_out(vty, "%% MGCP client (MGW %s) is still serving ongoing calls -- can't remove it now!%s",
375 mgcp_client_pool_member_name(pool_member), VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200376 return CMD_WARNING;
377 }
378
Pau Espin Pedrol6ddc79b2022-10-13 17:03:42 +0200379 mgcp_client_pool_member_free(pool_member);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200380
381 return CMD_SUCCESS;
382}
383
384DEFUN_ATTR(mgw_reconnect, mgw_reconnect_cmd,
385 "mgw <0-255> reconnect",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200386 MGW_STR "reference number\n" "reconfigure and reconnect MGCP client\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200387{
388 int nr = atoi(argv[0]);
389 struct mgcp_client_pool_member *pool_member = NULL;
390
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200391 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200392 if (!pool_member) {
393 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
394 return CMD_WARNING;
395 }
396
397 /* Make sure that there are no ongoing calls */
398 if (pool_member->refcount > 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200399 vty_out(vty, "%% MGCP client (MGW %s) is still serving ongoing calls -- can't reconnect it now!%s",
400 mgcp_client_pool_member_name(pool_member), VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200401 return CMD_WARNING;
402 }
403
Pau Espin Pedrol35bd2522022-10-13 17:26:33 +0200404 if (mgcp_client_pool_member_reinit_client(pool_member) < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200405 LOGP(DLMGCP, LOGL_ERROR, "(manual) MGW %s connect failed at (%s:%u)\n",
406 mgcp_client_pool_member_name(pool_member), pool_member->conf.remote_addr,
407 pool_member->conf.remote_port);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200408 vty_out(vty, "%% MGCP client (MGW %s) initalization failed ('%s')%s",
409 mgcp_client_pool_member_name(pool_member), argv[0], VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200410 return CMD_WARNING;
411 }
412
413 return CMD_SUCCESS;
414}
415
416DEFUN_ATTR(mgw_block, mgw_block_cmd,
417 "mgw <0-255> block",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200418 MGW_STR "reference number\n" "block MGCP client so that it won't be used for new calls\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200419{
420 int nr = atoi(argv[0]);
421 struct mgcp_client_pool_member *pool_member = NULL;
422
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200423 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200424 if (!pool_member) {
425 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
426 return CMD_WARNING;
427 }
428
429 pool_member->blocked = true;
430 return CMD_SUCCESS;
431}
432
433DEFUN_ATTR(mgw_unblock, mgw_unblock_cmd,
434 "mgw <0-255> unblock",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200435 MGW_STR "reference number\n" "unblock MGCP client so that it will be available for new calls\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200436{
437 int nr = atoi(argv[0]);
438 struct mgcp_client_pool_member *pool_member = NULL;
439
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200440 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200441 if (!pool_member) {
442 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
443 return CMD_WARNING;
444 }
445
446 pool_member->blocked = false;
447 return CMD_SUCCESS;
448}
449
Pau Espin Pedrola6917fe2022-10-13 14:40:56 +0200450DEFUN(mgw_show, mgw_show_cmd, "show mgw-pool", SHOW_STR "Display information about the MGW-Pool\n")
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200451{
452 vty_out(vty, "%% MGW-Pool:%s", VTY_NEWLINE);
453 struct mgcp_client_pool_member *pool_member;
454
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200455 if (llist_empty(&global_mgcp_client_pool->member_list) && global_mgcp_client_pool->mgcp_client_single) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200456 vty_out(vty, "%% (pool is empty, single MGCP client will be used)%s", VTY_NEWLINE);
457 return CMD_SUCCESS;
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200458 } else if (llist_empty(&global_mgcp_client_pool->member_list)) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200459 vty_out(vty, "%% (pool is empty)%s", VTY_NEWLINE);
460 return CMD_SUCCESS;
461 }
462
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200463 llist_for_each_entry(pool_member, &global_mgcp_client_pool->member_list, list) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200464 vty_out(vty, "%% MGW %s%s", mgcp_client_pool_member_name(pool_member), VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200465 vty_out(vty, "%% mgcp-client: %s%s", pool_member->client ? "connected" : "disconnected",
466 VTY_NEWLINE);
467 vty_out(vty, "%% service: %s%s", pool_member->blocked ? "blocked" : "unblocked", VTY_NEWLINE);
468 vty_out(vty, "%% ongoing calls: %u%s", pool_member->refcount, VTY_NEWLINE);
469 }
470 return CMD_SUCCESS;
471}
472
473/*! Set up MGCP client VTY (pooled)
474 * (called once at startup by the application process).
475 * \param[in] parent_node identifier of the parent node on which the mgw node appears.
476 * \param[in] mgw_node identifier that should be used with the newly installed MGW node.
477 * \param[in] indent indentation string to match the indentation in the VTY config
478 * \param[in] pool user provided memory to store the configured MGCP client (MGW) pool. */
479void mgcp_client_pool_vty_init(int parent_node, int mgw_node, const char *indent, struct mgcp_client_pool *pool)
480{
Philipp Maierda3a5752021-08-19 14:24:14 +0200481 /* A pool must be allocated before this function can be called */
482 OSMO_ASSERT(pool);
483
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200484 /* Never allow this function to be called twice on the same pool */
485 OSMO_ASSERT(!pool->vty_indent);
486 OSMO_ASSERT(!pool->vty_node);
487
488 pool->vty_indent = talloc_strdup(pool, indent);
489 OSMO_ASSERT(pool->vty_indent);
490 pool->vty_node = talloc_zero(pool, struct cmd_node);
491 OSMO_ASSERT(pool->vty_node);
492 pool->vty_node->node = mgw_node;
493 pool->vty_node->vtysh = 1;
494 pool->vty_node->prompt = talloc_strdup(pool->vty_node, "%s(config-mgw)# ");
495
496 install_lib_element(parent_node, &cfg_mgw_cmd);
497 install_lib_element(parent_node, &cfg_no_mgw_cmd);
498
499 install_node(pool->vty_node, config_write_pool);
500 vty_init_common(pool, mgw_node);
501
Philipp Maierdf9192e2021-09-03 17:50:51 +0200502 install_element(mgw_node, &cfg_description_cmd);
503
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200504 install_lib_element(ENABLE_NODE, &mgw_reconnect_cmd);
505 install_lib_element(ENABLE_NODE, &mgw_block_cmd);
506 install_lib_element(ENABLE_NODE, &mgw_unblock_cmd);
507
Pau Espin Pedrola6917fe2022-10-13 14:40:56 +0200508 install_lib_element_ve(&mgw_show_cmd);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200509
510 global_mgcp_client_pool = pool;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200511}