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; |
| 8 | |
| 9 | class SockDiag { |
| 10 | |
| 11 | public: |
| 12 | static const int kBufferSize = 4096; |
| 13 | typedef std::function<int(uint8_t proto, const inet_diag_msg *)> DumpCallback; |
| 14 | |
| 15 | struct DestroyRequest { |
| 16 | nlmsghdr nlh; |
| 17 | inet_diag_req_v2 req; |
| 18 | } __attribute__((__packed__)); |
| 19 | |
| 20 | SockDiag() : mSock(-1), mWriteSock(-1) {} |
| 21 | bool open(); |
| 22 | virtual ~SockDiag() { closeSocks(); } |
| 23 | |
| 24 | int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr); |
| 25 | int readDiagMsg(uint8_t proto, DumpCallback callback); |
| 26 | int sockDestroy(uint8_t proto, const inet_diag_msg *); |
| 27 | |
| 28 | private: |
| 29 | int mSock; |
| 30 | int mWriteSock; |
| 31 | bool hasSocks() { return mSock != -1 && mWriteSock != -1; } |
| 32 | void closeSocks() { close(mSock); close(mWriteSock); mSock = mWriteSock = -1; } |
| 33 | }; |