term.c revision 1760:48ebe25cedc8
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 != 3 || !argv[1] || !argv[2])
78                usage(1);
79
80        host = argv[1];
81        port = argv[2];
82
83
84        if (!isatty(STDIN_FILENO))
85                errx(1, "not attached to a terminal");
86
87        raw_term();
88
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;
94
95        s = remote_connect(host, port, hints);
96        ret = 0;
97        readwrite(s);
98
99        if (s)
100                close(s);
101
102        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{
113        struct addrinfo *res, *res0;
114        int s, error;
115
116        if ((error = getaddrinfo(host, port, &hints, &res)))
117                errx(1, "getaddrinfo: %s", gai_strerror(error));
118
119        res0 = res;
120        do {
121                if ((s = socket(res0->ai_family, res0->ai_socktype,
122                    res0->ai_protocol)) < 0)
123                        continue;
124
125                if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
126                        break;
127
128                close(s);
129                s = -1;
130        } while ((res0 = res0->ai_next) != NULL);
131
132        freeaddrinfo(res);
133
134        return (s);
135}
136
137/*
138 * readwrite()
139 * Loop that polls on the network file descriptor and stdin.
140 */
141void
142readwrite(int nfd)
143{
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;
149
150        /* Setup Network FD */
151        pfd[0].fd = nfd;
152        pfd[0].events = POLLIN;
153
154        /* Setup STDIN FD */
155        pfd[1].fd = wfd;
156        pfd[1].events = POLLIN;
157
158        while (pfd[0].fd != -1) {
159                if ((n = poll(pfd, 2, -1)) < 0) {
160                        close(nfd);
161                        err(1, "Polling Error");
162                }
163
164                if (n == 0)
165                        return;
166
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                }
179
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                        }
207                }
208        }
209}
210
211void
212usage(int ret)
213{
214        fprintf(stderr, "usage: %s hostname port\n", progname);
215        if (ret)
216                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
225 * are met:
226 * 1. Redistributions of source code must retain the above copyright
227 *    notice, this list of conditions and the following disclaimer.
228 * 2. Redistributions in binary form must reproduce the above copyright
229 *    notice, this list of conditions and the following disclaimer in the
230 *    documentation and/or other materials provided with the distribution.
231 *
232 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
233 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
234 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
235 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
236 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
237 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
239 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
240 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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{
250        char *s = _s;
251        ssize_t res, pos = 0;
252
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                }
264        }
265        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.
274 *
275 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
276 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
277 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
278 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
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{
287        struct termios ios;
288
289        if (tcgetattr(STDIN_FILENO, &ios) < 0)
290            errx(1, "tcgetagttr\n");
291
292        memcpy(&saved_ios, &ios, sizeof(struct termios));
293
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;
300
301        if (tcsetattr(STDIN_FILENO, TCSANOW, &ios) < 0)
302            errx(1, "tcsetattr\n");
303
304        atexit(restore_term);
305}
306
307void
308restore_term()
309{
310        tcsetattr(STDIN_FILENO, TCSANOW, &saved_ios);
311}
312