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