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

     1  // Copyright 2018 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_NETLINK_UTIL_H_
    16  #define GVISOR_TEST_SYSCALLS_SOCKET_NETLINK_UTIL_H_
    17  
    18  #include <sys/socket.h>
    19  // socket.h has to be included before if_arp.h.
    20  #include <linux/if_arp.h>
    21  #include <linux/netlink.h>
    22  #include <linux/rtnetlink.h>
    23  
    24  #include "test/util/file_descriptor.h"
    25  #include "test/util/posix_error.h"
    26  
    27  namespace gvisor {
    28  namespace testing {
    29  
    30  // Returns a bound netlink socket.
    31  PosixErrorOr<FileDescriptor> NetlinkBoundSocket(int protocol);
    32  
    33  // Returns the port ID of the passed socket.
    34  PosixErrorOr<uint32_t> NetlinkPortID(int fd);
    35  
    36  // Send the passed request and call fn on all response netlink messages.
    37  //
    38  // To be used on requests with NLM_F_MULTI reponses.
    39  PosixError NetlinkRequestResponse(
    40      const FileDescriptor& fd, void* request, size_t len,
    41      const std::function<void(const struct nlmsghdr* hdr)>& fn,
    42      bool expect_nlmsgerr);
    43  
    44  // Call fn on all response netlink messages.
    45  //
    46  // To be used on requests with NLM_F_MULTI reponses.
    47  PosixError NetlinkResponse(
    48      const FileDescriptor& fd,
    49      const std::function<void(const struct nlmsghdr* hdr)>& fn,
    50      bool expect_nlmsgerr);
    51  
    52  // Send the passed request and call fn on all response netlink messages.
    53  //
    54  // To be used on requests without NLM_F_MULTI reponses.
    55  PosixError NetlinkRequestResponseSingle(
    56      const FileDescriptor& fd, void* request, size_t len,
    57      const std::function<void(const struct nlmsghdr* hdr)>& fn);
    58  
    59  // Send the passed request then expect and return an ack or error.
    60  PosixError NetlinkRequestAckOrError(const FileDescriptor& fd, uint32_t seq,
    61                                      void* request, size_t len);
    62  
    63  // Find rtnetlink attribute in message.
    64  const struct rtattr* FindRtAttr(const struct nlmsghdr* hdr,
    65                                  const struct ifinfomsg* msg, int16_t attr);
    66  
    67  }  // namespace testing
    68  }  // namespace gvisor
    69  
    70  #endif  // GVISOR_TEST_SYSCALLS_SOCKET_NETLINK_UTIL_H_