Doxygenation of SSDP library.

This commit is contained in:
Marcelo Roberto Jimenez 2010-12-19 13:30:32 -02:00
parent 704dca3df1
commit 04d64a893b
8 changed files with 1503 additions and 1986 deletions

View File

@ -1,3 +1,6 @@
#ifndef UPNP_H
#define UPNP_H
/******************************************************************************* /*******************************************************************************
* *
* Copyright (c) 2000-2003 Intel Corporation * Copyright (c) 2000-2003 Intel Corporation
@ -29,11 +32,6 @@
* *
******************************************************************************/ ******************************************************************************/
#ifndef UPNP_H
#define UPNP_H
/*! /*!
* \file * \file
* *
@ -42,13 +40,11 @@
* @{ * @{
*/ */
#include "ixml.h" #include "ixml.h"
#include "upnpconfig.h" #include "upnpconfig.h"
#include "UpnpGlobal.h" #include "UpnpGlobal.h"
#include "UpnpInet.h" #include "UpnpInet.h"
/* /*
* \todo Document the exact reason of these include files and solve this * \todo Document the exact reason of these include files and solve this
* include mess in an include file like UpnpTime.h * include mess in an include file like UpnpTime.h
@ -61,14 +57,12 @@
/* Other systems ??? */ /* Other systems ??? */
#endif #endif
#ifdef WIN32 #ifdef WIN32
/* Do not #include <sys/param.h> */ /* Do not #include <sys/param.h> */
#else #else
#include <sys/param.h> #include <sys/param.h>
#endif #endif
#define LINE_SIZE 180 #define LINE_SIZE 180
#define NAME_SIZE 256 #define NAME_SIZE 256
#define MNFT_NAME_SIZE 64 #define MNFT_NAME_SIZE 64
@ -79,7 +73,6 @@
#define UPNP_USING_CHUNKED -3 #define UPNP_USING_CHUNKED -3
#define UPNP_UNTIL_CLOSE -4 #define UPNP_UNTIL_CLOSE -4
/*! /*!
* \name Error codes * \name Error codes
* *
@ -423,7 +416,6 @@
#include "SubscriptionRequest.h" #include "SubscriptionRequest.h"
#endif /* UPNP_VERSION >= 10800 */ #endif /* UPNP_VERSION >= 10800 */
/*! /*!
* \name Constants and Types * \name Constants and Types
* *

View File

@ -60,7 +60,6 @@
#define fseeko fseek #define fseeko fseek
#else #else
#include <arpa/inet.h> #include <arpa/inet.h>
#include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/time.h> #include <sys/time.h>
@ -84,78 +83,17 @@ const int CHUNK_TAIL_SIZE = 10;
/* in seconds */ /* in seconds */
#define DEFAULT_TCP_CONNECT_TIMEOUT 5 #define DEFAULT_TCP_CONNECT_TIMEOUT 5
/************************************************************************ /*!
* Function : Make_Socket_NoBlocking * \brief Checks socket connection and wait if it is not connected.
* It should be called just after connect.
* *
* Parameters: * \return 0 if successful, else -1.
* IN int sock: socket */
* static int Check_Connect_And_Wait_Connection(
* Description: /*! [in] socket. */
* This function makes socket non-blocking. SOCKET sock,
* /*! [in] result of connect. */
* Returns: int int connect_res)
* 0 if successful else -1
***************************************************************************/
static int Make_Socket_NoBlocking(SOCKET sock)
{
#ifdef WIN32
u_long val = 1;
return ioctlsocket(sock, FIONBIO, &val);
#else
int val;
val = fcntl(sock, F_GETFL, 0);
if (fcntl(sock, F_SETFL, val | O_NONBLOCK) == -1) {
return -1;
}
#endif
return 0;
}
/************************************************************************
* Function : Make_Socket_Blocking
*
* Parameters:
* IN int sock: socket
*
* Description:
* This function makes socket blocking.
*
* Returns: int
* 0 if successful else -1
***************************************************************************/
static int Make_Socket_Blocking(int sock)
{
#ifdef WIN32
u_long val = 0;
return ioctlsocket(sock, FIONBIO, &val);
#else
int val;
val = fcntl(sock, F_GETFL, 0);
if (fcntl(sock, F_SETFL, val & ~O_NONBLOCK) == -1) {
return -1;
}
#endif
return 0;
}
/************************************************************************
* Function : Check_Connect_And_Wait_Connection
*
* Parameters:
* IN int sock: socket
* IN int connect_res: result of connect
*
* Description:
* This function checks socket connection and wait if it is not connected
* It should be called just after connect
*
* Returns: int
* 0 if successful else -1
***************************************************************************/
static int Check_Connect_And_Wait_Connection(int sock, int connect_res)
{ {
struct timeval tmvTimeout = {DEFAULT_TCP_CONNECT_TIMEOUT, 0}; struct timeval tmvTimeout = {DEFAULT_TCP_CONNECT_TIMEOUT, 0};
int result; int result;
@ -210,12 +148,12 @@ static int private_connect(
socklen_t addrlen) socklen_t addrlen)
{ {
#ifndef UPNP_ENABLE_BLOCKING_TCP_CONNECTIONS #ifndef UPNP_ENABLE_BLOCKING_TCP_CONNECTIONS
int ret = Make_Socket_NoBlocking(sockfd); int ret = sock_make_no_blocking(sockfd);
if (ret != - 1) { if (ret != - 1) {
ret = connect(sockfd, serv_addr, addrlen); ret = connect(sockfd, serv_addr, addrlen);
ret = Check_Connect_And_Wait_Connection(sockfd, ret); ret = Check_Connect_And_Wait_Connection(sockfd, ret);
if (ret != - 1) { if (ret != - 1) {
ret = Make_Socket_Blocking(sockfd); ret = sock_make_blocking(sockfd);
} }
} }

View File

@ -44,6 +44,7 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> /* for F_GETFL, F_SETFL, O_NONBLOCK */
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
@ -207,3 +208,35 @@ int sock_write(IN SOCKINFO *info, IN const char *buffer, IN int bufsize,
return sock_read_write(info, (char *)buffer, bufsize, timeoutSecs, FALSE); return sock_read_write(info, (char *)buffer, bufsize, timeoutSecs, FALSE);
} }
int sock_make_blocking(SOCKET sock)
{
#ifdef WIN32
u_long val = 0;
return ioctlsocket(sock, FIONBIO, &val);
#else
int val;
val = fcntl(sock, F_GETFL, 0);
if (fcntl(sock, F_SETFL, val & ~O_NONBLOCK) == -1) {
return -1;
}
#endif
return 0;
}
int sock_make_no_blocking(SOCKET sock)
{
#ifdef WIN32
u_long val = 1;
return ioctlsocket(sock, FIONBIO, &val);
#else /* WIN32 */
int val;
val = fcntl(sock, F_GETFL, 0);
if (fcntl(sock, F_SETFL, val | O_NONBLOCK) == -1) {
return -1;
}
#endif /* WIN32 */
return 0;
}

View File

@ -34,6 +34,10 @@
/*! /*!
* \file * \file
*
* \defgroup Sock Network Socket Library
*
* @{
*/ */
#include "upnputil.h" #include "upnputil.h"
@ -64,6 +68,23 @@ typedef struct
#extern "C" { #extern "C" {
#endif #endif
/*!
* \brief Closes the socket if it is different from -1.
*
* \return -1 if an error occurred or if the socket is -1.
*/
static UPNP_INLINE int sock_close(
/*! Socket descriptor. */
SOCKET sock)
{
int ret = -1;
if (sock != -1)
ret = UpnpCloseSocket(sock);
return ret;
}
/*! /*!
* \brief Assign the passed in socket descriptor to socket descriptor in the * \brief Assign the passed in socket descriptor to socket descriptor in the
* SOCKINFO structure. * SOCKINFO structure.
@ -96,6 +117,23 @@ int sock_init_with_ip(
/*! Remote socket address. */ /*! Remote socket address. */
IN struct sockaddr *foreign_sockaddr); IN struct sockaddr *foreign_sockaddr);
/*!
* \brief Shutsdown the socket using the ShutdownMethod to indicate whether
* sends and receives on the socket will be dis-allowed.
*
* After shutting down the socket, closesocket is called to release system
* resources used by the socket calls.
*
* \return Integer:
* \li \c UPNP_E_SOCKET_ERROR on failure.
* \li \c UPNP_E_SUCCESS on success.
*/
int sock_destroy(
/*! Socket Information Object. */
INOUT SOCKINFO* info,
/*! How to shutdown the socket. Used by sockets's shutdown(). */
int ShutdownMethod);
/*! /*!
* \brief Reads data on socket in sockinfo. * \brief Reads data on socket in sockinfo.
* *
@ -133,42 +171,27 @@ int sock_write(
INOUT int *timeoutSecs); INOUT int *timeoutSecs);
/*! /*!
* \brief Shutsdown the socket using the ShutdownMethod to indicate whether * \brief Make socket blocking.
* sends and receives on the socket will be dis-allowed.
* *
* After shutting down the socket, closesocket is called to release system * \return 0 if successful, -1 otherwise.
* resources used by the socket calls.
*
* \return Integer:
* \li \c UPNP_E_SOCKET_ERROR on failure.
* \li \c UPNP_E_SUCCESS on success.
*/ */
int sock_destroy( int sock_make_blocking(
/*! Socket Information Object. */ /* [in] socket. */
INOUT SOCKINFO* info, SOCKET sock);
/*! How to shutdown the socket. Used by sockets's shutdown(). */
int ShutdownMethod);
/*! /*!
* \brief Closes the socket if it is different from -1. * \brief Make socket non-blocking.
* *
* \return -1 if an error occurred or if the socket is -1. * \return 0 if successful, -1 otherwise.
*/ */
static UPNP_INLINE int sock_close( int sock_make_no_blocking(
/*! Socket descriptor. */ /* [in] socket. */
SOCKET sock) SOCKET sock);
{
int ret = -1;
if (sock != -1) {
ret = UpnpCloseSocket(sock);
}
return ret;
}
#ifdef __cplusplus #ifdef __cplusplus
} /* #extern "C" */ } /* #extern "C" */
#endif #endif
/* @} Sock Network Socket Library */
#endif /* GENLIB_NET_SOCK_H */ #endif /* GENLIB_NET_SOCK_H */

