PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libUPnP/Platinum/Targets/universal-apple-ios/Release/Platinum.framework/Versions/A/Headers/PltSsdp.h

https://gitlab.com/jothow/kodi
C Header | 389 lines | 203 code | 47 blank | 139 comment | 3 complexity | 13b03b25970d8bfd0ac7311c1fd5364d MD5 | raw file
  1. /*****************************************************************
  2. |
  3. | Platinum - SSDP
  4. |
  5. | Copyright (c) 2004-2010, Plutinosoft, LLC.
  6. | All rights reserved.
  7. | http://www.plutinosoft.com
  8. |
  9. | This program is free software; you can redistribute it and/or
  10. | modify it under the terms of the GNU General Public License
  11. | as published by the Free Software Foundation; either version 2
  12. | of the License, or (at your option) any later version.
  13. |
  14. | OEMs, ISVs, VARs and other distributors that combine and
  15. | distribute commercially licensed software with Platinum software
  16. | and do not wish to distribute the source code for the commercially
  17. | licensed software under version 2, or (at your option) any later
  18. | version, of the GNU General Public License (the "GPL") must enter
  19. | into a commercial license agreement with Plutinosoft, LLC.
  20. | licensing@plutinosoft.com
  21. |
  22. | This program is distributed in the hope that it will be useful,
  23. | but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. | GNU General Public License for more details.
  26. |
  27. | You should have received a copy of the GNU General Public License
  28. | along with this program; see the file LICENSE.txt. If not, write to
  29. | the Free Software Foundation, Inc.,
  30. | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  31. | http://www.gnu.org/licenses/gpl-2.0.html
  32. |
  33. ****************************************************************/
  34. /** @file
  35. UPnP SSDP
  36. */
  37. #ifndef _PLT_SSDP_H_
  38. #define _PLT_SSDP_H_
  39. /*----------------------------------------------------------------------
  40. | includes
  41. +---------------------------------------------------------------------*/
  42. #include "Neptune.h"
  43. #include "PltThreadTask.h"
  44. #include "PltHttpServerTask.h"
  45. /*----------------------------------------------------------------------
  46. | forward declarations
  47. +---------------------------------------------------------------------*/
  48. class PLT_DeviceHost;
  49. /*----------------------------------------------------------------------
  50. | PLT_SsdpAnnounceType
  51. +---------------------------------------------------------------------*/
  52. typedef enum {
  53. PLT_ANNOUNCETYPE_BYEBYE,
  54. PLT_ANNOUNCETYPE_ALIVE,
  55. PLT_ANNOUNCETYPE_UPDATE
  56. } PLT_SsdpAnnounceType;
  57. /*----------------------------------------------------------------------
  58. | PLT_SsdpPacketListener class
  59. +---------------------------------------------------------------------*/
  60. /**
  61. The PLT_SsdpPacketListener class is an interface for handling SSDP packets
  62. (M-SEARCH and NOTIFY).
  63. */
  64. class PLT_SsdpPacketListener
  65. {
  66. public:
  67. virtual ~PLT_SsdpPacketListener() {}
  68. virtual NPT_Result OnSsdpPacket(const NPT_HttpRequest& request,
  69. const NPT_HttpRequestContext& context) = 0;
  70. };
  71. /*----------------------------------------------------------------------
  72. | PLT_SsdpSearchResponseListener class
  73. +---------------------------------------------------------------------*/
  74. /**
  75. The PLT_SsdpSearchResponseListener class is an interface for handling SSDP M-SEARCH
  76. responses.
  77. */
  78. class PLT_SsdpSearchResponseListener
  79. {
  80. public:
  81. virtual ~PLT_SsdpSearchResponseListener() {}
  82. virtual NPT_Result ProcessSsdpSearchResponse(NPT_Result res,
  83. const NPT_HttpRequestContext& context,
  84. NPT_HttpResponse* response) = 0;
  85. };
  86. /*----------------------------------------------------------------------
  87. | PLT_SsdpSender class
  88. +---------------------------------------------------------------------*/
  89. /**
  90. The PLT_SsdpSender class provides a mechanism to format and send SSDP packets.
  91. */
  92. class PLT_SsdpSender
  93. {
  94. public:
  95. static NPT_Result SendSsdp(NPT_HttpRequest& request,
  96. const char* usn,
  97. const char* nt,
  98. NPT_UdpSocket& socket,
  99. bool notify,
  100. const NPT_SocketAddress* addr = NULL);
  101. static NPT_Result SendSsdp(NPT_HttpResponse& response,
  102. const char* usn,
  103. const char* nt,
  104. NPT_UdpSocket& socket,
  105. bool notify,
  106. const NPT_SocketAddress* addr = NULL);
  107. private:
  108. static NPT_Result FormatPacket(NPT_HttpMessage& message,
  109. const char* usn,
  110. const char* nt,
  111. NPT_UdpSocket& socket,
  112. bool notify);
  113. };
  114. /*----------------------------------------------------------------------
  115. | PLT_SsdpDeviceSearchResponseInterfaceIterator class
  116. +---------------------------------------------------------------------*/
  117. /**
  118. The PLT_SsdpDeviceSearchResponseInterfaceIterator class looks for the best network
  119. interface to use then sends a SSDP M-SEARCH response.
  120. */
  121. class PLT_SsdpDeviceSearchResponseInterfaceIterator
  122. {
  123. public:
  124. PLT_SsdpDeviceSearchResponseInterfaceIterator(PLT_DeviceHost* device,
  125. NPT_SocketAddress remote_addr,
  126. const char* st) :
  127. m_Device(device), m_RemoteAddr(remote_addr), m_ST(st) {}
  128. virtual ~PLT_SsdpDeviceSearchResponseInterfaceIterator() {}
  129. NPT_Result operator()(NPT_NetworkInterface*& if_addr) const;
  130. private:
  131. PLT_DeviceHost* m_Device;
  132. NPT_SocketAddress m_RemoteAddr;
  133. NPT_String m_ST;
  134. };
  135. /*----------------------------------------------------------------------
  136. | PLT_SsdpDeviceSearchResponseTask class
  137. +---------------------------------------------------------------------*/
  138. /**
  139. The PLT_SsdpDeviceSearchResponseTask class is used by a PLT_DeviceHost to respond
  140. to SSDP M-SEARCH requests from UPnP ControlPoints.
  141. */
  142. class PLT_SsdpDeviceSearchResponseTask : public PLT_ThreadTask
  143. {
  144. public:
  145. PLT_SsdpDeviceSearchResponseTask(PLT_DeviceHost* device,
  146. NPT_SocketAddress remote_addr,
  147. const char* st) :
  148. m_Device(device), m_RemoteAddr(remote_addr), m_ST(st) {}
  149. protected:
  150. virtual ~PLT_SsdpDeviceSearchResponseTask() {}
  151. // PLT_ThreadTask methods
  152. virtual void DoRun();
  153. protected:
  154. PLT_DeviceHost* m_Device;
  155. NPT_SocketAddress m_RemoteAddr;
  156. NPT_String m_ST;
  157. };
  158. /*----------------------------------------------------------------------
  159. | PLT_SsdpAnnounceInterfaceIterator class
  160. +---------------------------------------------------------------------*/
  161. /**
  162. The PLT_SsdpAnnounceInterfaceIterator class is used to send SSDP announcements
  163. given a list of network interaces.
  164. */
  165. class PLT_SsdpAnnounceInterfaceIterator
  166. {
  167. public:
  168. PLT_SsdpAnnounceInterfaceIterator(PLT_DeviceHost* device, PLT_SsdpAnnounceType type, bool broadcast = false) :
  169. m_Device(device), m_Type(type), m_Broadcast(broadcast) {}
  170. NPT_Result operator()(NPT_NetworkInterface*& if_addr) const;
  171. private:
  172. PLT_DeviceHost* m_Device;
  173. PLT_SsdpAnnounceType m_Type;
  174. bool m_Broadcast;
  175. };
  176. /*----------------------------------------------------------------------
  177. | PLT_SsdpInitMulticastIterator class
  178. +---------------------------------------------------------------------*/
  179. /**
  180. The PLT_SsdpInitMulticastIterator class is used to join a multicast group
  181. given a list of IP addresses.
  182. */
  183. class PLT_SsdpInitMulticastIterator
  184. {
  185. public:
  186. PLT_SsdpInitMulticastIterator(NPT_UdpMulticastSocket* socket) :
  187. m_Socket(socket) {}
  188. NPT_Result operator()(NPT_IpAddress& if_addr) const {
  189. NPT_IpAddress addr;
  190. addr.ResolveName("239.255.255.250");
  191. // OSX bug, since we're reusing the socket, we need to leave group first
  192. // before joining it
  193. m_Socket->LeaveGroup(addr, if_addr);
  194. return m_Socket->JoinGroup(addr, if_addr);
  195. }
  196. private:
  197. NPT_UdpMulticastSocket* m_Socket;
  198. };
  199. /*----------------------------------------------------------------------
  200. | PLT_SsdpDeviceAnnounceTask class
  201. +---------------------------------------------------------------------*/
  202. /**
  203. The PLT_SsdpDeviceAnnounceTask class is a task to send UPnP Device SSDP announcements
  204. (alive or byebye). It can be setup to automatically repeat after an interval.
  205. */
  206. class PLT_SsdpDeviceAnnounceTask : public PLT_ThreadTask
  207. {
  208. public:
  209. PLT_SsdpDeviceAnnounceTask(PLT_DeviceHost* device,
  210. NPT_TimeInterval repeat,
  211. bool is_byebye_first = false,
  212. bool extra_broadcast = false) :
  213. m_Device(device),
  214. m_Repeat(repeat),
  215. m_IsByeByeFirst(is_byebye_first),
  216. m_ExtraBroadcast(extra_broadcast) {}
  217. protected:
  218. virtual ~PLT_SsdpDeviceAnnounceTask() {}
  219. // PLT_ThreadTask methods
  220. virtual void DoRun();
  221. protected:
  222. PLT_DeviceHost* m_Device;
  223. NPT_TimeInterval m_Repeat;
  224. bool m_IsByeByeFirst;
  225. bool m_ExtraBroadcast;
  226. };
  227. /*----------------------------------------------------------------------
  228. | PLT_NetworkInterfaceAddressSearchIterator class
  229. +---------------------------------------------------------------------*/
  230. /**
  231. The PLT_NetworkInterfaceAddressSearchIterator class returns the network interface
  232. given an IP address.
  233. */
  234. class PLT_NetworkInterfaceAddressSearchIterator
  235. {
  236. public:
  237. PLT_NetworkInterfaceAddressSearchIterator(NPT_String ip) : m_Ip(ip) {}
  238. virtual ~PLT_NetworkInterfaceAddressSearchIterator() {}
  239. NPT_Result operator()(NPT_NetworkInterface*& addr) const {
  240. NPT_List<NPT_NetworkInterfaceAddress>::Iterator niaddr = addr->GetAddresses().GetFirstItem();
  241. if (!niaddr) return NPT_FAILURE;
  242. return (m_Ip.Compare((*niaddr).GetPrimaryAddress().ToString(), true) == 0) ? NPT_SUCCESS : NPT_FAILURE;
  243. }
  244. private:
  245. NPT_String m_Ip;
  246. };
  247. /*----------------------------------------------------------------------
  248. | PLT_SsdpPacketListenerIterator class
  249. +---------------------------------------------------------------------*/
  250. /**
  251. The PLT_SsdpPacketListenerIterator class iterates through a list of
  252. PLT_SsdpPacketListener instances to notify of a new SSDP incoming packet.
  253. */
  254. class PLT_SsdpPacketListenerIterator
  255. {
  256. public:
  257. PLT_SsdpPacketListenerIterator(NPT_HttpRequest& request,
  258. const NPT_HttpRequestContext& context) :
  259. m_Request(request), m_Context(context) {}
  260. NPT_Result operator()(PLT_SsdpPacketListener*& listener) const {
  261. return listener->OnSsdpPacket(m_Request, m_Context);
  262. }
  263. private:
  264. NPT_HttpRequest& m_Request;
  265. const NPT_HttpRequestContext& m_Context;
  266. };
  267. /*----------------------------------------------------------------------
  268. | PLT_SsdpListenTask class
  269. +---------------------------------------------------------------------*/
  270. /**
  271. The PLT_SsdpListenTask class is used to listen for incoming SSDP packets and
  272. keep track of a list of PLT_SsdpPacketListener listeners to notify when a new
  273. SSDP packet has arrived.
  274. */
  275. class PLT_SsdpListenTask : public PLT_HttpServerSocketTask
  276. {
  277. public:
  278. PLT_SsdpListenTask(NPT_Socket* socket) :
  279. PLT_HttpServerSocketTask(socket, true) {
  280. // Change read time out for UDP because iPhone 3.0 seems to hang
  281. // after reading everything from the socket even though
  282. // more stuff arrived
  283. #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
  284. m_Socket->SetReadTimeout(10000);
  285. #endif
  286. }
  287. NPT_Result AddListener(PLT_SsdpPacketListener* listener) {
  288. NPT_AutoLock lock(m_Mutex);
  289. m_Listeners.Add(listener);
  290. return NPT_SUCCESS;
  291. }
  292. NPT_Result RemoveListener(PLT_SsdpPacketListener* listener) {
  293. NPT_AutoLock lock(m_Mutex);
  294. m_Listeners.Remove(listener);
  295. return NPT_SUCCESS;
  296. }
  297. // PLT_Task methods
  298. void DoAbort();
  299. protected:
  300. virtual ~PLT_SsdpListenTask() {}
  301. // PLT_HttpServerSocketTask methods
  302. NPT_Result GetInputStream(NPT_InputStreamReference& stream);
  303. NPT_Result GetInfo(NPT_SocketInfo& info);
  304. NPT_Result SetupResponse(NPT_HttpRequest& request,
  305. const NPT_HttpRequestContext& context,
  306. NPT_HttpResponse& response);
  307. protected:
  308. PLT_InputDatagramStreamReference m_Datagram;
  309. NPT_List<PLT_SsdpPacketListener*> m_Listeners;
  310. NPT_Mutex m_Mutex;
  311. };
  312. /*----------------------------------------------------------------------
  313. | PLT_SsdpSearchTask class
  314. +---------------------------------------------------------------------*/
  315. /**
  316. The PLT_SsdpSearchTask class is a task used by a PLT_CtrlPoint to issue a SSDP
  317. M-SEARCH request. It can be set to repeat at a certain frequencey.
  318. */
  319. class PLT_SsdpSearchTask : public PLT_ThreadTask
  320. {
  321. public:
  322. PLT_SsdpSearchTask(NPT_UdpSocket* socket,
  323. PLT_SsdpSearchResponseListener* listener,
  324. NPT_HttpRequest* request,
  325. NPT_TimeInterval frequency = NPT_TimeInterval(0.)); // pass 0 for one time
  326. protected:
  327. virtual ~PLT_SsdpSearchTask();
  328. // PLT_ThreadTask methods
  329. virtual void DoAbort();
  330. virtual void DoRun();
  331. virtual NPT_Result ProcessResponse(NPT_Result res,
  332. const NPT_HttpRequest& request,
  333. const NPT_HttpRequestContext& context,
  334. NPT_HttpResponse* response);
  335. private:
  336. PLT_SsdpSearchResponseListener* m_Listener;
  337. NPT_HttpRequest* m_Request;
  338. NPT_TimeInterval m_Frequency;
  339. bool m_Repeat;
  340. NPT_UdpSocket* m_Socket;
  341. };
  342. #endif /* _PLT_SSDP_H_ */