2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2000-05-13 01:11:47 +05:30
|
|
|
/* nc: mini-netcat - built from the ground up for LRP
|
1999-10-05 21:54:54 +05:30
|
|
|
Copyright (C) 1998 Charles P. Wright
|
|
|
|
|
|
|
|
0.0.1 6K It works.
|
|
|
|
0.0.2 5K Smaller and you can also check the exit condition if you wish.
|
2005-05-06 10:30:34 +05:30
|
|
|
0.0.3 Uses select()
|
1999-10-05 21:54:54 +05:30
|
|
|
|
|
|
|
19980918 Busy Boxed! Dave Cinege
|
1999-10-12 21:12:48 +05:30
|
|
|
19990512 Uses Select. Charles P. Wright
|
|
|
|
19990513 Fixes stdin stupidity and uses buffers. Charles P. Wright
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2006-04-12 23:25:51 +05:30
|
|
|
Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
1999-10-05 21:54:54 +05:30
|
|
|
*/
|
2001-01-23 04:18:42 +05:30
|
|
|
|
1999-10-05 21:54:54 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-03-09 20:06:42 +05:30
|
|
|
#include <string.h>
|
1999-10-05 21:54:54 +05:30
|
|
|
#include <unistd.h>
|
2005-05-06 10:15:38 +05:30
|
|
|
#include <signal.h>
|
1999-10-05 21:54:54 +05:30
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/ioctl.h>
|
2001-02-20 11:44:08 +05:30
|
|
|
#include "busybox.h"
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2005-05-06 10:15:38 +05:30
|
|
|
static void timeout(int signum)
|
|
|
|
{
|
|
|
|
bb_error_msg_and_die("Timed out");
|
|
|
|
}
|
|
|
|
|
2000-05-13 01:11:47 +05:30
|
|
|
int nc_main(int argc, char **argv)
|
1999-10-05 21:54:54 +05:30
|
|
|
{
|
2005-05-06 10:15:38 +05:30
|
|
|
int do_listen = 0, lport = 0, delay = 0, wsecs = 0, tmpfd, opt, sfd, x;
|
2006-01-25 05:38:53 +05:30
|
|
|
|
2005-05-06 10:30:34 +05:30
|
|
|
#ifdef CONFIG_NC_GAPING_SECURITY_HOLE
|
|
|
|
char *pr00gie = NULL;
|
2002-04-27 05:29:12 +05:30
|
|
|
#endif
|
2000-02-09 01:28:47 +05:30
|
|
|
|
|
|
|
struct sockaddr_in address;
|
|
|
|
struct hostent *hostinfo;
|
|
|
|
|
|
|
|
fd_set readfds, testfds;
|
|
|
|
|
2005-05-06 10:15:38 +05:30
|
|
|
while ((opt = getopt(argc, argv, "lp:i:e:w:")) > 0) {
|
2001-02-07 09:39:23 +05:30
|
|
|
switch (opt) {
|
|
|
|
case 'l':
|
|
|
|
do_listen++;
|
|
|
|
break;
|
|
|
|
case 'p':
|
2004-01-17 10:33:31 +05:30
|
|
|
lport = bb_lookup_port(optarg, "tcp", 0);
|
2001-02-07 09:39:23 +05:30
|
|
|
break;
|
2002-04-27 05:29:12 +05:30
|
|
|
case 'i':
|
|
|
|
delay = atoi(optarg);
|
|
|
|
break;
|
2005-05-06 10:30:34 +05:30
|
|
|
#ifdef CONFIG_NC_GAPING_SECURITY_HOLE
|
2002-04-27 05:29:12 +05:30
|
|
|
case 'e':
|
|
|
|
pr00gie = optarg;
|
|
|
|
break;
|
|
|
|
#endif
|
2005-05-06 10:15:38 +05:30
|
|
|
case 'w':
|
|
|
|
wsecs = atoi(optarg);
|
|
|
|
break;
|
2001-02-07 09:39:23 +05:30
|
|
|
default:
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2001-02-07 09:39:23 +05:30
|
|
|
}
|
2000-04-15 22:04:54 +05:30
|
|
|
}
|
1999-11-13 10:17:09 +05:30
|
|
|
|
2001-02-07 09:39:23 +05:30
|
|
|
if ((do_listen && optind != argc) || (!do_listen && optind + 2 != argc))
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2001-02-07 09:39:23 +05:30
|
|
|
|
2006-04-12 23:25:51 +05:30
|
|
|
sfd = bb_xsocket(AF_INET, SOCK_STREAM, 0);
|
2002-05-05 09:10:14 +05:30
|
|
|
x = 1;
|
2005-05-07 12:15:29 +05:30
|
|
|
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x)) == -1)
|
|
|
|
bb_perror_msg_and_die("reuseaddr");
|
2000-02-09 01:28:47 +05:30
|
|
|
address.sin_family = AF_INET;
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2005-05-06 10:15:38 +05:30
|
|
|
if (wsecs) {
|
|
|
|
signal(SIGALRM, timeout);
|
|
|
|
alarm(wsecs);
|
|
|
|
}
|
|
|
|
|
2001-02-07 09:39:23 +05:30
|
|
|
if (lport != 0) {
|
|
|
|
memset(&address.sin_addr, 0, sizeof(address.sin_addr));
|
2004-01-17 10:33:31 +05:30
|
|
|
address.sin_port = lport;
|
2001-02-07 09:39:23 +05:30
|
|
|
|
2006-04-12 23:54:37 +05:30
|
|
|
bb_xbind(sfd, (struct sockaddr *) &address, sizeof(address));
|
2001-02-07 09:39:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (do_listen) {
|
2001-05-15 08:35:39 +05:30
|
|
|
socklen_t addrlen = sizeof(address);
|
|
|
|
|
2006-04-12 23:54:37 +05:30
|
|
|
bb_xlisten(sfd, 1);
|
2001-05-15 08:35:39 +05:30
|
|
|
if ((tmpfd = accept(sfd, (struct sockaddr *) &address, &addrlen)) < 0)
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg_and_die("accept");
|
2001-02-07 09:39:23 +05:30
|
|
|
|
|
|
|
close(sfd);
|
|
|
|
sfd = tmpfd;
|
|
|
|
} else {
|
2001-05-16 21:10:51 +05:30
|
|
|
hostinfo = xgethostbyname(argv[optind]);
|
2001-02-07 09:39:23 +05:30
|
|
|
|
|
|
|
address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
|
2004-01-17 10:33:31 +05:30
|
|
|
address.sin_port = bb_lookup_port(argv[optind+1], "tcp", 0);
|
2001-02-07 09:39:23 +05:30
|
|
|
|
|
|
|
if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg_and_die("connect");
|
2001-02-07 09:39:23 +05:30
|
|
|
}
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2005-05-06 10:15:38 +05:30
|
|
|
if (wsecs) {
|
|
|
|
alarm(0);
|
|
|
|
signal(SIGALRM, SIG_DFL);
|
|
|
|
}
|
|
|
|
|
2005-05-06 10:30:34 +05:30
|
|
|
#ifdef CONFIG_NC_GAPING_SECURITY_HOLE
|
2002-04-27 05:29:12 +05:30
|
|
|
/* -e given? */
|
|
|
|
if (pr00gie) {
|
|
|
|
dup2(sfd, 0);
|
|
|
|
close(sfd);
|
2005-05-06 10:30:34 +05:30
|
|
|
dup2(0, 1);
|
|
|
|
dup2(0, 2);
|
|
|
|
execl(pr00gie, pr00gie, NULL);
|
2002-04-27 05:29:12 +05:30
|
|
|
/* Don't print stuff or it will go over the wire.... */
|
|
|
|
_exit(-1);
|
|
|
|
}
|
2005-05-06 10:30:34 +05:30
|
|
|
#endif /* CONFIG_NC_GAPING_SECURITY_HOLE */
|
2002-04-27 05:29:12 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(sfd, &readfds);
|
2000-12-16 04:04:34 +05:30
|
|
|
FD_SET(STDIN_FILENO, &readfds);
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
while (1) {
|
|
|
|
int fd;
|
1999-10-12 21:12:48 +05:30
|
|
|
int ofd;
|
2000-02-09 01:28:47 +05:30
|
|
|
int nread;
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
testfds = readfds;
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2000-12-16 04:04:34 +05:30
|
|
|
if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_perror_msg_and_die("select");
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
for (fd = 0; fd < FD_SETSIZE; fd++) {
|
|
|
|
if (FD_ISSET(fd, &testfds)) {
|
2006-05-26 00:01:11 +05:30
|
|
|
if ((nread = safe_read(fd, bb_common_bufsiz1,
|
|
|
|
sizeof(bb_common_bufsiz1))) < 0)
|
|
|
|
{
|
2005-05-07 12:15:29 +05:30
|
|
|
bb_perror_msg_and_die(bb_msg_read_error);
|
2006-05-26 00:01:11 +05:30
|
|
|
}
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
if (fd == sfd) {
|
|
|
|
if (nread == 0)
|
|
|
|
exit(0);
|
2000-12-16 04:04:34 +05:30
|
|
|
ofd = STDOUT_FILENO;
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
2005-07-19 03:53:16 +05:30
|
|
|
if (nread <= 0) {
|
|
|
|
shutdown(sfd, 1 /* send */ );
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
FD_CLR(STDIN_FILENO, &readfds);
|
|
|
|
}
|
1999-10-12 21:12:48 +05:30
|
|
|
ofd = sfd;
|
|
|
|
}
|
|
|
|
|
2006-05-26 00:01:11 +05:30
|
|
|
if (bb_full_write(ofd, bb_common_bufsiz1, nread) < 0)
|
2005-05-07 12:15:29 +05:30
|
|
|
bb_perror_msg_and_die(bb_msg_write_error);
|
2002-04-27 05:29:12 +05:30
|
|
|
if (delay > 0) {
|
|
|
|
sleep(delay);
|
|
|
|
}
|
1999-10-12 21:12:48 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
|
|
|
}
|
1999-10-05 21:54:54 +05:30
|
|
|
}
|