📄 program-datapipe.html
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="Edward Fu">
<meta name="GENERATOR" content="Mozilla/4.05 [zh-CN] (X11; I; Linux 2.1.127 i686) [Netscape]">
<title>Freesoft Linux FAQ -- lenx写的datapipe的程序</title>
</head>
<body>
发信人: 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>∶ 如题,想用的时候总是没有,呵呵.
<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>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -