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
|
//
// This file is part of the aMule Project.
//
// Copyright (c) 2004-2011 Marcelo Roberto Jimenez ( phoenix@amule.org )
// Copyright (c) 2006-2011 aMule Team ( admin@amule.org / http://www.amule.org )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// 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.
//
// 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; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
// This define must not conflict with the one in the standard header
#ifndef AMULE_UPNP_H
#define AMULE_UPNP_H
#include <map>
#include <string>
#include <sstream>
#include <memory>
#include "UPnPCompatibility.h"
extern std::string stdEmptyString;
class CUPnPPortMapping
{
private:
std::string m_port;
std::string m_protocol;
std::string m_enabled;
std::string m_description;
std::string m_key;
public:
CUPnPPortMapping(
int port = 0,
const std::string &protocol = stdEmptyString,
bool enabled = false,
const std::string &description = stdEmptyString);
~CUPnPPortMapping() {}
const std::string &getPort() const
{ return m_port; }
const std::string &getProtocol() const
{ return m_protocol; }
const std::string &getEnabled() const
{ return m_enabled; }
const std::string &getDescription() const
{ return m_description; }
const std::string &getKey() const
{ return m_key; }
};
namespace IXML
{
namespace Document {
IXML_Element *GetRootElement(IXML_Document *doc);
void Free(IXML_Document *doc);
}
namespace Element {
IXML_Element *GetFirstChild(IXML_Element *parent);
IXML_Element *GetNextSibling(IXML_Element *child);
const DOMString GetTag(IXML_Element *element);
const std::string GetTextValue(IXML_Element *element);
const std::string GetChildValueByTag(IXML_Element *element, const DOMString tag);
IXML_Element *GetFirstChildByTag(IXML_Element *element, const DOMString tag);
IXML_Element *GetNextSiblingByTag(IXML_Element *element, const DOMString tag);
const std::string GetAttributeByTag(IXML_Element *element, const DOMString tag);
}
}
class CUPnPControlPoint;
template <typename T, char const *XML_ELEMENT_NAME, char const *XML_LIST_NAME>
class CXML_List : public std::map<const std::string, T *>
{
public:
CXML_List(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *parent,
const std::string &url);
~CXML_List();
};
template <typename T, char const *XML_ELEMENT_NAME, char const *XML_LIST_NAME>
CXML_List<T, XML_ELEMENT_NAME, XML_LIST_NAME>::CXML_List(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *parent,
const std::string &url)
{
IXML_Element *elementList = IXML::Element::GetFirstChildByTag(parent, XML_LIST_NAME);
unsigned int i = 0;
for ( IXML_Element *element = IXML::Element::GetFirstChildByTag(elementList, XML_ELEMENT_NAME);
element;
element = IXML::Element::GetNextSiblingByTag(element, XML_ELEMENT_NAME)) {
// Add a new element to the element list
T *upnpElement = new T(upnpControlPoint, element, url);
(*this)[upnpElement->GetKey()] = upnpElement;
++i;
}
std::ostringstream msg;
msg << "\n " << XML_LIST_NAME << ": " <<
i << " " << XML_ELEMENT_NAME << "s.";
AddDebugLogLineN(logUPnP, msg);
}
template <typename T, char const *XML_ELEMENT_NAME, char const *XML_LIST_NAME>
CXML_List<T, XML_ELEMENT_NAME, XML_LIST_NAME>::~CXML_List()
{
typename CXML_List<T, XML_ELEMENT_NAME, XML_LIST_NAME>::iterator it;
for(it = this->begin(); it != this->end(); ++it) {
delete (*it).second;
}
}
extern const char s_argument[];
extern const char s_argumentList[];
extern const char s_action[];
extern const char s_actionList[];
extern const char s_allowedValue[];
extern const char s_allowedValueList[];
extern const char s_stateVariable[];
extern const char s_serviceStateTable[];
extern const char s_service[];
extern const char s_serviceList[];
extern const char s_device[];
extern const char s_deviceList[];
class CUPnPArgument;
typedef CXML_List<CUPnPArgument, s_argument, s_argumentList> ArgumentList;
class CUPnPAction;
typedef CXML_List<CUPnPAction, s_action, s_actionList> ActionList;
class CUPnPStateVariable;
typedef CXML_List<CUPnPStateVariable, s_stateVariable, s_serviceStateTable> ServiceStateTable;
class CUPnPAllowedValue;
typedef CXML_List<CUPnPAllowedValue, s_allowedValue, s_allowedValueList> AllowedValueList;
class CUPnPService;
typedef CXML_List<CUPnPService, s_service, s_serviceList> ServiceList;
class CUPnPDevice;
typedef CXML_List<CUPnPDevice, s_device, s_deviceList> DeviceList;
class CUPnPError
{
private:
IXML_Element *m_root;
const std::string m_ErrorCode;
const std::string m_ErrorDescription;
public:
CUPnPError(IXML_Document *errorDoc);
~CUPnPError() {}
const std::string &getErrorCode() const
{ return m_ErrorCode; }
const std::string &getErrorDescription() const
{ return m_ErrorDescription; }
};
class CUPnPArgument
{
private:
const std::string m_name;
const std::string m_direction;
bool m_retval;
const std::string m_relatedStateVariable;
public:
CUPnPArgument(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *argument,
const std::string &SCPDURL);
~CUPnPArgument() {}
const std::string &GetName() const
{ return m_name; }
const std::string &GetDirection() const
{ return m_direction; }
bool GetRetVal() const
{ return m_retval; }
const std::string &GetRelatedStateVariable() const
{ return m_relatedStateVariable; }
const std::string &GetKey() const
{ return m_name; }
};
class CUPnPAction
{
private:
ArgumentList m_ArgumentList;
const std::string m_name;
public:
CUPnPAction(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *action,
const std::string &SCPDURL);
~CUPnPAction() {}
const std::string &GetName() const
{ return m_name; }
const std::string &GetKey() const
{ return m_name; }
const ArgumentList &GetArgumentList() const
{ return m_ArgumentList; }
};
class CUPnPAllowedValue
{
private:
const std::string m_allowedValue;
public:
CUPnPAllowedValue(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *allowedValue,
const std::string &SCPDURL);
~CUPnPAllowedValue() {}
const std::string &GetAllowedValue() const
{ return m_allowedValue; }
const std::string &GetKey() const
{ return m_allowedValue; }
};
class CUPnPStateVariable
{
private:
AllowedValueList m_AllowedValueList;
const std::string m_name;
const std::string m_dataType;
const std::string m_defaultValue;
const std::string m_sendEvents;
public:
CUPnPStateVariable(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *stateVariable,
const std::string &URLBase);
~CUPnPStateVariable() {}
const std::string &GetNname() const
{ return m_name; }
const std::string &GetDataType() const
{ return m_dataType; }
const std::string &GetDefaultValue() const
{ return m_defaultValue; }
const std::string &GetKey() const
{ return m_name; }
const AllowedValueList &GetAllowedValueList() const
{ return m_AllowedValueList; }
};
class CUPnPSCPD
{
private:
ActionList m_ActionList;
ServiceStateTable m_ServiceStateTable;
const std::string m_SCPDURL;
public:
CUPnPSCPD(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *scpd,
const std::string &SCPDURL);
~CUPnPSCPD() {}
const ActionList &GetActionList() const
{ return m_ActionList; }
const ServiceStateTable &GetServiceStateTable() const
{ return m_ServiceStateTable; }
};
class CUPnPArgumentValue
{
private:
std::string m_argument;
std::string m_value;
public:
CUPnPArgumentValue();
CUPnPArgumentValue(const std::string &argument, const std::string &value);
~CUPnPArgumentValue() {}
const std::string &GetArgument() const { return m_argument; }
const std::string &GetValue() const { return m_value; }
const std::string &SetArgument(const std::string& argument) { return m_argument = argument; }
const std::string &SetValue(const std::string &value) { return m_value = value; }
};
class CUPnPService
{
private:
const CUPnPControlPoint &m_UPnPControlPoint;
const std::string m_serviceType;
const std::string m_serviceId;
const std::string m_SCPDURL;
const std::string m_controlURL;
const std::string m_eventSubURL;
std::string m_absSCPDURL;
std::string m_absControlURL;
std::string m_absEventSubURL;
int m_timeout;
Upnp_SID m_SID;
std::auto_ptr<CUPnPSCPD> m_SCPD;
public:
CUPnPService(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *service,
const std::string &URLBase);
~CUPnPService();
const std::string &GetServiceType() const
{ return m_serviceType; }
const std::string &GetServiceId() const
{ return m_serviceId; }
const std::string &GetSCPDURL() const
{ return m_SCPDURL; }
const std::string &GetAbsSCPDURL() const
{ return m_absSCPDURL; }
const std::string &GetControlURL() const
{ return m_controlURL; }
const std::string &GetEventSubURL() const
{ return m_eventSubURL; }
const std::string &GetAbsControlURL() const
{ return m_absControlURL; }
const std::string &GetAbsEventSubURL() const
{ return m_absEventSubURL; }
int GetTimeout() const
{ return m_timeout; }
void SetTimeout(int t)
{ m_timeout = t; }
int *GetTimeoutAddr()
{ return &m_timeout; }
char *GetSID()
{ return m_SID; }
void SetSID(const char *s)
{ memcpy(m_SID, s, sizeof(Upnp_SID)); }
const std::string &GetKey() const
{ return m_serviceId; }
bool IsSubscribed() const
{ return m_SCPD.get() != NULL; }
void SetSCPD(CUPnPSCPD *SCPD)
{ m_SCPD.reset(SCPD); }
bool Execute(
const std::string &ActionName,
const std::vector<CUPnPArgumentValue> &ArgValue) const;
const std::string GetStateVariable(
const std::string &stateVariableName) const;
};
class CUPnPDevice
{
private:
// Please, lock these lists before use
DeviceList m_DeviceList;
ServiceList m_ServiceList;
const std::string m_deviceType;
const std::string m_friendlyName;
const std::string m_manufacturer;
const std::string m_manufacturerURL;
const std::string m_modelDescription;
const std::string m_modelName;
const std::string m_modelNumber;
const std::string m_modelURL;
const std::string m_serialNumber;
const std::string m_UDN;
const std::string m_UPC;
std::string m_presentationURL;
public:
CUPnPDevice(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *device,
const std::string &URLBase);
~CUPnPDevice() {}
const std::string &GetUDN() const
{ return m_UDN; }
const std::string &GetDeviceType() const
{ return m_deviceType; }
const std::string &GetFriendlyName() const
{ return m_friendlyName; }
const std::string &GetPresentationURL() const
{ return m_presentationURL; }
const std::string &GetKey() const
{ return m_UDN; }
};
class CUPnPRootDevice : public CUPnPDevice
{
private:
const std::string m_URLBase;
const std::string m_location;
int m_expires;
public:
CUPnPRootDevice(
const CUPnPControlPoint &upnpControlPoint,
IXML_Element *rootDevice,
const std::string &OriginalURLBase,
const std::string &FixedURLBase,
const char *location,
int expires);
~CUPnPRootDevice() {}
const std::string &GetURLBase() const
{ return m_URLBase; }
const std::string &GetLocation() const
{ return m_location; }
int GetExpires() const
{ return m_expires; }
void SetExpires(int expires)
{ m_expires = expires; }
};
typedef std::map<const std::string, CUPnPRootDevice *> RootDeviceMap;
typedef std::map<const std::string, CUPnPService *> ServiceMap;
typedef std::map<const std::string, CUPnPPortMapping> PortMappingMap;
class CUPnPControlPoint
{
private:
static CUPnPControlPoint *s_CtrlPoint;
// upnp stuff
UpnpClient_Handle m_UPnPClientHandle;
RootDeviceMap m_RootDeviceMap;
ServiceMap m_ServiceMap;
PortMappingMap m_ActivePortMappingsMap;
CUPnPMutex m_RootDeviceListMutex;
bool m_IGWDeviceDetected;
//#warning This variable is for testing purposes only and should disappear on release.
CUPnPService *m_WanService;
CUPnPMutex m_WaitForSearchTimeoutMutex;
public:
CUPnPControlPoint(unsigned short udpPort);
~CUPnPControlPoint();
void Subscribe(CUPnPService &service);
void Unsubscribe(CUPnPService &service);
bool AddPortMappings(
std::vector<CUPnPPortMapping> &upnpPortMapping);
bool DeletePortMappings(
std::vector<CUPnPPortMapping> &upnpPortMapping);
UpnpClient_Handle GetUPnPClientHandle() const
{ return m_UPnPClientHandle; }
bool GetIGWDeviceDetected() const
{ return m_IGWDeviceDetected; }
void SetIGWDeviceDetected(bool b)
{ m_IGWDeviceDetected = b; }
bool WanServiceDetected() const
{ return !m_ServiceMap.empty(); }
void SetWanService(CUPnPService *service)
{ m_WanService = service; }
// Callback function
static int Callback(
Upnp_EventType EventType,
const void* Event,
void* Cookie);
#if UPNP_VERSION < 10800
/* in libupnp 1.6 Event is not const */
static int Callback(
Upnp_EventType EventType,
void* Event,
void* Cookie) {
return Callback(EventType, (const void *)Event, Cookie);
}
#endif
private:
void OnEventReceived(
const std::string &Sid,
int EventKey,
IXML_Document *ChangedVariables);
void AddRootDevice(
IXML_Element *rootDevice,
const std::string &urlBase,
const char *location,
int expires);
void RemoveRootDevice(
const char *udn);
void RefreshPortMappings();
bool PrivateAddPortMapping(
CUPnPPortMapping &upnpPortMapping);
bool PrivateDeletePortMapping(
CUPnPPortMapping &upnpPortMapping);
};
#endif /* AMULE_UPNP_H */
// File_checked_for_headers
|