aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/hostip.cpp
blob: cb8d43bf903898b774cc556f74fc97e34889d778 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "socket.h"
#include "hostip.h"

#include <util/system/defaults.h>
#include <util/system/byteorder.h>

#if defined(_unix_) || defined(_cygwin_)
    #include <netdb.h>
#endif

#if !defined(BIND_LIB)
    #if !defined(__FreeBSD__) && !defined(_win32_) && !defined(_cygwin_)
        #define AGENT_USE_GETADDRINFO
    #endif

    #if defined(__FreeBSD__)
        #define AGENT_USE_GETADDRINFO
    #endif
#endif

int NResolver::GetHostIP(const char* hostname, ui32* ip, size_t* slots) {
    size_t i = 0;
    size_t ipsFound = 0;

#ifdef AGENT_USE_GETADDRINFO
    int ret = 0;
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    struct addrinfo* gai_res = nullptr;
    int gai_ret = getaddrinfo(hostname, nullptr, &hints, &gai_res);
    if (gai_ret == 0 && gai_res->ai_addr) {
        struct addrinfo* cur = gai_res;
        for (i = 0; i < *slots && cur; i++, cur = cur->ai_next, ipsFound++) {
            ip[i] = *(ui32*)(&((sockaddr_in*)(cur->ai_addr))->sin_addr);
        }
    } else {
        if (gai_ret == EAI_NONAME || gai_ret == EAI_SERVICE) {
            ret = HOST_NOT_FOUND;
        } else {
            ret = GetDnsError();
        }
    }

    if (gai_res) {
        freeaddrinfo(gai_res);
    }

    if (ret) {
        return ret;
    }
#else
    hostent* hostent = gethostbyname(hostname);

    if (!hostent)
        return GetDnsError();

    if (hostent->h_addrtype != AF_INET || (unsigned)hostent->h_length < sizeof(ui32))
        return HOST_NOT_FOUND;

    char** cur = hostent->h_addr_list;
    for (i = 0; i < *slots && *cur; i++, cur++, ipsFound++)
        ip[i] = *(ui32*)*cur;
#endif
    for (i = 0; i < ipsFound; i++) {
        ip[i] = InetToHost(ip[i]);
    }
    *slots = ipsFound;

    return 0;
}

int NResolver::GetDnsError() {
    return h_errno;
}