Skip to content
Snippets Groups Projects
Commit 07274320 authored by Leon Dietrich's avatar Leon Dietrich Committed by Benny Baumann
Browse files

chg: Move local endpoint name retrieval to utility header

parent 49b026a0
No related branches found
No related tags found
No related merge requests found
......@@ -12,15 +12,6 @@ namespace rmrf::net {
[[nodiscard]] std::unique_ptr<udp_client> client_factory_construct_udp_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb = nullptr);
[[nodiscard]] std::unique_ptr<tcp_client> client_factory_construct_tcp_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb = nullptr);
/**
* This method queries the remote address of a connected client. Please note that only TCP sockets are supported
* at the moment. This operation will fail if an unsupported socket type is provided.
* @brief Get the address of the connected remote client
* @param socket The socket representing the client
* @return The remote socketaddr pair
*/
[[nodiscard]] socketaddr get_own_address_after_connect(const auto_fd& socket);
/**
* This method directly connects to the given address and returns an invalid pointer if it fails.
* @brief Connect to a remote destination
......
......@@ -11,4 +11,29 @@ namespace rmrf::net {
throw netio_exception("Failed to set socket mode. fcntl resulted in error:" + std::to_string(errno));
}
}
[[nodiscard]] socketaddr get_own_address_after_connect(const auto_fd& socket) {
socketaddr own_address;
socklen_t sa_local_len = sizeof(sockaddr_storage);
if (getsockname(socket.get(), own_address.ptr(), &sa_local_len) == 0) {
// Update length field after the internal structure was modified
// TODO: Maybe make this an internal method in socketaddr to update the size
own_address = own_address.ptr();
} else {
switch(errno) {
case EBADF:
case ENOTSOCK:
throw netio_exception("Invalid file descriptor provided to obtain own address. ERRNO: " + std::to_string(errno));
case EFAULT:
case EINVAL:
throw netio_exception("Invlid data structure for information retrival of own socket address provided. ERRNO: " + std::to_string(errno));
case ENOBUFS:
throw netio_exception("Kernel temporarily out of buffer space to store own address informatio.n ERRNO:." + std::to_string(errno));
default:
throw netio_exception("Unexpected error while requesting own socket address. ERRNO: " + std::to_string(errno));
}
}
return own_address;
}
}
#pragma once
#include "net/async_fd.hpp"
#include "net/socketaddress.hpp"
namespace rmrf::net {
/**
......@@ -8,4 +9,13 @@ namespace rmrf::net {
* @param socket The socket to modify
*/
void make_socket_nonblocking(auto_fd& socket);
/**
* This method queries the remote address of a connected client. Please note that only TCP sockets are supported
* at the moment. This operation will fail if an unsupported socket type is provided.
* @brief Get the address of the connected remote client
* @param socket The socket representing the client
* @return The remote socketaddr pair
*/
[[nodiscard]] socketaddr get_own_address_after_connect(const auto_fd& socket);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment