Rename C++ header files to .hpp
[lttng-tools.git] / src / common / sessiond-comm / sessiond-comm.cpp
index 6a011b8b4cea9dfbb02661bb8bccb0668999bd95..7b20007a8ea2ef0068a57cdd6c348043ad0a907c 100644 (file)
@@ -1,11 +1,12 @@
 /*
- * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
+ * Copyright (C) 2011 EfficiOS Inc.
  * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * SPDX-License-Identifier: GPL-2.0-only
  *
  */
 
+#include <sys/socket.h>
 #define _LGPL_SOURCE
 #include <limits.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <inttypes.h>
 
-#include <common/common.h>
-#include <common/compat/errno.h>
+#include <common/common.hpp>
+#include <common/compat/errno.hpp>
 
-#include "sessiond-comm.h"
+#include "sessiond-comm.hpp"
 
 /* For Unix socket */
-#include <common/unix.h>
+#include <common/unix.hpp>
 /* For Inet socket */
-#include "inet.h"
+#include "inet.hpp"
 /* For Inet6 socket */
-#include "inet6.h"
+#include "inet6.hpp"
 
 #define NETWORK_TIMEOUT_ENV    "LTTNG_NETWORK_SOCKET_TIMEOUT"
 
@@ -511,3 +512,72 @@ unsigned long lttcomm_get_network_timeout(void)
 {
        return network_timeout;
 }
+
+/*
+ * Only valid for an ipv4 and ipv6 bound socket that is already connected to its
+ * peer.
+ */
+int lttcomm_populate_sock_from_open_socket(
+               struct lttcomm_sock *sock,
+               int fd,
+               enum lttcomm_sock_proto protocol)
+{
+       int ret = 0;
+       socklen_t storage_len;
+       struct sockaddr_storage storage = {};
+
+       assert(sock);
+       assert(fd >= 0);
+
+       sock->proto = protocol;
+
+       storage_len = sizeof(storage);
+       ret = getpeername(fd, (struct sockaddr *) &storage,
+                       &storage_len);
+       if (ret) {
+               ERR("Failed to get peer info for socket %d (errno: %d)", fd,
+                               errno);
+               ret = -1;
+               goto end;
+       }
+
+       if (storage_len > sizeof(storage)) {
+               ERR("Failed to get peer info for socket %d: storage size is too small", fd);
+               ret = -1;
+               goto end;
+       }
+
+       switch (storage.ss_family) {
+       case AF_INET:
+               sock->sockaddr.type = LTTCOMM_INET;
+               memcpy(&sock->sockaddr.addr, &storage,
+                               sizeof(struct sockaddr_in));
+               break;
+       case AF_INET6:
+               sock->sockaddr.type = LTTCOMM_INET6;
+               memcpy(&sock->sockaddr.addr, &storage,
+                               sizeof(struct sockaddr_in6));
+               break;
+       default:
+               abort();
+               break;
+       }
+
+       /* Create a valid socket object with a temporary fd. */
+       ret = lttcomm_create_sock(sock);
+       if (ret < 0) {
+               ERR("Failed to create temporary socket object");
+               ret = -1;
+               goto end;
+       }
+
+       /* Substitute the fd. */
+       if (sock->ops->close(sock)) {
+               ret = -1;
+               goto end;
+       }
+       sock->fd = fd;
+
+end:
+       return ret;
+}
This page took 0.025372 seconds and 4 git commands to generate.