blob: 81d6224e7f7e88a83d6328775cf7a7cdcc397cd7 [file] [log] [blame]
Tomasz Wasilczyk87329672019-07-12 11:43:00 -07001/*
2 * Copyright (C) 2019 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#pragma once
18
19#include "NetlinkRequest.h"
20
21#include <android-base/macros.h>
22#include <android-base/unique_fd.h>
23
24#include <linux/netlink.h>
25
26namespace android {
27namespace netdevice {
28
29/**
30 * A wrapper around AF_NETLINK sockets.
31 *
32 * This class is not thread safe to use a single instance between multiple threads, but it's fine to
33 * use multiple instances over multiple threads.
34 */
35struct NetlinkSocket {
36 NetlinkSocket(int protocol);
37
38 /**
39 * Send Netlink message to Kernel.
40 *
41 * @param msg Message to send, nlmsg_seq will be set to next sequence number
42 * @return true, if succeeded
43 */
44 template <class T, unsigned int BUFSIZE>
45 bool send(NetlinkRequest<T, BUFSIZE>& req) {
46 if (!req.isGood()) return false;
47 return send(req.header());
48 }
49
50 /**
51 * Receive Netlink ACK message from Kernel.
52 *
53 * @return true if received ACK message, false in case of error
54 */
55 bool receiveAck();
56
57 private:
58 uint32_t mSeq = 0;
59 base::unique_fd mFd;
60 bool mFailed = false;
61
62 bool send(struct nlmsghdr* msg);
63
64 DISALLOW_COPY_AND_ASSIGN(NetlinkSocket);
65};
66
67} // namespace netdevice
68} // namespace android