libUPnP  1.8.2
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 typedef struct ssdpsearchexparg
152 {
153  int handle;
154  int timeoutEventId;
156 
157 typedef struct
158 {
159  http_parser_t parser;
160  struct sockaddr_storage dest_addr;
162 
163 /* globals */
164 
165 #ifdef INCLUDE_CLIENT_APIS
166  extern SOCKET gSsdpReqSocket4;
167  #ifdef UPNP_ENABLE_IPV6
168  extern SOCKET gSsdpReqSocket6;
169  #endif /* UPNP_ENABLE_IPV6 */
170 #endif /* INCLUDE_CLIENT_APIS */
171 typedef int (*ParserFun)(char *, SsdpEvent *);
172 
185  /* [in] -1 = Send shutdown, 0 = send reply, 1 = Send Advertisement. */
186  int AdFlag,
187  /* [in] Device handle. */
188  UpnpDevice_Handle Hnd,
189  /* [in] Search type for sending replies. */
190  enum SsdpSearchType SearchType,
191  /* [in] Destination address. */
192  struct sockaddr *DestAddr,
193  /* [in] Device type. */
194  char *DeviceType,
195  /* [in] Device UDN. */
196  char *DeviceUDN,
197  /* [in] Service type. */
198  char *ServiceType,
199  /* [in] Advertisement age. */
200  int Exp);
201 
209  /* [in] Service Name string. */
210  char *cmd,
211  /* [out] The SSDP event structure partially filled by all the
212  * function. */
213  SsdpEvent *Evt);
214 
223  /* [in] command came in the ssdp request. */
224  char *cmd);
225 
233  /* [in] command came in the ssdp request. */
234  char *cmd,
235  /* [out] The event structure partially filled by this function. */
236  SsdpEvent *Evt);
237 
241 void readFromSSDPSocket(
242  /* [in] SSDP socket. */
243  SOCKET socket);
244 
251 int get_ssdp_sockets(
252  /* [out] Array of SSDP sockets. */
253  MiniServerSockArray *out);
254 
255 /* @} SSDP Server Functions */
256 
269  /* [in] SSDP message from the device. */
270  http_message_t *hmsg,
271  /* [in] Address of the device. */
272  struct sockaddr_storage *dest_addr,
273  /* [in] timeout kept by the control point while sending search message.
274  * Only in search reply. */
275  int timeout,
276  /* [in] Cookie stored by the control point application. This cookie will
277  * be returned to the control point in the callback.
278  * Only in search reply. */
279  void *cookie);
280 
298 int SearchByTarget(
299  /* [in] The handle of the client performing the search. */
300  int Hnd,
301  /* [in] Number of seconds to wait, to collect all the responses. */
302  int Mx,
303  /* [in] Search target. */
304  char *St,
305  /* [in] Cookie provided by control point application. This cokie will
306  * be returned to application in the callback. */
307  void *Cookie);
308 
309 /* @} SSDP Control Point Functions */
310 
324  /* [in] Structure containing the search request. */
325  void *data);
326 
332 #ifdef INCLUDE_DEVICE_APIS
334  /* [in] . */
335  http_message_t *hmsg,
336  /* [in] . */
337  struct sockaddr_storage *dest_addr);
338 #else /* INCLUDE_DEVICE_APIS */
340  /* [in] . */
341  http_message_t *hmsg,
342  /* [in] . */
343  struct sockaddr_storage *dest_addr) {}
344 #endif /* INCLUDE_DEVICE_APIS */
345 
353  /* [in] type of the device. */
354  char *DevType,
355  /* [in] flag to indicate if the device is root device. */
356  int RootDev,
357  /* [in] UDN. */
358  char *Udn,
359  /* [in] Location URL. */
360  char *Location,
361  /* [in] Service duration in sec. */
362  int Duration,
363  /* [in] Device address family. */
364  int AddressFamily,
365  /* [in] PowerState as defined by UPnP Low Power. */
366  int PowerState,
367  /* [in] SleepPeriod as defined by UPnP Low Power. */
368  int SleepPeriod,
369  /* [in] RegistrationState as defined by UPnP Low Power. */
370  int RegistrationState);
371 
378 int SendReply(
379  /* [in] destination IP address. */
380  struct sockaddr *DestAddr,
381  /* [in] Device type. */
382  char *DevType,
383  /* [in] 1 means root device 0 means embedded device. */
384  int RootDev,
385  /* [in] Device UDN. */
386  char *Udn,
387  /* [in] Location of Device description document. */
388  char *Location,
389  /* [in] Life time of this device. */
390  int Duration,
391  /* [in] . */
392  int ByType,
393  /* [in] PowerState as defined by UPnP Low Power. */
394  int PowerState,
395  /* [in] SleepPeriod as defined by UPnP Low Power. */
396  int SleepPeriod,
397  /* [in] RegistrationState as defined by UPnP Low Power. */
398  int RegistrationState);
399 
406 int DeviceReply(
407  /* [in] destination IP address. */
408  struct sockaddr *DestAddr,
409  /* [in] Device type. */
410  char *DevType,
411  /* [in] 1 means root device 0 means embedded device. */
412  int RootDev,
413  /* [in] Device UDN. */
414  char *Udn,
415  /* [in] Location of Device description document. */
416  char *Location,
417  /* [in] Life time of this device. */
418  int Duration,
419  /* [in] PowerState as defined by UPnP Low Power. */
420  int PowerState,
421  /* [in] SleepPeriod as defined by UPnP Low Power. */
422  int SleepPeriod,
423  /* [in] RegistrationState as defined by UPnP Low Power. */
424  int RegistrationState);
425 
433  /* [in] Device UDN. */
434  char *Udn,
435  /* [in] Service Type. */
436  char *ServType,
437  /* [in] Location of Device description document. */
438  char *Location,
439  /* [in] Life time of this device. */
440  int Duration,
441  /* [in] Device address family. */
442  int AddressFamily,
443  /* [in] PowerState as defined by UPnP Low Power. */
444  int PowerState,
445  /* [in] SleepPeriod as defined by UPnP Low Power. */
446  int SleepPeriod,
447  /* [in] RegistrationState as defined by UPnP Low Power. */
448  int RegistrationState);
449 
456 int ServiceReply(
457  /* [in] . */
458  struct sockaddr *DestAddr,
459  /* [in] Service Type. */
460  char *ServType,
461  /* [in] Device UDN. */
462  char *Udn,
463  /* [in] Location of Device description document. */
464  char *Location,
465  /* [in] Life time of this device. */
466  int Duration,
467  /* [in] PowerState as defined by UPnP Low Power. */
468  int PowerState,
469  /* [in] SleepPeriod as defined by UPnP Low Power. */
470  int SleepPeriod,
471  /* [in] RegistrationState as defined by UPnP Low Power. */
472  int RegistrationState);
473 
480 int ServiceShutdown(
481  /* [in] Device UDN. */
482  char *Udn,
483  /* [in] Service Type. */
484  char *ServType,
485  /* [in] Location of Device description document. */
486  char *Location,
487  /* [in] Service duration in sec. */
488  int Duration,
489  /* [in] Device address family. */
490  int AddressFamily,
491  /* [in] PowerState as defined by UPnP Low Power. */
492  int PowerState,
493  /* [in] SleepPeriod as defined by UPnP Low Power. */
494  int SleepPeriod,
495  /* [in] RegistrationState as defined by UPnP Low Power. */
496  int RegistrationState);
497 
504 int DeviceShutdown(
505  /* [in] Device Type. */
506  char *DevType,
507  /* [in] 1 means root device. */
508  int RootDev,
509  /* [in] Device UDN. */
510  char *Udn,
511  /* [in] Location URL. */
512  char *Location,
513  /* [in] Device duration in sec. */
514  int Duration,
515  /* [in] Device address family. */
516  int AddressFamily,
517  /* [in] PowerState as defined by UPnP Low Power. */
518  int PowerState,
519  /* [in] SleepPeriod as defined by UPnP Low Power. */
520  int SleepPeriod,
521  /* [in] RegistrationState as defined by UPnP Low Power. */
522  int RegistrationState);
523 
524 /* @} SSDP Device Functions */
525 
526 /* @} SSDPlib SSDP Library */
527 
528 #endif /* SSDPLIB_H */
Definition: ssdplib.h:68
Definition: miniserver.h:45
Definition: httpparser.h:179
Definition: ssdplib.h:157
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 Hnd, int Mx, char *St, void *Cookie)
Creates and send the search request for a specific URL.
Definition: ssdp_ctrlpt.c:543
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
Definition: ssdplib.h:151
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:99
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