term.c revision 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 158 max_fd = nfd + 1; 159 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 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; 176 continue; 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 } 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} 324