libUPnP  1.8.0
ssdplib.h
1 /**************************************************************************
2  *
3  * Copyright (c) 2000-2003 Intel Corporation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * - Neither name of Intel Corporation nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  **************************************************************************/
31 
32 
33 #ifndef SSDPLIB_H
34 #define SSDPLIB_H
35 
36 #include "httpparser.h"
37 #include "httpreadwrite.h"
38 #include "miniserver.h"
39 #include "UpnpInet.h"
40 
41 
42 #include <sys/types.h>
43 #include <signal.h>
44 #include <setjmp.h>
45 #include <fcntl.h>
46 #include <errno.h>
47 
48 
49 #ifdef WIN32
50 #else
51  #include <syslog.h>
52  #include <sys/socket.h>
53  #include <netinet/in_systm.h>
54  #include <netinet/ip.h>
55  #include <netinet/ip_icmp.h>
56  #include <sys/time.h>
57  #include <arpa/inet.h>
58 #endif
59 
60 
61 /* Enumeration to define all different types of ssdp searches */
62 typedef enum SsdpSearchType{
63  SSDP_SERROR=-1,
64  SSDP_ALL,SSDP_ROOTDEVICE,
65  SSDP_DEVICEUDN,
66  SSDP_DEVICETYPE,
67  SSDP_SERVICE
68 } SType;
69 
70 
71 /* Enumeration to define all different type of ssdp messages */
72 typedef enum SsdpCmdType{
73  SSDP_ERROR=-1,
74  SSDP_OK,
75  SSDP_ALIVE,
76  SSDP_BYEBYE,
77  SSDP_SEARCH,
78  SSDP_NOTIFY,
79  SSDP_TIMEOUT
80 } Cmd;
81 
82 
83 
84 /* Constant */
85 #define BUFSIZE 2500
86 #define SSDP_IP "239.255.255.250"
87 #define SSDP_IPV6_LINKLOCAL "FF02::C"
88 #define SSDP_PORT 1900
89 #define NUM_TRY 3
90 #define NUM_COPY 1
91 #define THREAD_LIMIT 50
92 #define COMMAND_LEN 300
93 
94 /* can be overwritten by configure CFLAGS argument */
95 #ifndef X_USER_AGENT
96 
102  #define X_USER_AGENT "redsonic"
103 #endif
104 
105 /* Error code */
106 #define NO_ERROR_FOUND 0
107 #define E_REQUEST_INVALID -3
108 #define E_RES_EXPIRED -4
109 #define E_MEM_ALLOC -5
110 #define E_HTTP_SYNTEX -6
111 #define E_SOCKET -7
112 #define RQST_TIMEOUT 20
113 
114 
115 
116 /* Structure to store the SSDP information */
117 typedef struct SsdpEventStruct
118 {
119  enum SsdpCmdType Cmd;
120  enum SsdpSearchType RequestType;
121  int ErrCode;
122  int MaxAge;
123  int Mx;
124  char UDN[LINE_SIZE];
125  char DeviceType[LINE_SIZE];
126  char ServiceType[LINE_SIZE]; //NT or ST
127  char Location[LINE_SIZE];
128  char HostAddr[LINE_SIZE];
129  char Os[LINE_SIZE];
130  char Ext[LINE_SIZE];
131  char Date[LINE_SIZE];
132  struct sockaddr *DestAddr;
133  void * Cookie;
134 } Event;
135 
136 typedef void (* SsdpFunPtr)(Event *);
137 
138 typedef Event SsdpEvent ;
139 
140 typedef struct TData
141 {
142  int Mx;
143  void * Cookie;
144  char * Data;
145  struct sockaddr_storage DestAddr;
146 
147 }ThreadData;
148 
149 typedef struct ssdpsearchreply
150 {
151  int MaxAge;
152  UpnpDevice_Handle handle;
153  struct sockaddr_storage dest_addr;
154  SsdpEvent event;
155 
157 
158 typedef struct ssdpsearcharg
159 {
160  int timeoutEventId;
161  char * searchTarget;
162  void *cookie;
163  enum SsdpSearchType requestType;
164 } SsdpSearchArg;
165 
166 
167 typedef struct
168 {
169  http_parser_t parser;
170  struct sockaddr_storage dest_addr;
172 
173 
174 /* globals */
175 
176 CLIENTONLY(extern SOCKET gSsdpReqSocket4;);
177 CLIENTONLY(extern SOCKET gSsdpReqSocket6;);
178 
179 typedef int (*ParserFun)(char *, Event *);
180 
181 
182 /************************************************************************
183 * Function : Make_Socket_NoBlocking
184 *
185 * Parameters:
186 * IN int sock: socket
187 *
188 * Description:
189 * This function to make ssdp socket non-blocking.
190 *
191 * Returns: int
192 * 0 if successful else -1
193 ***************************************************************************/
194 int Make_Socket_NoBlocking (int sock);
195 
196 /************************************************************************
197 * Function : ssdp_handle_device_request
198 *
199 * Parameters:
200 * IN void *data:
201 *
202 * Description:
203 * This function handles the search request. It do the sanity checks of
204 * the request and then schedules a thread to send a random time reply (
205 * random within maximum time given by the control point to reply).
206 *
207 * Returns: void *
208 * 1 if successful else appropriate error
209 ***************************************************************************/
210 #ifdef INCLUDE_DEVICE_APIS
211 void ssdp_handle_device_request(
212  IN http_message_t* hmsg,
213  IN struct sockaddr* dest_addr );
214 #else
215 static inline void ssdp_handle_device_request(
216  IN http_message_t* hmsg,
217  IN struct sockaddr* dest_addr ) {}
218 #endif
219 
220 /************************************************************************
221 * Function : ssdp_handle_ctrlpt_msg
222 *
223 * Parameters:
224 * IN http_message_t* hmsg: SSDP message from the device
225 * IN struct sockaddr* dest_addr: Address of the device
226 * IN xboolean timeout: timeout kept by the control point while sending
227 * search message
228 * IN void* cookie: Cookie stored by the control point application.
229 * This cookie will be returned to the control point
230 * in the callback
231 *
232 * Description:
233 * This function handles the ssdp messages from the devices. These
234 * messages includes the search replies, advertisement of device coming
235 * alive and bye byes.
236 *
237 * Returns: void
238 *
239 ***************************************************************************/
240 void ssdp_handle_ctrlpt_msg(
241  IN http_message_t* hmsg,
242  IN struct sockaddr* dest_addr,
243  IN xboolean timeout,
244  IN void* cookie );
245 
246 /************************************************************************
247 * Function : unique_service_name
248 *
249 * Parameters:
250 * IN char *cmd: Service Name string
251 * OUT SsdpEvent *Evt: The SSDP event structure partially filled
252 * by all the function.
253 *
254 * Description:
255 * This function fills the fields of the event structure like DeviceType,
256 * Device UDN and Service Type
257 *
258 * Returns: int
259 * 0 if successful else -1
260 ***************************************************************************/
261 int unique_service_name(char * cmd, SsdpEvent * Evt);
262 
263 
264 /************************************************************************
265 * Function : get_ssdp_sockets
266 *
267 * Parameters:
268 * OUT MiniServerSockArray *out: Arrays of SSDP sockets
269 *
270 * Description:
271 * This function creates the ssdp sockets. It set their option to listen
272 * for multicast traffic.
273 *
274 * Returns: int
275 * return UPNP_E_SUCCESS if successful else returns appropriate error
276 ***************************************************************************/
277 int get_ssdp_sockets(MiniServerSockArray *out);
278 
279 
280 /************************************************************************
281 * Function : readFromSSDPSocket
282 *
283 * Parameters:
284 * IN SOCKET socket: SSDP socket
285 *
286 * Description:
287 * This function reads the data from the ssdp socket.
288 *
289 * Returns: void
290 *
291 ***************************************************************************/
292 void readFromSSDPSocket(SOCKET socket);
293 
294 
295 /************************************************************************
296 * Function : ssdp_request_type1
297 *
298 * Parameters:
299 * IN char *cmd: command came in the ssdp request
300 *
301 * Description:
302 * This function figures out the type of the SSDP search in the
303 * in the request.
304 *
305 * Returns: enum SsdpSearchType
306 * return appropriate search type else returns SSDP_ERROR
307 ***************************************************************************/
308 enum SsdpSearchType ssdp_request_type1(IN char *cmd);
309 
310 
311 /************************************************************************
312 * Function : ssdp_request_type
313 *
314 * Parameters:
315 * IN char *cmd: command came in the ssdp request
316 * OUT SsdpEvent *Evt: The event structure partially filled by
317 * this function.
318 *
319 * Description:
320 * This function starts filling the SSDP event structure based upon the
321 * request received.
322 *
323 * Returns: int
324 * 0 on success; -1 on error
325 ***************************************************************************/
326 int ssdp_request_type(IN char * cmd, OUT SsdpEvent * Evt);
327 
328 
329 /************************************************************************
330 * Function : SearchByTarget
331 *
332 * Parameters:
333 * IN int Mx:Number of seconds to wait, to collect all the responses.
334 * char *St: Search target.
335 * void *Cookie: cookie provided by control point application. This
336 * cokie will be returned to application in the callback.
337 *
338 * Description:
339 * This function creates and send the search request for a specific URL.
340 *
341 * Returns: int
342 * 1 if successful else appropriate error
343 ***************************************************************************/
344 int SearchByTarget(IN int Mx, IN char *St, IN void *Cookie);
345 
346 /************************************************************************
347 * Function : DeviceAdvertisement
348 *
349 * Parameters:
350 * IN char *DevType : type of the device
351 * IN int RootDev : flag to indicate if the device is root device
352 * IN char *Udn :
353 * IN char *Location: Location URL.
354 * IN int Duration : Service duration in sec.
355 * IN int AddressFamily: Device address family.
356 *
357 * Description:
358 * This function creates the device advertisement request based on
359 * the input parameter, and send it to the multicast channel.
360 *
361 * Returns: int
362 * UPNP_E_SUCCESS if successful else appropriate error
363 ***************************************************************************/
364 int DeviceAdvertisement(
365  IN char *DevType,
366  IN int RootDev,
367  IN char *Udn,
368  IN char *Location,
369  IN int Duration,
370  IN int AddressFamily);
371 
372 
373 /************************************************************************
374 * Function : DeviceShutdown
375 *
376 * Parameters:
377 * IN char *DevType: Device Type.
378 * IN int RootDev:1 means root device.
379 * IN char *Udn: Device UDN
380 * IN char *_Server:
381 * IN char *Location: Location URL
382 * IN int Duration :Device duration in sec.
383 * IN int AddressFamily: Device address family.
384 *
385 * Description:
386 * This function creates a HTTP device shutdown request packet
387 * and sent it to the multicast channel through RequestHandler.
388 *
389 * Returns: int
390 * UPNP_E_SUCCESS if successful else appropriate error
391 ***************************************************************************/
392 int DeviceShutdown(
393  IN char *DevType,
394  IN int RootDev,
395  IN char *Udn,
396  IN char *_Server,
397  IN char *Location,
398  IN int Duration,
399  IN int AddressFamily);
400 
401 /************************************************************************
402 * Function : DeviceReply
403 *
404 * Parameters:
405 * IN struct sockaddr *DestAddr: destination IP address.
406 * IN char *DevType: Device type
407 * IN int RootDev: 1 means root device 0 means embedded device.
408 * IN char *Udn: Device UDN
409 * IN char *Location: Location of Device description document.
410 * IN int Duration :Life time of this device.
411 *
412 * Description:
413 * This function creates the reply packet based on the input parameter,
414 * and send it to the client address given in its input parameter DestAddr.
415 *
416 * Returns: int
417 * UPNP_E_SUCCESS if successful else appropriate error
418 ***************************************************************************/
419 int DeviceReply(
420  IN struct sockaddr *DestAddr,
421  IN char *DevType,
422  IN int RootDev,
423  IN char *Udn,
424  IN char *Location,
425  IN int Duration);
426 
427 /************************************************************************
428 * Function : SendReply
429 *
430 * Parameters:
431 * IN struct sockaddr *DestAddr: destination IP address.
432 * IN char *DevType: Device type
433 * IN int RootDev: 1 means root device 0 means embedded device.
434 * IN char * Udn: Device UDN
435 * IN char *_Server:
436 * IN char *Location: Location of Device description document.
437 * IN int Duration :Life time of this device.
438 * IN int ByType:
439 *
440 * Description:
441 * This function creates the reply packet based on the input parameter,
442 * and send it to the client addesss given in its input parameter DestAddr.
443 *
444 * Returns: int
445 * UPNP_E_SUCCESS if successful else appropriate error
446 ***************************************************************************/
447 int SendReply(
448  IN struct sockaddr *DestAddr,
449  IN char *DevType,
450  IN int RootDev,
451  IN char *Udn,
452  IN char *Location,
453  IN int Duration,
454  IN int ByType );
455 
456 /************************************************************************
457 * Function : ServiceAdvertisement
458 *
459 * Parameters:
460 * IN char * Udn: Device UDN
461 * IN char *ServType: Service Type.
462 * IN char * Location: Location of Device description document.
463 * IN int Duration: Life time of this device.
464 * IN int AddressFamily: Device address family
465 *
466 * Description:
467 * This function creates the advertisement packet based
468 * on the input parameter, and send it to the multicast channel.
469 *
470 * Returns: int
471 * UPNP_E_SUCCESS if successful else appropriate error
472 ***************************************************************************/
473 int ServiceAdvertisement(
474  IN char *Udn,
475  IN char *ServType,
476  IN char *Location,
477  IN int Duration,
478  IN int AddressFamily);
479 
480 /************************************************************************
481 * Function : ServiceReply
482 *
483 * Parameters:
484 * IN struct sockaddr *DestAddr:
485 * IN char *Udn: Device UDN
486 * IN char *ServType: Service Type.
487 * IN char *Server: Not used
488 * IN char *Location: Location of Device description document.
489 * IN int Duration :Life time of this device.
490 *
491 * Description:
492 * This function creates the advertisement packet based
493 * on the input parameter, and send it to the multicast channel.
494 *
495 * Returns: int
496 * UPNP_E_SUCCESS if successful else appropriate error
497 ***************************************************************************/
498 int ServiceReply(
499  IN struct sockaddr *DestAddr,
500  IN char *ServType,
501  IN char *Udn,
502  IN char *Location,
503  IN int Duration);
504 
505 /************************************************************************
506 * Function : ServiceShutdown
507 *
508 * Parameters:
509 * IN char *Udn: Device UDN
510 * IN char *ServType: Service Type.
511 * IN char *Location: Location of Device description document.
512 * IN int Duration :Service duration in sec.
513 * IN int AddressFamily: Device address family
514 *
515 * Description:
516 * This function creates a HTTP service shutdown request packet
517 * and sent it to the multicast channel through RequestHandler.
518 *
519 * Returns: int
520 * UPNP_E_SUCCESS if successful else appropriate error
521 ***************************************************************************/
522 int ServiceShutdown(
523  IN char *Udn,
524  IN char *ServType,
525  IN char *Location,
526  IN int Duration,
527  IN int AddressFamily);
528 
529 
530 /************************************************************************
531 * Function : advertiseAndReplyThread
532 *
533 * Parameters:
534 * IN void *data: Structure containing the search request
535 *
536 * Description:
537 * This function is a wrapper function to reply the search request
538 * coming from the control point.
539 *
540 * Returns: void *
541 * always return NULL
542 ***************************************************************************/
543 void *advertiseAndReplyThread(IN void * data);
544 
545 /************************************************************************
546 * Function : AdvertiseAndReply
547 *
548 * Parameters:
549 * IN int AdFlag: -1 = Send shutdown,
550 * 0 = send reply,
551 * 1 = Send Advertisement
552 * IN UpnpDevice_Handle Hnd: Device handle
553 * IN enum SsdpSearchType SearchType:Search type for sending replies
554 * IN struct sockaddr *DestAddr:Destination address
555 * IN char *DeviceType:Device type
556 * IN char *DeviceUDN:Device UDN
557 * IN char *ServiceType:Service type
558 * IN int Exp:Advertisement age
559 *
560 * Description:
561 * This function to send SSDP advertisements, replies and shutdown messages.
562 *
563 * Returns: int
564 * UPNP_E_SUCCESS if successful else appropriate error
565 ***************************************************************************/
566 int AdvertiseAndReply(
567  IN int AdFlag,
568  IN UpnpDevice_Handle Hnd,
569  IN enum SsdpSearchType SearchType,
570  IN struct sockaddr *DestAddr,
571  IN char *DeviceType,
572  IN char *DeviceUDN,
573  IN char *ServiceType, int Exp);
574 
575 #endif /* SSDPLIB_H */
576