libUPnP  1.6.17
ssdplib.h
Go to the documentation of this file.
00001 #ifndef SSDPLIB_H
00002 #define SSDPLIB_H 
00003 
00004 /**************************************************************************
00005  *
00006  * Copyright (c) 2000-2003 Intel Corporation 
00007  * All rights reserved. 
00008  * Copyright (C) 2011-2012 France Telecom All rights reserved. 
00009  *
00010  * Redistribution and use in source and binary forms, with or without 
00011  * modification, are permitted provided that the following conditions are met: 
00012  *
00013  * - Redistributions of source code must retain the above copyright notice, 
00014  * this list of conditions and the following disclaimer. 
00015  * - Redistributions in binary form must reproduce the above copyright notice, 
00016  * this list of conditions and the following disclaimer in the documentation 
00017  * and/or other materials provided with the distribution. 
00018  * - Neither name of Intel Corporation nor the names of its contributors 
00019  * may be used to endorse or promote products derived from this software 
00020  * without specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
00025  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 
00026  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
00030  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  **************************************************************************/
00035 
00044 #include "httpparser.h"
00045 #include "httpreadwrite.h"
00046 #include "miniserver.h"
00047 #include "UpnpInet.h"
00048 
00049 #include <sys/types.h>
00050 #include <signal.h>
00051 #include <setjmp.h>
00052 #include <errno.h>
00053 
00054 #ifdef WIN32
00055 #else /* WIN32 */
00056         #include <syslog.h>
00057         #ifndef __APPLE__
00058                 #include <netinet/in_systm.h>
00059                 #include <netinet/ip.h>
00060                 #include <netinet/ip_icmp.h>
00061         #endif /* __APPLE__ */
00062         #include <sys/time.h>
00063 #endif /* WIN32 */
00064 
00066 typedef enum SsdpSearchType {
00068         SSDP_SERROR = -1,
00069         SSDP_ALL,
00070         SSDP_ROOTDEVICE,
00071         SSDP_DEVICEUDN,
00072         SSDP_DEVICETYPE,
00073         SSDP_SERVICE
00074 } SType;
00075 
00076 #define BUFSIZE   (size_t)2500
00077 #define SSDP_IP   "239.255.255.250"
00078 #define SSDP_IPV6_LINKLOCAL "FF02::C"
00079 #define SSDP_IPV6_SITELOCAL "FF05::C"
00080 #define SSDP_PORT 1900
00081 #define NUM_TRY 3
00082 #define THREAD_LIMIT 50
00083 #define COMMAND_LEN  300
00084 
00086 #ifndef X_USER_AGENT
00087 
00093         #define X_USER_AGENT "redsonic"
00094 #endif
00095 
00097 #define NO_ERROR_FOUND    0
00098 #define E_REQUEST_INVALID       -3
00099 #define E_RES_EXPIRED           -4
00100 #define E_MEM_ALLOC             -5
00101 #define E_HTTP_SYNTEX           -6
00102 #define E_SOCKET                -7
00103 
00104 #define RQST_TIMEOUT    20
00105 
00107 typedef struct SsdpEventStruct {
00108         enum SsdpSearchType RequestType;
00109         int  ErrCode;
00110         int  MaxAge;
00111         int  Mx;
00112         char UDN[LINE_SIZE];
00113         char DeviceType[LINE_SIZE];
00114         /* NT or ST */
00115         char ServiceType[LINE_SIZE];
00116         char Location[LINE_SIZE];
00117         char HostAddr[LINE_SIZE];
00118         char Os[LINE_SIZE];
00119         char Ext[LINE_SIZE];
00120         char Date[LINE_SIZE];
00121         struct sockaddr *DestAddr;
00122         void * Cookie;
00123 } SsdpEvent;
00124 
00125 typedef void (* SsdpFunPtr)(SsdpEvent *);
00126 
00127 typedef struct TData
00128 {
00129         int Mx;
00130         void * Cookie;
00131         char * Data;
00132         struct sockaddr_storage DestAddr;
00133 } ThreadData;
00134 
00135 typedef struct ssdpsearchreply
00136 {
00137         int MaxAge;
00138         UpnpDevice_Handle handle;
00139         struct sockaddr_storage dest_addr;
00140         SsdpEvent event;
00141 } SsdpSearchReply;
00142 
00143 typedef struct ssdpsearcharg
00144 {
00145         int timeoutEventId;
00146         char * searchTarget;
00147         void *cookie;
00148         enum SsdpSearchType requestType;
00149 } SsdpSearchArg;
00150 
00151 
00152 typedef struct 
00153 {
00154         http_parser_t parser;
00155         struct sockaddr_storage dest_addr;
00156 } ssdp_thread_data;
00157 
00158 /* globals */
00159 
00160 #ifdef INCLUDE_CLIENT_APIS
00161         extern SOCKET gSsdpReqSocket4;
00162         #ifdef UPNP_ENABLE_IPV6
00163                 extern SOCKET gSsdpReqSocket6;
00164         #endif /* UPNP_ENABLE_IPV6 */
00165 #endif /* INCLUDE_CLIENT_APIS */
00166 typedef int (*ParserFun)(char *, SsdpEvent *);
00167 
00179 int AdvertiseAndReply(
00180         /* [in] -1 = Send shutdown, 0 = send reply, 1 = Send Advertisement. */
00181         int AdFlag, 
00182         /* [in] Device handle. */
00183         UpnpDevice_Handle Hnd, 
00184         /* [in] Search type for sending replies. */
00185         enum SsdpSearchType SearchType, 
00186         /* [in] Destination address. */
00187         struct sockaddr *DestAddr,
00188         /* [in] Device type. */
00189         char *DeviceType, 
00190         /* [in] Device UDN. */
00191         char *DeviceUDN, 
00192         /* [in] Service type. */
00193         char *ServiceType,
00194         /* [in] Advertisement age. */
00195         int Exp);
00196 
00203 int unique_service_name(
00204         /* [in] Service Name string. */
00205         char *cmd,
00206         /* [out] The SSDP event structure partially filled by all the
00207          * function. */
00208         SsdpEvent *Evt);
00209 
00217 enum SsdpSearchType ssdp_request_type1(
00218         /* [in] command came in the ssdp request. */
00219         char *cmd);
00220 
00227 int ssdp_request_type(
00228         /* [in] command came in the ssdp request. */
00229         char *cmd,
00230         /* [out] The event structure partially filled by this function. */
00231         SsdpEvent *Evt);
00232 
00236 void readFromSSDPSocket(
00237         /* [in] SSDP socket. */
00238         SOCKET socket);
00239 
00246 int get_ssdp_sockets(
00247         /* [out] Array of SSDP sockets. */
00248         MiniServerSockArray *out);
00249 
00250 /* @} SSDP Server Functions */
00251 
00263 void ssdp_handle_ctrlpt_msg(
00264         /* [in] SSDP message from the device. */
00265         http_message_t *hmsg, 
00266         /* [in] Address of the device. */
00267         struct sockaddr_storage *dest_addr,
00268         /* [in] timeout kept by the control point while sending search message.
00269          * Only in search reply. */
00270         int timeout,
00271         /* [in] Cookie stored by the control point application. This cookie will
00272          * be returned to the control point in the callback.
00273          * Only in search reply. */
00274         void *cookie);
00275 
00293 int SearchByTarget(
00294         /* [in] Number of seconds to wait, to collect all the responses. */
00295         int Mx,
00296         /* [in] Search target. */
00297         char *St,
00298         /* [in] Cookie provided by control point application. This cokie will
00299          * be returned to application in the callback. */
00300         void *Cookie);
00301 
00302 /* @} SSDP Control Point Functions */
00303 
00316 void *advertiseAndReplyThread(
00317         /* [in] Structure containing the search request. */
00318         void *data);
00319 
00325 #ifdef INCLUDE_DEVICE_APIS
00326 void ssdp_handle_device_request(
00327         /* [in] . */
00328         http_message_t *hmsg, 
00329         /* [in] . */
00330         struct sockaddr_storage *dest_addr);
00331 #else /* INCLUDE_DEVICE_APIS */
00332 static UPNP_INLINE void ssdp_handle_device_request(
00333         /* [in] . */
00334         http_message_t *hmsg, 
00335         /* [in] . */
00336         struct sockaddr_storage *dest_addr) {}
00337 #endif /* INCLUDE_DEVICE_APIS */
00338 
00345 int DeviceAdvertisement(
00346         /* [in] type of the device. */
00347         char *DevType,
00348         /* [in] flag to indicate if the device is root device. */
00349         int RootDev,
00350         /* [in] UDN. */
00351         char *Udn, 
00352         /* [in] Location URL. */
00353         char *Location,
00354         /* [in] Service duration in sec. */
00355         int Duration,
00356         /* [in] Device address family. */
00357         int AddressFamily,
00358         /* [in] PowerState as defined by UPnP Low Power. */
00359         int PowerState,
00360         /* [in] SleepPeriod as defined by UPnP Low Power. */
00361         int SleepPeriod,
00362         /* [in] RegistrationState as defined by UPnP Low Power. */
00363         int RegistrationState);
00364 
00371 int SendReply(
00372         /* [in] destination IP address. */
00373         struct sockaddr *DestAddr, 
00374         /* [in] Device type. */
00375         char *DevType, 
00376         /* [in] 1 means root device 0 means embedded device. */
00377         int RootDev, 
00378         /* [in] Device UDN. */
00379         char *Udn, 
00380         /* [in] Location of Device description document. */
00381         char *Location, 
00382         /* [in] Life time of this device. */
00383         int Duration, 
00384         /* [in] . */
00385         int ByType,
00386         /* [in] PowerState as defined by UPnP Low Power. */
00387         int PowerState,
00388         /* [in] SleepPeriod as defined by UPnP Low Power. */
00389         int SleepPeriod,
00390         /* [in] RegistrationState as defined by UPnP Low Power. */
00391         int RegistrationState);
00392 
00399 int DeviceReply(
00400         /* [in] destination IP address. */
00401         struct sockaddr *DestAddr, 
00402         /* [in] Device type. */
00403         char *DevType, 
00404         /* [in] 1 means root device 0 means embedded device. */
00405         int RootDev, 
00406         /* [in] Device UDN. */
00407         char *Udn, 
00408         /* [in] Location of Device description document. */
00409         char *Location, 
00410         /* [in] Life time of this device. */
00411         int Duration,
00412         /* [in] PowerState as defined by UPnP Low Power. */
00413         int PowerState,
00414         /* [in] SleepPeriod as defined by UPnP Low Power. */
00415         int SleepPeriod,
00416         /* [in] RegistrationState as defined by UPnP Low Power. */
00417         int RegistrationState);
00418 
00425 int ServiceAdvertisement(
00426         /* [in] Device UDN. */
00427         char *Udn, 
00428         /* [in] Service Type. */
00429         char *ServType,
00430         /* [in] Location of Device description document. */
00431         char *Location,
00432         /* [in] Life time of this device. */
00433         int Duration,
00434         /* [in] Device address family. */
00435         int AddressFamily,
00436         /* [in] PowerState as defined by UPnP Low Power. */
00437         int PowerState,
00438         /* [in] SleepPeriod as defined by UPnP Low Power. */
00439         int SleepPeriod,
00440         /* [in] RegistrationState as defined by UPnP Low Power. */
00441         int RegistrationState);
00442 
00449 int ServiceReply(
00450         /* [in] . */
00451         struct sockaddr *DestAddr,  
00452         /* [in] Service Type. */
00453         char *ServType, 
00454         /* [in] Device UDN. */
00455         char *Udn, 
00456         /* [in] Location of Device description document. */
00457         char *Location,
00458         /* [in] Life time of this device. */
00459         int Duration,
00460         /* [in] PowerState as defined by UPnP Low Power. */
00461         int PowerState,
00462         /* [in] SleepPeriod as defined by UPnP Low Power. */
00463         int SleepPeriod,
00464         /* [in] RegistrationState as defined by UPnP Low Power. */
00465         int RegistrationState);
00466 
00473 int ServiceShutdown(
00474         /* [in] Device UDN. */
00475         char *Udn,
00476         /* [in] Service Type. */
00477         char *ServType,
00478         /* [in] Location of Device description document. */
00479         char *Location,
00480         /* [in] Service duration in sec. */
00481         int Duration,
00482         /* [in] Device address family. */
00483         int AddressFamily,
00484         /* [in] PowerState as defined by UPnP Low Power. */
00485         int PowerState,
00486         /* [in] SleepPeriod as defined by UPnP Low Power. */
00487         int SleepPeriod,
00488         /* [in] RegistrationState as defined by UPnP Low Power. */
00489         int RegistrationState);
00490 
00497 int DeviceShutdown(
00498         /* [in] Device Type. */
00499         char *DevType, 
00500         /* [in] 1 means root device. */
00501         int RootDev,
00502         /* [in] Device UDN. */
00503         char *Udn, 
00504         /* [in] Location URL. */
00505         char *Location, 
00506         /* [in] Device duration in sec. */
00507         int Duration,
00508         /* [in] Device address family. */
00509         int AddressFamily,
00510         /* [in] PowerState as defined by UPnP Low Power. */
00511         int PowerState,
00512         /* [in] SleepPeriod as defined by UPnP Low Power. */
00513         int SleepPeriod,
00514         /* [in] RegistrationState as defined by UPnP Low Power. */
00515         int RegistrationState);
00516 
00517 /* @} SSDP Device Functions */
00518 
00519 /* @} SSDPlib SSDP Library */
00520 
00521 #endif /* SSDPLIB_H */