test_inet_pton.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "../lib/unp.h"
  2. #ifndef AF_INET6
  3. #define AF_INET6 AF_MAX+1 /* just to let this compile */
  4. #endif
  5. int inet_pton(int, const char *, void *);
  6. int
  7. main(int argc, char **argv)
  8. {
  9. int i;
  10. char buff[100];
  11. /*
  12. * Make certain that we can test the difference between 0.0.0.0
  13. * being acceptable for AF_INET (but not acceptable for AF_INET6)
  14. * and 0::0 being OK for AF_INET6 (but not OK for AF_INET).
  15. * This way a server can be coded as protocol independent (IPv4 or
  16. * IPv6) but let the user specify the local IP address as either
  17. * 0.0.0.0 or 0::0 as an indirect way of telling the server when
  18. * it starts, which protocol to use (but still allowing the server
  19. * to bind the wildcard address).
  20. */
  21. if ( (i = inet_pton(AF_INET, "0.0.0.0", buff)) != 1) /* should be OK */
  22. printf("AF_INET, 0.0.0.0 returned: %d\n", i);
  23. if ( (i = inet_pton(AF_INET6, "0.0.0.0", buff)) != 0)
  24. printf("AF_INET6, 0.0.0.0 returned: %d\n", i);
  25. if ( (i = inet_pton(AF_INET6, "0::0", buff)) != 1) /* should be OK */
  26. printf("AF_INET6, 0::0 returned: %d\n", i);
  27. if ( (i = inet_pton(AF_INET, "0::0", buff)) != 0)
  28. printf("AF_INET, 0::0 returned: %d\n", i);
  29. printf("inet_pton(AF_INET6, \"1.2.3.4\", buff) returns %d\n",
  30. inet_pton(AF_INET6, "1.2.3.4", buff));
  31. printf("inet_pton(AF_INET6, \"::1.2.3.4\", buff) returns %d\n",
  32. inet_pton(AF_INET6, "::1.2.3.4", buff));
  33. exit(0);
  34. }