📄 00000007.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: raner (毕设好无聊呀!), 信区: Linux <BR>标 题: Re: 谁把lenx的datapipe的程序再贴一次? <BR>发信站: BBS 水木清华站 (Wed May 6 21:14:33 1998) <BR> <BR>【 在 scaner (S.c.a.n.e.R) 的大作中提到: 】 <BR>∶<I> 如题,想用的时候总是没有,呵呵. </I><BR> <BR>/* <BR>一个非常短小实用的程序, 可以非常简单的实现类似proxy的中转功能 <BR> <BR>比如, 我在mirg运行 <BR>datapipe 2222 21 159.226.23.7 <BR> <BR>以后, ftp mirg:2222就会通过mirg连接到23.7, 非常的方便 <BR> <BR>可以用datapipe层层接力, 或者用于实现简单的NAT功能, 十分好玩 :-) <BR> <BR>The following is an interesting snippet of code I wrote recently. It <BR>makes a data pipe between a listen port on the machine it's being run on <BR>and a port on a remote machine. For example, running <BR> datapipe 2222 23 your.machine.com <BR> <BR>would create a port 2222 on the local machine that, if telnetted to, would <BR>be the same as telnetting to port 23 on your.machine.com. This can be used <BR>for a variety of purposes: redirect IRC connections so that identd shows <BR>the username of the datapipe process; redirect sendmail direct connections <BR>for the same reason; even use on a firewall machine to give access to an <BR>internal service (ftpd, for instance). Cascaded datapipes make for <BR>interesting traceback dilemmas. Questions and comments accepted. <BR> <BR>Compile with: <BR> cc -o datapipe -O datapipe.c <BR>On boxes without strerror() (like SunOS 4.x), compile with: <BR> cc -o datapipe -O -DSTRERROR datapipe.c <BR> <BR>Run as: <BR> datapipe localport remoteport remotehost <BR> <BR>It will fork itself into the background. <BR> <BR>/* <BR> * Datapipe - Create a listen socket to pipe connections to another <BR> * machine/port. 'localport' accepts connections on the machine running <BR> * datapipe, which will connect to 'remoteport' on 'remotehost'. Fairly <BR> * standard 500 xxxx extended errors are used if something drastic <BR> * happens. <BR>for a variety of purposes: redirect IRC connections so that identd shows <BR>the username of the datapipe process; redirect sendmail direct connections <BR>for the same reason; even use on a firewall machine to give access to an <BR>internal service (ftpd, for instance). Cascaded datapipes make for <BR>interesting traceback dilemmas. Questions and comments accepted. <BR> <BR>Compile with: <BR> cc -o datapipe -O datapipe.c <BR>On boxes without strerror() (like SunOS 4.x), compile with: <BR> cc -o datapipe -O -DSTRERROR datapipe.c <BR> <BR>Run as: <BR> datapipe localport remoteport remotehost <BR> <BR>It will fork itself into the background. <BR> <BR>/* <BR> * Datapipe - Create a listen socket to pipe connections to another <BR> * machine/port. 'localport' accepts connections on the machine running <BR> * datapipe, which will connect to 'remoteport' on 'remotehost'. Fairly <BR> * standard 500 xxxx extended errors are used if something drastic <BR> * happens. <BR> * <BR> * (c) 1995 Todd Vierling <BR> * <BR> * Define STRERROR while compiling on a SunOS 4.x box <BR> */ <BR> <BR>#include <sys/types.h> <BR>#include <sys/socket.h> <BR>#include <sys/wait.h> <BR>#include <netinet/in.h> <BR>#include <stdio.h> <BR>#include <stdlib.h> <BR>#include <errno.h> <BR>#include <unistd.h> <BR>#include <netdb.h> <BR> <BR>#//include <linux/time.h> <BR> <BR>#ifdef STRERROR <BR>extern char *sys_errlist[]; <BR>extern int sys_nerr; <BR>char *undef = "Undefined error"; <BR> <BR>char *strerror(error) <BR> int error; <BR>{ <BR> if (error > sys_nerr) <BR> return undef; <BR> return sys_errlist[error]; <BR>} <BR>#endif <BR> <BR>main(argc, argv) <BR> int argc; <BR> char **argv; <BR>{ <BR> int lsock, csock, osock; <BR> FILE *cfile; <BR> char buf[4096]; <BR> struct sockaddr_in laddr, caddr, oaddr; <BR> int caddrlen = sizeof(caddr); <BR> fd_set fdsr, fdse; <BR> struct hostent *h; <BR> struct servent *s; <BR> int nbyt; <BR> unsigned long a; <BR> unsigned short oport; <BR> <BR> if (argc != 4) { <BR> fprintf(stderr,"Usage: %s localport remoteport remotehost\n",argv[0]); <BR> return 30; <BR> } <BR> a = inet_addr(argv[3]); <BR> if (!(h = gethostbyname(argv[3])) && <BR> !(h = gethostbyaddr(&a, 4, AF_INET))) { <BR> perror(argv[3]); <BR> return 25; <BR> } <BR> oport = atol(argv[2]); <BR> laddr.sin_port = htons((unsigned short)(atol(argv[1]))); <BR> if ((lsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { <BR> perror("socket"); <BR> return 20; <BR> } <BR> laddr.sin_family = htons(AF_INET); <BR> laddr.sin_addr.s_addr = htonl(0); <BR> if (bind(lsock, &laddr, sizeof(laddr))) { <BR> perror("bind"); <BR> return 20; <BR> } <BR> if (listen(lsock, 1)) { <BR> perror("listen"); <BR> return 20; <BR> } <BR> if ((nbyt = fork()) == -1) { <BR> perror("fork"); <BR> return 20; <BR> } <BR> if (nbyt > 0) <BR> return 0; <BR> setsid(); <BR> while ((csock = accept(lsock, &caddr, &caddrlen)) != -1) { <BR> cfile = fdopen(csock,"r+"); <BR> if ((nbyt = fork()) == -1) { <BR> fprintf(cfile, "500 fork: %s\n", strerror(errno)); <BR> shutdown(csock,2); <BR> fclose(cfile); <BR> continue; <BR> } <BR> if (nbyt == 0) <BR> goto gotsock; <BR> fclose(cfile); <BR> while (waitpid(-1, NULL, WNOHANG) > 0); <BR> } <BR> return 20; <BR> <BR> gotsock: <BR> if ((osock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { <BR> fprintf(cfile, "500 socket: %s\n", strerror(errno)); <BR> goto quit1; <BR> } <BR> oaddr.sin_family = h->h_addrtype; <BR> oaddr.sin_port = htons(oport); <BR> memcpy(&oaddr.sin_addr, h->h_addr, h->h_length); <BR> if (connect(osock, &oaddr, sizeof(oaddr))) { <BR> fprintf(cfile, "500 connect: %s\n", strerror(errno)); <BR> goto quit1; <BR> } <BR> while (1) { <BR> FD_ZERO(&fdsr); <BR> FD_ZERO(&fdse); <BR> FD_SET(csock,&fdsr); <BR> FD_SET(csock,&fdse); <BR> FD_SET(osock,&fdsr); <BR> FD_SET(osock,&fdse); <BR> if (select(20, &fdsr, NULL, &fdse, NULL) == -1) { <BR> fprintf(cfile, "500 select: %s\n", strerror(errno)); <BR> goto quit2; <BR> } <BR> if (FD_ISSET(csock,&fdsr) || FD_ISSET(csock,&fdse)) { <BR> if ((nbyt = read(csock,buf,4096)) <= 0) <BR> goto quit2; <BR> if ((write(osock,buf,nbyt)) <= 0) <BR> goto quit2; <BR> } else if (FD_ISSET(osock,&fdsr) || FD_ISSET(osock,&fdse)) { <BR> if ((nbyt = read(osock,buf,4096)) <= 0) <BR> goto quit2; <BR> if ((write(csock,buf,nbyt)) <= 0) <BR> goto quit2; <BR> } <BR> } <BR> <BR> quit2: <BR> shutdown(osock,2); <BR> close(osock); <BR> quit1: <BR> fflush(cfile); <BR> shutdown(csock,2); <BR> quit0: <BR> fclose(cfile); <BR> return 0; <BR>} <BR> <BR>-- <BR>※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 166.111.68.98] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -