On 5 July 2016 at 23:44, Bernhard Reutner-Fischer <[email protected]> wrote: >Remove odd code until size whois.o is about 300 bytes in total, i'd say. >cheers,
I managed to go down to 264 bytes, removed all the unused code and used strrchr as suggested instead of strtok, thanks. Got rid also of func pipe_out, put the needed logic into main. Cheers Signed-off-by: Vito Mule' <[email protected]> function old new delta whois_server - 225 +225 whois_main 133 242 +109 WHOIS_HOST - 15 +15 pipe_out 85 - -85 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 1/0 up/down: 349/-85) Total: 264 bytes --- networking/whois.c | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) (add/remove: 2/1 grow/shrink: 1/0 up/down: 349/-85) Total: 264 bytes diff --git a/networking/whois.c b/networking/whois.c index bf33033..c9cd51e 100644 --- a/networking/whois.c +++ b/networking/whois.c @@ -3,6 +3,7 @@ * whois - tiny client for the whois directory service * * Copyright (c) 2011 Pere Orga <[email protected]> + * Copyright (c) 2016 Vito Mule' <[email protected]> * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ /* TODO @@ -28,38 +29,69 @@ #include "libbb.h" -static void pipe_out(int fd) +static const char WHOIS_HOST[] = "whois.iana.org"; +#define WHOIS_HOST_LEN 63 +#define WHOIS_PORT 43 + +/* Gets tld from NAME to find right whois sever. */ +/* Called only from main once. */ +void whois_server(char* host, char *argv_host) { FILE *fp; char buf[1024]; + char * tld = strrchr(argv_host, '.'); + /* don't support querying TLD objects for now */ + if (tld == NULL || ++tld == NULL) + exit(1); + + int fd = create_and_connect_stream_or_die(WHOIS_HOST, WHOIS_PORT); + fdprintf(fd, "%s\r\n", tld); + fp = xfdopen_for_read(fd); while (fgets(buf, sizeof(buf), fp)) { char *p = strpbrk(buf, "\r\n"); if (p) *p = '\0'; - puts(buf); + if (strstr(buf,"whois:") != NULL) { + char * whois_hostname = strrchr(buf, ' '); + if (whois_hostname == NULL || ++whois_hostname == NULL) + exit(1); + strncpy(host, whois_hostname, WHOIS_HOST_LEN); + } } - - fclose(fp); /* closes fd too */ + fclose(fp); } int whois_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int whois_main(int argc UNUSED_PARAM, char **argv) { + + char *host = malloc(WHOIS_HOST_LEN); int port = 43; - const char *host = "whois-servers.net"; opt_complementary = "-1:p+"; getopt32(argv, "h:p:", &host, &port); - argv += optind; + + if (strlen(host) < 1) + whois_server(host, *argv); do { int fd = create_and_connect_stream_or_die(host, port); fdprintf(fd, "%s\r\n", *argv); - pipe_out(fd); + FILE *fp; + char buf[1024]; + + fp = xfdopen_for_read(fd); + while (fgets(buf, sizeof(buf), fp)) { + char *p = strpbrk(buf, "\r\n"); + if (p) + *p = '\0'; + puts(buf); + } + fclose(fp); /* closes fd too */ } while (*++argv); - return EXIT_SUCCESS; } On 5 July 2016 at 23:44, Bernhard Reutner-Fischer <[email protected]> wrote: > On 5 July 2016 at 22:13, Vito Mulè <[email protected]> wrote: > > On 5 July 2016 at 12:36, Bernhard Reutner-Fischer <[email protected] > > > > wrote: > > On Tue, Jul 05, 2016 at 12:48:36PM +0200, Bernhard Reutner-Fischer wrote: > >> On July 5, 2016 12:26:07 PM GMT+02:00, "Vito Mulè" <[email protected] > > > >> wrote: > >> > >> > >> Care to send an updated patch, including bloat-o-meter output? > > > > Hello I managed to get the whois server hostname as you suggested, please > > let me know what you think. > > Patch also attached to the bug > > Thanks > > > > vmule@agent4:~/busybox.old$ ./busybox_unstripped whois nic.ikano > > Domain Name: nic.ikano > > Domain ID: D0000000007-IKANO > > WHOIS Server: whois.nic.ikano > > Referral URL: http://www.nic.ikano/ > > > > vmule@agent4:~/busybox.old$ ./busybox_unstripped whois amazon.it > > > > Domain: amazon.it > > Status: ok > > Created: 2000-02-10 00:00:00 > > Last Update: 2016-01-28 00:47:21 > > Expire Date: 2017-01-12 > > > > > > > > Signed-off-by: Vito Mule' <[email protected]> > > > > function old new delta > > whois_host - 277 +277 > > whois_main 133 277 +144 > > WHOIS_HOST - 15 +15 > > > ------------------------------------------------------------------------------ > > (add/remove: 2/0 grow/shrink: 1/0 up/down: 436/0) Total: 436 > > bytes > > Sill way too big ;) > > > > --- > > networking/whois.c | 78 > > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- > > 1 file changed, 68 insertions(+), 10 deletions(-) > > > > diff --git a/networking/whois.c b/networking/whois.c > > index bf33033..052f079 100644 > > --- a/networking/whois.c > > +++ b/networking/whois.c > > @@ -3,6 +3,7 @@ > > * whois - tiny client for the whois directory service > > * > > * Copyright (c) 2011 Pere Orga <[email protected]> > > + * modified by Vito Mule' <[email protected]> > > * Licensed under GPLv2 or later, see file LICENSE in this source tree. > > */ > > /* TODO > > @@ -28,6 +29,11 @@ > > > > #include "libbb.h" > > > > +static const char WHOIS_HOST[] = "whois.iana.org"; > > +#define WHOIS_HOST_LEN 128 > > +#define WHOIS_DOMAIN_LEN 32 > > A domain label can be at most 63 characters long. > meinedomainistschwerzulesenjedochgenaudreiundsechzigzeichenlang.at > looooooooooooooooooooooooooooooooooooooooooooooooooooooooooongc.at > > > +#define WHOIS_PORT 43 > > + > > static void pipe_out(int fd) > > { > > FILE *fp; > > @@ -36,30 +42,82 @@ static void pipe_out(int fd) > > fp = xfdopen_for_read(fd); > > while (fgets(buf, sizeof(buf), fp)) { > > char *p = strpbrk(buf, "\r\n"); > > - if (p) > > + if (p) { > > *p = '\0'; > > + } > > puts(buf); > > } > > - > > fclose(fp); /* closes fd too */ > > } > > > > +/* Gets tld from NAME to find right whois sever. */ > > +/* Called only from main for each NAME*/ > > +void whois_host(char* host, char *argv_host) > > should thus be named whois_server() or, better yet, fold it into > whois_main? > > > +{ > > + FILE *fp; > > + char buf[1024]; > > + char domain[WHOIS_DOMAIN_LEN]; > > + char *str_token = strdup(argv_host); > > + > > + if (strlen(host) >= 1) { > > + memset(&host[0], 0, WHOIS_HOST_LEN); > > + } > > + char *domain_token = strtok(str_token, "."); > > + while (domain_token != NULL) { > > + strncpy(domain, domain_token, WHOIS_DOMAIN_LEN); > > + domain_token = strtok(NULL, "."); > > + } > > char * tld = strrchr(argv_host, '.'); > /* don't support querying TLD objects for now */ > if (tld == NULL || ++tld == NULL) > exit(1); > > > + int fd = create_and_connect_stream_or_die(WHOIS_HOST, WHOIS_PORT); > > + fdprintf(fd, "%s\r\n", domain); > > s/domain/tld/; > > > + > > + fp = xfdopen_for_read(fd); > > + while (fgets(buf, sizeof(buf), fp)) { > > + char *p = strpbrk(buf, "\r\n"); > > + if (p) { > > + *p = '\0'; > > + } > > + if (strstr(buf,"whois:") != NULL) { > > + char *whois_token = strtok(buf, " "); > > + whois_token = strtok(NULL, " "); > > + strncpy(host, whois_token, WHOIS_HOST_LEN); > > + } > > + } > > + fclose(fp); > > + free(str_token); > > +} > > + > > int whois_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; > > int whois_main(int argc UNUSED_PARAM, char **argv) > > { > > + > > + char *host = malloc(WHOIS_HOST_LEN); > > int port = 43; > > - const char *host = "whois-servers.net"; > > > > opt_complementary = "-1:p+"; > > getopt32(argv, "h:p:", &host, &port); > > - > > argv += optind; > > - do { > > - int fd = create_and_connect_stream_or_die(host, port); > > - fdprintf(fd, "%s\r\n", *argv); > > - pipe_out(fd); > > + > > + if (strlen(host) < 1) { > > + do { > > + if (strlen(*argv) >= 67) { > > + dprintf(1, "Invalid request: %s\n", *argv); > > + return EXIT_FAILURE; > > + } > > + whois_host(host, *argv); > > + int fd = create_and_connect_stream_or_die(host, port); > > + fdprintf(fd, "%s\r\n", *argv); > > + pipe_out(fd); > > + } > > + while (*++argv); > > + free(host); > > + } else { > > + do { > > + int fd = create_and_connect_stream_or_die(host, port); > > + fdprintf(fd, "%s\r\n", *argv); > > + pipe_out(fd); > > + } > > It is generally expensive to have the same code twice in there. Just > setup the server if -h was not given. The > create_and_connect_stream_or_die etc is a common thing to do > afterwards. > > Remove odd code until size whois.o is about 300 bytes in total, i'd say. > cheers, >
_______________________________________________ busybox mailing list [email protected] http://lists.busybox.net/mailman/listinfo/busybox
