ssdplib.h

00001 /**************************************************************************
00002  *
00003  * Copyright (c) 2000-2003 Intel Corporation 
00004  * All rights reserved. 
00005  *
00006  * Redistribution and use in source and binary forms, with or without 
00007  * modification, are permitted provided that the following conditions are met: 
00008  *
00009  * - Redistributions of source code must retain the above copyright notice, 
00010  * this list of conditions and the following disclaimer. 
00011  * - Redistributions in binary form must reproduce the above copyright notice, 
00012  * this list of conditions and the following disclaimer in the documentation 
00013  * and/or other materials provided with the distribution. 
00014  * - Neither name of Intel Corporation nor the names of its contributors 
00015  * may be used to endorse or promote products derived from this software 
00016  * without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
00021  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
00026  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  **************************************************************************/
00031 
00032 
00033 #ifndef SSDPLIB_H
00034 #define SSDPLIB_H 
00035 
00036 #include "httpparser.h"
00037 #include "httpreadwrite.h"
00038 #include "miniserver.h"
00039 #include "UpnpInet.h"
00040 
00041 
00042 #include <sys/types.h>
00043 #include <signal.h>
00044 #include <setjmp.h>
00045 #include <fcntl.h>
00046 #include <errno.h>
00047 
00048 
00049 #ifdef WIN32
00050 #else
00051         #include <syslog.h>
00052         #include <sys/socket.h>
00053         #include <netinet/in_systm.h>
00054         #include <netinet/ip.h>
00055         #include <netinet/ip_icmp.h>
00056         #include <sys/time.h>
00057         #include <arpa/inet.h>
00058 #endif
00059 
00060 
00061 /* Enumeration to define all different types of ssdp searches */
00062 typedef enum SsdpSearchType{
00063         SSDP_SERROR=-1,
00064         SSDP_ALL,SSDP_ROOTDEVICE,
00065         SSDP_DEVICEUDN,
00066         SSDP_DEVICETYPE,
00067         SSDP_SERVICE
00068 } SType;
00069 
00070 
00071 /* Enumeration to define all different type of ssdp messages */
00072 typedef enum SsdpCmdType{
00073         SSDP_ERROR=-1,
00074         SSDP_OK,
00075         SSDP_ALIVE,
00076         SSDP_BYEBYE,
00077         SSDP_SEARCH,
00078         SSDP_NOTIFY,
00079         SSDP_TIMEOUT
00080 } Cmd;
00081 
00082 
00083 
00084 /* Constant */
00085 #define  BUFSIZE   2500
00086 #define  SSDP_IP   "239.255.255.250"
00087 #define  SSDP_IPV6_LINKLOCAL "FF02::C"
00088 #define  SSDP_PORT 1900
00089 #define  NUM_TRY 3
00090 #define  NUM_COPY 1
00091 #define  THREAD_LIMIT 50
00092 #define  COMMAND_LEN  300
00093 
00094 /* can be overwritten by configure CFLAGS argument */
00095 #ifndef X_USER_AGENT
00096 
00102  #define X_USER_AGENT "redsonic"
00103 #endif
00104 
00105 /* Error code */
00106 #define NO_ERROR_FOUND    0
00107 #define E_REQUEST_INVALID       -3
00108 #define E_RES_EXPIRED           -4
00109 #define E_MEM_ALLOC             -5
00110 #define E_HTTP_SYNTEX           -6
00111 #define E_SOCKET                -7
00112 #define RQST_TIMEOUT    20
00113 
00114 
00115 
00116 /* Structure to store the SSDP information */
00117 typedef struct SsdpEventStruct
00118 {
00119   enum SsdpCmdType Cmd;
00120   enum SsdpSearchType RequestType;
00121   int  ErrCode;
00122   int  MaxAge;
00123   int  Mx;
00124   char UDN[LINE_SIZE];
00125   char DeviceType[LINE_SIZE];
00126   char ServiceType[LINE_SIZE];  //NT or ST
00127   char Location[LINE_SIZE];
00128   char HostAddr[LINE_SIZE];
00129   char Os[LINE_SIZE];
00130   char Ext[LINE_SIZE];
00131   char Date[LINE_SIZE];
00132   struct sockaddr *DestAddr;
00133   void * Cookie;
00134 } Event;
00135 
00136 typedef void (* SsdpFunPtr)(Event *);
00137 
00138 typedef Event SsdpEvent ;
00139 
00140 typedef struct TData
00141 {
00142    int Mx;
00143    void * Cookie;
00144    char * Data;
00145    struct sockaddr_storage DestAddr;
00146    
00147 }ThreadData;
00148 
00149 typedef struct ssdpsearchreply
00150 {
00151   int MaxAge;
00152   UpnpDevice_Handle handle;
00153   struct sockaddr_storage dest_addr;
00154   SsdpEvent event;
00155   
00156 }SsdpSearchReply;
00157 
00158 typedef struct ssdpsearcharg
00159 {
00160   int timeoutEventId;
00161   char * searchTarget;
00162   void *cookie;
00163   enum SsdpSearchType requestType;
00164 } SsdpSearchArg;
00165 
00166 
00167 typedef struct 
00168 {
00169   http_parser_t parser;
00170   struct sockaddr_storage dest_addr;
00171 } ssdp_thread_data;
00172 
00173 
00174 /* globals */
00175 
00176 CLIENTONLY(extern SOCKET gSsdpReqSocket4;);
00177 CLIENTONLY(extern SOCKET gSsdpReqSocket6;);
00178 
00179 typedef int (*ParserFun)(char *, Event *);
00180 
00181 
00182 /************************************************************************
00183 * Function : Make_Socket_NoBlocking
00184 *
00185 * Parameters:
00186 *       IN int sock: socket
00187 *
00188 * Description:
00189 *       This function to make ssdp socket non-blocking.
00190 *
00191 * Returns: int
00192 *       0 if successful else -1 
00193 ***************************************************************************/
00194 int Make_Socket_NoBlocking (int sock);
00195 
00196 /************************************************************************
00197 * Function : ssdp_handle_device_request
00198 *
00199 * Parameters:
00200 *               IN void *data:
00201 *
00202 * Description:
00203 *       This function handles the search request. It do the sanity checks of
00204 *       the request and then schedules a thread to send a random time reply (
00205 *       random within maximum time given by the control point to reply).
00206 *
00207 * Returns: void *
00208 *       1 if successful else appropriate error
00209 ***************************************************************************/
00210 #ifdef INCLUDE_DEVICE_APIS
00211 void ssdp_handle_device_request(
00212         IN http_message_t* hmsg, 
00213         IN struct sockaddr* dest_addr );
00214 #else
00215 static inline void ssdp_handle_device_request(
00216         IN http_message_t* hmsg, 
00217         IN struct sockaddr* dest_addr ) {}
00218 #endif
00219 
00220 /************************************************************************
00221 * Function : ssdp_handle_ctrlpt_msg
00222 *
00223 * Parameters:
00224 *       IN http_message_t* hmsg: SSDP message from the device
00225 *       IN struct sockaddr* dest_addr: Address of the device
00226 *       IN xboolean timeout: timeout kept by the control point while sending 
00227 *               search message
00228 *       IN void* cookie: Cookie stored by the control point application. 
00229 *               This cookie will be returned to the control point
00230 *               in the callback
00231 *
00232 * Description:
00233 *       This function handles the ssdp messages from the devices. These 
00234 *       messages includes the search replies, advertisement of device coming 
00235 *       alive and bye byes.
00236 *
00237 * Returns: void
00238 *
00239 ***************************************************************************/
00240 void ssdp_handle_ctrlpt_msg(
00241         IN http_message_t* hmsg, 
00242         IN struct sockaddr* dest_addr,
00243         IN xboolean timeout,
00244         IN void* cookie );
00245 
00246 /************************************************************************
00247 * Function : unique_service_name
00248 *
00249 * Parameters:
00250 *       IN char *cmd: Service Name string
00251 *       OUT SsdpEvent *Evt: The SSDP event structure partially filled
00252 *               by all the function.
00253 *
00254 * Description:
00255 *       This function fills the fields of the event structure like DeviceType,
00256 *       Device UDN and Service Type
00257 *
00258 * Returns: int
00259 *       0 if successful else -1 
00260 ***************************************************************************/
00261 int unique_service_name(char * cmd, SsdpEvent * Evt);
00262 
00263 
00264 /************************************************************************
00265 * Function : get_ssdp_sockets
00266 *
00267 * Parameters:
00268 *       OUT MiniServerSockArray *out: Arrays of SSDP sockets
00269 *
00270 * Description:
00271 *       This function creates the ssdp sockets. It set their option to listen 
00272 *       for multicast traffic.
00273 *
00274 * Returns: int
00275 *       return UPNP_E_SUCCESS if successful else returns appropriate error
00276 ***************************************************************************/
00277 int get_ssdp_sockets(MiniServerSockArray *out);
00278 
00279 
00280 /************************************************************************
00281 * Function : readFromSSDPSocket 
00282 *
00283 * Parameters:
00284 *       IN SOCKET socket: SSDP socket
00285 *
00286 * Description:
00287 *       This function reads the data from the ssdp socket.
00288 *
00289 * Returns: void
00290 *       
00291 ***************************************************************************/
00292 void readFromSSDPSocket(SOCKET socket);
00293 
00294 
00295 /************************************************************************
00296 * Function : ssdp_request_type1
00297 *
00298 * Parameters:
00299 *       IN char *cmd: command came in the ssdp request
00300 *
00301 * Description:
00302 *       This function figures out the type of the SSDP search in the
00303 *       in the request.
00304 *
00305 * Returns: enum SsdpSearchType
00306 *       return appropriate search type else returns SSDP_ERROR
00307 ***************************************************************************/
00308 enum SsdpSearchType ssdp_request_type1(IN char *cmd);
00309 
00310 
00311 /************************************************************************
00312 * Function : ssdp_request_type
00313 *
00314 * Parameters:
00315 *       IN char *cmd: command came in the ssdp request
00316 *       OUT SsdpEvent *Evt: The event structure partially filled by
00317 *                this function.
00318 *
00319 * Description:
00320 *       This function starts filling the SSDP event structure based upon the 
00321 *       request received. 
00322 *
00323 * Returns: int
00324 *       0 on success; -1 on error
00325 ***************************************************************************/
00326 int ssdp_request_type(IN char * cmd, OUT SsdpEvent * Evt);
00327 
00328 
00329 /************************************************************************
00330 * Function : SearchByTarget
00331 *
00332 * Parameters:
00333 *       IN int Mx:Number of seconds to wait, to collect all the responses.
00334 *       char *St: Search target.
00335 *       void *Cookie: cookie provided by control point application. This
00336 *               cokie will be returned to application in the callback.
00337 *
00338 * Description:
00339 *       This function creates and send the search request for a specific URL.
00340 *
00341 * Returns: int
00342 *       1 if successful else appropriate error
00343 ***************************************************************************/
00344 int SearchByTarget(IN int Mx, IN char *St, IN void *Cookie);
00345 
00346 /************************************************************************
00347 * Function : DeviceAdvertisement
00348 *
00349 * Parameters:
00350 *       IN char *DevType : type of the device
00351 *       IN int RootDev   : flag to indicate if the device is root device
00352 *       IN char *Udn     :
00353 *       IN char *Location: Location URL.
00354 *       IN int Duration  : Service duration in sec.
00355 *       IN int AddressFamily: Device address family.
00356 *
00357 * Description:
00358 *       This function creates the device advertisement request based on
00359 *       the input parameter, and send it to the multicast channel.
00360 *
00361 * Returns: int
00362 *       UPNP_E_SUCCESS if successful else appropriate error
00363 ***************************************************************************/
00364 int DeviceAdvertisement(
00365         IN char *DevType,
00366         IN int RootDev,
00367         IN char *Udn, 
00368         IN char *Location,
00369         IN int Duration,
00370         IN int AddressFamily);
00371 
00372 
00373 /************************************************************************
00374 * Function : DeviceShutdown
00375 *
00376 * Parameters:   
00377 *       IN char *DevType: Device Type.
00378 *       IN int RootDev:1 means root device.
00379 *       IN char *Udn: Device UDN
00380 *       IN char *_Server:
00381 *       IN char *Location: Location URL
00382 *       IN int Duration :Device duration in sec.
00383 *       IN int AddressFamily: Device address family.
00384 *
00385 * Description:
00386 *       This function creates a HTTP device shutdown request packet 
00387 *       and sent it to the multicast channel through RequestHandler.
00388 *
00389 * Returns: int
00390 *       UPNP_E_SUCCESS if successful else appropriate error
00391 ***************************************************************************/
00392 int DeviceShutdown(
00393         IN char *DevType, 
00394         IN int RootDev,
00395         IN char *Udn, 
00396         IN char *_Server, 
00397         IN char *Location, 
00398         IN int Duration,
00399         IN int AddressFamily);
00400 
00401 /************************************************************************
00402 * Function : DeviceReply
00403 *
00404 * Parameters:   
00405 *       IN struct sockaddr *DestAddr: destination IP address.
00406 *       IN char *DevType: Device type
00407 *       IN int RootDev: 1 means root device 0 means embedded device.
00408 *       IN char *Udn: Device UDN
00409 *       IN char *Location: Location of Device description document.
00410 *       IN int Duration :Life time of this device.
00411 *
00412 * Description:
00413 *       This function creates the reply packet based on the input parameter, 
00414 *       and send it to the client address given in its input parameter DestAddr.
00415 *
00416 * Returns: int
00417 *       UPNP_E_SUCCESS if successful else appropriate error
00418 ***************************************************************************/
00419 int DeviceReply(
00420         IN struct sockaddr *DestAddr, 
00421         IN char *DevType, 
00422         IN int RootDev, 
00423         IN char *Udn, 
00424         IN char *Location, 
00425         IN int  Duration);
00426 
00427 /************************************************************************
00428 * Function : SendReply
00429 *
00430 * Parameters:   
00431 *       IN struct sockaddr *DestAddr: destination IP address.
00432 *       IN char *DevType: Device type
00433 *       IN int RootDev: 1 means root device 0 means embedded device.
00434 *       IN char * Udn: Device UDN
00435 *       IN char *_Server:
00436 *       IN char *Location: Location of Device description document.
00437 *       IN int Duration :Life time of this device.
00438 *       IN int ByType:
00439 *
00440 * Description:
00441 *       This function creates the reply packet based on the input parameter, 
00442 *       and send it to the client addesss given in its input parameter DestAddr.
00443 *
00444 * Returns: int
00445 *       UPNP_E_SUCCESS if successful else appropriate error
00446 ***************************************************************************/
00447 int SendReply(
00448         IN struct sockaddr *DestAddr, 
00449         IN char *DevType, 
00450         IN int RootDev, 
00451         IN char *Udn, 
00452         IN char *Location, 
00453         IN int Duration, 
00454         IN int ByType );
00455 
00456 /************************************************************************
00457 * Function : ServiceAdvertisement
00458 *
00459 * Parameters:   
00460 *       IN char * Udn: Device UDN
00461 *       IN char *ServType: Service Type.
00462 *       IN char * Location: Location of Device description document.
00463 *       IN int Duration: Life time of this device.
00464 *       IN int AddressFamily: Device address family
00465 *
00466 * Description:
00467 *       This function creates the advertisement packet based 
00468 *       on the input parameter, and send it to the multicast channel.
00469 *
00470 * Returns: int
00471 *       UPNP_E_SUCCESS if successful else appropriate error
00472 ***************************************************************************/
00473 int ServiceAdvertisement(
00474         IN char *Udn, 
00475         IN char *ServType,
00476         IN char *Location,
00477         IN int Duration,
00478         IN int AddressFamily);
00479 
00480 /************************************************************************
00481 * Function : ServiceReply
00482 *
00483 * Parameters:   
00484 *       IN struct sockaddr *DestAddr:
00485 *       IN char *Udn: Device UDN
00486 *       IN char *ServType: Service Type.
00487 *       IN char *Server: Not used
00488 *       IN char *Location: Location of Device description document.
00489 *       IN int Duration :Life time of this device.
00490 *
00491 * Description:
00492 *       This function creates the advertisement packet based 
00493 *       on the input parameter, and send it to the multicast channel.
00494 *
00495 * Returns: int
00496 *       UPNP_E_SUCCESS if successful else appropriate error
00497 ***************************************************************************/
00498 int ServiceReply(
00499         IN struct sockaddr *DestAddr,  
00500         IN char *ServType, 
00501         IN char *Udn, 
00502         IN char *Location,
00503         IN int Duration);
00504 
00505 /************************************************************************
00506 * Function : ServiceShutdown
00507 *
00508 * Parameters:
00509 *       IN char *Udn: Device UDN
00510 *       IN char *ServType: Service Type.
00511 *       IN char *Location: Location of Device description document.
00512 *       IN int Duration :Service duration in sec.
00513 *       IN int AddressFamily: Device address family
00514 *
00515 * Description:
00516 *       This function creates a HTTP service shutdown request packet 
00517 *       and sent it to the multicast channel through RequestHandler.
00518 *
00519 * Returns: int
00520 *       UPNP_E_SUCCESS if successful else appropriate error
00521 ***************************************************************************/
00522 int ServiceShutdown(
00523         IN char *Udn,
00524         IN char *ServType,
00525         IN char *Location,
00526         IN int Duration,
00527         IN int AddressFamily);
00528 
00529 
00530 /************************************************************************
00531 * Function : advertiseAndReplyThread
00532 *
00533 * Parameters:
00534 *       IN void *data: Structure containing the search request
00535 *
00536 * Description:
00537 *       This function is a wrapper function to reply the search request 
00538 *       coming from the control point.
00539 *
00540 * Returns: void *
00541 *       always return NULL
00542 ***************************************************************************/
00543 void *advertiseAndReplyThread(IN void * data);
00544 
00545 /************************************************************************
00546 * Function : AdvertiseAndReply
00547 *
00548 * Parameters:
00549 *       IN int AdFlag: -1 = Send shutdown,
00550 *                       0 = send reply, 
00551 *                       1 = Send Advertisement
00552 *       IN UpnpDevice_Handle Hnd: Device handle
00553 *       IN enum SsdpSearchType SearchType:Search type for sending replies
00554 *       IN struct sockaddr *DestAddr:Destination address
00555 *       IN char *DeviceType:Device type
00556 *       IN char *DeviceUDN:Device UDN
00557 *       IN char *ServiceType:Service type
00558 *       IN int Exp:Advertisement age
00559 *
00560 * Description:
00561 *       This function to send SSDP advertisements, replies and shutdown messages.
00562 *
00563 * Returns: int
00564 *       UPNP_E_SUCCESS if successful else appropriate error
00565 ***************************************************************************/
00566 int AdvertiseAndReply(
00567         IN int AdFlag, 
00568         IN UpnpDevice_Handle Hnd, 
00569         IN enum SsdpSearchType SearchType, 
00570         IN struct sockaddr *DestAddr,
00571         IN char *DeviceType, 
00572         IN char *DeviceUDN, 
00573         IN char *ServiceType, int Exp);
00574 
00575 #endif /* SSDPLIB_H */
00576