| 1 |
/* Example from getaddrinfo.3 manpage, which has this license: |
| 2 |
|
| 3 |
Copyright (c) 2007, 2008 Michael Kerrisk <[email protected]> |
| 4 |
and Copyright (c) 2006 Ulrich Drepper <[email protected]> |
| 5 |
A few pieces of an earlier version remain: |
| 6 |
Copyright 2000, Sam Varshavchik <[email protected]> |
| 7 |
|
| 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. |
| 11 |
|
| 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. |
| 16 |
|
| 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. |
| 24 |
|
| 25 |
Formatted or processed versions of this manual, if unaccompanied by |
| 26 |
the source, must acknowledge the copyright and authors of this work. |
| 27 |
*/ |
| 28 |
|
| 29 |
/* { dg-require-effective-target sockets } */ |
| 30 |
/* { dg-additional-options "-Wno-analyzer-too-complex" } */ |
| 31 |
|
| 32 |
/* Needed on some targets until we have exception-handling working (PR 111475). */ |
| 33 |
/* { dg-additional-options "-fno-exceptions" } */ |
| 34 |
|
| 35 |
/* { dg-skip-if "" { hppa*-*-hpux* powerpc*-*-aix* } } */ |
| 36 |
|
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/socket.h> |
| 39 |
#include <netdb.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <stdlib.h> |
| 42 |
#include <unistd.h> |
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
#define BUF_SIZE 500 |
| 46 |
|
| 47 |
int |
| 48 |
main(int argc, char *argv[]) |
| 49 |
{ |
| 50 |
struct addrinfo hints; |
| 51 |
struct addrinfo *result, *rp; |
| 52 |
int sfd, s; |
| 53 |
size_t len; |
| 54 |
ssize_t nread; |
| 55 |
char buf[BUF_SIZE]; |
| 56 |
|
| 57 |
if (argc < 3) { |
| 58 |
fprintf(stderr, "Usage: %s host port msg...\n", argv[0]); |
| 59 |
exit(EXIT_FAILURE); |
| 60 |
} |
| 61 |
|
| 62 |
/* Obtain address(es) matching host/port. */ |
| 63 |
|
| 64 |
memset(&hints, 0, sizeof(hints)); |
| 65 |
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ |
| 66 |
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ |
| 67 |
hints.ai_flags = 0; |
| 68 |
hints.ai_protocol = 0; /* Any protocol */ |
| 69 |
|
| 70 |
s = getaddrinfo(argv[1], argv[2], &hints, &result); |
| 71 |
if (s != 0) { |
| 72 |
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); |
| 73 |
exit(EXIT_FAILURE); |
| 74 |
} |
| 75 |
|
| 76 |
/* getaddrinfo() returns a list of address structures. |
| 77 |
Try each address until we successfully connect(2). |
| 78 |
If socket(2) (or connect(2)) fails, we (close the socket |
| 79 |
and) try the next address. */ |
| 80 |
|
| 81 |
for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 82 |
sfd = socket(rp->ai_family, rp->ai_socktype, |
| 83 |
rp->ai_protocol); |
| 84 |
if (sfd == -1) |
| 85 |
continue; |
| 86 |
|
| 87 |
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) |
| 88 |
break; /* Success */ |
| 89 |
|
| 90 |
close(sfd); |
| 91 |
} |
| 92 |
|
| 93 |
freeaddrinfo(result); /* No longer needed */ |
| 94 |
|
| 95 |
if (rp == NULL) { /* No address succeeded */ |
| 96 |
fprintf(stderr, "Could not connect\n"); |
| 97 |
exit(EXIT_FAILURE); |
| 98 |
} |
| 99 |
|
| 100 |
/* Send remaining command-line arguments as separate |
| 101 |
datagrams, and read responses from server. */ |
| 102 |
|
| 103 |
for (int j = 3; j < argc; j++) { |
| 104 |
len = strlen(argv[j]) + 1; |
| 105 |
/* +1 for terminating null byte */ |
| 106 |
|
| 107 |
if (len > BUF_SIZE) { |
| 108 |
fprintf(stderr, |
| 109 |
"Ignoring long message in argument %d\n", j); |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
if (write(sfd, argv[j], len) != len) { |
| 114 |
fprintf(stderr, "partial/failed write\n"); |
| 115 |
exit(EXIT_FAILURE); |
| 116 |
} |
| 117 |
|
| 118 |
nread = read(sfd, buf, BUF_SIZE); |
| 119 |
if (nread == -1) { |
| 120 |
perror("read"); |
| 121 |
exit(EXIT_FAILURE); |
| 122 |
} |
| 123 |
|
| 124 |
printf("Received %zd bytes: %s\n", nread, buf); |
| 125 |
} |
| 126 |
|
| 127 |
exit(EXIT_SUCCESS); |
| 128 |
} |