OffloadUtils - refactor out common code

Test: builds, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iad4b52f1d14ce0a4bf82f882ae773bcaa3598f8b
diff --git a/server/OffloadUtils.cpp b/server/OffloadUtils.cpp
index 715d840..c3b1d9e 100644
--- a/server/OffloadUtils.cpp
+++ b/server/OffloadUtils.cpp
@@ -93,14 +93,18 @@
 }
 
 // TODO: merge with //system/netd/server/SockDiag.cpp:checkError(fd)
-static int processNetlinkResponse(int fd) {
+static int sendAndProcessNetlinkResponse(int fd, const void* req, int len) {
+    int rv = send(fd, req, len, 0);
+    if (rv == -1) return -errno;
+    if (rv != len) return -EMSGSIZE;
+
     struct {
         nlmsghdr h;
         nlmsgerr e;
         char buf[256];
     } resp = {};
 
-    const int rv = recv(fd, &resp, sizeof(resp), MSG_TRUNC);
+    rv = recv(fd, &resp, sizeof(resp), MSG_TRUNC);
 
     if (rv == -1) {
         const int err = errno;
@@ -172,11 +176,7 @@
 #undef ASCIIZ_LEN_CLSACT
 #undef CLSACT
 
-    const int rv = send(fd, &req, sizeof(req), 0);
-    if (rv == -1) return -errno;
-    if (rv != sizeof(req)) return -EMSGSIZE;
-
-    return processNetlinkResponse(fd);
+    return sendAndProcessNetlinkResponse(fd, &req, sizeof(req));
 }
 
 // tc filter add dev .. in/egress prio 1 protocol ipv6/ip bpf object-pinned /sys/fs/bpf/...
@@ -351,11 +351,7 @@
 #undef ASCIIZ_LEN_BPF
 #undef BPF
 
-    const int rv = send(fd, &req, sizeof(req), 0);
-    if (rv == -1) return -errno;
-    if (rv != sizeof(req)) return -EMSGSIZE;
-
-    return processNetlinkResponse(fd);
+    return sendAndProcessNetlinkResponse(fd, &req, sizeof(req));
 }
 
 // tc filter del dev .. in/egress prio .. protocol ..
@@ -381,11 +377,7 @@
                     },
     };
 
-    const int rv = send(fd, &req, sizeof(req), 0);
-    if (rv == -1) return -errno;
-    if (rv != sizeof(req)) return -EMSGSIZE;
-
-    return processNetlinkResponse(fd);
+    return sendAndProcessNetlinkResponse(fd, &req, sizeof(req));
 }
 
 }  // namespace net