libUPnP  1.14.24
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 "UpnpInet.h"
45 #include "httpparser.h"
46 #include "httpreadwrite.h"
47 #include "miniserver.h"
48 
49 #include <errno.h>
50 #include <setjmp.h>
51 #include <signal.h>
52 #include <sys/types.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
67 {
70  SSDP_ALL,
71  SSDP_ROOTDEVICE,
72  SSDP_DEVICEUDN,
73  SSDP_DEVICETYPE,
74  SSDP_SERVICE
75 } SType;
76 
77 #define BUFSIZE (size_t)2500
78 #define SSDP_IP "239.255.255.250"
79 #define SSDP_IPV6_LINKLOCAL "FF02::C"
80 #define SSDP_IPV6_SITELOCAL "FF05::C"
81 #define SSDP_PORT 1900
82 #define SSDP_PORT_STR "1900"
83 #define NUM_TRY 3
84 #define THREAD_LIMIT 50
85 #define COMMAND_LEN 300
86 
88 #ifndef X_USER_AGENT
89 
95  #define X_USER_AGENT "redsonic"
96 #endif
97 
99 #define NO_ERROR_FOUND 0
100 #define E_REQUEST_INVALID -3
101 #define E_RES_EXPIRED -4
102 #define E_MEM_ALLOC -5
103 #define E_HTTP_SYNTEX -6
104 #define E_SOCKET -7
105 
106 #define RQST_TIMEOUT 20
107 
109 typedef struct SsdpEventStruct
110 {
111  enum SsdpSearchType RequestType;
112  int ErrCode;
113  int MaxAge;
114  int Mx;
115  char UDN[LINE_SIZE];
116  char DeviceType[LINE_SIZE];
117  /* NT or ST */
118  char ServiceType[LINE_SIZE];
119  char Location[LINE_SIZE];
120  char HostAddr[LINE_SIZE];
121  char Os[LINE_SIZE];
122  char Ext[LINE_SIZE];
123  char Date[LINE_SIZE];
124  struct sockaddr *DestAddr;
125  void *Cookie;
126 } SsdpEvent;
127 
128 typedef void (*SsdpFunPtr)(SsdpEvent *);
129 
130 typedef struct TData
131 {
132  int Mx;
133  void *Cookie;
134  char *Data;
135  struct sockaddr_storage DestAddr;
136 } ThreadData;
137 
138 typedef struct ssdpsearchreply
139 {
140  int MaxAge;
141  UpnpDevice_Handle handle;
142  struct sockaddr_storage dest_addr;
143  SsdpEvent event;
145 
146 typedef struct ssdpsearcharg
147 {
148  int timeoutEventId;
149  char *searchTarget;
150  void *cookie;
151  enum SsdpSearchType requestType;
152 } SsdpSearchArg;
153 
154 typedef struct ssdpsearchexparg
155 {
156  int handle;
157  int timeoutEventId;
159 
160 typedef struct
161 {
162  http_parser_t parser;
163  struct sockaddr_storage dest_addr;
165 
166 /* globals */
167 
168 #ifdef INCLUDE_CLIENT_APIS
169 extern SOCKET gSsdpReqSocket4;
170  #ifdef UPNP_ENABLE_IPV6
171 extern SOCKET gSsdpReqSocket6;
172  #endif /* UPNP_ENABLE_IPV6 */
173 #endif /* INCLUDE_CLIENT_APIS */
174 typedef int (*ParserFun)(char *, SsdpEvent *);
175 
188  /* [in] -1 = Send shutdown, 0 = send reply, 1 = Send Advertisement. */
189  int AdFlag,
190  /* [in] Device handle. */
191  UpnpDevice_Handle Hnd,
192  /* [in] Search type for sending replies. */
193  enum SsdpSearchType SearchType,
194  /* [in] Destination address. */
195  struct sockaddr *DestAddr,
196  /* [in] Device type. */
197  char *DeviceType,
198  /* [in] Device UDN. */
199  char *DeviceUDN,
200  /* [in] Service type. */
201  char *ServiceType,
202  /* [in] Advertisement age. */
203  int Exp);
204 
212  /* [in] Service Name string. */
213  char *cmd,
214  /* [out] The SSDP event structure partially filled by all the
215  * function. */
216  SsdpEvent *Evt);
217 
226  /* [in] command came in the ssdp request. */
227  char *cmd);
228 
236  /* [in] command came in the ssdp request. */
237  char *cmd,
238  /* [out] The event structure partially filled by this function. */
239  SsdpEvent *Evt);
240 
247  /* [in] SSDP socket. */
248  SOCKET socket);
249 
256 int get_ssdp_sockets(
257  /* [out] Array of SSDP sockets. */
258  MiniServerSockArray *out);
259 
260 /* @} SSDP Server Functions */
261 
274  /* [in] SSDP message from the device. */
275  http_message_t *hmsg,
276  /* [in] Address of the device. */
277  struct sockaddr_storage *dest_addr,
278  /* [in] timeout kept by the control point while sending search message.
279  * Only in search reply. */
280  int timeout);
281 
299 int SearchByTarget(
300  /* [in] The handle of the client performing the search. */
301  int Hnd,
302  /* [in] Number of seconds to wait, to collect all the responses. */
303  int Mx,
304  /* [in] Search target. */
305  char *St,
306  /* [in] Cookie provided by control point application. This cokie will
307  * be returned to application in the callback. */
308  void *Cookie);
309 
310 /* @} SSDP Control Point Functions */
311 
323  /* [in] Structure containing the search request. */
324  void *data);
325 
331 #ifdef INCLUDE_DEVICE_APIS
333  /* [in] . */
334  http_message_t *hmsg,
335  /* [in] . */
336  struct sockaddr_storage *dest_addr);
337 #else /* INCLUDE_DEVICE_APIS */
339  /* [in] . */
340  http_message_t *hmsg,
341  /* [in] . */
342  struct sockaddr_storage *dest_addr)
343 {
344 }
345 #endif /* INCLUDE_DEVICE_APIS */
346 
354  /* [in] type of the device. */
355  char *DevType,
356  /* [in] flag to indicate if the device is root device. */
357  int RootDev,
358  /* [in] UDN. */
359  char *Udn,
360  /* [in] Location URL. */
361  char *Location,
362  /* [in] Service duration in sec. */
363  int Duration,
364  /* [in] Device address family. */
365  int AddressFamily,
366  /* [in] PowerState as defined by UPnP Low Power. */
367  int PowerState,
368  /* [in] SleepPeriod as defined by UPnP Low Power. */
369  int SleepPeriod,
370  /* [in] RegistrationState as defined by UPnP Low Power. */
371  int RegistrationState);
372 
379 int SendReply(
380  /* [in] destination IP address. */
381  struct sockaddr *DestAddr,
382  /* [in] Device type. */
383  char *DevType,
384  /* [in] 1 means root device 0 means embedded device. */
385  int RootDev,
386  /* [in] Device UDN. */
387  char *Udn,
388  /* [in] Location of Device description document. */
389  char *Location,
390  /* [in] Life time of this device. */
391  int Duration,
392  /* [in] . */
393  int ByType,
394  /* [in] PowerState as defined by UPnP Low Power. */
395  int PowerState,
396  /* [in] SleepPeriod as defined by UPnP Low Power. */
397  int SleepPeriod,
398  /* [in] RegistrationState as defined by UPnP Low Power. */
399  int RegistrationState);
400 
407 int DeviceReply(
408  /* [in] destination IP address. */
409  struct sockaddr *DestAddr,
410  /* [in] Device type. */
411  char *DevType,
412  /* [in] 1 means root device 0 means embedded device. */
413  int RootDev,
414  /* [in] Device UDN. */
415  char *Udn,
416  /* [in] Location of Device description document. */
417  char *Location,
418  /* [in] Life time of this device. */
419  int Duration,
420  /* [in] PowerState as defined by UPnP Low Power. */
421  int PowerState,
422  /* [in] SleepPeriod as defined by UPnP Low Power. */
423  int SleepPeriod,
424  /* [in] RegistrationState as defined by UPnP Low Power. */
425  int RegistrationState);
426 
434  /* [in] Device UDN. */
435  char *Udn,
436  /* [in] Service Type. */
437  char *ServType,
438  /* [in] Location of Device description document. */
439  char *Location,
440  /* [in] Life time of this device. */
441  int Duration,
442  /* [in] Device address family. */
443  int AddressFamily,
444  /* [in] PowerState as defined by UPnP Low Power. */
445  int PowerState,
446  /* [in] SleepPeriod as defined by UPnP Low Power. */
447  int SleepPeriod,
448  /* [in] RegistrationState as defined by UPnP Low Power. */
449  int RegistrationState);
450 
457 int ServiceReply(
458  /* [in] . */
459  struct sockaddr *DestAddr,
460  /* [in] Service Type. */
461  char *ServType,
462  /* [in] Device UDN. */
463  char *Udn,
464  /* [in] Location of Device description document. */
465  char *Location,
466  /* [in] Life time of this device. */
467  int Duration,
468  /* [in] PowerState as defined by UPnP Low Power. */
469  int PowerState,
470  /* [in] SleepPeriod as defined by UPnP Low Power. */
471  int SleepPeriod,
472  /* [in] RegistrationState as defined by UPnP Low Power. */
473  int RegistrationState);
474 
481 int ServiceShutdown(
482  /* [in] Device UDN. */
483  char *Udn,
484  /* [in] Service Type. */
485  char *ServType,
486  /* [in] Location of Device description document. */
487  char *Location,
488  /* [in] Service duration in sec. */
489  int Duration,
490  /* [in] Device address family. */
491  int AddressFamily,
492  /* [in] PowerState as defined by UPnP Low Power. */
493  int PowerState,
494  /* [in] SleepPeriod as defined by UPnP Low Power. */
495  int SleepPeriod,
496  /* [in] RegistrationState as defined by UPnP Low Power. */
497  int RegistrationState);
498 
505 int DeviceShutdown(
506  /* [in] Device Type. */
507  char *DevType,
508  /* [in] 1 means root device. */
509  int RootDev,
510  /* [in] Device UDN. */
511  char *Udn,
512  /* [in] Location URL. */
513  char *Location,
514  /* [in] Device duration in sec. */
515  int Duration,
516  /* [in] Device address family. */
517  int AddressFamily,
518  /* [in] PowerState as defined by UPnP Low Power. */
519  int PowerState,
520  /* [in] SleepPeriod as defined by UPnP Low Power. */
521  int SleepPeriod,
522  /* [in] RegistrationState as defined by UPnP Low Power. */
523  int RegistrationState);
524 
525 /* @} SSDP Device Functions */
526 
527 /* @} SSDPlib SSDP Library */
528 
529 #endif /* SSDPLIB_H */
Definition: ssdplib.h:69
Definition: miniserver.h:45
Definition: httpparser.h:181
Definition: ssdplib.h:160
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:909
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:789
int readFromSSDPSocket(SOCKET socket)
This function reads the data from the ssdp socket.
Definition: ssdp_server.c:808
struct SsdpEventStruct SsdpEvent
Definition: ssdplib.h:146
int ssdp_request_type(char *cmd, SsdpEvent *Evt)
Starts filling the SSDP event structure based upon the request received.
Definition: ssdp_server.c:650
void ssdp_handle_ctrlpt_msg(http_message_t *hmsg, struct sockaddr_storage *dest_addr, int timeout)
This function handles the ssdp messages from the devices. These messages includes the search replies...
Definition: ssdp_ctrlpt.c:76
Definition: ssdplib.h:109
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:1091
Definition: ssdplib.h:130
int SearchByTarget(int Hnd, int Mx, char *St, void *Cookie)
Creates and send the search request for a specific URL.
Definition: ssdp_ctrlpt.c:573
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:80
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:635
int UpnpDevice_Handle
Returned when a device application registers with UpnpRegisterRootDevice, UpnpRegisterRootDevice2, UpnpRegisterRootDevice3 or UpnpRegisterRootDevice4.
Definition: upnp.h:439
Definition: ssdplib.h:138
int SOCKET
Definition: UpnpInet.h:50
void advertiseAndReplyThread(void *data)
Wrapper function to reply the search request coming from the control point.
Definition: ssdp_device.c:64
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:1263
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:1158
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:90
SsdpSearchType
Definition: ssdplib.h:66
Definition: httpparser.h:216
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:1001
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:543
Definition: ssdplib.h:154
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:1478
#define UPNP_INLINE
Declares an inline function.
Definition: UpnpGlobal.h:119
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:1196