term.c (1760:48ebe25cedc8) term.c (3343:539f62f485dd)
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 *

--- 46 unchanged lines hidden (view full) ---

55void restore_term();
56
57char progname[256];
58void usage(int);
59
60int
61main(int argc, char *argv[])
62{
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 *

--- 46 unchanged lines hidden (view full) ---

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;
63 int ch, s, ret;
64 char *host, *port, *endp;
65 struct addrinfo hints;
66 socklen_t len;
67
67
68 ret = 1;
69 s = 0;
70 host = NULL;
71 port = NULL;
72 endp = NULL;
68 ret = 1;
69 s = 0;
70 host = NULL;
71 port = NULL;
72 endp = NULL;
73
73
74 strncpy(progname, argv[0], sizeof progname);
74 strncpy(progname, argv[0], sizeof progname);
75
75
76 /* Cruft to make sure options are clean, and used properly. */
77 if (argc != 3 || !argv[1] || !argv[2])
78 usage(1);
79
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) {
80 host = argv[1];
81 port = argv[2];
81 host = argv[1];
82 port = argv[2];
83 } else {
84 usage(1);
85 }
82
86
87 if (!isatty(STDIN_FILENO))
88 errx(1, "not attached to a terminal");
83
89
84 if (!isatty(STDIN_FILENO))
85 errx(1, "not attached to a terminal");
90 raw_term();
86
91
87 raw_term();
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;
88
97
89 /* Initialize addrinfo structure */
90 memset(&hints, 0, sizeof(struct addrinfo));
91 hints.ai_family = AF_UNSPEC;
92 hints.ai_socktype = SOCK_STREAM;
93 hints.ai_protocol = IPPROTO_TCP;
98 s = remote_connect(host, port, hints);
99 ret = 0;
100 readwrite(s);
94
101
95 s = remote_connect(host, port, hints);
96 ret = 0;
97 readwrite(s);
102 if (s)
103 close(s);
98
104
99 if (s)
100 close(s);
101
102 exit(ret);
105 exit(ret);
103}
104
105/*
106 * remote_connect()
107 * Return's a socket connected to a remote host. Properly bind's to a local
108 * port or source address if needed. Return's -1 on failure.
109 */
110int
111remote_connect(char *host, char *port, struct addrinfo hints)
112{
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{
113 struct addrinfo *res, *res0;
114 int s, error;
116 struct addrinfo *res, *res0;
117 int s, error;
115
118
116 if ((error = getaddrinfo(host, port, &hints, &res)))
117 errx(1, "getaddrinfo: %s", gai_strerror(error));
119 if ((error = getaddrinfo(host, port, &hints, &res)))
120 errx(1, "getaddrinfo: %s", gai_strerror(error));
118
121
119 res0 = res;
120 do {
121 if ((s = socket(res0->ai_family, res0->ai_socktype,
122 res0->ai_protocol)) < 0)
123 continue;
122 res0 = res;
123 do {
124 if ((s = socket(res0->ai_family, res0->ai_socktype,
125 res0->ai_protocol)) < 0)
126 continue;
124
127
125 if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
126 break;
128 if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
129 break;
127
130
128 close(s);
129 s = -1;
130 } while ((res0 = res0->ai_next) != NULL);
131 close(s);
132 s = -1;
133 } while ((res0 = res0->ai_next) != NULL);
131
134
132 freeaddrinfo(res);
135 freeaddrinfo(res);
133
136
134 return (s);
137 return (s);
135}
136
137/*
138 * readwrite()
139 * Loop that polls on the network file descriptor and stdin.
140 */
141void
142readwrite(int nfd)
143{
138}
139
140/*
141 * readwrite()
142 * Loop that polls on the network file descriptor and stdin.
143 */
144void
145readwrite(int nfd)
146{
144 struct pollfd pfd[2];
145 char buf[BUFSIZ];
146 int wfd = fileno(stdin), n, ret;
147 int lfd = fileno(stdout);
148 int escape = 0;
147 struct pollfd pfd[2];
148 char buf[BUFSIZ];
149 int wfd = fileno(stdin), n, ret;
150 int lfd = fileno(stdout);
151 int escape = 0;
149
152
150 /* Setup Network FD */
151 pfd[0].fd = nfd;
152 pfd[0].events = POLLIN;
153 /* Setup Network FD */
154 pfd[0].fd = nfd;
155 pfd[0].events = POLLIN;
153
156
154 /* Setup STDIN FD */
155 pfd[1].fd = wfd;
156 pfd[1].events = POLLIN;
157 /* Setup STDIN FD */
158 pfd[1].fd = wfd;
159 pfd[1].events = POLLIN;
157
160
158 while (pfd[0].fd != -1) {
159 if ((n = poll(pfd, 2, -1)) < 0) {
160 close(nfd);
161 err(1, "Polling Error");
162 }
161 while (pfd[0].fd != -1) {
162 if ((n = poll(pfd, 2, -1)) < 0) {
163 close(nfd);
164 err(1, "Polling Error");
165 }
163
166
164 if (n == 0)
165 return;
167 if (n == 0)
168 return;
166
169
167 if (pfd[0].revents & POLLIN) {
168 if ((n = read(nfd, buf, sizeof(buf))) < 0)
169 return;
170 else if (n == 0) {
171 shutdown(nfd, SHUT_RD);
172 pfd[0].fd = -1;
173 pfd[0].events = 0;
174 } else {
175 if ((ret = atomicio(write, lfd, buf, n)) != n)
176 return;
177 }
178 }
170 if (pfd[0].revents & POLLIN) {
171 if ((n = read(nfd, buf, sizeof(buf))) < 0)
172 return;
173 else if (n == 0) {
174 shutdown(nfd, SHUT_RD);
175 pfd[0].fd = -1;
176 pfd[0].events = 0;
177 } else {
178 if ((ret = atomicio(write, lfd, buf, n)) != n)
179 return;
180 }
181 }
179
182
180 if (pfd[1].revents & POLLIN) {
181 if ((n = read(wfd, buf, sizeof(buf))) < 0)
182 return;
183 else if (n == 0) {
184 shutdown(nfd, SHUT_WR);
185 pfd[1].fd = -1;
186 pfd[1].events = 0;
187 } else {
188 if (escape) {
189 char buf2[] = "~";
190 if (*buf == '.') {
191 printf("quit!\n");
192 return;
193 }
194 escape = 0;
195 if (*buf != '~' &&
196 (ret = atomicio(write, nfd, buf2, 1)) != n)
197 return;
198 } else {
199 escape = (*buf == '~');
200 if (escape)
201 continue;
202 }
203
204 if ((ret = atomicio(write, nfd, buf, n)) != n)
205 return;
206 }
183 if (pfd[1].revents & POLLIN) {
184 if ((n = read(wfd, buf, sizeof(buf))) < 0)
185 return;
186 else if (n == 0) {
187 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;
207 }
205 }
206
207 if ((ret = atomicio(write, nfd, buf, n)) != n)
208 return;
209 }
208 }
210 }
211 }
209}
210
211void
212usage(int ret)
213{
212}
213
214void
215usage(int ret)
216{
214 fprintf(stderr, "usage: %s hostname port\n", progname);
215 if (ret)
216 exit(1);
217 fprintf(stderr, "usage: %s hostname port\n", progname);
218 if (ret)
219 exit(1);
217}
218
219/*
220 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
221 * All rights reserved.
222 *
223 * Redistribution and use in source and binary forms, with or without
224 * modification, are permitted provided that the following conditions

--- 17 unchanged lines hidden (view full) ---

242 */
243
244/*
245 * ensure all of data on socket comes through. f==read || f==write
246 */
247ssize_t
248atomicio(ssize_t (*f) (), int fd, void *_s, size_t n)
249{
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

--- 17 unchanged lines hidden (view full) ---

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{
250 char *s = _s;
251 ssize_t res, pos = 0;
253 char *s = _s;
254 ssize_t res, pos = 0;
252
255
253 while (n > pos) {
254 res = (f) (fd, s + pos, n - pos);
255 switch (res) {
256 case -1:
257 if (errno == EINTR || errno == EAGAIN)
258 continue;
259 case 0:
260 return (res);
261 default:
262 pos += res;
263 }
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;
264 }
266 }
265 return (pos);
267 }
268 return (pos);
266}
267
268/*
269 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
270 *
271 * Permission to use, copy, modify, and distribute this software for any
272 * purpose with or without fee is hereby granted, provided that the above
273 * copyright notice and this permission notice appear in all copies.

--- 5 unchanged lines hidden (view full) ---

279 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
280 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
281 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
282 */
283
284void
285raw_term()
286{
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.

--- 5 unchanged lines hidden (view full) ---

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{
287 struct termios ios;
290 struct termios ios;
288
291
289 if (tcgetattr(STDIN_FILENO, &ios) < 0)
290 errx(1, "tcgetagttr\n");
292 if (tcgetattr(STDIN_FILENO, &ios) < 0)
293 errx(1, "tcgetagttr\n");
291
294
292 memcpy(&saved_ios, &ios, sizeof(struct termios));
295 memcpy(&saved_ios, &ios, sizeof(struct termios));
293
296
294 ios.c_iflag &= ~(ISTRIP|ICRNL|IGNCR|ICRNL|IXOFF|IXON);
295 ios.c_oflag &= ~(OPOST);
296 ios.c_oflag &= (ONLCR);
297 ios.c_lflag &= ~(ISIG|ICANON|ECHO);
298 ios.c_cc[VMIN] = 1;
299 ios.c_cc[VTIME] = 0;
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;
300
303
301 if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0)
302 errx(1, "tcsetattr\n");
304 if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0)
305 errx(1, "tcsetattr\n");
303
306
304 atexit(restore_term);
307 atexit(restore_term);
305}
306
307void
308restore_term()
309{
308}
309
310void
311restore_term()
312{
310 tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios);
313 tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios);
311}
314}