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 
30 {
31  public:
33  enum IpType
34  {
35  IPv4 = 0x0800,
36  IPv6 = 0x86DD
37  };
38 
41  : m_Type(IPv4), m_Ipv4(0), m_Ipv6(), m_bSet(false), m_Ipv6Prefix(128)
42  {
43  }
44  IpAddress(IpType type)
45  : m_Type(type), m_Ipv4(0), m_Ipv6(), m_bSet(false), m_Ipv6Prefix(128)
46  {
47  }
48  IpAddress(uint32_t ipv4)
49  : m_Type(IPv4), m_Ipv4(ipv4), m_Ipv6(), m_bSet(true), m_Ipv6Prefix(128)
50  {
51  }
52  IpAddress(uint8_t *ipv6)
53  : m_Type(IPv6), m_Ipv4(0), m_Ipv6(), m_bSet(true), m_Ipv6Prefix(128)
54  {
55  MemoryCopy(m_Ipv6, ipv6, 16);
56  }
57  IpAddress(const IpAddress &other) : IpAddress()
58  {
59  if (other.getType() == IPv6)
60  {
61  other.getIp(m_Ipv6);
62  m_Ipv4 = 0;
63  m_Type = IPv6;
64  m_Ipv6Prefix = other.getIpv6Prefix();
65  m_bSet = true;
66  }
67  else
68  {
69  setIp(other.getIp());
70  }
71  }
72 
75  void setIp(uint32_t ipv4)
76  {
77  m_Ipv4 = ipv4;
78  ByteSet(m_Ipv6, 0, 16);
79  m_bSet = true;
80  m_Type = IPv4;
81  }
82 
83  void setIp(uint8_t *ipv6)
84  {
85  m_Ipv4 = 0;
86  MemoryCopy(m_Ipv6, ipv6, 16);
87  m_bSet = true;
88  m_Type = IPv6;
89  }
90 
93  inline uint32_t getIp() const
94  {
95  return m_Ipv4;
96  }
97 
98  inline void getIp(uint8_t *ipv6) const
99  {
100  if (ipv6)
101  MemoryCopy(ipv6, m_Ipv6, 16);
102  }
103 
106  IpType getType() const
107  {
108  return m_Type;
109  }
110 
113  IpAddress operator+(const IpAddress &a) const
114  {
115  if (a.getType() != m_Type || m_Type == IPv6 || a.getType() == IPv6)
116  return IpAddress();
117  else
118  {
119  IpAddress ret(getIp() + a.getIp());
120  return ret;
121  }
122  }
123 
124  IpAddress operator&(const IpAddress &a) const
125  {
126  if (a.getType() != m_Type || m_Type == IPv6 || a.getType() == IPv6)
127  return IpAddress();
128  else
129  {
130  IpAddress ret(getIp() & a.getIp());
131  return ret;
132  }
133  }
134 
135  IpAddress &operator=(const IpAddress &a)
136  {
137  if (a.getType() == IPv6)
138  {
139  a.getIp(m_Ipv6);
140  m_Ipv4 = 0;
141  m_Type = IPv6;
142  m_Ipv6Prefix = a.getIpv6Prefix();
143  m_bSet = true;
144  }
145  else
146  setIp(a.getIp());
147  return *this;
148  }
149 
150  bool operator==(const IpAddress &a) const
151  {
152  if (a.getType() == IPv4)
153  return (a.getIp() == getIp());
154  else
155  {
156  return !MemoryCompare(m_Ipv6, a.m_Ipv6, 16);
157  }
158  }
159 
160  bool operator!=(const IpAddress &a) const
161  {
162  if (a.getType() == IPv4)
163  return (a.getIp() != getIp());
164  else
165  {
166  return !MemoryCompare(m_Ipv6, a.m_Ipv6, 16);
167  }
168  }
169 
170  bool operator==(uint32_t a) const
171  {
172  if (getType() == IPv4)
173  return (a == getIp());
174  else
175  return false;
176  }
177 
178  bool operator!=(uint32_t a) const
179  {
180  if (getType() != IPv4)
181  return (a != getIp());
182  else
183  return false;
184  }
185 
186  String toString() const;
187 
189  String prefixString(size_t override = 256) const;
190 
192  bool isLinkLocal() const;
193 
195  bool isMulticast() const;
196 
198  inline bool isUnicast() const
199  {
200  return !isMulticast();
201  }
202 
204  inline size_t getIpv6Prefix() const
205  {
206  return m_Ipv6Prefix;
207  }
208 
210  inline void setIpv6Prefix(size_t prefix)
211  {
212  m_Ipv6Prefix = prefix;
213  }
214 
215  private:
216  IpType m_Type;
217 
218  uint32_t m_Ipv4; // the IPv4 address
219  uint8_t m_Ipv6[16]; // the IPv6 address
220 
221  bool m_bSet; // has the IP been set yet?
222 
223  size_t m_Ipv6Prefix; // IPv6 prefix for this IP.
224 };
225 
226 #endif
IpAddress()
Definition: IpAddress.h:40
Definition: String.h:49
IpAddress operator+(const IpAddress &a) const
Definition: IpAddress.h:113
void setIp(uint32_t ipv4)
Definition: IpAddress.h:75
void setIpv6Prefix(size_t prefix)
Sets the IPv6 prefix for this address.
Definition: IpAddress.h:210
bool isUnicast() const
Whether the IP address is a valid unicast address.
Definition: IpAddress.h:198
IpType getType() const
Definition: IpAddress.h:106
size_t getIpv6Prefix() const
Obtains the IPv6 prefix for this address.
Definition: IpAddress.h:204
uint32_t getIp() const
Definition: IpAddress.h:93
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
Definition: Iterator.h:326
bool operator!=(const T1 &x1, const T2 &x2)
Global != operator for types with overloaded == operator.
Definition: template.h:32