aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/ipv6_address/ipv6_address.cpp
blob: 24f3bde65d0e2631a3426cf08e857994dfbc49ae (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "ipv6_address.h"
#include "ipv6_address_p.h"

#ifdef _unix_
#include <netinet/in.h>
#endif

#include <util/network/address.h>
#include <util/network/init.h>
#include <util/string/cast.h>
#include <util/string/split.h>
#include <util/system/byteorder.h> 
#include <util/ysaveload.h>

#include <array>

namespace {
    // reinterpret_cast from memory, where most significant bit is first
    inline ui128 FromMemMSF(const char* memPtr) {
        Y_VERIFY(memPtr, " ");
        return ui128{
            *reinterpret_cast<const ui64*>(memPtr),
            *(reinterpret_cast<const ui64*>(memPtr) + 1)
        };
    }

    // zero-terminated copy of address string
    template <size_t N>
    inline auto AddrBuf(TStringBuf str) noexcept {
        std::array<char, N+1> res;
        auto len = Min(str.size(), N);
        CopyN(str.begin(), len, res.begin());
        res[len] = '\0';
        return res;
    }
}

void TIpv6Address::InitFrom(const in6_addr& addr) {
    const ui64* const ui64Ptr = reinterpret_cast<const ui64*>(&addr);
    const ui64 raw[2] = {SwapBytes(*ui64Ptr), SwapBytes(*(ui64Ptr + 1))};
    Ip = FromMemMSF(reinterpret_cast<const char*>(raw));
    Type_ = Ipv6;
}
void TIpv6Address::InitFrom(const in_addr& addr) {
    unsigned long swapped = SwapBytes(addr.s_addr);
    Ip = ui128{0, swapped};
    Type_ = Ipv4;
}

void TIpv6Address::InitFrom(const sockaddr_in6& Addr) {
    InitFrom(Addr.sin6_addr);
    ScopeId_ = Addr.sin6_scope_id;
}
void TIpv6Address::InitFrom(const sockaddr_in& Addr) {
    InitFrom(Addr.sin_addr);
}

TIpv6Address::TIpv6Address(const NAddr::IRemoteAddr& addr) {
    if (addr.Addr()->sa_family == AF_INET) { // IPv4
        const sockaddr_in* Tmp = reinterpret_cast<const sockaddr_in*>(addr.Addr());
        InitFrom(*Tmp);
    } else { // IPv6
        const sockaddr_in6* Tmp = reinterpret_cast<const sockaddr_in6*>(addr.Addr());
        InitFrom(*Tmp);
    }
}
TIpv6Address::TIpv6Address(const sockaddr_in6& Addr) {
    InitFrom(Addr);
}
TIpv6Address::TIpv6Address(const sockaddr_in& Addr) {
    InitFrom(Addr);
}
TIpv6Address::TIpv6Address(const in6_addr& addr, ui32 Scope) {
    InitFrom(addr);
    ScopeId_ = Scope;
}
TIpv6Address::TIpv6Address(const in_addr& addr) {
    InitFrom(addr);
}

TIpv6Address TIpv6Address::FromString(TStringBuf str, bool& ok) noexcept {
    const TIpType ipType = FigureOutType(str);

    if (ipType == Ipv6) {
        ui32 scopeId = 0;
        if (size_t pos = str.find('%'); pos != TStringBuf::npos) {
            ::TryFromString(str.substr(pos + 1), scopeId);
            str.Trunc(pos);
        }

        const auto buf = AddrBuf<INET6_ADDRSTRLEN>(str);
        in6_addr addr;
        if (inet_pton(AF_INET6, buf.data(), &addr) != 1) {
            ok = false;
            return TIpv6Address();
        }

        ok = true;
        return TIpv6Address(addr, scopeId);
    } else { // if (ipType == Ipv4) {
        const auto buf = AddrBuf<INET_ADDRSTRLEN>(str);
        in_addr addr;
        if (inet_pton(AF_INET, buf.data(), &addr) != 1) {
            ok = false;
            return TIpv6Address();
        }

        ok = true;
        return TIpv6Address(addr);
    }
}

TString TIpv6Address::ToString(bool* ok) const noexcept {
    return ToString(true, ok);
}
TString TIpv6Address::ToString(bool PrintScopeId, bool* ok) const noexcept {
    TString result;
    bool isOk = true;

    if (Type_ == TIpv6Address::Ipv4) {
        result.resize(INET_ADDRSTRLEN + 2);
        in_addr addr;
        ToInAddr(addr);
        isOk = inet_ntop(AF_INET, &addr, result.begin(), INET_ADDRSTRLEN);
        result.resize(result.find('\0'));
    } else if (Type_ == TIpv6Address::Ipv6) {
        result.resize(INET6_ADDRSTRLEN + 2);
        in6_addr addr;
        ToIn6Addr(addr);
        isOk = inet_ntop(AF_INET6, &addr, result.begin(), INET6_ADDRSTRLEN);
        result.resize(result.find('\0'));
        if (PrintScopeId)
            result += "%" + ::ToString(ScopeId_);
    } else {
        result = "null";
        isOk = true;
    }

    if (ok) {
        *ok = isOk;
    }

    return result;
}

void TIpv6Address::ToSockaddrAndSocklen(sockaddr_in& sockAddrIPv4,
                                        sockaddr_in6& sockAddrIPv6, // in
                                        const sockaddr*& sockAddrPtr,
                                        socklen_t& sockAddrSize,
                                        ui16 Port) const { // out

    if (Type_ == Ipv4) {
        memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4));
        sockAddrIPv4.sin_family = AF_INET;
        sockAddrIPv4.sin_port = htons(Port);
        ToInAddr(sockAddrIPv4.sin_addr);

        sockAddrSize = sizeof(sockAddrIPv4);
        sockAddrPtr = reinterpret_cast<sockaddr*>(&sockAddrIPv4);

    } else if (Type_ == Ipv6) {
        memset(&sockAddrIPv6, 0, sizeof(sockAddrIPv6));
        sockAddrIPv6.sin6_family = AF_INET6;
        sockAddrIPv6.sin6_port = htons(Port);
        ToIn6Addr(sockAddrIPv6.sin6_addr);
        sockAddrIPv6.sin6_scope_id = ScopeId_;
        sockAddrIPv6.sin6_flowinfo = 0;

        sockAddrSize = sizeof(sockAddrIPv6);
        sockAddrPtr = reinterpret_cast<sockaddr*>(&sockAddrIPv6);
    } else
        Y_VERIFY(false);
}

