|  | 
|  | 1 | +#include "Athena/IPAddress.hpp" | 
|  | 2 | + | 
|  | 3 | +#ifdef __unix__ | 
|  | 4 | +#include <sys/types.h> | 
|  | 5 | +#include <sys/socket.h> | 
|  | 6 | +#include <netinet/in.h> | 
|  | 7 | +#include <netinet/tcp.h> | 
|  | 8 | +#include <arpa/inet.h> | 
|  | 9 | +#include <netdb.h> | 
|  | 10 | +#include <unistd.h> | 
|  | 11 | +#elif defined(_WIN32) | 
|  | 12 | +#ifdef _WIN32_WINDOWS | 
|  | 13 | +#undef _WIN32_WINDOWS | 
|  | 14 | +#endif | 
|  | 15 | +#ifdef _WIN32_WINNT | 
|  | 16 | +#undef _WIN32_WINNT | 
|  | 17 | +#endif | 
|  | 18 | +#define _WIN32_WINDOWS 0x0501 | 
|  | 19 | +#define _WIN32_WINNT   0x0501 | 
|  | 20 | +#include <winsock2.h> | 
|  | 21 | +#include <ws2tcpip.h> | 
|  | 22 | +#endif | 
|  | 23 | + | 
|  | 24 | +namespace Athena | 
|  | 25 | +{ | 
|  | 26 | +namespace net | 
|  | 27 | +{ | 
|  | 28 | +const IPAddress IPAddress::Any; | 
|  | 29 | +const IPAddress IPAddress::None      = IPAddress(  0,   0,   0,   0); | 
|  | 30 | +const IPAddress IPAddress::Localhost = IPAddress(127,   0,   0,   1); | 
|  | 31 | +const IPAddress IPAddress::Broadcast = IPAddress(255, 255, 255, 255); | 
|  | 32 | + | 
|  | 33 | +IPAddress::IPAddress(const std::string& address) | 
|  | 34 | +    : m_valid(false) | 
|  | 35 | +{ | 
|  | 36 | +    resolve(address); | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +IPAddress::IPAddress(atUint8 a, atUint8 b, atUint8 c, atUint8 d) | 
|  | 40 | +    : m_address(htonl((a << 24)| (b << 16) | (c << 8) | d)), | 
|  | 41 | +      m_valid(true) | 
|  | 42 | +{ | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +IPAddress::IPAddress(atUint32 address) | 
|  | 46 | +    : m_address(htonl(address)), | 
|  | 47 | +      m_valid(true) | 
|  | 48 | +{ | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +const std::string IPAddress::toString() const | 
|  | 52 | +{ | 
|  | 53 | +    in_addr address; | 
|  | 54 | +    address.s_addr = m_address; | 
|  | 55 | +    return inet_ntoa(address); | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | +void IPAddress::resolve(const std::string& address) | 
|  | 59 | +{ | 
|  | 60 | +    if (address == "0.0.0.0") | 
|  | 61 | +    { | 
|  | 62 | +        m_address = 0; | 
|  | 63 | +        m_valid = true; | 
|  | 64 | +    } | 
|  | 65 | +    else if(address == "255.255.255.255") | 
|  | 66 | +    { | 
|  | 67 | +        m_address = ~0u; | 
|  | 68 | +        m_valid = true; | 
|  | 69 | +    } | 
|  | 70 | +    else | 
|  | 71 | +    { | 
|  | 72 | +        atUint32 ip = inet_addr(address.c_str()); | 
|  | 73 | +        if (ip == INADDR_NONE) | 
|  | 74 | +        { | 
|  | 75 | +            addrinfo hints = {0}; | 
|  | 76 | +            hints.ai_family = AF_INET; | 
|  | 77 | +            addrinfo* result = nullptr; | 
|  | 78 | +            if (getaddrinfo(address.c_str(), NULL, &hints, &result)) | 
|  | 79 | +            { | 
|  | 80 | +                if (result) | 
|  | 81 | +                { | 
|  | 82 | +                    ip = reinterpret_cast<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr; | 
|  | 83 | +                    freeaddrinfo(result); | 
|  | 84 | +                    m_address = ip; | 
|  | 85 | +                    m_valid = true; | 
|  | 86 | +                } | 
|  | 87 | +            } | 
|  | 88 | +        } | 
|  | 89 | +        else | 
|  | 90 | +        { | 
|  | 91 | +            m_address = ip; | 
|  | 92 | +            m_valid = true; | 
|  | 93 | +        } | 
|  | 94 | +    } | 
|  | 95 | +} | 
|  | 96 | +} | 
|  | 97 | +} | 
0 commit comments