term.c (5579:29cd33ac16cb) term.c (5580:6be5ac0eb1ea)
1/* $OpenBSD: netcat.c,v 1.57 2002/12/30 18:00:18 stevesk Exp $ */
2/*
3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/termios.h>
32#include <sys/time.h>
33#include <sys/un.h>
34
35#include <netinet/in.h>
36#include <arpa/telnet.h>
37
38#include <err.h>
39#include <errno.h>
40#include <netdb.h>
41#include <poll.h>
42#include <stdarg.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47#include <fcntl.h>
48
49ssize_t atomicio(ssize_t (*)(), int, void *, size_t);
50void readwrite(int);
51int remote_connect(char *, char *, struct addrinfo);
52
53struct termios saved_ios;
54void raw_term();
55void restore_term();
56
57char progname[256];
58void usage(int);
59
60int
61main(int argc, char *argv[])
62{
63 int ch, s, ret;
64 char *host, *port, *endp;
65 struct addrinfo hints;
66 socklen_t len;
67
68 ret = 1;
69 s = 0;
70 host = NULL;
71 port = NULL;
72 endp = NULL;
73
74 strncpy(progname, argv[0], sizeof progname);
75
76 /* Cruft to make sure options are clean, and used properly. */
77 if (argc == 2) {
78 host = "localhost";
79 port = argv[1];
80 } else if (argc == 3) {
81 host = argv[1];
82 port = argv[2];
83 } else {
84 usage(1);
85 }
86
87 if (!isatty(STDIN_FILENO))
88 errx(1, "not attached to a terminal");
89
90 raw_term();
91
92 /* Initialize addrinfo structure */
93 memset(&hints, 0, sizeof(struct addrinfo));
94 hints.ai_family = AF_UNSPEC;
95 hints.ai_socktype = SOCK_STREAM;
96 hints.ai_protocol = IPPROTO_TCP;
97
98 s = remote_connect(host, port, hints);
99 ret = 0;
100 readwrite(s);
101
102 if (s)
103 close(s);
104
105 exit(ret);
106}
107
108/*
109 * remote_connect()
110 * Return's a socket connected to a remote host. Properly bind's to a local
111 * port or source address if needed. Return's -1 on failure.
112 */
113int
114remote_connect(char *host, char *port, struct addrinfo hints)
115{
116 struct addrinfo *res, *res0;
117 int s, error;
118
119 if ((error = getaddrinfo(host, port, &hints, &res)))
120 errx(1, "getaddrinfo: %s", gai_strerror(error));
121
122 res0 = res;
123 do {
124 if ((s = socket(res0->ai_family, res0->ai_socktype,
125 res0->ai_protocol)) < 0)
126 continue;
127
128 if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
129 break;
130
131 close(s);
132 s = -1;
133 } while ((res0 = res0->ai_next) != NULL);
134
135 freeaddrinfo(res);
136
137 return (s);
138}
139
140/*
141 * readwrite()
142 * Loop that selects on the network file descriptor and stdin.
143 * Changed from poll() by Ali Saidi to make work on Mac OS X >= 10.4
144 */
145void
146readwrite(int nfd)
147{
148 fd_set read_fds;
149 char buf[BUFSIZ];
150 int wfd = fileno(stdin), n, ret, max_fd;
151 int lfd = fileno(stdout);
152 int escape = 0;
153 struct timeval timeout;
154
155 if (nfd == -1)
156 return;
157
1/* $OpenBSD: netcat.c,v 1.57 2002/12/30 18:00:18 stevesk Exp $ */
2/*
3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/termios.h>
32#include <sys/time.h>
33#include <sys/un.h>
34
35#include <netinet/in.h>
36#include <arpa/telnet.h>
37
38#include <err.h>
39#include <errno.h>
40#include <netdb.h>
41#include <poll.h>
42#include <stdarg.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47#include <fcntl.h>
48
49ssize_t atomicio(ssize_t (*)(), int, void *, size_t);
50void readwrite(int);
51int remote_connect(char *, char *, struct addrinfo);
52
53struct termios saved_ios;
54void raw_term();
55void restore_term();
56
57char progname[256];
58void usage(int);
59
60int
61main(int argc, char *argv[])
62{
63 int ch, s, ret;
64 char *host, *port, *endp;
65 struct addrinfo hints;
66 socklen_t len;
67
68 ret = 1;
69 s = 0;
70 host = NULL;
71 port = NULL;
72 endp = NULL;
73
74 strncpy(progname, argv[0], sizeof progname);
75
76 /* Cruft to make sure options are clean, and used properly. */
77 if (argc == 2) {
78 host = "localhost";
79 port = argv[1];
80 } else if (argc == 3) {
81 host = argv[1];
82 port = argv[2];
83 } else {
84 usage(1);
85 }
86
87 if (!isatty(STDIN_FILENO))
88 errx(1, "not attached to a terminal");
89
90 raw_term();
91
92 /* Initialize addrinfo structure */
93 memset(&hints, 0, sizeof(struct addrinfo));
94 hints.ai_family = AF_UNSPEC;
95 hints.ai_socktype = SOCK_STREAM;
96 hints.ai_protocol = IPPROTO_TCP;
97
98 s = remote_connect(host, port, hints);
99 ret = 0;
100 readwrite(s);
101
102 if (s)
103 close(s);
104
105 exit(ret);
106}
107
108/*
109 * remote_connect()
110 * Return's a socket connected to a remote host. Properly bind's to a local
111 * port or source address if needed. Return's -1 on failure.
112 */
113int
114remote_connect(char *host, char *port, struct addrinfo hints)
115{
116 struct addrinfo *res, *res0;
117 int s, error;
118
119 if ((error = getaddrinfo(host, port, &hints, &res)))
120 errx(1, "getaddrinfo: %s", gai_strerror(error));
121
122 res0 = res;
123 do {
124 if ((s = socket(res0->ai_family, res0->ai_socktype,
125 res0->ai_protocol)) < 0)
126 continue;
127
128 if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
129 break;
130
131 close(s);
132 s = -1;
133 } while ((res0 = res0->ai_next) != NULL);
134
135 freeaddrinfo(res);
136
137 return (s);
138}
139
140/*
141 * readwrite()
142 * Loop that selects on the network file descriptor and stdin.
143 * Changed from poll() by Ali Saidi to make work on Mac OS X >= 10.4
144 */
145void
146readwrite(int nfd)
147{
148 fd_set read_fds;
149 char buf[BUFSIZ];
150 int wfd = fileno(stdin), n, ret, max_fd;
151 int lfd = fileno(stdout);
152 int escape = 0;
153 struct timeval timeout;
154
155 if (nfd == -1)
156 return;
157
158 FD_ZERO(&read_fds);
159
160 FD_SET(wfd, &read_fds);
161 FD_SET(nfd, &read_fds);
162 max_fd = nfd + 1;
163
158 max_fd = nfd + 1;
159
164 timeout.tv_sec = 1;
165 timeout.tv_usec = 0;
166
167 while (1) {
160 while (1) {
161 FD_ZERO(&read_fds);
162 FD_SET(wfd, &read_fds);
163 FD_SET(nfd, &read_fds);
164 timeout.tv_sec = 1;
165 timeout.tv_usec = 0;
166
168 n = select(max_fd, &read_fds, NULL, NULL, &timeout);
169 if (n < 0) {
170 close(nfd);
171 perror("Select Error:");
172 }
173
174 if (n == 0) {
175 if (read(nfd, buf, 0) < 0)
176 return;
167 n = select(max_fd, &read_fds, NULL, NULL, &timeout);
168 if (n < 0) {
169 close(nfd);
170 perror("Select Error:");
171 }
172
173 if (n == 0) {
174 if (read(nfd, buf, 0) < 0)
175 return;
177 goto setup_select;
176 continue;
178 }
179
180 if (read(nfd, buf, 0) < 0)
181 return;
182
183 if (FD_ISSET(nfd, &read_fds)) {
184 if ((n = read(nfd, buf, sizeof(buf))) < 0)
185 return;
186 else if (n == 0) {
187 shutdown(nfd, SHUT_RD);
188 return;
189 } else {
190 if ((ret = atomicio(write, lfd, buf, n)) != n)
191 return;
192 }
193 }
194
195 if (FD_ISSET(wfd, &read_fds)) {
196 if ((n = read(wfd, buf, sizeof(buf))) < 0)
197 return;
198 else if (n == 0) {
199 shutdown(nfd, SHUT_WR);
200 } else {
201 if (escape) {
202 char buf2[] = "~";
203 if (*buf == '.') {
204 printf("quit!\n");
205 return;
206 }
207 escape = 0;
208 if (*buf != '~' &&
209 (ret = atomicio(write, nfd, buf2, 1)) != n)
210 return;
211 } else {
212 escape = (*buf == '~');
213 if (escape)
214 continue;
215 }
216
217 if ((ret = atomicio(write, nfd, buf, n)) != n)
218 return;
219 }
220 }
177 }
178
179 if (read(nfd, buf, 0) < 0)
180 return;
181
182 if (FD_ISSET(nfd, &read_fds)) {
183 if ((n = read(nfd, buf, sizeof(buf))) < 0)
184 return;
185 else if (n == 0) {
186 shutdown(nfd, SHUT_RD);
187 return;
188 } else {
189 if ((ret = atomicio(write, lfd, buf, n)) != n)
190 return;
191 }
192 }
193
194 if (FD_ISSET(wfd, &read_fds)) {
195 if ((n = read(wfd, buf, sizeof(buf))) < 0)
196 return;
197 else if (n == 0) {
198 shutdown(nfd, SHUT_WR);
199 } else {
200 if (escape) {
201 char buf2[] = "~";
202 if (*buf == '.') {
203 printf("quit!\n");
204 return;
205 }
206 escape = 0;
207 if (*buf != '~' &&
208 (ret = atomicio(write, nfd, buf2, 1)) != n)
209 return;
210 } else {
211 escape = (*buf == '~');
212 if (escape)
213 continue;
214 }
215
216 if ((ret = atomicio(write, nfd, buf, n)) != n)
217 return;
218 }
219 }
221setup_select:
222 FD_ZERO(&read_fds);
223 FD_SET(wfd, &read_fds);
224 FD_SET(nfd, &read_fds);
225 timeout.tv_sec = 1;
226 timeout.tv_usec = 0;
227 } // while
228}
229
230void
231usage(int ret)
232{
233 fprintf(stderr, "usage: %s hostname port\n", progname);
234 if (ret)
235 exit(1);
236}
237
238/*
239 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
240 * All rights reserved.
241 *
242 * Redistribution and use in source and binary forms, with or without
243 * modification, are permitted provided that the following conditions
244 * are met:
245 * 1. Redistributions of source code must retain the above copyright
246 * notice, this list of conditions and the following disclaimer.
247 * 2. Redistributions in binary form must reproduce the above copyright
248 * notice, this list of conditions and the following disclaimer in the
249 * documentation and/or other materials provided with the distribution.
250 *
251 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
252 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
253 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
254 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
255 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
256 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
257 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
260 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261 */
262
263/*
264 * ensure all of data on socket comes through. f==read || f==write
265 */
266ssize_t
267atomicio(ssize_t (*f) (), int fd, void *_s, size_t n)
268{
269 char *s = _s;
270 ssize_t res, pos = 0;
271
272 while (n > pos) {
273 res = (f) (fd, s + pos, n - pos);
274 switch (res) {
275 case -1:
276 if (errno == EINTR || errno == EAGAIN)
277 continue;
278 case 0:
279 return (res);
280 default:
281 pos += res;
282 }
283 }
284 return (pos);
285}
286
287/*
288 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
289 *
290 * Permission to use, copy, modify, and distribute this software for any
291 * purpose with or without fee is hereby granted, provided that the above
292 * copyright notice and this permission notice appear in all copies.
293 *
294 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
295 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
296 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
297 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
298 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
299 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
300 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
301 */
302
303void
304raw_term()
305{
306 struct termios ios;
307
308 if (tcgetattr(STDIN_FILENO, &ios) < 0)
309 errx(1, "tcgetagttr\n");
310
311 memcpy(&saved_ios, &ios, sizeof(struct termios));
312
313 ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
314 ios.c_oflag &= ~(OPOST);
315 ios.c_oflag &= (ONLCR);
316 ios.c_lflag &= ~(ISIG|ICANON|ECHO);
317 ios.c_cc[VMIN] = 1;
318 ios.c_cc[VTIME] = 0;
319
320 if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0)
321 errx(1, "tcsetattr\n");
322
323 atexit(restore_term);
324}
325
326void
327restore_term()
328{
329 tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios);
330}
220 } // while
221}
222
223void
224usage(int ret)
225{
226 fprintf(stderr, "usage: %s hostname port\n", progname);
227 if (ret)
228 exit(1);
229}
230
231/*
232 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
233 * All rights reserved.
234 *
235 * Redistribution and use in source and binary forms, with or without
236 * modification, are permitted provided that the following conditions
237 * are met:
238 * 1. Redistributions of source code must retain the above copyright
239 * notice, this list of conditions and the following disclaimer.
240 * 2. Redistributions in binary form must reproduce the above copyright
241 * notice, this list of conditions and the following disclaimer in the
242 * documentation and/or other materials provided with the distribution.
243 *
244 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
245 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
246 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
248 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
249 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
250 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
253 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
254 */
255
256/*
257 * ensure all of data on socket comes through. f==read || f==write
258 */
259ssize_t
260atomicio(ssize_t (*f) (), int fd, void *_s, size_t n)
261{
262 char *s = _s;
263 ssize_t res, pos = 0;
264
265 while (n > pos) {
266 res = (f) (fd, s + pos, n - pos);
267 switch (res) {
268 case -1:
269 if (errno == EINTR || errno == EAGAIN)
270 continue;
271 case 0:
272 return (res);
273 default:
274 pos += res;
275 }
276 }
277 return (pos);
278}
279
280/*
281 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
282 *
283 * Permission to use, copy, modify, and distribute this software for any
284 * purpose with or without fee is hereby granted, provided that the above
285 * copyright notice and this permission notice appear in all copies.
286 *
287 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
288 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
289 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
290 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
291 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
292 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
293 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
294 */
295
296void
297raw_term()
298{
299 struct termios ios;
300
301 if (tcgetattr(STDIN_FILENO, &ios) < 0)
302 errx(1, "tcgetagttr\n");
303
304 memcpy(&saved_ios, &ios, sizeof(struct termios));
305
306 ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
307 ios.c_oflag &= ~(OPOST);
308 ios.c_oflag &= (ONLCR);
309 ios.c_lflag &= ~(ISIG|ICANON|ECHO);
310 ios.c_cc[VMIN] = 1;
311 ios.c_cc[VTIME] = 0;
312
313 if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0)
314 errx(1, "tcsetattr\n");
315
316 atexit(restore_term);
317}
318
319void
320restore_term()
321{
322 tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios);
323}