1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
|
/*****************************************************************
|
| Platinum - Utilities
|
| Copyright (c) 2004-2010, Plutinosoft, LLC.
| All rights reserved.
| http://www.plutinosoft.com
|
| This program is free software; you can redistribute it and/or
| modify it under the terms of the GNU General Public License
| as published by the Free Software Foundation; either version 2
| of the License, or (at your option) any later version.
|
| OEMs, ISVs, VARs and other distributors that combine and
| distribute commercially licensed software with Platinum software
| and do not wish to distribute the source code for the commercially
| licensed software under version 2, or (at your option) any later
| version, of the GNU General Public License (the "GPL") must enter
| into a commercial license agreement with Plutinosoft, LLC.
| licensing@plutinosoft.com
|
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; see the file LICENSE.txt. If not, write to
| the Free Software Foundation, Inc.,
| 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
| http://www.gnu.org/licenses/gpl-2.0.html
|
****************************************************************/
#ifndef _PLT_UTILITIES_H_
#define _PLT_UTILITIES_H_
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "Neptune.h"
/*----------------------------------------------------------------------
| PLT_XmlAttributeFinder
+---------------------------------------------------------------------*/
/**
The PLT_XmlAttributeFinder class is used to determine if an attribute
exists given an xml element node, an attribute name and namespace.
*/
class PLT_XmlAttributeFinder
{
public:
// if 'namespc' is NULL, we're looking for ANY namespace
// if 'namespc' is '\0', we're looking for NO namespace
// if 'namespc' is non-empty, look for that SPECIFIC namespace
PLT_XmlAttributeFinder(const NPT_XmlElementNode& element,
const char* name,
const char* namespc) :
m_Element(element), m_Name(name), m_Namespace(namespc) {}
bool operator()(const NPT_XmlAttribute* const & attribute) const {
if (attribute->GetName() == m_Name) {
if (m_Namespace) {
const NPT_String& prefix = attribute->GetPrefix();
if (m_Namespace[0] == '\0') {
// match if the attribute has NO namespace
return prefix.IsEmpty();
} else {
// match if the attribute has the SPECIFIC namespace
// we're looking for
const NPT_String* namespc = m_Element.GetNamespaceUri(prefix);
return namespc && *namespc == m_Namespace;
}
} else {
// ANY namespace will match
return true;
}
} else {
return false;
}
}
private:
const NPT_XmlElementNode& m_Element;
const char* m_Name;
const char* m_Namespace;
};
/*----------------------------------------------------------------------
| PLT_XmlHelper
+---------------------------------------------------------------------*/
/**
The PLT_XmlHelper class is a set of utility functions for manipulating
xml documents and DOM trees.
*/
class PLT_XmlHelper
{
public:
// static methods
static NPT_Result GetChildText(NPT_XmlElementNode* node,
const char* tag,
NPT_String& value,
const char* namespc = "",
NPT_Cardinal max_size = 1024) {
value = "";
if (!node) return NPT_FAILURE;
// special case "" means we look for the same namespace as the parent
if (namespc && namespc[0] == '\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
NPT_XmlElementNode* child = node->GetChild(tag, namespc);
if (!child) return NPT_FAILURE;
const NPT_String* text = child->GetText();
// DLNA 7.3.17
value = text?text->SubString(0, max_size):"";
return NPT_SUCCESS;
}
static NPT_Result RemoveAttribute(NPT_XmlElementNode* node,
const char* name,
const char* namespc = "") {
if (!node) return NPT_FAILURE;
// special case "" means we look for the same namespace as the parent
if (namespc && namespc[0] == '\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
NPT_List<NPT_XmlAttribute*>::Iterator attribute;
attribute = node->GetAttributes().Find(PLT_XmlAttributeFinder(*node, name, namespc));
if (!attribute) return NPT_FAILURE;
delete *attribute;
NPT_CHECK(node->GetAttributes().Erase(attribute));
return NPT_SUCCESS;
}
static NPT_Result GetAttribute(NPT_XmlElementNode* node,
const char* name,
NPT_XmlAttribute*& attr,
const char* namespc = "") {
attr = NULL;
if (!node) return NPT_FAILURE;
// special case "" means we look for the same namespace as the parent
if (namespc && namespc[0] == '\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
NPT_List<NPT_XmlAttribute*>::Iterator attribute;
attribute = node->GetAttributes().Find(PLT_XmlAttributeFinder(*node, name, namespc));
if (!attribute) {
//NPT_Debug("Failed to find attribute [%s]:%s", namespc, name);
return NPT_FAILURE;
}
attr = (*attribute);
return NPT_SUCCESS;
}
static NPT_Result GetAttribute(NPT_XmlElementNode* node,
const char* name,
NPT_String& value,
const char* namespc = "",
NPT_Cardinal max_size = 1024) {
value = "";
NPT_XmlAttribute* attribute = NULL;
NPT_Result result = GetAttribute(node, name, attribute, namespc);
if (NPT_FAILED(result)) return result;
if (!attribute) return NPT_FAILURE;
// DLNA 7.3.17 truncate to 1024 bytes
value = attribute->GetValue().SubString(0, max_size);
return NPT_SUCCESS;
}
static NPT_Result SetAttribute(NPT_XmlElementNode* node,
const char* name,
NPT_String& value,
const char* namespc = "") {
NPT_XmlAttribute* attribute = NULL;
NPT_CHECK(GetAttribute(node, name, attribute, namespc));
if (!attribute) return NPT_FAILURE;
attribute->SetValue(value);
return NPT_SUCCESS;
}
static NPT_Result AddChildText(NPT_XmlElementNode* node,
const char* tag,
const char* text,
const char* prefix = NULL) {
if (!node) return NPT_FAILURE;
NPT_XmlElementNode* child = new NPT_XmlElementNode(prefix, tag);
child->AddText(text);
return node->AddChild(child);
}
static bool IsMatch(const NPT_XmlNode* const & node, const char* tag, const char* namespc_mapped) {
// if m_Namespace is NULL, we're looking for ANY namespace
// if m_Namespace is '\0', we're looking for NO namespace
// if m_Namespace is non-empty, look for that SPECIFIC namespace
const NPT_XmlElementNode* element = node->AsElementNode();
// is tag the same (case sensitive)?
if (element && element->GetTag() == tag) {
if (namespc_mapped) {
// look for a SPECIFIC namespace or NO namespace
const NPT_String* namespc = element->GetNamespace();
if (namespc) {
// the element has a namespace, match if it is equal to
// what we're looking for
return *namespc == namespc_mapped;
} else {
// the element does not have a namespace, match if we're
// looking for NO namespace
return namespc_mapped[0] == '\0';
}
} else {
// ANY namespace will match
return true;
}
}
return false;
}
static NPT_Result GetChildren(NPT_XmlElementNode* node,
NPT_Array<NPT_XmlElementNode*>& children,
const char* tag,
const char* namespc = "") {
if (!node) return NPT_FAILURE;
// special case "" means we look for the same namespace as the parent
if (namespc && namespc[0] == '\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
const char* namespc_mapped = (namespc==NULL)?"":(namespc[0]=='*' && namespc[1]=='\0')?NULL:namespc;
// get all children first
NPT_List<NPT_XmlNode*>& allchildren = node->GetChildren();
// iterate through children and add only elements with matching tag
NPT_List<NPT_XmlNode*>::Iterator child = allchildren.GetFirstItem();
while (child) {
if (IsMatch(*child, tag, namespc_mapped)) {
children.Add((*child)->AsElementNode());
}
++child;
}
return NPT_SUCCESS;
}
static NPT_XmlElementNode* GetChild(NPT_XmlElementNode* node,
const char* tag,
const char* namespc = "") {
if (!node) return NULL;
// special case "" means we look for the same namespace as the parent
if (namespc && namespc[0] == '\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
return node->GetChild(tag, namespc);
}
static NPT_Result GetChild(NPT_XmlElementNode* parent,
NPT_XmlElementNode*& child,
NPT_Ordinal n = 0) {
if (!parent) return NPT_FAILURE;
// reset child
child = NULL;
// get all children first
NPT_List<NPT_XmlNode*>::Iterator children = parent->GetChildren().GetFirstItem();
while (children) {
if ((*children)->AsElementNode() && n-- == 0) {
child = (*children)->AsElementNode();
return NPT_SUCCESS;
}
children++;
}
return NPT_FAILURE;
}
static NPT_Result Serialize(NPT_XmlNode& node, NPT_String& xml, bool add_header = true, NPT_Int8 indentation = 0) {
NPT_XmlWriter writer(indentation);
NPT_StringOutputStreamReference stream(new NPT_StringOutputStream(&xml));
NPT_CHECK(writer.Serialize(node, *stream, add_header));
return NPT_SUCCESS;
}
static NPT_String Serialize(NPT_XmlNode& node, bool add_header = true, NPT_Int8 indentation = 0) {
NPT_XmlWriter writer(indentation);
NPT_String xml;
NPT_StringOutputStreamReference stream(new NPT_StringOutputStream(&xml));
if (NPT_FAILED(writer.Serialize(node, *stream, add_header))) {
NPT_Debug("Failed to serialize xml node");
return "";
}
return xml;
}
private:
// members
};
/*----------------------------------------------------------------------
| NPT_StringFinder
+---------------------------------------------------------------------*/
/**
The NPT_StringFinder class is used to determine if a string is found
as part of a list of strings.
*/
class NPT_StringFinder
{
public:
// methods
NPT_StringFinder(const char* value, bool ignore_case = false) :
m_Value(value), m_IgnoreCase(ignore_case) {}
virtual ~NPT_StringFinder() {}
bool operator()(const NPT_String* const & value) const {
return value->Compare(m_Value, m_IgnoreCase) ? false : true;
}
bool operator()(const NPT_String& value) const {
return value.Compare(m_Value, m_IgnoreCase) ? false : true;
}
private:
// members
NPT_String m_Value;
bool m_IgnoreCase;
};
/*----------------------------------------------------------------------
| NPT_IpAddressFinder
+---------------------------------------------------------------------*/
/**
The NPT_IpAddressFinder class is used to determine if a IP Address is found
as part of a list of IP Addresses.
*/
class NPT_IpAddressFinder
{
public:
// methods
NPT_IpAddressFinder(NPT_IpAddress ip) :
m_Value(ip) {}
virtual ~NPT_IpAddressFinder() {}
bool operator()(const NPT_IpAddress* const & value) const {
return *value == m_Value;
}
bool operator()(const NPT_IpAddress& value) const {
return value == m_Value;
}
private:
// members
NPT_IpAddress m_Value;
};
/*----------------------------------------------------------------------
| PLT_UPnPMessageHelper class
+---------------------------------------------------------------------*/
/**
The PLT_UPnPMessageHelper class is a set of utility functions for manipulating
specific UPnP HTTP headers.
*/
class PLT_UPnPMessageHelper
{
public:
// methods
static const NPT_String* GetST(const NPT_HttpMessage& message) {
return message.GetHeaders().GetHeaderValue("ST");
}
static NPT_Result SetST(NPT_HttpMessage& message,
const char* st) {
return message.GetHeaders().SetHeader("ST", st);
}
static const NPT_String* GetNT(const NPT_HttpMessage& message) {
return message.GetHeaders().GetHeaderValue("NT");
}
static NPT_Result SetNT(NPT_HttpMessage& message,
const char* nt) {
return message.GetHeaders().SetHeader("NT", nt);
}
static const NPT_String* GetNTS(const NPT_HttpMessage& message) {
return message.GetHeaders().GetHeaderValue("NTS");
}
static NPT_Result SetNTS(NPT_HttpMessage& message,
const char* nts) {
return message.GetHeaders().SetHeader("NTS", nts);
}
static const NPT_String* GetMAN(const NPT_HttpMessage& message) {
return message.GetHeaders().GetHeaderValue("MAN");
}
static NPT_Result SetMAN(NPT_HttpMessage& message,
const char* man) {
return message.GetHeaders().SetHeader("MAN", man);
}
static const NPT_String* GetLocation(const NPT_HttpMessage& message) {
return message.GetHeaders().GetHeaderValue("Location");
}
static NPT_Result SetLocation(NPT_HttpMessage& message,
const char* location) {
return message.GetHeaders().SetHeader("Location", location);
}
static const NPT_String* GetServer(const NPT_HttpMessage& message) {
return message.GetHeaders().GetHeaderValue(NPT_HTTP_HEADER_SERVER);
}
static NPT_Result SetServer(NPT_HttpMessage& message,
const char* server,
bool replace = true) {
return message.GetHeaders().SetHeader(
NPT_HTTP_HEADER_SERVER,
server,
replace);
}
static const NPT_String* GetUSN(const NPT_HttpMessage& message) {
return message.GetHeaders().GetHeaderValue("USN");
}
static NPT_Result SetUSN(NPT_HttpMessage& message,
const char* usn) {
return message.GetHeaders().SetHeader("USN", usn);
}
static const NPT_String* GetCallbacks(const NPT_HttpMessage& message) {
return message.GetHeaders().GetHeaderValue("CALLBACK");
}
static NPT_Result SetCallbacks(NPT_HttpMessage& message,
const char* callbacks) {
return message.GetHeaders().SetHeader("CALLBACK", callbacks);
}
static const NPT_String* GetSID(const NPT_HttpMessage& message) {
return message.GetHeaders().GetHeaderValue("SID");
}
static NPT_Result SetSID(NPT_HttpMessage& message,
const char* sid) {
return message.GetHeaders().SetHeader("SID", sid);
}
static NPT_Result GetLeaseTime(const NPT_HttpMessage& message,
NPT_TimeInterval& lease) {
const NPT_String* cc =
message.GetHeaders().GetHeaderValue("Cache-Control");
NPT_CHECK_POINTER(cc);
return ExtractLeaseTime(*cc, lease);
}
static NPT_Result SetLeaseTime(NPT_HttpMessage& message,
const NPT_TimeInterval& lease) {
return message.GetHeaders().SetHeader(
"Cache-Control",
"max-age="+NPT_String::FromInteger(lease.ToSeconds()));
}
static NPT_Result GetTimeOut(const NPT_HttpMessage& message,
NPT_Int32& seconds) {
seconds = 0;
const NPT_String* timeout =
message.GetHeaders().GetHeaderValue("TIMEOUT");
NPT_CHECK_POINTER(timeout);
return ExtractTimeOut(*timeout, seconds);
}
static NPT_Result SetTimeOut(NPT_HttpMessage& message,
const NPT_Int32 seconds) {
if (seconds >= 0) {
return message.GetHeaders().SetHeader(
"TIMEOUT",
"Second-"+NPT_String::FromInteger(seconds));
} else {
return message.GetHeaders().SetHeader(
"TIMEOUT",
"Second-infinite");
}
}
static NPT_Result SetDate(NPT_HttpMessage& message) {
NPT_TimeStamp now;
NPT_System::GetCurrentTimeStamp(now);
NPT_DateTime date(now);
return message.GetHeaders().SetHeader("Date", date.ToString(NPT_DateTime::FORMAT_RFC_1123));
}
static NPT_Result GetIfModifiedSince(const NPT_HttpMessage& message,
NPT_DateTime& date) {
const NPT_String* value =
message.GetHeaders().GetHeaderValue("If-Modified-Since");
if (!value) return NPT_FAILURE;
// Try RFC 1123, RFC 1036, then ANSI
if (NPT_SUCCEEDED(date.FromString(*value, NPT_DateTime::FORMAT_RFC_1123)))
return NPT_SUCCESS;
if (NPT_SUCCEEDED(date.FromString(*value, NPT_DateTime::FORMAT_RFC_1036)))
return NPT_SUCCESS;
return date.FromString(*value, NPT_DateTime::FORMAT_ANSI);
}
static NPT_Result SetIfModifiedSince(NPT_HttpMessage& message,
const NPT_DateTime& date) {
return message.GetHeaders().SetHeader(
"If-Modified-Since",
date.ToString(NPT_DateTime::FORMAT_RFC_1123));
}
static NPT_Result GetMX(const NPT_HttpMessage& message,
NPT_UInt32& value) {
value = 0;
const NPT_String* mx =
message.GetHeaders().GetHeaderValue("MX");
NPT_CHECK_POINTER(mx);
return NPT_ParseInteger32(*mx, value, false); // no relax to be UPnP compliant
}
static NPT_Result SetMX(NPT_HttpMessage& message,
const NPT_UInt32 mx) {
return message.GetHeaders().SetHeader(
"MX",
NPT_String::FromInteger(mx));
}
static NPT_Result GetSeq(const NPT_HttpMessage& message,
NPT_UInt32& value) {
value = 0;
const NPT_String* seq =
message.GetHeaders().GetHeaderValue("SEQ");
NPT_CHECK_POINTER(seq);
return NPT_ParseInteger32(*seq, value);
}
static NPT_Result SetSeq(NPT_HttpMessage& message,
const NPT_UInt32 seq) {
return message.GetHeaders().SetHeader(
"SEQ",
NPT_String::FromInteger(seq));
}
static const char* GenerateUUID(int count,
NPT_String& uuid) {
uuid = "";
for (int i=0;i<(count<100?count:100);i++) {
int random = NPT_System::GetRandomInteger();
uuid += (char)((random % 25) + 66);
}
return uuid;
}
static const char* GenerateSerialNumber(NPT_String& sn, int count = 40) {
sn = "{";
for (int i=0;i<count;i++) {
char nibble = (char)(NPT_System::GetRandomInteger() % 16);
sn += (nibble < 10) ? ('0' + nibble) : ('a' + (nibble-10));
}
sn += "}";
return sn;
}
static const char* GenerateGUID(NPT_String& guid) {
guid = "";
for (int i=0;i<32;i++) {
char nibble = (char)(NPT_System::GetRandomInteger() % 16);
guid += (nibble < 10) ? ('0' + nibble) : ('a' + (nibble-10));
if (i == 7 || i == 11 || i == 15 || i == 19) {
guid += '-';
}
}
return guid;
}
static NPT_Result ExtractLeaseTime(const NPT_String& cache_control,
NPT_TimeInterval& lease) {
NPT_Int32 value;
if (cache_control.StartsWith("max-age=", true) &&
NPT_SUCCEEDED(NPT_ParseInteger32(cache_control.GetChars()+8,
value))) {
lease.SetSeconds(value);
return NPT_SUCCESS;
}
return NPT_FAILURE;
}
static NPT_Result ExtractTimeOut(const char* timeout,
NPT_Int32& len) {
NPT_String temp = timeout;
if (temp.CompareN("Second-", 7, true)) {
return NPT_ERROR_INVALID_FORMAT;
}
if (temp.Compare("Second-infinite", true) == 0) {
len = NPT_TIMEOUT_INFINITE;
return NPT_SUCCESS;
}
return temp.SubString(7).ToInteger(len);
}
static NPT_Result GetIPAddresses(NPT_List<NPT_IpAddress>& ips,
bool with_localhost = false) {
NPT_List<NPT_NetworkInterface*> if_list;
NPT_CHECK(GetNetworkInterfaces(if_list, with_localhost));
NPT_List<NPT_NetworkInterface*>::Iterator iface = if_list.GetFirstItem();
while (iface) {
NPT_IpAddress ip = (*(*iface)->GetAddresses().GetFirstItem()).GetPrimaryAddress();
if (ip.ToString().Compare("0.0.0.0") &&
(with_localhost || ip.ToString().Compare("127.0.0.1"))) {
ips.Add(ip);
}
++iface;
}
if (with_localhost && !ips.Find(NPT_IpAddressFinder(NPT_IpAddress(127, 0, 0, 1)))) {
NPT_IpAddress localhost;
localhost.Parse("127.0.0.1");
ips.Add(localhost);
}
if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>());
return NPT_SUCCESS;
}
static NPT_Result GetNetworkInterfaces(NPT_List<NPT_NetworkInterface*>& if_list,
bool with_localhost = false) {
NPT_CHECK(_GetNetworkInterfaces(if_list, false));
// if no valid interfaces or if requested, add localhost interface
if (if_list.GetItemCount() == 0 || with_localhost) {
NPT_CHECK(_GetNetworkInterfaces(if_list, true));
}
return NPT_SUCCESS;
}
static NPT_Result GetMACAddresses(NPT_List<NPT_String>& addresses) {
NPT_List<NPT_NetworkInterface*> if_list;
NPT_CHECK(GetNetworkInterfaces(if_list));
NPT_List<NPT_NetworkInterface*>::Iterator iface = if_list.GetFirstItem();
while (iface) {
NPT_String ip = (*(*iface)->GetAddresses().GetFirstItem()).GetPrimaryAddress().ToString();
if (ip.Compare("0.0.0.0") && ip.Compare("127.0.0.1")) {
addresses.Add((*iface)->GetMacAddress().ToString());
}
++iface;
}
if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>());
return NPT_SUCCESS;
}
static bool IsLocalNetworkAddress(const NPT_IpAddress& address) {
if (address.ToString() == "127.0.0.1") return true;
NPT_List<NPT_NetworkInterface*> if_list;
NPT_NetworkInterface::GetNetworkInterfaces(if_list);
NPT_List<NPT_NetworkInterface*>::Iterator iface = if_list.GetFirstItem();
while (iface) {
if((*iface)->IsAddressInNetwork(address)) return true;
++iface;
}
if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>());
return false;
}
private:
static NPT_Result _GetNetworkInterfaces(NPT_List<NPT_NetworkInterface*>& if_list,
bool only_localhost = false) {
NPT_List<NPT_NetworkInterface*> _if_list;
NPT_CHECK(NPT_NetworkInterface::GetNetworkInterfaces(_if_list));
NPT_NetworkInterface* iface;
while (NPT_SUCCEEDED(_if_list.PopHead(iface))) {
// only interested in non PTP & multicast capable interfaces
if ((iface->GetAddresses().GetItemCount() == 0)||
(!(iface->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_MULTICAST)) ||
(iface->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_POINT_TO_POINT)) {
delete iface;
continue;
}
NPT_String ip = iface->GetAddresses().GetFirstItem()->GetPrimaryAddress().ToString();
if (only_localhost && (iface->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_LOOPBACK)) {
if_list.Add(iface);
break;
} else if (ip.Compare("0.0.0.0")) {
if_list.Add(iface);
} else {
delete iface;
}
}
// cleanup any remaining items in list if we breaked early
_if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>());
return NPT_SUCCESS;
}
};
#endif // _PLT_UTILITIES_H_
|