Linux 版 (精华区)

发信人: netiscpu (说不如做), 信区: Unix
标  题: 将前面的proxyverify.c改在linux下
发信站: 紫 丁 香 (Fri Jul 17 08:21:21 1998), 转信


发信人: tjb (老六), 信区: Linux
标  题: 将前面的proxyverify.c改在linux下
发信站: BBS 水木清华站 (Wed Jul 15 08:29:59 1998)

改动真的很少, //blush
不好意思

/* Free Proxy 验证程序 ,for UNIX                        */
/* Author:  Happy(Netguy)                               */
/*                                                      */
/*  1998.07.02 calvin注:                                */
/*  原来版本在Solaris上,我将它改到FreeBSD上来. :)       */
/*                                                      */
/*  1998.07.15 tjb:                                     */
/*  又改在linux下跑了:p 我没有linux下的, 所以改了改可以 */
/*  在linux下跑, 可能大家都有linux下的了:p              */

//#define  DEBUG

#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#define ZERO  (fd_set *)0 /*tjb将(struct fd_set *)改为(fd_set *) */
#define BUFLEN  256

#ifdef  DEBUG
     int tmp;
#endif

char            target[]="GET
http://www.digital.com/index.html\n";/*HTTP命令*/
char            result[80];       /* 存放过滤结果的文件的名字 */
char            buf[BUFLEN];      /* 缓冲区                   */
FILE            *f,*out;          /* 输入文件和输出文件       */
int             port;             /* server的端口号           */
int             status=-1;        /* read,write,select的返回值*/
char            serverName[30];   /* server的IP字符串         */
u_char          p1,p2,p3,p4;      /* IP地址的四个部分         */
int             sockfd=-1;        /* socket描述符             */
struct timeval  timeout={2,0};    /* 超时限制                 */
fd_set   rmask,wmask;      /* socket的读写屏蔽         */ /*tjb去掉struct*/
struct          sockaddr_in host;
u_long          serverAddr;       /* server的IP地址           */
int             counter;          /* 输入文件的行数           */
int             i;                /* 文件描述符               */


void killHandle(int sig) /* TJB 加int sig */ /* 处理SIGTERM信号       */
{
   fprintf(out,"killed at %s\t%d\n",serverName,port);
   exit(0);
}

#ifdef DEBUG
   void sigpipeHandle(void)
   { fprintf(out,"SIGPIPE at  %s\t%d\n",serverName,port);
   }
#endif


