Platinum UPnP SDK  1.0.5.13
PltSsdp.h
Go to the documentation of this 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 
39 #ifndef _PLT_SSDP_H_
40 #define _PLT_SSDP_H_
41 
42 /*----------------------------------------------------------------------
43 | includes
44 +---------------------------------------------------------------------*/
45 #include "Neptune.h"
46 #include "PltThreadTask.h"
47 #include "PltHttpServerTask.h"
48 
49 /*----------------------------------------------------------------------
50 | forward declarations
51 +---------------------------------------------------------------------*/
52 class PLT_DeviceHost;
53 
54 /*----------------------------------------------------------------------
55 | PLT_SsdpAnnounceType
56 +---------------------------------------------------------------------*/
57 typedef enum {
58  PLT_ANNOUNCETYPE_BYEBYE,
59  PLT_ANNOUNCETYPE_ALIVE,
60  PLT_ANNOUNCETYPE_UPDATE
61 } PLT_SsdpAnnounceType;
62 
63 /*----------------------------------------------------------------------
64 | PLT_SsdpPacketListener class
65 +---------------------------------------------------------------------*/
71 {
72 public:
73  virtual ~PLT_SsdpPacketListener() {}
74  virtual NPT_Result OnSsdpPacket(const NPT_HttpRequest& request,
75  const NPT_HttpRequestContext& context) = 0;
76 };
77 
78 /*----------------------------------------------------------------------
79 | PLT_SsdpSearchResponseListener class
80 +---------------------------------------------------------------------*/
86 {
87 public:
88  virtual ~PLT_SsdpSearchResponseListener() {}
89  virtual NPT_Result ProcessSsdpSearchResponse(NPT_Result res,
90  const NPT_HttpRequestContext& context,
91  NPT_HttpResponse* response) = 0;
92 };
93 
94 /*----------------------------------------------------------------------
95 | PLT_SsdpSender class
96 +---------------------------------------------------------------------*/
101 {
102 public:
103  static NPT_Result SendSsdp(NPT_HttpRequest& request,
104  const char* usn,
105  const char* nt,
106  NPT_UdpSocket& socket,
107  bool notify,
108  const NPT_SocketAddress* addr = NULL);
109 
110  static NPT_Result SendSsdp(NPT_HttpResponse& response,
111  const char* usn,
112  const char* nt,
113  NPT_UdpSocket& socket,
114  bool notify,
115  const NPT_SocketAddress* addr = NULL);
116 
117 private:
118  static NPT_Result FormatPacket(NPT_HttpMessage& message,
119  const char* usn,
120  const char* nt,
121  NPT_UdpSocket& socket,
122  bool notify);
123 };
124 
125 /*----------------------------------------------------------------------
126 | PLT_SsdpDeviceSearchResponseInterfaceIterator class
127 +---------------------------------------------------------------------*/
133 {
134 public:
136  NPT_SocketAddress remote_addr,
137  const char* st) :
138  m_Device(device), m_RemoteAddr(remote_addr), m_ST(st) {}
140 
141  NPT_Result operator()(NPT_NetworkInterface*& if_addr) const;
142 
143 private:
144  PLT_DeviceHost* m_Device;
145  NPT_SocketAddress m_RemoteAddr;
146  NPT_String m_ST;
147 };
148 
149 /*----------------------------------------------------------------------
150 | PLT_SsdpDeviceSearchResponseTask class
151 +---------------------------------------------------------------------*/
157 {
158 public:
160  NPT_SocketAddress remote_addr,
161  const char* st) :
162  m_Device(device), m_RemoteAddr(remote_addr), m_ST(st) {}
163 
164 protected:
166 
167  // PLT_ThreadTask methods
168  virtual void DoRun();
169 
170 protected:
171  PLT_DeviceHost* m_Device;
172  NPT_SocketAddress m_RemoteAddr;
173  NPT_String m_ST;
174 };
175 
176 /*----------------------------------------------------------------------
177 | PLT_SsdpAnnounceInterfaceIterator class
178 +---------------------------------------------------------------------*/
184 {
185 public:
186  PLT_SsdpAnnounceInterfaceIterator(PLT_DeviceHost* device, PLT_SsdpAnnounceType type, bool broadcast = false) :
187  m_Device(device), m_Type(type), m_Broadcast(broadcast) {}
188 
189  NPT_Result operator()(NPT_NetworkInterface*& if_addr) const;
190 
191 private:
192  PLT_DeviceHost* m_Device;
193  PLT_SsdpAnnounceType m_Type;
194  bool m_Broadcast;
195 };
196 
197 /*----------------------------------------------------------------------
198 | PLT_SsdpInitMulticastIterator class
199 +---------------------------------------------------------------------*/
205 {
206 public:
207  PLT_SsdpInitMulticastIterator(NPT_UdpMulticastSocket* socket) :
208  m_Socket(socket) {}
209 
210  NPT_Result operator()(NPT_IpAddress& if_addr) const {
211  NPT_IpAddress addr;
212  addr.ResolveName("239.255.255.250");
213  // OSX bug, since we're reusing the socket, we need to leave group first
214  // before joining it
215  m_Socket->LeaveGroup(addr, if_addr);
216  return m_Socket->JoinGroup(addr, if_addr);
217  }
218 
219 private:
220  NPT_UdpMulticastSocket* m_Socket;
221 };
222 
223 /*----------------------------------------------------------------------
224 | PLT_SsdpDeviceAnnounceTask class
225 +---------------------------------------------------------------------*/
231 {
232 public:
234  NPT_TimeInterval repeat,
235  bool is_byebye_first = false,
236  bool extra_broadcast = false) :
237  m_Device(device),
238  m_Repeat(repeat),
239  m_IsByeByeFirst(is_byebye_first),
240  m_ExtraBroadcast(extra_broadcast) {}
241 
242 protected:
243  virtual ~PLT_SsdpDeviceAnnounceTask() {}
244 
245  // PLT_ThreadTask methods
246  virtual void DoRun();
247 
248 protected:
249  PLT_DeviceHost* m_Device;
250  NPT_TimeInterval m_Repeat;
251  bool m_IsByeByeFirst;
252  bool m_ExtraBroadcast;
253 };
254 
255 /*----------------------------------------------------------------------
256 | PLT_NetworkInterfaceAddressSearchIterator class
257 +---------------------------------------------------------------------*/
263 {
264 public:
265  PLT_NetworkInterfaceAddressSearchIterator(NPT_String ip) : m_Ip(ip) {}
267 
268  NPT_Result operator()(NPT_NetworkInterface*& addr) const {
269  NPT_List<NPT_NetworkInterfaceAddress>::Iterator niaddr = addr->GetAddresses().GetFirstItem();
270  if (!niaddr) return NPT_FAILURE;
271 
272  return (m_Ip.Compare((*niaddr).GetPrimaryAddress().ToString(), true) == 0) ? NPT_SUCCESS : NPT_FAILURE;
273  }
274 
275 private:
276  NPT_String m_Ip;
277 };
278 
279 /*----------------------------------------------------------------------
280 | PLT_SsdpPacketListenerIterator class
281 +---------------------------------------------------------------------*/
287 {
288 public:
289  PLT_SsdpPacketListenerIterator(NPT_HttpRequest& request,
290  const NPT_HttpRequestContext& context) :
291  m_Request(request), m_Context(context) {}
292 
293  NPT_Result operator()(PLT_SsdpPacketListener*& listener) const {
294  return listener->OnSsdpPacket(m_Request, m_Context);
295  }
296 
297 private:
298  NPT_HttpRequest& m_Request;
299  const NPT_HttpRequestContext& m_Context;
300 };
301 
302 /*----------------------------------------------------------------------
303 | PLT_SsdpListenTask class
304 +---------------------------------------------------------------------*/
311 {
312 public:
313  PLT_SsdpListenTask(NPT_Socket* socket) :
314  PLT_HttpServerSocketTask(socket, true) {
315  // Change read time out for UDP because iPhone 3.0 seems to hang
316  // after reading everything from the socket even though
317  // more stuff arrived
318 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
319  m_Socket->SetReadTimeout(10000);
320 #endif
321  }
322 
323  NPT_Result AddListener(PLT_SsdpPacketListener* listener) {
324  NPT_AutoLock lock(m_Mutex);
325  m_Listeners.Add(listener);
326  return NPT_SUCCESS;
327  }
328 
329  NPT_Result RemoveListener(PLT_SsdpPacketListener* listener) {
330  NPT_AutoLock lock(m_Mutex);
331  m_Listeners.Remove(listener);
332  return NPT_SUCCESS;
333  }
334 
335  // PLT_Task methods
336  void DoAbort();
337 
338 protected:
339  virtual ~PLT_SsdpListenTask() {}
340 
341  // PLT_HttpServerSocketTask methods
342  NPT_Result GetInputStream(NPT_InputStreamReference& stream);
343  NPT_Result GetInfo(NPT_SocketInfo& info);
344  NPT_Result SetupResponse(NPT_HttpRequest& request,
345  const NPT_HttpRequestContext& context,
346  NPT_HttpResponse& response);
347 
348 protected:
349  PLT_InputDatagramStreamReference m_Datagram;
350  NPT_List<PLT_SsdpPacketListener*> m_Listeners;
351  NPT_Mutex m_Mutex;
352 };
353 
354 /*----------------------------------------------------------------------
355 | PLT_SsdpSearchTask class
356 +---------------------------------------------------------------------*/
362 {
363 public:
364  PLT_SsdpSearchTask(NPT_UdpSocket* socket,
366  NPT_HttpRequest* request,
367  NPT_TimeInterval frequency = NPT_TimeInterval(0.)); // pass 0 for one time
368 
369 protected:
370  virtual ~PLT_SsdpSearchTask();
371 
372  // PLT_ThreadTask methods
373  virtual void DoAbort();
374  virtual void DoRun();
375 
376  virtual NPT_Result ProcessResponse(NPT_Result res,
377  const NPT_HttpRequest& request,
378  const NPT_HttpRequestContext& context,
379  NPT_HttpResponse* response);
380 
381 private:
382  PLT_SsdpSearchResponseListener* m_Listener;
383  NPT_HttpRequest* m_Request;
384  NPT_TimeInterval m_Frequency;
385  bool m_Repeat;
386  NPT_UdpSocket* m_Socket;
387 };
388 
389 #endif /* _PLT_SSDP_H_ */
The PLT_NetworkInterfaceAddressSearchIterator class returns the network interface given an IP address...
Definition: PltSsdp.h:262
The PLT_SsdpDeviceAnnounceTask class is a task to send UPnP Device SSDP announcements (alive or byeby...
Definition: PltSsdp.h:230
Runnable Task.
The PLT_SsdpInitMulticastIterator class is used to join a multicast group given a list of IP addresse...
Definition: PltSsdp.h:204
virtual void DoAbort()
This method to override in derived classes is called when the task is about to stop.
The PLT_SsdpAnnounceInterfaceIterator class is used to send SSDP announcements given a list of networ...
Definition: PltSsdp.h:183
The PLT_HttpServerSocketTask class is a task used for handling one or more HTTP requests from a clien...
Definition: PltHttpServerTask.h:59
The PLT_SsdpDeviceSearchResponseInterfaceIterator class looks for the best network interface to use t...
Definition: PltSsdp.h:132
The PLT_SsdpListenTask class is used to listen for incoming SSDP packets and keep track of a list of ...
Definition: PltSsdp.h:310
The PLT_SsdpPacketListenerIterator class iterates through a list of PLT_SsdpPacketListener instances ...
Definition: PltSsdp.h:286
virtual void DoRun()
This method to override in derived classes is the main task loop.
The PLT_SsdpDeviceSearchResponseTask class is used by a PLT_DeviceHost to respond to SSDP M-SEARCH re...
Definition: PltSsdp.h:156
The PLT_SsdpSearchResponseListener class is an interface for handling SSDP M-SEARCH responses...
Definition: PltSsdp.h:85
void DoAbort()
This method to override in derived classes is called when the task is about to stop.
The PLT_SsdpPacketListener class is an interface for handling SSDP packets (M-SEARCH and NOTIFY)...
Definition: PltSsdp.h:70
HTTP Server Tasks.
UPnP Device Host.
Definition: PltDeviceHost.h:69
virtual void DoRun()
This method to override in derived classes is the main task loop.
virtual void DoRun()
This method to override in derived classes is the main task loop.
The PLT_SsdpSearchTask class is a task used by a PLT_CtrlPoint to issue a SSDP M-SEARCH request...
Definition: PltSsdp.h:361
The PLT_ThreadTask class is a base class for executing a given task in a worker thread.
Definition: PltThreadTask.h:56
The PLT_SsdpSender class provides a mechanism to format and send SSDP packets.
Definition: PltSsdp.h:100