Back to index

glibc  2.9
tst-getaddrinfo2.c
Go to the documentation of this file.
00001 /* Test by David L Stevens <dlstevens@us.ibm.com> [BZ #358] */
00002 #include <errno.h>
00003 #include <netdb.h>
00004 #include <unistd.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <sys/socket.h>
00008 
00009 static int
00010 do_test (void)
00011 {
00012   const char portstr[] = "583";
00013   int port = atoi (portstr);
00014   struct addrinfo hints, *aires, *pai;
00015   int rv;
00016   int res = 1;
00017 
00018   memset (&hints, 0, sizeof (hints));
00019   hints.ai_family = AF_INET;
00020   rv = getaddrinfo (NULL, portstr, &hints, &aires);
00021   if (rv == 0)
00022     {
00023       struct sockaddr_in *psin = 0;
00024       int got_tcp, got_udp;
00025       int err = 0;
00026 
00027       got_tcp = got_udp = 0;
00028       for (pai = aires; pai; pai = pai->ai_next)
00029         {
00030           printf ("ai_family=%d, ai_addrlen=%d, ai_socktype=%d",
00031                   (int) pai->ai_family, (int) pai->ai_addrlen,
00032                   (int) pai->ai_socktype);
00033           if (pai->ai_family == AF_INET)
00034             printf (", port=%d",
00035                     ntohs (((struct sockaddr_in *) pai->ai_addr)->sin_port));
00036           puts ("");
00037 
00038           err |= pai->ai_family != AF_INET;
00039           err |= pai->ai_addrlen != sizeof (struct sockaddr_in);
00040           err |= pai->ai_addr == 0;
00041           if (pai->ai_family == AF_INET)
00042             err |=
00043               ntohs (((struct sockaddr_in *) pai->ai_addr)->sin_port) != port;
00044           got_tcp |= pai->ai_socktype == SOCK_STREAM;
00045           got_udp |= pai->ai_socktype == SOCK_DGRAM;
00046           if (err)
00047             break;
00048         }
00049       if (err)
00050         {
00051           printf ("FAIL getaddrinfo IPv4 socktype 0,513: "
00052                   "fam %d alen %d addr %p addr/fam %d "
00053                   "addr/port %d H[%d]\n",
00054                   pai->ai_family, pai->ai_addrlen, psin,
00055                   psin ? psin->sin_family : 0,
00056                   psin ? psin->sin_port : 0,
00057                   psin ? htons (psin->sin_port) : 0);
00058         }
00059       else if (got_tcp && got_udp)
00060         {
00061           printf ("SUCCESS getaddrinfo IPv4 socktype 0,513\n");
00062           res = 0;
00063         }
00064       else
00065         printf ("FAIL getaddrinfo IPv4 socktype 0,513 TCP %d"
00066                 " UDP %d\n", got_tcp, got_udp);
00067       freeaddrinfo (aires);
00068     }
00069   else
00070     printf ("FAIL getaddrinfo IPv4 socktype 0,513 returns %d "
00071             "(\"%s\")\n", rv, gai_strerror (rv));
00072 
00073   return res;
00074 }
00075 
00076 #define TEST_FUNCTION do_test ()
00077 #include "../test-skeleton.c"