// -----------------------------------------------------------------------------
// CLocalAddrResolver::CheckAndSetAddr
// -----------------------------------------------------------------------------
//
void CLocalAddrResolver::CheckAndSetAddr( TInetAddr& aTarget,
                                          TInetAddr& aCandidate,
                                          TUint32 aCandidateIap,
				                          TUint32 aSpecifiedIap )
    {
    if ( !aCandidate.IsUnspecified() && !aCandidate.IsLoopback() )
        {
        if ( aCandidate.IsV4Mapped())
            {
            aCandidate.ConvertToV4();
            }
        if ( aCandidateIap == aSpecifiedIap )
            {
            SetAddr( aTarget, aCandidate );
            }
        }
    }				                          
Example #2
0
/**
*   List existing network interfaces, IP addresses bound to them 
*   and fill up array of IP addresses for all interfaces.
*/
void CTestStepLLMNR_Init::ListInterfacesL()
    {
    
    RSocket socket;
    TInt    nRes;
    TInt    exceed;
    TInt    idx;
    TName   tmpBuf;
    TName   tmpBuf1;
    
    nRes = socket.Open(ipTestServer->iSocketServer, KAfInet, KSockStream, KProtocolInetTcp);
    TESTL(nRes == KErrNone);
    
    TUint   bufsize = 2048;
    
    HBufC8 *buffer =NULL;
    buffer = GetBuffer(buffer, bufsize);
    TESTL(buffer != NULL);
    
    TPtr8 bufdes = buffer->Des();
    
    //-- reset array of local addresses
    ipTestServer->iLocalAddrs.Reset();
    
    //-- list all available network interfaces
    INFO_PRINTF1(KNewLine);
    INFO_PRINTF1(_L("--- available network interfaces:"));
    
    do
        {//-- get list of network interfaces
        // if exceed>0, all interface could not fit into the buffer.
        // In that case allocate a bigger buffer and retry.
        // There should be no reason for this while loop to take more than 2 rounds.
        bufdes.Set(buffer->Des());
        exceed = socket.GetOpt(KSoInetInterfaceInfo, KSolInetIfQuery, bufdes);
        if(exceed > 0)
            {
            bufsize += exceed * sizeof(TInetInterfaceInfo);
            buffer = GetBuffer(buffer, bufsize);
            TESTL(buffer != NULL);
            }
        } while (exceed > 0);
        
        if (exceed < 0)
            {
            INFO_PRINTF1(_L("socket.GetOpt() error!"));
            TESTL(EFalse);
            }
        
        TOverlayArray<TInetInterfaceInfo> infoIface(bufdes);
        
        for(idx=0; idx < infoIface.Length(); ++idx)
            {
            TInetInterfaceInfo& iface = infoIface[idx];
            
            tmpBuf.Format(_L("index:%d, name: "),iface.iIndex);
            tmpBuf.Append(iface.iName );
            tmpBuf.AppendFormat(_L(" state:%d"), iface.iState);
            INFO_PRINTF1(tmpBuf);
            }
        
        //-- list all IP addresses, bound to the interfaces
        //-- and append this address to the array of host-local addresses
        INFO_PRINTF1(KNewLine);
        INFO_PRINTF1(_L("--- IP addresses bound to the interfaces:"));
        
        do
            {
            // if exceed>0, all interface could not fit into the buffer.
            // In that case allocate a bigger buffer and retry.
            // There should be no reason for this while loop to take more than 2 rounds.
	        bufdes.Set(buffer->Des());
            exceed = socket.GetOpt(KSoInetAddressInfo, KSolInetIfQuery, bufdes);
            if(exceed > 0)
                {
                bufsize += exceed * sizeof(TInetAddressInfo);
                buffer = GetBuffer(buffer, bufsize);
                }
            } while (exceed > 0);
            
            if (exceed < 0)
                {
                INFO_PRINTF1(_L("socket.GetOpt() error!"));
                TESTL(EFalse);
                }
            
            
            //-- print out IP addresses
            TOverlayArray<TInetAddressInfo> infoAddr(bufdes);
            TInetAddr inetAddr;
            
            for(idx=0; idx < infoAddr.Length(); ++idx)
                {
                TInetAddressInfo& addr = infoAddr[idx];
                
                tmpBuf.Format(_L("iface index: %d, scopeID: %d, state: %d, IP addr: "), addr.iInterface, addr.iScopeId, addr.iState);
                inetAddr.SetAddress(addr.iAddress);
                inetAddr.Output(tmpBuf1);
                tmpBuf.Append(tmpBuf1);
                INFO_PRINTF1(tmpBuf);
                
                //-- if obtained IP address is valid and not loopback, add it to the array.
                if(inetAddr.IsLoopback() || inetAddr.IsUnspecified())
                    {}
                else
                    ipTestServer->iLocalAddrs.Append(inetAddr);
                
                }
            
            
            delete buffer;
            socket.Close();
            
            INFO_PRINTF1(KNewLine);
}