File: tst-getaddrinfo2.c

package info (click to toggle)
glibc 2.19-18+deb8u9
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 204,712 kB
  • ctags: 145,148
  • sloc: ansic: 970,427; asm: 241,207; sh: 10,069; makefile: 8,476; cpp: 3,595; perl: 2,077; pascal: 1,839; awk: 1,704; yacc: 317; sed: 73
file content (77 lines) | stat: -rw-r--r-- 2,325 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Test by David L Stevens <dlstevens@us.ibm.com> [BZ #358] */
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>

static int
do_test (void)
{
  const char portstr[] = "583";
  int port = atoi (portstr);
  struct addrinfo hints, *aires, *pai;
  int rv;
  int res = 1;

  memset (&hints, 0, sizeof (hints));
  hints.ai_family = AF_INET;
  rv = getaddrinfo (NULL, portstr, &hints, &aires);
  if (rv == 0)
    {
      struct sockaddr_in *psin = 0;
      int got_tcp, got_udp;
      int err = 0;

      got_tcp = got_udp = 0;
      for (pai = aires; pai; pai = pai->ai_next)
        {
          printf ("ai_family=%d, ai_addrlen=%d, ai_socktype=%d",
                  (int) pai->ai_family, (int) pai->ai_addrlen,
                  (int) pai->ai_socktype);
          if (pai->ai_family == AF_INET)
            printf (", port=%d",
                    ntohs (((struct sockaddr_in *) pai->ai_addr)->sin_port));
          puts ("");

          err |= pai->ai_family != AF_INET;
          err |= pai->ai_addrlen != sizeof (struct sockaddr_in);
          err |= pai->ai_addr == 0;
          if (pai->ai_family == AF_INET)
            err |=
              ntohs (((struct sockaddr_in *) pai->ai_addr)->sin_port) != port;
          got_tcp |= pai->ai_socktype == SOCK_STREAM;
          got_udp |= pai->ai_socktype == SOCK_DGRAM;
          if (err)
            break;
        }
      if (err)
        {
          printf ("FAIL getaddrinfo IPv4 socktype 0,513: "
                  "fam %d alen %d addr %p addr/fam %d "
                  "addr/port %d H[%d]\n",
                  pai->ai_family, pai->ai_addrlen, psin,
                  psin ? psin->sin_family : 0,
                  psin ? psin->sin_port : 0,
                  psin ? htons (psin->sin_port) : 0);
        }
      else if (got_tcp && got_udp)
        {
          printf ("SUCCESS getaddrinfo IPv4 socktype 0,513\n");
          res = 0;
        }
      else
        printf ("FAIL getaddrinfo IPv4 socktype 0,513 TCP %d"
                " UDP %d\n", got_tcp, got_udp);
      freeaddrinfo (aires);
    }
  else
    printf ("FAIL getaddrinfo IPv4 socktype 0,513 returns %d "
            "(\"%s\")\n", rv, gai_strerror (rv));

  return res;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"