Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • ReadMailReallyFast/code
1 result
Show changes
Commits on Source (3)
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "net/udp_client.hpp" #include "net/udp_client.hpp"
namespace rmrf::net { namespace rmrf::net {
[[nodiscard]] socketaddr get_own_address_after_connect(const auto_fd& socket) { [[nodiscard]] socketaddr get_own_address_after_connect(const auto_fd& socket) {
socketaddr own_address; socketaddr own_address;
socklen_t sa_local_len = sizeof(sockaddr_storage); socklen_t sa_local_len = sizeof(sockaddr_storage);
...@@ -61,15 +61,15 @@ namespace rmrf::net { ...@@ -61,15 +61,15 @@ namespace rmrf::net {
} }
return c; return c;
} }
[[nodiscard]] std::unique_ptr<tcp_client> client_factory_construct_tcp_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb) { [[nodiscard]] std::unique_ptr<tcp_client> client_factory_construct_tcp_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb) {
auto_fd socket_candidate{socket(socket_identifier.family(), SOCK_STREAM, 0)}; auto_fd socket_candidate{socket(socket_identifier.family(), SOCK_STREAM, 0)};
if (socket_candidate.valid()) { if (socket_candidate.valid()) {
if (connect(socket_candidate.get(), socket_identifier.ptr(), socket_identifier.size()) == 0) { if (connect(socket_candidate.get(), socket_identifier.ptr(), socket_identifier.size()) == 0) {
make_socket_nonblocking(socket_candidate); make_socket_nonblocking(socket_candidate);
const auto own_address = get_own_address_after_connect(socket_candidate); const auto own_address = get_own_address_after_connect(socket_candidate);
// TODO create client object using socket_candidate, own_address and socket_identifier as remote address // TODO create client object using socket_candidate, own_address and socket_identifier as remote address
auto c = std::make_unique<tcp_client>(nullptr, std::move(socket_candidate), own_address, socket_identifier); auto c = std::make_unique<tcp_client>(nullptr, std::move(socket_candidate), own_address, socket_identifier);
if(cb) { if(cb) {
...@@ -80,7 +80,7 @@ namespace rmrf::net { ...@@ -80,7 +80,7 @@ namespace rmrf::net {
} }
return nullptr; return nullptr;
} }
[[nodiscard]] std::unique_ptr<connection_client> connect(const socketaddr& address, const socket_t& type) { [[nodiscard]] std::unique_ptr<connection_client> connect(const socketaddr& address, const socket_t& type) {
switch(type) { switch(type) {
case socket_t::TCP: case socket_t::TCP:
...@@ -94,7 +94,7 @@ namespace rmrf::net { ...@@ -94,7 +94,7 @@ namespace rmrf::net {
return nullptr; return nullptr;
} }
} }
socket_t guess_socket_type_from_address(const socketaddr& address) { socket_t guess_socket_type_from_address(const socketaddr& address) {
switch(address.family()) { switch(address.family()) {
case AF_INET: case AF_INET:
...@@ -117,11 +117,11 @@ namespace rmrf::net { ...@@ -117,11 +117,11 @@ namespace rmrf::net {
[[nodiscard]] std::unique_ptr<connection_client> connect(const std::string& peer_address, const std::string& service, int ip_addr_family) { [[nodiscard]] std::unique_ptr<connection_client> connect(const std::string& peer_address, const std::string& service, int ip_addr_family) {
const auto candidates = get_socketaddr_list(peer_address, service); const auto candidates = get_socketaddr_list(peer_address, service);
if (candidates.empty()) { if (candidates.empty()) {
throw netio_exception("Unable to find suitable connection candidate."); throw netio_exception("Unable to find suitable connection candidate.");
} }
if (ip_addr_family == AF_UNSPEC) { if (ip_addr_family == AF_UNSPEC) {
ip_addr_family = candidates.front().family(); ip_addr_family = candidates.front().family();
} }
...@@ -129,20 +129,21 @@ namespace rmrf::net { ...@@ -129,20 +129,21 @@ namespace rmrf::net {
if (!(ip_addr_family == AF_INET || ip_addr_family == AF_INET6 || ip_addr_family == AF_UNIX)) { if (!(ip_addr_family == AF_INET || ip_addr_family == AF_INET6 || ip_addr_family == AF_UNIX)) {
throw netio_exception("Invalid IP address family."); throw netio_exception("Invalid IP address family.");
} }
std::unique_ptr<connection_client> latest_client; std::unique_ptr<connection_client> latest_client;
for(const auto& current_connection_candidate : candidates) { for(const auto& current_connection_candidate : candidates) {
if(latest_client = connect(current_connection_candidate, guess_socket_type_from_address(current_connection_candidate)); if(latest_client = connect(current_connection_candidate, guess_socket_type_from_address(current_connection_candidate));
latest_client) { latest_client) {
break; break;
} }
} }
if (!latest_client) { if (!latest_client) {
throw netio_exception("Unable to find suitable connection candidate."); throw netio_exception("Unable to find suitable connection candidate.");
} }
return latest_client; return latest_client;
} }
}
}
...@@ -30,46 +30,46 @@ ...@@ -30,46 +30,46 @@
namespace rmrf::net { namespace rmrf::net {
auto_fd create_tcp_server_socket(const socketaddr& socket_identifier) { static auto_fd create_tcp_server_socket(const socketaddr& socket_identifier) {
auto_fd socket_fd{socket(socket_identifier.family(), SOCK_STREAM, 0)}; auto_fd socket_fd{socket(socket_identifier.family(), SOCK_STREAM, 0)};
if (!socket_fd.valid()) { if (!socket_fd.valid()) {
// TODO implement propper error handling // TODO implement propper error handling
throw netio_exception("Failed to create socket fd."); throw netio_exception("Failed to create socket fd.");
} }
if (auto error = bind(socket_fd.get(), socket_identifier.ptr(), socket_identifier.size()); error != 0) { if (auto error = bind(socket_fd.get(), socket_identifier.ptr(), socket_identifier.size()); error != 0) {
std::string msg = "Failed to bind to all addresses (FIXME). Errorcode: " + std::to_string(error); std::string msg = "Failed to bind to all addresses (FIXME). Errorcode: " + std::to_string(error);
if (socket_identifier.family() == AF_INET6) { if (socket_identifier.family() == AF_INET6) {
sockaddr_in* inptr = (sockaddr_in*) socket_identifier.ptr(); sockaddr_in* inptr = (sockaddr_in*) socket_identifier.ptr();
const auto port = ntohs(inptr->sin_port); const auto port = ntohs(inptr->sin_port);
if (port < 1024) { if (port < 1024) {
msg += "\nYou tried to bind to a port smaller than 1024. Are you root?"; msg += "\nYou tried to bind to a port smaller than 1024. Are you root?";
}
} else if (socket_identifier.family() == AF_INET) {
sockaddr_in6* inptr = (sockaddr_in6*) socket_identifier.ptr();
const auto port = ntohs(inptr->sin6_port);
if (port < 1024) {
msg += "\nYou tried to bind to a port smaller than 1024. Are you root?";
}
} }
} else if (socket_identifier.family() == AF_INET) {
sockaddr_in6* inptr = (sockaddr_in6*) socket_identifier.ptr();
const auto port = ntohs(inptr->sin6_port);
throw netio_exception(msg); if (port < 1024) {
} msg += "\nYou tried to bind to a port smaller than 1024. Are you root?";
}
make_socket_nonblocking(socket_fd);
if (listen(socket_fd.get(), 5) == -1) {
throw netio_exception("Failed to enable listening mode for raw socket");
} }
return socket_fd; throw netio_exception(msg);
}
make_socket_nonblocking(socket_fd);
if (listen(socket_fd.get(), 128) == -1) {
throw netio_exception("Failed to enable listening mode for raw socket");
} }
return socket_fd;
}
tcp_server_socket::tcp_server_socket( tcp_server_socket::tcp_server_socket(
const socketaddr& socket_identifier, const socketaddr& socket_identifier,
async_server_socket::accept_handler_type client_listener async_server_socket::accept_handler_type client_listener
...@@ -111,7 +111,7 @@ std::shared_ptr<connection_client> tcp_server_socket::await_raw_socket_incomming ...@@ -111,7 +111,7 @@ std::shared_ptr<connection_client> tcp_server_socket::await_raw_socket_incomming
if (client_fd_raw < 0) { if (client_fd_raw < 0) {
throw netio_exception("Unable to bind incoming client"); throw netio_exception("Unable to bind incoming client");
} }
auto client_socket = auto_fd(client_fd_raw); auto client_socket = auto_fd(client_fd_raw);
make_socket_nonblocking(client_socket); make_socket_nonblocking(client_socket);
...@@ -122,7 +122,7 @@ std::shared_ptr<connection_client> tcp_server_socket::await_raw_socket_incomming ...@@ -122,7 +122,7 @@ std::shared_ptr<connection_client> tcp_server_socket::await_raw_socket_incomming
setsockopt(client_socket.get(), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); setsockopt(client_socket.get(), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
#endif #endif
} }
return std::make_shared<tcp_client>( return std::make_shared<tcp_client>(
this->get_locked_destructor_callback(), this->get_locked_destructor_callback(),
std::move(client_socket), std::move(client_socket),
......