Change in libosmocore[master]: vty: Make TCP port configurable and introduce telnet_init_default

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Holger Freyther gerrit-no-reply at lists.osmocom.org
Sun Dec 23 04:20:08 UTC 2018


Holger Freyther has submitted this change and it was merged. ( https://gerrit.osmocom.org/12321 )

Change subject: vty: Make TCP port configurable and introduce telnet_init_default
......................................................................

vty: Make TCP port configurable and introduce telnet_init_default

Extend the vty_bind_cmd VTY command to allow to optionally specify
a port in addition to the IPv4 address.

Introduce telnet_init_default to relieve client code from having
to query the bind IPv4 address (and now the TCP port). Instead a
client only needs to pass the default TCP port to use.

Client code should use it like:

	int rc = telnet_init_default(ctx, priv, OSMO_VTY_PORT_SGSN);

Change-Id: Id5fb2faaf4311bd7284ee870526a6f87b7e260f3
---
M include/osmocom/vty/telnet_interface.h
M include/osmocom/vty/vty.h
M src/vty/telnet_interface.c
M src/vty/vty.c
4 files changed, 34 insertions(+), 3 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/include/osmocom/vty/telnet_interface.h b/include/osmocom/vty/telnet_interface.h
index d653466..da7cf83 100644
--- a/include/osmocom/vty/telnet_interface.h
+++ b/include/osmocom/vty/telnet_interface.h
@@ -47,6 +47,7 @@
 
 int telnet_init(void *tall_ctx, void *priv, int port);
 int telnet_init_dynif(void *tall_ctx, void *priv, const char *ip, int port);
+int telnet_init_default(void *tall_ctx, void *priv, int default_port);
 
 void telnet_exit(void);
 
diff --git a/include/osmocom/vty/vty.h b/include/osmocom/vty/vty.h
index 02ba03e..c4cf707 100644
--- a/include/osmocom/vty/vty.h
+++ b/include/osmocom/vty/vty.h
@@ -210,6 +210,8 @@
 
 /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
 const char *vty_get_bind_addr(void);
+/** Returns configured port passed to the 'line vty'/'bind' command or default_port. */
+int vty_get_bind_port(int default_port);
 
 extern void *tall_vty_ctx;
 
diff --git a/src/vty/telnet_interface.c b/src/vty/telnet_interface.c
index 47c45fc..e090e17 100644
--- a/src/vty/telnet_interface.c
+++ b/src/vty/telnet_interface.c
@@ -105,6 +105,18 @@
 	return 0;
 }
 
+/*! Initializes telnet based VTY interface using the configured bind addr/port.
+ *  \param[in] tall_ctx \ref talloc context
+ *  \param[in] priv private data to be passed to callback
+ *  \param[in] default_port TCP port number to bind to if not explicitely configured
+ */
+int telnet_init_default(void *tall_ctx, void *priv, int default_port)
+{
+	return telnet_init_dynif(tall_ctx, priv, vty_get_bind_addr(),
+				 vty_get_bind_port(default_port));
+}
+
+
 extern struct host host;
 
 /*! close a telnet connection */
diff --git a/src/vty/vty.c b/src/vty/vty.c
index 70f6811..7f6c225 100644
--- a/src/vty/vty.c
+++ b/src/vty/vty.c
@@ -90,6 +90,8 @@
  * use NULL and VTY_BIND_ADDR_DEFAULT instead. */
 static const char *vty_bind_addr = NULL;
 #define VTY_BIND_ADDR_DEFAULT "127.0.0.1"
+/* Port the VTY should bind to. -1 means not configured */
+static int vty_bind_port = -1;
 
 /* Configure lock. */
 static int vty_config;
@@ -1612,12 +1614,13 @@
 }
 
 /* vty bind */
-DEFUN(vty_bind, vty_bind_cmd, "bind A.B.C.D",
+DEFUN(vty_bind, vty_bind_cmd, "bind A.B.C.D [<0-65535>]",
       "Accept VTY telnet connections on local interface\n"
       "Local interface IP address (default: " VTY_BIND_ADDR_DEFAULT ")\n")
 {
 	talloc_free((void*)vty_bind_addr);
 	vty_bind_addr = talloc_strdup(tall_vty_ctx, argv[0]);
+	vty_bind_port = argc > 1 ? atoi(argv[1]) : -1;
 	return CMD_SUCCESS;
 }
 
@@ -1628,6 +1631,13 @@
 	return vty_bind_addr;
 }
 
+int vty_get_bind_port(int default_port)
+{
+	if (vty_bind_port >= 0)
+		return vty_bind_port;
+	return default_port;
+}
+
 DEFUN(service_advanced_vty,
       service_advanced_vty_cmd,
       "service advanced-vty",
@@ -1700,8 +1710,14 @@
 		vty_out(vty, " login%s", VTY_NEWLINE);
 
 	/* bind */
-	if (vty_bind_addr && (strcmp(vty_bind_addr, VTY_BIND_ADDR_DEFAULT) != 0))
-		vty_out(vty, " bind %s%s", vty_bind_addr, VTY_NEWLINE);
+	if (vty_bind_addr && (strcmp(vty_bind_addr, VTY_BIND_ADDR_DEFAULT) != 0 || vty_bind_port >= 0)) {
+		if (vty_bind_port >= 0) {
+			vty_out(vty, " bind %s %d%s", vty_bind_addr,
+				vty_bind_port, VTY_NEWLINE);
+		} else {
+			vty_out(vty, " bind %s%s", vty_bind_addr, VTY_NEWLINE);
+		}
+	}
 
 	vty_out(vty, "!%s", VTY_NEWLINE);
 

-- 
To view, visit https://gerrit.osmocom.org/12321
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Id5fb2faaf4311bd7284ee870526a6f87b7e260f3
Gerrit-Change-Number: 12321
Gerrit-PatchSet: 4
Gerrit-Owner: Holger Freyther <holger at freyther.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Holger Freyther <holger at freyther.de>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Stefan Sperling <stsp at stsp.name>
Gerrit-Reviewer: Vadim Yanitskiy <axilirator at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181223/c44dcf9e/attachment.htm>


More information about the gerrit-log mailing list