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
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>
41 #include <sys/socket.h>
47 main(int argc
, char *argv
[])
49 struct addrinfo hints
;
50 struct addrinfo
*result
, *rp
;
52 struct sockaddr_storage peer_addr
;
53 socklen_t peer_addr_len
;
58 fprintf(stderr
, "Usage: %s port\n", argv
[0]);
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
;
71 s
= getaddrinfo(NULL
, argv
[1], &hints
, &result
);
73 fprintf(stderr
, "getaddrinfo: %s\n", gai_strerror(s
));
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
,
88 if (bind(sfd
, rp
->ai_addr
, rp
->ai_addrlen
) == 0)
94 freeaddrinfo(result
); /* No longer needed */
96 if (rp
== NULL
) { /* No address succeeded */
97 fprintf(stderr
, "Could not bind\n");
101 /* Read datagrams and echo them back to sender. */
104 peer_addr_len
= sizeof(peer_addr
);
105 nread
= recvfrom(sfd
, buf
, BUF_SIZE
, 0,
106 (struct sockaddr
*) &peer_addr
, &peer_addr_len
);
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
);
116 printf("Received %zd bytes from %s:%s\n",
117 nread
, host
, service
);
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");