libUPnP  1.8.1
ssdplib.h
Go to the documentation of this file.
1 #ifndef SSDPLIB_H
2 #define SSDPLIB_H
3 
4 /**************************************************************************
5  *
6  * Copyright (c) 2000-2003 Intel Corporation
7  * All rights reserved.
8  * Copyright (C) 2011-2012 France Telecom All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * - Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  * - Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  * - Neither name of Intel Corporation nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
30  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  **************************************************************************/
35 
44 #include "httpparser.h"
45 #include "httpreadwrite.h"
46 #include "miniserver.h"
47 #include "UpnpInet.h"
48 
49 #include <sys/types.h>
50 #include <signal.h>
51 #include <setjmp.h>
52 #include <errno.h>
53 
54 #ifdef _WIN32
55 #else /* _WIN32 */
56  #include <syslog.h>
57  #ifndef __APPLE__
58  #include <netinet/in_systm.h>
59  #include <netinet/ip.h>
60  #include <netinet/ip_icmp.h>
61  #endif /* __APPLE__ */
62  #include <sys/time.h>
63 #endif /* _WIN32 */
64 
66 typedef enum SsdpSearchType {
69  SSDP_ALL,
70  SSDP_ROOTDEVICE,
71  SSDP_DEVICEUDN,
72  SSDP_DEVICETYPE,
73  SSDP_SERVICE
74 } SType;
75 
76 #define BUFSIZE (size_t)2500
77 #define SSDP_IP "239.255.255.250"
78 #define SSDP_IPV6_LINKLOCAL "FF02::C"
79 #define SSDP_IPV6_SITELOCAL "FF05::C"
80 #define SSDP_PORT 1900
81 #define NUM_TRY 3
82 #define THREAD_LIMIT 50
83 #define COMMAND_LEN 300
84 
86 #ifndef X_USER_AGENT
87 
93  #define X_USER_AGENT "redsonic"
94 #endif
95 
97 #define NO_ERROR_FOUND 0
98 #define E_REQUEST_INVALID -3
99 #define E_RES_EXPIRED -4
100 #define E_MEM_ALLOC -5
101 #define E_HTTP_SYNTEX -6
102 #define E_SOCKET -7
103 
104 #define RQST_TIMEOUT 20
105 
107 typedef struct SsdpEventStruct {
108  enum SsdpSearchType RequestType;
109  int ErrCode;
110  int MaxAge;
111  int Mx;
112  char UDN[LINE_SIZE];
113  char DeviceType[LINE_SIZE];
114  /* NT or ST */
115  char ServiceType[LINE_SIZE];
116  char Location[LINE_SIZE];
117  char HostAddr[LINE_SIZE];
118  char Os[LINE_SIZE];
119  char Ext[LINE_SIZE];
120  char Date[LINE_SIZE];
121  struct sockaddr *DestAddr;
122  void * Cookie;
123 } SsdpEvent;
124 
125 typedef void (* SsdpFunPtr)(SsdpEvent *);
126 
127 typedef struct TData
128 {
129  int Mx;
130  void * Cookie;
131  char * Data;
132  struct sockaddr_storage DestAddr;
133 } ThreadData;
134 
135 typedef struct ssdpsearchreply
136 {
137  int MaxAge;
138  UpnpDevice_Handle handle;
139  struct sockaddr_storage dest_addr;
140  SsdpEvent event;
142 
143 typedef struct ssdpsearcharg
144 {
145  int timeoutEventId;
146  char * searchTarget;
147  void *cookie;
148  enum SsdpSearchType requestType;
149 } SsdpSearchArg;
150 
151 
152 typedef struct
153 {
154  http_parser_t parser;
155  struct sockaddr_storage dest_addr;
157 
158 /* globals */
159 
160 #ifdef INCLUDE_CLIENT_APIS
161  extern SOCKET gSsdpReqSocket4;
162  #ifdef UPNP_ENABLE_IPV6
163  extern SOCKET gSsdpReqSocket6;
164  #endif /* UPNP_ENABLE_IPV6 */
165 #endif /* INCLUDE_CLIENT_APIS */
166 typedef int (*ParserFun)(char *, SsdpEvent *);
167 
180  /* [in] -1 = Send shutdown, 0 = send reply, 1 = Send Advertisement. */
181  int AdFlag,
182  /* [in] Device handle. */
183  UpnpDevice_Handle Hnd,
184  /* [in] Search type for sending replies. */
185  enum SsdpSearchType SearchType,
186  /* [in] Destination address. */
187  struct sockaddr *DestAddr,
188  /* [in] Device type. */
189  char *DeviceType,
190  /* [in] Device UDN. */
191  char *DeviceUDN,
192  /* [in] Service type. */
193  char *ServiceType,
194  /* [in] Advertisement age. */
195  int Exp);
196 
204  /* [in] Service Name string. */
205  char *cmd,
206  /* [out] The SSDP event structure partially filled by all the
207  * function. */
208  SsdpEvent *Evt);
209 
218  /* [in] command came in the ssdp request. */
219  char *cmd);
220 
228  /* [in] command came in the ssdp request. */
229  char *cmd,
230  /* [out] The event structure partially filled by this function. */
231  SsdpEvent *Evt);
232 
236 void readFromSSDPSocket(
237  /* [in] SSDP socket. */
238  SOCKET socket);
239 
246 int get_ssdp_sockets(
247  /* [out] Array of SSDP sockets. */
248  MiniServerSockArray *out);
249 
250 /* @} SSDP Server Functions */
251 
264  /* [in] SSDP message from the device. */
265  http_message_t *hmsg,
266  /* [in] Address of the device. */
267  struct sockaddr_storage *dest_addr,
268  /* [in] timeout kept by the control point while sending search message.
269  * Only in search reply. */
270  int timeout,
271  /* [in] Cookie stored by the control point application. This cookie will
272  * be returned to the control point in the callback.
273  * Only in search reply. */
274  void *cookie);
275 
293 int SearchByTarget(
294  /* [in] Number of seconds to wait, to collect all the responses. */
295  int Mx,
296  /* [in] Search target. */
297  char *St,
298  /* [in] Cookie provided by control point application. This cokie will
299  * be returned to application in the callback. */
300  void *Cookie);
301 
302 /* @} SSDP Control Point Functions */
303 
317  /* [in] Structure containing the search request. */
318  void *data);
319 
325 #ifdef INCLUDE_DEVICE_APIS
327  /* [in] . */
328  http_message_t *hmsg,
329  /* [in] . */
330  struct sockaddr_storage *dest_addr);
331 #else /* INCLUDE_DEVICE_APIS */
333  /* [in] . */
334  http_message_t *hmsg,
335  /* [in] . */
336  struct sockaddr_storage *dest_addr) {}
337 #endif /* INCLUDE_DEVICE_APIS */
338 
346  /* [in] type of the device. */
347  char *DevType,
348  /* [in] flag to indicate if the device is root device. */
349  int RootDev,
350  /* [in] UDN. */
351  char *Udn,
352  /* [in] Location URL. */
353  char *Location,
354  /* [in] Service duration in sec. */
355  int Duration,
356  /* [in] Device address family. */
357  int AddressFamily,
358  /* [in] PowerState as defined by UPnP Low Power. */
359  int PowerState,
360  /* [in] SleepPeriod as defined by UPnP Low Power. */
361  int SleepPeriod,
362  /* [in] RegistrationState as defined by UPnP Low Power. */
363  int RegistrationState);
364 
371 int SendReply(
372  /* [in] destination IP address. */
373  struct sockaddr *DestAddr,
374  /* [in] Device type. */
375  char *DevType,
376  /* [in] 1 means root device 0 means embedded device. */
377  int RootDev,
378  /* [in] Device UDN. */
379  char *Udn,
380  /* [in] Location of Device description document. */
381  char *Location,
382  /* [in] Life time of this device. */
383  int Duration,
384  /* [in] . */
385  int ByType,
386  /* [in] PowerState as defined by UPnP Low Power. */
387  int PowerState,
388  /* [in] SleepPeriod as defined by UPnP Low Power. */
389  int SleepPeriod,
390  /* [in] RegistrationState as defined by UPnP Low Power. */
391  int RegistrationState);
392 
399 int DeviceReply(
400  /* [in] destination IP address. */
401  struct sockaddr *DestAddr,
402  /* [in] Device type. */
403  char *DevType,
404  /* [in] 1 means root device 0 means embedded device. */
405  int RootDev,
406  /* [in] Device UDN. */
407  char *Udn,
408  /* [in] Location of Device description document. */
409  char *Location,
410  /* [in] Life time of this device. */
411  int Duration,
412  /* [in] PowerState as defined by UPnP Low Power. */
413  int PowerState,
414  /* [in] SleepPeriod as defined by UPnP Low Power. */
415  int SleepPeriod,
416  /* [in] RegistrationState as defined by UPnP Low Power. */
417  int RegistrationState);
418 
426  /* [in] Device UDN. */
427  char *Udn,
428  /* [in] Service Type. */
429  char *ServType,
430  /* [in] Location of Device description document. */
431  char *Location,
432  /* [in] Life time of this device. */
433  int Duration,
434  /* [in] Device address family. */
435  int AddressFamily,
436  /* [in] PowerState as defined by UPnP Low Power. */
437  int PowerState,
438  /* [in] SleepPeriod as defined by UPnP Low Power. */
439  int SleepPeriod,
440  /* [in] RegistrationState as defined by UPnP Low Power. */
441  int RegistrationState);
442 
449 int ServiceReply(
450  /* [in] . */
451  struct sockaddr *DestAddr,
452  /* [in] Service Type. */
453  char *ServType,
454  /* [in] Device UDN. */
455  char *Udn,
456  /* [in] Location of Device description document. */
457  char *Location,
458  /* [in] Life time of this device. */
459  int Duration,
460  /* [in] PowerState as defined by UPnP Low Power. */
461  int PowerState,
462  /* [in] SleepPeriod as defined by UPnP Low Power. */
463  int SleepPeriod,
464  /* [in] RegistrationState as defined by UPnP Low Power. */
465  int RegistrationState);
466 
473 int ServiceShutdown(
474  /* [in] Device UDN. */
475  char *Udn,
476  /* [in] Service Type. */
477  char *ServType,
478  /* [in] Location of Device description document. */
479  char *Location,
480  /* [in] Service duration in sec. */
481  int Duration,
482  /* [in] Device address family. */
483  int AddressFamily,
484  /* [in] PowerState as defined by UPnP Low Power. */
485  int PowerState,
486  /* [in] SleepPeriod as defined by UPnP Low Power. */
487  int SleepPeriod,
488  /* [in] RegistrationState as defined by UPnP Low Power. */
489  int RegistrationState);
490 
497 int DeviceShutdown(
498  /* [in] Device Type. */
499  char *DevType,
500  /* [in] 1 means root device. */
501  int RootDev,
502  /* [in] Device UDN. */
503  char *Udn,
504  /* [in] Location URL. */
505  char *Location,
506  /* [in] Device duration in sec. */
507  int Duration,
508  /* [in] Device address family. */
509  int AddressFamily,
510  /* [in] PowerState as defined by UPnP Low Power. */
511  int PowerState,
512  /* [in] SleepPeriod as defined by UPnP Low Power. */
513  int SleepPeriod,
514  /* [in] RegistrationState as defined by UPnP Low Power. */
515  int RegistrationState);
516 
517 /* @} SSDP Device Functions */
518 
519 /* @} SSDPlib SSDP Library */
520 
521 #endif /* SSDPLIB_H */
Definition: ssdplib.h:68
Definition: miniserver.h:45
Definition: httpparser.h:179
Definition: ssdplib.h:152
int SendReply(struct sockaddr *DestAddr, char *DevType, int RootDev, char *Udn, char *Location, int Duration, int ByType, int PowerState, int SleepPeriod, int RegistrationState)
Creates the reply packet based on the input parameter, and send it to the client addesss given in its...
Definition: ssdp_device.c:577
int DeviceAdvertisement(char *DevType, int RootDev, char *Udn, char *Location, int Duration, int AddressFamily, int PowerState, int SleepPeriod, int RegistrationState)
Creates the device advertisement request based on the input parameter, and send it to the multicast c...
Definition: ssdp_device.c:493
void readFromSSDPSocket(SOCKET socket)
This function reads the data from the ssdp socket.
Definition: ssdp_server.c:697
struct SsdpEventStruct SsdpEvent
Definition: ssdplib.h:143
int ssdp_request_type(char *cmd, SsdpEvent *Evt)
Starts filling the SSDP event structure based upon the request received.
Definition: ssdp_server.c:549
Definition: ssdplib.h:107
enum SsdpSearchType SType
int ServiceAdvertisement(char *Udn, char *ServType, char *Location, int Duration, int AddressFamily, int PowerState, int SleepPeriod, int RegistrationState)
Creates the advertisement packet based on the input parameter, and send it to the multicast channel...
Definition: ssdp_device.c:704
void * advertiseAndReplyThread(void *data)
Wrapper function to reply the search request coming from the control point.
Definition: ssdp_device.c:67
Definition: ssdplib.h:127
int SearchByTarget(int Mx, char *St, void *Cookie)
Creates and send the search request for a specific URL.
Definition: ssdp_ctrlpt.c:510
void ssdp_handle_device_request(http_message_t *hmsg, struct sockaddr_storage *dest_addr)
Handles the search request. It does the sanity checks of the request and then schedules a thread to s...
Definition: ssdp_device.c:82
Provides a platform independent way to include TCP/IP types and functions.
enum SsdpSearchType ssdp_request_type1(char *cmd)
This function figures out the type of the SSDP search in the in the request.
Definition: ssdp_server.c:534
int UpnpDevice_Handle
Returned when a device application registers with UpnpRegisterRootDevice, UpnpRegisterRootDevice2, UpnpRegisterRootDevice3 or UpnpRegisterRootDevice4.
Definition: upnp.h:436
void ssdp_handle_ctrlpt_msg(http_message_t *hmsg, struct sockaddr_storage *dest_addr, int timeout, void *cookie)
This function handles the ssdp messages from the devices. These messages includes the search replies...
Definition: ssdp_ctrlpt.c:78
Definition: ssdplib.h:135
int SOCKET
Definition: UpnpInet.h:48
int DeviceShutdown(char *DevType, int RootDev, char *Udn, char *Location, int Duration, int AddressFamily, int PowerState, int SleepPeriod, int RegistrationState)
Creates a HTTP device shutdown request packet and send it to the multicast channel through RequestHan...
Definition: ssdp_device.c:832
int ServiceReply(struct sockaddr *DestAddr, char *ServType, char *Udn, char *Location, int Duration, int PowerState, int SleepPeriod, int RegistrationState)
Creates the advertisement packet based on the input parameter, and send it to the multicast channel...
Definition: ssdp_device.c:755
int AdvertiseAndReply(int AdFlag, UpnpDevice_Handle Hnd, enum SsdpSearchType SearchType, struct sockaddr *DestAddr, char *DeviceType, char *DeviceUDN, char *ServiceType, int Exp)
Sends SSDP advertisements, replies and shutdown messages.
Definition: ssdp_server.c:92
SsdpSearchType
Definition: ssdplib.h:66
Definition: httpparser.h:213
int DeviceReply(struct sockaddr *DestAddr, char *DevType, int RootDev, char *Udn, char *Location, int Duration, int PowerState, int SleepPeriod, int RegistrationState)
Creates the reply packet based on the input parameter, and send it to the client address given in its...
Definition: ssdp_device.c:641
int unique_service_name(char *cmd, SsdpEvent *Evt)
Fills the fields of the event structure like DeviceType, Device UDN and Service Type.
Definition: ssdp_server.c:448
int get_ssdp_sockets(MiniServerSockArray *out)
Creates the IPv4 and IPv6 ssdp sockets required by the control point and device operation.
Definition: ssdp_server.c:1168
#define UPNP_INLINE
Declares an inline function.
Definition: UpnpGlobal.h:93
int ServiceShutdown(char *Udn, char *ServType, char *Location, int Duration, int AddressFamily, int PowerState, int SleepPeriod, int RegistrationState)
Creates a HTTP service shutdown request packet and sends it to the multicast channel through RequestH...
Definition: ssdp_device.c:781