str2addr.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. /*
  6. * File: str2addr.c
  7. * Description: a test for PR_StringToNetAddr
  8. */
  9. #include "nspr.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. /* Address string to convert */
  13. #define DEFAULT_IPV4_ADDR_STR "207.200.73.41"
  14. /* Expected conversion result, in network byte order */
  15. static unsigned char default_ipv4_addr[] = {207, 200, 73, 41};
  16. int main(int argc, char **argv)
  17. {
  18. PRNetAddr addr;
  19. const char *addrStr;
  20. unsigned char *bytes;
  21. int idx;
  22. addrStr = DEFAULT_IPV4_ADDR_STR;
  23. if (PR_StringToNetAddr(addrStr, &addr) == PR_FAILURE) {
  24. fprintf(stderr, "PR_StringToNetAddr failed\n");
  25. exit(1);
  26. }
  27. if (addr.inet.family != PR_AF_INET) {
  28. fprintf(stderr, "addr.inet.family should be %d but is %d\n",
  29. PR_AF_INET, addr.inet.family);
  30. exit(1);
  31. }
  32. bytes = (unsigned char *) &addr.inet.ip;
  33. for (idx = 0; idx < 4; idx++) {
  34. if (bytes[idx] != default_ipv4_addr[idx]) {
  35. fprintf(stderr, "byte %d of IPv4 addr should be %d but is %d\n",
  36. idx, default_ipv4_addr[idx], bytes[idx]);
  37. exit(1);
  38. }
  39. }
  40. printf("PASS\n");
  41. return 0;
  42. }