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. */
77 if (argc
!= 3 || !argv
[1] || !argv
[2])
84 if (!isatty(STDIN_FILENO
))
85 errx(1, "not attached to a terminal");
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
;
95 s
= remote_connect(host
, port
, hints
);
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.
111 remote_connect(char *host
, char *port
, struct addrinfo hints
)
113 struct addrinfo
*res
, *res0
;
116 if ((error
= getaddrinfo(host
, port
, &hints
, &res
)))
117 errx(1, "getaddrinfo: %s", gai_strerror(error
));
121 if ((s
= socket(res0
->ai_family
, res0
->ai_socktype
,
122 res0
->ai_protocol
)) < 0)
125 if (connect(s
, res0
->ai_addr
, res0
->ai_addrlen
) == 0)
130 } while ((res0
= res0
->ai_next
) != NULL
);
139 * Loop that polls on the network file descriptor and stdin.
144 struct pollfd pfd
[2];
146 int wfd
= fileno(stdin
), n
, ret
;
147 int lfd
= fileno(stdout
);
150 /* Setup Network FD */
152 pfd
[0].events
= POLLIN
;
156 pfd
[1].events
= POLLIN
;
158 while (pfd
[0].fd
!= -1) {
159 if ((n
= poll(pfd
, 2, -1)) < 0) {
161 err(1, "Polling Error");
167 if (pfd
[0].revents
& POLLIN
) {
168 if ((n
= read(nfd
, buf
, sizeof(buf
))) < 0)
171 shutdown(nfd
, SHUT_RD
);
175 if ((ret
= atomicio(write
, lfd
, buf
, n
)) != n
)
180 if (pfd
[1].revents
& POLLIN
) {
181 if ((n
= read(wfd
, buf
, sizeof(buf
))) < 0)
184 shutdown(nfd
, SHUT_WR
);
196 (ret
= atomicio(write
, nfd
, buf2
, 1)) != n
)
199 escape
= (*buf
== '~');
204 if ((ret
= atomicio(write
, nfd
, buf
, n
)) != n
)
214 fprintf(stderr
, "usage: %s hostname port\n", progname
);
220 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
221 * All rights reserved.
223 * Redistribution and use in source and binary forms, with or without
224 * modification, are permitted provided that the following conditions
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.
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.
245 * ensure all of data on socket comes through. f==read || f==write
248 atomicio(ssize_t (*f
) (), int fd
, void *_s
, size_t n
)
251 ssize_t res
, pos
= 0;
254 res
= (f
) (fd
, s
+ pos
, n
- pos
);
257 if (errno
== EINTR
|| errno
== EAGAIN
)
269 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
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.
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.
289 if (tcgetattr(STDIN_FILENO
, &ios
) < 0)
290 errx(1, "tcgetagttr\n");
292 memcpy(&saved_ios
, &ios
, sizeof(struct termios
));
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
);
301 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &ios
) < 0)
302 errx(1, "tcsetattr\n");
304 atexit(restore_term
);
310 tcsetattr(STDIN_FILENO
, TCSANOW
, &saved_ios
);