vty: add ctrl section for Control interface bind address

This may seem like overkill for a mere const char * config item, but it makes
the Control interface VTY commands reusable in any main() scope (inspired by
libosmo-abis' VTY config).

Add API functions ctrl_vty_init() and ctrl_vty_get_bind_addr(), in new files
src/ctrl/control_vty.c and include/osmocom/ctrl/control_vty.h, compiled and/or
installed dependent on ENABLE_VTY.

Using these functions allows configuring a static const char* with the VTY
commands

    ctrl
     bind A.B.C.D

which callers shall subsequently use to bind the Control interface to a
specific local interface address, by passing the return value of
ctrl_vty_get_bind_addr() to control_interface_setup().

Add CTRL_NODE to enum node_type, "eating" RESERVED4_NODE to heed that comment
on avoiding ABI changes.
This commit is contained in:
Neels Hofmeyr 2016-02-24 00:10:41 +01:00
parent 4934309dab
commit f81eb328b6
5 changed files with 104 additions and 2 deletions

View File

@ -117,7 +117,8 @@ nobase_include_HEADERS += \
osmocom/vty/telnet_interface.h \
osmocom/vty/vector.h \
osmocom/vty/vty.h \
osmocom/vty/ports.h
osmocom/vty/ports.h \
osmocom/ctrl/control_vty.h
endif
noinst_HEADERS = \

View File

@ -0,0 +1,9 @@
#pragma once
/* Add the 'ctrl' section to VTY, containing the 'bind' command. */
int ctrl_vty_init(void *ctx);
/* Obtain the IP address configured by the 'ctrl'/'bind A.B.C.D' VTY command.
* This should be fed to ctrl_interface_setup() once the configuration has been
* read. */
const char *ctrl_vty_get_bind_addr(void);

View File

@ -83,6 +83,7 @@ enum node_type {
L_IPA_NODE, /*!< \brief IPA proxying commands in libosmo-abis. */
L_NS_NODE, /*!< \brief NS node in libosmo-gb. */
L_BSSGP_NODE, /*!< \brief BSSGP node in libosmo-gb. */
L_CTRL_NODE, /*!< \brief Control interface node. */
/*
* When adding new nodes to the libosmocore project, these nodes can be
@ -91,7 +92,6 @@ enum node_type {
RESERVED1_NODE, /*!< \brief Reserved for later extensions */
RESERVED2_NODE, /*!< \brief Reserved for later extensions */
RESERVED3_NODE, /*!< \brief Reserved for later extensions */
RESERVED4_NODE, /*!< \brief Reserved for later extensions */
_LAST_OSMOVTY_NODE
};

View File

@ -13,3 +13,7 @@ libosmoctrl_la_LIBADD = \
$(top_builddir)/src/libosmocore.la \
$(top_builddir)/src/gsm/libosmogsm.la \
$(top_builddir)/src/vty/libosmovty.la
if ENABLE_VTY
libosmoctrl_la_SOURCES += control_vty.c
endif

88
src/ctrl/control_vty.c Normal file
View File

@ -0,0 +1,88 @@
/* VTY configuration for Control interface
*
* (C) 2016 by sysmocom s.m.f.c. GmbH <info@sysmocom.de>
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <stdlib.h>
#include <talloc.h>
#include <osmocom/ctrl/control_vty.h>
#include <osmocom/vty/command.h>
static void *ctrl_vty_ctx = NULL;
static const char *ctrl_vty_bind_addr = NULL;
DEFUN(cfg_ctrl_bind_addr,
cfg_ctrl_bind_addr_cmd,
"bind A.B.C.D",
"Set bind address to listen for Control connections\n"
"Local IP address (default 127.0.0.1)\n")
{
talloc_free((char*)ctrl_vty_bind_addr);
ctrl_vty_bind_addr = NULL;
ctrl_vty_bind_addr = talloc_strdup(ctrl_vty_ctx, argv[0]);
return CMD_SUCCESS;
}
const char *ctrl_vty_get_bind_addr(void)
{
if (!ctrl_vty_bind_addr)
return "127.0.0.1";
return ctrl_vty_bind_addr;
}
static struct cmd_node ctrl_node = {
L_CTRL_NODE,
"%s(config-ctrl)# ",
1,
};
DEFUN(cfg_ctrl,
cfg_ctrl_cmd,
"ctrl", "Configure the Control Interface")
{
vty->index = NULL;
vty->node = L_CTRL_NODE;
return CMD_SUCCESS;
}
static int config_write_ctrl(struct vty *vty)
{
/* So far there's only one element. Omit the entire section if the bind
* element is omitted. */
if (!ctrl_vty_bind_addr)
return CMD_SUCCESS;
vty_out(vty, "ctrl%s", VTY_NEWLINE);
vty_out(vty, " bind %s%s", ctrl_vty_bind_addr, VTY_NEWLINE);
return CMD_SUCCESS;
}
int ctrl_vty_init(void *ctx)
{
ctrl_vty_ctx = ctx;
install_element(CONFIG_NODE, &cfg_ctrl_cmd);
install_node(&ctrl_node, config_write_ctrl);
install_element(L_CTRL_NODE, &cfg_ctrl_bind_addr_cmd);
return 0;
}