Functions | Variables

upnpapi.c File Reference

#include "config.h"
#include <sys/stat.h>
#include <assert.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
#include "upnpapi.h"
#include "httpreadwrite.h"
#include "membuffer.h"
#include "ssdplib.h"
#include "soaplib.h"
#include "ThreadPool.h"
#include "sysdep.h"
#include "uuid.h"
#include "gena.h"
#include "miniserver.h"
#include "service_table.h"
Include dependency graph for upnpapi.c:

Functions

static int WinsockInit (void)
 (Windows Only) Initializes the Windows Winsock library.
static int UpnpInitMutexes (void)
 Initializes the global mutexes used by the UPnP SDK.
static int UpnpInitThreadPools (void)
 Initializes the global threadm pools used by the UPnP SDK.
static int UpnpInitPreamble (void)
 Performs the initial steps in initializing the UPnP SDK.
static int UpnpInitStartServers (unsigned short DestPort)
 Finishes initializing the UPnP SDK.
int UpnpInit (const char *HostIP, unsigned short DestPort)
 Initializes the Linux SDK for UPnP Devices (IPv4 only).
int UpnpInit2 (const char *IfName, unsigned short DestPort)
 Initializes the Linux SDK for UPnP Devices (IPv4 or IPv6).
int UpnpFinish (void)
 Terminates the Linux SDK for UPnP Devices.
unsigned short UpnpGetServerPort (void)
 Returns the internal server IPv4 UPnP listening port.
unsigned short UpnpGetServerPort6 (void)
 Returns the internal server IPv6 UPnP listening port.
const char * UpnpGetServerIpAddress (void)
 Returns the local IPv4 listening ip address.
const char * UpnpGetServerIp6Address (void)
 Returns the local IPv6 listening ip address.
int UpnpRegisterRootDevice (const char *DescUrl, Upnp_FunPtr Fun, const void *Cookie, UpnpDevice_Handle *Hnd)
 Registers a device application with the UPnP Library.
static int GetDescDocumentAndURL (Upnp_DescType descriptionType, char *description, unsigned int bufferLen, int config_baseURL, int AddressFamily, IXML_Document **xmlDoc, char *descURL)
 Fills the sockadr_in with miniserver information.
int UpnpRegisterRootDevice2 (Upnp_DescType descriptionType, const char *description_const, size_t bufferLen, int config_baseURL, Upnp_FunPtr Fun, const void *Cookie, UpnpDevice_Handle *Hnd)
 Registers a device application with the UPnP Library. Similar to UpnpRegisterRootDevice, except that it also allows the description document to be specified as a file or a memory buffer.
int UpnpRegisterRootDevice3 (const char *DescUrl, Upnp_FunPtr Fun, const void *Cookie, UpnpDevice_Handle *Hnd, const int AddressFamily)
 Registers a device application for a specific address family with the UPnP library.
int UpnpUnRegisterRootDevice (UpnpDevice_Handle Hnd)
 Unregisters a root device registered with UpnpRegisterRootDevice or UpnpRegisterRootDevice2.
int UpnpRegisterClient (Upnp_FunPtr Fun, const void *Cookie, UpnpClient_Handle *Hnd)
 Registers a control point application with the UPnP Library.
int UpnpUnRegisterClient (UpnpClient_Handle Hnd)
 Unregisters a control point application, unsubscribing all active subscriptions.
int UpnpSendAdvertisement (UpnpDevice_Handle Hnd, int Exp)
 Sends out the discovery announcements for all devices and services for a device.
int UpnpSearchAsync (UpnpClient_Handle Hnd, int Mx, const char *Target_const, const void *Cookie_const)
 Searches for devices matching the given search target.
int UpnpSetMaxSubscriptions (UpnpDevice_Handle Hnd, int MaxSubscriptions)
 Sets the maximum number of subscriptions accepted per service.
int UpnpSetMaxSubscriptionTimeOut (UpnpDevice_Handle Hnd, int MaxSubscriptionTimeOut)
 Sets the maximum time-out accepted for a subscription request or renewal.
