OSDN Git Service

DO NOT MERGE Benchmarks for network metrics reporting
[android-x86/system-netd.git] / server / SockDiag.h
1 /*
2  * Copyright (C) 2016 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 _SOCK_DIAG_H
18 #define _SOCK_DIAG_H
19
20 #include <unistd.h>
21 #include <sys/socket.h>
22
23 #include <linux/netlink.h>
24 #include <linux/sock_diag.h>
25 #include <linux/inet_diag.h>
26
27 #include <functional>
28 #include <set>
29
30 #include "Permission.h"
31 #include "UidRanges.h"
32
33 struct inet_diag_msg;
34 class SockDiagTest;
35
36 class SockDiag {
37
38   public:
39     static const int kBufferSize = 4096;
40
41     // Callback function that is called once for every socket in the dump. A return value of true
42     // means destroy the socket.
43     typedef std::function<bool(uint8_t proto, const inet_diag_msg *)> DumpCallback;
44
45     struct DestroyRequest {
46         nlmsghdr nlh;
47         inet_diag_req_v2 req;
48     } __attribute__((__packed__));
49
50     SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {}
51     bool open();
52     virtual ~SockDiag() { closeSocks(); }
53
54     int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states);
55     int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr);
56     int readDiagMsg(uint8_t proto, DumpCallback callback);
57     int sockDestroy(uint8_t proto, const inet_diag_msg *);
58     // Destroys all sockets on the given IPv4 or IPv6 address.
59     int destroySockets(const char *addrstr);
60     // Destroys all sockets for the given protocol and UID.
61     int destroySockets(uint8_t proto, uid_t uid, bool excludeLoopback);
62     // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets for the given UID ranges.
63     int destroySockets(const UidRanges& uidRanges, const std::set<uid_t>& skipUids,
64                        bool excludeLoopback);
65     // Destroys all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets that no longer have
66     // the permissions required by the specified network.
67     int destroySocketsLackingPermission(unsigned netId, Permission permission,
68                                         bool excludeLoopback);
69
70   private:
71     friend class SockDiagTest;
72     int mSock;
73     int mWriteSock;
74     int mSocketsDestroyed;
75     int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states, iovec *iov, int iovcnt);
76     int destroySockets(uint8_t proto, int family, const char *addrstr);
77     int destroyLiveSockets(DumpCallback destroy, const char *what, iovec *iov, int iovcnt);
78     bool hasSocks() { return mSock != -1 && mWriteSock != -1; }
79     void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; }
80     static bool isLoopbackSocket(const inet_diag_msg *msg);
81 };
82
83 #endif  // _SOCK_DIAG_H