Limit C linkage to public symbols

Switched all internal functions to C++ linkage. This makes it harder to
accidentally call a function with a mismatched signature, and paves the
way to using C++ data structures internally.

This is the current list of symbols expoted by libnetd_resolv.so:
  _resolv_delete_cache_for_net
  _resolv_flush_cache_for_net
  _resolv_set_nameservers_for_net
  android_getaddrinfofornet
  android_getaddrinfofornetcontext
  android_gethostbyaddrfornet
  android_gethostbyaddrfornetcontext
  android_gethostbynamefornet
  android_gethostbynamefornetcontext
  android_net_res_stats_aggregate
  android_net_res_stats_get_info_for_net
  android_net_res_stats_get_usable_servers

A mass-renaming pass would improve consistency. Perhaps we could use
the prefix "netd_resolv_", or just "resolv_" for brevity. Once we begin
shipping netd binaries using this interface, we might have to live with
it for some time.

Test: atest netd_integration_test (after flashing with new netd binary)
Change-Id: I52f32add73fd908ad4a715ef8f8aff1f8d9733d0
diff --git a/resolv/getaddrinfo.cpp b/resolv/getaddrinfo.cpp
index d3e8ce0..8f287d0 100644
--- a/resolv/getaddrinfo.cpp
+++ b/resolv/getaddrinfo.cpp
@@ -1012,11 +1012,11 @@
         }
         cp += n; /* name */
         BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
-        type = _getshort(cp);
+        type = ns_get16(cp);
         cp += INT16SZ; /* type */
-        int cl = _getshort(cp);
+        int cl = ns_get16(cp);
         cp += INT16SZ + INT32SZ; /* class, TTL */
-        n = _getshort(cp);
+        n = ns_get16(cp);
         cp += INT16SZ; /* len */
         BOUNDS_CHECK(cp, n);
         if (cl != C_IN) {
diff --git a/resolv/gethnamaddr.cpp b/resolv/gethnamaddr.cpp
index 5c4874d..3fd6324 100644
--- a/resolv/gethnamaddr.cpp
+++ b/resolv/gethnamaddr.cpp
@@ -272,11 +272,11 @@
         }
         cp += n; /* name */
         BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
-        int type = _getshort(cp);
+        int type = ns_get16(cp);
         cp += INT16SZ; /* type */
-        int cl = _getshort(cp);
+        int cl = ns_get16(cp);
         cp += INT16SZ + INT32SZ; /* class, TTL */
-        n = _getshort(cp);
+        n = ns_get16(cp);
         cp += INT16SZ; /* len */
         BOUNDS_CHECK(cp, n);
         erdata = cp + n;
diff --git a/resolv/res_mkquery.cpp b/resolv/res_mkquery.cpp
index bda6ad9..82c42d5 100644
--- a/resolv/res_mkquery.cpp
+++ b/resolv/res_mkquery.cpp
@@ -85,30 +85,26 @@
 #define DEBUG
 #endif
 
-#define UNUSED(a) (void) &a
-
 extern const char* _res_opcodes[];
 
 /*
  * Form all types of queries.
  * Returns the size of the result or -1.
  */
-int res_nmkquery(res_state statp, int op, /* opcode of query */
-                 const char* dname,       /* domain name */
-                 int cl, int type,        /* class and type of query */
-                 const u_char* data,      /* resource record data */
-                 int datalen,             /* length of data */
-                 const u_char* newrr_in,  /* new rr for modify or append */
-                 u_char* buf,             /* buffer to put query */
-                 int buflen)              /* size of buffer */
+int res_nmkquery(res_state statp, int op,    /* opcode of query */
+                 const char* dname,          /* domain name */
+                 int cl, int type,           /* class and type of query */
+                 const u_char* data,         /* resource record data */
+                 int datalen,                /* length of data */
+                 const u_char* /*newrr_in*/, /* new rr for modify or append */
+                 u_char* buf,                /* buffer to put query */
+                 int buflen)                 /* size of buffer */
 {
     HEADER* hp;
     u_char *cp, *ep;
     int n;
     u_char *dnptrs[20], **dpp, **lastdnptr;
 
-    UNUSED(newrr_in);
-
 #ifdef DEBUG
     if (statp->options & RES_DEBUG)
         printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(cl),
diff --git a/resolv/res_stats.cpp b/resolv/res_stats.cpp
index e2cfd17..e6c2b65 100644
--- a/resolv/res_stats.cpp
+++ b/resolv/res_stats.cpp
@@ -33,16 +33,16 @@
               "Do not enable in release builds.");
 #endif
 
