Make translate_packet take a fd instead of a tun header.

This will make it easier to use separate fds for reading and
writing in a future change.

(cherry picked from commit 91d0f1bc6dd24e54ed3caef9b08525b332ab0adf)

Bug: 15340961
Change-Id: I374b85f28ae570dc82d21e1ea82a2f072fd7fba3
diff --git a/clatd.c b/clatd.c
index d8a9e72..af54ff2 100644
--- a/clatd.c
+++ b/clatd.c
@@ -306,7 +306,8 @@
   ssize_t readlen;
   uint8_t packet[PACKETLEN];
 
-  // in case something ignores the packet length
+  // In case something ignores the packet length.
+  // TODO: remove it.
   memset(packet, 0, PACKETLEN);
 
   readlen = read(active_fd,packet,PACKETLEN);
@@ -325,7 +326,23 @@
       return;
     }
 
-    translate_packet(tunnel, (struct tun_pi *) packet, packet + header_size, readlen - header_size);
+    struct tun_pi *tun_header = (struct tun_pi *) packet;
+    if(tun_header->flags != 0) {
+      logmsg(ANDROID_LOG_WARN, "%s: unexpected flags = %d", __func__, tun_header->flags);
+    }
+
+    int fd;
+    uint16_t proto = ntohs(tun_header->proto);
+    if (proto == ETH_P_IP) {
+      fd = tunnel->fd6;
+    } else if (proto == ETH_P_IPV6) {
+      fd = tunnel->fd4;
+    } else {
+      logmsg(ANDROID_LOG_WARN, "%s: unknown packet type = 0x%x", __func__, proto);
+      return;
+    }
+
+    translate_packet(fd, (proto == ETH_P_IP), packet + header_size, readlen - header_size);
   }
 }
 
diff --git a/clatd_test.cpp b/clatd_test.cpp
index 5172d3f..bc32a84 100644
--- a/clatd_test.cpp
+++ b/clatd_test.cpp
@@ -424,17 +424,13 @@
   if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, fds)) {
     abort();
   }
-  struct tun_data tunnel = {
-    "clat", "clat4",
-    fds[0], fds[1]
-  };
   struct tun_pi tun_header = { 0, 0 };
 
   char foo[512];
   snprintf(foo, sizeof(foo), "%s: Invalid original packet", msg);
   check_packet(original, original_len, foo);
 
-  int read_fd;
+  int read_fd, write_fd;
   uint16_t expected_proto;
   int version = ip_version(original);
   switch (version) {
@@ -442,18 +438,20 @@
       tun_header.proto = htons(ETH_P_IP);
       expected_proto = htons(ETH_P_IPV6);
       read_fd = fds[1];
+      write_fd = fds[0];
       break;
     case 6:
       tun_header.proto = htons(ETH_P_IPV6);
       expected_proto = htons(ETH_P_IP);
       read_fd = fds[0];
+      write_fd = fds[1];
       break;
     default:
       FAIL() << msg << ": Unsupported IP version " << version << "\n";
       break;
   }
 
-  translate_packet(&tunnel, &tun_header, original, original_len);
+  translate_packet(write_fd, (version == 4), original, original_len);
 
   struct tun_pi new_tun_header;
   struct iovec iov[] = {
diff --git a/translate.c b/translate.c
index e93a93a..958ce2f 100644
--- a/translate.c
+++ b/translate.c
@@ -466,15 +466,13 @@
 }
 
 /* function: translate_packet
- * takes a tun header and a packet and sends it down the stack
- * tunnel     - tun device data
- * tun_header - tun header
+ * takes a packet, translates it, and writes it to fd
+ * fd         - fd to write translated packet to
+ * to_ipv6    - true if translating to ipv6, false if translating to ipv4
  * packet     - packet
  * packetsize - size of packet
  */
-void translate_packet(const struct tun_data *tunnel, struct tun_pi *tun_header,
-                      const uint8_t *packet, size_t packetsize) {
-  int fd;
+void translate_packet(int fd, int to_ipv6, const uint8_t *packet, size_t packetsize) {
   int iov_len = 0;
 
   // Allocate buffers for all packet headers.
@@ -498,23 +496,14 @@
     { NULL, 0 },                      // Payload. No buffer, it's a pointer to the original payload.
   };
 
-  if(tun_header->flags != 0) {
-    logmsg(ANDROID_LOG_WARN, "translate_packet: unexpected flags = %d", tun_header->flags);
-  }
-
-  if(ntohs(tun_header->proto) == ETH_P_IP) {
-    fd = tunnel->fd6;
-    fill_tun_header(&tun_targ, ETH_P_IPV6);
+  if (to_ipv6) {
     iov_len = ipv4_packet(out, CLAT_POS_IPHDR, packet, packetsize);
-  } else if(ntohs(tun_header->proto) == ETH_P_IPV6) {
-    fd = tunnel->fd4;
-    fill_tun_header(&tun_targ, ETH_P_IP);
-    iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
   } else {
-    logmsg(ANDROID_LOG_WARN, "translate_packet: unknown packet type = %x",tun_header->proto);
+    iov_len = ipv6_packet(out, CLAT_POS_IPHDR, packet, packetsize);
   }
 
   if (iov_len > 0) {
+    fill_tun_header(&tun_targ, to_ipv6 ? ETH_P_IPV6 : ETH_P_IP);
     writev(fd, out, iov_len);
   }
 }
diff --git a/translate.h b/translate.h
index 6d4f126..46e178b 100644
--- a/translate.h
+++ b/translate.h
@@ -60,8 +60,7 @@
                      const struct iphdr *old_header);
 
 // Translate and send packets.
-void translate_packet(const struct tun_data *tunnel, struct tun_pi *tun_header,
-                      const uint8_t *packet, size_t packetsize);
+void translate_packet(int fd, int to_ipv6, const uint8_t *packet, size_t packetsize);
 
 // Translate IPv4 and IPv6 packets.
 int ipv4_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len);