From 30ecb0a9f82eccba8e661c2d1e422efd7bfa11e4 Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Oct 07 2021 17:42:20 +0000 Subject: addres resolve --- diff --git a/main.cpp b/main.cpp index cba5585..abb01e1 100644 --- a/main.cpp +++ b/main.cpp @@ -6,17 +6,18 @@ int main(int argc, char** argv) { - if (argc != 2) - { printf("one arg expected\n"); return 1; } + if (argc != 3) + { printf("two args expected\n"); return 1; } - Tcp::Address address(127, 0, 0, 1, 1562); + Tcp::Address address; + address.resolve(argv[2]); if (0 == strcmp(argv[1], "tcpconnect")) { printf("tcpconnect\n"); Tcp::Connection connection; if (connection.open(address)) - printf( "connected to server: %hhd.%hhd.%hhd.%hhd:%hd\n", + printf( "connected to server: %hhu.%hhu.%hhu.%hhu:%hu\n", address.ip[0], address.ip[1], address.ip[2], address.ip[3], address.port ); char buf[65] = {}; while(connection.check()) { @@ -38,7 +39,7 @@ int main(int argc, char** argv) { public: void handleTcpConnection(Tcp::Connection &connection) override { const Tcp::Address &address = connection.getRemoteAddr(); - printf( "received connection from: %hhd.%hhd.%hhd.%hhd:%hd\n", + printf( "received connection from: %hhu.%hhu.%hhu.%hhu:%hu\n", address.ip[0], address.ip[1], address.ip[2], address.ip[3], address.port ); while(connection.check()) { char buf[65] = {}; @@ -53,7 +54,7 @@ int main(int argc, char** argv) { Tcp::Server server; if (server.start(address, &handler)) - printf( "listening at: %hhd.%hhd.%hhd.%hhd:%hd\n", + printf( "listening at: %hhu.%hhu.%hhu.%hhu:%hu\n", address.ip[0], address.ip[1], address.ip[2], address.ip[3], address.port ); server.join(); diff --git a/tcp.cpp b/tcp.cpp index 40a703d..3653bd6 100644 --- a/tcp.cpp +++ b/tcp.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -11,6 +12,7 @@ #include #include #include +#include #include "tcp.h" @@ -21,7 +23,60 @@ static const int pollTimeoutMs = 100; using namespace Tcp; -//ErrorCode Address::resolve(const char *addrString); +ErrorCode Address::resolve(const char *addrString) { + *this = Address(); + + if (!addrString) + return ERR_TCP_BAD_ADDRESS; + + ErrorCode errorCode = ERR_NONE; + + const char *colon = addrString; + while(*colon && *colon != ':') ++colon; + + char *hostBuf = nullptr; + const char *host = nullptr; + const char *port = nullptr; + if (*colon) { + hostBuf = new char[colon - addrString + 1]; + memcpy(hostBuf, addrString, colon - addrString); + hostBuf[colon - addrString] = 0; + host = hostBuf; + port = colon + 1; + } else { + bool allDigits = true; + for(const char *c = addrString; *c && allDigits; ++c) + if (*c < '0' || *c > '9') allDigits = false; + (allDigits ? port : host) = addrString; + } + + if (host) { + if (4 != sscanf(host, "%hhu.%hhu.%hhu.%hhu", &ip[0], &ip[1], &ip[2], &ip[3])) { + memset(ip, 0, sizeof(ip)); + hostent *he; + in_addr **addr_list; + if ( (he = gethostbyname(host)) + && (addr_list = (struct in_addr**)he->h_addr_list) + && (*addr_list) ) + { + memcpy(ip, *addr_list, sizeof(ip)); + } else { + errorCode = ERR_TCP_BAD_ADDRESS; + } + } + } + + if (port) { + if (1 != sscanf(port, "%hu", &this->port)) + errorCode = ERR_TCP_BAD_ADDRESS; + } + + if (!host && !port) + errorCode = ERR_TCP_BAD_ADDRESS; + + if (hostBuf) delete[] hostBuf; + return errorCode; +} Connection::Connection(): @@ -203,7 +258,7 @@ bool Server::start(const Address &localAddr, Handler *handler) { this->localAddr = localAddr; this->handler = handler; - if (!this->localAddr.isValid()) + if (!this->localAddr.isValidPort()) { status = STATUS_LOST; lastError = ERR_TCP_BAD_ADDRESS; return false; } if (!this->handler) { status = STATUS_LOST; lastError = ERR_TCP_BAD_HANDLER; return false; }