aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-04-15 14:10:45 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2018-04-15 14:10:45 +0200
commit58e43a4c40f56a463a475bec5ef915612fc5ed8c (patch)
treedb80893ca689be4113edba568c50e87fe33f395c
parent50aea2786b275c1f1d5f1b2df85b519dbaef6188 (diff)
downloadbusybox-w32-58e43a4c40f56a463a475bec5ef915612fc5ed8c.tar.gz
busybox-w32-58e43a4c40f56a463a475bec5ef915612fc5ed8c.tar.bz2
busybox-w32-58e43a4c40f56a463a475bec5ef915612fc5ed8c.zip
nslookup: move array of queries to "globals"
function old new delta add_query 95 89 -6 nslookup_main 2692 2641 -51 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-57) Total: -57 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/nslookup.c86
1 files changed, 38 insertions, 48 deletions
diff --git a/networking/nslookup.c b/networking/nslookup.c
index 88a638a79..d1697f2fd 100644
--- a/networking/nslookup.c
+++ b/networking/nslookup.c
@@ -314,8 +314,10 @@ struct globals {
314 unsigned default_port; 314 unsigned default_port;
315 unsigned default_retry; 315 unsigned default_retry;
316 unsigned default_timeout; 316 unsigned default_timeout;
317 unsigned query_count;
317 unsigned serv_count; 318 unsigned serv_count;
318 struct ns *server; 319 struct ns *server;
320 struct query *query;
319} FIX_ALIASING; 321} FIX_ALIASING;
320#define G (*(struct globals*)bb_common_bufsiz1) 322#define G (*(struct globals*)bb_common_bufsiz1)
321#define INIT_G() do { \ 323#define INIT_G() do { \
@@ -518,9 +520,9 @@ static char *make_ptr(char resbuf[80], const char *addrstr)
518 520
519/* 521/*
520 * Function logic borrowed & modified from musl libc, res_msend.c 522 * Function logic borrowed & modified from musl libc, res_msend.c
521 * n_queries is always > 0. 523 * G.query_count is always > 0.
522 */ 524 */
523static int send_queries(struct ns *ns, struct query *query, int n_queries) 525static int send_queries(struct ns *ns)
524{ 526{
525 unsigned char reply[512]; 527 unsigned char reply[512];
526 uint8_t rcode; 528 uint8_t rcode;
@@ -557,10 +559,10 @@ static int send_queries(struct ns *ns, struct query *query, int n_queries)
557 559
558 if (tcur - tsent >= retry_interval) { 560 if (tcur - tsent >= retry_interval) {
559 send: 561 send:
560 for (qn = 0; qn < n_queries; qn++) { 562 for (qn = 0; qn < G.query_count; qn++) {
561 if (query[qn].rlen) 563 if (G.query[qn].rlen)
562 continue; 564 continue;
563 if (write(pfd.fd, query[qn].query, query[qn].qlen) < 0) { 565 if (write(pfd.fd, G.query[qn].query, G.query[qn].qlen) < 0) {
564 bb_perror_msg("write to '%s'", ns->name); 566 bb_perror_msg("write to '%s'", ns->name);
565 n_replies = -1; /* "no go, try next server" */ 567 n_replies = -1; /* "no go, try next server" */
566 goto ret; 568 goto ret;
@@ -568,7 +570,7 @@ static int send_queries(struct ns *ns, struct query *query, int n_queries)
568 dbg("query %u sent\n", qn); 570 dbg("query %u sent\n", qn);
569 } 571 }
570 tsent = tcur; 572 tsent = tcur;
571 servfail_retry = 2 * n_queries; 573 servfail_retry = 2 * G.query_count;
572 } 574 }
573 575
574 /* Wait for a response, or until time to retry */ 576 /* Wait for a response, or until time to retry */
@@ -602,17 +604,17 @@ static int send_queries(struct ns *ns, struct query *query, int n_queries)
602// qn = save_idx; 604// qn = save_idx;
603 qn = 0; 605 qn = 0;
604 for (;;) { 606 for (;;) {
605 if (memcmp(reply, query[qn].query, 2) == 0) { 607 if (memcmp(reply, G.query[qn].query, 2) == 0) {
606 dbg("response matches query %u\n", qn); 608 dbg("response matches query %u\n", qn);
607 break; 609 break;
608 } 610 }
609 if (++qn >= n_queries) { 611 if (++qn >= G.query_count) {
610 dbg("response does not match any query\n"); 612 dbg("response does not match any query\n");
611 goto next; 613 goto next;
612 } 614 }
613 } 615 }
614 616
615 if (query[qn].rlen) { 617 if (G.query[qn].rlen) {
616 dbg("dropped duplicate response to query %u\n", qn); 618 dbg("dropped duplicate response to query %u\n", qn);
617 goto next; 619 goto next;
618 } 620 }
@@ -625,14 +627,14 @@ static int send_queries(struct ns *ns, struct query *query, int n_queries)
625 ns->failures++; 627 ns->failures++;
626 if (servfail_retry) { 628 if (servfail_retry) {
627 servfail_retry--; 629 servfail_retry--;
628 write(pfd.fd, query[qn].query, query[qn].qlen); 630 write(pfd.fd, G.query[qn].query, G.query[qn].qlen);
629 dbg("query %u resent\n", qn); 631 dbg("query %u resent\n", qn);
630 goto next; 632 goto next;
631 } 633 }
632 } 634 }
633 635
634 /* Process reply */ 636 /* Process reply */
635 query[qn].rlen = recvlen; 637 G.query[qn].rlen = recvlen;
636 tcur = monotonic_ms(); 638 tcur = monotonic_ms();
637#if 1 639#if 1
638 if (option_mask32 & OPT_debug) { 640 if (option_mask32 & OPT_debug) {
@@ -640,30 +642,30 @@ static int send_queries(struct ns *ns, struct query *query, int n_queries)
640 } 642 }
641 if (rcode != 0) { 643 if (rcode != 0) {
642 printf("** server can't find %s: %s\n", 644 printf("** server can't find %s: %s\n",
643 query[qn].name, rcodes[rcode]); 645 G.query[qn].name, rcodes[rcode]);
644 } else { 646 } else {
645 if (parse_reply(reply, recvlen) < 0) 647 if (parse_reply(reply, recvlen) < 0)
646 printf("*** Can't find %s: Parse error\n", query[qn].name); 648 printf("*** Can't find %s: Parse error\n", G.query[qn].name);
647 } 649 }
648 bb_putchar('\n'); 650 bb_putchar('\n');
649 n_replies++; 651 n_replies++;
650 if (n_replies >= n_queries) 652 if (n_replies >= G.query_count)
651 goto ret; 653 goto ret;
652#else 654#else
653//used to store replies and process them later 655//used to store replies and process them later
654 query[qn].latency = tcur - tstart; 656 G.query[qn].latency = tcur - tstart;
655 n_replies++; 657 n_replies++;
656 if (qn != save_idx) { 658 if (qn != save_idx) {
657 /* "wrong" receive buffer, move to correct one */ 659 /* "wrong" receive buffer, move to correct one */
658 memcpy(query[qn].reply, query[save_idx].reply, recvlen); 660 memcpy(G.query[qn].reply, G.query[save_idx].reply, recvlen);
659 continue; 661 continue;
660 } 662 }
661 /* query[0..save_idx] have replies, move to next one, if exists */ 663 /* G.query[0..save_idx] have replies, move to next one, if exists */
662 for (;;) { 664 for (;;) {
663 save_idx++; 665 save_idx++;
664 if (save_idx >= n_queries) 666 if (save_idx >= G.query_count)
665 goto ret; /* all are full: we have all results */ 667 goto ret; /* all are full: we have all results */
666 if (!query[save_idx].rlen) 668 if (!G.query[save_idx].rlen)
667 break; /* this one is empty */ 669 break; /* this one is empty */
668 } 670 }
669#endif 671#endif
@@ -718,36 +720,27 @@ static void parse_resolvconf(void)
718 } 720 }
719} 721}
720 722
721static struct query *add_query(struct query **queries, int *n_queries, 723static void add_query(int type, const char *dname)
722 int type, const char *dname)
723{ 724{
724 struct query *new_q; 725 struct query *new_q;
725 int pos; 726 unsigned count;
726 ssize_t qlen; 727 ssize_t qlen;
727 728
728 pos = *n_queries; 729 count = G.query_count++;
729 *n_queries = pos + 1;
730 *queries = new_q = xrealloc(*queries, sizeof(**queries) * (pos + 1));
731 730
732 dbg("new query#%u type %u for '%s'\n", pos, type, dname); 731 G.query = xrealloc_vector(G.query, /*2=2^1:*/ 1, count);
733 new_q += pos; 732 new_q = &G.query[count];
734 memset(new_q, 0, sizeof(*new_q));
735 733
734 dbg("new query#%u type %u for '%s'\n", count, type, dname);
735 new_q->name = dname;
736 qlen = res_mkquery(QUERY, dname, C_IN, type, NULL, 0, NULL, 736 qlen = res_mkquery(QUERY, dname, C_IN, type, NULL, 0, NULL,
737 new_q->query, sizeof(new_q->query)); 737 new_q->query, sizeof(new_q->query));
738
739 new_q->qlen = qlen; 738 new_q->qlen = qlen;
740 new_q->name = dname;
741
742 return new_q;
743} 739}
744 740
745int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 741int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
746int nslookup_main(int argc UNUSED_PARAM, char **argv) 742int nslookup_main(int argc UNUSED_PARAM, char **argv)
747{ 743{
748 struct ns *ns;
749 struct query *queries;
750 int n_queries;
751 unsigned types; 744 unsigned types;
752 int rc; 745 int rc;
753 int err; 746 int err;
@@ -837,8 +830,6 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
837 } 830 }
838 } 831 }
839 832
840 n_queries = 0;
841 queries = NULL;
842 if (types == 0) { 833 if (types == 0) {
843 /* No explicit type given, guess query type. 834 /* No explicit type given, guess query type.
844 * If we can convert the domain argument into a ptr (means that 835 * If we can convert the domain argument into a ptr (means that
@@ -851,18 +842,18 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
851 842
852 ptr = make_ptr(buf80, argv[0]); 843 ptr = make_ptr(buf80, argv[0]);
853 if (ptr) { 844 if (ptr) {
854 add_query(&queries, &n_queries, T_PTR, xstrdup(ptr)); 845 add_query(T_PTR, xstrdup(ptr));
855 } else { 846 } else {
856 add_query(&queries, &n_queries, T_A, argv[0]); 847 add_query(T_A, argv[0]);
857#if ENABLE_FEATURE_IPV6 848#if ENABLE_FEATURE_IPV6
858 add_query(&queries, &n_queries, T_AAAA, argv[0]); 849 add_query(T_AAAA, argv[0]);
859#endif 850#endif
860 } 851 }
861 } else { 852 } else {
862 int c; 853 int c;
863 for (c = 0; c < ARRAY_SIZE(qtypes); c++) { 854 for (c = 0; c < ARRAY_SIZE(qtypes); c++) {
864 if (types & (1 << c)) 855 if (types & (1 << c))
865 add_query(&queries, &n_queries, qtypes[c].type, argv[0]); 856 add_query(qtypes[c].type, argv[0]);
866 } 857 }
867 } 858 }
868 859
@@ -881,7 +872,7 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
881 for (rc = 0; rc < G.serv_count;) { 872 for (rc = 0; rc < G.serv_count;) {
882 int c; 873 int c;
883 874
884 c = send_queries(&G.server[rc], queries, n_queries); 875 c = send_queries(&G.server[rc]);
885 if (c > 0) { 876 if (c > 0) {
886 /* more than zero replies received */ 877 /* more than zero replies received */
887#if 0 /* which version does this? */ 878#if 0 /* which version does this? */
@@ -921,9 +912,9 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
921 } 912 }
922 913
923 err = 0; 914 err = 0;
924 for (rc = 0; rc < n_queries; rc++) { 915 for (rc = 0; rc < G.query_count; rc++) {
925 if (queries[rc].rlen == 0) { 916 if (G.query[rc].rlen == 0) {
926 printf("*** Can't find %s: No answer\n", queries[rc].name); 917 printf("*** Can't find %s: No answer\n", G.query[rc].name);
927 err = 1; 918 err = 1;
928 } 919 }
929 } 920 }
@@ -931,9 +922,8 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
931 bb_putchar('\n'); /* should this affect exicode too? */ 922 bb_putchar('\n'); /* should this affect exicode too? */
932 923
933 if (ENABLE_FEATURE_CLEAN_UP) { 924 if (ENABLE_FEATURE_CLEAN_UP) {
934 free(ns); 925 free(G.server);
935 if (n_queries) 926 free(G.query);
936 free(queries);
937 } 927 }
938 928
939 return EXIT_SUCCESS; 929 return EXIT_SUCCESS;