Using RSocket::LocalName(TInetAddr&)

Login to reply to this topic.
Thu, 2007-02-22 07:54
Joined: 2006-10-07
Forum posts: 131
Greetings,


I have the following code snippet:

Code:
TInetAddr aAddr(KInetAddrAny, 0);

...

User::LeaveIfError( iListenSock.Open(iServ, KAfInet, KSockStream, KProtocolInetTcp, iConn) );
User::LeaveIfError( iListenSock.Bind(aAddr) );
User::LeaveIfError( iListenSock.Listen(QUEUE_SIZE) );

...

TInetAddr addr;
iListenSock.LocalName(addr);

RSocket::LocalName gets the allocated port, but doesn't retrieve the interface IP address this socket is bound to (address returned is always of the form 0.0.0.0:<port>).

  • Is there any issue with code listed above?
  • Is there an alternative method of getting IP interface that a socket is bound to (having in possession RSocket and RConnection handles)?


Thanks in advance.

Fri, 2007-02-23 16:38
Joined: 2005-09-23
Forum posts: 9
Re: Using RSocket::LocalName(TInetAddr&)
This works using a temporary socket.
No problem with Listen



/*----------------Finds out local IP address of the device----------------------*/
TInt CSocketsEngine::GetLocalIPAddressL( RConnection& aConnection, TInetAddr& aAddr )
{
  RSocket sock;
  User::LeaveIfError(sock.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp));
  // Get the IAP id of the underlying interface of this RConnection
  TUint32 iapId = 0;
  User::LeaveIfError(aConnection.GetIntSetting(_L("IAP\\Id"), iapId));
  // Get IP information from the socket
  TSoInetInterfaceInfo ifinfo;
  TPckg<TSoInetInterfaceInfo> ifinfopkg(ifinfo);
  TSoInetIfQuery ifquery;
  TPckg<TSoInetIfQuery> ifquerypkg(ifquery);
  // To find out which interfaces are using our current IAP, we must
  // enumerate and go through all of them and make a query by name for each.
  User::LeaveIfError(sock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
  while(sock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifinfopkg) == KErrNone) {
    ifquery.iName = ifinfo.iName;
    TInt err = sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifquerypkg);
    if( err == KErrNone && ifquery.iZone[1] == iapId) { // IAP ID is index 1 of iZone
        // We have found an interface using the IAP we are interested in.
        if( ifinfo.iAddress.Address() >0 ) {
            // found a IPv4 address
            aAddr = ifinfo.iAddress;
            sock.Close();
            return err; // stop & return KErrNone
        }
      }
      else if( err !=KErrNone ) {
        sock.Close();
        return err; // return with error
    }
  } // while
  sock.Close();
  return KErrNotFound; // return with KErrNotFound
}
Mon, 2007-02-26 07:57
Joined: 2006-10-07
Forum posts: 131
Re: Using RSocket::LocalName(TInetAddr&)
Thanks andrea.
  • Login to reply to this topic.