dns cache per interface iteration 2

name server addresses are read from the dns
cache associated wih the interface on which
the request shall be done.

processes which has requested to issue dns request
using specific interface are now proxied to netd.

added methods to attach/detach a process to a specific
dns cache/interface.

added getaddrinfoforinface method which takes an
interface as an argument.

bug:4815099
bug:5465296
Change-Id: I7a8fe1980cdf99d4d296ddc5c6411f0c72162263
diff --git a/libc/private/resolv_cache.h b/libc/private/resolv_cache.h
index 1dcc53f..4f32fb7 100644
--- a/libc/private/resolv_cache.h
+++ b/libc/private/resolv_cache.h
@@ -28,13 +28,13 @@
 #ifndef _RESOLV_CACHE_H_
 #define _RESOLV_CACHE_H_
 
+struct __res_state;
 struct resolv_cache;  /* forward */
 
-/* gets the cache for the default interface. Might be NULL*/
-extern struct resolv_cache*  __get_res_cache(void);
-
-/* get the cache for a specified interface. Can be NULL*/
-extern struct resolv_cache* __get_res_cache_for_iface(const char* ifname);
+/* gets the cache for an interface. Set ifname argument to NULL or
+ * empty buffer ('\0') to get cache for default interface.
+ * returned cache might be NULL*/
+extern struct resolv_cache*  __get_res_cache(const char* ifname);
 
 /* this gets called everytime we detect some changes in the DNS configuration
  * and will flush the cache */
@@ -67,8 +67,14 @@
 /* gets the address associated with the specified interface */
 extern struct in_addr* _resolv_get_addr_of_iface(const char* ifname);
 
-/* Get name of default interface */
-extern char* _resolv_get_default_iface();
+/* Copy the name of the default interface to provided buffer.
+ * Return length of buffer on success on failure -1 is returned */
+extern int _resolv_get_default_iface(char* buff, int buffLen);
+
+/* sets the name server addresses to the provided res_state structure. The
+ * name servers are retrieved from the cache which is associated
+ * with the interface to which the res_state structure is associated */
+extern int _resolv_populate_res_for_iface(struct __res_state* statp);
 
 typedef enum {
     RESOLV_CACHE_UNSUPPORTED,  /* the cache can't handle that kind of queries */
diff --git a/libc/private/resolv_iface.h b/libc/private/resolv_iface.h
index 3fe586e..bf5abad 100644
--- a/libc/private/resolv_iface.h
+++ b/libc/private/resolv_iface.h
@@ -60,6 +60,21 @@
 /* flush the cache associated with a certain interface */
 extern void _resolv_flush_cache_for_iface(const char* ifname);
 
+/* set a pid to use the name servers of the specified interface */
+extern void _resolv_set_iface_for_pid(const char* ifname, int pid);
+
+/* clear pid from being associated with an interface */
+extern void _resolv_clear_iface_for_pid(int pid);
+
+/** Gets the name of the interface to which the pid is attached.
+ *  On error, -1 is returned.
+ *  If no interface is found, 0 is returned and buff is set to empty ('\0').
+ *  If an interface is found, the name is copied to buff and the length of the name is returned.
+ *  Arguments:   pid The pid to find an interface for
+ *               buff A buffer to copy the result to
+ *               buffLen Length of buff. An interface is at most IF_NAMESIZE in length */
+extern int _resolv_get_pids_associated_interface(int pid, char* buff, int buffLen);
+
 #endif /* _BIONIC_RESOLV_IFACE_FUNCTIONS_DECLARED */
 
 __END_DECLS
diff --git a/libc/private/resolv_private.h b/libc/private/resolv_private.h
index 0f3c6c0..1c3c1a2 100644
--- a/libc/private/resolv_private.h
+++ b/libc/private/resolv_private.h
@@ -56,6 +56,7 @@
 
 #include <resolv.h>
 #include "resolv_static.h"
+#include <net/if.h>
 
 /*
  * Revision information.  This is the release date in YYYYMMDD format.
@@ -139,6 +140,7 @@
 struct __res_state_ext;
 
 struct __res_state {
+	char	iface[IF_NAMESIZE+1];
 	int	retrans;	 	/* retransmission time interval */
 	int	retry;			/* number of times to retransmit */
 #ifdef sun
@@ -491,7 +493,7 @@
 int		res_getservers(res_state,
 				    union res_sockaddr_union *, int);
 
-int res_get_dns_changed();
+void res_setiface();
 u_int  res_randomid(void);
 
 __END_DECLS