/contrib/bind9/lib/export/samples/sample-gai.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 77 lines · 49 code · 12 blank · 16 comment · 8 complexity · 2c04a6943debc763eca299838b023d18 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* $Id: sample-gai.c,v 1.4 2009/09/02 23:48:02 tbox Exp $ */
  17. #include <config.h>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <irs/netdb.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. static void
  25. do_gai(int family, char *hostname) {
  26. struct addrinfo hints, *res, *res0;
  27. int error;
  28. char namebuf[1024], addrbuf[1024], servbuf[1024];
  29. memset(&hints, 0, sizeof(hints));
  30. hints.ai_family = family;
  31. hints.ai_socktype = SOCK_STREAM;
  32. hints.ai_flags = AI_CANONNAME;
  33. error = getaddrinfo(hostname, "http", &hints, &res0);
  34. if (error) {
  35. fprintf(stderr, "getaddrinfo failed for %s,family=%d: %s\n",
  36. hostname, family, gai_strerror(error));
  37. return;
  38. }
  39. for (res = res0; res; res = res->ai_next) {
  40. error = getnameinfo(res->ai_addr, res->ai_addrlen,
  41. addrbuf, sizeof(addrbuf),
  42. NULL, 0, NI_NUMERICHOST);
  43. if (error == 0)
  44. error = getnameinfo(res->ai_addr, res->ai_addrlen,
  45. namebuf, sizeof(namebuf),
  46. servbuf, sizeof(servbuf), 0);
  47. if (error != 0) {
  48. fprintf(stderr, "getnameinfo failed: %s\n",
  49. gai_strerror(error));
  50. } else {
  51. printf("%s(%s/%s)=%s:%s\n", hostname,
  52. res->ai_canonname, addrbuf, namebuf, servbuf);
  53. }
  54. }
  55. freeaddrinfo(res);
  56. }
  57. int
  58. main(int argc, char *argv[]) {
  59. if (argc < 2)
  60. exit(1);
  61. do_gai(AF_INET, argv[1]);
  62. do_gai(AF_INET6, argv[1]);
  63. do_gai(AF_UNSPEC, argv[1]);
  64. exit(0);
  65. }