int UpnpSubscribeAsync (UpnpClient_Handle Hnd, const char *EvtUrl_const, int TimeOut, Upnp_FunPtr Fun, const void *Cookie_const)
 Performs the same operation as UpnpSubscribe, but returns immediately and calls the registered callback function when the operation is complete.
int UpnpSubscribe (UpnpClient_Handle Hnd, const char *EvtUrl_const, int *TimeOut, Upnp_SID SubsId)
 Registers a control point to receive event notifications from another device.
int UpnpUnSubscribe (UpnpClient_Handle Hnd, const Upnp_SID SubsId)
 Removes the subscription of a control point from a service previously subscribed to using UpnpSubscribe or UpnpSubscribeAsync.
int UpnpUnSubscribeAsync (UpnpClient_Handle Hnd, Upnp_SID SubsId, Upnp_FunPtr Fun, const void *Cookie_const)
 Removes a subscription of a control point from a service previously subscribed to using UpnpSubscribe or UpnpSubscribeAsync, generating a callback when the operation is complete.
int UpnpRenewSubscription (UpnpClient_Handle Hnd, int *TimeOut, const Upnp_SID SubsId)
 Renews a subscription that is about to expire.
int UpnpRenewSubscriptionAsync (UpnpClient_Handle Hnd, int TimeOut, Upnp_SID SubsId, Upnp_FunPtr Fun, const void *Cookie_const)
 Renews a subscription that is about to expire, generating a callback when the operation is complete.
int UpnpNotify (UpnpDevice_Handle Hnd, const char *DevID_const, const char *ServName_const, const char **VarName_const, const char **NewVal_const, int cVariables)
 Sends out an event change notification to all control points subscribed to a particular service.
int UpnpNotifyExt (UpnpDevice_Handle Hnd, const char *DevID_const, const char *ServName_const, IXML_Document *PropSet)
 Similar to UpnpNotify except that it takes a DOM document for the event rather than an array of strings.
int UpnpAcceptSubscription (UpnpDevice_Handle Hnd, const char *DevID_const, const char *ServName_const, const char **VarName_const, const char **NewVal_const, int cVariables, const Upnp_SID SubsId)
 Accepts a subscription request and sends out the current state of the eventable variables for a service.
int UpnpAcceptSubscriptionExt (UpnpDevice_Handle Hnd, const char *DevID_const, const char *ServName_const, IXML_Document *PropSet, const Upnp_SID SubsId)
 Similar to UpnpAcceptSubscription() except that it takes a DOM document for the variables to event rather than an array of strings.
int UpnpSendAction (UpnpClient_Handle Hnd, const char *ActionURL_const, const char *ServiceType_const, const char *DevUDN_const, IXML_Document *Action, IXML_Document **RespNodePtr)
 Sends a message to change a state variable in a service.
int UpnpSendActionEx (UpnpClient_Handle Hnd, const char *ActionURL_const, const char *ServiceType_const, const char *DevUDN_const, IXML_Document *Header, IXML_Document *Action, IXML_Document **RespNodePtr)
 Sends a message to change a state variable in a service.
int UpnpSendActionAsync (UpnpClient_Handle Hnd, const char *ActionURL_const, const char *ServiceType_const, const char *DevUDN_const, IXML_Document *Act, Upnp_FunPtr Fun, const void *Cookie_const)
 Sends a message to change a state variable in a service, generating a callback when the operation is complete.
int UpnpSendActionExAsync (UpnpClient_Handle Hnd, const char *ActionURL_const, const char *ServiceType_const, const char *DevUDN_const, IXML_Document *Header, IXML_Document *Act, Upnp_FunPtr Fun, const void *Cookie_const)
 Sends a message to change a state variable in a service, generating a callback when the operation is complete.
int UpnpGetServiceVarStatusAsync (UpnpClient_Handle Hnd, const char *ActionURL_const, const char *VarName_const, Upnp_FunPtr Fun, const void *Cookie_const)
 Queries the state of a variable of a service, generating a callback when the operation is complete.
