ssdplib.h
Go to the documentation of this file.
#ifndef SSDPLIB_H
#define SSDPLIB_H
/**************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
* Copyright (C) 2011-2012 France Telecom All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************/
#include "httpparser.h"
#include "httpreadwrite.h"
#include "miniserver.h"
#include "UpnpInet.h"
#include <sys/types.h>
#include <signal.h>
#include <setjmp.h>
#include <errno.h>
#ifdef _WIN32
#else /* _WIN32 */
#include <syslog.h>
#ifndef __APPLE__
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#endif /* __APPLE__ */
#include <sys/time.h>
#endif /* _WIN32 */
typedef enum SsdpSearchType {
SSDP_SERROR = -1,
SSDP_ALL,
SSDP_ROOTDEVICE,
SSDP_DEVICEUDN,
SSDP_DEVICETYPE,
SSDP_SERVICE
} SType;
#define BUFSIZE (size_t)2500
#define SSDP_IP "239.255.255.250"
#define SSDP_IPV6_LINKLOCAL "FF02::C"
#define SSDP_IPV6_SITELOCAL "FF05::C"
#define SSDP_PORT 1900
#define NUM_TRY 3
#define THREAD_LIMIT 50
#define COMMAND_LEN 300
#ifndef X_USER_AGENT
#define X_USER_AGENT "redsonic"
#endif
#define NO_ERROR_FOUND 0
#define E_REQUEST_INVALID -3
#define E_RES_EXPIRED -4
#define E_MEM_ALLOC -5
#define E_HTTP_SYNTEX -6
#define E_SOCKET -7
#define RQST_TIMEOUT 20
typedef struct SsdpEventStruct {
enum SsdpSearchType RequestType;
int ErrCode;
int MaxAge;
int Mx;
char UDN[LINE_SIZE];
char DeviceType[LINE_SIZE];
/* NT or ST */
char ServiceType[LINE_SIZE];
char Location[LINE_SIZE];
char HostAddr[LINE_SIZE];
char Os[LINE_SIZE];
char Ext[LINE_SIZE];
char Date[LINE_SIZE];
struct sockaddr *DestAddr;
void * Cookie;
} SsdpEvent;
typedef void (* SsdpFunPtr)(SsdpEvent *);
typedef struct TData
{
int Mx;
void * Cookie;
char * Data;
struct sockaddr_storage DestAddr;
} ThreadData;
typedef struct ssdpsearchreply
{
int MaxAge;
UpnpDevice_Handle handle;
struct sockaddr_storage dest_addr;
SsdpEvent event;
} SsdpSearchReply;
typedef struct ssdpsearcharg
{
int timeoutEventId;
char * searchTarget;
void *cookie;
enum SsdpSearchType requestType;
} SsdpSearchArg;
typedef struct ssdpsearchexparg
{
int handle;
int timeoutEventId;
} SsdpSearchExpArg;
typedef struct
{
http_parser_t parser;
struct sockaddr_storage dest_addr;
} ssdp_thread_data;
/* globals */
#ifdef INCLUDE_CLIENT_APIS
extern SOCKET gSsdpReqSocket4;
#ifdef UPNP_ENABLE_IPV6
extern SOCKET gSsdpReqSocket6;
#endif /* UPNP_ENABLE_IPV6 */
#endif /* INCLUDE_CLIENT_APIS */
typedef int (*ParserFun)(char *, SsdpEvent *);
int AdvertiseAndReply(
/* [in] -1 = Send shutdown, 0 = send reply, 1 = Send Advertisement. */
int AdFlag,
/* [in] Device handle. */
UpnpDevice_Handle Hnd,
/* [in] Search type for sending replies. */
enum SsdpSearchType SearchType,
/* [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);
int unique_service_name(
/* [in] Service Name string. */
char *cmd,
/* [out] The SSDP event structure partially filled by all the
* function. */
SsdpEvent *Evt);
enum SsdpSearchType ssdp_request_type1(
/* [in] command came in the ssdp request. */
char *cmd);
int ssdp_request_type(
/* [in] command came in the ssdp request. */
char *cmd,
/* [out] The event structure partially filled by this function. */
SsdpEvent *Evt);
void readFromSSDPSocket(
/* [in] SSDP socket. */
SOCKET socket);
int get_ssdp_sockets(
/* [out] Array of SSDP sockets. */
MiniServerSockArray *out);
/* @} SSDP Server Functions */
void ssdp_handle_ctrlpt_msg(
/* [in] SSDP message from the device. */
http_message_t *hmsg,
/* [in] Address of the device. */
struct sockaddr_storage *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);
int SearchByTarget(
/* [in] The handle of the client performing the search. */
int Hnd,
/* [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);
/* @} SSDP Control Point Functions */
void *advertiseAndReplyThread(
/* [in] Structure containing the search request. */
void *data);
#ifdef INCLUDE_DEVICE_APIS
void ssdp_handle_device_request(
/* [in] . */
http_message_t *hmsg,
/* [in] . */
struct sockaddr_storage *dest_addr);
#else /* INCLUDE_DEVICE_APIS */
static UPNP_INLINE void ssdp_handle_device_request(
/* [in] . */
http_message_t *hmsg,
/* [in] . */
struct sockaddr_storage *dest_addr) {}
#endif /* INCLUDE_DEVICE_APIS */
int DeviceAdvertisement(
/* [in] type of the device. */
char *DevType,
/* [in] flag to indicate if the device is root device. */
int RootDev,
/* [in] UDN. */
char *Udn,
/* [in] Location URL. */
char *Location,
/* [in] Service duration in sec. */
int Duration,
/* [in] Device address family. */
int AddressFamily,
/* [in] PowerState as defined by UPnP Low Power. */
int PowerState,
/* [in] SleepPeriod as defined by UPnP Low Power. */
int SleepPeriod,
/* [in] RegistrationState as defined by UPnP Low Power. */
int RegistrationState);
int SendReply(
/* [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,
/* [in] . */
int ByType,
/* [in] PowerState as defined by UPnP Low Power. */
int PowerState,
/* [in] SleepPeriod as defined by UPnP Low Power. */
int SleepPeriod,
/* [in] RegistrationState as defined by UPnP Low Power. */
int RegistrationState);
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,
/* [in] PowerState as defined by UPnP Low Power. */
int PowerState,
/* [in] SleepPeriod as defined by UPnP Low Power. */
int SleepPeriod,
/* [in] RegistrationState as defined by UPnP Low Power. */
int RegistrationState);
int ServiceAdvertisement(
/* [in] Device UDN. */
char *Udn,
/* [in] Service Type. */
char *ServType,
/* [in] Location of Device description document. */
char *Location,
/* [in] Life time of this device. */
int Duration,
/* [in] Device address family. */
int AddressFamily,
/* [in] PowerState as defined by UPnP Low Power. */
int PowerState,
/* [in] SleepPeriod as defined by UPnP Low Power. */
int SleepPeriod,
/* [in] RegistrationState as defined by UPnP Low Power. */
int RegistrationState);
int ServiceReply(
/* [in] . */
struct sockaddr *DestAddr,
/* [in] Service Type. */
char *ServType,
/* [in] Device UDN. */
char *Udn,
/* [in] Location of Device description document. */
char *Location,
/* [in] Life time of this device. */
int Duration,
/* [in] PowerState as defined by UPnP Low Power. */
int PowerState,
/* [in] SleepPeriod as defined by UPnP Low Power. */
int SleepPeriod,
/* [in] RegistrationState as defined by UPnP Low Power. */
int RegistrationState);
int ServiceShutdown(
/* [in] Device UDN. */
char *Udn,
/* [in] Service Type. */
char *ServType,
/* [in] Location of Device description document. */
char *Location,
/* [in] Service duration in sec. */
int Duration,
/* [in] Device address family. */
int AddressFamily,
/* [in] PowerState as defined by UPnP Low Power. */
int PowerState,
/* [in] SleepPeriod as defined by UPnP Low Power. */
int SleepPeriod,
/* [in] RegistrationState as defined by UPnP Low Power. */
int RegistrationState);
int DeviceShutdown(
/* [in] Device Type. */
char *DevType,
/* [in] 1 means root device. */
int RootDev,
/* [in] Device UDN. */
char *Udn,
/* [in] Location URL. */
char *Location,
/* [in] Device duration in sec. */
int Duration,
/* [in] Device address family. */
int AddressFamily,
/* [in] PowerState as defined by UPnP Low Power. */
int PowerState,
/* [in] SleepPeriod as defined by UPnP Low Power. */
int SleepPeriod,
/* [in] RegistrationState as defined by UPnP Low Power. */
int RegistrationState);
/* @} SSDP Device Functions */
/* @} SSDPlib SSDP Library */
#endif /* SSDPLIB_H */
SSDP_SERROR
Definition: ssdplib.h:68
miniserver.h
MServerSockArray
Definition: miniserver.h:45
http_message_t
Definition: httpparser.h:179
ssdp_thread_data
Definition: ssdplib.h:157
SendReply
int SendReply(struct sockaddr *DestAddr, char *DevType, int RootDev, char *Udn, char *Location, int Duration, int ByType, int PowerState, int SleepPeriod, int RegistrationState)
Creates the reply packet based on the input parameter, and send it to the client addesss given in its...
Definition: ssdp_device.c:577
DeviceAdvertisement
int DeviceAdvertisement(char *DevType, int RootDev, char *Udn, char *Location, int Duration, int AddressFamily, int PowerState, int SleepPeriod, int RegistrationState)
Creates the device advertisement request based on the input parameter, and send it to the multicast c...
Definition: ssdp_device.c:493
readFromSSDPSocket
void readFromSSDPSocket(SOCKET socket)
This function reads the data from the ssdp socket.
Definition: ssdp_server.c:697
SsdpEvent
struct SsdpEventStruct SsdpEvent
ssdpsearcharg
Definition: ssdplib.h:143
ssdp_request_type
int ssdp_request_type(char *cmd, SsdpEvent *Evt)
Starts filling the SSDP event structure based upon the request received.
Definition: ssdp_server.c:549
SsdpEventStruct
Definition: ssdplib.h:107
SType
enum SsdpSearchType SType
ServiceAdvertisement
int ServiceAdvertisement(char *Udn, char *ServType, char *Location, int Duration, int AddressFamily, int PowerState, int SleepPeriod, int RegistrationState)
Creates the advertisement packet based on the input parameter, and send it to the multicast channel...
Definition: ssdp_device.c:704
advertiseAndReplyThread
void * advertiseAndReplyThread(void *data)
Wrapper function to reply the search request coming from the control point.
Definition: ssdp_device.c:67
TData
Definition: ssdplib.h:127
SearchByTarget
int SearchByTarget(int Hnd, int Mx, char *St, void *Cookie)
Creates and send the search request for a specific URL.
Definition: ssdp_ctrlpt.c:543
ssdp_handle_device_request
void ssdp_handle_device_request(http_message_t *hmsg, struct sockaddr_storage *dest_addr)
Handles the search request. It does the sanity checks of the request and then schedules a thread to s...
Definition: ssdp_device.c:82
UpnpInet.h
Provides a platform independent way to include TCP/IP types and functions.
ssdp_request_type1
enum SsdpSearchType ssdp_request_type1(char *cmd)
This function figures out the type of the SSDP search in the in the request.
Definition: ssdp_server.c:534
UpnpDevice_Handle
int UpnpDevice_Handle
Returned when a device application registers with UpnpRegisterRootDevice, UpnpRegisterRootDevice2, UpnpRegisterRootDevice3 or UpnpRegisterRootDevice4.
Definition: upnp.h:436
ssdp_handle_ctrlpt_msg
void ssdp_handle_ctrlpt_msg(http_message_t *hmsg, struct sockaddr_storage *dest_addr, int timeout, void *cookie)
This function handles the ssdp messages from the devices. These messages includes the search replies...
Definition: ssdp_ctrlpt.c:78
ssdpsearchreply
Definition: ssdplib.h:135
SOCKET
int SOCKET
Definition: UpnpInet.h:48
DeviceShutdown
int DeviceShutdown(char *DevType, int RootDev, char *Udn, char *Location, int Duration, int AddressFamily, int PowerState, int SleepPeriod, int RegistrationState)
Creates a HTTP device shutdown request packet and send it to the multicast channel through RequestHan...
Definition: ssdp_device.c:832
ServiceReply
int ServiceReply(struct sockaddr *DestAddr, char *ServType, char *Udn, char *Location, int Duration, int PowerState, int SleepPeriod, int RegistrationState)
Creates the advertisement packet based on the input parameter, and send it to the multicast channel...
Definition: ssdp_device.c:755
AdvertiseAndReply
int AdvertiseAndReply(int AdFlag, UpnpDevice_Handle Hnd, enum SsdpSearchType SearchType, struct sockaddr *DestAddr, char *DeviceType, char *DeviceUDN, char *ServiceType, int Exp)
Sends SSDP advertisements, replies and shutdown messages.
Definition: ssdp_server.c:92
SsdpSearchType
SsdpSearchType
Definition: ssdplib.h:66
http_parser_t
Definition: httpparser.h:213
DeviceReply
int DeviceReply(struct sockaddr *DestAddr, char *DevType, int RootDev, char *Udn, char *Location, int Duration, int PowerState, int SleepPeriod, int RegistrationState)
Creates the reply packet based on the input parameter, and send it to the client address given in its...
Definition: ssdp_device.c:641
unique_service_name
int unique_service_name(char *cmd, SsdpEvent *Evt)
Fills the fields of the event structure like DeviceType, Device UDN and Service Type.
Definition: ssdp_server.c:448
httpparser.h
ssdpsearchexparg
Definition: ssdplib.h:151
get_ssdp_sockets
int get_ssdp_sockets(MiniServerSockArray *out)
Creates the IPv4 and IPv6 ssdp sockets required by the control point and device operation.
Definition: ssdp_server.c:1168
UPNP_INLINE
#define UPNP_INLINE
Declares an inline function.
Definition: UpnpGlobal.h:99
ServiceShutdown
int ServiceShutdown(char *Udn, char *ServType, char *Location, int Duration, int AddressFamily, int PowerState, int SleepPeriod, int RegistrationState)
Creates a HTTP service shutdown request packet and sends it to the multicast channel through RequestH...
Definition: ssdp_device.c:781