nslookup.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <assert.h>
  6. #include <string.h>
  7. #ifdef __wasi__
  8. #include <wasi/api.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <wasi_socket_ext.h>
  12. #else
  13. #include <netdb.h>
  14. #endif
  15. void
  16. test_nslookup(int af)
  17. {
  18. struct addrinfo *res;
  19. int count = 0;
  20. struct addrinfo hints;
  21. char *url = "google-public-dns-a.google.com";
  22. memset(&hints, 0, sizeof(hints));
  23. hints.ai_family = af;
  24. hints.ai_socktype = SOCK_STREAM;
  25. int ret = getaddrinfo(url, 0, &hints, &res);
  26. assert(ret == 0);
  27. struct addrinfo *address = res;
  28. while (address) {
  29. assert(address->ai_family == af);
  30. assert(address->ai_socktype == SOCK_STREAM);
  31. count++;
  32. address = address->ai_next;
  33. }
  34. assert(count > 0);
  35. freeaddrinfo(res);
  36. }
  37. int
  38. main()
  39. {
  40. test_nslookup(AF_INET); /* for ipv4 */
  41. test_nslookup(AF_INET6); /* for ipv6 */
  42. return 0;
  43. }