void TIpv6Address::ToInAddr(in_addr& Addr4) const {
    Y_VERIFY(Type_ == TIpv6Address::Ipv4);

    Zero(Addr4);
    ui32 Value = GetLow(Ip);
    Y_VERIFY(Value == GetLow(Ip), " ");
    Y_VERIFY(GetHigh(Ip) == 0, " ");
    Addr4.s_addr = SwapBytes(Value);
}
void TIpv6Address::ToIn6Addr(in6_addr& Addr6) const {
    Y_VERIFY(Type_ == TIpv6Address::Ipv6);

    Zero(Addr6);
    ui64 Raw[2] = {GetHigh(Ip), GetLow(Ip)};
    *Raw = SwapBytes(*Raw);
    Raw[1] = SwapBytes(1 [Raw]);
    memcpy(&Addr6, Raw, sizeof(Raw));
}

void TIpv6Address::Save(IOutputStream* out) const {
    ::Save(out, Ip);
    ::Save(out, static_cast<ui8>(Type_));
    ::Save(out, ScopeId_);
}
void TIpv6Address::Load(IInputStream* in) {
    ::Load(in, Ip);
    ui8 num;
    ::Load(in, num);
    Type_ = static_cast<TIpType>(num);
    ::Load(in, ScopeId_);
}

bool TIpv6Address::Isv4MappedTov6() const noexcept {
    /// http://en.wikipedia.org/wiki/IPv6
    /// Hybrid dual-stack IPv6/IPv4 implementations recognize a special class of addresses,
    /// the IPv4-mapped IPv6 addresses.  In these addresses, the first 80 bits are zero, the next 16 bits are one,
    /// and the remaining 32 bits are the IPv4 address.

    if (Type_ != Ipv6)
        return false;

    if (GetHigh(Ip) != 0)
        return false; // First 64 bit are not zero -> it is not ipv4-mapped-ipv6 address

    const ui64 Low = GetLow(Ip) >> 32;
    if (Low != 0x0000ffff)
        return false;

    return true;
}

TIpv6Address TIpv6Address::TryToExtractIpv4From6() const noexcept {
    if (Isv4MappedTov6() == false)
        return TIpv6Address();

    const ui64 NewLow = GetLow(Ip) & 0x00000000ffffffff;
    TIpv6Address Result(ui128(0, NewLow), Ipv4);
    return Result;
}

TIpv6Address TIpv6Address::Normalized() const noexcept {
    if (Isv4MappedTov6() == false)
        return *this;

    TIpv6Address Result = TryToExtractIpv4From6();
    Y_VERIFY(Result.IsNull() == false);
    return Result;
}

IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address::TIpType Type) noexcept {
    switch (Type) {
    case TIpv6Address::Ipv4:
        Out << "Ipv4";
        return Out;
    case TIpv6Address::Ipv6:
        Out << "Ipv6";
        return Out;
    default:
        Out << "UnknownType";
        return Out;
    }
}

IOutputStream& operator<<(IOutputStream& out, const TIpv6Address& ipv6Address) noexcept {
    bool ok;
    const TString& strIp = ipv6Address.ToString(&ok);
    if (!ok) {
        return out << "Can not convert ip to string";
    } else {
        return out << strIp;
    }
}

TString THostAddressAndPort::ToString() const noexcept {
    TStringStream Str;
    Str << *this;
    return Str.Str();
}

IOutputStream& operator<<(IOutputStream& Out, const THostAddressAndPort& HostAddressAndPort) noexcept {
    Out << HostAddressAndPort.Ip << ":" << HostAddressAndPort.Port;
    return Out;
}

