-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgethostbyname.c
86 lines (73 loc) · 1.84 KB
/
gethostbyname.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Demonstrate that gethostbyname() is not safe to use even in single threaded
* program. Call it twice on different hostnames and see what happens if we use
* both pointers to print the output.
*
* Compile:
* gcc gethostbyname.c
*
* Run like this:
* ./a.out www.devnull.cz www.mff.cuni.cz
* ./a.out www.mff.cuni.cz www.devnull.cz
*
* Expected output for the first case:
* # host1 [0x7febced31600/0x18f5020]:
* 195.113.27.222 [wwwmff.karlov.mff.cuni.cz]
* # host2 [0x7febced31600/0x18f5020]:
* 195.113.27.222 [wwwmff.karlov.mff.cuni.cz]
*
* (c) vlada@devnull.cz
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
void
print_hp(const char *comment, struct hostent *hp)
{
char **p;
printf("# %s [%p/%p]:\n", comment, (void *)hp, (void *)hp->h_addr_list);
for (p = hp->h_addr_list; *p != 0; p++) {
struct in_addr in;
(void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
(void) printf("%s [%s]\n", inet_ntoa(in), hp->h_name);
}
}
int
main(int argc, char *argv[]) {
struct hostent *hp1, *hp2;
int optidx = 0;
int seq = 0;
if (argc != 3 && argc != 4) {
printf("usage: %s [-s] <host1> <host2>\n", argv[0]);
printf("-s\tprint right after each call to gethostbyname()\n");
exit(1);
}
if (strcmp(argv[1], "-s") == 0) {
seq = 1;
optidx++;
}
optidx++;
if (strcmp(argv[optidx], argv[optidx + 1]) == 0) {
printf("hosts should be different to make the point\n");
exit(1);
}
if (!(hp1 = gethostbyname(argv[optidx]))) {
printf("cannot resolve %s\n", argv[optidx]);
exit(1);
}
if (seq)
print_hp("host1", hp1);
if (!(hp2 = gethostbyname(argv[optidx + 1]))) {
printf("cannot resolve %s\n", argv[optidx + 1]);
exit(1);
}
if (!seq)
print_hp("host1", hp1);
print_hp("host2", hp2);
return (0);
}