]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
[master] split up main and add callback function pointers to support iOS
authorEvan Hunt <each@isc.org>
Fri, 11 Aug 2017 05:51:24 +0000 (22:51 -0700)
committerEvan Hunt <each@isc.org>
Fri, 11 Aug 2017 05:51:24 +0000 (22:51 -0700)
4677. [port] Split up the main function in dig to better support
the iOS app version. [RT #45508]

CHANGES
bin/dig/dig.c
bin/dig/dighost.c
bin/dig/host.c
bin/dig/include/dig/dig.h
bin/dig/nslookup.c

diff --git a/CHANGES b/CHANGES
index ea34d8a2293f84de9dd4fc8d9f83b6570574ce40..b855902fea0b8aed76baebed9c67d0c5377988fc 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,6 @@
+4677.  [port]          Split up the main function in dig to better support
+                       the iOS app version. [RT #45508]
+
 4676.  [cleanup]       Allow BIND to be built using OpenSSL 1.0.X with
                        deprecated functions removed. [RT #45706]
 
index 435e2d000797e0e86665ff7e937a9c97dbea1424..2d13374455c5cf8d3d1d9fc23c5bba42415acf23 100644 (file)
@@ -226,7 +226,7 @@ help(void) {
 /*%
  * Callback from dighost.c to print the received message.
  */
-void
+static void
 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
        isc_uint64_t diff;
        time_t tnow;
@@ -294,7 +294,7 @@ received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
  * Not used in dig.
  * XXX print_trying
  */
-void
+static void
 trying(char *frm, dig_lookup_t *lookup) {
        UNUSED(frm);
        UNUSED(lookup);
@@ -424,7 +424,7 @@ isdotlocal(dns_message_t *msg) {
 /*
  * Callback from dighost.c to print the reply from a server
  */
-isc_result_t
+static isc_result_t
 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
        isc_result_t result;
        dns_messagetextflag_t flags;
@@ -2089,8 +2089,8 @@ parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
  * Here, we're possibly reading from a batch file, then shutting down
  * for real if there's nothing in the batch file to read.
  */
-void
-dighost_shutdown(void) {
+static void
+query_finished(void) {
        char batchline[MXNAME];
        int bargc;
        char *bargv[16];
@@ -2136,23 +2136,38 @@ dighost_shutdown(void) {
        }
 }
 
-/*% Main processing routine for dig */
-int
-main(int argc, char **argv) {
+void dig_setup(int argc, char **argv)
+{
        isc_result_t result;
 
        ISC_LIST_INIT(lookup_list);
        ISC_LIST_INIT(server_list);
        ISC_LIST_INIT(search_list);
 
-       debug("main()");
+       debug("dig_setup()");
+
+       /* setup dighost callbacks */
+       dighost_printmessage = printmessage;
+       dighost_received = received;
+       dighost_trying = trying;
+       dighost_shutdown = query_finished;
+
        progname = argv[0];
        preparse_args(argc, argv);
+
        result = isc_app_start();
        check_result(result, "isc_app_start");
+
        setup_libs();
        setup_system(ipv4only, ipv6only);
-       parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
+}
+
+void dig_query_setup(isc_boolean_t is_batchfile, isc_boolean_t config_only,
+               int argc, char **argv)
+{
+       debug("dig_query_setup");
+
+       parse_args(is_batchfile, config_only, argc, argv);
        if (keyfile[0] != 0)
                setup_file_key();
        else if (keysecret[0] != 0)
@@ -2161,9 +2176,25 @@ main(int argc, char **argv) {
                set_search_domain(domainopt);
                usesearch = ISC_TRUE;
        }
+}
+
+void dig_startup() {
+       isc_result_t result;
+
+       debug("dig_startup()");
+
        result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
        check_result(result, "isc_app_onrun");
        isc_app_run();
+}
+
+void dig_query_start()
+{
+       start_lookup();
+}
+
+void
+dig_shutdown() {
        destroy_lookup(default_lookup);
        if (batchname != NULL) {
                if (batchfp != stdin)
@@ -2173,5 +2204,16 @@ main(int argc, char **argv) {
        cancel_all();
        destroy_libs();
        isc_app_finish();
+}
+
+/*% Main processing routine for dig */
+int
+main(int argc, char **argv) {
+
+       dig_setup(argc, argv);
+       dig_query_setup(ISC_FALSE, ISC_FALSE, argc, argv);
+       dig_startup();
+       dig_shutdown();
+
        return (exitcode);
 }
index 4ac322a877e6d57e9cfded9f90188139a47d076f..d9d674a4934b13e29b93c28b1cb0a9d7eedee802 100644 (file)
@@ -199,6 +199,23 @@ dig_lookup_t *current_lookup = NULL;
                     "isc_mutex_unlock");\
 }
 
+/* dynamic callbacks */
+
+isc_result_t
+(*dighost_printmessage)(dig_query_t *query, dns_message_t *msg,
+       isc_boolean_t headers);
+
+void
+(*dighost_received)(int bytes, isc_sockaddr_t *from, dig_query_t *query);
+
+void
+(*dighost_trying)(char *frm, dig_lookup_t *lookup);
+
+void
+(*dighost_shutdown)(void);
+
+/* forward declarations */
+
 static void
 cancel_lookup(dig_lookup_t *lookup);
 
@@ -2196,7 +2213,7 @@ setup_lookup(dig_lookup_t *lookup) {
                }
        }
        dns_name_format(lookup->name, store, sizeof(store));
-       trying(store, lookup);
+       dighost_trying(store, lookup);
        INSIST(dns_name_isabsolute(lookup->name));
 
        isc_random_get(&id);
@@ -2532,8 +2549,8 @@ setup_lookup(dig_lookup_t *lookup) {
        /* XXX qrflag, print_query, etc... */
        if (!ISC_LIST_EMPTY(lookup->q) && lookup->qr) {
                extrabytes = 0;
-               printmessage(ISC_LIST_HEAD(lookup->q), lookup->sendmsg,
-                            ISC_TRUE);
+               dighost_printmessage(ISC_LIST_HEAD(lookup->q),
+                                    lookup->sendmsg, ISC_TRUE);
                if (lookup->stats)
                        printf(";; QUERY SIZE: %u\n\n",
                               isc_buffer_usedlength(&lookup->renderbuf));
@@ -3359,7 +3376,7 @@ check_for_more_data(dig_query_t *query, dns_message_t *msg,
        launch_next_query(query, ISC_FALSE);
        return (ISC_FALSE);
  doexit:
-       received(sevent->n, &sevent->address, query);
+       dighost_received(sevent->n, &sevent->address, query);
        return (ISC_TRUE);
 }
 
@@ -3861,18 +3878,19 @@ recv_done(isc_task_t *task, isc_event_t *event) {
                if (msg->rcode == dns_rcode_nxdomain &&
                    (l->origin != NULL || l->need_search)) {
                        if (!next_origin(query->lookup) || showsearch) {
-                               printmessage(query, msg, ISC_TRUE);
-                               received(b->used, &sevent->address, query);
+                               dighost_printmessage(query, msg, ISC_TRUE);
+                               dighost_received(b->used, &sevent->address,
+                                                query);
                        }
                } else if (!l->trace && !l->ns_search_only) {
-                       printmessage(query, msg, ISC_TRUE);
+                       dighost_printmessage(query, msg, ISC_TRUE);
                } else if (l->trace) {
                        int nl = 0;
                        int count = msg->counts[DNS_SECTION_ANSWER];
 
                        debug("in TRACE code");
                        if (!l->ns_search_only)
-                               printmessage(query, msg, ISC_TRUE);
+                               dighost_printmessage(query, msg, ISC_TRUE);
 
                        l->rdtype = l->qrdtype;
                        if (l->trace_root || (l->ns_search_only && count > 0)) {
@@ -3903,7 +3921,7 @@ recv_done(isc_task_t *task, isc_event_t *event) {
                                l->trace_root = ISC_FALSE;
                                usesearch = ISC_FALSE;
                        } else {
-                               printmessage(query, msg, ISC_TRUE);
+                               dighost_printmessage(query, msg, ISC_TRUE);
                        }
                }
        }
@@ -3929,7 +3947,7 @@ recv_done(isc_task_t *task, isc_event_t *event) {
        } else {
 
                if (msg->rcode == dns_rcode_noerror || l->origin == NULL) {
-                       received(b->used, &sevent->address, query);
+                       dighost_received(b->used, &sevent->address, query);
                }
 
                if (!query->lookup->ns_search_only)
index 5ea2854db377c7cdee4c4ecc0d390e8f5b44e466..38bdba3faaa17576abaaa9c96e9c97f6a4cc288c 100644 (file)
@@ -163,12 +163,12 @@ show_usage(void) {
        exit(1);
 }
 
-void
-dighost_shutdown(void) {
-       isc_app_shutdown();
+static void
+host_shutdown(void) {
+       (void) isc_app_shutdown();
 }
 
-void
+static void
 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
        isc_time_t now;
        int diff;
@@ -183,7 +183,7 @@ received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
        }
 }
 
-void
+static void
 trying(char *frm, dig_lookup_t *lookup) {
        UNUSED(lookup);
 
@@ -403,7 +403,7 @@ chase_cnamechain(dns_message_t *msg, dns_name_t *qname) {
        }
 }
 
-isc_result_t
+static isc_result_t
 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
        isc_boolean_t did_flag = ISC_FALSE;
        dns_rdataset_t *opt, *tsig = NULL;
@@ -890,6 +890,12 @@ main(int argc, char **argv) {
        idnoptions = IDN_ASCCHECK;
 #endif
 
+       /* setup dighost callbacks */
+       dighost_printmessage = printmessage;
+       dighost_received = received;
+       dighost_trying = trying;
+       dighost_shutdown = host_shutdown;
+
        debug("main()");
        progname = argv[0];
        pre_parse_args(argc, argv);
index 23c8966a42fba0c3b566d14675aebaf7ba458a8e..664b309e47d604df257df8fd08e1a40b8591141b 100644 (file)
@@ -356,37 +356,73 @@ destroy_libs(void);
 void
 set_search_domain(char *domain);
 
+char *
+next_token(char **stringp, const char *delim);
+
 /*
- * Routines to be defined in dig.c, host.c, and nslookup.c.
+ * Routines to be defined in dig.c, host.c, and nslookup.c. and
+ * then assigned to the appropriate function pointer
  */
-isc_result_t
-printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers);
+extern isc_result_t
+(*dighost_printmessage)(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers);
 /*%<
  * Print the final result of the lookup.
  */
 
-void
-received(int bytes, isc_sockaddr_t *from, dig_query_t *query);
+extern void
+(*dighost_received)(int bytes, isc_sockaddr_t *from, dig_query_t *query);
 /*%<
  * Print a message about where and when the response
  * was received from, like the final comment in the
  * output of "dig".
  */
 
-void
-trying(char *frm, dig_lookup_t *lookup);
+extern void
+(*dighost_trying)(char *frm, dig_lookup_t *lookup);
 
-void
-dighost_shutdown(void);
-
-char *
-next_token(char **stringp, const char *delim);
+extern void
+(*dighost_shutdown)(void);
 
 void save_opt(dig_lookup_t *lookup, char *code, char *value);
 
 void setup_file_key(void);
 void setup_text_key(void);
 
+/*
+ * Routines exported from dig.c for use by dig for iOS
+ */
+
+/*%<
+ * Call once only to set up libraries, parse global
+ * parameters and initial command line query parameters
+ */
+void
+dig_setup(int argc, char **argv);
+
+/*%<
+ * Call to supply new parameters for the next lookup
+ */
+void
+dig_query_setup(isc_boolean_t, isc_boolean_t, int argc, char **argv);
+
+/*%<
+ * set the main application event cycle running
+ */
+void
+dig_startup(void);
+
+/*%<
+ * Initiates the next lookup cycle
+ */
+void
+dig_query_start(void);
+
+/*%<
+ * Cleans up the application
+ */
+void
+dig_shutdown(void);
+
 ISC_LANG_ENDDECLS
 
 #endif
index 6b48329a4f04e228985ea63aab1cd8cb60266780..b124084065b577599bbc51e876324673450e2ead 100644 (file)
@@ -156,8 +156,8 @@ rcode_totext(dns_rcode_t rcode)
        return totext.deconsttext;
 }
 
-void
-dighost_shutdown(void) {
+static void
+query_finished(void) {
        isc_event_t *event = global_event;
 
        flush_lookup_list();
@@ -386,7 +386,7 @@ detailsection(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers,
        return (ISC_R_SUCCESS);
 }
 
-void
+static void
 received(int bytes, isc_sockaddr_t *from, dig_query_t *query)
 {
        UNUSED(bytes);
@@ -394,11 +394,10 @@ received(int bytes, isc_sockaddr_t *from, dig_query_t *query)
        UNUSED(query);
 }
 
-void
+static void
 trying(char *frm, dig_lookup_t *lookup) {
        UNUSED(frm);
        UNUSED(lookup);
-
 }
 
 static void
@@ -426,7 +425,7 @@ chase_cnamechain(dns_message_t *msg, dns_name_t *qname) {
        }
 }
 
-isc_result_t
+static isc_result_t
 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
        char servtext[ISC_SOCKADDR_FORMATSIZE];
 
@@ -973,6 +972,12 @@ main(int argc, char **argv) {
 
        check_ra = ISC_TRUE;
 
+       /* setup dighost callbacks */
+       dighost_printmessage = printmessage;
+       dighost_received = received;
+       dighost_trying = trying;
+       dighost_shutdown = query_finished;
+
        result = isc_app_start();
        check_result(result, "isc_app_start");