Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame^] | 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 | |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 17 | #include <functional> |
| 18 | |
| 19 | #include <linux/netlink.h> |
| 20 | #include <linux/sock_diag.h> |
| 21 | #include <linux/inet_diag.h> |
| 22 | |
| 23 | struct inet_diag_msg; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 24 | class SockDiagTest; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 25 | |
| 26 | class SockDiag { |
| 27 | |
| 28 | public: |
| 29 | static const int kBufferSize = 4096; |
| 30 | typedef std::function<int(uint8_t proto, const inet_diag_msg *)> DumpCallback; |
| 31 | |
| 32 | struct DestroyRequest { |
| 33 | nlmsghdr nlh; |
| 34 | inet_diag_req_v2 req; |
| 35 | } __attribute__((__packed__)); |
| 36 | |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 37 | SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {} |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 38 | bool open(); |
| 39 | virtual ~SockDiag() { closeSocks(); } |
| 40 | |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame^] | 41 | int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 42 | int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr); |
| 43 | int readDiagMsg(uint8_t proto, DumpCallback callback); |
| 44 | int sockDestroy(uint8_t proto, const inet_diag_msg *); |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 45 | int destroySockets(const char *addrstr); |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame^] | 46 | int destroySockets(uint8_t proto, uid_t uid); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | int mSock; |
| 50 | int mWriteSock; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 51 | int mSocketsDestroyed; |
Lorenzo Colitti | 94a7b43 | 2016-03-24 16:47:12 +0900 | [diff] [blame^] | 52 | int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states, iovec *iov, int iovcnt); |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame] | 53 | int destroySockets(uint8_t proto, int family, const char *addrstr); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 54 | bool hasSocks() { return mSock != -1 && mWriteSock != -1; } |
| 55 | void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; } |
| 56 | }; |