View File

@ -33,6 +33,10 @@
**************************************************************************/ **************************************************************************/
/*! /*!
* \defgroup SSDPlib SSDP Library
*
* @{
*
* \file * \file
*/ */
@ -44,7 +48,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <signal.h> #include <signal.h>
#include <setjmp.h> #include <setjmp.h>
#include <fcntl.h>
#include <errno.h> #include <errno.h>
#ifdef WIN32 #ifdef WIN32
@ -184,48 +187,33 @@ typedef struct
typedef int (*ParserFun)(char *, Event *); typedef int (*ParserFun)(char *, Event *);
/*! /*!
* \brief Make ssdp socket non-blocking. * \name SSDP Server Functions
* *
* \return 0 if successful, -1 otherwise. * @{
*/ */
int Make_Socket_NoBlocking(
/* [in] socket. */
SOCKET sock);
/*! /*!
* \brief Handles the search request. It does the sanity checks of the * \brief Sends SSDP advertisements, replies and shutdown messages.
* request and then schedules a thread to send a random time reply *
* (random within maximum time given by the control point to reply). * \return UPNP_E_SUCCESS if successful else appropriate error.
*/ */
#ifdef INCLUDE_DEVICE_APIS int AdvertiseAndReply(
void ssdp_handle_device_request( /* [in] -1 = Send shutdown, 0 = send reply, 1 = Send Advertisement. */
/* [in] . */ int AdFlag,
http_message_t *hmsg, /* [in] Device handle. */
/* [in] . */ UpnpDevice_Handle Hnd,
struct sockaddr *dest_addr); /* [in] Search type for sending replies. */
#else enum SsdpSearchType SearchType,
static inline void ssdp_handle_device_request( /* [in] Destination address. */
/* [in] . */ struct sockaddr *DestAddr,
http_message_t *hmsg, /* [in] Device type. */
/* [in] . */ char *DeviceType,
struct sockaddr* dest_addr) {} /* [in] Device UDN. */
#endif char *DeviceUDN,
/* [in] Service type. */
/*! char *ServiceType,
* \brief This function handles the ssdp messages from the devices. These /* [in] Advertisement age. */
* messages includes the search replies, advertisement of device coming alive int Exp);
* and bye byes.
*/
void ssdp_handle_ctrlpt_msg(
/* [in] SSDP message from the device. */
http_message_t *hmsg,
/* [in] Address of the device. */
struct sockaddr *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);
/*! /*!
* \brief Fills the fields of the event structure like DeviceType, Device UDN * \brief Fills the fields of the event structure like DeviceType, Device UDN
@ -240,24 +228,6 @@ int unique_service_name(
* function. */ * function. */
SsdpEvent *Evt); SsdpEvent *Evt);
/*!
* \brief Creates the ssdp sockets. It set their option to listen for
* 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);
/*! /*!
* \brief This function figures out the type of the SSDP search in the in the * \brief This function figures out the type of the SSDP search in the in the
* request. * request.
@ -269,7 +239,6 @@ enum SsdpSearchType ssdp_request_type1(
/* [in] command came in the ssdp request. */ /* [in] command came in the ssdp request. */
char *cmd); char *cmd);
/*! /*!
* \brief Starts filling the SSDP event structure based upon the * \brief Starts filling the SSDP event structure based upon the
* request received. * request received.
@ -282,9 +251,64 @@ int ssdp_request_type(
/* [out] The event structure partially filled by this function. */ /* [out] The event structure partially filled by this function. */
SsdpEvent *Evt); SsdpEvent *Evt);
/*!
* \brief This function reads the data from the ssdp socket.
*/
void readFromSSDPSocket(
/* [in] SSDP socket. */
SOCKET socket);
/*!
* \brief Creates the IPv4 and IPv6 ssdp sockets required by the
* control point and device operation.
*
* \return UPNP_E_SUCCESS if successful else returns appropriate error.
*/
int get_ssdp_sockets(
/* [out] Array of SSDP sockets. */
MiniServerSockArray *out);
/* @} SSDP Server Functions */
/*!
* \name SSDP Control Point Functions
*
* @{
*/
/*!
* \brief This function handles the ssdp messages from the devices. These
* messages includes the search replies, advertisement of device coming alive
* and bye byes.
*/
void ssdp_handle_ctrlpt_msg(
/* [in] SSDP message from the device. */
http_message_t *hmsg,
/* [in] Address of the device. */
struct sockaddr *dest_addr,
/* [in] timeout kept by the control point while sending search message.
* Only in search reply. */
int timeout,
/* [in] Cookie stored by the control point application. This cookie will
* be returned to the control point in the callback.
* Only in search reply. */
void *cookie);
/*! /*!
* \brief Creates and send the search request for a specific URL. * \brief Creates and send the search request for a specific URL.
* *
* This function implements the search request of the discovery phase.
* A M-SEARCH request is sent on the SSDP channel for both IPv4 and
* IPv6 addresses. The search target(ST) is required and must be one of
* the following:
* \li "ssdp:all" : Search for all devices and services.
* \li "ssdp:rootdevice" : Search for root devices only.
* \li "uuid:<device-uuid>" : Search for a particular device.
* \li "urn:schemas-upnp-org:device:<deviceType:v>"
* \li "urn:schemas-upnp-org:service:<serviceType:v>"
* \li "urn:<domain-name>:device:<deviceType:v>"
* \li "urn:<domain-name>:service:<serviceType:v>"
*
* \return 1 if successful else appropriate error. * \return 1 if successful else appropriate error.
*/ */
int SearchByTarget( int SearchByTarget(
@ -296,6 +320,43 @@ int SearchByTarget(
* be returned to application in the callback. */ * be returned to application in the callback. */
void *Cookie); void *Cookie);
/* @} SSDP Control Point Functions */
/*!
* \name SSDP Device Functions
*
* @{
*/
/*!
* \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);
/*!
* \brief Handles the search request. It does 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).
*/
#ifdef INCLUDE_DEVICE_APIS
void ssdp_handle_device_request(
/* [in] . */
http_message_t *hmsg,
/* [in] . */
struct sockaddr *dest_addr);
#else /* INCLUDE_DEVICE_APIS */
static inline void ssdp_handle_device_request(
/* [in] . */
http_message_t *hmsg,
/* [in] . */
struct sockaddr* dest_addr) {}
#endif /* INCLUDE_DEVICE_APIS */
/*! /*!
* \brief Creates the device advertisement request based on the input * \brief Creates the device advertisement request based on the input
* parameter, and send it to the multicast channel. * parameter, and send it to the multicast channel.
@ -316,48 +377,6 @@ int DeviceAdvertisement(
/* [in] Device address family. */ /* [in] Device address family. */
int AddressFamily); int AddressFamily);
/*!
* \brief Creates a HTTP device shutdown request packet and send it to the
* multicast channel through RequestHandler.
*
* \return UPNP_E_SUCCESS if successful else appropriate error.
*/
int DeviceShutdown(
/* [in] Device Type. */
char *DevType,
/* [in] 1 means root device. */
int RootDev,
/* [in] Device UDN. */
char *Udn,
/* [in] . */
char *_Server,
/* [in] Location URL. */
char *Location,
/* [in] Device duration in sec. */
int Duration,
/* [in] Device address family. */
int AddressFamily);
/*!
* \brief Creates the reply packet based on the input parameter, and send it
* to the client address given in its input parameter DestAddr.
*
* \return UPNP_E_SUCCESS if successful else appropriate error.
*/
int DeviceReply(
/* [in] destination IP address. */
struct sockaddr *DestAddr,
/* [in] Device type. */
char *DevType,
/* [in] 1 means root device 0 means embedded device. */
int RootDev,
/* [in] Device UDN. */
char *Udn,
/* [in] Location of Device description document. */
char *Location,
/* [in] Life time of this device. */
int Duration);
/*! /*!
* \brief Creates the reply packet based on the input parameter, and send it * \brief Creates the reply packet based on the input parameter, and send it
* to the client addesss given in its input parameter DestAddr. * to the client addesss given in its input parameter DestAddr.
@ -380,6 +399,26 @@ int SendReply(
/* [in] . */ /* [in] . */
int ByType ); int ByType );
/*!
* \brief Creates the reply packet based on the input parameter, and send it
* to the client address given in its input parameter DestAddr.
*
* \return UPNP_E_SUCCESS if successful else appropriate error.
*/
int DeviceReply(
/* [in] destination IP address. */
struct sockaddr *DestAddr,
/* [in] Device type. */
char *DevType,
/* [in] 1 means root device 0 means embedded device. */
int RootDev,
/* [in] Device UDN. */
char *Udn,
/* [in] Location of Device description document. */
char *Location,
/* [in] Life time of this device. */
int Duration);
/*! /*!
* \brief Creates the advertisement packet based on the input parameter, * \brief Creates the advertisement packet based on the input parameter,
* and send it to the multicast channel. * and send it to the multicast channel.
@ -435,36 +474,29 @@ int ServiceShutdown(
int AddressFamily); int AddressFamily);
/*! /*!
* \brief Wrapper function to reply the search request coming from the * \brief Creates a HTTP device shutdown request packet and send it to the
* control point. * multicast channel through RequestHandler.
*
* \return always return NULL
*/
void *advertiseAndReplyThread(
/* [in] Structure containing the search request. */
void *data);
/*!
* \brief Sends SSDP advertisements, replies and shutdown messages.
* *
* \return UPNP_E_SUCCESS if successful else appropriate error. * \return UPNP_E_SUCCESS if successful else appropriate error.
*/ */
int AdvertiseAndReply( int DeviceShutdown(
/* [in] -1 = Send shutdown, 0 = send reply, 1 = Send Advertisement. */ /* [in] Device Type. */
int AdFlag, char *DevType,
/* [in] Device handle. */ /* [in] 1 means root device. */
UpnpDevice_Handle Hnd, int RootDev,
/* [in] Search type for sending replies. */
enum SsdpSearchType SearchType,
/* [in] Destination address. */
struct sockaddr *DestAddr,
/* [in] Device type. */
char *DeviceType,
/* [in] Device UDN. */ /* [in] Device UDN. */
char *DeviceUDN, char *Udn,
/* [in] Service type. */ /* [in] . */
char *ServiceType, char *_Server,
/* [in] Advertisement age. */ /* [in] Location URL. */
int Exp); char *Location,
/* [in] Device duration in sec. */
int Duration,
/* [in] Device address family. */
int AddressFamily);
/* @} SSDP Device Functions */
/* @} SSDPlib SSDP Library */
#endif /* SSDPLIB_H */ #endif /* SSDPLIB_H */

