aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/ip.h
blob: dc7c2d24a06652639c039a44a2c2471fcd393ead (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once

#include "socket.h"
#include "hostip.h"

#include <util/system/error.h>
#include <util/system/byteorder.h>
#include <util/generic/string.h>
#include <util/generic/yexception.h>

/// IPv4 address in network format
using TIpHost = ui32;

/// Port number in host format
using TIpPort = ui16;

/*
 * ipStr is in 'ddd.ddd.ddd.ddd' format
 * returns IPv4 address in inet format
 */
static inline TIpHost IpFromString(const char* ipStr) {
    in_addr ia;

    if (inet_aton(ipStr, &ia) == 0) {
        ythrow TSystemError() << "Failed to convert (" << ipStr << ") to ip address";
    }

    return (ui32)ia.s_addr;
}

static inline char* IpToString(TIpHost ip, char* buf, size_t len) {
    if (!inet_ntop(AF_INET, (void*)&ip, buf, (socklen_t)len)) {
        ythrow TSystemError() << "Failed to get ip address string";
    }

    return buf;
}

static inline TString IpToString(TIpHost ip) {
    char buf[INET_ADDRSTRLEN];

    return TString(IpToString(ip, buf, sizeof(buf)));
}

static inline TIpHost ResolveHost(const char* data, size_t len) {
    TIpHost ret;
    const TString s(data, len);

    if (NResolver::GetHostIP(s.data(), &ret) != 0) {
        ythrow TSystemError(NResolver::GetDnsError()) << "can not resolve(" << s << ")";
    }

    return HostToInet(ret);
}

/// socket address
struct TIpAddress: public sockaddr_in {
    inline TIpAddress() noexcept {
        Clear();
    }

    inline TIpAddress(const sockaddr_in& addr) noexcept
        : sockaddr_in(addr)
        , tmp(0)
    {
    }

    inline TIpAddress(TIpHost ip, TIpPort port) noexcept {
        Set(ip, port);
    }

    inline TIpAddress(TStringBuf ip, TIpPort port) {
        Set(ResolveHost(ip.data(), ip.size()), port);
    }

    inline TIpAddress(const char* ip, TIpPort port) {
        Set(ResolveHost(ip, strlen(ip)), port);
    }

    inline operator sockaddr*() const noexcept {
        return (sockaddr*)(sockaddr_in*)this;
    }

    inline operator socklen_t*() const noexcept {
        tmp = sizeof(sockaddr_in);

        return (socklen_t*)&tmp;
    }

    inline operator socklen_t() const noexcept {
        tmp = sizeof(sockaddr_in);

        return tmp;
    }

    inline void Clear() noexcept {
        Zero((sockaddr_in&)(*this));
    }

    inline void Set(TIpHost ip, TIpPort port) noexcept {
        Clear();

        sin_family = AF_INET;
        sin_addr.s_addr = ip;
        sin_port = HostToInet(port);
    }

    inline TIpHost Host() const noexcept {
        return sin_addr.s_addr;
    }

    inline TIpPort Port() const noexcept {
        return InetToHost(sin_port);
    }

private:
    // required for "operator socklen_t*()"
    mutable socklen_t tmp;
};