Back to index

glibc  2.9
tst-inet_ntop.c
Go to the documentation of this file.
00001 #include <arpa/inet.h>
00002 #include <errno.h>
00003 #include <netinet/in.h>
00004 #include <stdio.h>
00005 #include <string.h>
00006 
00007 int
00008 main (void)
00009 {
00010   struct in_addr addr4;
00011   struct in6_addr addr6;
00012   char buf[64];
00013   int result = 0;
00014 
00015   addr4.s_addr = 0xe0e0e0e0;
00016   addr6.s6_addr16[0] = 0;
00017   addr6.s6_addr16[1] = 0;
00018   addr6.s6_addr16[2] = 0;
00019   addr6.s6_addr16[3] = 0;
00020   addr6.s6_addr16[4] = 0;
00021   addr6.s6_addr16[5] = 0xffff;
00022   addr6.s6_addr32[3] = 0xe0e0e0e0;
00023   memset (buf, 'x', sizeof buf);
00024 
00025   if (inet_ntop (AF_INET, &addr4, buf, 15) != NULL)
00026     {
00027       puts ("1st inet_ntop returned non-NULL");
00028       result++;
00029     }
00030   else if (errno != ENOSPC)
00031     {
00032       puts ("1st inet_ntop didn't fail with ENOSPC");
00033       result++;
00034     }
00035   if (buf[15] != 'x')
00036     {
00037       puts ("1st inet_ntop wrote past the end of buffer");
00038       result++;
00039     }
00040 
00041   if (inet_ntop (AF_INET, &addr4, buf, 16) != buf)
00042     {
00043       puts ("2nd inet_ntop did not return buf");
00044       result++;
00045     }
00046   if (memcmp (buf, "224.224.224.224\0" "xxxxxxxx", 24) != 0)
00047     {
00048       puts ("2nd inet_ntop wrote past the end of buffer");
00049       result++;
00050     }
00051 
00052   if (inet_ntop (AF_INET6, &addr6, buf, 22) != NULL)
00053     {
00054       puts ("3rd inet_ntop returned non-NULL");
00055       result++;
00056     }
00057   else if (errno != ENOSPC)
00058     {
00059       puts ("3rd inet_ntop didn't fail with ENOSPC");
00060       result++;
00061     }
00062   if (buf[22] != 'x')
00063     {
00064       puts ("3rd inet_ntop wrote past the end of buffer");
00065       result++;
00066     }
00067 
00068   if (inet_ntop (AF_INET6, &addr6, buf, 23) != buf)
00069     {
00070       puts ("4th inet_ntop did not return buf");
00071       result++;
00072     }
00073   if (memcmp (buf, "::ffff:224.224.224.224\0" "xxxxxxxx", 31) != 0)
00074     {
00075       puts ("4th inet_ntop wrote past the end of buffer");
00076       result++;
00077     }
00078 
00079   memset (&addr6.s6_addr, 0xe0, sizeof (addr6.s6_addr));
00080 
00081   if (inet_ntop (AF_INET6, &addr6, buf, 39) != NULL)
00082     {
00083       puts ("5th inet_ntop returned non-NULL");
00084       result++;
00085     }
00086   else if (errno != ENOSPC)
00087     {
00088       puts ("5th inet_ntop didn't fail with ENOSPC");
00089       result++;
00090     }
00091   if (buf[39] != 'x')
00092     {
00093       puts ("5th inet_ntop wrote past the end of buffer");
00094       result++;
00095     }
00096 
00097   if (inet_ntop (AF_INET6, &addr6, buf, 40) != buf)
00098     {
00099       puts ("6th inet_ntop did not return buf");
00100       result++;
00101     }
00102   if (memcmp (buf, "e0e0:e0e0:e0e0:e0e0:e0e0:e0e0:e0e0:e0e0\0"
00103                  "xxxxxxxx", 48) != 0)
00104     {
00105       puts ("6th inet_ntop wrote past the end of buffer");
00106       result++;
00107     }
00108 
00109   
00110   return result;
00111 }