upnpapi.c File Reference
UPnP API
`#include "config.h"`
`#include "upnpapi.h"`
`#include "ThreadPool.h"`
`#include "UpnpStdInt.h"`
`#include "UpnpUniStd.h"`
`#include "httpreadwrite.h"`
`#include "membuffer.h"`
`#include "soaplib.h"`
`#include "ssdplib.h"`
`#include "sysdep.h"`
`#include "uuid.h"`
`#include "gena.h"`
`#include "miniserver.h"`
`#include "service_table.h"`
`#include <sys/stat.h>`
`#include <assert.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include "posix_overwrites.h"`
`#include <ifaddrs.h>`
`#include <sys/ioctl.h>`
`#include <sys/param.h>`
`#include <sys/types.h>`
Include dependency graph for upnpapi.c:

## Data Structures

struct | job_arg

## Macros

#define | ifr_netmask ifr_addr
#define | IN6_IS_ADDR_GLOBAL(a)
#define | IN6_IS_ADDR_ULA(a)

## Functions

static void | free_advertise_arg (job_arg *arg)
| Free memory associated with advertise job's argument.
static void | free_action_arg (job_arg *arg)
| Free memory associated with an action job's argument.
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.
void | PrintThreadPoolStats (ThreadPool *tp, const char *DbgFileName, int DbgLineNo, const char *msg)
| Prints thread pool statistics.
static int | GetFreeHandle ()
| Get a free handle.
static int | FreeHandle (int Upnp_Handle)
| Free handle.
static int | GetDescDocumentAndURL (Upnp_DescType descriptionType, char *description, int config_baseURL, int AddressFamily, IXML_Document **xmlDoc, char descURL[LINE_SIZE])
| Fills the sockadr_in with miniserver information.
static unsigned | UpnpComputeIpv6PrefixLength (struct sockaddr_in6 *Netmask)
| Computes prefix length from IPv6 netmask.
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.
Upnp_Handle_Type | GetClientHandleInfo (UpnpClient_Handle *client_handle_out, struct Handle_Info **HndInfo)
| Get client handle info.
Upnp_Handle_Type | GetDeviceHandleInfo (UpnpDevice_Handle start, int AddressFamily, UpnpDevice_Handle *device_handle_out, struct Handle_Info **HndInfo)
| Retrieves the device handle and information of the first device of the address family specified. The search begins at the 'start' index, which should be 0 for the first call, then the last successful value returned. This allows listing all entries for the address family.
Upnp_Handle_Type | GetDeviceHandleInfoForPath (const char *path, int AddressFamily, UpnpDevice_Handle *device_handle_out, struct Handle_Info **HndInfo, service_info **serv_info)
| Retrieves the device handle and information of the first device of the address family specified, with a service having a controlURL or eventSubURL matching the path.
Upnp_Handle_Type | GetHandleInfo (UpnpClient_Handle Hnd, struct Handle_Info **HndInfo)
int | PrintHandleInfo (UpnpClient_Handle Hnd)
| Print handle info.
void | AutoAdvertise (void *input)
| This function is a timer thread scheduled by UpnpSendAdvertisement to the send advetisement again.
int | UpnpSetEventQueueLimits (int maxLen, int maxAge)
Initialization and Registration
int | UpnpInit2 (const char *IfName, unsigned short DestPort)
| Initializes the Linux SDK for UPnP Devices (IPv4 or IPv6).
int | UpnpFinish (void)
| Initializes the OpenSSL library, and the OpenSSL context for use with pupnp.
unsigned short | UpnpGetServerPort (void)
| Returns the internal server IPv4 UPnP listening port.
unsigned short | UpnpGetServerPort6 (void)
| Returns the internal server IPv6 link-local (LLA) UPnP listening port.
unsigned short | UpnpGetServerUlaGuaPort6 (void)
| Returns the internal server IPv6 ULA or GUA UPnP listening port.
char * | UpnpGetServerIpAddress (void)
| Returns the local IPv4 listening ip address.
char * | UpnpGetServerIp6Address (void)
| Returns the IPv6 link-local listening ip address.
char * | UpnpGetServerUlaGuaIp6Address (void)
| Returns the IPv6 unique-local or globally-unique listening ip address.
int | UpnpRegisterRootDevice (const char *DescUrl, Upnp_FunPtr Callback, const void *Cookie, UpnpDevice_Handle *Hnd)
| Registers a device application with the UPnP Library.
int | UpnpRegisterRootDevice2 (Upnp_DescType descriptionType, const char *description, 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 Callback, const void *Cookie, UpnpDevice_Handle *Hnd, int AddressFamily)
| Registers a device application for a specific address family with the UPnP library.
int | UpnpRegisterRootDevice4 (const char *DescUrl, Upnp_FunPtr Callback, const void *Cookie, UpnpDevice_Handle *Hnd, int AddressFamily, const char *LowerDescUrl)
| Registers a device application for a specific address family with the UPnP library. This function can also be used to specify a dedicated description URL to be returned for legacy CPs.
int | UpnpUnRegisterRootDevice (UpnpDevice_Handle Hnd)
| Unregisters a root device registered with UpnpRegisterRootDevice, UpnpRegisterRootDevice2, UpnpRegisterRootDevice3 or UpnpRegisterRootDevice4.
int | UpnpUnRegisterRootDeviceLowPower (UpnpDevice_Handle Hnd, int PowerState, int SleepPeriod, int RegistrationState)
| Unregisters a root device registered with UpnpRegisterRootDevice, UpnpRegisterRootDevice2, UpnpRegisterRootDevice3 or UpnpRegisterRootDevice4.
int | UpnpRegisterClient (Upnp_FunPtr Callback, 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 | UpnpSetContentLength (UpnpClient_Handle Hnd, size_t contentLength)
int | UpnpSetMaxContentLength (size_t contentLength)
| Sets the maximum content-length that the SDK will process on an incoming SOAP requests or responses.
Discovery
int | UpnpSendAdvertisement (UpnpDevice_Handle Hnd, int Exp)
| Sends out the discovery announcements for all devices and services for a device.
int | UpnpSendAdvertisementLowPower (UpnpDevice_Handle Hnd, int Exp, int PowerState, int SleepPeriod, int RegistrationState)
| Sends out the discovery announcements for all devices and services for a device.
int | UpnpSearchAsync (UpnpClient_Handle Hnd, int Mx, const char *TTarget_constarget_const, const void *Cookie_const)
| Searches for devices matching the given search target.
Eventing
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 *PublisherUrl, int TimeOut, Upnp_FunPtr Fun, const void *Cookie)
| 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 *PublisherUrl, 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)
| 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)
| Renews a subscription that is about to expire, generating a callback when the operation is complete.
int | UpnpNotify (UpnpDevice_Handle, const char *DevID, const char *ServID, const char **VarName, const char **NewVal, int cVariables)
| Sends out an event change notification to all control points subscribed to a particular service.
int | UpnpNotifyExt (UpnpDevice_Handle, const char *DevID, const char *ServID, 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 char *ServID, const char **VarName, const char **NewVal, 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 char *ServID, 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.
Control
int | UpnpSendAction (UpnpClient_Handle Hnd, const char *ActionURL, const char *ServiceType, const char *DevUDN, IXML_Document *Action, IXML_Document **RespNode)
| Sends a message to change a state variable in a service.
int | UpnpSendActionEx (UpnpClient_Handle Hnd, const char *ActionURL, const char *ServiceType, const char *DevUDN, IXML_Document *Header, IXML_Document *Action, IXML_Document **RespNode)
| Sends a message to change a state variable in a service.
int | UpnpSendActionAsync (UpnpClient_Handle Hnd, const char *ActionURL, const char *ServiceType, const char *DevUDN, IXML_Document *Action, Upnp_FunPtr Fun, const void *Cookie)
| 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 char *ServiceType, const char *DevUDN, IXML_Document *Header, IXML_Document *Action, Upnp_FunPtr Fun, const void *Cookie)
| 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 char *VarName, Upnp_FunPtr Fun, const void *Cookie)
| 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 char *VarName, DOMString *StVarVal)
| Queries the state of a state variable of a service on another device.
Control Point HTTP API
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, size_t *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, void **handle, char **contentType, int *contentLength, int *httpStatus, int timeout)
| Gets a file specified in a URL.
int | UpnpOpenHttpGetProxy (const char *url, 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, 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, size_t *size, int timeout)
| Gets specified number of bytes from a file specified in a URL.
int | UpnpHttpGetProgress (void *handle, size_t *length, size_t *total)
| Retrieve progress information of a http-get transfer.
int | UpnpOpenHttpConnection (const char *url, void **handle, int timeout)
| Opens a connection to the server.
int | UpnpMakeHttpRequest (Upnp_HttpMethod method, const char *url, void *handle, UpnpString *headers, const char *contentType, int contentLength, int timeout)
| Makes a HTTP request using a connection previously created by UpnpOpenHttpConnection.
int | UpnpWriteHttpRequest (void *handle, char *buf, size_t *size, int timeout)
| Writes the content of a HTTP request initiated by a UpnpMakeHttpRequest call. The end of the content should be indicated by a call to UpnpEndHttpRequest.
int | UpnpEndHttpRequest (void *handle, int timeout)
| Indicates the end of a HTTP request previously made by UpnpMakeHttpRequest.
int | UpnpGetHttpResponse (void *handle, UpnpString *headers, char **contentType, int *contentLength, int *httpStatus, int timeout)
| Gets the response from the server using a connection previously created by UpnpOpenHttpConnection.
int | UpnpReadHttpResponse (void *handle, char *buf, size_t *size, int timeout)
| Reads the content of a response using a connection previously created by UpnpOpenHttpConnection.
int | UpnpCloseHttpConnection (void *handle)
| Closes the connection created with UpnpOpenHttpConnection and frees any memory associated with the connection.
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.
Web Server API
int | UpnpAddVirtualDir (const char *dirName, const void *cookie, const void **oldcookie)
| 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)
| Returns `1` if the webserver is enabled, or `0` if it is not.
void | UpnpSetHostValidateCallback (WebCallback_HostValidate callback, void *cookie)
void | UpnpSetAllowLiteralHostRedirection (int enable)
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.

