This commit is contained in:
Marcelo Roberto Jimenez 2010-12-18 20:01:49 -02:00
parent 3ad98ad746
commit 6d7b3e65f8

View File

@ -1,3 +1,6 @@
#ifndef SSDPLIB_H
#define SSDPLIB_H
/************************************************************************** /**************************************************************************
* *
* Copyright (c) 2000-2003 Intel Corporation * Copyright (c) 2000-2003 Intel Corporation
@ -29,22 +32,21 @@
* *
**************************************************************************/ **************************************************************************/
#ifndef SSDPLIB_H /*!
#define SSDPLIB_H * \file
*/
#include "httpparser.h" #include "httpparser.h"
#include "httpreadwrite.h" #include "httpreadwrite.h"
#include "miniserver.h" #include "miniserver.h"
#include "UpnpInet.h" #include "UpnpInet.h"
#include <sys/types.h> #include <sys/types.h>
#include <signal.h> #include <signal.h>
#include <setjmp.h> #include <setjmp.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#ifdef WIN32 #ifdef WIN32
#else /* WIN32 */ #else /* WIN32 */
#include <syslog.h> #include <syslog.h>
@ -58,8 +60,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#endif /* WIN32 */ #endif /* WIN32 */
/*! Enumeration to define all different types of ssdp searches */
/* Enumeration to define all different types of ssdp searches */
typedef enum SsdpSearchType{ typedef enum SsdpSearchType{
SSDP_SERROR=-1, SSDP_SERROR=-1,
SSDP_ALL,SSDP_ROOTDEVICE, SSDP_ALL,SSDP_ROOTDEVICE,
@ -68,8 +69,7 @@ typedef enum SsdpSearchType{
SSDP_SERVICE SSDP_SERVICE
} SType; } SType;
/*! Enumeration to define all different type of ssdp messages */
/* Enumeration to define all different type of ssdp messages */
typedef enum SsdpCmdType{ typedef enum SsdpCmdType{
SSDP_ERROR=-1, SSDP_ERROR=-1,
SSDP_OK, SSDP_OK,
@ -80,9 +80,6 @@ typedef enum SsdpCmdType{
SSDP_TIMEOUT SSDP_TIMEOUT
} Cmd; } Cmd;
/* Constant */
#define BUFSIZE 2500 #define BUFSIZE 2500
#define SSDP_IP "239.255.255.250" #define SSDP_IP "239.255.255.250"
#define SSDP_IPV6_LINKLOCAL "FF02::C" #define SSDP_IPV6_LINKLOCAL "FF02::C"
@ -92,44 +89,45 @@ typedef enum SsdpCmdType{
#define THREAD_LIMIT 50 #define THREAD_LIMIT 50
#define COMMAND_LEN 300 #define COMMAND_LEN 300
/* can be overwritten by configure CFLAGS argument */ /*! can be overwritten by configure CFLAGS argument. */
#ifndef X_USER_AGENT #ifndef X_USER_AGENT
/** @name X_USER_AGENT /*! @name X_USER_AGENT
* The {\tt X_USER_AGENT} constant specifies the value of the X-User-Agent: * The {\tt X_USER_AGENT} constant specifies the value of the X-User-Agent:
* HTTP header. The value "redsonic" is needed for the DSM-320. See * HTTP header. The value "redsonic" is needed for the DSM-320. See
* https://sourceforge.net/forum/message.php?msg_id=3166856 for more * https://sourceforge.net/forum/message.php?msg_id=3166856 for more
* information * information
*/ */
#define X_USER_AGENT "redsonic" #define X_USER_AGENT "redsonic"
#endif #endif
/* Error code */ /*! Error codes. */
#define NO_ERROR_FOUND 0 #define NO_ERROR_FOUND 0
#define E_REQUEST_INVALID -3 #define E_REQUEST_INVALID -3
#define E_RES_EXPIRED -4 #define E_RES_EXPIRED -4
#define E_MEM_ALLOC -5 #define E_MEM_ALLOC -5
#define E_HTTP_SYNTEX -6 #define E_HTTP_SYNTEX -6
#define E_SOCKET -7 #define E_SOCKET -7
#define RQST_TIMEOUT 20 #define RQST_TIMEOUT 20
/*! Structure to store the SSDP information */ /*! Structure to store the SSDP information */
typedef struct SsdpEventStruct { typedef struct SsdpEventStruct {
enum SsdpCmdType Cmd; enum SsdpCmdType Cmd;
enum SsdpSearchType RequestType; enum SsdpSearchType RequestType;
int ErrCode; int ErrCode;
int MaxAge; int MaxAge;
int Mx; int Mx;
char UDN[LINE_SIZE]; char UDN[LINE_SIZE];
char DeviceType[LINE_SIZE]; char DeviceType[LINE_SIZE];
/* NT or ST */ /* NT or ST */
char ServiceType[LINE_SIZE]; char ServiceType[LINE_SIZE];
char Location[LINE_SIZE]; char Location[LINE_SIZE];
char HostAddr[LINE_SIZE]; char HostAddr[LINE_SIZE];
char Os[LINE_SIZE]; char Os[LINE_SIZE];
char Ext[LINE_SIZE]; char Ext[LINE_SIZE];
char Date[LINE_SIZE]; char Date[LINE_SIZE];
struct sockaddr *DestAddr; struct sockaddr *DestAddr;
void * Cookie; void * Cookie;
} Event; } Event;
typedef void (* SsdpFunPtr)(Event *); typedef void (* SsdpFunPtr)(Event *);
@ -138,38 +136,35 @@ typedef Event SsdpEvent ;
typedef struct TData typedef struct TData
{ {
int Mx; int Mx;
void * Cookie; void * Cookie;
char * Data; char * Data;
struct sockaddr_storage DestAddr; struct sockaddr_storage DestAddr;
} ThreadData; } ThreadData;
typedef struct ssdpsearchreply typedef struct ssdpsearchreply
{ {
int MaxAge; int MaxAge;
UpnpDevice_Handle handle; UpnpDevice_Handle handle;
struct sockaddr_storage dest_addr; struct sockaddr_storage dest_addr;
SsdpEvent event; SsdpEvent event;
} SsdpSearchReply; } SsdpSearchReply;
typedef struct ssdpsearcharg typedef struct ssdpsearcharg
{ {
int timeoutEventId; int timeoutEventId;
char * searchTarget; char * searchTarget;
void *cookie; void *cookie;
enum SsdpSearchType requestType; enum SsdpSearchType requestType;
} SsdpSearchArg; } SsdpSearchArg;
typedef struct typedef struct
{ {
http_parser_t parser; http_parser_t parser;
struct sockaddr_storage dest_addr; struct sockaddr_storage dest_addr;
} ssdp_thread_data; } ssdp_thread_data;
/* globals */ /* globals */
#ifdef INCLUDE_CLIENT_APIS #ifdef INCLUDE_CLIENT_APIS
@ -180,399 +175,288 @@ typedef struct
#endif /* INCLUDE_CLIENT_APIS */ #endif /* INCLUDE_CLIENT_APIS */
typedef int (*ParserFun)(char *, Event *); typedef int (*ParserFun)(char *, Event *);
/*!
* \brief Make ssdp socket non-blocking.
*
* \return 0 if successful, -1 otherwise.
*/
int Make_Socket_NoBlocking(
/* [in] socket. */
SOCKET sock);
/************************************************************************ /*!
* Function : Make_Socket_NoBlocking * \brief Handles the search request. It does the sanity checks of the
* * request and then schedules a thread to send a random time reply
* Parameters: * (random within maximum time given by the control point to reply).
* IN int sock: socket */
*
* Description:
* This function to make ssdp socket non-blocking.
*
* Returns: int
* 0 if successful else -1
***************************************************************************/
int Make_Socket_NoBlocking (SOCKET sock);
/************************************************************************
* Function : ssdp_handle_device_request
*
* Parameters:
* IN void *data:
*
* Description:
* This function handles the search request. It do the sanity checks of
* the request and then schedules a thread to send a random time reply (
* random within maximum time given by the control point to reply).
*
* Returns: void *
* 1 if successful else appropriate error
***************************************************************************/
#ifdef INCLUDE_DEVICE_APIS #ifdef INCLUDE_DEVICE_APIS
void ssdp_handle_device_request( void ssdp_handle_device_request(
IN http_message_t *hmsg, /* [in] . */
IN struct sockaddr_storage *dest_addr); http_message_t *hmsg,
/* [in] . */
struct sockaddr_storage *dest_addr);
#else #else
static inline void ssdp_handle_device_request( static inline void ssdp_handle_device_request(
IN http_message_t *hmsg, /* [in] . */
IN struct sockaddr_storage *dest_addr) {} http_message_t *hmsg,
/* [in] . */
struct sockaddr_storage *dest_addr) {}
#endif #endif
/************************************************************************ /*!
* Function : ssdp_handle_ctrlpt_msg * \brief This function handles the ssdp messages from the devices. These
* * messages includes the search replies, advertisement of device coming alive
* Parameters: * and bye byes.
* IN http_message_t* hmsg: SSDP message from the device */
* IN struct sockaddr* dest_addr: Address of the device
* IN int timeout: timeout kept by the control point while sending
* search message
* IN void* cookie: Cookie stored by the control point application.
* This cookie will be returned to the control point
* in the callback
*
* Description:
* This function handles the ssdp messages from the devices. These
* messages includes the search replies, advertisement of device coming
* alive and bye byes.
*
* Returns: void
*
***************************************************************************/
void ssdp_handle_ctrlpt_msg( void ssdp_handle_ctrlpt_msg(
IN http_message_t *hmsg, /* [in] SSDP message from the device. */
IN struct sockaddr_storage *dest_addr, http_message_t *hmsg,
IN int timeout, /* [in] Address of the device. */
IN void *cookie); struct sockaddr_storage *dest_addr,
/* [in] timeout kept by the control point while sending search message. */
int timeout,
/* [in] Cookie stored by the control point application. This cookie will
* be returned to the control point in the callback. */
void *cookie);
/************************************************************************ /*!
* Function : unique_service_name * \brief Fills the fields of the event structure like DeviceType, Device UDN
* * and Service Type.
* Parameters: *
* IN char *cmd: Service Name string * \return 0 if successful else -1.
* OUT SsdpEvent *Evt: The SSDP event structure partially filled */
* by all the function. int unique_service_name(
* /* [in] Service Name string. */
* Description: char *cmd,
* This function fills the fields of the event structure like DeviceType, /* [out] The SSDP event structure partially filled by all the
* Device UDN and Service Type * function. */
* SsdpEvent *Evt);
* Returns: int
* 0 if successful else -1 /*!
***************************************************************************/ * \brief Creates the ssdp sockets. It set their option to listen for
int unique_service_name(char * cmd, SsdpEvent * Evt); * multicast traffic.
*
* \return UPNP_E_SUCCESS if successful else returns appropriate error.
*/
int get_ssdp_sockets(
/* [out] Array of SSDP sockets. */
MiniServerSockArray *out);
/*!
* \brief This function reads the data from the ssdp socket.
*/
void readFromSSDPSocket(
/* [in] SSDP socket. */
SOCKET socket);
/************************************************************************ /*!
* Function : get_ssdp_sockets * \brief This function figures out the type of the SSDP search in the in the
* * request.
* Parameters: *
* OUT MiniServerSockArray *out: Arrays of SSDP sockets * \return enum SsdpSearchType. Returns appropriate search type,
* * else returns SSDP_ERROR
* Description: */
* This function creates the ssdp sockets. It set their option to listen enum SsdpSearchType ssdp_request_type1(
* for multicast traffic. /* [in] command came in the ssdp request. */
* char *cmd);
* Returns: int
* return UPNP_E_SUCCESS if successful else returns appropriate error
***************************************************************************/
int get_ssdp_sockets(MiniServerSockArray *out);
/************************************************************************ /*!
* Function : readFromSSDPSocket * \brief Starts filling the SSDP event structure based upon the
* * request received.
* Parameters: *
* IN SOCKET socket: SSDP socket * \return 0 on success; -1 on error.
* */
* Description: int ssdp_request_type(
* This function reads the data from the ssdp socket. /* [in] command came in the ssdp request. */
* char *cmd,
* Returns: void /* [out] The event structure partially filled by this function. */
* SsdpEvent *Evt);
***************************************************************************/
void readFromSSDPSocket(SOCKET socket);
/*!
* \brief Creates and send the search request for a specific URL.
*
* \return 1 if successful else appropriate error.
*/
int SearchByTarget(
/* [in] Number of seconds to wait, to collect all the responses. */
int Mx,
/* [in] Search target. */
char *St,
/* [in] Cookie provided by control point application. This cokie will
* be returned to application in the callback. */
void *Cookie);
/************************************************************************ /*!
* Function : ssdp_request_type1 * \brief Creates the device advertisement request based on the input
* * parameter, and send it to the multicast channel.
* Parameters: *
* IN char *cmd: command came in the ssdp request * \return UPNP_E_SUCCESS if successful else appropriate error.
* */
* Description:
* This function figures out the type of the SSDP search in the
* in the request.
*
* Returns: enum SsdpSearchType
* return appropriate search type else returns SSDP_ERROR
***************************************************************************/
enum SsdpSearchType ssdp_request_type1(IN char *cmd);
/************************************************************************
* Function : ssdp_request_type
*
* Parameters:
* IN char *cmd: command came in the ssdp request
* OUT SsdpEvent *Evt: The event structure partially filled by
* this function.
*
* Description:
* This function starts filling the SSDP event structure based upon the
* request received.
*
* Returns: int
* 0 on success; -1 on error
***************************************************************************/
int ssdp_request_type(IN char * cmd, OUT SsdpEvent * Evt);
/************************************************************************
* Function : SearchByTarget
*
* Parameters:
* IN int Mx:Number of seconds to wait, to collect all the responses.
* char *St: Search target.
* void *Cookie: cookie provided by control point application. This
* cokie will be returned to application in the callback.
*
* Description:
* This function creates and send the search request for a specific URL.
*
* Returns: int
* 1 if successful else appropriate error
***************************************************************************/
int SearchByTarget(IN int Mx, IN char *St, IN void *Cookie);
/************************************************************************
* Function : DeviceAdvertisement
*
* Parameters:
* IN char *DevType : type of the device
* IN int RootDev : flag to indicate if the device is root device
* IN char *Udn :
* IN char *Location: Location URL.
* IN int Duration : Service duration in sec.
* IN int AddressFamily: Device address family.
*
* Description:
* This function creates the device advertisement request based on
* the input parameter, and send it to the multicast channel.
*
* Returns: int
* UPNP_E_SUCCESS if successful else appropriate error
***************************************************************************/
int DeviceAdvertisement( int DeviceAdvertisement(
IN char *DevType, /* [in] type of the device. */
IN int RootDev, char *DevType,
IN char *Udn, /* [in] flag to indicate if the device is root device. */
IN char *Location, int RootDev,
IN int Duration, /* [in] UDN. */
IN int AddressFamily); char *Udn,
/* [in] Location URL. */
char *Location,
/* [in] Service duration in sec. */
int Duration,
/* [in] Device address family. */
int AddressFamily);
/*!
/************************************************************************ * \brief Creates a HTTP device shutdown request packet and send it to the
* Function : DeviceShutdown * multicast channel through RequestHandler.
* *
* Parameters: * \return UPNP_E_SUCCESS if successful else appropriate error.
* IN char *DevType: Device Type. */
* IN int RootDev:1 means root device.
* IN char *Udn: Device UDN
* IN char *_Server:
* IN char *Location: Location URL
* IN int Duration :Device duration in sec.
* IN int AddressFamily: Device address family.
*
* Description:
* This function creates a HTTP device shutdown request packet
* and sent it to the multicast channel through RequestHandler.
*
* Returns: int
* UPNP_E_SUCCESS if successful else appropriate error
***************************************************************************/
int DeviceShutdown( int DeviceShutdown(
IN char *DevType, /* [in] Device Type. */
IN int RootDev, char *DevType,
IN char *Udn, /* [in] 1 means root device. */
IN char *_Server, int RootDev,
IN char *Location, /* [in] Device UDN. */
IN int Duration, char *Udn,
IN int AddressFamily); /* [in] . */
char *_Server,
/* [in] Location URL. */
char *Location,
/* [in] Device duration in sec. */
int Duration,
/* [in] Device address family. */
int AddressFamily);
/************************************************************************ /*!
* Function : DeviceReply * \brief Creates the reply packet based on the input parameter, and send it
* * to the client address given in its input parameter DestAddr.
* Parameters: *
* IN struct sockaddr *DestAddr: destination IP address. * \return UPNP_E_SUCCESS if successful else appropriate error.
* IN char *DevType: Device type */
* IN int RootDev: 1 means root device 0 means embedded device.
* IN char *Udn: Device UDN
* IN char *Location: Location of Device description document.
* IN int Duration :Life time of this device.
*
* Description:
* This function creates the reply packet based on the input parameter,
* and send it to the client address given in its input parameter DestAddr.
*
* Returns: int
* UPNP_E_SUCCESS if successful else appropriate error
***************************************************************************/
int DeviceReply( int DeviceReply(
IN struct sockaddr *DestAddr, /* [in] destination IP address. */
IN char *DevType, struct sockaddr *DestAddr,
IN int RootDev, /* [in] Device type. */
IN char *Udn, char *DevType,
IN char *Location, /* [in] 1 means root device 0 means embedded device. */
IN int Duration); int RootDev,
/* [in] Device UDN. */
char *Udn,
/* [in] Location of Device description document. */
char *Location,
/* [in] Life time of this device. */
int Duration);
/************************************************************************ /*!
* Function : SendReply * \brief Creates the reply packet based on the input parameter, and send it
* * to the client addesss given in its input parameter DestAddr.
* Parameters: *
* IN struct sockaddr *DestAddr: destination IP address. * \return UPNP_E_SUCCESS if successful else appropriate error.
* IN char *DevType: Device type */
* IN int RootDev: 1 means root device 0 means embedded device.
* IN char * Udn: Device UDN
* IN char *_Server:
* IN char *Location: Location of Device description document.
* IN int Duration :Life time of this device.
* IN int ByType:
*
* Description:
* This function creates the reply packet based on the input parameter,
* and send it to the client addesss given in its input parameter DestAddr.
*
* Returns: int
* UPNP_E_SUCCESS if successful else appropriate error
***************************************************************************/
int SendReply( int SendReply(
IN struct sockaddr *DestAddr, /* [in] destination IP address. */
IN char *DevType, struct sockaddr *DestAddr,
IN int RootDev, /* [in] Device type. */
IN char *Udn, char *DevType,
IN char *Location, /* [in] 1 means root device 0 means embedded device. */
IN int Duration, int RootDev,
IN int ByType ); /* [in] Device UDN. */
char *Udn,
/* [in] Location of Device description document. */
char *Location,
/* [in] Life time of this device. */
int Duration,
/* [in] . */
int ByType );
/************************************************************************ /*!
* Function : ServiceAdvertisement * \brief Creates the advertisement packet based on the input parameter,
* * and send it to the multicast channel.
* Parameters: *
* IN char * Udn: Device UDN * \return UPNP_E_SUCCESS if successful else appropriate error.
* IN char *ServType: Service Type. */
* IN char * Location: Location of Device description document.
* IN int Duration: Life time of this device.
* IN int AddressFamily: Device address family
*
* Description:
* This function creates the advertisement packet based
* on the input parameter, and send it to the multicast channel.
*
* Returns: int
* UPNP_E_SUCCESS if successful else appropriate error
***************************************************************************/
int ServiceAdvertisement( int ServiceAdvertisement(
IN char *Udn, /* [in] Device UDN. */
IN char *ServType, char *Udn,
IN char *Location, /* [in] Service Type. */
IN int Duration, char *ServType,
IN int AddressFamily); /* [in] Location of Device description document. */
char *Location,
/* [in] Life time of this device. */
int Duration,
/* [in] Device address family. */
int AddressFamily);
/************************************************************************ /*!
* Function : ServiceReply * \brief Creates the advertisement packet based on the input parameter,
* * and send it to the multicast channel.
* Parameters: *
* IN struct sockaddr *DestAddr: * \return UPNP_E_SUCCESS if successful else appropriate error.
* IN char *Udn: Device UDN */
* IN char *ServType: Service Type.
* IN char *Server: Not used
* IN char *Location: Location of Device description document.
* IN int Duration :Life time of this device.
*
* Description:
* This function creates the advertisement packet based
* on the input parameter, and send it to the multicast channel.
*
* Returns: int
* UPNP_E_SUCCESS if successful else appropriate error
***************************************************************************/
int ServiceReply( int ServiceReply(
IN struct sockaddr *DestAddr, /* [in] . */
IN char *ServType, struct sockaddr *DestAddr,
IN char *Udn, /* [in] Service Type. */
IN char *Location, char *ServType,
IN int Duration); /* [in] Device UDN. */
char *Udn,
/* [in] Location of Device description document. */
char *Location,
/* [in] Life time of this device. */
int Duration);
/************************************************************************ /*!
* Function : ServiceShutdown * \brief Creates a HTTP service shutdown request packet and sends it to the
* * multicast channel through RequestHandler.
* Parameters: *
* IN char *Udn: Device UDN * \return UPNP_E_SUCCESS if successful else appropriate error.
* IN char *ServType: Service Type. */
* IN char *Location: Location of Device description document.
* IN int Duration :Service duration in sec.
* IN int AddressFamily: Device address family
*
* Description:
* This function creates a HTTP service shutdown request packet
* and sent it to the multicast channel through RequestHandler.
*
* Returns: int
* UPNP_E_SUCCESS if successful else appropriate error
***************************************************************************/
int ServiceShutdown( int ServiceShutdown(
IN char *Udn, /* [in] Device UDN. */
IN char *ServType, char *Udn,
IN char *Location, /* [in] Service Type. */
IN int Duration, char *ServType,
IN int AddressFamily); /* [in] Location of Device description document. */
char *Location,
/* [in] Service duration in sec. */
int Duration,
/* [in] Device address family. */
int AddressFamily);
/*!
* \brief Wrapper function to reply the search request coming from the
* control point.
*
* \return always return NULL
*/
void *advertiseAndReplyThread(
/* [in] Structure containing the search request. */
void *data);
/************************************************************************ /*!
* Function : advertiseAndReplyThread * \brief Sends SSDP advertisements, replies and shutdown messages.
* *
* Parameters: * \return UPNP_E_SUCCESS if successful else appropriate error.
* IN void *data: Structure containing the search request */
*
* Description:
* This function is a wrapper function to reply the search request
* coming from the control point.
*
* Returns: void *
* always return NULL
***************************************************************************/
void *advertiseAndReplyThread(IN void * data);
/************************************************************************
* Function : AdvertiseAndReply
*
* Parameters:
* IN int AdFlag: -1 = Send shutdown,
* 0 = send reply,
* 1 = Send Advertisement
* IN UpnpDevice_Handle Hnd: Device handle
* IN enum SsdpSearchType SearchType:Search type for sending replies
* IN struct sockaddr *DestAddr:Destination address
* IN char *DeviceType:Device type
* IN char *DeviceUDN:Device UDN
* IN char *ServiceType:Service type
* IN int Exp:Advertisement age
*
* Description:
* This function to send SSDP advertisements, replies and shutdown messages.
*
* Returns: int
* UPNP_E_SUCCESS if successful else appropriate error
***************************************************************************/
int AdvertiseAndReply( int AdvertiseAndReply(
IN int AdFlag, /* [in] -1 = Send shutdown, 0 = send reply, 1 = Send Advertisement. */
IN UpnpDevice_Handle Hnd, int AdFlag,
IN enum SsdpSearchType SearchType, /* [in] Device handle. */
IN struct sockaddr *DestAddr, UpnpDevice_Handle Hnd,
IN char *DeviceType, /* [in] Search type for sending replies. */
IN char *DeviceUDN, enum SsdpSearchType SearchType,
IN char *ServiceType, int Exp); /* [in] Destination address. */
struct sockaddr *DestAddr,
/* [in] Device type. */
char *DeviceType,
/* [in] Device UDN. */
char *DeviceUDN,
/* [in] Service type. */
char *ServiceType,
/* [in] Advertisement age. */
int Exp);
#endif /* SSDPLIB_H */ #endif /* SSDPLIB_H */