blob: a7ebddc274cb3664d44bff1ab20c8158d8db7a8e [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file control_vty.c
2 * VTY configuration for Control interface. */
3/*
Harald Weltee08da972017-11-13 01:00:26 +09004 * (C) 2016 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Neels Hofmeyrf81eb322016-02-24 00:10:41 +01005 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010010 * 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 Hofmeyrf81eb322016-02-24 00:10:41 +010020 */
21
22#include <stdlib.h>
Harald Weltef2899c62017-01-15 17:54:11 +010023#include <osmocom/core/talloc.h>
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010024#include <osmocom/ctrl/control_vty.h>
25#include <osmocom/vty/command.h>
26
27static void *ctrl_vty_ctx = NULL;
28static const char *ctrl_vty_bind_addr = NULL;
Max3f79ce82022-12-03 17:59:19 +030029/* Port the CTRL should bind to: -1 means not configured */
30static int ctrl_bind_port = -1;
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010031
32DEFUN(cfg_ctrl_bind_addr,
33 cfg_ctrl_bind_addr_cmd,
Max3f79ce82022-12-03 17:59:19 +030034 "bind A.B.C.D [<0-65535>]",
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010035 "Set bind address to listen for Control connections\n"
Max3f79ce82022-12-03 17:59:19 +030036 "Local IP address (default 127.0.0.1)\n"
37 "Local TCP port number\n")
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010038{
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]);
Max3f79ce82022-12-03 17:59:19 +030042 ctrl_bind_port = argc > 1 ? atoi(argv[1]) : -1;
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010043 return CMD_SUCCESS;
44}
45
46const 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
Max3f79ce82022-12-03 17:59:19 +030053uint16_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 Hofmeyrf81eb322016-02-24 00:10:41 +010058static struct cmd_node ctrl_node = {
59 L_CTRL_NODE,
60 "%s(config-ctrl)# ",
61 1,
62};
63
64DEFUN(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
74static 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
87int ctrl_vty_init(void *ctx)
88{
89 ctrl_vty_ctx = ctx;
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +070090 install_lib_element(CONFIG_NODE, &cfg_ctrl_cmd);
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010091 install_node(&ctrl_node, config_write_ctrl);
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010092
Vadim Yanitskiy8e7c4962020-10-04 15:37:31 +070093 install_lib_element(L_CTRL_NODE, &cfg_ctrl_bind_addr_cmd);
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010094 return 0;
95}
96