github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/socket_ip_udp_unbound_external_networking.cc (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #include "test/syscalls/linux/socket_ip_udp_unbound_external_networking.h"
    16  
    17  #include "test/syscalls/linux/socket_test_util.h"
    18  #include "test/util/test_util.h"
    19  
    20  namespace gvisor {
    21  namespace testing {
    22  
    23  void IPUDPUnboundExternalNetworkingSocketTest::SetUp() {
    24    // FIXME(b/137899561): Linux instance for syscall tests sometimes misses its
    25    // IPv4 address on eth0.
    26    found_net_interfaces_ = false;
    27  
    28    // Get interface list.
    29    ASSERT_NO_ERRNO(if_helper_.Load());
    30    std::vector<std::string> if_names = if_helper_.InterfaceList(AF_INET);
    31    if (if_names.size() != 2) {
    32      return;
    33    }
    34  
    35    // Figure out which interface is where.
    36    std::string lo = if_names[0];
    37    std::string eth = if_names[1];
    38    if (lo != "lo") std::swap(lo, eth);
    39    if (lo != "lo") return;
    40  
    41    lo_if_idx_ = ASSERT_NO_ERRNO_AND_VALUE(if_helper_.GetIndex(lo));
    42    auto lo_if_addr = if_helper_.GetAddr(AF_INET, lo);
    43    if (lo_if_addr == nullptr) {
    44      return;
    45    }
    46    lo_if_addr_ = *reinterpret_cast<const sockaddr_in*>(lo_if_addr);
    47  
    48    eth_if_idx_ = ASSERT_NO_ERRNO_AND_VALUE(if_helper_.GetIndex(eth));
    49    auto eth_if_addr = if_helper_.GetAddr(AF_INET, eth);
    50    if (eth_if_addr == nullptr) {
    51      return;
    52    }
    53    eth_if_addr_ = *reinterpret_cast<const sockaddr_in*>(eth_if_addr);
    54  
    55    found_net_interfaces_ = true;
    56  }
    57  
    58  }  // namespace testing
    59  }  // namespace gvisor