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
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)
@@ -380,6 +386,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)
 {