forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostent2.lc
48 lines (42 loc) · 2.59 KB
/
hostent2.lc
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
#include "unp.h"## 1 ##src/names/hostent2.c##
int## 2 ##src/names/hostent2.c##
main(int argc, char **argv)## 3 ##src/names/hostent2.c##
{## 4 ##src/names/hostent2.c##
char *ptr, **pptr;## 5 ##src/names/hostent2.c##
char str[INET6_ADDRSTRLEN];## 6 ##src/names/hostent2.c##
struct hostent *hptr;## 7 ##src/names/hostent2.c##
while (--argc > 0) {## 8 ##src/names/hostent2.c##
ptr = *++argv;## 9 ##src/names/hostent2.c##
if ((hptr = gethostbyname(ptr)) == NULL) {## 10 ##src/names/hostent2.c##
err_msg("gethostbyname error for host: %s: %s",## 11 ##src/names/hostent2.c##
ptr, hstrerror(h_errno));## 12 ##src/names/hostent2.c##
continue;## 13 ##src/names/hostent2.c##
}## 14 ##src/names/hostent2.c##
printf("official hostname: %s\n", hptr->h_name);## 15 ##src/names/hostent2.c##
for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)## 16 ##src/names/hostent2.c##
printf(" alias: %s\n", *pptr);## 17 ##src/names/hostent2.c##
switch (hptr->h_addrtype) {## 18 ##src/names/hostent2.c##
case AF_INET:## 19 ##src/names/hostent2.c##
#ifdef AF_INET6## 20 ##src/names/hostent2.c##
case AF_INET6:## 21 ##src/names/hostent2.c##
#endif## 22 ##src/names/hostent2.c##
pptr = hptr->h_addr_list;## 23 ##src/names/hostent2.c##
for (; *pptr != NULL; pptr++) {## 24 ##src/names/hostent2.c##
printf("\taddress: %s\n",## 25 ##src/names/hostent2.c##
Inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));## 26 ##src/names/hostent2.c##
if ((hptr = gethostbyaddr(*pptr, hptr->h_length,## 27 ##src/names/hostent2.c##
hptr->h_addrtype)) == NULL)## 28 ##src/names/hostent2.c##
printf("\t(gethostbyaddr failed)\n");## 29 ##src/names/hostent2.c##
else if (hptr->h_name != NULL)## 30 ##src/names/hostent2.c##
printf("\tname = %s\n", hptr->h_name);## 31 ##src/names/hostent2.c##
else## 32 ##src/names/hostent2.c##
printf("\t(no hostname returned by gethostbyaddr)\n");## 33 ##src/names/hostent2.c##
}## 34 ##src/names/hostent2.c##
break;## 35 ##src/names/hostent2.c##
default:## 36 ##src/names/hostent2.c##
err_ret("unknown address type");## 37 ##src/names/hostent2.c##
break;## 38 ##src/names/hostent2.c##
}## 39 ##src/names/hostent2.c##
}## 40 ##src/names/hostent2.c##
exit(0);## 41 ##src/names/hostent2.c##
}## 42 ##src/names/hostent2.c##