nslookup.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include <stdio.h>
  8. #include <pthread.h>
  9. #ifdef __wasi__
  10. #include <wasi/api.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <wasi_socket_ext.h>
  14. #else
  15. #include <netdb.h>
  16. #endif
  17. void
  18. test_nslookup(int af)
  19. {
  20. struct addrinfo *res;
  21. int count = 0;
  22. struct addrinfo hints;
  23. char *url = "google-public-dns-a.google.com";
  24. memset(&hints, 0, sizeof(hints));
  25. hints.ai_family = af;
  26. hints.ai_socktype = SOCK_STREAM;
  27. int ret = getaddrinfo(url, 0, &hints, &res);
  28. assert(ret == 0);
  29. struct addrinfo *address = res;
  30. while (address) {
  31. assert(address->ai_family == af);
  32. assert(address->ai_socktype == SOCK_STREAM);
  33. count++;
  34. address = address->ai_next;
  35. }
  36. assert(count > 0);
  37. freeaddrinfo(res);
  38. }
  39. void *
  40. test_nslookup_mt(void *params)
  41. {
  42. int *af = (int *)params;
  43. test_nslookup(*af);
  44. return NULL;
  45. }
  46. int
  47. main()
  48. {
  49. int afs[] = { AF_INET, AF_INET6 };
  50. for (int i = 0; i < sizeof(afs) / sizeof(afs[0]); i++) {
  51. pthread_t th;
  52. printf("Testing %d in main thread...\n", afs[i]);
  53. test_nslookup(afs[i]);
  54. printf("Testing %d in a new thread...\n", afs[i]);
  55. pthread_create(&th, NULL, test_nslookup_mt, &afs[i]);
  56. pthread_join(th, NULL);
  57. }
  58. return 0;
  59. }