From 1183e62088a2f89e01fb6525b70f383f6714602f Mon Sep 17 00:00:00 2001 From: Benny Baumann <BenBE@geshi.org> Date: Sun, 19 Feb 2023 23:06:37 +0100 Subject: [PATCH] add: TCP server socket binding --- src/net/tcp_server_socket.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/net/tcp_server_socket.cpp b/src/net/tcp_server_socket.cpp index da2ec54..ab1399c 100644 --- a/src/net/tcp_server_socket.cpp +++ b/src/net/tcp_server_socket.cpp @@ -45,16 +45,23 @@ tcp_server_socket::tcp_server_socket( throw netio_exception("Failed to create socket fd."); } - if (bind(socket_fd.get(), socket_identifier.ptr(), socket_identifier.size()) != 0) { - std::string msg = "Failed to bind to all addresses (FIXME)"; + 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); + + if (socket_identifier.family() == AF_INET6) { + sockaddr_in* inptr = (sockaddr_in*) socket_identifier.ptr(); + const auto port = ntohs(inptr->sin_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); - if (socket_identifier.family() == AF_INET6 || socket_identifier.family() == AF_INET) { - // TODO find a nice way to check for the port - /* if (port < 1024) { msg += "\nYou tried to bind to a port smaller than 1024. Are you root?"; } - */ } throw netio_exception(msg); -- GitLab