libUPnP 1.8.4
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
66typedef enum SsdpSearchType {
69 SSDP_ALL,
70 SSDP_ROOTDEVICE,
71 SSDP_DEVICEUDN,
72 SSDP_DEVICETYPE,
73 SSDP_SERVICE
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
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
107typedef 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;
124
125typedef void (* SsdpFunPtr)(SsdpEvent *);
126
127typedef struct TData
128{
129 int Mx;
130 void * Cookie;
131 char * Data;
132 struct sockaddr_storage DestAddr;
133} ThreadData;
134
135typedef struct ssdpsearchreply
136{
137 int MaxAge;
138 UpnpDevice_Handle handle;
139 struct sockaddr_storage dest_addr;
140 SsdpEvent event;
142
143typedef struct ssdpsearcharg
144{
145 int timeoutEventId;
146 char * searchTarget;
147 void *cookie;
148 enum SsdpSearchType requestType;
150
151typedef struct ssdpsearchexparg
152{
153 int handle;
154 int timeoutEventId;
156
157typedef 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 */
171typedef int (*ParserFun)(char *, SsdpEvent *);
172
185 /* [in] -1 = Send shutdown, 0 = send reply, 1 = Send Advertisement. */
186 int AdFlag,
187 /* [in] Device handle. */
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
242 /* [in] SSDP socket. */
243 SOCKET socket);
244
252 /* [out] Array of SSDP sockets. */
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
295 /* [in] The handle of the client performing the search. */
296 int Hnd,
297 /* [in] Number of seconds to wait, to collect all the responses. */
298 int Mx,
299 /* [in] Search target. */
300 char *St,
301 /* [in] Cookie provided by control point application. This cokie will
302 * be returned to application in the callback. */
303 void *Cookie);
304
305/* @} SSDP Control Point Functions */
306
318 /* [in] Structure containing the search request. */
319 void *data);
320
326#ifdef INCLUDE_DEVICE_APIS
328 /* [in] . */
329 http_message_t *hmsg,
330 /* [in] . */
331 struct sockaddr_storage *dest_addr);
332#else /* INCLUDE_DEVICE_APIS */
334 /* [in] . */
335 http_message_t *hmsg,
336 /* [in] . */
337 struct sockaddr_storage *dest_addr) {}
338#endif /* INCLUDE_DEVICE_APIS */
339
347 /* [in] type of the device. */
348 char *DevType,
349 /* [in] flag to indicate if the device is root device. */
350 int RootDev,
351 /* [in] UDN. */
352 char *Udn,
353 /* [in] Location URL. */
354 char *Location,
355 /* [in] Service duration in sec. */
356 int Duration,
357 /* [in] Device address family. */
358 int AddressFamily,
359 /* [in] PowerState as defined by UPnP Low Power. */
360 int PowerState,
361 /* [in] SleepPeriod as defined by UPnP Low Power. */
362 int SleepPeriod,
363 /* [in] RegistrationState as defined by UPnP Low Power. */
364 int RegistrationState);
365
372int SendReply(
373 /* [in] destination IP address. */
374 struct sockaddr *DestAddr,
375 /* [in] Device type. */
376 char *DevType,
377 /* [in] 1 means root device 0 means embedded device. */
378 int RootDev,
379 /* [in] Device UDN. */
380 char *Udn,
381 /* [in] Location of Device description document. */
382 char *Location,
383 /* [in] Life time of this device. */
384 int Duration,
385 /* [in] . */
386 int ByType,
387 /* [in] PowerState as defined by UPnP Low Power. */
388 int PowerState,
389 /* [in] SleepPeriod as defined by UPnP Low Power. */
390 int SleepPeriod,
391 /* [in] RegistrationState as defined by UPnP Low Power. */
392 int RegistrationState);
393
400int DeviceReply(
401 /* [in] destination IP address. */
402 struct sockaddr *DestAddr,
403 /* [in] Device type. */
404 char *DevType,
405 /* [in] 1 means root device 0 means embedded device. */
406 int RootDev,
407 /* [in] Device UDN. */
408 char *Udn,
409 /* [in] Location of Device description document. */
410 char *Location,
411 /* [in] Life time of this device. */
412 int Duration,
413 /* [in] PowerState as defined by UPnP Low Power. */
414 int PowerState,
415 /* [in] SleepPeriod as defined by UPnP Low Power. */
416 int SleepPeriod,
417 /* [in] RegistrationState as defined by UPnP Low Power. */
418 int RegistrationState);
419
427 /* [in] Device UDN. */
428 char *Udn,
429 /* [in] Service Type. */
430 char *ServType,
431 /* [in] Location of Device description document. */
432 char *Location,
433 /* [in] Life time of this device. */
434 int Duration,
435 /* [in] Device address family. */
436 int AddressFamily,
437 /* [in] PowerState as defined by UPnP Low Power. */
438 int PowerState,
439 /* [in] SleepPeriod as defined by UPnP Low Power. */
440 int SleepPeriod,
441 /* [in] RegistrationState as defined by UPnP Low Power. */
442 int RegistrationState);
443
450int ServiceReply(
451 /* [in] . */
452 struct sockaddr *DestAddr,
453 /* [in] Service Type. */
454 char *ServType,
455 /* [in] Device UDN. */
456 char *Udn,
457 /* [in] Location of Device description document. */
458 char *Location,
459 /* [in] Life time of this device. */
460 int Duration,
461 /* [in] PowerState as defined by UPnP Low Power. */
462 int PowerState,
463 /* [in] SleepPeriod as defined by UPnP Low Power. */
464 int SleepPeriod,
465 /* [in] RegistrationState as defined by UPnP Low Power. */
466 int RegistrationState);
467
475 /* [in] Device UDN. */
476 char *Udn,
477 /* [in] Service Type. */
478 char *ServType,
479 /* [in] Location of Device description document. */
480 char *Location,
481 /* [in] Service duration in sec. */
482 int Duration,
483 /* [in] Device address family. */
484 int AddressFamily,
485 /* [in] PowerState as defined by UPnP Low Power. */
486 int PowerState,
487 /* [in] SleepPeriod as defined by UPnP Low Power. */
488 int SleepPeriod,
489 /* [in] RegistrationState as defined by UPnP Low Power. */
490 int RegistrationState);
491
499 /* [in] Device Type. */
500 char *DevType,
501 /* [in] 1 means root device. */
502 int RootDev,
503 /* [in] Device UDN. */
504 char *Udn,
505 /* [in] Location URL. */
506 char *Location,
507 /* [in] Device duration in sec. */
508 int Duration,
509 /* [in] Device address family. */
510 int AddressFamily,
511 /* [in] PowerState as defined by UPnP Low Power. */
512 int PowerState,
513 /* [in] SleepPeriod as defined by UPnP Low Power. */
514 int SleepPeriod,
515 /* [in] RegistrationState as defined by UPnP Low Power. */
516 int RegistrationState);
517
518/* @} SSDP Device Functions */
519
520/* @} SSDPlib SSDP Library */
521
522#endif /* SSDPLIB_H */
#define UPNP_INLINE
Declares an inline function.
Definition UpnpGlobal.h:99
Provides a platform independent way to include TCP/IP types and functions.
int SearchByTarget(int Hnd, int Mx, char *St, void *Cookie)
Creates and send the search request for a specific URL.
Definition ssdp_ctrlpt.c:545
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:643
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
int ssdp_request_type(char *cmd, SsdpEvent *Evt)
Starts filling the SSDP event structure based upon the request received.
Definition ssdp_server.c:548
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:495
struct SsdpEventStruct SsdpEvent
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:1165
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:834
void readFromSSDPSocket(SOCKET socket)
This function reads the data from the ssdp socket.
Definition ssdp_server.c:694
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
enum SsdpSearchType SType
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:533
void advertiseAndReplyThread(void *data)
Wrapper function to reply the search request coming from the control point.
Definition ssdp_device.c:67
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:706
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:783
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:78
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:579
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:447
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:757
SsdpSearchType
Definition ssdplib.h:66
@ SSDP_SERROR
Definition ssdplib.h:68
int SOCKET
Definition UpnpInet.h:48
int UpnpDevice_Handle
Returned when a device application registers with UpnpRegisterRootDevice, UpnpRegisterRootDevice2,...
Definition upnp.h:436
Definition miniserver.h:45
Definition ssdplib.h:107
Definition ssdplib.h:128
Definition httpparser.h:179
Definition httpparser.h:213
Definition ssdplib.h:158
Definition ssdplib.h:144
Definition ssdplib.h:152
Definition ssdplib.h:136