tst-aton.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. /* Note: uClibc only supports the standard notation 'a.b.c.d' */
  6. static struct tests
  7. {
  8. const char *input;
  9. int valid;
  10. uint32_t result;
  11. } tests[] =
  12. {
  13. { "", 0, 0 },
  14. { "-1", 0, 0 },
  15. /*
  16. { "256", 1, 0x00000100 },
  17. */
  18. { "256.", 0, 0 },
  19. { "256a", 0, 0 },
  20. /*
  21. { "0x100", 1, 0x00000100 },
  22. { "0200.0x123456", 1, 0x80123456 },
  23. { "0300.0x89123456.", 0 ,0 },
  24. { "0100.-0xffff0000", 0, 0 },
  25. { "0.0xffffff", 1, 0x00ffffff },
  26. { "0.0x1000000", 0, 0 },
  27. { "0377.16777215", 1, 0xffffffff },
  28. { "0377.16777216", 0, 0 },
  29. { "0x87.077777777", 1, 0x87ffffff },
  30. { "0x87.0100000000", 0, 0 },
  31. { "0.1.3", 1, 0x00010003 },
  32. */
  33. { "0.256.3", 0, 0 },
  34. { "256.1.3", 0, 0 },
  35. /*
  36. { "0.1.0x10000", 0, 0 },
  37. { "0.1.0xffff", 1, 0x0001ffff },
  38. */
  39. { "0.1a.3", 0, 0 },
  40. { "0.1.a3", 0, 0 },
  41. { "1.2.3.4", 1, 0x01020304 },
  42. { "0400.2.3.4", 0, 0 },
  43. { "1.0x100.3.4", 0, 0 },
  44. { "1.2.256.4", 0, 0 },
  45. { "1.2.3.0x100", 0, 0 },
  46. { "323543357756889", 0, 0 },
  47. { "10.1.2.3.4", 0, 0},
  48. };
  49. int
  50. main (int argc, char *argv[])
  51. {
  52. int result = 0;
  53. size_t cnt;
  54. for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt)
  55. {
  56. struct in_addr addr;
  57. if ((int) inet_aton (tests[cnt].input, &addr) != tests[cnt].valid)
  58. {
  59. if (tests[cnt].valid)
  60. printf ("\"%s\" not seen as valid IP address\n", tests[cnt].input);
  61. else
  62. printf ("\"%s\" seen as valid IP address\n", tests[cnt].input);
  63. result = 1;
  64. }
  65. else if (tests[cnt].valid && addr.s_addr != ntohl (tests[cnt].result))
  66. {
  67. printf ("\"%s\" not converted correctly: is %08x, should be %08x\n",
  68. tests[cnt].input, addr.s_addr, tests[cnt].result);
  69. result = 1;
  70. }
  71. }
  72. return result;
  73. }