发信人: Calvin (灵飞), 信区: Linux
标  题: FreeProxy Verify for Solaris by happy
发信站: BBS 水木清华站 (Wed Jul 15 08:23:57 1998)
 
/* Free Proxy 验证程序 ,for UNIX                   */
/* Author:  Happy(Netguy)                          */
 
//#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>  */
#include       "/usr/ucbinclude/sys/ioctl.h"
 
#define ZERO  (struct fd_set *)0
#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};    /* 超时限制                 */
struct fd_set   rmask,wmask;      /* socket的读写屏蔽         */
struct          sockaddr_in host;
u_long          serverAddr;       /* server的IP地址           */
int             counter;          /* 输入文件的行数           */
int             i;                /* 文件描述符               */
 
 
void killHandle(void)                /* 处理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);
  }
 
  setpgrp();                          /* 脱离终端组和进程组,成为后台进程 */
 
  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);
}
 
--
m;36m※ 来源:.华南网木棉站 bbs.gznet.edu.cn.[FROM: 202.38.198.197]m
 
 

本文转自中文Linux论坛