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

BOOL PIPSocket::IsLocalHost ( const PString hostname  )  [static, inherited]

Determine if the specified host is actually the local machine. This can be any of the host aliases or multi-homed IP numbers or even the special number 127.0.0.1 for the loopback device.

Returns:
TRUE if the host is the local machine.

Definition at line 782 of file winsock.cxx.

References PIPSocket::GetHostAddress(), PIPSocket::GetHostName(), PString::IsEmpty(), and PIPSocket::Address::IsLoopback().

Referenced by PSTUNClient::GetNatType().

{
  if (hostname.IsEmpty())
    return TRUE;

  if (hostname *= "localhost")
    return TRUE;

  // lookup the host address using inet_addr, assuming it is a "." address
  PIPSocket::Address addr = hostname;
  if (addr.IsLoopback())  // Is 127.0.0.1 or ::1
    return TRUE;

  if (addr == 0) {
    if (!GetHostAddress(hostname, addr))
      return FALSE;
  }

  // Seb: Should check that it's really IPv4 aware.
  struct hostent * host_info = ::gethostbyname(GetHostName());

  if (P_IsOldWin95())
    return addr == *(struct in_addr *)host_info->h_addr_list[0];

  for (PINDEX i = 0; host_info->h_addr_list[i] != NULL; i++) {
#if P_HAS_IPV6
    if (host_info->h_length == 16) {
      if (addr == *(struct in6_addr *)host_info->h_addr_list[i])
        return TRUE;
    }
    else
#endif
    if (addr == *(struct in_addr *)host_info->h_addr_list[i])
      return TRUE;
  }
  return FALSE;
}


Generated by  Doxygen 1.6.0   Back to index