[MERGED] libosmocore[master]: Expand and expose ctrl connection allocation

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
Thu Feb 23 17:30:44 UTC 2017


Harald Welte has submitted this change and it was merged.

Change subject: Expand and expose ctrl connection allocation
......................................................................


Expand and expose ctrl connection allocation

Add function for allocating CTRL connection to public headers and
replace call to previous static function with it. Add doxygen docs for
this function.

It's useful if we need to allocate ctrl connection but don't need to
bind to any interfaces: when we act as ctrl client.

Related: OS#1615
Change-Id: I522ed809cbebfd3d7dd08b4ed9137b39ff192e32
---
M include/osmocom/ctrl/control_if.h
M src/ctrl/control_if.c
2 files changed, 52 insertions(+), 6 deletions(-)

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



diff --git a/include/osmocom/ctrl/control_if.h b/include/osmocom/ctrl/control_if.h
index 512ae10..f2af1db 100644
--- a/include/osmocom/ctrl/control_if.h
+++ b/include/osmocom/ctrl/control_if.h
@@ -27,5 +27,5 @@
 					       const char *bind_addr,
 					       uint16_t port,
 					       ctrl_cmd_lookup lookup);
-
+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);
diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c
index 254f9bc..cf79670 100644
--- a/src/ctrl/control_if.c
+++ b/src/ctrl/control_if.c
@@ -368,7 +368,13 @@
 	return rc;
 }
 
-static struct ctrl_connection *ctrl_connection_alloc(void *ctx)
+/*! \brief Allocate CTRL connection
+ *  \param[in] ctx Context from which talloc should allocate it
+ *  \param[in] data caller's private data parameter which should assigned to
+               write queue's file descriptor data parameter.
+ *  \return Allocated CTRL connection structure or NULL in case of errors
+ */
+struct ctrl_connection *osmo_ctrl_conn_alloc(void *ctx, void *data)
 {
 	struct ctrl_connection *ccon = talloc_zero(ctx, struct ctrl_connection);
 	if (!ccon)
@@ -379,6 +385,9 @@
 
 	INIT_LLIST_HEAD(&ccon->cmds);
 	INIT_LLIST_HEAD(&ccon->def_cmds);
+
+	ccon->write_queue.bfd.data = data;
+	ccon->write_queue.write_cb = control_write_cb;
 
 	return ccon;
 }
@@ -412,19 +421,17 @@
 		return ret;
 	}
 #endif
-	ccon = ctrl_connection_alloc(listen_bfd->data);
+	ctrl = listen_bfd->data;
+	ccon = osmo_ctrl_conn_alloc(listen_bfd->data, ctrl);
 	if (!ccon) {
 		LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
 		close(fd);
 		return -1;
 	}
 
-	ctrl = listen_bfd->data;
-	ccon->write_queue.bfd.data = ctrl;
 	ccon->write_queue.bfd.fd = fd;
 	ccon->write_queue.bfd.when = BSC_FD_READ;
 	ccon->write_queue.read_cb = handle_control_read;
-	ccon->write_queue.write_cb = control_write_cb;
 
 	ret = osmo_fd_register(&ccon->write_queue.bfd);
 	if (ret < 0) {
@@ -648,6 +655,45 @@
 	return 0;
 }
 
+/*! \brief Setup CTRL interface connection to a given address
+ *  \param[in] data Pointer which will be made available to each
+               set_..() get_..() verify_..() control command function
+ *  \param[in] addr Address to which we shall connect
+ *  \param[in] port Port to which we shall connect
+ *  \param[in] lookup Lookup function pointer, can be NULL
+ *  \returns ctrl_handle pointer or NULL in case of errors
+ */
+struct ctrl_handle *ctrl_interface_connect(void *data, const char *addr,
+					   uint16_t port,
+					   ctrl_cmd_lookup lookup)
+{
+	int ret;
+	struct ctrl_handle *ctrl;
+
+	ctrl = talloc_zero(data, struct ctrl_handle);
+	if (!ctrl)
+		return NULL;
+
+	INIT_LLIST_HEAD(&ctrl->ccon_list);
+
+	ctrl->data = data;
+	ctrl->lookup = lookup;
+
+	ctrl->listen_fd.cb = NULL;
+	ctrl->listen_fd.data = ctrl;
+	ret = osmo_sock_init_ifd(&ctrl->listen_fd, AF_INET, SOCK_STREAM,
+				 IPPROTO_TCP, addr, port, OSMO_SOCK_F_CONNECT);
+	if (ret < 0) {
+		LOGP(DLCTRL, LOGL_ERROR, "Cannot connect to CTRL at %s:%u\n",
+		     addr, port);
+		talloc_free(ctrl);
+		return NULL;
+	}
+	LOGP(DLCTRL, LOGL_NOTICE, "CTRL connected to %s:%u\n", addr, port);
+
+	return ctrl;
+}
+
 struct ctrl_handle *ctrl_interface_setup(void *data, uint16_t port,
 					 ctrl_cmd_lookup lookup)
 {

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I522ed809cbebfd3d7dd08b4ed9137b39ff192e32
Gerrit-PatchSet: 6
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Holger Freyther <holger at freyther.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Max <msuraev at sysmocom.de>



More information about the gerrit-log mailing list