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