getai.c 962 B

123456789101112131415161718192021222324252627282930313233
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nspr.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. int main(int argc, char **argv)
  9. {
  10. PRAddrInfo *ai;
  11. void *iter;
  12. PRNetAddr addr;
  13. ai = PR_GetAddrInfoByName(argv[1], PR_AF_UNSPEC, PR_AI_ADDRCONFIG);
  14. if (ai == NULL) {
  15. fprintf(stderr, "PR_GetAddrInfoByName failed: (%d, %d)\n",
  16. PR_GetError(), PR_GetOSError());
  17. exit(1);
  18. }
  19. printf("%s\n", PR_GetCanonNameFromAddrInfo(ai));
  20. iter = NULL;
  21. while ((iter = PR_EnumerateAddrInfo(iter, ai, 0, &addr)) != NULL) {
  22. char buf[128];
  23. PR_NetAddrToString(&addr, buf, sizeof buf);
  24. printf("%s\n", buf);
  25. }
  26. PR_FreeAddrInfo(ai);
  27. return 0;
  28. }