Add VTY command line vty bind A.B.C.D
The command merely stores the configured IP-address, which can then be used by the calling main program to set the telnet port of the VTY line. (Commits in openbsc and osmo-iuh will follow up on this.)
Add function vty_get_bind_addr() to publish the address in the vty.h API.
Add static vty_bind_addr to store.
For allocation/freeing reasons, a NULL address defaults to 127.0.0.1.
BTW, I decided against allowing keywords 'any' and 'localhost' in place of an actual IP address to make sure a written config is always identical to the parsed config. --- include/osmocom/vty/vty.h | 3 +++ src/vty/vty.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+)
diff --git a/include/osmocom/vty/vty.h b/include/osmocom/vty/vty.h index 3684397..43cb0cf 100644 --- a/include/osmocom/vty/vty.h +++ b/include/osmocom/vty/vty.h @@ -186,6 +186,9 @@ void *vty_current_index(struct vty *); int vty_current_node(struct vty *vty); int vty_go_parent(struct vty *vty);
+/* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */ +const char *vty_get_bind_addr(void); + extern void *tall_vty_ctx;
extern struct cmd_element cfg_description_cmd; diff --git a/src/vty/vty.c b/src/vty/vty.c index 5bcbe4a..7e27d7e 100644 --- a/src/vty/vty.c +++ b/src/vty/vty.c @@ -75,6 +75,10 @@ vector Vvty_serv_thread;
char *vty_cwd = NULL;
+/* IP address passed to the 'line vty'/'bind' command */ +static const char *vty_bind_addr = NULL; +#define VTY_BIND_ADDR_DEFAULT "127.0.0.1" + /* Configure lock. */ static int vty_config;
@@ -1585,6 +1589,29 @@ DEFUN(no_vty_login, return CMD_SUCCESS; }
+/* vty bind */ +DEFUN(vty_bind, vty_bind_cmd, "bind A.B.C.D", + "Accept VTY telnet connections on local interface\n" + "Local interface IP address (default: " VTY_BIND_ADDR_DEFAULT ")\n") +{ + /* Avoid (small) mem leak: initially, vty_bind_addr is NULL. Whenever + * this gets called, it is set to a strdup. So whenever it is non-NULL, + * free it first. See also vty_get_bind_addr() for the NULL default. */ + if (vty_bind_addr) { + talloc_free((void*)vty_bind_addr); + vty_bind_addr = NULL; + } + vty_bind_addr = talloc_strdup(tall_vty_ctx, argv[0]); + return CMD_SUCCESS; +} + +const char *vty_get_bind_addr(void) +{ + if (!vty_bind_addr) + return VTY_BIND_ADDR_DEFAULT; + return vty_bind_addr; +} + DEFUN(service_advanced_vty, service_advanced_vty_cmd, "service advanced-vty", @@ -1654,6 +1681,10 @@ static int vty_config_write(struct vty *vty) if (!password_check) vty_out(vty, " no login%s", VTY_NEWLINE);
+ /* bind */ + if (vty_bind_addr) + vty_out(vty, " bind %s%s", vty_bind_addr, VTY_NEWLINE); + vty_out(vty, "!%s", VTY_NEWLINE);
return CMD_SUCCESS; @@ -1757,6 +1788,7 @@ void vty_init(struct vty_app_info *app_info) vty_install_default(VTY_NODE); install_element(VTY_NODE, &vty_login_cmd); install_element(VTY_NODE, &no_vty_login_cmd); + install_element(VTY_NODE, &vty_bind_cmd); }
/*! \brief Read the configuration file using the VTY code
On Wed, Feb 24, 2016 at 03:39:35AM +0100, Neels Hofmeyr wrote:
- /* Avoid (small) mem leak: initially, vty_bind_addr is NULL. Whenever
* this gets called, it is set to a strdup. So whenever it is non-NULL,* free it first. See also vty_get_bind_addr() for the NULL default. */- if (vty_bind_addr) {
talloc_free((void*)vty_bind_addr);vty_bind_addr = NULL;- }
free() on NULL is very well-defined and valid, so is talloc_free(). So maybe just unconditionally talloc_free it and avoid three lines of code and three lines of comments?
You can keep it the way it is, but I fond it unusually verbose.
On Wed, Feb 24, 2016 at 10:04:40AM +0100, Harald Welte wrote:
On Wed, Feb 24, 2016 at 03:39:35AM +0100, Neels Hofmeyr wrote:
- /* Avoid (small) mem leak: initially, vty_bind_addr is NULL. Whenever
* this gets called, it is set to a strdup. So whenever it is non-NULL,* free it first. See also vty_get_bind_addr() for the NULL default. */- if (vty_bind_addr) {
talloc_free((void*)vty_bind_addr);vty_bind_addr = NULL;- }
free() on NULL is very well-defined and valid, so is talloc_free().
Really? Wasn't aware of that, indeed. Away with the ifs then.
I felt the comment should explain why I treat NULL as some default string -- if I set vty_bind_addr = "127.0.0.1" then I can't free that string, thus NULL. Hmm the comment sucks, yes.
Thanks!
~Neels