Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef NETD_SERVER_NETLINK_UTIL_H |
| 18 | #define NETD_SERVER_NETLINK_UTIL_H |
| 19 | |
| 20 | #include <functional> |
| 21 | |
| 22 | #include "NetdConstants.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace net { |
| 26 | |
| 27 | const sockaddr_nl KERNEL_NLADDR = {AF_NETLINK, 0, 0, 0}; |
| 28 | |
| 29 | // Generic code for processing netlink dumps. |
| 30 | const int kNetlinkDumpBufferSize = 8192; |
| 31 | typedef std::function<void(nlmsghdr *)> NetlinkDumpCallback; |
| 32 | |
| 33 | // Opens an RTNetlink socket and connects it to the kernel. |
| 34 | WARN_UNUSED_RESULT int openRtNetlinkSocket(); |
| 35 | |
| 36 | // Receives a netlink ACK. Returns 0 if the command succeeded or negative errno if the command |
| 37 | // failed or receiving the ACK failed. |
| 38 | WARN_UNUSED_RESULT int recvNetlinkAck(int sock); |
| 39 | |
| 40 | // Sends a netlink request and possibly expects an ACK. The first element of iov should be null and |
| 41 | // will be set to the netlink message headerheader. The subsequent elements are the contents of the |
| 42 | // request. |
| 43 | WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen); |
| 44 | |
| 45 | // Disable optimizations in ASan build. |
| 46 | // ASan reports an out-of-bounds 32-bit(!) access in the first loop of the |
| 47 | // function (over iov[]). |
| 48 | #ifdef __clang__ |
| 49 | #if __has_feature(address_sanitizer) |
| 50 | __attribute__((optnone)) |
| 51 | #endif |
| 52 | #endif |
| 53 | WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen, |
| 54 | const NetlinkDumpCallback& callback); |
| 55 | |
| 56 | // Processes a netlink dump, passing every message to the specified |callback|. |
| 57 | WARN_UNUSED_RESULT int processNetlinkDump(int sock, const NetlinkDumpCallback& callback); |
| 58 | |
| 59 | } // namespace net |
| 60 | } // namespace android |
| 61 | |
| 62 | #endif // NETD_SERVER_NETLINK_UTIL_H |