2 // This file is part of the aMule Project.
4 // Copyright (c) 2004-2011 Marcelo Roberto Jimenez ( phoenix@amule.org )
5 // Copyright (c) 2006-2011 aMule Team ( admin@amule.org / http://www.amule.org )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 // This define must not conflict with the one in the standard header
37 #include "UPnPCompatibility.h"
39 extern std::string stdEmptyString
;
42 class CUPnPPortMapping
46 std::string m_protocol
;
47 std::string m_enabled
;
48 std::string m_description
;
54 const std::string
&protocol
= stdEmptyString
,
56 const std::string
&description
= stdEmptyString
);
57 ~CUPnPPortMapping() {}
59 const std::string
&getPort() const
61 const std::string
&getProtocol() const
62 { return m_protocol
; }
63 const std::string
&getEnabled() const
65 const std::string
&getDescription() const
66 { return m_description
; }
67 const std::string
&getKey() const
75 IXML_Element
*GetRootElement(IXML_Document
*doc
);
76 void Free(IXML_Document
*doc
);
80 IXML_Element
*GetFirstChild(IXML_Element
*parent
);
81 IXML_Element
*GetNextSibling(IXML_Element
*child
);
82 const DOMString
GetTag(IXML_Element
*element
);
83 const std::string
GetTextValue(IXML_Element
*element
);
84 const std::string
GetChildValueByTag(IXML_Element
*element
, const DOMString tag
);
85 IXML_Element
*GetFirstChildByTag(IXML_Element
*element
, const DOMString tag
);
86 IXML_Element
*GetNextSiblingByTag(IXML_Element
*element
, const DOMString tag
);
87 const std::string
GetAttributeByTag(IXML_Element
*element
, const DOMString tag
);
92 class CUPnPControlPoint
;
94 template <typename T
, char const *XML_ELEMENT_NAME
, char const *XML_LIST_NAME
>
95 class CXML_List
: public std::map
<const std::string
, T
*>
99 const CUPnPControlPoint
&upnpControlPoint
,
100 IXML_Element
*parent
,
101 const std::string
&url
);
106 template <typename T
, char const *XML_ELEMENT_NAME
, char const *XML_LIST_NAME
>
107 CXML_List
<T
, XML_ELEMENT_NAME
, XML_LIST_NAME
>::CXML_List(
108 const CUPnPControlPoint
&upnpControlPoint
,
109 IXML_Element
*parent
,
110 const std::string
&url
)
112 IXML_Element
*elementList
= IXML::Element::GetFirstChildByTag(parent
, XML_LIST_NAME
);
114 for ( IXML_Element
*element
= IXML::Element::GetFirstChildByTag(elementList
, XML_ELEMENT_NAME
);
116 element
= IXML::Element::GetNextSiblingByTag(element
, XML_ELEMENT_NAME
)) {
117 // Add a new element to the element list
118 T
*upnpElement
= new T(upnpControlPoint
, element
, url
);
119 (*this)[upnpElement
->GetKey()] = upnpElement
;
122 std::ostringstream msg
;
123 msg
<< "\n " << XML_LIST_NAME
<< ": " <<
124 i
<< " " << XML_ELEMENT_NAME
<< "s.";
125 AddDebugLogLineN(logUPnP
, msg
);
129 template <typename T
, char const *XML_ELEMENT_NAME
, char const *XML_LIST_NAME
>
130 CXML_List
<T
, XML_ELEMENT_NAME
, XML_LIST_NAME
>::~CXML_List()
132 typename CXML_List
<T
, XML_ELEMENT_NAME
, XML_LIST_NAME
>::iterator it
;
133 for(it
= this->begin(); it
!= this->end(); ++it
) {
138 extern const char s_argument
[];
139 extern const char s_argumentList
[];
140 extern const char s_action
[];
141 extern const char s_actionList
[];
142 extern const char s_allowedValue
[];
143 extern const char s_allowedValueList
[];
144 extern const char s_stateVariable
[];
145 extern const char s_serviceStateTable
[];
146 extern const char s_service
[];
147 extern const char s_serviceList
[];
148 extern const char s_device
[];
149 extern const char s_deviceList
[];
154 typedef CXML_List
<CUPnPArgument
, s_argument
, s_argumentList
> ArgumentList
;
156 typedef CXML_List
<CUPnPAction
, s_action
, s_actionList
> ActionList
;
157 class CUPnPStateVariable
;
158 typedef CXML_List
<CUPnPStateVariable
, s_stateVariable
, s_serviceStateTable
> ServiceStateTable
;
159 class CUPnPAllowedValue
;
160 typedef CXML_List
<CUPnPAllowedValue
, s_allowedValue
, s_allowedValueList
> AllowedValueList
;
162 typedef CXML_List
<CUPnPService
, s_service
, s_serviceList
> ServiceList
;
164 typedef CXML_List
<CUPnPDevice
, s_device
, s_deviceList
> DeviceList
;
170 IXML_Element
*m_root
;
171 const std::string m_ErrorCode
;
172 const std::string m_ErrorDescription
;
174 CUPnPError(IXML_Document
*errorDoc
);
176 const std::string
&getErrorCode() const
177 { return m_ErrorCode
; }
178 const std::string
&getErrorDescription() const
179 { return m_ErrorDescription
; }
186 const std::string m_name
;
187 const std::string m_direction
;
189 const std::string m_relatedStateVariable
;
193 const CUPnPControlPoint
&upnpControlPoint
,
194 IXML_Element
*argument
,
195 const std::string
&SCPDURL
);
197 const std::string
&GetName() const
199 const std::string
&GetDirection() const
200 { return m_direction
; }
201 bool GetRetVal() const
203 const std::string
&GetRelatedStateVariable() const
204 { return m_relatedStateVariable
; }
205 const std::string
&GetKey() const
214 ArgumentList m_ArgumentList
;
215 const std::string m_name
;
219 const CUPnPControlPoint
&upnpControlPoint
,
220 IXML_Element
*action
,
221 const std::string
&SCPDURL
);
223 const std::string
&GetName() const
225 const std::string
&GetKey() const
227 const ArgumentList
&GetArgumentList() const
228 { return m_ArgumentList
; }
232 class CUPnPAllowedValue
235 const std::string m_allowedValue
;
239 const CUPnPControlPoint
&upnpControlPoint
,
240 IXML_Element
*allowedValue
,
241 const std::string
&SCPDURL
);
242 ~CUPnPAllowedValue() {}
243 const std::string
&GetAllowedValue() const
244 { return m_allowedValue
; }
245 const std::string
&GetKey() const
246 { return m_allowedValue
; }
250 class CUPnPStateVariable
253 AllowedValueList m_AllowedValueList
;
254 const std::string m_name
;
255 const std::string m_dataType
;
256 const std::string m_defaultValue
;
257 const std::string m_sendEvents
;
261 const CUPnPControlPoint
&upnpControlPoint
,
262 IXML_Element
*stateVariable
,
263 const std::string
&URLBase
);
264 ~CUPnPStateVariable() {}
265 const std::string
&GetNname() const
267 const std::string
&GetDataType() const
268 { return m_dataType
; }
269 const std::string
&GetDefaultValue() const
270 { return m_defaultValue
; }
271 const std::string
&GetKey() const
273 const AllowedValueList
&GetAllowedValueList() const
274 { return m_AllowedValueList
; }
281 ActionList m_ActionList
;
282 ServiceStateTable m_ServiceStateTable
;
283 const std::string m_SCPDURL
;
287 const CUPnPControlPoint
&upnpControlPoint
,
289 const std::string
&SCPDURL
);
291 const ActionList
&GetActionList() const
292 { return m_ActionList
; }
293 const ServiceStateTable
&GetServiceStateTable() const
294 { return m_ServiceStateTable
; }
298 class CUPnPArgumentValue
301 std::string m_argument
;
305 CUPnPArgumentValue();
306 CUPnPArgumentValue(const std::string
&argument
, const std::string
&value
);
307 ~CUPnPArgumentValue() {}
309 const std::string
&GetArgument() const { return m_argument
; }
310 const std::string
&GetValue() const { return m_value
; }
311 const std::string
&SetArgument(const std::string
& argument
) { return m_argument
= argument
; }
312 const std::string
&SetValue(const std::string
&value
) { return m_value
= value
; }
319 const CUPnPControlPoint
&m_UPnPControlPoint
;
320 const std::string m_serviceType
;
321 const std::string m_serviceId
;
322 const std::string m_SCPDURL
;
323 const std::string m_controlURL
;
324 const std::string m_eventSubURL
;
325 std::string m_absSCPDURL
;
326 std::string m_absControlURL
;
327 std::string m_absEventSubURL
;
330 std::auto_ptr
<CUPnPSCPD
> m_SCPD
;
334 const CUPnPControlPoint
&upnpControlPoint
,
335 IXML_Element
*service
,
336 const std::string
&URLBase
);
339 const std::string
&GetServiceType() const
340 { return m_serviceType
; }
341 const std::string
&GetServiceId() const
342 { return m_serviceId
; }
343 const std::string
&GetSCPDURL() const
344 { return m_SCPDURL
; }
345 const std::string
&GetAbsSCPDURL() const
346 { return m_absSCPDURL
; }
347 const std::string
&GetControlURL() const
348 { return m_controlURL
; }
349 const std::string
&GetEventSubURL() const
350 { return m_eventSubURL
; }
351 const std::string
&GetAbsControlURL() const
352 { return m_absControlURL
; }
353 const std::string
&GetAbsEventSubURL() const
354 { return m_absEventSubURL
; }
355 int GetTimeout() const
356 { return m_timeout
; }
357 void SetTimeout(int t
)
359 int *GetTimeoutAddr()
360 { return &m_timeout
; }
363 void SetSID(const char *s
)
364 { memcpy(m_SID
, s
, sizeof(Upnp_SID
)); }
365 const std::string
&GetKey() const
366 { return m_serviceId
; }
367 bool IsSubscribed() const
368 { return m_SCPD
.get() != NULL
; }
369 void SetSCPD(CUPnPSCPD
*SCPD
)
370 { m_SCPD
.reset(SCPD
); }
373 const std::string
&ActionName
,
374 const std::vector
<CUPnPArgumentValue
> &ArgValue
) const;
375 const std::string
GetStateVariable(
376 const std::string
&stateVariableName
) const;
383 // Please, lock these lists before use
384 DeviceList m_DeviceList
;
385 ServiceList m_ServiceList
;
387 const std::string m_deviceType
;
388 const std::string m_friendlyName
;
389 const std::string m_manufacturer
;
390 const std::string m_manufacturerURL
;
391 const std::string m_modelDescription
;
392 const std::string m_modelName
;
393 const std::string m_modelNumber
;
394 const std::string m_modelURL
;
395 const std::string m_serialNumber
;
396 const std::string m_UDN
;
397 const std::string m_UPC
;
398 std::string m_presentationURL
;
402 const CUPnPControlPoint
&upnpControlPoint
,
403 IXML_Element
*device
,
404 const std::string
&URLBase
);
407 const std::string
&GetUDN() const
409 const std::string
&GetDeviceType() const
410 { return m_deviceType
; }
411 const std::string
&GetFriendlyName() const
412 { return m_friendlyName
; }
413 const std::string
&GetPresentationURL() const
414 { return m_presentationURL
; }
415 const std::string
&GetKey() const
420 class CUPnPRootDevice
: public CUPnPDevice
423 const std::string m_URLBase
;
424 const std::string m_location
;
429 const CUPnPControlPoint
&upnpControlPoint
,
430 IXML_Element
*rootDevice
,
431 const std::string
&OriginalURLBase
,
432 const std::string
&FixedURLBase
,
433 const char *location
,
435 ~CUPnPRootDevice() {}
437 const std::string
&GetURLBase() const
438 { return m_URLBase
; }
439 const std::string
&GetLocation() const
440 { return m_location
; }
441 int GetExpires() const
442 { return m_expires
; }
443 void SetExpires(int expires
)
444 { m_expires
= expires
; }
448 typedef std::map
<const std::string
, CUPnPRootDevice
*> RootDeviceMap
;
449 typedef std::map
<const std::string
, CUPnPService
*> ServiceMap
;
450 typedef std::map
<const std::string
, CUPnPPortMapping
> PortMappingMap
;
453 class CUPnPControlPoint
456 static CUPnPControlPoint
*s_CtrlPoint
;
458 UpnpClient_Handle m_UPnPClientHandle
;
459 RootDeviceMap m_RootDeviceMap
;
460 ServiceMap m_ServiceMap
;
461 PortMappingMap m_ActivePortMappingsMap
;
462 CUPnPMutex m_RootDeviceListMutex
;
463 bool m_IGWDeviceDetected
;
464 //#warning This variable is for testing purposes only and should disappear on release.
465 CUPnPService
*m_WanService
;
466 CUPnPMutex m_WaitForSearchTimeoutMutex
;
469 CUPnPControlPoint(unsigned short udpPort
);
470 ~CUPnPControlPoint();
471 void Subscribe(CUPnPService
&service
);
472 void Unsubscribe(CUPnPService
&service
);
473 bool AddPortMappings(
474 std::vector
<CUPnPPortMapping
> &upnpPortMapping
);
475 bool DeletePortMappings(
476 std::vector
<CUPnPPortMapping
> &upnpPortMapping
);
478 UpnpClient_Handle
GetUPnPClientHandle() const
479 { return m_UPnPClientHandle
; }
481 bool GetIGWDeviceDetected() const
482 { return m_IGWDeviceDetected
; }
483 void SetIGWDeviceDetected(bool b
)
484 { m_IGWDeviceDetected
= b
; }
485 bool WanServiceDetected() const
486 { return !m_ServiceMap
.empty(); }
487 void SetWanService(CUPnPService
*service
)
488 { m_WanService
= service
; }
492 Upnp_EventType EventType
,
497 void OnEventReceived(
498 const std::string
&Sid
,
500 IXML_Document
*ChangedVariables
);
502 IXML_Element
*rootDevice
,
503 const std::string
&urlBase
,
504 const char *location
,
506 void RemoveRootDevice(
508 void RefreshPortMappings();
509 bool PrivateAddPortMapping(
510 CUPnPPortMapping
&upnpPortMapping
);
511 bool PrivateDeletePortMapping(
512 CUPnPPortMapping
&upnpPortMapping
);
516 #endif /* AMULE_UPNP_H */
518 // File_checked_for_headers