⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proxy serverȦ

📁 代理服务器源码
💻
📖 第 1 页 / 共 3 页
字号:
  signal(SIGCLD,reap_status);                                                    
                                                                                 
}                                                                                
                                                                                 
                                                                                 
                                                                                 
/****************************************************************************    
  function:      do_proxy                                                        
  description:   does the actual work of virtually connecting a client to        
                 the telnet service on the isolated host.                        
  arguments:                                                                     
    usersockfd   socket to which the client is connected.                        
                                                                                 
  return value:  none.                                                           
  calls:         none.                                                           
  globals:       reads hostaddr.                                                 
 ****************************************************************************/   
                                                                                 
void do_proxy (usersockfd)                                                       
int usersockfd;                                                                  
{                                                                                
  int isosockfd;                                                                 
  fd_set rdfdset;                                                                
  int connstat;                                                                  
  int iolen;                                                                     
  char buf [2048];                                                               
                                                                                 
  /* open a socket to connect to the isolated host */                            
  if ((isosockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)                           
    errorout("failed to create socket to host");                                 
                                                                                 
  /* attempt a connection */                                                     
  connstat = connect(isosockfd,                                                  
                     (struct sockaddr *) &hostaddr,                              
                     sizeof(hostaddr));                                          
  switch (connstat) {                                                            
    case 0:                                                                      
      break;                                                                     
    case ETIMEDOUT:                                                              
    case ECONNREFUSED:                                                           
    case ENETUNREACH:                                                            
      strcpy(buf,sys_errlist[errno]);                                            
      strcat(buf,"\r\n");                                                        
      write(usersockfd,buf,strlen(buf));                                         
      close(usersockfd);                                                         
      exit(1);  /* die peacefully if we can't establish a connection */          
      break;                                                                     
    default:                                                                     
      errorout("failed to connect to host");                                     
  }                                                                              
                                                                                 
                                                                                 
  /* now we're connected, serve fall into the data echo loop */                  
  while (1) {                                                                    
    /* Select for readability on either of our two sockets */                    
    FD_ZERO(&rdfdset);                                                           
    FD_SET(usersockfd,&rdfdset);                                                 
    FD_SET(isosockfd,&rdfdset);                                                  
    if (select(FD_SETSIZE,&rdfdset,NULL,NULL,NULL) < 0)                          
      errorout("select failed");                                                 
                                                                                 
    /* is the client sending data? */                                            
    if (FD_ISSET(usersockfd,&rdfdset)) {                                         
      if ((iolen = read(usersockfd,buf,sizeof(buf))) <= 0)                       
        break;   /* zero length means the client disconnected */                 
                                                                                 
      write(isosockfd,buf,iolen);   /* copy to host -- blocking semantics */     
    }                                                                            
                                                                                 
    /* is the host sending data? */                                              
    if (FD_ISSET(isosockfd,&rdfdset)) {                                          
      if ((iolen = read(isosockfd,buf,sizeof(buf))) <= 0)                        
        break;   /* zero length means the host disconnected */                   
                                                                                 
      write(usersockfd,buf,iolen);  /* copy to client -- blocking semantics */   
    }                                                                            
  }                                                                              
                                                                                 
  /* we're done with the sockets */                                              
  close(isosockfd);                                                              
  close(usersockfd);                                                             
}                                                                                
                                                                                 
                                                                                 
                                                                                 
/****************************************************************************    
  function:      errorout                                                        
  description:   displays an error message on the console and kills the          
                 current process.                                                
  arguments:                                                                     
    msg          message to be displayed.                                        
                                                                                 
  return value:  none -- does not return.                                        
  calls:         none.                                                           
  globals:       none.                                                           
 ****************************************************************************/   
                                                                                 
void errorout (msg)                                                              
char *msg;                                                                       
{                                                                                
  FILE *console;                                                                 
                                                                                 
  console = fopen("/dev/console","a");                                           
  fprintf(console,"proxyd: %s\r\n",msg);                                         
  fclose(console);                                                               
  exit(1);                                                                       
}                                                                                
                                                                                 
                                                                                 
                                                                                 
/****************************************************************************    
  function:      reap_status                                                     
  description:   handle a SIGCLD signal by reaping the exit status of the        
                 perished child, and discarding it.                              
  arguments:     none.                                                           
  return value:  none.                                                           
  calls:         none.                                                           
  globals:       none.                                                           
 ****************************************************************************/   
                                                                                 
void reap_status ()                                                              
{                                                                                
  int pid;                                                                       
  union wait status;                                                             
                                                                                 
  while ((pid = wait3(&status,WNOHANG,NULL)) > 0)                                
    ;  /* loop while there are more dead children */                             
}                                                                                
                                                                                 
                                                                                 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -