The Pedigree Project  0.1
IpAddress.cc
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 #include "pedigree/kernel/network/IpAddress.h"
21 #include "pedigree/kernel/utilities/StaticString.h"
22 
24 {
25  if (m_Type == IPv4)
26  return (m_Ipv4 & 0xFFFF) == 0xA9FE; // 169.254/16
27  else
28  return (m_Ipv6[0] == 0xFE) && (m_Ipv6[1] == 0x80);
29 }
30 
31 String IpAddress::prefixString(size_t override) const
32 {
33  if (m_Type == IPv4)
34  {
35  return String("");
36  }
37  else
38  {
40  str.clear();
41  size_t prefix = override <= 128 ? override : m_Ipv6Prefix;
42 
43  for (size_t i = 0; i < prefix / 8; i++)
44  {
45  if (i && ((i % 2) == 0))
46  str += ":";
47 
48  size_t pad = 1;
49  if (i && m_Ipv6[i - 1])
50  pad = 2; // Keep internal zeroes (eg f0f).
51  str.append(m_Ipv6[i], 16, pad);
52  }
53 
54  return String(static_cast<const char *>(str));
55  }
56 }
57 
58 String IpAddress::toString() const
59 {
60  if (m_Type == IPv4)
61  {
63  str.clear();
64  str.append(m_Ipv4 & 0xff);
65  str += ".";
66  str.append((m_Ipv4 >> 8) & 0xff);
67  str += ".";
68  str.append((m_Ipv4 >> 16) & 0xff);
69  str += ".";
70  str.append((m_Ipv4 >> 24) & 0xff);
71  return String(static_cast<const char *>(str));
72  }
73  else
74  {
76  str.clear();
77  bool bZeroComp = false;
78  bool alreadyZeroComp = false; // Compression can only come once.
79  // Naive algorithm, compresses first
80  // zeroes but not the largest set.
81  for (size_t i = 0; i < 16; i++)
82  {
83  if (i && ((i % 2) == 0))
84  {
85  if (!bZeroComp)
86  str += ":";
87 
88  if (alreadyZeroComp)
89  {
90  if (m_Ipv6[i])
91  str.append(m_Ipv6[i], 16);
92  continue;
93  }
94 
95  // Zero-compression
96  if (!m_Ipv6[i] && !m_Ipv6[i + 1])
97  {
98  i++;
99  bZeroComp = true;
100  continue;
101  }
102  else if (bZeroComp)
103  {
104  str += ":";
105  bZeroComp = false;
106  alreadyZeroComp = true;
107  }
108  }
109 
110  if (m_Ipv6[i] || (i && m_Ipv6[i - 1]))
111  {
112  size_t pad = 1;
113  if (m_Ipv6[i - 1])
114  pad = 2; // Keep internal zeroes (eg f0f).
115  str.append(m_Ipv6[i], 16, pad);
116  }
117  }
118 
119  // Append the prefix for this address.
120  str.append("/");
121  str.append(m_Ipv6Prefix, 10);
122 
123  return String(static_cast<const char *>(str));
124  }
125 }
126 
128 {
129  if (m_Type == IPv4)
130  {
131  // Leading bits of the IP address = 1110? The old class D is all
132  // multicast now.
133  if (((m_Ipv4 & 0xFF) & 0xE0) == 0xE0)
134  return true;
135  }
136  else if (m_Type == IPv6)
137  {
138  // FF00::/8 is the multicast range for IPv6.
139  if (m_Ipv6[0] == 0xFF)
140  return true;
141  }
142 
143  return false;
144 }
bool isLinkLocal() const
Whether the IP address is considered "link-local" or not.
Definition: IpAddress.cc:23
Definition: String.h:49
String prefixString(size_t override=256) const
Prefix string. Not zero-compressed. For routing.
Definition: IpAddress.cc:31
bool isMulticast() const
Whether the IP address is a valid multicast address.
Definition: IpAddress.cc:127