1 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | static const char rcsid[] = "$Id: athinfo.c,v 1.2 1999-09-15 23:56:19 danw Exp $"; |
---|
17 | |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/socket.h> |
---|
20 | #include <netinet/in.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <unistd.h> |
---|
23 | #include <errno.h> |
---|
24 | #include <netdb.h> |
---|
25 | #include <string.h> |
---|
26 | |
---|
27 | #define ATHINFO_FALLBACK_PORT 49155 |
---|
28 | |
---|
29 | int main(int argc, char **argv) |
---|
30 | { |
---|
31 | int s, count, pos, len; |
---|
32 | const char *host, *query; |
---|
33 | struct hostent *hostent; |
---|
34 | struct servent *servent; |
---|
35 | struct sockaddr_in sin; |
---|
36 | char buf[8192]; |
---|
37 | |
---|
38 | if (argc != 3) |
---|
39 | { |
---|
40 | fprintf(stderr, "athinfo: usage: %s host query\n", argv[0]); |
---|
41 | return 1; |
---|
42 | } |
---|
43 | host = argv[1]; |
---|
44 | query = argv[2]; |
---|
45 | |
---|
46 | /* Get host address and port. */ |
---|
47 | hostent = gethostbyname(host); |
---|
48 | if (!hostent || hostent->h_addrtype != AF_INET) |
---|
49 | { |
---|
50 | fprintf(stderr, "athinfo: host %s not found\n", host); |
---|
51 | return 1; |
---|
52 | } |
---|
53 | servent = getservbyname("athinfo", "tcp"); |
---|
54 | |
---|
55 | /* Create the client socket. */ |
---|
56 | s = socket(AF_INET, SOCK_STREAM, 0); |
---|
57 | if (s == -1) |
---|
58 | { |
---|
59 | fprintf(stderr, "athinfo: socket: %s\n", strerror(errno)); |
---|
60 | return 1; |
---|
61 | } |
---|
62 | |
---|
63 | /* Connect the socket. */ |
---|
64 | memset(&sin, 0, sizeof(sin)); |
---|
65 | sin.sin_family = AF_INET; |
---|
66 | memcpy(&sin.sin_addr, hostent->h_addr, sizeof(sin.sin_addr)); |
---|
67 | sin.sin_port = (servent) ? servent->s_port : htons(ATHINFO_FALLBACK_PORT); |
---|
68 | if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) |
---|
69 | { |
---|
70 | fprintf(stderr, "athinfo: connect: %s\n", strerror(errno)); |
---|
71 | return 1; |
---|
72 | } |
---|
73 | |
---|
74 | /* Write the query. */ |
---|
75 | len = strlen(query); |
---|
76 | pos = 0; |
---|
77 | while (pos < len) |
---|
78 | { |
---|
79 | count = write(s, query + pos, len - pos); |
---|
80 | if (count == -1 && errno != EINTR) |
---|
81 | { |
---|
82 | fprintf(stderr, "athinfo: write: %s\n", strerror(errno)); |
---|
83 | return 1; |
---|
84 | } |
---|
85 | else if (count > 0) |
---|
86 | pos += count; |
---|
87 | } |
---|
88 | do |
---|
89 | { |
---|
90 | count = write(s, "\n", 1); |
---|
91 | if (count == -1 && errno != EINTR) |
---|
92 | { |
---|
93 | fprintf(stderr, "athinfo: write: %s\n", strerror(errno)); |
---|
94 | return 1; |
---|
95 | } |
---|
96 | } |
---|
97 | while (count != 1); |
---|
98 | |
---|
99 | /* Display the response. */ |
---|
100 | while (1) |
---|
101 | { |
---|
102 | /* Read some data. */ |
---|
103 | count = read(s, buf, sizeof(buf)); |
---|
104 | if (count == -1 && errno == EINTR) |
---|
105 | continue; |
---|
106 | if (count == -1) |
---|
107 | { |
---|
108 | fprintf(stderr, "athinfo: read: %s\n", strerror(errno)); |
---|
109 | return 1; |
---|
110 | } |
---|
111 | if (count == 0) |
---|
112 | break; |
---|
113 | |
---|
114 | /* Write it to stdout. */ |
---|
115 | len = count; |
---|
116 | pos = 0; |
---|
117 | while (pos < len) |
---|
118 | { |
---|
119 | count = write(STDOUT_FILENO, buf + pos, len - pos); |
---|
120 | if (count == -1 && errno != EINTR) |
---|
121 | { |
---|
122 | fprintf(stderr, "athinfo: write: %s\n", strerror(errno)); |
---|
123 | return 1; |
---|
124 | } |
---|
125 | else if (count > 0) |
---|
126 | pos += count; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | close(s); |
---|
131 | return 0; |
---|
132 | } |
---|