1 /* $OpenBSD: netcat.c,v 1.57 2002/12/30 18:00:18 stevesk Exp $ */
3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <arpa/telnet.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <sys/termios.h>
34 #include <sys/types.h>
47 ssize_t
atomicio(ssize_t (*)(), int, void *, size_t);
49 int remote_connect(char *, char *, struct addrinfo
);
51 struct termios saved_ios
;
59 main(int argc
, char *argv
[])
62 char *host
, *port
, *endp
;
63 struct addrinfo hints
;
72 strncpy(progname
, argv
[0], sizeof progname
);
74 /* Cruft to make sure options are clean, and used properly. */
78 } else if (argc
== 3) {
85 if (!isatty(STDIN_FILENO
))
86 errx(1, "not attached to a terminal");
90 /* Initialize addrinfo structure */
91 memset(&hints
, 0, sizeof(struct addrinfo
));
92 hints
.ai_family
= AF_UNSPEC
;
93 hints
.ai_socktype
= SOCK_STREAM
;
94 hints
.ai_protocol
= IPPROTO_TCP
;
96 s
= remote_connect(host
, port
, hints
);
108 * Return's a socket connected to a remote host. Properly bind's to a local
109 * port or source address if needed. Return's -1 on failure.
112 remote_connect(char *host
, char *port
, struct addrinfo hints
)
114 struct addrinfo
*res
, *res0
;
117 if ((error
= getaddrinfo(host
, port
, &hints
, &res
)))
118 errx(1, "getaddrinfo: %s", gai_strerror(error
));
122 if ((s
= socket(res0
->ai_family
, res0
->ai_socktype
,
123 res0
->ai_protocol
)) < 0)
126 if (connect(s
, res0
->ai_addr
, res0
->ai_addrlen
) == 0)
131 } while ((res0
= res0
->ai_next
) != NULL
);
140 * Loop that selects on the network file descriptor and stdin.
141 * Changed from poll() by Ali Saidi to make work on Mac OS X >= 10.4
148 int wfd
= fileno(stdin
), n
, ret
, max_fd
;
149 int lfd
= fileno(stdout
);
151 struct timeval timeout
;
160 FD_SET(wfd
, &read_fds
);
161 FD_SET(nfd
, &read_fds
);
165 n
= select(max_fd
, &read_fds
, NULL
, NULL
, &timeout
);
168 perror("Select Error:");
172 if (read(nfd
, buf
, 0) < 0)
177 if (read(nfd
, buf
, 0) < 0)
180 if (FD_ISSET(nfd
, &read_fds
)) {
181 if ((n
= read(nfd
, buf
, sizeof(buf
))) < 0)
184 shutdown(nfd
, SHUT_RD
);
187 if ((ret
= atomicio(write
, lfd
, buf
, n
)) != n
)
192 if (FD_ISSET(wfd
, &read_fds
)) {
193 if ((n
= read(wfd
, buf
, sizeof(buf
))) < 0)
196 shutdown(nfd
, SHUT_WR
);
206 (ret
= atomicio(write
, nfd
, buf2
, 1)) != n
)
209 escape
= (*buf
== '~');
214 if ((ret
= atomicio(write
, nfd
, buf
, n
)) != n
)
224 fprintf(stderr
, "usage: %s hostname port\n", progname
);
230 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
231 * All rights reserved.
233 * Redistribution and use in source and binary forms, with or without
234 * modification, are permitted provided that the following conditions
236 * 1. Redistributions of source code must retain the above copyright
237 * notice, this list of conditions and the following disclaimer.
238 * 2. Redistributions in binary form must reproduce the above copyright
239 * notice, this list of conditions and the following disclaimer in the
240 * documentation and/or other materials provided with the distribution.
242 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
243 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
244 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
245 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
246 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
247 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
250 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
255 * ensure all of data on socket comes through. f==read || f==write
258 atomicio(ssize_t (*f
) (), int fd
, void *_s
, size_t n
)
261 ssize_t res
, pos
= 0;
264 res
= (f
) (fd
, s
+ pos
, n
- pos
);
267 if (errno
== EINTR
|| errno
== EAGAIN
)
279 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
281 * Permission to use, copy, modify, and distribute this software for any
282 * purpose with or without fee is hereby granted, provided that the above
283 * copyright notice and this permission notice appear in all copies.
285 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
286 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
287 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
288 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
289 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
290 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
291 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
299 if (tcgetattr(STDIN_FILENO
, &ios
) < 0)
300 errx(1, "tcgetagttr\n");
302 memcpy(&saved_ios
, &ios
, sizeof(struct termios
));
304 ios
.c_iflag
&= ~(ISTRIP
|ICRNL
|IGNCR
|ICRNL
|IXOFF
|IXON
);
305 ios
.c_oflag
&= ~(OPOST
);
306 ios
.c_oflag
&= (ONLCR
);
307 ios
.c_lflag
&= ~(ISIG
|ICANON
|ECHO
);
311 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &ios
) < 0)
312 errx(1, "tcsetattr\n");
314 atexit(restore_term
);
320 tcsetattr(STDIN_FILENO
, TCSANOW
, &saved_ios
);