Back to index

glibc  2.9
tst-aton.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <sys/socket.h>
00003 #include <netinet/in.h>
00004 #include <arpa/inet.h>
00005 
00006 
00007 static struct tests
00008 {
00009   const char *input;
00010   int valid;
00011   uint32_t result;
00012 } tests[] =
00013 {
00014   { "", 0, 0 },
00015   { "-1", 0, 0 },
00016   { "256", 1, 0x00000100 },
00017   { "256.", 0, 0 },
00018   { "256a", 0, 0 },
00019   { "0x100", 1, 0x00000100 },
00020   { "0200.0x123456", 1, 0x80123456 },
00021   { "0300.0x89123456.", 0 ,0 },
00022   { "0100.-0xffff0000", 0, 0 },
00023   { "0.0xffffff", 1, 0x00ffffff },
00024   { "0.0x1000000", 0, 0 },
00025   { "0377.16777215", 1, 0xffffffff },
00026   { "0377.16777216", 0, 0 },
00027   { "0x87.077777777", 1, 0x87ffffff },
00028   { "0x87.0100000000", 0, 0 },
00029   { "0.1.3", 1, 0x00010003 },
00030   { "0.256.3", 0, 0 },
00031   { "256.1.3", 0, 0 },
00032   { "0.1.0x10000", 0, 0 },
00033   { "0.1.0xffff", 1, 0x0001ffff },
00034   { "0.1a.3", 0, 0 },
00035   { "0.1.a3", 0, 0 },
00036   { "1.2.3.4", 1, 0x01020304 },
00037   { "0400.2.3.4", 0, 0 },
00038   { "1.0x100.3.4", 0, 0 },
00039   { "1.2.256.4", 0, 0 },
00040   { "1.2.3.0x100", 0, 0 },
00041   { "323543357756889", 0, 0 },
00042   { "10.1.2.3.4", 0, 0},
00043 };
00044 
00045 
00046 int
00047 main (int argc, char *argv[])
00048 {
00049   int result = 0;
00050   size_t cnt;
00051 
00052   for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt)
00053     {
00054       struct in_addr addr;
00055 
00056       if ((int) inet_aton (tests[cnt].input, &addr) != tests[cnt].valid)
00057        {
00058          if (tests[cnt].valid)
00059            printf ("\"%s\" not seen as valid IP address\n", tests[cnt].input);
00060          else
00061            printf ("\"%s\" seen as valid IP address\n", tests[cnt].input);
00062          result = 1;
00063        }
00064       else if (tests[cnt].valid && addr.s_addr != ntohl (tests[cnt].result))
00065        {
00066          printf ("\"%s\" not converted correctly: is %08x, should be %08x\n",
00067                 tests[cnt].input, addr.s_addr, tests[cnt].result);
00068          result = 1;
00069        }
00070     }
00071 
00072   return result;
00073 }