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 // 00031 00032 #ifndef SSDPLIB_H 00033 #define SSDPLIB_H 00034 00035 #include <sys/types.h> 00036 #include <signal.h> 00037 #include <setjmp.h> 00038 #include <fcntl.h> 00039 #include <errno.h> 00040 #include "httpparser.h" 00041 #include "httpreadwrite.h" 00042 #include "miniserver.h" 00043 #ifndef WIN32 00044 #include <syslog.h> 00045 #include <sys/socket.h> 00046 #include <netinet/in.h> 00047 #include <netinet/in_systm.h> 00048 #include <netinet/ip.h> 00049 #include <netinet/ip_icmp.h> 00050 #include <sys/time.h> 00051 #include <arpa/inet.h> 00052 #else 00053 #include <winsock2.h> 00054 #endif 00055 00056 //Enumeration to define all different types of ssdp searches 00057 typedef enum SsdpSearchType{ 00058 SSDP_SERROR=-1, 00059 SSDP_ALL,SSDP_ROOTDEVICE, 00060 SSDP_DEVICEUDN, 00061 SSDP_DEVICETYPE, 00062 SSDP_SERVICE 00063 } SType; 00064 00065 00066 //Enumeration to define all different type of ssdp messages 00067 typedef enum SsdpCmdType{SSDP_ERROR=-1, 00068 SSDP_OK, 00069 SSDP_ALIVE, 00070 SSDP_BYEBYE, 00071 SSDP_SEARCH, 00072 SSDP_NOTIFY, 00073 SSDP_TIMEOUT 00074 } Cmd; 00075 00076 00077 00078 //Constant 00079 #define BUFSIZE 2500 00080 #define SSDP_IP "239.255.255.250" 00081 #define SSDP_PORT 1900 00082 #define NUM_TRY 3 00083 #define NUM_COPY 1 00084 #define THREAD_LIMIT 50 00085 #define COMMAND_LEN 300 00086 00087 //Error code 00088 #define NO_ERROR_FOUND 0 00089 #define E_REQUEST_INVALID -3 00090 #define E_RES_EXPIRED -4 00091 #define E_MEM_ALLOC -5 00092 #define E_HTTP_SYNTEX -6 00093 #define E_SOCKET -7 00094 #define RQST_TIMEOUT 20 00095 00096 00097 00098 //Structure to store the SSDP information 00099 typedef struct SsdpEventStruct 00100 { 00101 enum SsdpCmdType Cmd; 00102 enum SsdpSearchType RequestType; 00103 int ErrCode; 00104 int MaxAge; 00105 int Mx; 00106 char UDN[LINE_SIZE]; 00107 char DeviceType[LINE_SIZE]; 00108 char ServiceType[LINE_SIZE]; //NT or ST 00109 char Location[LINE_SIZE]; 00110 char HostAddr[LINE_SIZE]; 00111 char Os[LINE_SIZE]; 00112 char Ext[LINE_SIZE]; 00113 char Date[LINE_SIZE]; 00114 struct sockaddr_in * DestAddr; 00115 void * Cookie; 00116 } Event; 00117 00118 typedef void (* SsdpFunPtr)(Event *); 00119 00120 typedef Event SsdpEvent ; 00121 00122 //Structure to contain Discovery response 00123 typedef struct resultData 00124 { 00125 struct Upnp_Discovery param; 00126 void *cookie; 00127 Upnp_FunPtr ctrlpt_callback; 00128 }ResultData; 00129 00130 00131 typedef struct TData 00132 { 00133 int Mx; 00134 void * Cookie; 00135 char * Data; 00136 struct sockaddr_in DestAddr; 00137 00138 }ThreadData; 00139 00140 typedef struct ssdpsearchreply 00141 { 00142 int MaxAge; 00143 UpnpDevice_Handle handle; 00144 struct sockaddr_in dest_addr; 00145 SsdpEvent event; 00146 00147 }SsdpSearchReply; 00148 00149 typedef struct ssdpsearcharg 00150 { 00151 int timeoutEventId; 00152 char * searchTarget; 00153 void *cookie; 00154 enum SsdpSearchType requestType; 00155 } SsdpSearchArg; 00156 00157 00158 typedef struct 00159 { 00160 http_parser_t parser; 00161 struct sockaddr_in dest_addr; 00162 } ssdp_thread_data; 00163 00164 00165 /* globals */ 00166 00167 CLIENTONLY(extern SOCKET gSsdpReqSocket;); 00168 00169 typedef int (*ParserFun)(char *, Event *); 00170 00171 00172 //void InitParser(); 00173 00174 //int AnalyzeCommand(char * szCommand, Event * Evt); 00175 00176 /************************************************************************ 00177 * Function : Make_Socket_NoBlocking 00178 * 00179 * Parameters: 00180 * IN int sock: socket 00181 * 00182 * Description: 00183 * This function to make ssdp socket non-blocking. 00184 * 00185 * Returns: int 00186 * 0 if successful else -1 00187 ***************************************************************************/ 00188 int Make_Socket_NoBlocking (int sock); 00189 00190 /************************************************************************ 00191 * Function : ssdp_handle_device_request 00192 * 00193 * Parameters: 00194 * IN void *data: 00195 * 00196 * Description: 00197 * This function handles the search request. It do the sanity checks of 00198 * the request and then schedules a thread to send a random time reply ( 00199 * random within maximum time given by the control point to reply). 00200 * 00201 * Returns: void * 00202 * 1 if successful else appropriate error 00203 ***************************************************************************/ 00204 void ssdp_handle_device_request( IN http_message_t* hmsg, 00205 IN struct sockaddr_in* dest_addr ); 00206 00207 /************************************************************************ 00208 * Function : ssdp_handle_ctrlpt_msg 00209 * 00210 * Parameters: 00211 * IN http_message_t* hmsg: SSDP message from the device 00212 * IN struct sockaddr_in* dest_addr: Address of the device 00213 * IN xboolean timeout: timeout kept by the control point while sending 00214 * search message 00215 * IN void* cookie: Cookie stored by the control point application. 00216 * This cookie will be returned to the control point 00217 * in the callback 00218 * 00219 * Description: 00220 * This function handles the ssdp messages from the devices. These 00221 * messages includes the search replies, advertisement of device coming 00222 * alive and bye byes. 00223 * 00224 * Returns: void 00225 * 00226 ***************************************************************************/ 00227 void ssdp_handle_ctrlpt_msg( IN http_message_t* hmsg, 00228 IN struct sockaddr_in* dest_addr, 00229 IN xboolean timeout, 00230 IN void* cookie ); 00231 00232 /************************************************************************ 00233 * Function : unique_service_name 00234 * 00235 * Parameters: 00236 * IN char *cmd: Service Name string 00237 * OUT SsdpEvent *Evt: The SSDP event structure partially filled 00238 * by all the function. 00239 * 00240 * Description: 00241 * This function fills the fields of the event structure like DeviceType, 00242 * Device UDN and Service Type 00243 * 00244 * Returns: int 00245 * 0 if successful else -1 00246 ***************************************************************************/ 00247 int unique_service_name(char * cmd, SsdpEvent * Evt); 00248 00249 00250 /************************************************************************ 00251 * Function : get_ssdp_sockets 00252 * 00253 * Parameters: 00254 * OUT MiniServerSockArray *out: Arrays of SSDP sockets 00255 * 00256 * Description: 00257 * This function creates the ssdp sockets. It set their option to listen 00258 * for multicast traffic. 00259 * 00260 * Returns: int 00261 * return UPNP_E_SUCCESS if successful else returns appropriate error 00262 ***************************************************************************/ 00263 int get_ssdp_sockets(MiniServerSockArray *out); 00264 00265 00266 /************************************************************************ 00267 * Function : readFromSSDPSocket 00268 * 00269 * Parameters: 00270 * IN SOCKET socket: SSDP socket 00271 * 00272 * Description: 00273 * This function reads the data from the ssdp socket. 00274 * 00275 * Returns: void 00276 * 00277 ***************************************************************************/ 00278 void readFromSSDPSocket(SOCKET socket); 00279 00280 00281 /************************************************************************ 00282 * Function : ssdp_request_type1 00283 * 00284 * Parameters: 00285 * IN char *cmd: command came in the ssdp request 00286 * 00287 * Description: 00288 * This function figures out the type of the SSDP search in the 00289 * in the request. 00290 * 00291 * Returns: enum SsdpSearchType 00292 * return appropriate search type else returns SSDP_ERROR 00293 ***************************************************************************/ 00294 enum SsdpSearchType ssdp_request_type1(IN char *cmd); 00295 00296 00297 /************************************************************************ 00298 * Function : ssdp_request_type 00299 * 00300 * Parameters: 00301 * IN char *cmd: command came in the ssdp request 00302 * OUT SsdpEvent *Evt: The event structure partially filled by 00303 * this function. 00304 * 00305 * Description: 00306 * This function starts filling the SSDP event structure based upon the 00307 * request received. 00308 * 00309 * Returns: int 00310 * 0 on success; -1 on error 00311 ***************************************************************************/ 00312 int ssdp_request_type(IN char * cmd, OUT SsdpEvent * Evt); 00313 00314 00315 /************************************************************************ 00316 * Function : SearchByTarget 00317 * 00318 * Parameters: 00319 * IN int Mx:Number of seconds to wait, to collect all the 00320 * responses. 00321 * char *St: Search target. 00322 * void *Cookie: cookie provided by control point application. This 00323 * cokie will be returned to application in the 00324 * callback. 00325 * 00326 * Description: 00327 * This function creates and send the search request for a specific URL. 00328 * 00329 * Returns: int 00330 * 1 if successful else appropriate error 00331 ***************************************************************************/ 00332 int SearchByTarget(IN int Mx, IN char *St, IN void *Cookie); 00333 00334 /************************************************************************ 00335 * Function : DeviceAdvertisement 00336 * 00337 * Parameters: 00338 * IN char * DevType : type of the device 00339 * IN int RootDev: flag to indicate if the device is root device 00340 * IN char * nt : value of NT 00341 * IN char * usn : 00342 * IN char * location :Location URL. 00343 * IN int duration :Service duration in sec. 00344 * 00345 * Description: 00346 * This function creates the device advertisement request based on 00347 * the input parameter, and send it to the multicast channel. 00348 * 00349 * Returns: int 00350 * UPNP_E_SUCCESS if successful else appropriate error 00351 ***************************************************************************/ 00352 int DeviceAdvertisement(IN char * DevType, int RootDev,char * Udn, 00353 IN char * Location, IN int Duration); 00354 00355 00356 /************************************************************************ 00357 * Function : DeviceShutdown 00358 * 00359 * Parameters: 00360 * IN char *DevType: Device Type. 00361 * IN int RootDev:1 means root device. 00362 * IN char * Udn: Device UDN 00363 * IN char *_Server: 00364 * IN char * Location: Location URL 00365 * IN int Duration :Device duration in sec. 00366 * 00367 * Description: 00368 * This function creates a HTTP device shutdown request packet 00369 * and sent it to the multicast channel through RequestHandler. 00370 * 00371 * Returns: int 00372 * UPNP_E_SUCCESS if successful else appropriate error 00373 ***************************************************************************/ 00374 int DeviceShutdown( IN char * DevType, 00375 IN int RootDev, 00376 IN char * Udn, 00377 IN char * _Server, 00378 IN char * Location, 00379 IN int Duration ); 00380 00381 /************************************************************************ 00382 * Function : DeviceReply 00383 * 00384 * Parameters: 00385 * IN struct sockaddr_in * DestAddr:destination IP address. 00386 * IN char *DevType: Device type 00387 * IN int RootDev: 1 means root device 0 means embedded device. 00388 * IN char * Udn: Device UDN 00389 * IN char * Location: Location of Device description document. 00390 * IN int Duration :Life time of this device. 00391 00392 * Description: 00393 * This function creates the reply packet based on the input parameter, 00394 * and send it to the client address given in its input parameter DestAddr. 00395 * 00396 * Returns: int 00397 * UPNP_E_SUCCESS if successful else appropriate error 00398 ***************************************************************************/ 00399 int DeviceReply(IN struct sockaddr_in * DestAddr, 00400 IN char *DevType, 00401 IN int RootDev, 00402 IN char * Udn, 00403 IN char * Location, IN int Duration); 00404 00405 /************************************************************************ 00406 * Function : SendReply 00407 * 00408 * Parameters: 00409 * IN struct sockaddr_in * DestAddr:destination IP address. 00410 * IN char *DevType: Device type 00411 * IN int RootDev: 1 means root device 0 means embedded device. 00412 * IN char * Udn: Device UDN 00413 * IN char *_Server: 00414 * IN char * Location: Location of Device description document. 00415 * IN int Duration :Life time of this device. 00416 * IN int ByType: 00417 * 00418 * Description: 00419 * This function creates the reply packet based on the input parameter, 00420 * and send it to the client addesss given in its input parameter DestAddr. 00421 * 00422 * Returns: int 00423 * UPNP_E_SUCCESS if successful else appropriate error 00424 ***************************************************************************/ 00425 int SendReply(IN struct sockaddr_in * DestAddr, 00426 IN char *DevType, 00427 IN int RootDev, 00428 IN char * Udn, 00429 IN char * Location, 00430 IN int Duration, 00431 IN int ByType ); 00432 00433 /************************************************************************ 00434 * Function : ServiceAdvertisement 00435 * 00436 * Parameters: 00437 * IN char * Udn: Device UDN 00438 * IN char *ServType: Service Type. 00439 * IN char * Location: Location of Device description document. 00440 * IN int Duration :Life time of this device. 00441 00442 * Description: 00443 * This function creates the advertisement packet based 00444 * on the input parameter, and send it to the multicast channel. 00445 00446 * 00447 * Returns: int 00448 * UPNP_E_SUCCESS if successful else appropriate error 00449 ***************************************************************************/ 00450 int ServiceAdvertisement( IN char * Udn, 00451 IN char * ServType, 00452 IN char * Location, 00453 IN int Duration); 00454 00455 /************************************************************************ 00456 * Function : ServiceReply 00457 * 00458 * Parameters: 00459 * IN struct sockaddr_in *DestAddr: 00460 * IN char * Udn: Device UDN 00461 * IN char *ServType: Service Type. 00462 * IN char *Server: Not used 00463 * IN char * Location: Location of Device description document. 00464 * IN int Duration :Life time of this device. 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 * 00471 * Returns: int 00472 * UPNP_E_SUCCESS if successful else appropriate error 00473 ***************************************************************************/ 00474 int ServiceReply(IN struct sockaddr_in *DestAddr, 00475 IN char * ServType, 00476 IN char * Udn, 00477 IN char * Location, 00478 IN int Duration); 00479 00480 /************************************************************************ 00481 * Function : ServiceShutdown 00482 * 00483 * Parameters: 00484 * IN char * Udn: Device UDN 00485 * IN char *ServType: Service Type. 00486 * IN char * Location: Location of Device description document. 00487 * IN int Duration :Service duration in sec. 00488 00489 * Description: 00490 * This function creates a HTTP service shutdown request packet 00491 * and sent it to the multicast channel through RequestHandler. 00492 * 00493 * Returns: int 00494 * UPNP_E_SUCCESS if successful else appropriate error 00495 ***************************************************************************/ 00496 int ServiceShutdown( IN char * Udn, IN char * ServType, 00497 IN char * Location, 00498 IN int Duration); 00499 00500 00501 /************************************************************************ 00502 * Function : advertiseAndReplyThread 00503 * 00504 * Parameters: 00505 * IN void *data: Structure containing the search request 00506 * 00507 * Description: 00508 * This function is a wrapper function to reply the search request 00509 * coming from the control point. 00510 * 00511 * Returns: void * 00512 * always return NULL 00513 ***************************************************************************/ 00514 void * advertiseAndReplyThread(IN void * data); 00515 00516 /************************************************************************ 00517 * Function : AdvertiseAndReply 00518 * 00519 * Parameters: 00520 * IN int AdFlag: -1 = Send shutdown, 0 = send reply, 00521 * 1 = Send Advertisement 00522 * IN UpnpDevice_Handle Hnd: Device handle 00523 * IN enum SsdpSearchType SearchType:Search type for sending replies 00524 * IN struct sockaddr_in *DestAddr:Destination address 00525 * IN char *DeviceType:Device type 00526 * IN char *DeviceUDN:Device UDN 00527 * IN char *ServiceType:Service type 00528 * IN int Exp:Advertisement age 00529 * 00530 * Description: 00531 * This function to send SSDP advertisements, replies and shutdown messages. 00532 * 00533 * Returns: int 00534 * UPNP_E_SUCCESS if successful else appropriate error 00535 ***************************************************************************/ 00536 int AdvertiseAndReply(IN int AdFlag, 00537 IN UpnpDevice_Handle Hnd, 00538 IN enum SsdpSearchType SearchType, 00539 IN struct sockaddr_in *DestAddr, 00540 IN char *DeviceType, 00541 IN char *DeviceUDN, 00542 IN char *ServiceType, int Exp); 00543 00544 #endif
1.6.1