whois.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * whois - tiny client for the whois directory service
  4. *
  5. * Copyright (c) 2011 Pere Orga <gotrunks@gmail.com>
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. /* TODO
  9. * Add ipv6 support
  10. * Add proxy support
  11. */
  12. //config:config WHOIS
  13. //config: bool "whois"
  14. //config: default y
  15. //config: help
  16. //config: whois is a client for the whois directory service
  17. //applet:IF_WHOIS(APPLET(whois, BB_DIR_USR_BIN, BB_SUID_DROP))
  18. //kbuild:lib-$(CONFIG_WHOIS) += whois.o
  19. //usage:#define whois_trivial_usage
  20. //usage: "[-h SERVER] [-p PORT] NAME..."
  21. //usage:#define whois_full_usage "\n\n"
  22. //usage: "Query WHOIS info about NAME\n"
  23. //usage: "\n -h,-p Server to query"
  24. #include "libbb.h"
  25. static void pipe_out(int fd)
  26. {
  27. FILE *fp;
  28. char buf[1024];
  29. fp = xfdopen_for_read(fd);
  30. while (fgets(buf, sizeof(buf), fp)) {
  31. char *p = strpbrk(buf, "\r\n");
  32. if (p)
  33. *p = '\0';
  34. puts(buf);
  35. }
  36. fclose(fp); /* closes fd too */
  37. }
  38. int whois_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int whois_main(int argc UNUSED_PARAM, char **argv)
  40. {
  41. int port = 43;
  42. const char *host = "whois-servers.net";
  43. opt_complementary = "-1:p+";
  44. getopt32(argv, "h:p:", &host, &port);
  45. argv += optind;
  46. do {
  47. int fd = create_and_connect_stream_or_die(host, port);
  48. fdprintf(fd, "%s\r\n", *argv);
  49. pipe_out(fd);
  50. }
  51. while (*++argv);
  52. return EXIT_SUCCESS;
  53. }