namespace {
    class TRemoteAddr: public NAddr::IRemoteAddr {
    public:
        TRemoteAddr(const TIpv6Address& Address, TIpPort Port);
        const sockaddr* Addr() const override;
        socklen_t Len() const override;

    private:
        sockaddr_in SockAddrIPv4;
        sockaddr_in6 SockAddrIPv6;
        const sockaddr* SockAddrPtr = nullptr;
        socklen_t SockAddrSize = 0;
    };

    TRemoteAddr::TRemoteAddr(const TIpv6Address& Address, TIpPort Port) {
        Address.ToSockaddrAndSocklen(SockAddrIPv4, SockAddrIPv6, SockAddrPtr, SockAddrSize, Port);
    }
    const sockaddr* TRemoteAddr::Addr() const {
        return SockAddrPtr;
    }
    socklen_t TRemoteAddr::Len() const {
        return SockAddrSize;
    }
}

NAddr::IRemoteAddr* ToIRemoteAddr(const TIpv6Address& Address, TIpPort Port) {
    return new TRemoteAddr(Address, Port);
}

std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromString(const TString& RawStr,
                                                                                  TIpPort DefaultPort,
                                                                                  bool& Ok) noexcept {
    // Cout << "ParseHostAndMayBePortFromString: " << RawStr << ", Port: " << DefaultPort << Endl;

    using TRes = std::tuple<THostAddressAndPort, TString, TIpPort>;

    // ---------------------------------------------------------------------

    const size_t BracketColPos = RawStr.find("]:");
    if (BracketColPos != TString::npos) {
        // [ipv6]:port
        if (!RawStr.StartsWith('[')) {
            Ok = false;
            return {};
        }
        const TStringBuf StrIpv6(RawStr.begin() + 1, RawStr.begin() + BracketColPos);
        const TStringBuf StrPort(RawStr.begin() + BracketColPos + 2, RawStr.end());

        bool IpConverted;
        const TIpv6Address Ip = TIpv6Address::FromString(StrIpv6, IpConverted);
        if (!IpConverted) {
            Ok = false;
            return {};
        }
        if (Ip.Type() != TIpv6Address::Ipv6) {
            Ok = false;
            return {};
        }
        TIpPort Port {};
        if (!::TryFromString(StrPort, Port)) {
            Ok = false;
            return {};
        }

        Ok = true;
        TRes Res{{Ip, Port}, {}, {}};
        return Res;
    }

    // ---------------------------------------------------------------------

    if (RawStr.StartsWith('[')) {
        // [ipv6]
        if (!RawStr.EndsWith(']')) {
            Ok = false;
            return {};
        }
        const TStringBuf StrIpv6(RawStr.begin() + 1, RawStr.end() - 1);

        bool IpConverted;
        const TIpv6Address Ip = TIpv6Address::FromString(StrIpv6, IpConverted);
        if (!IpConverted || Ip.Type() != TIpv6Address::Ipv6) {
            Ok = false;
            return {};
        }

        Ok = true;
        TRes Res{{Ip, DefaultPort}, {}, {}};
        return Res;
    }

    // ---------------------------------------------------------------------

    const size_t ColPos = RawStr.find(':');
    if (ColPos != TString::npos) {
        // host:port
        // ipv4:port
        // ipv6

        {
            bool IpConverted;
            const TIpv6Address Ipv6 = TIpv6Address::FromString(RawStr, IpConverted);
            if (IpConverted && Ipv6.Type() == TIpv6Address::Ipv6) {
                // ipv6
                Ok = true;
                TRes Res{{Ipv6, DefaultPort}, {}, {}};
                return Res;
            }
        }

        const TStringBuf StrPort(RawStr.begin() + ColPos + 1, RawStr.end());
        TIpPort Port {};
        if (!::TryFromString(StrPort, Port)) {
            Ok = false;
            return {};
        }

        const TStringBuf StrIpv4OrHost(RawStr.begin(), RawStr.begin() + ColPos);
        {
            bool IpConverted;
            const TIpv6Address Ipv4 = TIpv6Address::FromString(StrIpv4OrHost, IpConverted);
            if (IpConverted && Ipv4.Type() == TIpv6Address::Ipv4) {
                // ipv4:port
                Ok = true;
                TRes Res{{Ipv4, Port}, {}, {}};
                return Res;
            }
        }

        {
            // host:port
            Ok = true;
            TRes Res{THostAddressAndPort{}, TString(StrIpv4OrHost), Port};
            return Res;
        }
    }

    // ---------------------------------------------------------------------

    {
        // ipv4
        bool IpConverted;
        const TIpv6Address Ipv4 = TIpv6Address::FromString(RawStr, IpConverted);
        if (IpConverted && Ipv4.Type() == TIpv6Address::Ipv4) {
            Ok = true;
            TRes Res{{Ipv4, DefaultPort}, {}, {}};
            return Res;
        }
    }

    // ---------------------------------------------------------------------

    {
        // host
        Ok = true;
        TRes Res{THostAddressAndPort{}, TString(RawStr), DefaultPort};
        return Res;
    }
}