+
+/*
+ * Return allocated lttcomm socket structure from lttng URI.
+ */
+struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri)
+{
+ int ret;
+ int _sock_proto;
+ struct lttcomm_sock *sock = NULL;
+
+ /* Safety net */
+ assert(uri);
+
+ /* Check URI protocol */
+ if (uri->proto == LTTNG_TCP) {
+ _sock_proto = LTTCOMM_SOCK_TCP;
+ } else {
+ ERR("Relayd invalid URI proto: %d", uri->proto);
+ goto alloc_error;
+ }
+
+ sock = lttcomm_alloc_sock(_sock_proto);
+ if (sock == NULL) {
+ goto alloc_error;
+ }
+
+ /* Check destination type */
+ if (uri->dtype == LTTNG_DST_IPV4) {
+ ret = lttcomm_init_inet_sockaddr(&sock->sockaddr, uri->dst.ipv4,
+ uri->port);
+ if (ret < 0) {
+ goto error;
+ }
+ } else if (uri->dtype == LTTNG_DST_IPV6) {
+ ret = lttcomm_init_inet6_sockaddr(&sock->sockaddr, uri->dst.ipv6,
+ uri->port);
+ if (ret < 0) {
+ goto error;
+ }
+ } else {
+ /* Command URI is invalid */
+ ERR("Relayd invalid URI dst type: %d", uri->dtype);
+ goto error;
+ }
+
+ return sock;
+
+error:
+ lttcomm_destroy_sock(sock);
+alloc_error:
+ return NULL;
+}
+
+/*
+ * Destroy and free lttcomm socket.
+ */
+void lttcomm_destroy_sock(struct lttcomm_sock *sock)
+{
+ if (sock != NULL) {
+ free(sock);
+ }
+}