The Pedigree Project 0.1
IpAddress.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef IP_ADDRESS_H
21#define IP_ADDRESS_H
22
23#include "pedigree/kernel/compiler.h"
24#include "pedigree/kernel/processor/types.h"
25#include "pedigree/kernel/utilities/String.h"
26#include "pedigree/kernel/utilities/utility.h"
27
29class EXPORTED_PUBLIC IpAddress {
30 public:
32 enum IpType { IPv4 = 0x0800, IPv6 = 0x86DD };
33
35 IpAddress() : m_Type(IPv4), m_Ipv4(0), m_Ipv6(), m_bSet(false), m_Ipv6Prefix(128) {}
36 IpAddress(IpType type) : m_Type(type), m_Ipv4(0), m_Ipv6(), m_bSet(false), m_Ipv6Prefix(128) {}
37 IpAddress(uint32_t ipv4)
38 : m_Type(IPv4), m_Ipv4(ipv4), m_Ipv6(), m_bSet(true), m_Ipv6Prefix(128) {}
39 IpAddress(uint8_t* ipv6) : m_Type(IPv6), m_Ipv4(0), m_Ipv6(), m_bSet(true), m_Ipv6Prefix(128) {
40 MemoryCopy(m_Ipv6, ipv6, 16);
41 }
42 IpAddress(const IpAddress& other) : IpAddress() {
43 if (other.getType() == IPv6) {
44 other.getIp(m_Ipv6);
45 m_Ipv4 = 0;
46 m_Type = IPv6;
47 m_Ipv6Prefix = other.getIpv6Prefix();
48 m_bSet = true;
49 } else {
50 setIp(other.getIp());
51 }
52 }
53
56 void setIp(uint32_t ipv4) {
57 m_Ipv4 = ipv4;
58 ByteSet(m_Ipv6, 0, 16);
59 m_bSet = true;
60 m_Type = IPv4;
61 }
62
63 void setIp(uint8_t* ipv6) {
64 m_Ipv4 = 0;
65 MemoryCopy(m_Ipv6, ipv6, 16);
66 m_bSet = true;
67 m_Type = IPv6;
68 }
69
72 inline uint32_t getIp() const {
73 return m_Ipv4;
74 }
75
76 inline void getIp(uint8_t* ipv6) const {
77 if (ipv6)
78 MemoryCopy(ipv6, m_Ipv6, 16);
79 }
80
83 IpType getType() const {
84 return m_Type;
85 }
86
89 IpAddress operator+(const IpAddress& a) const {
90 if (a.getType() != m_Type || m_Type == IPv6 || a.getType() == IPv6)
91 return IpAddress();
92 else {
93 IpAddress ret(getIp() + a.getIp());
94 return ret;
95 }
96 }
97
98 IpAddress operator&(const IpAddress& a) const {
99 if (a.getType() != m_Type || m_Type == IPv6 || a.getType() == IPv6)
100 return IpAddress();
101 else {
102 IpAddress ret(getIp() & a.getIp());
103 return ret;
104 }
105 }
106
107 IpAddress& operator=(const IpAddress& a) {
108 if (a.getType() == IPv6) {
109 a.getIp(m_Ipv6);
110 m_Ipv4 = 0;
111 m_Type = IPv6;
112 m_Ipv6Prefix = a.getIpv6Prefix();
113 m_bSet = true;
114 } else
115 setIp(a.getIp());
116 return *this;
117 }
118
119 bool operator==(const IpAddress& a) const {
120 if (a.getType() == IPv4)
121 return (a.getIp() == getIp());
122 else {
123 return !MemoryCompare(m_Ipv6, a.m_Ipv6, 16);
124 }
125 }
126
127 bool operator!=(const IpAddress& a) const {
128 if (a.getType() == IPv4)
129 return (a.getIp() != getIp());
130 else {
131 return !MemoryCompare(m_Ipv6, a.m_Ipv6, 16);
132 }
133 }
134
135 bool operator==(uint32_t a) const {
136 if (getType() == IPv4)
137 return (a == getIp());
138 else
139 return false;
140 }
141
142 bool operator!=(uint32_t a) const {
143 if (getType() != IPv4)
144 return (a != getIp());
145 else
146 return false;
147 }
148
149 String toString() const;
150
152 String prefixString(size_t override = 256) const;
153
155 bool isLinkLocal() const;
156
158 bool isMulticast() const;
159
161 inline bool isUnicast() const {
162 return !isMulticast();
163 }
164
166 inline size_t getIpv6Prefix() const {
167 return m_Ipv6Prefix;
168 }
169
171 inline void setIpv6Prefix(size_t prefix) {
172 m_Ipv6Prefix = prefix;
173 }
174
175 private:
176 IpType m_Type;
177
178 uint32_t m_Ipv4; // the IPv4 address
179 uint8_t m_Ipv6[16]; // the IPv6 address
180
181 bool m_bSet; // has the IP been set yet?
182
183 size_t m_Ipv6Prefix; // IPv6 prefix for this IP.
184};
185
186#endif
void setIp(uint32_t ipv4)
Definition IpAddress.h:56
bool isUnicast() const
Whether the IP address is a valid unicast address.
Definition IpAddress.h:161
size_t getIpv6Prefix() const
Obtains the IPv6 prefix for this address.
Definition IpAddress.h:166
void setIpv6Prefix(size_t prefix)
Sets the IPv6 prefix for this address.
Definition IpAddress.h:171
IpType getType() const
Definition IpAddress.h:83
IpAddress operator+(const IpAddress &a) const
Definition IpAddress.h:89
uint32_t getIp() const
Definition IpAddress.h:72
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
Definition Iterator.h:256