## Variables

struct VirtualDirCallbacks | virtualDirCallback
virtualDirList * | pVirtualDirList
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
WebCallback_HostValidate | gWebCallback_HostValidate = 0
void * | gWebCallback_HostValidateCookie = 0
int | gAllowLiteralHostRedirection = 0
char | gIF_NAME [LINE_SIZE] = {'\0'}
char | gIF_IPV4 [INET_ADDRSTRLEN] = {'\0'}
char | gIF_IPV4_NETMASK [INET_ADDRSTRLEN] = {'\0'}
char | gIF_IPV6 [INET6_ADDRSTRLEN] = {'\0'}
unsigned | gIF_IPV6_PREFIX_LENGTH = 0
char | gIF_IPV6_ULA_GUA [INET6_ADDRSTRLEN] = {'\0'}
unsigned | gIF_IPV6_ULA_GUA_PREFIX_LENGTH = 0
unsigned | gIF_INDEX = (unsigned)-1
unsigned short | LOCAL_PORT_V4
unsigned short | LOCAL_PORT_V6
unsigned short | LOCAL_PORT_V6_ULA_GUA
static void * | HandleTable [NUM_HANDLE]
membuffer | gDocumentRootDir
membuffer | gWebserverCorsString
size_t | g_maxContentLength = DEFAULT_SOAP_CONTENT_LENGTH
int | g_UpnpSdkEQMaxLen = MAX_SUBSCRIPTION_QUEUED_EVENTS
int | g_UpnpSdkEQMaxAge = MAX_SUBSCRIPTION_EVENT_AGE
int | UpnpSdkInit = 0
int | UpnpSdkClientRegistered = 0
int | UpnpSdkDeviceRegisteredV4 = 0
int | UpnpSdkDeviceregisteredV6 = 0