int UpnpGetServiceVarStatus (UpnpClient_Handle Hnd, const char *ActionURL_const, const char *VarName_const, DOMString *StVar)
 Queries the state of a state variable of a service on another device.
int UpnpOpenHttpPost (const char *url, void **handle, const char *contentType, int contentLength, int timeout)
 Makes an HTTP POST request message, opens a connection to the server and sends the POST request to the server if the connection to the server succeeds.
int UpnpWriteHttpPost (void *handle, char *buf, unsigned int *size, int timeout)
 Sends a request to a server to copy the contents of a buffer to the URI specified in the UpnpOpenHttpPost call.
int UpnpCloseHttpPost (void *handle, int *httpStatus, int timeout)
 Sends and receives any pending data, closes the connection with the server, and frees memory allocated during the UpnpOpenHttpPost call.
int UpnpOpenHttpGet (const char *url_str, void **Handle, char **contentType, int *contentLength, int *httpStatus, int timeout)
 Gets a file specified in a URL.
int UpnpOpenHttpGetProxy (const char *url_str, const char *proxy_str, void **Handle, char **contentType, int *contentLength, int *httpStatus, int timeout)
 Gets a file specified in a URL through the specified proxy.
int UpnpOpenHttpGetEx (const char *url_str, void **Handle, char **contentType, int *contentLength, int *httpStatus, int lowRange, int highRange, int timeout)
 Gets specified number of bytes from a file specified in the URL.
int UpnpCancelHttpGet (void *Handle)
 Set the cancel flag of the handle parameter.
int UpnpCloseHttpGet (void *Handle)
 Closes the connection and frees memory that was allocated for the handle parameter.
int UpnpReadHttpGet (void *Handle, char *buf, unsigned int *size, int timeout)
 Gets specified number of bytes from a file specified in a URL.
int UpnpHttpGetProgress (void *Handle, unsigned int *length, unsigned int *total)
 Retrieve progress information of a http-get transfer.
int UpnpDownloadUrlItem (const char *url, char **outBuf, char *contentType)
 Downloads a file specified in a URL.
int UpnpDownloadXmlDoc (const char *url, IXML_Document **xmlDoc)
 Downloads an XML document specified in a URL.
int UpnpGetIfInfo (const char *IfName)
 Retrieve interface information and keep it in global variables. If NULL, we'll find the first suitable interface for operation.
void UpnpThreadDistribution (struct UpnpNonblockParam *Param)
 Schedule async functions in threadpool.
Upnp_FunPtr GetCallBackFn (UpnpClient_Handle Hnd)
 Get callback function ptr from a handle.
int GetFreeHandle ()
 Get a free handle.
Upnp_Handle_Type GetClientHandleInfo (UpnpClient_Handle *client_handle_out, struct Handle_Info **HndInfo)
Upnp_Handle_Type GetDeviceHandleInfo (const int AddressFamily, UpnpDevice_Handle *device_handle_out, struct Handle_Info **HndInfo)
Upnp_Handle_Type GetHandleInfo (UpnpClient_Handle Hnd, struct Handle_Info **HndInfo)
int FreeHandle (int Upnp_Handle)
 Free handle.
int PrintHandleInfo (UpnpClient_Handle Hnd)
 Print handle info.
int getlocalhostname (char *out, const int out_len)
 Get local IP address.
void AutoAdvertise (void *input)
 This function is a timer thread scheduled by UpnpSendAdvertisement to the send advetisement again.
int UpnpAddVirtualDir (const char *newDirName)
 Adds a virtual directory mapping.
int UpnpRemoveVirtualDir (const char *dirName)
 Removes a virtual directory mapping made with UpnpAddVirtualDir.
void UpnpRemoveAllVirtualDirs (void)
 Removes all virtual directory mappings.
int UpnpEnableWebserver (int enable)
 Enables or disables the webserver.
int UpnpIsWebserverEnabled (void)
 Checks if the webserver is enabled or disabled.
