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 <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/termios.h>
35 #include <netinet/in.h>
36 #include <arpa/telnet.h>
49 ssize_t
atomicio(ssize_t (*)(), int, void *, size_t);
51 int remote_connect(char *, char *, struct addrinfo
);
53 struct termios saved_ios
;
61 main(int argc
, char *argv
[])
64 char *host
, *port
, *endp
;
65 struct addrinfo hints
;
74 strncpy(progname
, argv
[0], sizeof progname
);
76 /* Cruft to make sure options are clean, and used properly. */
80 } else if (argc
== 3) {
87 if (!isatty(STDIN_FILENO
))
88 errx(1, "not attached to a terminal");
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
;
98 s
= remote_connect(host
, port
, hints
);
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.
114 remote_connect(char *host
, char *port
, struct addrinfo hints
)
116 struct addrinfo
*res
, *res0
;
119 if ((error
= getaddrinfo(host
, port
, &hints
, &res
)))
120 errx(1, "getaddrinfo: %s", gai_strerror(error
));
124 if ((s
= socket(res0
->ai_family
, res0
->ai_socktype
,
125 res0
->ai_protocol
)) < 0)
128 if (connect(s
, res0
->ai_addr
, res0
->ai_addrlen
) == 0)
133 } while ((res0
= res0
->ai_next
) != NULL
);
142 * Loop that polls on the network file descriptor and stdin.
147 struct pollfd pfd
[2];
149 int wfd
= fileno(stdin
), n
, ret
;
150 int lfd
= fileno(stdout
);
153 /* Setup Network FD */
155 pfd
[0].events
= POLLIN
;
159 pfd
[1].events
= POLLIN
;
161 while (pfd
[0].fd
!= -1) {
162 if ((n
= poll(pfd
, 2, -1)) < 0) {
164 err(1, "Polling Error");
170 if (pfd
[0].revents
& POLLIN
) {
171 if ((n
= read(nfd
, buf
, sizeof(buf
))) < 0)
174 shutdown(nfd
, SHUT_RD
);
178 if ((ret
= atomicio(write
, lfd
, buf
, n
)) != n
)
183 if (pfd
[1].revents
& POLLIN
) {
184 if ((n
= read(wfd
, buf
, sizeof(buf
))) < 0)
187 shutdown(nfd
, SHUT_WR
);
199 (ret
= atomicio(write
, nfd
, buf2
, 1)) != n
)
202 escape
= (*buf
== '~');
207 if ((ret
= atomicio(write
, nfd
, buf
, n
)) != n
)
217 fprintf(stderr
, "usage: %s hostname port\n", progname
);
223 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
224 * All rights reserved.
226 * Redistribution and use in source and binary forms, with or without
227 * modification, are permitted provided that the following conditions
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.
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.
248 * ensure all of data on socket comes through. f==read || f==write
251 atomicio(ssize_t (*f
) (), int fd
, void *_s
, size_t n
)
254 ssize_t res
, pos
= 0;
257 res
= (f
) (fd
, s
+ pos
, n
- pos
);
260 if (errno
== EINTR
|| errno
== EAGAIN
)
272 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
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.
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.
292 if (tcgetattr(STDIN_FILENO
, &ios
) < 0)
293 errx(1, "tcgetagttr\n");
295 memcpy(&saved_ios
, &ios
, sizeof(struct termios
));
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
);
304 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &ios
) < 0)
305 errx(1, "tcsetattr\n");
307 atexit(restore_term
);
313 tcsetattr(STDIN_FILENO
, TCSANOW
, &saved_ios
);