1 /*****************************************************************
5 | Copyright (c) 2002-2016, Axiomatic Systems, LLC.
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 ****************************************************************/
32 /*----------------------------------------------------------------------
34 +---------------------------------------------------------------------*/
35 #include "NptSockets.h"
38 /*----------------------------------------------------------------------
39 | NPT_IpAddress::NPT_IpAddress
40 +---------------------------------------------------------------------*/
41 NPT_IpAddress::NPT_IpAddress() :
45 NPT_SetMemory(m_Address
, 0, sizeof(m_Address
));
48 /*----------------------------------------------------------------------
49 | NPT_IpAddress::NPT_IpAddress
50 +---------------------------------------------------------------------*/
51 NPT_IpAddress::NPT_IpAddress(Type type
) :
55 NPT_SetMemory(m_Address
, 0, sizeof(m_Address
));
58 /*----------------------------------------------------------------------
59 | NPT_IpAddress::NPT_IpAddress
60 +---------------------------------------------------------------------*/
61 NPT_IpAddress::NPT_IpAddress(unsigned long address
) :
68 /*----------------------------------------------------------------------
69 | NPT_IpAddress::NPT_IpAddress
70 +---------------------------------------------------------------------*/
71 NPT_IpAddress::NPT_IpAddress(unsigned char a
,
78 NPT_SetMemory(&m_Address
[0], 0, sizeof(m_Address
));
85 /*----------------------------------------------------------------------
86 | NPT_IpAddress::NPT_IpAddress
87 +---------------------------------------------------------------------*/
88 NPT_IpAddress::NPT_IpAddress(Type type
, const unsigned char* address
, unsigned int size
, NPT_UInt32 scope_id
) :
92 if (type
== IPV6
&& size
== 16) {
93 NPT_CopyMemory(&m_Address
[0], address
, 16);
94 } else if (type
== IPV4
&& size
== 4) {
95 NPT_CopyMemory(&m_Address
[0], address
, 4);
96 NPT_SetMemory(&m_Address
[4], 0, 12);
99 NPT_SetMemory(&m_Address
[0], 0, 16);
104 /*----------------------------------------------------------------------
105 | NPT_IpAddress::AsLong
106 +---------------------------------------------------------------------*/
108 NPT_IpAddress::AsLong() const
111 (((unsigned long)m_Address
[0])<<24) |
112 (((unsigned long)m_Address
[1])<<16) |
113 (((unsigned long)m_Address
[2])<< 8) |
114 (((unsigned long)m_Address
[3]));
117 /*----------------------------------------------------------------------
118 | NPT_IpAddress::AsBytes
119 +---------------------------------------------------------------------*/
121 NPT_IpAddress::AsBytes() const
126 /*----------------------------------------------------------------------
128 +---------------------------------------------------------------------*/
130 NPT_IpAddress::Set(const unsigned char bytes
[4])
133 m_Address
[0] = bytes
[0];
134 m_Address
[1] = bytes
[1];
135 m_Address
[2] = bytes
[2];
136 m_Address
[3] = bytes
[3];
137 NPT_SetMemory(&m_Address
[4], 0, sizeof(m_Address
)-4);
138 m_ScopeId
= 0; // always 0 for IPv4
143 /*----------------------------------------------------------------------
145 +---------------------------------------------------------------------*/
147 NPT_IpAddress::Set(unsigned long address
)
150 m_Address
[0] = (unsigned char)((address
>> 24) & 0xFF);
151 m_Address
[1] = (unsigned char)((address
>> 16) & 0xFF);
152 m_Address
[2] = (unsigned char)((address
>> 8) & 0xFF);
153 m_Address
[3] = (unsigned char)((address
) & 0xFF);
154 NPT_SetMemory(&m_Address
[4], 0, sizeof(m_Address
)-4);
155 m_ScopeId
= 0; // always 0 for IPv4
160 /*----------------------------------------------------------------------
162 +---------------------------------------------------------------------*/
164 NPT_IpAddress::Set(const unsigned char* bytes
, unsigned int size
, NPT_UInt32 scope_id
)
166 NPT_SetMemory(&m_Address
[0], 0, sizeof(m_Address
));
169 NPT_CopyMemory(&m_Address
[0], bytes
, 4);
170 m_ScopeId
= 0; // always 0 for IPv4
171 } else if (size
== 16) {
173 NPT_CopyMemory(&m_Address
[0], bytes
, 16);
174 m_ScopeId
= scope_id
;
176 return NPT_ERROR_INVALID_PARAMETERS
;
182 /*----------------------------------------------------------------------
183 | NPT_IpAddress::operator==
184 +---------------------------------------------------------------------*/
186 NPT_IpAddress::operator==(const NPT_IpAddress
& other
) const
188 unsigned int bytes_to_check
= (m_Type
== IPV4
)?4:16;
189 for (unsigned int i
=0; i
<bytes_to_check
; i
++) {
190 if (m_Address
[i
] != other
.m_Address
[i
]) {
194 return m_Type
== other
.m_Type
;
197 /*----------------------------------------------------------------------
198 | NPT_IpAddress::ToUrlHost
199 +---------------------------------------------------------------------*/
201 NPT_IpAddress::ToUrlHost() const
203 if (m_Type
== IPV6
) {
204 NPT_String result
= "[";
205 result
+= ToString();
212 /*----------------------------------------------------------------------
213 | NPT_IpAddress::IsUnspecified
214 +---------------------------------------------------------------------*/
216 NPT_IpAddress::IsUnspecified() const
218 for (unsigned int i
=0; i
<(unsigned int)(m_Type
==IPV4
?4:16); i
++) {
219 if (m_Address
[i
]) return false;
224 /*----------------------------------------------------------------------
225 | NPT_IpAddress::IsLooppack
226 +---------------------------------------------------------------------*/
228 NPT_IpAddress::IsLooppack() const
230 if (m_Type
== IPV4
) {
231 return m_Address
[0] == 127 &&
236 return m_Address
[ 0] == 0 &&
237 m_Address
[ 1] == 0 &&
238 m_Address
[ 2] == 0 &&
239 m_Address
[ 3] == 0 &&
240 m_Address
[ 4] == 0 &&
241 m_Address
[ 5] == 0 &&
242 m_Address
[ 6] == 0 &&
243 m_Address
[ 7] == 0 &&
244 m_Address
[ 8] == 0 &&
245 m_Address
[ 9] == 0 &&
246 m_Address
[10] == 0 &&
247 m_Address
[11] == 0 &&
248 m_Address
[12] == 0 &&
249 m_Address
[13] == 0 &&
250 m_Address
[14] == 0 &&
255 /*----------------------------------------------------------------------
256 | NPT_IpAddress::IsV4Compatible
257 +---------------------------------------------------------------------*/
259 NPT_IpAddress::IsV4Compatible() const
261 if (m_Type
== IPV4
) return true;
262 return m_Address
[ 0] == 0 &&
263 m_Address
[ 1] == 0 &&
264 m_Address
[ 2] == 0 &&
265 m_Address
[ 3] == 0 &&
266 m_Address
[ 4] == 0 &&
267 m_Address
[ 5] == 0 &&
268 m_Address
[ 6] == 0 &&
269 m_Address
[ 7] == 0 &&
270 m_Address
[ 8] == 0 &&
271 m_Address
[ 9] == 0 &&
272 m_Address
[10] == 0 &&
273 m_Address
[11] == 0 &&
274 !(m_Address
[12] == 0 &&
275 m_Address
[13] == 0 &&
276 m_Address
[14] == 0 &&
277 m_Address
[15] == 0) &&
278 !(m_Address
[12] == 0 &&
279 m_Address
[13] == 0 &&
280 m_Address
[14] == 0 &&
284 /*----------------------------------------------------------------------
285 | NPT_IpAddress::IsV4Mapped
286 +---------------------------------------------------------------------*/
288 NPT_IpAddress::IsV4Mapped() const
290 if (m_Type
== IPV4
) return false;
291 return m_Address
[ 0] == 0 &&
292 m_Address
[ 1] == 0 &&
293 m_Address
[ 2] == 0 &&
294 m_Address
[ 3] == 0 &&
295 m_Address
[ 4] == 0 &&
296 m_Address
[ 5] == 0 &&
297 m_Address
[ 6] == 0 &&
298 m_Address
[ 7] == 0 &&
299 m_Address
[ 8] == 0 &&
300 m_Address
[ 9] == 0 &&
301 m_Address
[10] == 0xFF &&
302 m_Address
[11] == 0xFF;
305 /*----------------------------------------------------------------------
306 | NPT_IpAddress::IsLinkLocal
307 +---------------------------------------------------------------------*/
309 NPT_IpAddress::IsLinkLocal() const
311 if (m_Type
== IPV4
) {
312 return m_Address
[0] == 169 && m_Address
[1] == 254;
314 return m_Address
[0] == 0xFE && ((m_Address
[1]&0xC0) == 0x80);
318 /*----------------------------------------------------------------------
319 | NPT_IpAddress::IsSiteLocal
320 +---------------------------------------------------------------------*/
322 NPT_IpAddress::IsSiteLocal() const
324 if (m_Type
== IPV4
) return false;
325 return m_Address
[0] == 0xFE && ((m_Address
[1]&0xC0) == 0xC0);
328 /*----------------------------------------------------------------------
329 | NPT_IpAddress::IsUniqueLocal
330 +---------------------------------------------------------------------*/
332 NPT_IpAddress::IsUniqueLocal() const
334 if (m_Type
== IPV4
) {
335 return (m_Address
[0] == 10) ||
336 (m_Address
[0] == 172 && (m_Address
[1]&0xF0) == 16) ||
337 (m_Address
[0] == 192 && m_Address
[1] == 168);
339 return ((m_Address
[0] & 0xFE) == 0xFC);
343 /*----------------------------------------------------------------------
344 | NPT_IpAddress::IsMulticast
345 +---------------------------------------------------------------------*/
347 NPT_IpAddress::IsMulticast() const
349 if (m_Type
== IPV4
) {
350 return (m_Address
[0] & 0xF0) == 224;
352 return m_Address
[0] == 0xFF;
356 /*----------------------------------------------------------------------
357 | NPT_MacAddress::NPT_MacAddress
358 +---------------------------------------------------------------------*/
359 NPT_MacAddress::NPT_MacAddress(Type type
,
360 const unsigned char* address
,
363 SetAddress(type
, address
, length
);
366 /*----------------------------------------------------------------------
367 | NPT_MacAddress::SetAddress
368 +---------------------------------------------------------------------*/
370 NPT_MacAddress::SetAddress(Type type
,
371 const unsigned char* address
,
375 if (length
> NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH
) {
376 length
= NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH
;
379 for (unsigned int i
=0; i
<length
; i
++) {
380 m_Address
[i
] = address
[i
];
384 /*----------------------------------------------------------------------
385 | NPT_MacAddress::ToString
386 +---------------------------------------------------------------------*/
388 NPT_MacAddress::ToString() const
393 char s
[3*NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH
];
394 const char hex
[17] = "0123456789abcdef";
395 for (unsigned int i
=0; i
<m_Length
; i
++) {
396 s
[i
*3 ] = hex
[m_Address
[i
]>>4];
397 s
[i
*3+1] = hex
[m_Address
[i
]&0xf];
400 s
[3*m_Length
-1] = '\0';
407 /*----------------------------------------------------------------------
408 | NPT_NetworkInterface::NPT_NetworkInterface
409 +---------------------------------------------------------------------*/
410 NPT_NetworkInterface::NPT_NetworkInterface(const char* name
,
411 const NPT_MacAddress
& mac
,
419 /*----------------------------------------------------------------------
420 | NPT_NetworkInterface::NPT_NetworkInterface
421 +---------------------------------------------------------------------*/
422 NPT_NetworkInterface::NPT_NetworkInterface(const char* name
,
429 /*----------------------------------------------------------------------
430 | NPT_NetworkInterface::AddAddress
431 +---------------------------------------------------------------------*/
433 NPT_NetworkInterface::AddAddress(const NPT_NetworkInterfaceAddress
& address
)
435 return m_Addresses
.Add(address
);