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

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.
out_len  [in] Length of the output buffer.

Definition at line 3470 of file upnpapi.c.

References UPNP_E_INIT, and UpnpPrintf().

Referenced by UpnpInit().

{
#ifdef WIN32
      struct hostent *h=NULL;
      struct sockaddr_in LocalAddr;

            gethostname(out, out_len);
            h=gethostbyname(out);
            if (h!=NULL){
                  memcpy(&LocalAddr.sin_addr,h->h_addr_list[0],4);
                  strncpy(out, inet_ntoa(LocalAddr.sin_addr), out_len);
            }
            return UPNP_E_SUCCESS;
#elif (defined(BSD) && BSD >= 199306)
    struct ifaddrs *ifap, *ifa;

    if (getifaddrs(&ifap) != 0) {
        UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__,
            "DiscoverInterfaces: getifaddrs() returned error\n" );
        return UPNP_E_INIT;
    }

    // cycle through available interfaces
    for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
        // Skip loopback, point-to-point and down interfaces, 
        // except don't skip down interfaces
        // if we're trying to get a list of configurable interfaces. 
        if( ( ifa->ifa_flags & IFF_LOOPBACK )
            || ( !( ifa->ifa_flags & IFF_UP ) ) ) {
            continue;
        }
        if( ifa->ifa_addr->sa_family == AF_INET ) {
            // We don't want the loopback interface. 
            if( ((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr ==
                htonl( INADDR_LOOPBACK ) ) {
                continue;
            }

            strncpy( out, inet_ntoa( ((struct sockaddr_in *)(ifa->ifa_addr))->
                sin_addr ), out_len );
            out[LINE_SIZE-1] = '\0';
            UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__,
                "Inside getlocalhostname : after strncpy %s\n",
                out );
            break;
        }
    }
    freeifaddrs(ifap);

    return ifa ? UPNP_E_SUCCESS : UPNP_E_INIT;
#else
    char szBuffer[MAX_INTERFACES * sizeof( struct ifreq )];
    struct ifconf ifConf;
    struct ifreq ifReq;
    int nResult;
    int i;
    int LocalSock;
    struct sockaddr_in LocalAddr;
    int j = 0;

    // Create an unbound datagram socket to do the SIOCGIFADDR ioctl on. 
    if( ( LocalSock = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ) {
        UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__,
            "Can't create addrlist socket\n" );
        return UPNP_E_INIT;
    }
    // Get the interface configuration information... 
    ifConf.ifc_len = sizeof szBuffer;
    ifConf.ifc_ifcu.ifcu_buf = ( caddr_t ) szBuffer;
    nResult = ioctl( LocalSock, SIOCGIFCONF, &ifConf );

    if( nResult < 0 ) {
        UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__,
            "DiscoverInterfaces: SIOCGIFCONF returned error\n" );

        return UPNP_E_INIT;
    }
    // Cycle through the list of interfaces looking for IP addresses. 

    for( i = 0; ( ( i < ifConf.ifc_len ) && ( j < DEFAULT_INTERFACE ) ); ) {
        struct ifreq *pifReq =
            ( struct ifreq * )( ( caddr_t ) ifConf.ifc_req + i );
        i += sizeof *pifReq;

        // See if this is the sort of interface we want to deal with.
        strcpy( ifReq.ifr_name, pifReq->ifr_name );
        if( ioctl( LocalSock, SIOCGIFFLAGS, &ifReq ) < 0 ) {
            UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__,
                "Can't get interface flags for %s:\n",
                ifReq.ifr_name );
        }
        // Skip loopback, point-to-point and down interfaces, 
        // except don't skip down interfaces
        // if we're trying to get a list of configurable interfaces. 
        if( ( ifReq.ifr_flags & IFF_LOOPBACK )
            || ( !( ifReq.ifr_flags & IFF_UP ) ) ) {
            continue;
        }
        if( pifReq->ifr_addr.sa_family == AF_INET ) {
            // Get a pointer to the address...
            memcpy( &LocalAddr, &pifReq->ifr_addr,
                    sizeof pifReq->ifr_addr );

            // We don't want the loopback interface. 
            if( LocalAddr.sin_addr.s_addr == htonl( INADDR_LOOPBACK ) ) {
                continue;
            }

        }
        //increment j if we found an address which is not loopback
        //and is up
        j++;

    }
    close( LocalSock );

    strncpy( out, inet_ntoa( LocalAddr.sin_addr ), out_len );

    UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__,
        "Inside getlocalhostname : after strncpy %s\n",
        out );
    return UPNP_E_SUCCESS;
#endif
}


Generated by  Doxygen 1.6.0   Back to index