-/* Calculate the round-trip-time from start time t0 and end time t1. */
-int _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0) {
+// Calculate the round-trip-time from start time t0 and end time t1.
+int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0) {
     // Divide ns by one million to get ms, multiply s by thousand to get ms (obvious)
     long ms0 = t0->tv_sec * 1000 + t0->tv_nsec / 1000000;
     long ms1 = t1->tv_sec * 1000 + t1->tv_nsec / 1000000;
     return (int) (ms1 - ms0);
 }
 
-/* Create a sample for calculating server reachability statistics. */
-void _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt) {
+// Create a sample for calculating server reachability statistics.
+void _res_stats_set_sample(__res_sample* sample, time_t now, int rcode, int rtt) {
     VLOG << __func__ << ": rcode = " << rcode << ", sec = " << rtt;
     sample->at = now;
     sample->rcode = rcode;
@@ -116,7 +116,10 @@
     *last_sample_time = last;
 }
 
-bool _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats) {
+// Returns true if the server is considered unusable, i.e. if the success rate is not lower than the
+// threshold for the stored stored samples. If not enough samples are stored, the server is
+// considered usable.
+static bool res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats) {
     int successes = -1;
     int errors = -1;
     int timeouts = -1;
@@ -160,7 +163,7 @@
                                               bool usable_servers[]) {
     unsigned usable_servers_found = 0;
     for (int ns = 0; ns < nscount; ns++) {
-        bool usable = _res_stats_usable_server(params, &stats[ns]);
+        bool usable = res_stats_usable_server(params, &stats[ns]);
         if (usable) {
             ++usable_servers_found;
         }
diff --git a/resolv/resolv_netid.h b/resolv/resolv_netid.h
index f410ed1..aa4757f 100644
--- a/resolv/resolv_netid.h
+++ b/resolv/resolv_netid.h
@@ -48,12 +48,9 @@
  */
 #define MARK_UNSET 0u
 
-__BEGIN_DECLS
-
 struct __res_params;
 struct addrinfo;
-
-#define __used_in_netd __attribute__((visibility("default")))
+struct hostent;
 
 /*
  * A struct to capture context relevant to network operations.
@@ -81,34 +78,33 @@
 #define NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS 0x00000001
 #define NET_CONTEXT_FLAG_USE_EDNS 0x00000002
 
-struct hostent* android_gethostbyaddrfornet(const void*, socklen_t, int, unsigned,
-                                            unsigned) __used_in_netd;
-struct hostent* android_gethostbynamefornet(const char*, int, unsigned, unsigned) __used_in_netd;
-int android_getaddrinfofornet(const char*, const char*, const struct addrinfo*, unsigned, unsigned,
-                              struct addrinfo**) __used_in_netd;
+LIBNETD_RESOLV_PUBLIC hostent* android_gethostbyaddrfornet(const void*, socklen_t, int, unsigned,
+                                                           unsigned);
+LIBNETD_RESOLV_PUBLIC hostent* android_gethostbynamefornet(const char*, int, unsigned, unsigned);
+LIBNETD_RESOLV_PUBLIC int android_getaddrinfofornet(const char*, const char*,
+                                                    const addrinfo*, unsigned, unsigned,
+                                                    addrinfo**);
 /*
  * TODO: consider refactoring android_getaddrinfo_proxy() to serve as an
  * explore_fqdn() dispatch table method, with the below function only making DNS calls.
  */
-struct hostent* android_gethostbyaddrfornetcontext(
-        const void*, socklen_t, int, const struct android_net_context*) __used_in_netd;
-struct hostent* android_gethostbynamefornetcontext(
-        const char*, int, const struct android_net_context*) __used_in_netd;
-int android_getaddrinfofornetcontext(const char*, const char*, const struct addrinfo*,
-                                     const struct android_net_context*,
-                                     struct addrinfo**) __used_in_netd;
+LIBNETD_RESOLV_PUBLIC hostent* android_gethostbyaddrfornetcontext(const void*, socklen_t, int,
+                                                                  const android_net_context*);
+LIBNETD_RESOLV_PUBLIC hostent* android_gethostbynamefornetcontext(const char*, int,
+                                                                  const android_net_context*);
+LIBNETD_RESOLV_PUBLIC int android_getaddrinfofornetcontext(const char*, const char*,
+                                                           const addrinfo*,
+                                                           const android_net_context*, addrinfo**);
 
-/* set name servers for a network */
-extern int _resolv_set_nameservers_for_net(unsigned netid, const char** servers,
-                                           unsigned numservers, const char* domains,
-                                           const struct __res_params* params) __used_in_netd;
+// Set name servers for a network
+LIBNETD_RESOLV_PUBLIC int _resolv_set_nameservers_for_net(unsigned netid, const char** servers,
+                                                          unsigned numservers, const char* domains,
+                                                          const __res_params* params);
 
-/* flush the cache associated with a certain network */
-extern void _resolv_flush_cache_for_net(unsigned netid) __used_in_netd;
+// Flush the cache associated with a certain network
+LIBNETD_RESOLV_PUBLIC void _resolv_flush_cache_for_net(unsigned netid);
 
-/* delete the cache associated with a certain network */
-extern void _resolv_delete_cache_for_net(unsigned netid) __used_in_netd;
-
-__END_DECLS
+// Delete the cache associated with a certain network
+LIBNETD_RESOLV_PUBLIC void _resolv_delete_cache_for_net(unsigned netid);
 
 #endif /* _RESOLV_NETID_H */
diff --git a/resolv/resolv_params.h b/resolv/resolv_params.h
index e2c7e48..d2f058b 100644
--- a/resolv/resolv_params.h
+++ b/resolv/resolv_params.h
@@ -58,4 +58,6 @@
 typedef res_sendhookact (*res_send_rhook)(const struct sockaddr*, const u_char*, int, u_char*, int,
                                           int*);
 
+#define LIBNETD_RESOLV_PUBLIC extern "C" [[gnu::visibility("default")]]
+
 #endif  // _RESOLV_PARAMS_H
diff --git a/resolv/resolv_private.h b/resolv/resolv_private.h
index 2503395..b633920 100644
--- a/resolv/resolv_private.h
+++ b/resolv/resolv_private.h
@@ -164,6 +164,13 @@
 void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
                                              const struct __res_sample* sample, int max_samples);
 
+
+// Calculate the round-trip-time from start time t0 and end time t1.
+int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0);
+
+// Create a sample for calculating server reachability statistics.
+void _res_stats_set_sample(__res_sample* sample, time_t now, int rcode, int rtt);
+
 /* End of stats related definitions */
 
 union res_sockaddr_union {
@@ -248,13 +255,9 @@
 /*			0x00010000	*/
 
 /* Things involving an internal (static) resolver context. */
-__BEGIN_DECLS
-
 struct __res_state* __res_get_state(void);
 void __res_put_state(struct __res_state*);
 
-__END_DECLS
-
 #define fp_nquery __fp_nquery
 #define fp_query __fp_query
 #define hostalias __hostalias
@@ -266,7 +269,6 @@
 #define res_send __res_send
 #define res_sendsigned __res_sendsigned
 
-__BEGIN_DECLS
 const char* hostalias(const char*);
 void p_query(const u_char*);
 void res_close(void);
@@ -279,7 +281,6 @@
 int res_search(const char*, int, int, u_char*, int);
 int res_send(const u_char*, int, u_char*, int);
 int res_sendsigned(const u_char*, int, ns_tsig_key*, u_char*, int);
-__END_DECLS
 
 #define dn_count_labels __dn_count_labels
 #define dn_skipname __dn_skipname
@@ -328,7 +329,6 @@
 #define res_send_setrhook __res_send_setrhook
 #define res_servicename __res_servicename
 #define res_servicenumber __res_servicenumber
-__BEGIN_DECLS
 int res_hnok(const char*);
 int res_ownok(const char*);
 int res_mailok(const char*);
@@ -338,8 +338,6 @@
 int dn_skipname(const u_char*, const u_char*);
 void putlong(uint32_t, u_char*);
 void putshort(uint16_t, u_char*);
-uint16_t _getshort(const u_char*);
-uint32_t _getlong(const u_char*);
 const char* p_class(int);
 const char* p_time(uint32_t);
 const char* p_type(int);
@@ -396,6 +394,4 @@
 int ns_name_map(ns_nname_ct, size_t, ns_namemap_t, int);
 int ns_name_labels(ns_nname_ct, size_t);
 
-__END_DECLS
-
 #endif /* !_RESOLV_PRIVATE_H_ */
diff --git a/resolv/resolv_stats.h b/resolv/resolv_stats.h
index 37e5a58..2d88be6 100644
--- a/resolv/resolv_stats.h
+++ b/resolv/resolv_stats.h
@@ -46,35 +46,20 @@
     uint8_t sample_next;
 };
 
