[PATCH] libosmocore[master]: control_if: Add API to initialize control interface without ...

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/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Sun Apr 16 17:23:21 UTC 2017


Review at  https://gerrit.osmocom.org/2375

control_if: Add API to initialize control interface without TCP port bind

When executing test cases, we don't want to bind to a local TCP port, as
we cannot make assumptions as to which ports are actually free.

Change-Id: I5717f9dd92d1f143f069cecd4b4c8ba3d03b25f8
---
M include/osmocom/ctrl/control_if.h
M src/ctrl/control_if.c
2 files changed, 62 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/75/2375/1

diff --git a/include/osmocom/ctrl/control_if.h b/include/osmocom/ctrl/control_if.h
index a740a96..0d37959 100644
--- a/include/osmocom/ctrl/control_if.h
+++ b/include/osmocom/ctrl/control_if.h
@@ -21,6 +21,7 @@
 
 int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd);
 int ctrl_cmd_send_trap(struct ctrl_handle *ctrl, const char *name, char *value);
+struct ctrl_handle *ctrl_handle_alloc(void *ctx, void *data, ctrl_cmd_lookup lookup);
 struct ctrl_handle *ctrl_interface_setup(void *data, uint16_t port,
 					 ctrl_cmd_lookup lookup);
 struct ctrl_handle *ctrl_interface_setup_dynip(void *data,
diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c
index 5730850..82a78d8 100644
--- a/src/ctrl/control_if.c
+++ b/src/ctrl/control_if.c
@@ -694,6 +694,66 @@
 	return ctrl_interface_setup_dynip(data, "127.0.0.1", port, lookup);
 }
 
+static int ctrl_initialized = 0;
+
+/* global ctrl initialization */
+static int ctrl_init(void)
+{
+	int ret;
+
+	if (ctrl_initialized)
+		return 0;
+
+	ctrl_node_vec = vector_init(5);
+	if (!ctrl_node_vec)
+		goto err;
+
+	ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_rate_ctr);
+	if (ret)
+		goto err_vec;
+	ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_counter);
+	if (ret)
+		goto err_vec;
+
+	ret = osmo_fsm_ctrl_cmds_install();
+	if (ret)
+		goto err_vec;
+
+	ctrl_initialized = 1;
+	return 0;
+
+err_vec:
+	vector_free(ctrl_node_vec);
+	ctrl_node_vec = NULL;
+err:
+	return -1;
+}
+
+/*! \brief Allocate a CTRL interface handle
+ *  \param[in] ctx Tallo callocation context to be used
+ *  \param[in] data Pointer which will be made available to each
+               set_..() get_..() verify_..() control command function
+ *  \param[in] lookup Lookup function pointer, can be NULL
+ *  \returns ctrl_handle pointer or NULL in case of errors
+ */
+struct ctrl_handle *ctrl_handle_alloc(void *ctx, void *data, ctrl_cmd_lookup lookup)
+{
+	struct ctrl_handle *ctrl;
+
+	ctrl_init();
+
+	ctrl = talloc_zero(ctx, struct ctrl_handle);
+	if (!ctrl)
+		return NULL;
+
+	INIT_LLIST_HEAD(&ctrl->ccon_list);
+
+	ctrl->data = data;
+	ctrl->lookup = lookup;
+
+	return ctrl;
+}
+
 /*! \brief Setup CTRL interface on a given address
  *  \param[in] data Pointer which will be made available to each
                set_..() get_..() verify_..() control command function
@@ -710,18 +770,9 @@
 	int ret;
 	struct ctrl_handle *ctrl;
 
-	ctrl = talloc_zero(data, struct ctrl_handle);
+	ctrl = ctrl_handle_alloc(data, data, lookup);
 	if (!ctrl)
 		return NULL;
-
-	INIT_LLIST_HEAD(&ctrl->ccon_list);
-
-	ctrl->data = data;
-	ctrl->lookup = lookup;
-
-	ctrl_node_vec = vector_init(5);
-	if (!ctrl_node_vec)
-		goto err;
 
 	/* Listen for control connections */
 	ctrl->listen_fd.cb = listen_fd_cb;
@@ -740,14 +791,6 @@
 
 	LOGP(DLCTRL, LOGL_NOTICE, "CTRL at %s %u\n", bind_addr, port);
 	return ctrl;
-err_vec:
-	vector_free(ctrl_node_vec);
-	ctrl_node_vec = NULL;
-err:
-	LOGP(DLCTRL, LOGL_ERROR, "Cannot bind CTRL at %s %u\n",
-	     bind_addr, port);
-	talloc_free(ctrl);
-	return NULL;
 }
 
 /*! \brief Install a lookup helper function for control nodes

-- 
To view, visit https://gerrit.osmocom.org/2375
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5717f9dd92d1f143f069cecd4b4c8ba3d03b25f8
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>



More information about the gerrit-log mailing list