aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/netliba/v6/udp_address.h
blob: 3e283fe5459c958fb7d1f7085d68c0cb216aadfc (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
#pragma once

#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/system/defaults.h>

struct sockaddr_in6;

namespace NNetliba {
    struct TUdpAddress {
        ui64 Network, Interface;
        int Scope, Port;

        TUdpAddress()
            : Network(0)
            , Interface(0)
            , Scope(0)
            , Port(0)
        {
        }
        bool IsIPv4() const {
            return (Network == 0 && (Interface & 0xffffffffll) == 0xffff0000ll);
        }
        ui32 GetIPv4() const {
            return Interface >> 32;
        }
    };

    inline bool operator==(const TUdpAddress& a, const TUdpAddress& b) {
        return a.Network == b.Network && a.Interface == b.Interface && a.Scope == b.Scope && a.Port == b.Port;
    }

    enum EUdpAddressType {
        UAT_ANY,
        UAT_IPV4,
        UAT_IPV6,
    };

    // accepts sockaddr_in & sockaddr_in6
    void GetUdpAddress(TUdpAddress* res, const sockaddr_in6& addr);
    // generates sockaddr_in6 for both ipv4 & ipv6
    void GetWinsockAddr(sockaddr_in6* res, const TUdpAddress& addr);
    // supports formats like hostname, hostname:124, 127.0.0.1, 127.0.0.1:80, fe34::12, [fe34::12]:80
    TUdpAddress CreateAddress(const TString& server, int defaultPort, EUdpAddressType type = UAT_ANY);
    TString GetAddressAsString(const TUdpAddress& addr);

    bool GetLocalAddresses(TVector<TUdpAddress>* addrs);
}