src/net/base/network_interfaces_win_unittest.cc
bool FillAdapterAddress(IP_ADAPTER_ADDRESSES* adapter_address,
                        const char* ifname,
                        const IPAddress& ip_address,
                        const IPAddress& ip_netmask,
                        sockaddr_storage sock_addrs[2]) {
  adapter_address->AdapterName = const_cast<char*>(ifname);
  adapter_address->FriendlyName = const_cast<PWCHAR>(L"interface");
  adapter_address->IfType = IF_TYPE_ETHERNET_CSMACD;
  adapter_address->OperStatus = IfOperStatusUp;
  adapter_address->FirstUnicastAddress->DadState = IpDadStatePreferred;
  adapter_address->FirstUnicastAddress->PrefixOrigin = IpPrefixOriginOther;
  adapter_address->FirstUnicastAddress->SuffixOrigin = IpSuffixOriginOther;
  adapter_address->FirstUnicastAddress->PreferredLifetime = 100;
  adapter_address->FirstUnicastAddress->ValidLifetime = 1000;

  socklen_t sock_len = sizeof(sockaddr_storage);

  // Convert to sockaddr for next check.
  if (!IPEndPoint(ip_address, 0)
           .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addrs[0]),
                       &sock_len)) {
    return false;
  }
  adapter_address->FirstUnicastAddress->Address.lpSockaddr =
      reinterpret_cast<sockaddr*>(&sock_addrs[0]);
  adapter_address->FirstUnicastAddress->Address.iSockaddrLength = sock_len;
  adapter_address->FirstUnicastAddress->OnLinkPrefixLength = 1;

  sock_len = sizeof(sockaddr_storage);
  if (!IPEndPoint(ip_netmask, 0)
           .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addrs[1]),
                       &sock_len)) {
    return false;
  }
  adapter_address->FirstPrefix->Address.lpSockaddr =
      reinterpret_cast<sockaddr*>(&sock_addrs[1]);
  adapter_address->FirstPrefix->Address.iSockaddrLength = sock_len;
  adapter_address->FirstPrefix->PrefixLength = 1;

  DCHECK_EQ(sock_addrs[0].ss_family, sock_addrs[1].ss_family);
  if (sock_addrs[0].ss_family == AF_INET6) {
    adapter_address->Ipv6IfIndex = 0;
  } else {
    DCHECK_EQ(sock_addrs[0].ss_family, AF_INET);
    adapter_address->IfIndex = 0;
  }

  return true;
}
// Helper function to create a valid IP_ADAPTER_ADDRESSES with reasonable
// default value. The output is the |adapter_address|. All the rests are input
// to fill the |adapter_address|. |sock_addrs| are temporary storage used by
// |adapter_address| once the function is returned.
Helper function to create a valid IP_ADAPTER_ADDRESSES with reasonable
default value. The output is the |adapter_address|. All the rests are input
to fill the |adapter_address|. |sock_addrs| are temporary storage used by
|adapter_address| once the function is returned.
Helper function to create a valid IP_ADAPTER_ADDRESSES with reasonable default value . The output is the |adapter_address| . All the rests are input to fill the |adapter_address| . |sock_addrs| are temporary storage used by |adapter_address| once the function is returned .
bool FillAdapterAddress(IP_ADAPTER_ADDRESSES* adapter_address,
                        const char* ifname,
                        const IPAddress& ip_address,
                        const IPAddress& ip_netmask,
                        sockaddr_storage sock_addrs[2]) {
  adapter_address->AdapterName = const_cast<char*>(ifname);
  adapter_address->FriendlyName = const_cast<PWCHAR>(L"interface");
  adapter_address->IfType = IF_TYPE_ETHERNET_CSMACD;
  adapter_address->OperStatus = IfOperStatusUp;
  adapter_address->FirstUnicastAddress->DadState = IpDadStatePreferred;
  adapter_address->FirstUnicastAddress->PrefixOrigin = IpPrefixOriginOther;
  adapter_address->FirstUnicastAddress->SuffixOrigin = IpSuffixOriginOther;
  adapter_address->FirstUnicastAddress->PreferredLifetime = 100;
  adapter_address->FirstUnicastAddress->ValidLifetime = 1000;
  socklen_t sock_len = sizeof(sockaddr_storage);
  if (!IPEndPoint(ip_address, 0)
           .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addrs[0]),
                       &sock_len)) {
    return false;
  }
  adapter_address->FirstUnicastAddress->Address.lpSockaddr =
      reinterpret_cast<sockaddr*>(&sock_addrs[0]);
  adapter_address->FirstUnicastAddress->Address.iSockaddrLength = sock_len;
  adapter_address->FirstUnicastAddress->OnLinkPrefixLength = 1;
  sock_len = sizeof(sockaddr_storage);
  if (!IPEndPoint(ip_netmask, 0)
           .ToSockAddr(reinterpret_cast<sockaddr*>(&sock_addrs[1]),
                       &sock_len)) {
    return false;
  }
  adapter_address->FirstPrefix->Address.lpSockaddr =
      reinterpret_cast<sockaddr*>(&sock_addrs[1]);
  adapter_address->FirstPrefix->Address.iSockaddrLength = sock_len;
  adapter_address->FirstPrefix->PrefixLength = 1;
  DCHECK_EQ(sock_addrs[0].ss_family, sock_addrs[1].ss_family);
  if (sock_addrs[0].ss_family == AF_INET6) {
    adapter_address->Ipv6IfIndex = 0;
  } else {
    DCHECK_EQ(sock_addrs[0].ss_family, AF_INET);
    adapter_address->IfIndex = 0;
  }
  return true;
}
bool FillAdapterAddress ( IP_ADAPTER_ADDRESSES * adapter_address , const char * ifname , const IPAddress & ip_address , const IPAddress & ip_netmask , sockaddr_storage sock_addrs [ 2 ] ) { adapter_address -> AdapterName = const_cast < char * > ( ifname ) ; adapter_address -> FriendlyName = const_cast < PWCHAR > ( L" " ) ; adapter_address -> IfType = IF_TYPE_ETHERNET_CSMACD ; adapter_address -> OperStatus = IfOperStatusUp ; adapter_address -> FirstUnicastAddress -> DadState = IpDadStatePreferred ; adapter_address -> FirstUnicastAddress -> PrefixOrigin = IpPrefixOriginOther ; adapter_address -> FirstUnicastAddress -> SuffixOrigin = IpSuffixOriginOther ; adapter_address -> FirstUnicastAddress -> PreferredLifetime = 100 ; adapter_address -> FirstUnicastAddress -> ValidLifetime = 1000 ; socklen_t sock_len = sizeof ( sockaddr_storage ) ; if ( ! IPEndPoint ( ip_address , 0 ) . ToSockAddr ( reinterpret_cast < sockaddr * > ( & sock_addrs [ 0 ] ) , & sock_len ) ) { return false ; } adapter_address -> FirstUnicastAddress -> Address . lpSockaddr = reinterpret_cast < sockaddr * > ( & sock_addrs [ 0 ] ) ; adapter_address -> FirstUnicastAddress -> Address . iSockaddrLength = sock_len ; adapter_address -> FirstUnicastAddress -> OnLinkPrefixLength = 1 ; sock_len = sizeof ( sockaddr_storage ) ; if ( ! IPEndPoint ( ip_netmask , 0 ) . ToSockAddr ( reinterpret_cast < sockaddr * > ( & sock_addrs [ 1 ] ) , & sock_len ) ) { return false ; } adapter_address -> FirstPrefix -> Address . lpSockaddr = reinterpret_cast < sockaddr * > ( & sock_addrs [ 1 ] ) ; adapter_address -> FirstPrefix -> Address . iSockaddrLength = sock_len ; adapter_address -> FirstPrefix -> PrefixLength = 1 ; DCHECK_EQ ( sock_addrs [ 0 ] . ss_family , sock_addrs [ 1 ] . ss_family ) ; if ( sock_addrs [ 0 ] . ss_family == AF_INET6 ) { adapter_address -> Ipv6IfIndex = 0 ; } else { DCHECK_EQ ( sock_addrs [ 0 ] . ss_family , AF_INET ) ; adapter_address -> IfIndex = 0 ; } return true ; }
Helper function to create a valid IP_ADAPTER_ADDRESSES with reasonable
default value.
Helper function to create a valid IP_ADAPTER_ADDRESSES with reasonable default value .
// Convert to sockaddr for next check.