Logo Search packages:      
Sourcecode: libupnp4 version File versions  Download package

EXPORT_SPEC int UpnpInit ( const char *  HostIP,
unsigned short  DestPort 

Initializes the Linux SDK for UPnP Devices.

Kept for backwards compatibility. Use UpnpInit2 for new implementations.
This function must be called before any other API function can be called. It should be called only once. Subsequent calls to this API return a UPNP_E_INIT error code.

Optionally, the application can specify a host IP address (in the case of a multi-homed configuration) and a port number to use for all UPnP operations. Since a port number can be used only by one process, multiple processes using the SDK must specify different port numbers.

If unspecified, the SDK will use the first adapter's IP address and an arbitrary port.

This call is synchronous.

An integer representing one of the following:
  • UPNP_E_SUCCESS: The operation completed successfully.
  • UPNP_E_OUTOF_MEMORY: Insufficient resources exist to initialize the SDK.
  • UPNP_E_INIT: The SDK is already initialized.
  • UPNP_E_INIT_FAILED: The SDK initialization failed for an unknown reason.
  • UPNP_E_SOCKET_BIND: An error occurred binding a socket.
  • UPNP_E_LISTEN: An error occurred listening to a socket.
  • UPNP_E_OUTOF_SOCKET: An error ocurred creating a socket.
  • UPNP_E_INTERNAL_ERROR: An internal error ocurred.
HostIP  The host local IP address to use, in string format, for example "", or NULL to use the first adapter's IP address.
DestPort  Local Port to listen for incoming connections NULL will pick an arbitrary free port.

Definition at line 202 of file upnpapi.c.

References getlocalhostname(), gIF_IPV4, gSDKInitMutex, LOCAL_PORT_V4, UPNP_E_INIT, UPNP_E_INIT_FAILED, UPNP_E_SUCCESS, UpnpInitPreamble(), UpnpInitStartServers(), UpnpPrintf(), and UpnpSdkInit.

      int retVal;


      // Check if we're already initialized.
      if( UpnpSdkInit == 1 ) {
            return UPNP_E_INIT;

      // Perform initialization preamble.
      retVal = UpnpInitPreamble();
      if( retVal != UPNP_E_SUCCESS ) {
            return retVal;

      UpnpPrintf( UPNP_INFO, API, __FILE__, __LINE__,
            "UpnpInit with HostIP=%s, DestPort=%d.\n", 
            HostIP ? HostIP : "", DestPort );

      // Verify HostIP, if provided, or find it ourselves.
      if( HostIP != NULL ) {
            strncpy( gIF_IPV4, HostIP, sizeof(gIF_IPV4) );
      } else {
            if( getlocalhostname( gIF_IPV4, sizeof(gIF_IPV4) ) != UPNP_E_SUCCESS ) {
                  return UPNP_E_INIT_FAILED;

      // Set the UpnpSdkInit flag to 1 to indicate we're sucessfully initialized.
      UpnpSdkInit = 1;

      // Finish initializing the SDK.
      retVal = UpnpInitStartServers( DestPort );
      if( retVal != UPNP_E_SUCCESS ) {
            UpnpSdkInit = 0;
            ithread_mutex_unlock( &gSDKInitMutex );
            return retVal;

      UpnpPrintf( UPNP_INFO, API, __FILE__, __LINE__,
            "Host Ip: %s Host Port: %d\n", gIF_IPV4,
            LOCAL_PORT_V4 );

      ithread_mutex_unlock( &gSDKInitMutex );

      return UPNP_E_SUCCESS;

Generated by  Doxygen 1.6.0   Back to index