main(int argc,char **argv)
{

  if(argc!=3) { printf("Usage: %s   dataFileName number\n",argv[0]);
    exit(-1); }

  f=fopen(argv[1],"rb");       /* 作为输入的数据文件  */
  if(! f) { fprintf(stdout,"open file error\n"); exit(-1); }

  strcpy(result,argv[1]);
  strcat(result,".ok");
  out=fopen(result,"a");       /* 存放结果的输出文件 */
  if(! out) { fprintf(stdout,"open file error\n"); exit(-1); }

  counter=atoi(argv[2]);                            /* 输入文件的行数  */
  fprintf(stdout,"Free proxy filter...\n");
  fprintf(stdout,"\tInput file:\t%s\n",argv[1]);
  fprintf(stdout,"\tTotal    :\t%s\n",argv[2]);
  fprintf(stdout,"written by Netguy(造梦人)\n");

  signal(SIGTERM,killHandle);

#ifdef DEBUG                          /* 处理SIGPIPE信号   */
  signal(SIGPIPE,sigpipeHandle);
#else
  signal(SIGPIPE,SIG_IGN);
#endif

  switch(fork( ))
  { case 0:                                         /* 子进程继续 */
         break;
    case -1:                                        /* 出错   */
         perror("fork( )");
         exit(-1);
    default:
         fclose(out);                              /* 父进程退出 */
         fclose(f);
         exit(0);
  }

  setpgid(0, getpgrp());      /* 脱离终端组和进程组,成为后台进程 */
                              /* calvin修改... 19980702 */
                              /* TJB 改setpgrp为setpgid*/
  i=open("/dev/tty",O_RDWR);
  if(i>=0)
  {  ioctl(i,TIOCNOTTY,0);
     close(i);
  }
  else { fprintf(out,"TTY eacape error\n"); fflush(out); }

  for ( ;counter >0;counter--)
  {
        fscanf(f,"%s%d",serverName,& port);         /* 从输入文件里读一行 */

#ifdef  DEBUG
        fprintf(out,"%s\t%d ...\n",serverName,port);
#endif

        bzero((char *)& host,sizeof(host));
        serverAddr=inet_addr(serverName);
        host.sin_family=AF_INET;
        host.sin_addr.s_addr=htonl(serverAddr);
        host.sin_port=htons(port);

        if ( (sockfd=socket(AF_INET,SOCK_STREAM,0))<0 )
        { fprintf(out," Error open socket at %s %d\n",serverName,port);
          exit(-1);
        }

        /* 非阻塞式O_NDELAY = FNDELAY = O_NONBLOCK */
        if(fcntl(sockfd,F_SETFL,O_NDELAY) < 0 )
        { fprintf(out,"fcntl() error at %s %d\n",serverName,port);
          exit(-1);
        }

        status=connect(sockfd,(struct sockaddr *)& host,sizeof(host));

        timeout.tv_sec=2;
        timeout.tv_usec=0;                          /* 超时限制 */
        FD_ZERO( & wmask);
        FD_SET(sockfd,& wmask);
        status=select(sockfd+1,ZERO,& wmask, ZERO,& timeout);
        switch(status)
        { case -1:
                  fprintf(out,"select error\n");
                  exit(-1);
          case 0:                                    /* 连接超时 */
                  close(sockfd);
                  continue;
          default:                                   /* 连上了 */
                  if(  FD_ISSET(sockfd,& wmask) )    /* 管套可写吗  */
                       break;
                  else
                  { close(sockfd);
                    continue;
                  }
         }
        /* 下面一句可能会收到SIGPIPE信号,须忽略此信号 */
        status=write(sockfd,target,sizeof(target)); /* 写入GET命令给server */

        timeout.tv_sec=10;
        timeout.tv_usec=0;                    /* 超时限制 */
        FD_ZERO( & rmask);
        FD_SET(sockfd,& rmask);
        status=select(sockfd+1,& rmask,ZERO,ZERO,& timeout);
        switch(status)
        { case -1:
                  fprintf(out,"select error\n");
                  exit(-1);
          case 0:                               /* 超时了 */
                  close(sockfd);
                  continue;
          default:                              /* 连上了 */
              if(  FD_ISSET(sockfd,& rmask) )
              {
                 bzero(buf,BUFLEN);                /* 清缓冲区           */
                 status=read(sockfd,buf,BUFLEN);   /* 读server的返回结果 */
                 close(sockfd);
                 if(status<=0)  continue;          /* 没读到东西         */

#ifdef DEBUG
                 for(tmp=0;tmp<status;tmp++) fputc(buf[tmp],out);
#endif

/* 下面的语句采用Digital公司的主页作为判断free proxy的依据.如果其主页内容变
   了,那麽相应的程序段也要改变
 */
                 if( ! strncmp((buf+22),"Digital",7) )   /* 是free proxy吗 ?
*/
                 {
                    fprintf(out,"free\t%s\t%d\n",serverName,port);
                    fflush(out);
                 }
               }
               else close(sockfd);
        }
  }
  fclose(f);
  fprintf(out,"Free proxy filter done.\n");
  fclose(out);
}

--
  2m                      m
  2m;36m    一壶浊酒喜相逢    4m  m
  2m;33m 古今多少事均赋笑谈中 4m  m
  2m                      4m  m
    4m                      m

m3m※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 202.200.37.100]m


--

                              Enjoy Linux!
                          -----It's FREE!-----

※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: mtlab.hit.edu.cn]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:214.747毫秒