Add __builtion_unreachable to vector::size(), vector::capacity()
[official-gcc.git] / gcc / testsuite / c-c++-common / analyzer / fd-manpage-getaddrinfo-server.c
blob7bc456800961027b559d658c070e8ed65a935f26
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 } */
31 /* Needed on some targets until we have exception-handling working (PR 111475). */
32 /* { dg-additional-options "-fno-exceptions" } */
34 /* { dg-skip-if "" { hppa*-*-hpux* powerpc*-*-aix* } } */
36 #include <sys/types.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <sys/socket.h>
42 #include <netdb.h>
44 #define BUF_SIZE 500
46 int
47 main(int argc, char *argv[])
49 struct addrinfo hints;
50 struct addrinfo *result, *rp;
51 int sfd, s;
52 struct sockaddr_storage peer_addr;
53 socklen_t peer_addr_len;
54 ssize_t nread;
55 char buf[BUF_SIZE];
57 if (argc != 2) {
58 fprintf(stderr, "Usage: %s port\n", argv[0]);
59 exit(EXIT_FAILURE);
62 memset(&hints, 0, sizeof(hints));
63 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
64 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
65 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
66 hints.ai_protocol = 0; /* Any protocol */
67 hints.ai_canonname = NULL;
68 hints.ai_addr = NULL;
69 hints.ai_next = NULL;
71 s = getaddrinfo(NULL, argv[1], &hints, &result);
72 if (s != 0) {
73 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
74 exit(EXIT_FAILURE);
77 /* getaddrinfo() returns a list of address structures.
78 Try each address until we successfully bind(2).
79 If socket(2) (or bind(2)) fails, we (close the socket
80 and) try the next address. */
82 for (rp = result; rp != NULL; rp = rp->ai_next) {
83 sfd = socket(rp->ai_family, rp->ai_socktype,
84 rp->ai_protocol);
85 if (sfd == -1)
86 continue;
88 if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
89 break; /* Success */
91 close(sfd);
94 freeaddrinfo(result); /* No longer needed */
96 if (rp == NULL) { /* No address succeeded */
97 fprintf(stderr, "Could not bind\n");
98 exit(EXIT_FAILURE);
101 /* Read datagrams and echo them back to sender. */
103 for (;;) {
104 peer_addr_len = sizeof(peer_addr);
105 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
106 (struct sockaddr *) &peer_addr, &peer_addr_len);
107 if (nread == -1)
108 continue; /* Ignore failed request */
110 char host[NI_MAXHOST], service[NI_MAXSERV];
112 s = getnameinfo((struct sockaddr *) &peer_addr,
113 peer_addr_len, host, NI_MAXHOST,
114 service, NI_MAXSERV, NI_NUMERICSERV);
115 if (s == 0)
116 printf("Received %zd bytes from %s:%s\n",
117 nread, host, service);
118 else
119 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
121 if (sendto(sfd, buf, nread, 0,
122 (struct sockaddr *) &peer_addr,
123 peer_addr_len) != nread)
124 fprintf(stderr, "Error sending response\n");