-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy path16-16.c
70 lines (62 loc) · 1.72 KB
/
16-16.c
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
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include "apue.h"
#define BUFLEN 128
/*
extern int connect_retry(int, int, int, const struct sockaddr *, socklen_t);
*/
#define MAXSLEEP 128
int connect_retry(int domain, int type, int protocol,
const struct sockaddr *addr, socklen_t alen) {
int numsec, fd;
for (numsec = 1; numsec <= MAXSLEEP; numsec <<= 1) {
if ((fd = socket(domain, type, protocol)) < 0) {
return (-1);
}
if (connect(fd, addr, alen) == 0) {
return (fd);
}
close(fd);
if (numsec <= MAXSLEEP / 2) {
sleep(numsec);
}
}
return (-1);
}
void print_uptime(int sockfd) {
int n;
char buf[BUFLEN];
while ((n = recv(sockfd, buf, BUFLEN, 0)) > 0) {
write(STDOUT_FILENO, buf, n);
}
if (n < 0) {
err_sys("recv error");
}
}
int main(int argc, char *argv[]) {
struct addrinfo *ailist, *aip;
struct addrinfo hint;
int sockfd, err;
if (argc != 2) {
err_quit("usage: ruptime hostname");
}
memset(&hint, 0, sizeof(hint));
hint.ai_socktype = SOCK_STREAM;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;
if ((err = getaddrinfo(argv[1], "ruptime", &hint, &ailist)) != 0) {
err_quit("getaddrinfo error: %s", gai_strerror(err));
}
for (aip = ailist; aip != NULL; aip = aip->ai_next) {
if ((sockfd = connect_retry(aip->ai_family, SOCK_STREAM, 0,
aip->ai_addr, aip->ai_addrlen)) < 0) {
err = errno;
} else {
print_uptime(sockfd);
exit(0);
}
}
err_exit(err, "can't connect to %s", argv[1]);
}