Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 1 | #include <functional> |
| 2 | |
| 3 | #include <linux/netlink.h> |
| 4 | #include <linux/sock_diag.h> |
| 5 | #include <linux/inet_diag.h> |
| 6 | |
| 7 | struct inet_diag_msg; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame^] | 8 | class SockDiagTest; |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 9 | |
| 10 | class SockDiag { |
| 11 | |
| 12 | public: |
| 13 | static const int kBufferSize = 4096; |
| 14 | typedef std::function<int(uint8_t proto, const inet_diag_msg *)> DumpCallback; |
| 15 | |
| 16 | struct DestroyRequest { |
| 17 | nlmsghdr nlh; |
| 18 | inet_diag_req_v2 req; |
| 19 | } __attribute__((__packed__)); |
| 20 | |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame^] | 21 | SockDiag() : mSock(-1), mWriteSock(-1), mSocketsDestroyed(0) {} |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 22 | bool open(); |
| 23 | virtual ~SockDiag() { closeSocks(); } |
| 24 | |
| 25 | int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr); |
| 26 | int readDiagMsg(uint8_t proto, DumpCallback callback); |
| 27 | int sockDestroy(uint8_t proto, const inet_diag_msg *); |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame^] | 28 | int destroySockets(const char *addrstr); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 29 | |
| 30 | private: |
| 31 | int mSock; |
| 32 | int mWriteSock; |
Lorenzo Colitti | f32fc59 | 2016-02-15 01:09:14 +0900 | [diff] [blame^] | 33 | int mSocketsDestroyed; |
| 34 | int destroySockets(uint8_t proto, int family, const char *addrstr); |
Lorenzo Colitti | 8464e1e | 2016-02-05 00:57:26 +0900 | [diff] [blame] | 35 | bool hasSocks() { return mSock != -1 && mWriteSock != -1; } |
| 36 | void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; } |
| 37 | }; |