-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.h
37 lines (28 loc) · 762 Bytes
/
ip.h
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
#pragma once
#include <cstdint>
#include <string>
struct Ip final {
static const int SIZE = 4;
// constructor
Ip() {}
Ip(const uint32_t r) : ip_(r) {}
Ip(const std::string r);
// casting operator
operator uint32_t() const { return ip_; } // default
explicit operator std::string() const;
// comparison operator
bool operator == (const Ip& r) const { return ip_ == r.ip_; }
bool isLocalHost() const { // 127.*.*.*
uint8_t prefix = (ip_ & 0xFF000000) >> 24;
return prefix == 0x7F;
}
bool isBroadcast() const { // 255.255.255.255
return ip_ == 0xFFFFFFFF;
}
bool isMulticast() const { // 224.0.0.0 ~ 239.255.255.255
uint8_t prefix = (ip_ & 0xFF000000) >> 24;
return prefix >= 0xE0 && prefix < 0xF0;
}
protected:
uint32_t ip_;
};