ctrl: add optional port to bind command

So far ctrl interface did not allow to specify port to bind to.
Let's fix this and make it consistent with the way vty bind works.

N. B: the functions which ignore port configured via vty are marked as deprecated,
the sw which uses them should be ported to either newly added ctrl_init_default()
or simplified ctrl_interface_setup()

The similar change for vty interface will be addressed via separate patch series.

Related: OS#5809
Change-Id: I0fd87fd41fd3ac975273968d24f477daa3cd3aa9
This commit is contained in:
Max 2022-12-03 17:59:19 +03:00
parent 7a79dd3dab
commit 3f79ce8f23
6 changed files with 37 additions and 7 deletions

View File

@ -36,19 +36,20 @@ struct ctrl_handle *ctrl_handle_alloc2(void *ctx, void *data,
unsigned int node_count);
struct ctrl_handle *ctrl_interface_setup(void *data, uint16_t port,
ctrl_cmd_lookup lookup);
struct ctrl_handle *ctrl_interface_setup2(void *data, uint16_t default_port, ctrl_cmd_lookup lookup,
unsigned int node_count);
struct ctrl_handle *ctrl_interface_setup_dynip(void *data,
const char *bind_addr,
uint16_t port,
ctrl_cmd_lookup lookup);
ctrl_cmd_lookup lookup) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
struct ctrl_handle *ctrl_interface_setup_dynip2(void *data,
const char *bind_addr,
uint16_t port,
ctrl_cmd_lookup lookup,
unsigned int node_count);
unsigned int node_count) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
struct ctrl_connection *osmo_ctrl_conn_alloc(void *ctx, void *data);
int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data);
struct ctrl_cmd *ctrl_cmd_exec_from_string(struct ctrl_handle *ch, const char *cmdstr);
int ctrl_lookup_register(ctrl_cmd_lookup lookup);
int ctrl_handle_msg(struct ctrl_handle *ctrl, struct ctrl_connection *ccon, struct msgb *msg);

View File

@ -2,6 +2,8 @@
#pragma once
#include <stdint.h>
/* Add the 'ctrl' section to VTY, containing the 'bind' command. */
int ctrl_vty_init(void *ctx);
@ -9,3 +11,6 @@ int ctrl_vty_init(void *ctx);
* This should be fed to ctrl_interface_setup() once the configuration has been
* read. */
const char *ctrl_vty_get_bind_addr(void);
/* Returns configured port passed to the 'line ctrl'/'bind' command or default_port. */
uint16_t ctrl_vty_get_bind_port(uint16_t default_port);

View File

@ -47,6 +47,7 @@
#include <osmocom/ctrl/control_cmd.h>
#include <osmocom/ctrl/control_if.h>
#include <osmocom/ctrl/control_vty.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/rate_ctr.h>
@ -878,7 +879,7 @@ static int verify_counter(struct ctrl_cmd *cmd, const char *value, void *data)
struct ctrl_handle *ctrl_interface_setup(void *data, uint16_t port,
ctrl_cmd_lookup lookup)
{
return ctrl_interface_setup_dynip(data, "127.0.0.1", port, lookup);
return ctrl_interface_setup2(data, port, lookup, 0);
}
static int ctrl_initialized = 0;
@ -1030,6 +1031,18 @@ struct ctrl_handle *ctrl_interface_setup_dynip(void *data,
return ctrl_interface_setup_dynip2(data, bind_addr, port, lookup, 0);
}
/*! Initializes CTRL interface using the configured bind addr/port.
* \param[in] data Pointer which will be made available to each set_..() get_..() verify_..() control command function
* \param[in] default_port TCP port number to bind to if not explicitly configured
* \param[in] lookup Lookup function pointer, can be NULL
* \param[in] node_count Number of CTRL nodes to allocate, 0 for default.
*/
struct ctrl_handle *ctrl_interface_setup2(void *data, uint16_t default_port, ctrl_cmd_lookup lookup,
unsigned int node_count)
{
return ctrl_interface_setup_dynip2(data, ctrl_vty_get_bind_addr(), ctrl_vty_get_bind_port(default_port), lookup,
node_count);
}
/*! Install a lookup helper function for control nodes
* This function is used by e.g. library code to install lookup helpers

View File

@ -26,16 +26,20 @@
static void *ctrl_vty_ctx = NULL;
static const char *ctrl_vty_bind_addr = NULL;
/* Port the CTRL should bind to: -1 means not configured */
static int ctrl_bind_port = -1;
DEFUN(cfg_ctrl_bind_addr,
cfg_ctrl_bind_addr_cmd,
"bind A.B.C.D",
"bind A.B.C.D [<0-65535>]",
"Set bind address to listen for Control connections\n"
"Local IP address (default 127.0.0.1)\n")
"Local IP address (default 127.0.0.1)\n"
"Local TCP port number\n")
{
talloc_free((char*)ctrl_vty_bind_addr);
ctrl_vty_bind_addr = NULL;
ctrl_vty_bind_addr = talloc_strdup(ctrl_vty_ctx, argv[0]);
ctrl_bind_port = argc > 1 ? atoi(argv[1]) : -1;
return CMD_SUCCESS;
}
@ -46,6 +50,11 @@ const char *ctrl_vty_get_bind_addr(void)
return ctrl_vty_bind_addr;
}
uint16_t ctrl_vty_get_bind_port(uint16_t default_port)
{
return ctrl_bind_port >= 0 ? ctrl_bind_port : default_port;
}
static struct cmd_node ctrl_node = {
L_CTRL_NODE,
"%s(config-ctrl)# ",

View File

@ -22,12 +22,14 @@ ctrl_handle_alloc; /* could be removed? */
ctrl_handle_alloc2; /* could be removed? */
ctrl_handle_msg; /* only used in unit test */
ctrl_interface_setup;
ctrl_interface_setup2;
ctrl_interface_setup_dynip;
ctrl_interface_setup_dynip2;
ctrl_lookup_register;
ctrl_parse_get_num;
ctrl_type_vals;
ctrl_vty_get_bind_addr;
ctrl_vty_get_bind_port;
ctrl_vty_init;
osmo_ctrl_conn_alloc;

View File

@ -280,7 +280,7 @@ int main (int argc, char *argv[])
}
if (ctrl_port > 0) {
ctrl = ctrl_interface_setup_dynip(NULL, ctrl_vty_get_bind_addr(), ctrl_port, NULL);
ctrl = ctrl_interface_setup(NULL, ctrl_port, NULL);
if (!ctrl) {
fprintf(stderr, "Failed to initialize control interface. Exiting.\n");
exit(1);