Add vty interface

Related: OS#2545
Change-Id: I6f2d8d1d62b97be471ebcf1bb14aac0b895833d1
This commit is contained in:
Oliver Smith 2021-12-02 14:29:12 +01:00
parent 6a2ac0a0d5
commit c448efb3aa
9 changed files with 297 additions and 0 deletions

View File

@ -1,3 +1,5 @@
noinst_HEADERS = \
bsc_nat.h \
logging.h \
vty.h \
$(NULL)

View File

@ -17,3 +17,12 @@
*
*/
#pragma once
struct bsc_nat {
};
struct bsc_nat *bsc_nat_alloc(void *tall_ctx);
void bsc_nat_free(struct bsc_nat *bsc_nat);
extern void *tall_bsc_nat_ctx;
extern struct bsc_nat *g_bsc_nat;

View File

@ -0,0 +1,27 @@
/* (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
* Author: Oliver Smith <osmith@sysmocom.de>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/lienses/>.
*
*/
#pragma once
#include <osmocom/core/logging.h>
enum {
DMAIN,
};
extern const struct log_info bsc_nat_log_info;

View File

@ -0,0 +1,31 @@
/* (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
* Author: Oliver Smith <osmith@sysmocom.de>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/lienses/>.
*
*/
#pragma once
#include <osmocom/vty/vty.h>
#include <osmocom/vty/buffer.h>
#include <osmocom/vty/command.h>
enum bsc_nat_vty_nodes {
BSC_NAT_NODE = _LAST_OSMOVTY_NODE,
};
void bsc_nat_vty_init(void);
int bsc_nat_vty_go_parent(struct vty *vty);

View File

@ -25,7 +25,10 @@ bin_PROGRAMS = \
$(NULL)
osmo_bsc_nat_SOURCES = \
bsc_nat.c \
logging.c \
main.c \
vty.c \
$(NULL)
osmo_bsc_nat_LDADD = \

View File

@ -0,0 +1,38 @@
/* (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
* Author: Oliver Smith <osmith@sysmocom.de>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/lienses/>.
*
*/
#include "config.h"
#include <osmocom/core/talloc.h>
#include <osmocom/bsc_nat/bsc_nat.h>
struct bsc_nat *bsc_nat_alloc(void *tall_ctx)
{
struct bsc_nat *bsc_nat;
bsc_nat = talloc_zero(tall_ctx, struct bsc_nat);
OSMO_ASSERT(bsc_nat);
return bsc_nat;
}
void bsc_nat_free(struct bsc_nat *bsc_nat)
{
talloc_free(bsc_nat);
}

View File

@ -0,0 +1,37 @@
/* (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
* Author: Oliver Smith <osmith@sysmocom.de>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/lienses/>.
*
*/
#include <osmocom/core/logging.h>
#include <osmocom/core/utils.h>
#include <osmocom/bsc_nat/logging.h>
static const struct log_info_cat log_cat[] = {
[DMAIN] = {
.name = "DMAIN",
.loglevel = LOGL_NOTICE,
.enabled = 1,
.color = "",
.description = "Main program",
},
};
const struct log_info bsc_nat_log_info = {
.cat = log_cat,
.num_cat = ARRAY_SIZE(log_cat),
};

View File

@ -19,7 +19,78 @@
#include "config.h"
#include <osmocom/core/application.h>
#include <osmocom/vty/cpu_sched_vty.h>
#include <osmocom/vty/logging.h>
#include <osmocom/vty/misc.h>
#include <osmocom/vty/ports.h>
#include <osmocom/vty/telnet_interface.h>
#include <osmocom/bsc_nat/bsc_nat.h>
#include <osmocom/bsc_nat/logging.h>
#include <osmocom/bsc_nat/vty.h>
static const char *const copyright =
"OsmoBSCNAT - Osmocom BSC NAT\r\n"
"Copyright (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n"
"Author: Oliver Smith\r\n"
"License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
"This is free software: you are free to change and redistribute it.\r\n"
"There is NO WARRANTY, to the extent permitted by law.\r\n";
void *tall_bsc_nat_ctx;
struct bsc_nat *g_bsc_nat;
static struct vty_app_info vty_info = {
.name = "OsmoBSCNAT",
.version = PACKAGE_VERSION,
.go_parent_cb = bsc_nat_vty_go_parent,
};
static void main_vty_init()
{
int rc;
vty_info.copyright = copyright;
vty_info.tall_ctx = tall_bsc_nat_ctx;
vty_init(&vty_info);
bsc_nat_vty_init();
logging_vty_add_cmds();
osmo_talloc_vty_add_cmds();
osmo_cpu_sched_vty_init(tall_bsc_nat_ctx);
rc = telnet_init_dynif(tall_bsc_nat_ctx, g_bsc_nat, vty_get_bind_addr(), OSMO_VTY_PORT_BSC_NAT);
if (rc < 0) {
perror("Error binding VTY port");
exit(1);
}
}
int main(int argc, char **argv)
{
int rc;
talloc_enable_null_tracking();
tall_bsc_nat_ctx = talloc_named_const(NULL, 0, "bsc_nat");
rc = osmo_init_logging2(tall_bsc_nat_ctx, &bsc_nat_log_info);
if (rc < 0)
exit(1);
g_bsc_nat = bsc_nat_alloc(tall_bsc_nat_ctx);
main_vty_init();
while (!osmo_select_shutdown_done())
osmo_select_main_ctx(0);
talloc_report_full(tall_bsc_nat_ctx, stderr);
talloc_free(tall_bsc_nat_ctx);
talloc_free(tall_vty_ctx);
talloc_report_full(NULL, stderr);
talloc_disable_null_tracking();
return 0;
}

79
src/osmo-bsc-nat/vty.c Normal file
View File

@ -0,0 +1,79 @@
/* (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
* Author: Oliver Smith <osmith@sysmocom.de>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/lienses/>.
*
*/
#include "config.h"
#include <unistd.h>
#include <osmocom/vty/telnet_interface.h>
#include <osmocom/vty/logging.h>
#include <osmocom/vty/command.h>
#include <osmocom/bsc_nat/bsc_nat.h>
#include <osmocom/bsc_nat/vty.h>
int bsc_nat_vty_go_parent(struct vty *vty)
{
switch (vty->node) {
case BSC_NAT_NODE:
vty->node = CONFIG_NODE;
vty->index = g_bsc_nat;
break;
case CONFIG_NODE:
vty->node = ENABLE_NODE;
vty->index = NULL;
break;
default:
vty->node = CONFIG_NODE;
vty->index = NULL;
break;
}
return vty->node;
}
static struct cmd_node bsc_nat_node = {
BSC_NAT_NODE,
"%s(config-bsc-nat)# ",
1,
};
DEFUN(cfg_bsc_nat,
cfg_bsc_nat_cmd,
"bsc-nat", "Configure the BSC NAT\n")
{
OSMO_ASSERT(g_bsc_nat);
vty->index = g_bsc_nat;
vty->node = BSC_NAT_NODE;
return CMD_SUCCESS;
}
static int config_write_bsc_nat(struct vty *vty)
{
vty_out(vty, "bsc-nat%s", VTY_NEWLINE);
return CMD_SUCCESS;
}
void bsc_nat_vty_init(void)
{
install_element(CONFIG_NODE, &cfg_bsc_nat_cmd);
install_node(&bsc_nat_node, config_write_bsc_nat);
}