1 /* Test by David L Stevens <dlstevens@us.ibm.com> [BZ #358] */
2 #include <errno.h>
3 #include <netdb.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/socket.h>
9 
10 static int
do_test(void)11 do_test (void)
12 {
13   const char portstr[] = "583";
14   int port = atoi (portstr);
15   struct addrinfo hints, *aires, *pai;
16   int rv;
17   int res = 1;
18 
19   memset (&hints, 0, sizeof (hints));
20   hints.ai_family = AF_INET;
21   rv = getaddrinfo (NULL, portstr, &hints, &aires);
22   if (rv == 0)
23     {
24       struct sockaddr_in *psin = 0;
25       int got_tcp, got_udp;
26       int err = 0;
27 
28       got_tcp = got_udp = 0;
29       for (pai = aires; pai; pai = pai->ai_next)
30         {
31           printf ("ai_family=%d, ai_addrlen=%d, ai_socktype=%d",
32                   (int) pai->ai_family, (int) pai->ai_addrlen,
33                   (int) pai->ai_socktype);
34           if (pai->ai_family == AF_INET)
35             printf (", port=%d",
36                     ntohs (((struct sockaddr_in *) pai->ai_addr)->sin_port));
37           puts ("");
38 
39           err |= pai->ai_family != AF_INET;
40           err |= pai->ai_addrlen != sizeof (struct sockaddr_in);
41           err |= pai->ai_addr == 0;
42           if (pai->ai_family == AF_INET)
43             err |=
44               ntohs (((struct sockaddr_in *) pai->ai_addr)->sin_port) != port;
45           got_tcp |= pai->ai_socktype == SOCK_STREAM;
46           got_udp |= pai->ai_socktype == SOCK_DGRAM;
47           if (err)
48             break;
49         }
50       if (err)
51         {
52           printf ("FAIL getaddrinfo IPv4 socktype 0,513: "
53                   "fam %d alen %d addr %p addr/fam %d "
54                   "addr/port %d H[%d]\n",
55                   pai->ai_family, pai->ai_addrlen, psin,
56                   psin ? psin->sin_family : 0,
57                   psin ? psin->sin_port : 0,
58                   psin ? htons (psin->sin_port) : 0);
59         }
60       else if (got_tcp && got_udp)
61         {
62           printf ("SUCCESS getaddrinfo IPv4 socktype 0,513\n");
63           res = 0;
64         }
65       else
66         printf ("FAIL getaddrinfo IPv4 socktype 0,513 TCP %d"
67                 " UDP %d\n", got_tcp, got_udp);
68       freeaddrinfo (aires);
69     }
70   else
71     printf ("FAIL getaddrinfo IPv4 socktype 0,513 returns %d "
72             "(\"%s\")\n", rv, gai_strerror (rv));
73 
74   return res;
75 }
76 
77 #define TEST_FUNCTION do_test ()
78 #include "../test-skeleton.c"
79