int UpnpVirtualDir_set_GetInfoCallback (VDCallback_GetInfo callback)
 Sets the get_info callback function to be used to access a virtual directory.
int UpnpVirtualDir_set_OpenCallback (VDCallback_Open callback)
 Sets the open callback function to be used to access a virtual directory.
int UpnpVirtualDir_set_ReadCallback (VDCallback_Read callback)
 Sets the read callback function to be used to access a virtual directory.
int UpnpVirtualDir_set_WriteCallback (VDCallback_Write callback)
 Sets the write callback function to be used to access a virtual directory.
int UpnpVirtualDir_set_SeekCallback (VDCallback_Seek callback)
 Sets the seek callback function to be used to access a virtual directory.
int UpnpVirtualDir_set_CloseCallback (VDCallback_Close callback)
 Sets the close callback function to be used to access a virtual directory.
int UpnpSetContentLength (UpnpClient_Handle Hnd, int contentLength)
int UpnpSetMaxContentLength (size_t contentLength)
 Sets the maximum content-length that the SDK will process on an incoming SOAP requests or responses.

Variables

struct VirtualDirCallbacks virtualDirCallback
virtualDirListpVirtualDirList
ithread_mutex_t GlobalClientSubscribeMutex
ithread_rwlock_t GlobalHndRWLock
ithread_mutex_t gUUIDMutex
ithread_mutex_t gSDKInitMutex = PTHREAD_MUTEX_INITIALIZER
TimerThread gTimerThread
ThreadPool gSendThreadPool
ThreadPool gRecvThreadPool
ThreadPool gMiniServerThreadPool
WebServerState bWebServerState = WEB_SERVER_DISABLED
char gIF_NAME [LINE_SIZE] = { '\0' }
char gIF_IPV4 [22] = { '\0' }
char gIF_IPV6 [65] = { '\0' }
int gIF_INDEX = -1
unsigned short LOCAL_PORT_V4
unsigned short LOCAL_PORT_V6
void * HandleTable [NUM_HANDLE]
membuffer gDocumentRootDir
size_t g_maxContentLength = DEFAULT_SOAP_CONTENT_LENGTH
int UpnpSdkInit = 0
int UpnpSdkClientRegistered = 0
int UpnpSdkDeviceRegisteredV4 = 0
int UpnpSdkDeviceregisteredV6 = 0
Upnp_SID gUpnpSdkNLSuuid

Detailed Description


Function Documentation

void AutoAdvertise ( void *  input  ) 

This function is a timer thread scheduled by UpnpSendAdvertisement to the send advetisement again.

Parameters:
[in] input Information provided to the thread.

References free_upnp_timeout(), and UpnpSendAdvertisement().

Referenced by UpnpSendAdvertisement().

int FreeHandle ( int  Handle  ) 

Free handle.

Returns:
UPNP_E_SUCCESS if successful or UPNP_E_INVALID_HANDLE if not
Parameters:
[in] Upnp_Handle Handle index.

References HandleTable, and UpnpPrintf().

Referenced by UpnpRegisterRootDevice(), UpnpRegisterRootDevice2(), UpnpRegisterRootDevice3(), UpnpUnRegisterClient(), and UpnpUnRegisterRootDevice().

Upnp_FunPtr GetCallBackFn ( UpnpClient_Handle  Hnd  ) 

Get callback function ptr from a handle.

Returns:
Upnp_FunPtr

References HandleTable.

static int GetDescDocumentAndURL ( Upnp_DescType  descriptionType,
char *  description,
unsigned int  bufferLen,
int  config_baseURL,
int  AddressFamily,
IXML_Document **  xmlDoc,
char *  descURL 
) [static]

Fills the sockadr_in with miniserver information.

References UPNP_E_SUCCESS, UpnpDownloadXmlDoc(), and UPNPREG_URL_DESC.

Referenced by UpnpRegisterRootDevice2().

int GetFreeHandle (  ) 

Get a free handle.

