Chinaunix首页 | 论坛 | 博客
  • 博客访问: 57000
  • 博文数量: 12
  • 博客积分: 331
  • 博客等级: 一等列兵
  • 技术积分: 155
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-17 10:53
文章分类
文章存档

2012年(12)

我的朋友

分类: LINUX

2012-09-11 22:57:45

由于要在linux下面用到类似lcx的程序,百度找到了几份源代码,但是在BT5 R3里面编译都错误,就仔细看了看,改了下,主要修改的地方有2个,第一个是void closeallfd()改成了void closeallfd(int);另一个地方是accept函数的第三个参数加上了强制类型转换(socklen_t *),修改后代码如下:


点击(此处)折叠或打开

  1. /* Socket data transport tool
  2. * by bkbll(bkbll@cnhonker.net)
  3. * [bkbll@mobile socket]$ uname -a
  4. * Linux mobile 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386 GNU/Linux
  5. * [bkbll@mobile socket]$ gcc -o trtool trtool.c
  6. * [bkbll@mobile socket]$ ./trtool
  7. * Socket data transport tool
  8. * by bkbll(bkbll@cnhonker.net)
  9. * Usage:./trtool -m method [-h1 host1] -p1 port1 [-h2 host2] -p2 port2 [-v] [-log filename]
  10. * -v: version
  11. * -h1: host1
  12. * -h2: host2
  13. * -p1: port1
  14. * -p2: port2
  15. * -log: log the data
  16. * -m: the action method for this tool
  17. * 1: listen on PORT1 and connect to HOST2:PORT2
  18. * 2: listen on PORT1 and PORT2
  19. * 3: connect to HOST1:PORT1 and HOST2:PORT2
  20. */
  21. #include <sys/time.h>
  22. #include <signal.h>
  23. #include <stdlib.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <string.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <unistd.h>
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #include <netdb.h>

  33. #define VERSION "2.2"
  34. #define TIMEOUT 300
  35. #define max(a,b) (a)>(b)?(a):(b)
  36. #define MAXSIZE 10240
  37. #define HOSTLEN 40
  38. #define CONNECT_NUMBER 5

  39. /* define function here */
  40. void usage(char *s);
  41. void transdata(int fd1,int fd2);
  42. void closeallfd(int);
  43. void makelog(char *buffer,int length);
  44. int testifisvalue(char *str);
  45. int bind2conn(int port1,char *host,int port2);
  46. int bind2bind(int port1,int port2);
  47. int conn2conn(char *host1,int port1,char *host2,int port2);
  48. int create_socket();
  49. int create_serv(int sockfd,int port);
  50. int client_connect(int sockfd,char* server,int port);

  51. /* define GLOBAL varible here */
  52. extern int errno;
  53. FILE *fp;

  54. main(int argc,char **argv)
  55. {
  56.     char **p;
  57.     char host1[HOSTLEN],host2[HOSTLEN];
  58.     int port1=0,port2=0,method=0;
  59.     int length;
  60.     char *logfile=NULL;

  61.     p=argv;
  62.     memset(host1,0,HOSTLEN);
  63.     memset(host2,0,HOSTLEN);
  64.     while(*p)
  65.     {
  66.         if(strcmp(*p,"-v")==0)
  67.         {
  68.             printf("Socket data transport tool.\r\nVersion:%s\r\n",VERSION);
  69.             p++;
  70.             continue;
  71.         }
  72.         if(strcmp(*p,"-h1")==0)
  73.         {
  74.             if(testifisvalue(*(p+1))==1)
  75.             {
  76.                 length=(strlen(*(p+1))>HOSTLEN-1)?HOSTLEN-1:strlen(*(p+1));
  77.                 strncpy(host1,*(++p),length);
  78.             }
  79.             p++;
  80.             continue;
  81.         }
  82.         if(strcmp(*p,"-h2")==0)
  83.         {
  84.             if(testifisvalue(*(p+1))==1)
  85.             {
  86.                 length=(strlen(*(p+1))>HOSTLEN-1)?HOSTLEN-1:strlen(*(p+1));
  87.                 strncpy(host2,*(++p),length);
  88.             }
  89.             p++;
  90.             continue;
  91.         }
  92.         if(strcmp(*p,"-p1")==0)
  93.         {
  94.             if(testifisvalue(*(p+1))==1)
  95.                 port1=atoi(*(++p));
  96.             p++;
  97.             continue;
  98.         }
  99.         if(strcmp(*p,"-p2")==0)
  100.         {
  101.             if(testifisvalue(*(p+1))==1)
  102.                 port2=atoi(*(++p));
  103.             p++;
  104.             continue;
  105.         }
  106.         if(strcmp(*p,"-m")==0)
  107.         {
  108.             if(testifisvalue(*(p+1))==1)
  109.                 method=atoi(*(++p));
  110.             p++;
  111.             continue;
  112.         }
  113.         if(strcmp(*p,"-log")==0)
  114.         {
  115.             if(testifisvalue(*(p+1))==1)
  116.                 logfile=*(++p);
  117.             else
  118.             {
  119.                 printf("[ERROR]:must supply logfile name\r\n");
  120.                 exit(0);
  121.             }
  122.             p++;
  123.             continue;
  124.         }
  125.         p++;
  126.     }
  127.     signal(SIGCLD,SIG_IGN);
  128.     signal(SIGINT,&closeallfd);
  129.     if(logfile !=NULL)
  130.     {
  131.         fp=fopen(logfile,"a");
  132.         if(fp == NULL )
  133.         {
  134.             perror("open logfile");
  135.             exit(0);
  136.         }
  137.     }
  138.     makelog("######################## start ################\r\n",49);
  139.     switch(method)
  140.     {
  141.     case 0:
  142.         usage(argv[0]);
  143.         break;
  144.     case 1:
  145.         if((port1==0) || (port2==0))
  146.         {
  147.             printf("[ERROR]:must supply PORT1 and PORT2.\r\n");
  148.             break;
  149.         }
  150.         if(strlen(host2)==0)
  151.         {
  152.             printf("[ERROR]:must supply HOST2.\r\n");
  153.             break;
  154.         }
  155.         bind2conn(port1,host2,port2);
  156.         break;
  157.     case 2:
  158.         if((port1==0) || (port2==0))
  159.         {
  160.             printf("[ERROR]:must supply PORT1 and PORT2.\r\n");
  161.             break;
  162.         }
  163.         bind2bind(port1,port2);
  164.         break;
  165.     case 3:
  166.         if((port1==0) || (port2==0))
  167.         {
  168.             printf("[ERROR]:must supply PORT1 and PORT2.\r\n");
  169.             break;
  170.         }
  171.         if(strlen(host1)==0)
  172.         {
  173.             printf("[ERROR]:must supply HOST1.\r\n");
  174.             break;
  175.         }
  176.         if(strlen(host2)==0)
  177.         {
  178.             printf("[ERROR]:must supply HOST2.\r\n");
  179.             break;
  180.         }
  181.         conn2conn(host1,port1,host2,port2);
  182.         break;
  183.     default:
  184.         usage(argv[0]);
  185.     }
  186.     closeallfd(0);
  187. }

  188. int testifisvalue(char *str)
  189. {
  190.     if(str == NULL ) return(0);
  191.     if(str[0]=='-') return(0);
  192.     return(1);
  193. }

  194. void usage(char *s)
  195. {
  196.     printf("Socket data transport tool\r\n");
  197.     printf("by bkbll(bkbll@cnhonker.net)\r\n\r\n");
  198.     printf("Usage:%s -m method [-h1 host1] -p1 port1 [-h2 host2] -p2 port2 [-v] [-log filename]\r\n",s);
  199.     printf(" -v: version\r\n");
  200.     printf(" -h1: host1\r\n");
  201.     printf(" -h2: host2\r\n");
  202.     printf(" -p1: port1\r\n");
  203.     printf(" -p2: port2\r\n");
  204.     printf(" -log: log the data\r\n");
  205.     printf(" -m: the action method for this tool\r\n");
  206.     printf(" 1: listen on PORT1 and connect to HOST2:PORT2\r\n");
  207.     printf(" 2: listen on PORT1 and PORT2\r\n");
  208.     printf(" 3: connect to HOST1:PORT1 and HOST2:PORT2\r\n");

  209.     closeallfd(0);
  210. }

  211. int bind2conn(int port1,char *host,int port2)
  212. {
  213.     int sockfd,sockfd1,sockfd2;
  214.     struct sockaddr_in remote;
  215.     int size;
  216.     int pid;
  217.     char buffer[1024];

  218.     memset(buffer,0,1024);
  219.     if((sockfd=create_socket())==0) exit(0);
  220.     if(create_serv(sockfd,port1)==0)
  221.     {
  222.         close(sockfd1);
  223.         exit(0);
  224.     }
  225.     size=sizeof(struct sockaddr);
  226.     while(1)
  227.     {
  228.         printf("waiting for response.........\n");
  229.         if((sockfd1=accept(sockfd,(struct sockaddr *)&remote,(socklen_t *)&size))<0)
  230.         {
  231.             perror("accept error\n");
  232.             continue;
  233.         }
  234.         printf("accept a client from %s:%d\n",inet_ntoa(remote.sin_addr),ntohs(remote.sin_port));
  235.         if((sockfd2=create_socket())==0)
  236.         {
  237.             close(sockfd1);
  238.             continue;
  239.         }
  240.         printf("make a connection to %s:%d....",host,port2);
  241.         fflush(stdout);
  242.         if(client_connect(sockfd2,host,port2)==0)
  243.         {
  244.             close(sockfd2);
  245.             sprintf(buffer,"[SERVER]connection to %s:%d error\r\n",host,port2);
  246.             write(sockfd1,buffer,strlen(buffer));
  247.             memset(buffer,0,1024);
  248.             close(sockfd1);
  249.             continue;
  250.         }
  251.         printf("ok\r\n");
  252.         pid=fork();
  253.         if(pid==0) transdata(sockfd1,sockfd2);
  254.         // sleep(2);
  255.         close(sockfd1);
  256.         close(sockfd2);
  257.     }

  258. }

  259. int bind2bind(int port1,int port2)
  260. {
  261.     int fd1,fd2,sockfd1,sockfd2;
  262.     struct sockaddr_in client1,client2;
  263.     int size1,size2;
  264.     int pid;

  265.     if((fd1=create_socket())==0) exit(0);
  266.     if((fd2=create_socket())==0) exit(0);
  267.     printf("binding port %d......",port1);
  268.     fflush(stdout);
  269.     if(create_serv(fd1,port1)==0)
  270.     {
  271.         close(fd1);
  272.         exit(0);
  273.     }
  274.     printf("ok\r\n");
  275.     printf("binding port %d......",port2);
  276.     fflush(stdout);
  277.     if(create_serv(fd2,port2)==0)
  278.     {
  279.         close(fd2);
  280.         exit(0);
  281.     }
  282.     printf("ok\r\n");
  283.     size1=size2=sizeof(struct sockaddr);
  284.     while(1)
  285.     {
  286.         printf("waiting for response on port %d.........\n",port1);
  287.         if((sockfd1=accept(fd1,(struct sockaddr *)&client1,(socklen_t *)&size1))<0)
  288.         {
  289.             perror("accept1 error");
  290.             continue;
  291.         }
  292.         printf("accept a client on port %d from %s,waiting another on port %d....\n",port1,inet_ntoa(client1.sin_addr),port2);
  293.         if((sockfd2=accept(fd2,(struct sockaddr *)&client2,(socklen_t *)&size2))<0)
  294.         {
  295.             perror("accept2 error");
  296.             close(sockfd1);
  297.             continue;
  298.         }
  299.         printf("accept a client on port %d from %s\n",port2,inet_ntoa(client2.sin_addr));
  300.         pid=fork();
  301.         if(pid==0) transdata(sockfd1,sockfd2);
  302.         //sleep(2);
  303.         close(sockfd1);
  304.         close(sockfd2);
  305.     }
  306. }

  307. int conn2conn(char *host1,int port1,char *host2,int port2)
  308. {
  309.     int sockfd1,sockfd2;
  310.     int pid;

  311.     while(1)
  312.     {
  313.         if((sockfd1=create_socket())==0) exit(0);
  314.         if((sockfd2=create_socket())==0) exit(0);
  315.         printf("make a connection to %s:%d....",host1,port1);
  316.         fflush(stdout);
  317.         if(client_connect(sockfd1,host1,port1)==0)
  318.         {
  319.             close(sockfd1);
  320.             close(sockfd2);
  321.             break;
  322.         }
  323.         printf("ok\r\n");
  324.         printf("make a connection to %s:%d....",host2,port2);
  325.         fflush(stdout);
  326.         if(client_connect(sockfd2,host2,port2)==0)
  327.         {
  328.             close(sockfd1);
  329.             close(sockfd2);
  330.             break;
  331.         }
  332.         printf("ok\r\n");
  333.         pid=fork();
  334.         if(pid==0) transdata(sockfd1,sockfd2);
  335.         //sleep(2);
  336.         close(sockfd1);
  337.         close(sockfd2);

  338.     }
  339. }

  340. void transdata(int fd1,int fd2)
  341. {
  342.     struct timeval timeset;
  343.     fd_set readfd,writefd;
  344.     int result,i=0;
  345.     char read_in1[MAXSIZE],send_out1[MAXSIZE];
  346.     char read_in2[MAXSIZE],send_out2[MAXSIZE];
  347.     int read1=0,totalread1=0,send1=0;
  348.     int read2=0,totalread2=0,send2=0;
  349.     int sendcount1,sendcount2;
  350.     int maxfd;
  351.     struct sockaddr_in client1,client2;
  352.     int structsize1,structsize2;
  353.     char host1[20],host2[20];
  354.     int port1=0,port2=0;
  355.     char tmpbuf1[100],tmpbuf2[100];

  356.     memset(host1,0,20);
  357.     memset(host2,0,20);
  358.     memset(tmpbuf1,0,100);
  359.     memset(tmpbuf2,0,100);
  360.     if(fp!=NULL)
  361.     {
  362.         structsize1=sizeof(struct sockaddr);
  363.         structsize2=sizeof(struct sockaddr);
  364.         if(getpeername(fd1,(struct sockaddr *)&client1,(socklen_t *)&structsize1)<0)
  365.         {
  366.             strcpy(host1,"fd1");
  367.         }
  368.         else
  369.         {
  370.             printf("got,ip:%s,port:%d\r\n",inet_ntoa(client1.sin_addr),ntohs(client1.sin_port));
  371.             strcpy(host1,inet_ntoa(client1.sin_addr));
  372.             port1=ntohs(client1.sin_port);
  373.         }
  374.         if(getpeername(fd2,(struct sockaddr *)&client2,(socklen_t *)&structsize2)<0)
  375.         {
  376.             strcpy(host2,"fd2");
  377.         }
  378.         else
  379.         {
  380.             printf("got,ip:%s,port:%d\r\n",inet_ntoa(client2.sin_addr),ntohs(client2.sin_port));
  381.             strcpy(host2,inet_ntoa(client2.sin_addr));
  382.             port2=ntohs(client2.sin_port);
  383.         }
  384.         sprintf(tmpbuf1,"\r\n########### read from %s:%d ####################\r\n",host1,port1);
  385.         sprintf(tmpbuf2,"\r\n########### reply from %s:%d ####################\r\n",host2,port2);
  386.     }

  387.     maxfd=max(fd1,fd2)+1;
  388.     memset(read_in1,0,MAXSIZE);
  389.     memset(read_in2,0,MAXSIZE);
  390.     memset(send_out1,0,MAXSIZE);
  391.     memset(send_out2,0,MAXSIZE);

  392.     timeset.tv_sec=TIMEOUT;
  393.     timeset.tv_usec=0;
  394.     while(1)
  395.     {
  396.         FD_ZERO(&readfd);
  397.         FD_ZERO(&writefd);

  398.         FD_SET(fd1,&readfd);
  399.         FD_SET(fd1,&writefd);
  400.         FD_SET(fd2,&writefd);
  401.         FD_SET(fd2,&readfd);

  402.         result=select(maxfd,&readfd,&writefd,NULL,&timeset);
  403.         if((result<0) && (errno!=EINTR))
  404.         {
  405.             perror("select error");
  406.             break;
  407.         }
  408.         else if(result==0)
  409.         {
  410.             printf("time out\n");
  411.             break;
  412.         }
  413.         if(FD_ISSET(fd1,&readfd))
  414.         {
  415.             /* 不能超过MAXSIZE-totalread1,不然send_out1会溢出 */
  416.             if(totalread1<MAXSIZE)
  417.             {
  418.                 read1=read(fd1,read_in1,MAXSIZE-totalread1);
  419.                 if(read1==0) break;
  420.                 if((read1<0) && (errno!=EINTR))
  421.                 {
  422.                     perror("read data error");
  423.                     break;
  424.                 }
  425.                 memcpy(send_out1+totalread1,read_in1,read1);
  426.                 makelog(tmpbuf1,strlen(tmpbuf1));
  427.                 makelog(read_in1,read1);
  428.                 totalread1+=read1;
  429.                 memset(read_in1,0,MAXSIZE);
  430.             }
  431.         }
  432.         if(FD_ISSET(fd2,&writefd))
  433.         {
  434.             int err=0;
  435.             sendcount1=0;
  436.             while(totalread1>0)
  437.             {
  438.                 send1=write(fd2,send_out1+sendcount1,totalread1);
  439.                 if(send1==0)break;
  440.                 if((send1<0) && (errno!=EINTR))
  441.                 {
  442.                     perror("unknow error");
  443.                     err=1;
  444.                     break;
  445.                 }
  446.                 if((send1<0) && (errno==ENOSPC)) break;
  447.                 sendcount1+=send1;
  448.                 totalread1-=send1;
  449.             }
  450.             if(err==1) break;
  451.             if((totalread1>0) && (sendcount1>0))
  452.             {
  453.                 /* 移动未发送完的数据到开始 */
  454.                 memcpy(send_out1,send_out1+sendcount1,totalread1);
  455.                 memset(send_out1+totalread1,0,MAXSIZE-totalread1);
  456.             }
  457.             else
  458.                 memset(send_out1,0,MAXSIZE);
  459.         }
  460.         if(FD_ISSET(fd2,&readfd))
  461.         {

  462.             if(totalread2<MAXSIZE)
  463.             {
  464.                 read2=read(fd2,read_in2,MAXSIZE-totalread2);
  465.                 if(read2==0)break;
  466.                 if((read2<0) && (errno!=EINTR))
  467.                 {
  468.                     perror("read data error");
  469.                     break;
  470.                 }
  471.                 memcpy(send_out2+totalread2,read_in2,read2);
  472.                 makelog(tmpbuf2,strlen(tmpbuf2));
  473.                 makelog(read_in2,read2);
  474.                 totalread2+=read2;
  475.                 memset(read_in2,0,MAXSIZE);
  476.             }
  477.         }
  478.         if(FD_ISSET(fd1,&writefd))
  479.         {
  480.             int err2=0;
  481.             sendcount2=0;
  482.             while(totalread2>0)
  483.             {
  484.                 send2=write(fd1,send_out2+sendcount2,totalread2);
  485.                 if(send2==0)break;
  486.                 if((send2<0) && (errno!=EINTR))
  487.                 {
  488.                     perror("unknow error");
  489.                     err2=1;
  490.                     break;
  491.                 }
  492.                 if((send2<0) && (errno==ENOSPC)) break;
  493.                 sendcount2+=send2;
  494.                 totalread2-=send2;
  495.             }
  496.             if(err2==1) break;
  497.             if((totalread2>0) && (sendcount2 > 0))
  498.             {
  499.                 /* 移动未发送完的数据到开始 */
  500.                 memcpy(send_out2,send_out2+sendcount2,totalread2);
  501.                 memset(send_out2+totalread2,0,MAXSIZE-totalread2);
  502.             }
  503.             else
  504.                 memset(send_out2,0,MAXSIZE);
  505.         }
  506.     }

  507.     close(fd1);
  508.     close(fd2);
  509.     printf("ok,I closed the two fd\r\n");
  510.     exit(0);
  511. }

  512. void closeallfd(int n)
  513. {
  514.     int i;
  515.     printf("Let me exit...");
  516.     fflush(stdout);
  517.     for(i=3;i<256;i++)
  518.     {
  519.         close(i);
  520.     }
  521.     if(fp != NULL)
  522.     {
  523.         fprintf(fp,"exited\r\n");
  524.         fclose(fp);
  525.     }
  526.     printf("all overd\r\n");
  527.     exit(0);
  528. }
  529. void makelog(char *buffer,int length)
  530. {
  531.     if(fp !=NULL)
  532.     {
  533.         //fprintf(fp,"%s",buffer);
  534.         write(fileno(fp),buffer,length);
  535.         fflush(fp);
  536.     }
  537. }

  538. int create_socket()
  539. {
  540.     int sockfd;

  541.     sockfd=socket(AF_INET,SOCK_STREAM,0);
  542.     if(sockfd<0)
  543.     {
  544.         perror("create socket error");
  545.         return(0);
  546.     }
  547.     return(sockfd);
  548. }

  549. int create_serv(int sockfd,int port)
  550. {
  551.     struct sockaddr_in srvaddr;
  552.     int on=1;

  553.     bzero(&srvaddr,sizeof(struct sockaddr));
  554.     srvaddr.sin_port=htons(port);
  555.     srvaddr.sin_family=AF_INET;
  556.     srvaddr.sin_addr.s_addr=htonl(INADDR_ANY);

  557.     setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); //so I can rebind the port
  558.     if(bind(sockfd,(struct sockaddr *)&srvaddr,sizeof(struct sockaddr))<0)
  559.     {
  560.         perror("error");
  561.         return(0);
  562.     }
  563.     if(listen(sockfd,CONNECT_NUMBER)<0)
  564.     {
  565.         perror("listen error\n");
  566.         return(0);
  567.     }
  568.     return(1);
  569. }

  570. int client_connect(int sockfd,char* server,int port)
  571. {
  572.     struct sockaddr_in cliaddr;
  573.     struct hostent *host;

  574.     if(!(host=gethostbyname(server)))
  575.     {
  576.         printf("gethostbyname(%s) error:%s\n",server,strerror(errno));
  577.         return(0);
  578.     }

  579.     bzero(&cliaddr,sizeof(struct sockaddr));
  580.     cliaddr.sin_family=AF_INET;
  581.     cliaddr.sin_port=htons(port);
  582.     cliaddr.sin_addr=*((struct in_addr *)host->h_addr);

  583.     if(connect(sockfd,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr))<0)
  584.     {
  585.         perror("error");
  586.         return(0);
  587.     }
  588.     return(1);
  589. }


阅读(2125) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~