testsuite: Skip analyzer tests on AIX.
[official-gcc.git] / gcc / testsuite / c-c++-common / analyzer / fd-mappage-getaddrinfo-server.c
blobc02ee6ff6437ffccaff77095be730ab095110634
1 /* Example from getaddrinfo.3 manpage, which has this license:
3 Copyright (c) 2007, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
4 and Copyright (c) 2006 Ulrich Drepper <drepper@redhat.com>
5 A few pieces of an earlier version remain:
6 Copyright 2000, Sam Varshavchik <mrsam@courier-mta.com>
8 Permission is granted to make and distribute verbatim copies of this
9 manual provided the copyright notice and this permission notice are
10 preserved on all copies.
12 Permission is granted to copy and distribute modified versions of this
13 manual under the conditions for verbatim copying, provided that the
14 entire resulting derived work is distributed under the terms of a
15 permission notice identical to this one.
17 Since the Linux kernel and libraries are constantly changing, this
18 manual page may be incorrect or out-of-date. The author(s) assume no
19 responsibility for errors or omissions, or for damages resulting from
20 the use of the information contained herein. The author(s) may not
21 have taken the same level of care in the production of this manual,
22 which is licensed free of charge, as they might when working
23 professionally.
25 Formatted or processed versions of this manual, if unaccompanied by
26 the source, must acknowledge the copyright and authors of this work.
29 /* { dg-require-effective-target sockets } */
30 /* { dg-skip-if "" { powerpc*-*-aix* } } */
32 #include <sys/types.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <sys/socket.h>
38 #include <netdb.h>
40 #define BUF_SIZE 500
42 int
43 main(int argc, char *argv[])
45 struct addrinfo hints;
46 struct addrinfo *result, *rp;
47 int sfd, s;
48 struct sockaddr_storage peer_addr;
49 socklen_t peer_addr_len;
50 ssize_t nread;
51 char buf[BUF_SIZE];
53 if (argc != 2) {
54 fprintf(stderr, "Usage: %s port\n", argv[0]);
55 exit(EXIT_FAILURE);
58 memset(&hints, 0, sizeof(hints));
59 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
60 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
61 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
62 hints.ai_protocol = 0; /* Any protocol */
63 hints.ai_canonname = NULL;
64 hints.ai_addr = NULL;
65 hints.ai_next = NULL;
67 s = getaddrinfo(NULL, argv[1], &hints, &result);
68 if (s != 0) {
69 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
70 exit(EXIT_FAILURE);
73 /* getaddrinfo() returns a list of address structures.
74 Try each address until we successfully bind(2).
75 If socket(2) (or bind(2)) fails, we (close the socket
76 and) try the next address. */
78 for (rp = result; rp != NULL; rp = rp->ai_next) {
79 sfd = socket(rp->ai_family, rp->ai_socktype,
80 rp->ai_protocol);
81 if (sfd == -1)
82 continue;
84 if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
85 break; /* Success */
87 close(sfd);
90 freeaddrinfo(result); /* No longer needed */
92 if (rp == NULL) { /* No address succeeded */
93 fprintf(stderr, "Could not bind\n");
94 exit(EXIT_FAILURE);
97 /* Read datagrams and echo them back to sender. */
99 for (;;) {
100 peer_addr_len = sizeof(peer_addr);
101 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
102 (struct sockaddr *) &peer_addr, &peer_addr_len);
103 if (nread == -1)
104 continue; /* Ignore failed request */
106 char host[NI_MAXHOST], service[NI_MAXSERV];
108 s = getnameinfo((struct sockaddr *) &peer_addr,
109 peer_addr_len, host, NI_MAXHOST,
110 service, NI_MAXSERV, NI_NUMERICSERV);
111 if (s == 0)
112 printf("Received %zd bytes from %s:%s\n",
113 nread, host, service);
114 else
115 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
117 if (sendto(sfd, buf, nread, 0,
118 (struct sockaddr *) &peer_addr,
119 peer_addr_len) != nread)
120 fprintf(stderr, "Error sending response\n");