-/* Calculate the round-trip-time from start time t0 and end time t1. */
-extern int _res_stats_calculate_rtt(const struct timespec* t1, const struct timespec* t0);
+// Aggregates the reachability statistics for the given server based on on the stored samples.
+LIBNETD_RESOLV_PUBLIC void android_net_res_stats_aggregate(__res_stats* stats,
+                                                           int* successes, int* errors,
+                                                           int* timeouts, int* internal_errors,
+                                                           int* rtt_avg, time_t* last_sample_time);
 
-/* Initialize a sample for calculating server reachability statistics. */
-extern void _res_stats_set_sample(struct __res_sample* sample, time_t now, int rcode, int rtt);
+LIBNETD_RESOLV_PUBLIC int android_net_res_stats_get_info_for_net(
+        unsigned netid, int* nscount, sockaddr_storage servers[MAXNS], int* dcount,
+        char domains[MAXDNSRCH][MAXDNSRCHPATH], __res_params* params, __res_stats stats[MAXNS]);
 
-/* Returns true if the server is considered unusable, i.e. if the success rate is not lower than the
- * threshold for the stored stored samples. If not enough samples are stored, the server is
- * considered usable.
- */
-extern bool _res_stats_usable_server(const struct __res_params* params, struct __res_stats* stats);
-
-__BEGIN_DECLS
-/* Aggregates the reachability statistics for the given server based on on the stored samples. */
-extern void android_net_res_stats_aggregate(struct __res_stats* stats, int* successes, int* errors,
-                                            int* timeouts, int* internal_errors, int* rtt_avg,
-                                            time_t* last_sample_time)
-        __attribute__((visibility("default")));
-
-extern int android_net_res_stats_get_info_for_net(
-        unsigned netid, int* nscount, struct sockaddr_storage servers[MAXNS], int* dcount,
-        char domains[MAXDNSRCH][MAXDNSRCHPATH], struct __res_params* params,
-        struct __res_stats stats[MAXNS]) __attribute__((visibility("default")));
-
-/* Returns an array of bools indicating which servers are considered good */
-extern void android_net_res_stats_get_usable_servers(const struct __res_params* params,
-                                                     struct __res_stats stats[], int nscount,
-                                                     bool valid_servers[])
-        __attribute__((visibility("default")));
-__END_DECLS
+// Returns an array of bools indicating which servers are considered good
+LIBNETD_RESOLV_PUBLIC void android_net_res_stats_get_usable_servers(const __res_params* params,
+                                                                    __res_stats stats[],
+                                                                    int nscount,
+                                                                    bool valid_servers[]);
 
 #endif  // _RES_STATS_H_