Neels Hofmeyr | f81eb32 | 2016-02-24 00:10:41 +0100 | [diff] [blame] | 1 | /* VTY configuration for Control interface |
| 2 | * |
| 3 | * (C) 2016 by sysmocom s.m.f.c. GmbH <info@sysmocom.de> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
Harald Welte | f2899c6 | 2017-01-15 17:54:11 +0100 | [diff] [blame] | 24 | #include <osmocom/core/talloc.h> |
Neels Hofmeyr | f81eb32 | 2016-02-24 00:10:41 +0100 | [diff] [blame] | 25 | #include <osmocom/ctrl/control_vty.h> |
| 26 | #include <osmocom/vty/command.h> |
| 27 | |
| 28 | static void *ctrl_vty_ctx = NULL; |
| 29 | static const char *ctrl_vty_bind_addr = NULL; |
| 30 | |
| 31 | DEFUN(cfg_ctrl_bind_addr, |
| 32 | cfg_ctrl_bind_addr_cmd, |
| 33 | "bind A.B.C.D", |
| 34 | "Set bind address to listen for Control connections\n" |
| 35 | "Local IP address (default 127.0.0.1)\n") |
| 36 | { |
| 37 | talloc_free((char*)ctrl_vty_bind_addr); |
| 38 | ctrl_vty_bind_addr = NULL; |
| 39 | ctrl_vty_bind_addr = talloc_strdup(ctrl_vty_ctx, argv[0]); |
| 40 | return CMD_SUCCESS; |
| 41 | } |
| 42 | |
| 43 | const char *ctrl_vty_get_bind_addr(void) |
| 44 | { |
| 45 | if (!ctrl_vty_bind_addr) |
| 46 | return "127.0.0.1"; |
| 47 | return ctrl_vty_bind_addr; |
| 48 | } |
| 49 | |
| 50 | static struct cmd_node ctrl_node = { |
| 51 | L_CTRL_NODE, |
| 52 | "%s(config-ctrl)# ", |
| 53 | 1, |
| 54 | }; |
| 55 | |
| 56 | DEFUN(cfg_ctrl, |
| 57 | cfg_ctrl_cmd, |
| 58 | "ctrl", "Configure the Control Interface") |
| 59 | { |
| 60 | vty->index = NULL; |
| 61 | vty->node = L_CTRL_NODE; |
| 62 | |
| 63 | return CMD_SUCCESS; |
| 64 | } |
| 65 | |
| 66 | static int config_write_ctrl(struct vty *vty) |
| 67 | { |
| 68 | /* So far there's only one element. Omit the entire section if the bind |
| 69 | * element is omitted. */ |
| 70 | if (!ctrl_vty_bind_addr) |
| 71 | return CMD_SUCCESS; |
| 72 | |
| 73 | vty_out(vty, "ctrl%s", VTY_NEWLINE); |
| 74 | vty_out(vty, " bind %s%s", ctrl_vty_bind_addr, VTY_NEWLINE); |
| 75 | |
| 76 | return CMD_SUCCESS; |
| 77 | } |
| 78 | |
| 79 | int ctrl_vty_init(void *ctx) |
| 80 | { |
| 81 | ctrl_vty_ctx = ctx; |
| 82 | install_element(CONFIG_NODE, &cfg_ctrl_cmd); |
| 83 | install_node(&ctrl_node, config_write_ctrl); |
| 84 | |
| 85 | install_element(L_CTRL_NODE, &cfg_ctrl_bind_addr_cmd); |
| 86 | return 0; |
| 87 | } |
| 88 | |