TCP socket monitoring: minimum SockDiag primitives

This patch adds a getSocketsInfo() method on the SockDiag class for
dumping the struct tcp_info of all IPv4 and IPv6 sockets on the system,
using the sock_diag netlink interface.

Bug: 64147860
Test: - manual tests usint dumpsys command from follow-up patch
      - TODO: netd unit tests
Change-Id: I3ad1726b4fde005c7b9506af96ed7bd79f527316
diff --git a/server/SockDiag.h b/server/SockDiag.h
index 7af8152..c95aa8c 100644
--- a/server/SockDiag.h
+++ b/server/SockDiag.h
@@ -32,6 +32,7 @@
 #include "UidRanges.h"
 
 struct inet_diag_msg;
+struct tcp_info;
 class SockDiagTest;
 
 namespace android {
@@ -42,10 +43,14 @@
   public:
     static const int kBufferSize = 4096;
 
-    // Callback function that is called once for every socket in the dump. A return value of true
-    // means destroy the socket.
+    // Callback function that is called once for every socket in the sockDestroy dump.
+    // A return value of true means destroy the socket.
     typedef std::function<bool(uint8_t proto, const inet_diag_msg *)> DestroyFilter;
 
+    // Callback function that is called once for every socket in the sockInfo dump.
+    typedef std::function<void(const struct inet_diag_msg *, const struct tcp_info *)>
+            TcpInfoReader;
+
     struct DestroyRequest {
         nlmsghdr nlh;
         inet_diag_req_v2 req;
@@ -58,6 +63,7 @@
     int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states);
     int sendDumpRequest(uint8_t proto, uint8_t family, const char *addrstr);
     int readDiagMsg(uint8_t proto, const DestroyFilter& callback);
+    int readDiagMsgWithTcpInfo(const TcpInfoReader& callback);
 
     int sockDestroy(uint8_t proto, const inet_diag_msg *);
     // Destroys all sockets on the given IPv4 or IPv6 address.
@@ -72,12 +78,16 @@
     int destroySocketsLackingPermission(unsigned netId, Permission permission,
                                         bool excludeLoopback);
 
+    // Dump struct tcp_info for all "live" (CONNECTED, SYN_SENT, SYN_RECV) TCP sockets.
+    int getLiveTcpInfos(const TcpInfoReader& sockInfoReader);
+
   private:
     friend class SockDiagTest;
     int mSock;
     int mWriteSock;
     int mSocketsDestroyed;
-    int sendDumpRequest(uint8_t proto, uint8_t family, uint32_t states, iovec *iov, int iovcnt);
+    int sendDumpRequest(uint8_t proto, uint8_t family, uint8_t extensions, uint32_t states,
+                        iovec *iov, int iovcnt);
     int destroySockets(uint8_t proto, int family, const char *addrstr);
     int destroyLiveSockets(DestroyFilter destroy, const char *what, iovec *iov, int iovcnt);
     bool hasSocks() { return mSock != -1 && mWriteSock != -1; }