Returns:
On success, an integer greater than zero or UPNP_E_OUTOF_HANDLE on failure.

References HandleTable.

Referenced by UpnpRegisterClient(), UpnpRegisterRootDevice(), UpnpRegisterRootDevice2(), and UpnpRegisterRootDevice3().

int getlocalhostname ( char *  out,
const int  out_len 
)

Get local IP address.

Gets the ip address for the DEFAULT_INTERFACE interface which is up and not a loopback. Assumes at most MAX_INTERFACES interfaces

Returns:
UPNP_E_SUCCESS if successful or UPNP_E_INIT.
Parameters:
[out] out IP address of the interface.
[in] out_len Length of the output buffer.

References UPNP_E_SUCCESS, and UpnpPrintf().

Referenced by UpnpInit().

int PrintHandleInfo ( UpnpClient_Handle  Hnd  ) 

Print handle info.

Returns:
UPNP_E_SUCCESS if successful, otherwise returns appropriate error.
Parameters:
[in] Hnd Handle index.

References Handle_Info::DescURL, HandleTable, Handle_Info::HType, and UpnpPrintf().

int UpnpGetIfInfo ( const char *  IfName  ) 

Retrieve interface information and keep it in global variables. If NULL, we'll find the first suitable interface for operation.

The interface must fulfill these requirements:

  • Be UP.
  • Not be LOOPBACK.
  • Support MULTICAST.
  • Have a valid IPv4 or IPv6 address.

We'll retrieve the following information from the interface:

  • gIF_NAME -> Interface name (by input or found).
  • gIF_IPV4 -> IPv4 address (if any).
  • gIF_IPV6 -> IPv6 address (if any).
  • gIF_INDEX -> Interface index number.
Returns:
UPNP_E_SUCCESS on success.
Parameters:
[in] IfName Interface name (can be NULL).

References gIF_INDEX, gIF_IPV4, gIF_IPV6, gIF_NAME, and UpnpPrintf().

Referenced by UpnpInit2().

static int UpnpInitMutexes ( void   )  [static]

Initializes the global mutexes used by the UPnP SDK.

Returns:
UPNP_E_SUCCESS on success or UPNP_E_INIT_FAILED if a mutex could not be initialized.

References GlobalClientSubscribeMutex, GlobalHndRWLock, and gUUIDMutex.

Referenced by UpnpInitPreamble().

static int UpnpInitPreamble ( void   )  [static]

Performs the initial steps in initializing the UPnP SDK.

  • Winsock library is initialized for the process (Windows specific).
  • The logging (for debug messages) is initialized.
  • Mutexes, Handle table and thread pools are allocated and initialized.
  • Callback functions for SOAP and GENA are set, if they're enabled.
  • The SDK timer thread is initialized.
Returns:
UPNP_E_SUCCESS on success.

References genaCallback(), gUpnpSdkNLSuuid, HandleTable, SetGenaCallback(), SetSoapCallback(), TimerThreadInit(), UPNP_E_SUCCESS, UpnpFinish(), UpnpInitLog(), UpnpInitMutexes(), UpnpInitThreadPools(), UpnpPrintf(), and WinsockInit().

Referenced by UpnpInit(), and UpnpInit2().

static int UpnpInitStartServers ( unsigned short  DestPort  )  [static]

Finishes initializing the UPnP SDK.

  • The MiniServer is started, if enabled.
  • The WebServer is started, if enabled.
Returns:
UPNP_E_SUCCESS on success or UPNP_E_INIT_FAILED if a mutex could not be initialized.
Parameters:
[in] DestPort Local Port to listen for incoming connections.

References LOCAL_PORT_V4, LOCAL_PORT_V6, StartMiniServer(), UPNP_E_SUCCESS, UpnpEnableWebserver(), UpnpFinish(), and UpnpPrintf().

Referenced by UpnpInit(), and UpnpInit2().

static int UpnpInitThreadPools ( void   )  [static]

Initializes the global threadm pools used by the UPnP SDK.

