term.c (5543:3af77710f397) term.c (5579:29cd33ac16cb)
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()
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 polls on the network file descriptor and stdin.
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
143 */
144void
145readwrite(int nfd)
146{
144 */
145void
146readwrite(int nfd)
147{
147 struct pollfd pfd[2];
148 fd_set read_fds;
148 char buf[BUFSIZ];
149 char buf[BUFSIZ];
149 int wfd = fileno(stdin), n, ret;
150 int wfd = fileno(stdin), n, ret, max_fd;
150 int lfd = fileno(stdout);
151 int escape = 0;
151 int lfd = fileno(stdout);
152 int escape = 0;
153 struct timeval timeout;
152
154
153 /* Setup Network FD */
154 pfd[0].fd = nfd;
155 pfd[0].events = POLLIN;
155 if (nfd == -1)
156 return;
156
157
157 /* Setup STDIN FD */
158 pfd[1].fd = wfd;
159 pfd[1].events = POLLIN;
158 FD_ZERO(&read_fds);
160
159
161 while (pfd[0].fd != -1) {
162 if ((n = poll(pfd, 2, -1)) < 0) {
160 FD_SET(wfd, &read_fds);
161 FD_SET(nfd, &read_fds);
162 max_fd = nfd + 1;
163
164 timeout.tv_sec = 1;
165 timeout.tv_usec = 0;
166
167 while (1) {
168 n = select(max_fd, &read_fds, NULL, NULL, &timeout);
169 if (n < 0) {
163 close(nfd);
170 close(nfd);
164 err(1, "Polling Error");
171 perror("Select Error:");
165 }
166
172 }
173
167 if (n == 0)
174 if (n == 0) {
175 if (read(nfd, buf, 0) < 0)
176 return;
177 goto setup_select;
178 }
179
180 if (read(nfd, buf, 0) < 0)
168 return;
169
181 return;
182
170 if (pfd[0].revents & POLLIN) {
183 if (FD_ISSET(nfd, &read_fds)) {
171 if ((n = read(nfd, buf, sizeof(buf))) < 0)
172 return;
173 else if (n == 0) {
174 shutdown(nfd, SHUT_RD);
184 if ((n = read(nfd, buf, sizeof(buf))) < 0)
185 return;
186 else if (n == 0) {
187 shutdown(nfd, SHUT_RD);
175 pfd[0].fd = -1;
176 pfd[0].events = 0;
188 return;
177 } else {
178 if ((ret = atomicio(write, lfd, buf, n)) != n)
179 return;
180 }
181 }
182
189 } else {
190 if ((ret = atomicio(write, lfd, buf, n)) != n)
191 return;
192 }
193 }
194
183 if (pfd[1].revents & POLLIN) {
195 if (FD_ISSET(wfd, &read_fds)) {
184 if ((n = read(wfd, buf, sizeof(buf))) < 0)
185 return;
186 else if (n == 0) {
187 shutdown(nfd, SHUT_WR);
196 if ((n = read(wfd, buf, sizeof(buf))) < 0)
197 return;
198 else if (n == 0) {
199 shutdown(nfd, SHUT_WR);
188 pfd[1].fd = -1;
189 pfd[1].events = 0;
190 } else {
191 if (escape) {
192 char buf2[] = "~";
193 if (*buf == '.') {
194 printf("quit!\n");
195 return;
196 }
197 escape = 0;
198 if (*buf != '~' &&
199 (ret = atomicio(write, nfd, buf2, 1)) != n)
200 return;
201 } else {
202 escape = (*buf == '~');
203 if (escape)
204 continue;
205 }
206
207 if ((ret = atomicio(write, nfd, buf, n)) != n)
208 return;
209 }
210 }
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 }
211 }
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
212}
213
214void
215usage(int ret)
216{
217 fprintf(stderr, "usage: %s hostname port\n", progname);
218 if (ret)
219 exit(1);
220}
221
222/*
223 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
224 * All rights reserved.
225 *
226 * Redistribution and use in source and binary forms, with or without
227 * modification, are permitted provided that the following conditions
228 * are met:
229 * 1. Redistributions of source code must retain the above copyright
230 * notice, this list of conditions and the following disclaimer.
231 * 2. Redistributions in binary form must reproduce the above copyright
232 * notice, this list of conditions and the following disclaimer in the
233 * documentation and/or other materials provided with the distribution.
234 *
235 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
236 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
238 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
239 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
240 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
243 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
244 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245 */
246
247/*
248 * ensure all of data on socket comes through. f==read || f==write
249 */
250ssize_t
251atomicio(ssize_t (*f) (), int fd, void *_s, size_t n)
252{
253 char *s = _s;
254 ssize_t res, pos = 0;
255
256 while (n > pos) {
257 res = (f) (fd, s + pos, n - pos);
258 switch (res) {
259 case -1:
260 if (errno == EINTR || errno == EAGAIN)
261 continue;
262 case 0:
263 return (res);
264 default:
265 pos += res;
266 }
267 }
268 return (pos);
269}
270
271/*
272 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
273 *
274 * Permission to use, copy, modify, and distribute this software for any
275 * purpose with or without fee is hereby granted, provided that the above
276 * copyright notice and this permission notice appear in all copies.
277 *
278 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
279 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
280 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
281 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
282 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
283 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
284 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
285 */
286
287void
288raw_term()
289{
290 struct termios ios;
291
292 if (tcgetattr(STDIN_FILENO, &ios) < 0)
293 errx(1, "tcgetagttr\n");
294
295 memcpy(&saved_ios, &ios, sizeof(struct termios));
296
297 ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
298 ios.c_oflag &= ~(OPOST);
299 ios.c_oflag &= (ONLCR);
300 ios.c_lflag &= ~(ISIG|ICANON|ECHO);
301 ios.c_cc[VMIN] = 1;
302 ios.c_cc[VTIME] = 0;
303
304 if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0)
305 errx(1, "tcsetattr\n");
306
307 atexit(restore_term);
308}
309
310void
311restore_term()
312{
313 tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios);
314}
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}