Back to index

glibc  2.9
tst-getaddrinfo3.c
Go to the documentation of this file.
00001 #include <mcheck.h>
00002 #include <netdb.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <arpa/inet.h>
00006 #include <arpa/nameser.h>
00007 
00008 
00009 static int
00010 do_test (void)
00011 {
00012   mtrace ();
00013 
00014   int result = 0;
00015   struct addrinfo hints;
00016   struct addrinfo *ai_res;
00017   int s;
00018 
00019 #define T(no, fail, addr, fam, coraddr)                                     \
00020   s = getaddrinfo (addr, NULL, &hints, &ai_res);                     \
00021   if (s != 0)                                                        \
00022     {                                                                \
00023       if (s != fail)                                                 \
00024        {                                                             \
00025          printf ("getaddrinfo test %d failed: %s\n", no, gai_strerror (s));  \
00026          result = 1;                                                 \
00027        }                                                             \
00028       ai_res = NULL;                                                 \
00029     }                                                                \
00030   else if (fail)                                                     \
00031     {                                                                \
00032       printf ("getaddrinfo test %d should have failed but did not\n", no);    \
00033       result = 1;                                                    \
00034     }                                                                \
00035   else if (ai_res->ai_family != fam)                                        \
00036     {                                                                \
00037       printf ("\
00038 getaddrinfo test %d return address of family %d, expected %d\n",            \
00039              no, ai_res->ai_family, fam);                            \
00040       result = 1;                                                    \
00041     }                                                                \
00042   else if (fam == AF_INET)                                           \
00043     {                                                                \
00044       if (ai_res->ai_addrlen != sizeof (struct sockaddr_in))                \
00045        {                                                             \
00046          printf ("getaddrinfo test %d: address size %zu, expected %zu\n",    \
00047                 no, (size_t) ai_res->ai_addrlen,                     \
00048                 sizeof (struct sockaddr_in));                               \
00049          result = 1;                                                 \
00050        }                                                             \
00051       else if (strcmp (coraddr, \
00052                      inet_ntoa (((struct sockaddr_in *) ai_res->ai_addr)->sin_addr))\
00053               != 0)                                                  \
00054        {                                                             \
00055          printf ("getaddrinfo test %d: got value %s, expected %s\n",        \
00056                 no,                                                  \
00057                 inet_ntoa (((struct sockaddr_in *) ai_res->ai_addr)->sin_addr), \
00058                 coraddr);                                            \
00059          result = 1;                                                 \
00060        }                                                             \
00061     }                                                                \
00062   else                                                               \
00063     {                                                                \
00064       char buf[100];                                                 \
00065                                                                      \
00066       if (ai_res->ai_addrlen != sizeof (struct sockaddr_in6))               \
00067        {                                                             \
00068          printf ("getaddrinfo test %d: address size %zu, expected %zu\n",    \
00069                 no, (size_t) ai_res->ai_addrlen,                     \
00070                 sizeof (struct sockaddr_in6));                       \
00071          result = 1;                                                 \
00072        }                                                             \
00073       else if (strcmp (coraddr, \
00074                      inet_ntop (AF_INET6,                            \
00075                               &((struct sockaddr_in6 *) ai_res->ai_addr)->sin6_addr,\
00076                               buf, sizeof (buf)))                           \
00077               != 0)                                                  \
00078        {                                                             \
00079          printf ("getaddrinfo test %d: got value %s, expected %s\n",        \
00080                 no,                                                  \
00081                 inet_ntop (AF_INET6,                                        \
00082                           & ((struct sockaddr_in6 *) ai_res->ai_addr)->sin6_addr, \
00083                           buf, sizeof (buf)),                        \
00084                 coraddr);                                            \
00085          result = 1;                                                 \
00086        }                                                             \
00087     }                                                                \
00088   if (ai_res != NULL && ai_res->ai_next != NULL)                     \
00089     {                                                                \
00090       puts ("expected only one result");                             \
00091       result = 1;                                                    \
00092     }                                                                \
00093   freeaddrinfo (ai_res)
00094 
00095 
00096   memset (&hints, '\0', sizeof (hints));
00097   hints.ai_family = AF_UNSPEC;
00098   hints.ai_socktype = SOCK_STREAM;
00099   T (1, 0, "127.0.0.1", AF_INET, "127.0.0.1");
00100 
00101   memset (&hints, '\0', sizeof (hints));
00102   hints.ai_family = AF_INET;
00103   hints.ai_socktype = SOCK_STREAM;
00104   T (2, 0, "127.0.0.1", AF_INET, "127.0.0.1");
00105 
00106   memset (&hints, '\0', sizeof (hints));
00107   hints.ai_family = AF_INET6;
00108   hints.ai_socktype = SOCK_STREAM;
00109   hints.ai_flags = AI_V4MAPPED;
00110   T (3, 0, "127.0.0.1", AF_INET6, "::ffff:127.0.0.1");
00111 
00112   memset (&hints, '\0', sizeof (hints));
00113   hints.ai_family = AF_INET6;
00114   hints.ai_socktype = SOCK_STREAM;
00115   T (4, EAI_ADDRFAMILY, "127.0.0.1", AF_INET6, "");
00116 
00117   memset (&hints, '\0', sizeof (hints));
00118   hints.ai_family = AF_UNSPEC;
00119   hints.ai_socktype = SOCK_STREAM;
00120   T (5, 0, "::1", AF_INET6, "::1");
00121 
00122   memset (&hints, '\0', sizeof (hints));
00123   hints.ai_family = AF_INET;
00124   hints.ai_socktype = SOCK_STREAM;
00125   T (6, EAI_ADDRFAMILY, "::1", AF_INET6, "");
00126 
00127   memset (&hints, '\0', sizeof (hints));
00128   hints.ai_family = AF_INET6;
00129   hints.ai_socktype = SOCK_STREAM;
00130   T (7, 0, "::1", AF_INET6, "::1");
00131 
00132   memset (&hints, '\0', sizeof (hints));
00133   hints.ai_family = AF_UNSPEC;
00134   hints.ai_socktype = SOCK_STREAM;
00135   T (8, 0, "::ffff:127.0.0.1", AF_INET6, "::ffff:127.0.0.1");
00136 
00137   memset (&hints, '\0', sizeof (hints));
00138   hints.ai_family = AF_INET;
00139   hints.ai_socktype = SOCK_STREAM;
00140   T (9, 0, "::ffff:127.0.0.1", AF_INET, "127.0.0.1");
00141 
00142   memset (&hints, '\0', sizeof (hints));
00143   hints.ai_family = AF_INET6;
00144   hints.ai_socktype = SOCK_STREAM;
00145   T (10, 0, "::ffff:127.0.0.1", AF_INET6, "::ffff:127.0.0.1");
00146 
00147   return result;
00148 }
00149 
00150 #define TEST_FUNCTION do_test ()
00151 #include "../test-skeleton.c"