gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/socket_bind_to_device_util.h (about)

     1  // Copyright 2019 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  #ifndef GVISOR_TEST_SYSCALLS_SOCKET_BIND_TO_DEVICE_UTILS_H_
    16  #define GVISOR_TEST_SYSCALLS_SOCKET_BIND_TO_DEVICE_UTILS_H_
    17  
    18  #include <arpa/inet.h>
    19  #include <linux/if_tun.h>
    20  #include <net/if.h>
    21  #include <netinet/in.h>
    22  #include <sys/ioctl.h>
    23  #include <sys/socket.h>
    24  #include <sys/types.h>
    25  #include <sys/un.h>
    26  #include <unistd.h>
    27  
    28  #include <cstdio>
    29  #include <cstring>
    30  #include <map>
    31  #include <memory>
    32  #include <string>
    33  #include <unordered_map>
    34  #include <unordered_set>
    35  #include <utility>
    36  #include <vector>
    37  
    38  #include "absl/memory/memory.h"
    39  #include "test/util/test_util.h"
    40  
    41  namespace gvisor {
    42  namespace testing {
    43  
    44  class Tunnel {
    45   public:
    46    static PosixErrorOr<std::unique_ptr<Tunnel>> New(
    47        std::string tunnel_name = "");
    48    const std::string& GetName() const { return name_; }
    49  
    50    ~Tunnel() {
    51      if (fd_ != -1) {
    52        close(fd_);
    53      }
    54    }
    55  
    56   private:
    57    Tunnel(int fd) : fd_(fd) {}
    58    int fd_ = -1;
    59    std::string name_;
    60  };
    61  
    62  std::unordered_set<std::string> GetInterfaceNames();
    63  
    64  }  // namespace testing
    65  }  // namespace gvisor
    66  
    67  #endif  // GVISOR_TEST_SYSCALLS_SOCKET_BIND_TO_DEVICE_UTILS_H_