Merge pull request #25959 from neo1973/TagLib_deprecation_warnings
[xbmc.git] / lib / libUPnP / Neptune / Source / Core / NptNetwork.cpp
blob1b8bde7e12f8539b6297805147375707052cdebb
1 /*****************************************************************
3 | Neptune - Network
5 | Copyright (c) 2002-2016, Axiomatic Systems, LLC.
6 | All rights reserved.
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 /*----------------------------------------------------------------------
33 | includes
34 +---------------------------------------------------------------------*/
35 #include "NptSockets.h"
36 #include "NptUtils.h"
38 /*----------------------------------------------------------------------
39 | NPT_IpAddress::NPT_IpAddress
40 +---------------------------------------------------------------------*/
41 NPT_IpAddress::NPT_IpAddress() :
42 m_Type(IPV4),
43 m_ScopeId(0)
45 NPT_SetMemory(m_Address, 0, sizeof(m_Address));
48 /*----------------------------------------------------------------------
49 | NPT_IpAddress::NPT_IpAddress
50 +---------------------------------------------------------------------*/
51 NPT_IpAddress::NPT_IpAddress(Type type) :
52 m_Type(type),
53 m_ScopeId(0)
55 NPT_SetMemory(m_Address, 0, sizeof(m_Address));
58 /*----------------------------------------------------------------------
59 | NPT_IpAddress::NPT_IpAddress
60 +---------------------------------------------------------------------*/
61 NPT_IpAddress::NPT_IpAddress(unsigned long address) :
62 m_Type(IPV4),
63 m_ScopeId(0)
65 Set(address);
68 /*----------------------------------------------------------------------
69 | NPT_IpAddress::NPT_IpAddress
70 +---------------------------------------------------------------------*/
71 NPT_IpAddress::NPT_IpAddress(unsigned char a,
72 unsigned char b,
73 unsigned char c,
74 unsigned char d) :
75 m_Type(IPV4),
76 m_ScopeId(0)
78 NPT_SetMemory(&m_Address[0], 0, sizeof(m_Address));
79 m_Address[0] = a;
80 m_Address[1] = b;
81 m_Address[2] = c;
82 m_Address[3] = d;
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) :
89 m_Type(type),
90 m_ScopeId(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);
97 m_ScopeId = 0;
98 } else {
99 NPT_SetMemory(&m_Address[0], 0, 16);
100 m_ScopeId = 0;
104 /*----------------------------------------------------------------------
105 | NPT_IpAddress::AsLong
106 +---------------------------------------------------------------------*/
107 unsigned long
108 NPT_IpAddress::AsLong() const
110 return
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 +---------------------------------------------------------------------*/
120 const unsigned char*
121 NPT_IpAddress::AsBytes() const
123 return m_Address;
126 /*----------------------------------------------------------------------
127 | NPT_IpAddress::Set
128 +---------------------------------------------------------------------*/
129 NPT_Result
130 NPT_IpAddress::Set(const unsigned char bytes[4])
132 m_Type = IPV4;
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
140 return NPT_SUCCESS;
143 /*----------------------------------------------------------------------
144 | NPT_IpAddress::Set
145 +---------------------------------------------------------------------*/
146 NPT_Result
147 NPT_IpAddress::Set(unsigned long address)
149 m_Type = IPV4;
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
157 return NPT_SUCCESS;
160 /*----------------------------------------------------------------------
161 | NPT_IpAddress::Set
162 +---------------------------------------------------------------------*/
163 NPT_Result
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));
167 if (size == 4) {
168 m_Type = IPV4;
169 NPT_CopyMemory(&m_Address[0], bytes, 4);
170 m_ScopeId = 0; // always 0 for IPv4
171 } else if (size == 16) {
172 m_Type = IPV6;
173 NPT_CopyMemory(&m_Address[0], bytes, 16);
174 m_ScopeId = scope_id;
175 } else {
176 return NPT_ERROR_INVALID_PARAMETERS;
179 return NPT_SUCCESS;
182 /*----------------------------------------------------------------------
183 | NPT_IpAddress::operator==
184 +---------------------------------------------------------------------*/
185 bool
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]) {
191 return false;
194 return m_Type == other.m_Type;
197 /*----------------------------------------------------------------------
198 | NPT_IpAddress::ToUrlHost
199 +---------------------------------------------------------------------*/
200 NPT_String
201 NPT_IpAddress::ToUrlHost() const
203 if (m_Type == IPV6) {
204 NPT_String result = "[";
205 result += ToString();
206 return result+"]";
207 } else {
208 return ToString();
212 /*----------------------------------------------------------------------
213 | NPT_IpAddress::IsUnspecified
214 +---------------------------------------------------------------------*/
215 bool
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;
221 return true;
224 /*----------------------------------------------------------------------
225 | NPT_IpAddress::IsLooppack
226 +---------------------------------------------------------------------*/
227 bool
228 NPT_IpAddress::IsLooppack() const
230 if (m_Type == IPV4) {
231 return m_Address[0] == 127 &&
232 m_Address[1] == 0 &&
233 m_Address[2] == 0 &&
234 m_Address[3] == 1;
235 } else {
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 &&
251 m_Address[15] == 1;
255 /*----------------------------------------------------------------------
256 | NPT_IpAddress::IsV4Compatible
257 +---------------------------------------------------------------------*/
258 bool
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 &&
281 m_Address[15] == 1);
284 /*----------------------------------------------------------------------
285 | NPT_IpAddress::IsV4Mapped
286 +---------------------------------------------------------------------*/
287 bool
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 +---------------------------------------------------------------------*/
308 bool
309 NPT_IpAddress::IsLinkLocal() const
311 if (m_Type == IPV4) {
312 return m_Address[0] == 169 && m_Address[1] == 254;
313 } else {
314 return m_Address[0] == 0xFE && ((m_Address[1]&0xC0) == 0x80);
318 /*----------------------------------------------------------------------
319 | NPT_IpAddress::IsSiteLocal
320 +---------------------------------------------------------------------*/
321 bool
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 +---------------------------------------------------------------------*/
331 bool
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);
338 } else {
339 return ((m_Address[0] & 0xFE) == 0xFC);
343 /*----------------------------------------------------------------------
344 | NPT_IpAddress::IsMulticast
345 +---------------------------------------------------------------------*/
346 bool
347 NPT_IpAddress::IsMulticast() const
349 if (m_Type == IPV4) {
350 return (m_Address[0] & 0xF0) == 224;
351 } else {
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,
361 unsigned int length)
363 SetAddress(type, address, length);
366 /*----------------------------------------------------------------------
367 | NPT_MacAddress::SetAddress
368 +---------------------------------------------------------------------*/
369 void
370 NPT_MacAddress::SetAddress(Type type,
371 const unsigned char* address,
372 unsigned int length)
374 m_Type = type;
375 if (length > NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH) {
376 length = NPT_NETWORK_MAX_MAC_ADDRESS_LENGTH;
378 m_Length = length;
379 for (unsigned int i=0; i<length; i++) {
380 m_Address[i] = address[i];
384 /*----------------------------------------------------------------------
385 | NPT_MacAddress::ToString
386 +---------------------------------------------------------------------*/
387 NPT_String
388 NPT_MacAddress::ToString() const
390 NPT_String result;
392 if (m_Length) {
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];
398 s[i*3+2] = ':';
400 s[3*m_Length-1] = '\0';
401 result = s;
404 return result;
407 /*----------------------------------------------------------------------
408 | NPT_NetworkInterface::NPT_NetworkInterface
409 +---------------------------------------------------------------------*/
410 NPT_NetworkInterface::NPT_NetworkInterface(const char* name,
411 const NPT_MacAddress& mac,
412 NPT_Flags flags) :
413 m_Name(name),
414 m_MacAddress(mac),
415 m_Flags(flags)
419 /*----------------------------------------------------------------------
420 | NPT_NetworkInterface::NPT_NetworkInterface
421 +---------------------------------------------------------------------*/
422 NPT_NetworkInterface::NPT_NetworkInterface(const char* name,
423 NPT_Flags flags) :
424 m_Name(name),
425 m_Flags(flags)
429 /*----------------------------------------------------------------------
430 | NPT_NetworkInterface::AddAddress
431 +---------------------------------------------------------------------*/
432 NPT_Result
433 NPT_NetworkInterface::AddAddress(const NPT_NetworkInterfaceAddress& address)
435 return m_Addresses.Add(address);