View File

@ -29,6 +29,14 @@
* *
**************************************************************************/ **************************************************************************/
/*!
* \addtogroup SSDPlib
*
* @{
*
* \file
*/
#include "config.h" #include "config.h"
#include "upnputil.h" #include "upnputil.h"
@ -52,58 +60,23 @@
#include <string.h> #include <string.h>
#endif /* WIN32 */ #endif /* WIN32 */
/************************************************************************ /*!
* Function: send_search_result * \brief Sends a callback to the control point application with a SEARCH
* * result.
* Parameters: */
* IN void *data: Search reply from the device static void send_search_result(
* /* [in] Search reply from the device. */
* Description: IN void *data)
* This function sends a callback to the control point application with
* a SEARCH result
*
* Returns: void
*
***************************************************************************/
void send_search_result(IN void *data)
{ {
ResultData *temp = (ResultData *) data; ResultData *temp = (ResultData *) data;
temp->ctrlpt_callback(UPNP_DISCOVERY_SEARCH_RESULT, &temp->param, temp->cookie); temp->ctrlpt_callback(UPNP_DISCOVERY_SEARCH_RESULT, &temp->param,
temp->cookie);
free(temp); free(temp);
} }
/************************************************************************ void ssdp_handle_ctrlpt_msg(http_message_t *hmsg, struct sockaddr *dest_addr,
* Function: ssdp_handle_ctrlpt_msg int timeout, void *cookie)
*
* Parameters:
* 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(
IN http_message_t *hmsg,
IN struct sockaddr *dest_addr,
/* only in search reply */
IN int timeout,
/* only in search reply */
IN void *cookie)
{ {
int handle; int handle;
struct Handle_Info *ctrlpt_info = NULL; struct Handle_Info *ctrlpt_info = NULL;
@ -277,17 +250,19 @@ void ssdp_handle_ctrlpt_msg(
break; break;
case SSDP_DEVICETYPE:{ case SSDP_DEVICETYPE:{
size_t m = min(hdr_value.length, size_t m = min(hdr_value.length,
strlen(searchArg-> strlen
searchTarget)); (searchArg->searchTarget));
matched = !strncmp(searchArg->searchTarget, matched =
!strncmp(searchArg->searchTarget,
hdr_value.buf, m); hdr_value.buf, m);
break; break;
} }
case SSDP_SERVICE:{ case SSDP_SERVICE:{
size_t m = min(hdr_value.length, size_t m = min(hdr_value.length,
strlen(searchArg-> strlen
searchTarget)); (searchArg->searchTarget));
matched = !strncmp(searchArg->searchTarget, matched =
!strncmp(searchArg->searchTarget,
hdr_value.buf, m); hdr_value.buf, m);
break; break;
} }
@ -304,8 +279,7 @@ void ssdp_handle_ctrlpt_msg(
threadData->cookie = searchArg->cookie; threadData->cookie = searchArg->cookie;
threadData->ctrlpt_callback = threadData->ctrlpt_callback =
ctrlpt_callback; ctrlpt_callback;
TPJobInit(&job, TPJobInit(&job, (start_routine)
(start_routine)
send_search_result, send_search_result,
threadData); threadData);
TPJobSetPriority(&job, MED_PRIORITY); TPJobSetPriority(&job, MED_PRIORITY);
@ -324,28 +298,18 @@ void ssdp_handle_ctrlpt_msg(
} }
} }
/*!
/************************************************************************ * \brief Creates a HTTP search request packet depending on the input
* Function : CreateClientRequestPacket * parameter.
* */
* Parameters: static void CreateClientRequestPacket(
* IN char * RqstBuf:Output string in HTTP format. /*! [in] Output string in HTTP format. */
* IN char *SearchTarget:Search Target IN char *RqstBuf,
* IN int Mx dest_addr: Number of seconds to wait to /*! [in] Search Target. */
* collect all the responses
* IN int AddressFamily: search address family
*
* Description:
* This function creates a HTTP search request packet
* depending on the input parameter.
*
* Returns: void
*
***************************************************************************/
static void
CreateClientRequestPacket( IN char *RqstBuf,
IN int Mx, IN int Mx,
/*! [in] Number of seconds to wait to collect all the responses. */
IN char *SearchTarget, IN char *SearchTarget,
/*! [in] search address family. */
IN int AddressFamily) IN int AddressFamily)
{ {
char TempBuf[COMMAND_LEN]; char TempBuf[COMMAND_LEN];
@ -355,7 +319,8 @@ CreateClientRequestPacket( IN char *RqstBuf,
if (AddressFamily == AF_INET) { if (AddressFamily == AF_INET) {
sprintf(TempBuf, "HOST: %s:%d\r\n", SSDP_IP, SSDP_PORT); sprintf(TempBuf, "HOST: %s:%d\r\n", SSDP_IP, SSDP_PORT);
} else if (AddressFamily == AF_INET6) { } else if (AddressFamily == AF_INET6) {
sprintf( TempBuf, "HOST: [%s]:%d\r\n", SSDP_IPV6_LINKLOCAL, SSDP_PORT ); sprintf(TempBuf, "HOST: [%s]:%d\r\n", SSDP_IPV6_LINKLOCAL,
SSDP_PORT);
} }
strcat(RqstBuf, TempBuf); strcat(RqstBuf, TempBuf);
strcat(RqstBuf, "MAN: \"ssdp:discover\"\r\n"); strcat(RqstBuf, "MAN: \"ssdp:discover\"\r\n");
@ -372,12 +337,18 @@ CreateClientRequestPacket( IN char *RqstBuf,
strcat(RqstBuf, "\r\n"); strcat(RqstBuf, "\r\n");
} }
/*!
* \brief
*/
static void CreateClientRequestPacketUlaGua( static void CreateClientRequestPacketUlaGua(
IN char *RqstBuf, /*! [in] . */
IN int Mx, char *RqstBuf,
IN char *SearchTarget, /*! [in] . */
IN int AddressFamily) int Mx,
/*! [in] . */
char *SearchTarget,
/*! [in] . */
int AddressFamily)
{ {
char TempBuf[COMMAND_LEN]; char TempBuf[COMMAND_LEN];
@ -385,7 +356,8 @@ static void CreateClientRequestPacketUlaGua(
if (AddressFamily == AF_INET) { if (AddressFamily == AF_INET) {
sprintf(TempBuf, "HOST: %s:%d\r\n", SSDP_IP, SSDP_PORT); sprintf(TempBuf, "HOST: %s:%d\r\n", SSDP_IP, SSDP_PORT);
} else if (AddressFamily == AF_INET6) { } else if (AddressFamily == AF_INET6) {
sprintf(TempBuf, "HOST: [%s]:%d\r\n", SSDP_IPV6_SITELOCAL, SSDP_PORT); sprintf(TempBuf, "HOST: [%s]:%d\r\n", SSDP_IPV6_SITELOCAL,
SSDP_PORT);
} }
strcat(RqstBuf, TempBuf); strcat(RqstBuf, TempBuf);
strcat(RqstBuf, "MAN: \"ssdp:discover\"\r\n"); strcat(RqstBuf, "MAN: \"ssdp:discover\"\r\n");
@ -400,20 +372,12 @@ static void CreateClientRequestPacketUlaGua(
strcat(RqstBuf, "\r\n"); strcat(RqstBuf, "\r\n");
} }
/*!
/************************************************************************ * \brief
* Function : searchExpired */
* static void searchExpired(
* Parameters: /* [in] . */
* IN void * arg: void *arg)
*
* Description:
* This function
*
* Returns: void
*
***************************************************************************/
void searchExpired(void *arg)
{ {
int *id = (int *)arg; int *id = (int *)arg;
@ -459,35 +423,7 @@ void searchExpired(void *arg)
free(id); free(id);
} }
/************************************************************************ int SearchByTarget(int Mx, char *St, void *Cookie)
* Function : SearchByTarget
*
* Parameters:
* IN int Mx:Number of seconds to wait, to collect all the responses.
* IN char *St: Search target.
* IN void *Cookie: cookie provided by control point application.
* This cokie will be returned to application in the callback.
*
* Description:
* This function implements the search request of the discovery phase.
* A M-SEARCH request is sent on the SSDP channel for both IPv4 and
* IPv6 addresses. The search target(ST) is required and must be one of
* the following:
* - "ssdp:all" : Search for all devices and services.
* - "ssdp:rootdevice" : Search for root devices only.
* - "uuid:<device-uuid>" : Search for a particular device.
* - "urn:schemas-upnp-org:device:<deviceType:v>"
* - "urn:schemas-upnp-org:service:<serviceType:v>"
* - "urn:<domain-name>:device:<deviceType:v>"
* - "urn:<domain-name>:service:<serviceType:v>"
*
* Returns: int
* 1 if successful else appropriate error
***************************************************************************/
int SearchByTarget(
IN int Mx,
IN char *St,
IN void *Cookie)
{ {
char errorBuffer[ERROR_BUFFER_LEN]; char errorBuffer[ERROR_BUFFER_LEN];
int *id = NULL; int *id = NULL;
@ -516,7 +452,8 @@ int SearchByTarget(
return UPNP_E_INVALID_PARAM; return UPNP_E_INVALID_PARAM;
} }
UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__, "Inside SearchByTarget\n"); UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__,
"Inside SearchByTarget\n");
timeTillRead = Mx; timeTillRead = Mx;
if (timeTillRead < MIN_SEARCH_TIME) { if (timeTillRead < MIN_SEARCH_TIME) {
@ -527,7 +464,8 @@ int SearchByTarget(
CreateClientRequestPacket(ReqBufv4, timeTillRead, St, AF_INET); CreateClientRequestPacket(ReqBufv4, timeTillRead, St, AF_INET);
CreateClientRequestPacket(ReqBufv6, timeTillRead, St, AF_INET6); CreateClientRequestPacket(ReqBufv6, timeTillRead, St, AF_INET6);
CreateClientRequestPacketUlaGua(ReqBufv6UlaGua, timeTillRead, St, AF_INET6); CreateClientRequestPacketUlaGua(ReqBufv6UlaGua, timeTillRead, St,
AF_INET6);
memset(&__ss_v4, 0, sizeof(__ss_v4)); memset(&__ss_v4, 0, sizeof(__ss_v4));
destAddr4->sin_family = AF_INET; destAddr4->sin_family = AF_INET;
@ -587,8 +525,7 @@ int SearchByTarget(
if (ret == -1) { if (ret == -1) {
strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__, UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__,
"SSDP_LIB: Error in select(): %s\n", "SSDP_LIB: Error in select(): %s\n", errorBuffer);
errorBuffer);
shutdown(gSsdpReqSocket4, SD_BOTH); shutdown(gSsdpReqSocket4, SD_BOTH);
UpnpCloseSocket(gSsdpReqSocket4); UpnpCloseSocket(gSsdpReqSocket4);
#ifdef UPNP_ENABLE_IPV6 #ifdef UPNP_ENABLE_IPV6
@ -644,7 +581,7 @@ int SearchByTarget(
return 1; return 1;
} }
#endif /* EXCLUDE_SSDP */ #endif /* EXCLUDE_SSDP */
#endif /* INCLUDE_CLIENT_APIS */ #endif /* INCLUDE_CLIENT_APIS */
/* @} SSDPlib */

View File

@ -29,14 +29,19 @@
* *
**************************************************************************/ **************************************************************************/
/*!
* \addtogroup SSDPlib
*
* @{
*
* \file
*/
#include "config.h" #include "config.h"
#ifdef INCLUDE_DEVICE_APIS #ifdef INCLUDE_DEVICE_APIS
#if EXCLUDE_SSDP == 0 #if EXCLUDE_SSDP == 0
#include "httpparser.h" #include "httpparser.h"
#include "httpreadwrite.h" #include "httpreadwrite.h"
#include "ssdplib.h" #include "ssdplib.h"
@ -46,32 +51,15 @@
#include "upnpapi.h" #include "upnpapi.h"
#include "UpnpInet.h" #include "UpnpInet.h"
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define MSGTYPE_SHUTDOWN 0 #define MSGTYPE_SHUTDOWN 0
#define MSGTYPE_ADVERTISEMENT 1 #define MSGTYPE_ADVERTISEMENT 1
#define MSGTYPE_REPLY 2 #define MSGTYPE_REPLY 2
void *advertiseAndReplyThread(void *data)
/************************************************************************
* Function : advertiseAndReplyThread
*
* Parameters:
* 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 )
{ {
SsdpSearchReply *arg = (SsdpSearchReply *) data; SsdpSearchReply *arg = (SsdpSearchReply *) data;
@ -79,35 +67,16 @@ advertiseAndReplyThread( IN void *data )
arg->event.RequestType, arg->event.RequestType,
(struct sockaddr *)&arg->dest_addr, (struct sockaddr *)&arg->dest_addr,
arg->event.DeviceType, arg->event.DeviceType,
arg->event.UDN, arg->event.UDN, arg->event.ServiceType, arg->MaxAge);
arg->event.ServiceType, arg->MaxAge );
free(arg); free(arg);
return NULL; return NULL;
} }
/************************************************************************
* Function : ssdp_handle_device_request
*
* Parameters:
* IN http_message_t *hmsg: SSDP search request from the control point
* IN struct sockaddr *dest_addr: The address info of control point
*
* 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(http_message_t *hmsg, struct sockaddr *dest_addr)
IN http_message_t *hmsg,
IN struct sockaddr *dest_addr)
{ {
#define MX_FUDGE_FACTOR 10 #define MX_FUDGE_FACTOR 10
int handle; int handle;
struct Handle_Info *dev_info = NULL; struct Handle_Info *dev_info = NULL;
memptr hdr_value; memptr hdr_value;
@ -122,28 +91,24 @@ void ssdp_handle_device_request(
/* check man hdr. */ /* check man hdr. */
if (httpmsg_find_hdr(hmsg, HDR_MAN, &hdr_value) == NULL || if (httpmsg_find_hdr(hmsg, HDR_MAN, &hdr_value) == NULL ||
memptr_cmp( &hdr_value, "\"ssdp:discover\"" ) != 0 ) { memptr_cmp(&hdr_value, "\"ssdp:discover\"") != 0)
/* bad or missing hdr. */ /* bad or missing hdr. */
return; return;
}
/* MX header. */ /* MX header. */
if (httpmsg_find_hdr(hmsg, HDR_MX, &hdr_value) == NULL || if (httpmsg_find_hdr(hmsg, HDR_MX, &hdr_value) == NULL ||
( mx = raw_to_int( &hdr_value, 10 ) ) < 0 ) { (mx = raw_to_int(&hdr_value, 10)) < 0)
return; return;
}
/* ST header. */ /* ST header. */
if( httpmsg_find_hdr( hmsg, HDR_ST, &hdr_value ) == NULL ) { if (httpmsg_find_hdr(hmsg, HDR_ST, &hdr_value) == NULL)
return; return;
}
save_char = hdr_value.buf[hdr_value.length]; save_char = hdr_value.buf[hdr_value.length];
hdr_value.buf[hdr_value.length] = '\0'; hdr_value.buf[hdr_value.length] = '\0';
ret_code = ssdp_request_type(hdr_value.buf, &event); ret_code = ssdp_request_type(hdr_value.buf, &event);
/* restore. */ /* restore. */
hdr_value.buf[hdr_value.length] = save_char; hdr_value.buf[hdr_value.length] = save_char;
if( ret_code == -1 ) { if (ret_code == -1)
/* bad ST header. */ /* bad ST header. */
return; return;
}
HandleLock(); HandleLock();
/* device info. */ /* device info. */
@ -169,13 +134,9 @@ void ssdp_handle_device_request(
"DeviceUuid = %s\n", event.UDN); "DeviceUuid = %s\n", event.UDN);
UpnpPrintf(UPNP_PACKET, API, __FILE__, __LINE__, UpnpPrintf(UPNP_PACKET, API, __FILE__, __LINE__,
"ServiceType = %s\n", event.ServiceType); "ServiceType = %s\n", event.ServiceType);
threadArg = (SsdpSearchReply *)malloc(sizeof(SsdpSearchReply));
threadArg = if (threadArg == NULL)
( SsdpSearchReply * ) malloc( sizeof( SsdpSearchReply ) );
if( threadArg == NULL ) {
return; return;
}
threadArg->handle = handle; threadArg->handle = handle;
memcpy(&threadArg->dest_addr, dest_addr, sizeof(threadArg->dest_addr)); memcpy(&threadArg->dest_addr, dest_addr, sizeof(threadArg->dest_addr));
threadArg->event = event; threadArg->event = event;
@ -187,38 +148,29 @@ void ssdp_handle_device_request(
/* Subtract a percentage from the mx to allow for network and processing /* Subtract a percentage from the mx to allow for network and processing
* delays (i.e. if search is for 30 seconds, respond * delays (i.e. if search is for 30 seconds, respond
* within 0 - 27 seconds). */ * within 0 - 27 seconds). */
if( mx >= 2 ) { if (mx >= 2)
mx -= MAXVAL(1, mx / MX_FUDGE_FACTOR); mx -= MAXVAL(1, mx / MX_FUDGE_FACTOR);
} if (mx < 1)
if( mx < 1 ) {
mx = 1; mx = 1;
}
replyTime = rand() % mx; replyTime = rand() % mx;
TimerThreadSchedule(&gTimerThread, replyTime, REL_SEC, &job, TimerThreadSchedule(&gTimerThread, replyTime, REL_SEC, &job,
SHORT_TERM, NULL); SHORT_TERM, NULL);
} }
#endif #endif
/************************************************************************ /*!
* Function : NewRequestHandler * \brief Works as a request handler which passes the HTTP request string
* to multicast channel.
* *
* Parameters: * \return 1 if successful else appropriate error.
* IN struct sockaddr *DestAddr: Ip address, to send the reply. */
* IN int NumPacket: Number of packet to be sent. static int NewRequestHandler(
* IN char **RqPacket:Number of packet to be sent. /*! [in] Ip address, to send the reply. */
* struct sockaddr *DestAddr,
* Description: /*! [in] Number of packet to be sent. */
* This function works as a request handler which passes the HTTP int NumPacket,
* request string to multicast channel then /*! [in] . */
* char **RqPacket)
* Returns: void *
* 1 if successful else appropriate error
***************************************************************************/
static int NewRequestHandler(IN struct sockaddr *DestAddr, IN int NumPacket,
IN char **RqPacket)
{ {
char errorBuffer[ERROR_BUFFER_LEN]; char errorBuffer[ERROR_BUFFER_LEN];
SOCKET ReplySock; SOCKET ReplySock;
@ -288,10 +240,12 @@ static int NewRequestHandler(IN struct sockaddr *DestAddr, IN int NumPacket,
return ret; return ret;
} }
/** /*!
* return 1 if an inet6 @ has been found * \brief
*
* \return 1 if an inet6 @ has been found.
*/ */
int extractIPv6address(char *url, char *address) static int extractIPv6address(char *url, char *address)
{ {
int i = 0; int i = 0;
int j = 0; int j = 0;
@ -324,12 +278,12 @@ exit_function:
return ret; return ret;
} }
/*!
/** * \brief
* return 1 if the Url contains an ULA or GUA IPv6 address *
* 0 otherwise * \return 1 if the Url contains an ULA or GUA IPv6 address, 0 otherwise.
*/ */
int isUrlV6UlaGua(char *descdocUrl) static int isUrlV6UlaGua(char *descdocUrl)
{ {
char address[INET6_ADDRSTRLEN]; char address[INET6_ADDRSTRLEN];
struct in6_addr v6_addr; struct in6_addr v6_addr;
@ -342,36 +296,27 @@ int isUrlV6UlaGua(char *descdocUrl)
return 0; return 0;
} }
/*!
/************************************************************************ * \brief Creates a HTTP request packet. Depending on the input parameter,
* Function : CreateServiceRequestPacket * it either creates a service advertisement request or service shutdown
* * request etc.
* Parameters: */
* IN int msg_type : type of the message ( Search Reply, Advertisement
* or Shutdown )
* IN char * nt : ssdp type
* IN char * usn : unique service name ( go in the HTTP Header)
* IN char * location :Location URL.
* IN int duration :Service duration in sec.
* OUT char** packet :Output buffer filled with HTTP statement.
* IN int AddressFamily: Address family of the HTTP request.
*
* Description:
* This function creates a HTTP request packet. Depending
* on the input parameter it either creates a service advertisement
* request or service shutdown request etc.
*
* Returns: void
*
***************************************************************************/
static void CreateServicePacket( static void CreateServicePacket(
IN int msg_type, /*! [in] type of the message (Search Reply, Advertisement
const IN char *nt, * or Shutdown). */
IN char *usn, int msg_type,
IN char *location, /*! [in] ssdp type. */
IN int duration, const char *nt,
OUT char **packet, /*! [in] unique service name ( go in the HTTP Header). */
IN int AddressFamily) char *usn,
/*! [in] Location URL. */
char *location,
/*! [in] Service duration in sec. */
int duration,
/*! [out] Output buffer filled with HTTP statement. */
char **packet,
/*! [in] Address family of the HTTP request. */
int AddressFamily)
{ {
int ret_code; int ret_code;
const char *nts; const char *nts;
@ -383,20 +328,17 @@ static void CreateServicePacket(
membuffer_init(&buf); membuffer_init(&buf);
buf.size_inc = 30; buf.size_inc = 30;
*packet = NULL; *packet = NULL;
if (msg_type == MSGTYPE_REPLY) { if (msg_type == MSGTYPE_REPLY) {
ret_code = http_MakeMessage( ret_code = http_MakeMessage(&buf, 1, 1,
&buf, 1, 1, "R" "sdc" "D" "sc" "ssc" "ssc" "ssc"
"R" "sdc" "D" "sc" "ssc" "ssc" "ssc" "S" "Xc" "ssc" "sscc", "S" "Xc" "ssc" "sscc", HTTP_OK,
HTTP_OK,
"CACHE-CONTROL: max-age=", duration, "CACHE-CONTROL: max-age=", duration,
"EXT:", "EXT:", "LOCATION: ", location,
"LOCATION: ", location, "OPT: ",
"OPT: ", "\"http://schemas.upnp.org/upnp/1/0/\"; ns=01", "\"http://schemas.upnp.org/upnp/1/0/\"; ns=01",
"01-NLS: ", gUpnpSdkNLSuuid, "01-NLS: ", gUpnpSdkNLSuuid,
X_USER_AGENT, X_USER_AGENT, "ST: ", nt, "USN: ",
"ST: ", nt, usn);
"USN: ", usn);
if (ret_code != 0) { if (ret_code != 0) {
return; return;
} }
@ -419,19 +361,16 @@ static void CreateServicePacket(
else else
host = "[" SSDP_IPV6_LINKLOCAL "]"; host = "[" SSDP_IPV6_LINKLOCAL "]";
} }
ret_code = http_MakeMessage( ret_code = http_MakeMessage(&buf, 1, 1,
&buf, 1, 1, "Q" "sssdc" "sdc" "ssc" "ssc" "ssc"
"Q" "sssdc" "sdc" "ssc" "ssc" "ssc" "ssc" "ssc" "S" "Xc" "sscc", "ssc" "ssc" "S" "Xc" "sscc",
HTTPMETHOD_NOTIFY, "*", (size_t) 1, HTTPMETHOD_NOTIFY, "*", (size_t) 1,
"HOST: ", host, "HOST: ", host, ":", SSDP_PORT,
":", SSDP_PORT,
"CACHE-CONTROL: max-age=", duration, "CACHE-CONTROL: max-age=", duration,
"LOCATION: ", location, "LOCATION: ", location, "OPT: ",
"OPT: ", "\"http://schemas.upnp.org/upnp/1/0/\"; ns=01", "\"http://schemas.upnp.org/upnp/1/0/\"; ns=01",
"01-NLS: ", gUpnpSdkNLSuuid, "01-NLS: ", gUpnpSdkNLSuuid, "NT: ",
"NT: ", nt, nt, "NTS: ", nts, X_USER_AGENT,
"NTS: ", nts,
X_USER_AGENT,
"USN: ", usn); "USN: ", usn);
if (ret_code) if (ret_code)
return; return;
@ -445,37 +384,12 @@ static void CreateServicePacket(
return; return;
} }
int DeviceAdvertisement(char *DevType, int RootDev, char *Udn, char *Location,
/************************************************************************ int Duration, int AddressFamily)
* Function : DeviceAdvertisement
*
* Parameters:
* IN char * DevType : type of the device
* IN int RootDev: flag to indicate if the device is root device
* IN char * nt : ssdp type
* IN char * usn : unique service name
* IN char * location :Location URL.
* IN int duration :Service duration in sec.
*
* 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( IN char *DevType,
int RootDev,
char *Udn,
IN char *Location,
IN int Duration,
IN int AddressFamily)
{ {
struct sockaddr_storage __ss; struct sockaddr_storage __ss;
struct sockaddr_in *DestAddr4 = (struct sockaddr_in *)&__ss; struct sockaddr_in *DestAddr4 = (struct sockaddr_in *)&__ss;
struct sockaddr_in6 *DestAddr6 = (struct sockaddr_in6 *)&__ss; struct sockaddr_in6 *DestAddr6 = (struct sockaddr_in6 *)&__ss;
/* char Mil_Nt[LINE_SIZE] */ /* char Mil_Nt[LINE_SIZE] */
char Mil_Usn[LINE_SIZE]; char Mil_Usn[LINE_SIZE];
char *msgs[3]; char *msgs[3];
@ -483,7 +397,6 @@ DeviceAdvertisement( IN char *DevType,
UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__, UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__,
"In function DeviceAdvertisement\n"); "In function DeviceAdvertisement\n");
memset(&__ss, 0, sizeof(__ss)); memset(&__ss, 0, sizeof(__ss));
if (AddressFamily == AF_INET) { if (AddressFamily == AF_INET) {
DestAddr4->sin_family = AF_INET; DestAddr4->sin_family = AF_INET;
@ -492,25 +405,24 @@ DeviceAdvertisement( IN char *DevType,
} else if (AddressFamily == AF_INET6) { } else if (AddressFamily == AF_INET6) {
DestAddr6->sin6_family = AF_INET6; DestAddr6->sin6_family = AF_INET6;
inet_pton(AF_INET6, inet_pton(AF_INET6,
(isUrlV6UlaGua(Location)) ? SSDP_IPV6_SITELOCAL : SSDP_IPV6_LINKLOCAL, (isUrlV6UlaGua(Location)) ? SSDP_IPV6_SITELOCAL :
&DestAddr6->sin6_addr ); SSDP_IPV6_LINKLOCAL, &DestAddr6->sin6_addr);
DestAddr6->sin6_port = htons(SSDP_PORT); DestAddr6->sin6_port = htons(SSDP_PORT);
DestAddr6->sin6_scope_id = gIF_INDEX; DestAddr6->sin6_scope_id = gIF_INDEX;
} else { } else {
UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__,
"Invalid device address family.\n"); "Invalid device address family.\n");
} }
msgs[0] = NULL; msgs[0] = NULL;
msgs[1] = NULL; msgs[1] = NULL;
msgs[2] = NULL; msgs[2] = NULL;
/* If deviceis a root device , here we need to send 3 advertisement /* If deviceis a root device , here we need to send 3 advertisement
* or reply */ * or reply */
if (RootDev) { if (RootDev) {
sprintf(Mil_Usn, "%s::upnp:rootdevice", Udn); sprintf(Mil_Usn, "%s::upnp:rootdevice", Udn);
CreateServicePacket(MSGTYPE_ADVERTISEMENT, "upnp:rootdevice", CreateServicePacket(MSGTYPE_ADVERTISEMENT, "upnp:rootdevice",
Mil_Usn, Location, Duration, &msgs[0], AddressFamily ); Mil_Usn, Location, Duration, &msgs[0],
AddressFamily);
} }
/* both root and sub-devices need to send these two messages */ /* both root and sub-devices need to send these two messages */
CreateServicePacket(MSGTYPE_ADVERTISEMENT, Udn, Udn, CreateServicePacket(MSGTYPE_ADVERTISEMENT, Udn, Udn,
@ -519,8 +431,7 @@ DeviceAdvertisement( IN char *DevType,
CreateServicePacket(MSGTYPE_ADVERTISEMENT, DevType, Mil_Usn, CreateServicePacket(MSGTYPE_ADVERTISEMENT, DevType, Mil_Usn,
Location, Duration, &msgs[2], AddressFamily); Location, Duration, &msgs[2], AddressFamily);
/* check error */ /* check error */
if( ( RootDev && msgs[0] == NULL ) || if ((RootDev && msgs[0] == NULL) || msgs[1] == NULL || msgs[2] == NULL) {
msgs[1] == NULL || msgs[2] == NULL ) {
free(msgs[0]); free(msgs[0]);
free(msgs[1]); free(msgs[1]);
free(msgs[2]); free(msgs[2]);
@ -529,13 +440,14 @@ DeviceAdvertisement( IN char *DevType,
/* send packets */ /* send packets */
if (RootDev) { if (RootDev) {
/* send 3 msg types */ /* send 3 msg types */
ret_code = NewRequestHandler( (struct sockaddr*)&__ss, 3, &msgs[0] ); ret_code =
} else /* sub-device */ NewRequestHandler((struct sockaddr *)&__ss, 3, &msgs[0]);
{ } else { /* sub-device */
/* send 2 msg types */
ret_code = NewRequestHandler( (struct sockaddr*)&__ss, 2, &msgs[1] );
}
/* send 2 msg types */
ret_code =
NewRequestHandler((struct sockaddr *)&__ss, 2, &msgs[1]);
}
/* free msgs */ /* free msgs */
free(msgs[0]); free(msgs[0]);
free(msgs[1]); free(msgs[1]);
@ -544,33 +456,8 @@ DeviceAdvertisement( IN char *DevType,
return ret_code; return ret_code;
} }
/************************************************************************ int SendReply(struct sockaddr *DestAddr, char *DevType, int RootDev,
* Function : SendReply char *Udn, char *Location, int Duration, int ByType)
*
* Parameters:
* IN struct sockaddr * DestAddr:destination IP address.
* 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.
* 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( IN struct sockaddr *DestAddr,
IN char *DevType,
IN int RootDev,
IN char *Udn,
IN char *Location,
IN int Duration,
IN int ByType )
{ {
int ret_code; int ret_code;
char *msgs[2]; char *msgs[2];
@ -580,14 +467,14 @@ SendReply( IN struct sockaddr *DestAddr,
msgs[0] = NULL; msgs[0] = NULL;
msgs[1] = NULL; msgs[1] = NULL;
if (RootDev) { if (RootDev) {
/* one msg for root device */ /* one msg for root device */
num_msgs = 1; num_msgs = 1;
sprintf(Mil_Usn, "%s::upnp:rootdevice", Udn); sprintf(Mil_Usn, "%s::upnp:rootdevice", Udn);
CreateServicePacket(MSGTYPE_REPLY, "upnp:rootdevice", CreateServicePacket(MSGTYPE_REPLY, "upnp:rootdevice",
Mil_Usn, Location, Duration, &msgs[0], DestAddr->sa_family ); Mil_Usn, Location, Duration, &msgs[0],
DestAddr->sa_family);
} else { } else {
/* two msgs for embedded devices */ /* two msgs for embedded devices */
num_msgs = 1; num_msgs = 1;
@ -595,14 +482,15 @@ SendReply( IN struct sockaddr *DestAddr,
/*NK: FIX for extra response when someone searches by udn */ /*NK: FIX for extra response when someone searches by udn */
if (!ByType) { if (!ByType) {
CreateServicePacket(MSGTYPE_REPLY, Udn, Udn, Location, CreateServicePacket(MSGTYPE_REPLY, Udn, Udn, Location,
Duration, &msgs[0], DestAddr->sa_family ); Duration, &msgs[0],
DestAddr->sa_family);
} else { } else {
sprintf(Mil_Usn, "%s::%s", Udn, DevType); sprintf(Mil_Usn, "%s::%s", Udn, DevType);
CreateServicePacket(MSGTYPE_REPLY, DevType, Mil_Usn, CreateServicePacket(MSGTYPE_REPLY, DevType, Mil_Usn,
Location, Duration, &msgs[0], DestAddr->sa_family ); Location, Duration, &msgs[0],
DestAddr->sa_family);
} }
} }
/* check error */ /* check error */
for (i = 0; i < num_msgs; i++) { for (i = 0; i < num_msgs; i++) {
if (msgs[i] == NULL) { if (msgs[i] == NULL) {
@ -610,7 +498,6 @@ SendReply( IN struct sockaddr *DestAddr,
return UPNP_E_OUTOF_MEMORY; return UPNP_E_OUTOF_MEMORY;
} }
} }
/* send msgs */ /* send msgs */
ret_code = NewRequestHandler(DestAddr, num_msgs, msgs); ret_code = NewRequestHandler(DestAddr, num_msgs, msgs);
for (i = 0; i < num_msgs; i++) { for (i = 0; i < num_msgs; i++) {
@ -621,47 +508,23 @@ SendReply( IN struct sockaddr *DestAddr,
return ret_code; return ret_code;
} }
/************************************************************************ int DeviceReply(struct sockaddr *DestAddr, char *DevType, int RootDev,
* Function : DeviceReply char *Udn, char *Location, int Duration)
*
* Parameters:
* IN struct sockaddr *DestAddr:destination IP address.
* 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( IN struct sockaddr *DestAddr,
IN char *DevType,
IN int RootDev,
IN char *Udn,
IN char *Location,
IN int Duration)
{ {
char *szReq[3], char *szReq[3], Mil_Nt[LINE_SIZE], Mil_Usn[LINE_SIZE];
Mil_Nt[LINE_SIZE],
Mil_Usn[LINE_SIZE];
int RetVal; int RetVal;
szReq[0] = NULL; szReq[0] = NULL;
szReq[1] = NULL; szReq[1] = NULL;
szReq[2] = NULL; szReq[2] = NULL;
/* create 2 or 3 msgs */ /* create 2 or 3 msgs */
if (RootDev) { if (RootDev) {
/* 3 replies for root device */ /* 3 replies for root device */
strcpy(Mil_Nt, "upnp:rootdevice"); strcpy(Mil_Nt, "upnp:rootdevice");
sprintf(Mil_Usn, "%s::upnp:rootdevice", Udn); sprintf(Mil_Usn, "%s::upnp:rootdevice", Udn);
CreateServicePacket(MSGTYPE_REPLY, Mil_Nt, Mil_Usn, CreateServicePacket(MSGTYPE_REPLY, Mil_Nt, Mil_Usn,
Location, Duration, &szReq[0], DestAddr->sa_family ); Location, Duration, &szReq[0],
DestAddr->sa_family);
} }
sprintf(Mil_Nt, "%s", Udn); sprintf(Mil_Nt, "%s", Udn);
sprintf(Mil_Usn, "%s", Udn); sprintf(Mil_Usn, "%s", Udn);
@ -693,28 +556,8 @@ DeviceReply( IN struct sockaddr *DestAddr,
return RetVal; return RetVal;
} }
/************************************************************************ int ServiceAdvertisement(char *Udn, char *ServType, char *Location,
* Function : ServiceAdvertisement int Duration, int AddressFamily)
*
* Parameters:
* IN char * Udn: Device UDN
* 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( IN char *Udn,
IN char *ServType,
IN char *Location,
IN int Duration,
IN int AddressFamily)
{ {
char Mil_Usn[LINE_SIZE]; char Mil_Usn[LINE_SIZE];
char *szReq[1]; char *szReq[1];
@ -731,17 +574,15 @@ ServiceAdvertisement( IN char *Udn,
} else if (AddressFamily == AF_INET6) { } else if (AddressFamily == AF_INET6) {
DestAddr6->sin6_family = AF_INET6; DestAddr6->sin6_family = AF_INET6;
inet_pton(AF_INET6, inet_pton(AF_INET6,
(isUrlV6UlaGua(Location)) ? SSDP_IPV6_SITELOCAL : SSDP_IPV6_LINKLOCAL, (isUrlV6UlaGua(Location)) ? SSDP_IPV6_SITELOCAL :
&DestAddr6->sin6_addr ); SSDP_IPV6_LINKLOCAL, &DestAddr6->sin6_addr);
DestAddr6->sin6_port = htons(SSDP_PORT); DestAddr6->sin6_port = htons(SSDP_PORT);
DestAddr6->sin6_scope_id = gIF_INDEX; DestAddr6->sin6_scope_id = gIF_INDEX;
} else { } else {
UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__,
"Invalid device address family.\n"); "Invalid device address family.\n");
} }
sprintf(Mil_Usn, "%s::%s", Udn, ServType); sprintf(Mil_Usn, "%s::%s", Udn, ServType);
/* CreateServiceRequestPacket(1,szReq[0],Mil_Nt,Mil_Usn, /* CreateServiceRequestPacket(1,szReq[0],Mil_Nt,Mil_Usn,
* Server,Location,Duration); */ * Server,Location,Duration); */
CreateServicePacket(MSGTYPE_ADVERTISEMENT, ServType, Mil_Usn, CreateServicePacket(MSGTYPE_ADVERTISEMENT, ServType, Mil_Usn,
@ -749,78 +590,33 @@ ServiceAdvertisement( IN char *Udn,
if (szReq[0] == NULL) { if (szReq[0] == NULL) {
return UPNP_E_OUTOF_MEMORY; return UPNP_E_OUTOF_MEMORY;
} }
RetVal = NewRequestHandler((struct sockaddr *)&__ss, 1, szReq); RetVal = NewRequestHandler((struct sockaddr *)&__ss, 1, szReq);
free(szReq[0]); free(szReq[0]);
return RetVal; return RetVal;
} }
/************************************************************************ int ServiceReply(struct sockaddr *DestAddr, char *ServType, char *Udn,
* Function : ServiceReply char *Location, int Duration)
*
* Parameters:
* IN struct sockaddr *DestAddr:
* IN char * Udn: Device UDN
* IN char *ServType: Service Type.
* 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( IN struct sockaddr *DestAddr,
IN char *ServType,
IN char *Udn,
IN char *Location,
IN int Duration )
{ {
char Mil_Usn[LINE_SIZE]; char Mil_Usn[LINE_SIZE];
char *szReq[1]; char *szReq[1];
int RetVal; int RetVal;
szReq[0] = NULL; szReq[0] = NULL;
sprintf(Mil_Usn, "%s::%s", Udn, ServType); sprintf(Mil_Usn, "%s::%s", Udn, ServType);
CreateServicePacket(MSGTYPE_REPLY, ServType, Mil_Usn, CreateServicePacket(MSGTYPE_REPLY, ServType, Mil_Usn,
Location, Duration, &szReq[0], DestAddr->sa_family); Location, Duration, &szReq[0], DestAddr->sa_family);
if( szReq[0] == NULL ) { if (szReq[0] == NULL)
return UPNP_E_OUTOF_MEMORY; return UPNP_E_OUTOF_MEMORY;
}
RetVal = NewRequestHandler(DestAddr, 1, szReq); RetVal = NewRequestHandler(DestAddr, 1, szReq);
free(szReq[0]); free(szReq[0]);
return RetVal; return RetVal;
} }
/************************************************************************ int ServiceShutdown(char *Udn, char *ServType, char *Location, int Duration,
* Function : ServiceShutdown int AddressFamily)
*
* Parameters:
* IN char * Udn: Device UDN
* 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( IN char *Udn,
IN char *ServType,
IN char *Location,
IN int Duration,
IN int AddressFamily)
{ {
char Mil_Usn[LINE_SIZE]; char Mil_Usn[LINE_SIZE];
char *szReq[1]; char *szReq[1];
@ -837,56 +633,30 @@ ServiceShutdown( IN char *Udn,
} else if (AddressFamily == AF_INET6) { } else if (AddressFamily == AF_INET6) {
DestAddr6->sin6_family = AF_INET6; DestAddr6->sin6_family = AF_INET6;
inet_pton(AF_INET6, inet_pton(AF_INET6,
(isUrlV6UlaGua(Location)) ? SSDP_IPV6_SITELOCAL : SSDP_IPV6_LINKLOCAL, (isUrlV6UlaGua(Location)) ? SSDP_IPV6_SITELOCAL :
&DestAddr6->sin6_addr ); SSDP_IPV6_LINKLOCAL, &DestAddr6->sin6_addr);
DestAddr6->sin6_port = htons(SSDP_PORT); DestAddr6->sin6_port = htons(SSDP_PORT);
DestAddr6->sin6_scope_id = gIF_INDEX; DestAddr6->sin6_scope_id = gIF_INDEX;
} else { } else {
UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__,
"Invalid device address family.\n"); "Invalid device address family.\n");
} }
/* sprintf(Mil_Nt,"%s",ServType); */ /* sprintf(Mil_Nt,"%s",ServType); */
sprintf(Mil_Usn, "%s::%s", Udn, ServType); sprintf(Mil_Usn, "%s::%s", Udn, ServType);
/* CreateServiceRequestPacket(0,szReq[0],Mil_Nt,Mil_Usn, /* CreateServiceRequestPacket(0,szReq[0],Mil_Nt,Mil_Usn,
* Server,Location,Duration); */ * Server,Location,Duration); */
CreateServicePacket(MSGTYPE_SHUTDOWN, ServType, Mil_Usn, CreateServicePacket(MSGTYPE_SHUTDOWN, ServType, Mil_Usn,
Location, Duration, &szReq[0], AddressFamily); Location, Duration, &szReq[0], AddressFamily);
if( szReq[0] == NULL ) { if (szReq[0] == NULL)
return UPNP_E_OUTOF_MEMORY; return UPNP_E_OUTOF_MEMORY;
}
RetVal = NewRequestHandler((struct sockaddr *)&__ss, 1, szReq); RetVal = NewRequestHandler((struct sockaddr *)&__ss, 1, szReq);
free(szReq[0]); free(szReq[0]);
return RetVal; return RetVal;
} }
/************************************************************************ int DeviceShutdown(char *DevType, int RootDev, char *Udn, char *_Server,
* Function : DeviceShutdown char *Location, int Duration, int AddressFamily)
*
* Parameters:
* IN char *DevType: Device Type.
* IN int RootDev:1 means root device.
* IN char * Udn: Device UDN
* 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( IN char *DevType,
IN int RootDev,
IN char *Udn,
IN char *_Server,
IN char *Location,
IN int Duration,
IN int AddressFamily)
{ {
struct sockaddr_storage __ss; struct sockaddr_storage __ss;
struct sockaddr_in *DestAddr4 = (struct sockaddr_in *)&__ss; struct sockaddr_in *DestAddr4 = (struct sockaddr_in *)&__ss;
@ -898,7 +668,6 @@ DeviceShutdown( IN char *DevType,
msgs[0] = NULL; msgs[0] = NULL;
msgs[1] = NULL; msgs[1] = NULL;
msgs[2] = NULL; msgs[2] = NULL;
memset(&__ss, 0, sizeof(__ss)); memset(&__ss, 0, sizeof(__ss));
if (AddressFamily == AF_INET) { if (AddressFamily == AF_INET) {
DestAddr4->sin_family = AF_INET; DestAddr4->sin_family = AF_INET;
@ -907,8 +676,8 @@ DeviceShutdown( IN char *DevType,
} else if (AddressFamily == AF_INET6) { } else if (AddressFamily == AF_INET6) {
DestAddr6->sin6_family = AF_INET6; DestAddr6->sin6_family = AF_INET6;
inet_pton(AF_INET6, inet_pton(AF_INET6,
(isUrlV6UlaGua(Location)) ? SSDP_IPV6_SITELOCAL : SSDP_IPV6_LINKLOCAL, (isUrlV6UlaGua(Location)) ? SSDP_IPV6_SITELOCAL :
&DestAddr6->sin6_addr ); SSDP_IPV6_LINKLOCAL, &DestAddr6->sin6_addr);
DestAddr6->sin6_port = htons(SSDP_PORT); DestAddr6->sin6_port = htons(SSDP_PORT);
DestAddr6->sin6_scope_id = gIF_INDEX; DestAddr6->sin6_scope_id = gIF_INDEX;
} else { } else {
@ -919,7 +688,8 @@ DeviceShutdown( IN char *DevType,
if (RootDev) { if (RootDev) {
sprintf(Mil_Usn, "%s::upnp:rootdevice", Udn); sprintf(Mil_Usn, "%s::upnp:rootdevice", Udn);
CreateServicePacket(MSGTYPE_SHUTDOWN, "upnp:rootdevice", CreateServicePacket(MSGTYPE_SHUTDOWN, "upnp:rootdevice",
Mil_Usn, Location, Duration, &msgs[0], AddressFamily ); Mil_Usn, Location, Duration, &msgs[0],
AddressFamily);
} }
UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__, UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__,
"In function DeviceShutdown\n"); "In function DeviceShutdown\n");
@ -930,8 +700,7 @@ DeviceShutdown( IN char *DevType,
CreateServicePacket(MSGTYPE_SHUTDOWN, DevType, Mil_Usn, CreateServicePacket(MSGTYPE_SHUTDOWN, DevType, Mil_Usn,
Location, Duration, &msgs[2], AddressFamily); Location, Duration, &msgs[2], AddressFamily);
/* check error */ /* check error */
if( ( RootDev && msgs[0] == NULL ) || if ((RootDev && msgs[0] == NULL) || msgs[1] == NULL || msgs[2] == NULL) {
msgs[1] == NULL || msgs[2] == NULL ) {
free(msgs[0]); free(msgs[0]);
free(msgs[1]); free(msgs[1]);
free(msgs[2]); free(msgs[2]);
@ -940,11 +709,13 @@ DeviceShutdown( IN char *DevType,
/* send packets */ /* send packets */
if (RootDev) { if (RootDev) {
/* send 3 msg types */ /* send 3 msg types */
ret_code = NewRequestHandler( (struct sockaddr*)&__ss, 3, &msgs[0] ); ret_code =
NewRequestHandler((struct sockaddr *)&__ss, 3, &msgs[0]);
} else { } else {
/* sub-device */ /* sub-device */
/* send 2 msg types */ /* send 2 msg types */
ret_code = NewRequestHandler( (struct sockaddr*)&__ss, 2, &msgs[1] ); ret_code =
NewRequestHandler((struct sockaddr *)&__ss, 2, &msgs[1]);
} }
/* free msgs */ /* free msgs */
free(msgs[0]); free(msgs[0]);
@ -954,7 +725,7 @@ DeviceShutdown( IN char *DevType,
return ret_code; return ret_code;
_Server = _Server; _Server = _Server;
} }
#endif /* EXCLUDE_SSDP */ #endif /* EXCLUDE_SSDP */
#endif /* INCLUDE_DEVICE_APIS */ #endif /* INCLUDE_DEVICE_APIS */
/* @} SSDPlib */

File diff suppressed because it is too large Load Diff