Returns:
UPNP_E_SUCCESS on success or UPNP_E_INIT_FAILED if a mutex could not be initialized.

References UPNP_E_SUCCESS, UpnpFinish(), and UpnpSdkInit.

Referenced by UpnpInitPreamble().

void UpnpThreadDistribution ( struct UpnpNonblockParam Param  ) 
static int WinsockInit ( void   )  [static]

(Windows Only) Initializes the Windows Winsock library.

Returns:
UPNP_E_SUCCESS on success, UPNP_E_INIT_FAILED on failure.

Referenced by UpnpInitPreamble().


Variable Documentation

WebServerState bWebServerState = WEB_SERVER_DISABLED

Flag to indicate the state of web server

Referenced by UpnpEnableWebserver(), and UpnpIsWebserverEnabled().

size_t g_maxContentLength = DEFAULT_SOAP_CONTENT_LENGTH

Maximum content-length (in bytes) that the SDK will process on an incoming packet. Content-Length exceeding this size will be not processed and error 413 (HTTP Error Code) will be returned to the remote end point.

Referenced by UpnpSetContentLength(), and UpnpSetMaxContentLength().

a local dir which serves as webserver root

int gIF_INDEX = -1

Contains interface index. (extern'ed in upnp.h)

Referenced by parse_hostport(), and UpnpGetIfInfo().

char gIF_IPV4[22] = { '\0' }

Static buffer to contain interface IPv4 address. (extern'ed in upnp.h)

Referenced by gena_subscribe(), UpnpGetIfInfo(), UpnpGetServerIpAddress(), and UpnpInit().

char gIF_IPV6[65] = { '\0' }

Static buffer to contain interface IPv6 address. (extern'ed in upnp.h)

Referenced by gena_subscribe(), UpnpGetIfInfo(), and UpnpGetServerIp6Address().

char gIF_NAME[LINE_SIZE] = { '\0' }

Static buffer to contain interface name. (extern'ed in upnp.h)

Referenced by UpnpGetIfInfo().

ithread_mutex_t GlobalClientSubscribeMutex

Mutex to synchronize the subscription handling at the client side.

Referenced by UpnpFinish(), and UpnpInitMutexes().

ithread_rwlock_t GlobalHndRWLock

rwlock to synchronize handles (root device or control point handle).

Referenced by UpnpFinish(), and UpnpInitMutexes().

Mini server thread pool.

Referenced by StartMiniServer().

Receive thread pool.

ithread_mutex_t gSDKInitMutex = PTHREAD_MUTEX_INITIALIZER

Initialization mutex.

Referenced by UpnpInit(), and UpnpInit2().

Global variable used in discovery notifications.

Referenced by UpnpInitPreamble().

ithread_mutex_t gUUIDMutex

Mutex to synchronize the uuid creation process.

Referenced by UpnpFinish(), and UpnpInitMutexes().

void* HandleTable[NUM_HANDLE]
unsigned short LOCAL_PORT_V4

local IPv4 port for the mini-server

Referenced by gena_subscribe(), UpnpGetServerPort(), UpnpInit(), and UpnpInitStartServers().

unsigned short LOCAL_PORT_V6

local IPv6 port for the mini-server

Referenced by gena_subscribe(), UpnpGetServerPort6(), and UpnpInitStartServers().

Pointer to the virtual directory list.

Global variable to denote the state of Upnp SDK client registration. == 0 if unregistered, == 1 if registered.

Referenced by UpnpRegisterClient(), and UpnpUnRegisterClient().

Global variable to denote the state of Upnp SDK IPv4 device registration. == 0 if unregistered, == 1 if registered.

Referenced by UpnpRegisterRootDevice(), UpnpRegisterRootDevice2(), UpnpRegisterRootDevice3(), and UpnpUnRegisterRootDevice().

Global variable to denote the state of Upnp SDK IPv6 device registration. == 0 if unregistered, == 1 if registered.

Referenced by UpnpRegisterRootDevice3(), and UpnpUnRegisterRootDevice().

int UpnpSdkInit = 0