-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAddressConversion.hpp
108 lines (90 loc) · 5.67 KB
/
AddressConversion.hpp
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
#ifndef __LIBSPEEDWIRE_ADDRESSCONVERSIONS_H__
#define __LIBSPEEDWIRE_ADDRESSCONVERSIONS_H__
#ifdef _WIN32
#include <Winsock2.h>
#include <ws2ipdef.h>
#include <inaddr.h>
#include <in6addr.h>
#else
#include <netinet/in.h>
#include <net/if.h>
#endif
#include <cstdint>
#include <string>
#include <array>
namespace libspeedwire {
/**
* Class implementing platform neutral conversions for bsd internet and socket addresses
*/
class AddressConversion {
public:
// conversions from bsd socket and ip address information to std::string
static std::string toString(const struct in_addr& address);
static std::string toString(const struct in6_addr& address);
static std::string toString(const struct sockaddr& address);
static std::string toString(const struct sockaddr_in& address);
static std::string toString(const struct sockaddr_in6& address);
// conversions for ip addresses
static bool isIpv4(const std::string& ip_address);
static bool isIpv6(const std::string& ip_address);
static bool isIpv4Uri(const std::string& uri_address);
static bool isIpv6Uri(const std::string& uri_address);
static struct in_addr toInAddress(const std::string& ipv4_address);
static struct in6_addr toIn6Address(const std::string& ipv6_address);
// conversions for network masks
static struct in_addr toInNetMask(const uint32_t prefix_length);
static struct in6_addr toIn6NetMask(const uint32_t prefix_length);
static bool resideOnSameSubnet(const struct in_addr& host1, const struct in_addr& host2, const uint32_t prefix_length);
static bool resideOnSameSubnet(const struct in6_addr& host1, const struct in6_addr& host2, const uint32_t prefix_length);
static bool resideOnSameSubnet(const std::string& host1, const std::string& host2, const uint32_t prefix_length);
// type casts for bsd socket address information
static struct sockaddr& toSockAddr(struct sockaddr_in& src);
static struct sockaddr& toSockAddr(struct sockaddr_in6& src);
static struct sockaddr& toSockAddr(struct sockaddr_storage& src);
static struct sockaddr_in& toSockAddrIn(struct sockaddr& src);
static struct sockaddr_in6& toSockAddrIn6(struct sockaddr& src);
static const struct sockaddr& toSockAddr(const struct sockaddr_in& src);
static const struct sockaddr& toSockAddr(const struct sockaddr_in6& src);
static const struct sockaddr& toSockAddr(const struct sockaddr_storage& src);
static const struct sockaddr_in& toSockAddrIn(const struct sockaddr& src);
static const struct sockaddr_in6& toSockAddrIn6(const struct sockaddr& src);
// conversions for sockaddr_storage
static struct sockaddr_storage toSockAddrStorage(const struct sockaddr_in& src);
static struct sockaddr_storage toSockAddrStorage(const struct sockaddr_in6& src);
// conversions for bsd socket address information
static struct sockaddr_in toSockAddrIn(const struct in_addr& address, const uint16_t port = 0);
static struct sockaddr_in6 toSockAddrIn6(const struct in6_addr& address, const uint16_t port = 0);
static struct sockaddr toSockAddr(const struct in_addr& address, const uint16_t port = 0);
static struct sockaddr toSockAddr(const struct in6_addr& address, const uint16_t port = 0);
static struct sockaddr_in toSockAddrIn(const std::string& ipv4_address, const uint16_t port = 0);
static struct sockaddr_in6 toSockAddrIn6(const std::string& ipv6_address, const uint16_t port = 0);
static struct sockaddr_storage toSockAddr(const std::string& ip_address, const uint16_t port = 0);
// check address scope
static bool isLoopbackAddress(const struct in_addr& address);
static bool isBroadcastAddress(const struct in_addr& address);
static bool isMulticastAddress(const struct in_addr& address);
static bool isPrivateAddress(const struct in_addr& address);
static bool isLinkLocalAddress(const struct in_addr& address);
static bool isLoopbackAddress(const struct in6_addr& address);
static bool isMulticastAddress(const struct in6_addr& address);
static bool isLinkLocalAddress(const struct in6_addr& address);
static bool isUniqueLocalAddress(const struct in6_addr& address);
static bool isGlobalAddress(const struct in6_addr& address);
// conversions for ethernet mac addresses
static std::array<uint8_t, 6> toMacAddress(const std::string& mac);
static std::string toString(const std::array<uint8_t, 6>& mac);
static std::array<uint8_t, 8> toEUI64(const std::array<uint8_t, 6>& mac);
static std::array<uint8_t, 8> toEUI64(const std::string& eui64);
static std::string toString(const std::array<uint8_t, 8>& eui64);
// extract address parts from uri, i.e. 192.168.1.1:8080 or [ff02::fb%21}:8080
static std::string extractIPAddress(const std::string& uri_address);
static std::string extractIPPort(const std::string& uri_address);
static std::string extractIPZoneId(const std::string& uri_address);
static struct sockaddr_storage toSockAddrStorageFromUri(const std::string& uri_address);
static std::string stripChars(const std::string& string, const std::string& chars);
static int hexToInt(const char nibble);
static size_t toUint(const std::string& string, size_t& nchars);
static std::string uintToString(size_t value, unsigned int radix = 10);
};
} // namespace libspeedwire
#endif