Use unique_fd instead of int for sockets

Also refactor socket closing method.

Bug: 135717624
Test: atest
Change-Id: Iedbaf2521c4453195708114d7892ddc2cbe22211
diff --git a/res_init.cpp b/res_init.cpp
index 16fd98c..049e225 100644
--- a/res_init.cpp
+++ b/res_init.cpp
@@ -94,23 +94,11 @@
 
 void res_init(ResState* statp, const struct android_net_context* _Nonnull netcontext,
               android::net::NetworkDnsEventReported* _Nonnull event) {
-    memset(statp, 0, sizeof *statp);
-
     statp->netid = netcontext->dns_netid;
     statp->uid = netcontext->uid;
     statp->pid = netcontext->pid;
+    statp->nscount = 1;
     statp->id = arc4random_uniform(65536);
-    statp->_mark = netcontext->dns_mark;
-    statp->netcontext_flags = netcontext->flags;
-    statp->event = event;
-
-    statp->ndots = 1;
-    statp->_vcsock = -1;
-
-    for (int ns = 0; ns < MAXNS; ns++) {
-        statp->nssocks[ns] = -1;
-    }
-
     // The following dummy initialization is probably useless because
     // it's overwritten later by resolv_populate_res_for_net().
     // TODO: check if it's safe to remove.
@@ -120,28 +108,13 @@
             .sin.sin_port = htons(NAMESERVER_PORT),
     };
     memcpy(&statp->nsaddrs, &u, sizeof(u));
-    statp->nscount = 1;
-}
 
-/*
- * This routine is for closing the socket if a virtual circuit is used and
- * the program wants to close it.  This provides support for endhostent()
- * which expects to close the socket.
- *
- * This routine is not expected to be user visible.
- */
-void res_nclose(res_state statp) {
-    int ns;
-
-    if (statp->_vcsock >= 0) {
-        (void) close(statp->_vcsock);
-        statp->_vcsock = -1;
-        statp->_flags &= ~RES_F_VC;
+    for (auto& sock : statp->nssocks) {
+        sock.reset();
     }
-    for (ns = 0; ns < MAXNS; ns++) {
-        if (statp->nssocks[ns] != -1) {
-            close(statp->nssocks[ns]);
-            statp->nssocks[ns] = -1;
-        }
-    }
+    statp->ndots = 1;
+    statp->_mark = netcontext->dns_mark;
+    statp->tcp_nssock.reset();
+    statp->event = event;
+    statp->netcontext_flags = netcontext->flags;
 }