connect.c
上传用户:seven77cht
上传日期:2007-01-04
资源大小:486k
文件大小:8k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/connect.c 2.21 1999/08/08 10:27:45 amb Exp $
  3.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.5.
  4.   Handle WWWOFFLE connections received by the demon.
  5.   ******************/ /******************
  6.   Written by Andrew M. Bishop
  7.   This file Copyright 1996,97,98,99 Andrew M. Bishop
  8.   It may be distributed under the GNU Public License, version 2, or
  9.   any higher version.  See section COPYING of the GNU Public license
  10.   for conditions under which this file may be redistributed.
  11.   ***************************************/
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <time.h>
  16. #include <signal.h>
  17. #include <unistd.h>
  18. #include "wwwoffle.h"
  19. #include "misc.h"
  20. #include "config.h"
  21. #include "errors.h"
  22. #include "sockets.h"
  23. /*+ The time that the program went online. +*/
  24. extern time_t OnlineTime;
  25. /*+ The server sockets that we listen on +*/
  26. extern int http_fd,             /*+ for the HTTP connections. +*/
  27.            wwwoffle_fd;         /*+ for the WWWOFFLE connections. +*/
  28. /*+ The proxy server that we use. +*/
  29. extern char *proxy;
  30. /*+ The online / offline / autodial status. +*/
  31. extern int online;
  32. /*+ The current number active servers +*/
  33. extern int n_servers,           /*+ in total. +*/
  34.            n_fetch_servers;     /*+ fetching a previously requested page. +*/
  35. /*+ The wwwoffle client file descriptor when fetching. +*/
  36. extern int fetch_fd;
  37. /*+ The pids of the servers. +*/
  38. extern int server_pids[MAX_SERVERS];
  39. /*+ The pids of the servers that are fetching. +*/
  40. extern int fetch_pids[MAX_FETCH_SERVERS];
  41. /*+ The current status, fetching or not. +*/
  42. extern int fetching;
  43. static void ForkRunModeScript(char *filename,char *mode,int client);
  44. /*++++++++++++++++++++++++++++++++++++++
  45.   Parse a request that comes from wwwoffle.
  46.   int client The file descriptor that corresponds to the wwwoffle connection.
  47.   ++++++++++++++++++++++++++++++++++++++*/
  48. void CommandConnect(int client)
  49. {
  50.  char *line=NULL;
  51.  if(!(line=read_line_or_timeout(client,line,SocketTimeout)))
  52.    {PrintMessage(Warning,"Nothing to read from the wwwoffle control socket [%!s]."); return;}
  53.  if(strncmp(line,"WWWOFFLE ",9))
  54.    {
  55.     PrintMessage(Warning,"WWWOFFLE Not a command."); /* Used in audit-usage.pl */
  56.     return;
  57.    }
  58.  if(PassWord || !strncmp(&line[9],"PASSWORD ",9))
  59.    {
  60.     char *password;
  61.     if(strlen(line)<18)
  62.        password="";
  63.     else
  64.       {
  65.        int i;
  66.        for(i=18;line[i];i++)
  67.           if(line[i]=='r' || line[i]=='n')
  68.              line[i]=0;
  69.        password=&line[18];
  70.       }
  71.     if(strcmp(password,PassWord))
  72.       {
  73.        write_string(client,"WWWOFFLE Incorrect Passwordn");
  74.        PrintMessage(Warning,"WWWOFFLE Incorrect Password."); /* Used in audit-usage.pl */
  75.        return;
  76.       }
  77.     if(!(line=read_line_or_timeout(client,line,SocketTimeout)))
  78.       {PrintMessage(Warning,"Unexpected end of wwwoffle control command [%!s]."); return;}
  79.     if(strncmp(line,"WWWOFFLE ",9))
  80.       {
  81.        PrintMessage(Warning,"WWWOFFLE Not a command."); /* Used in audit-usage.pl */
  82.        return;
  83.       }
  84.    }
  85.  if(!strncmp(&line[9],"ONLINE",6))
  86.    {
  87.     if(online==1)
  88.        write_string(client,"WWWOFFLE Already Onlinen");
  89.     else
  90.       {
  91.        write_string(client,"WWWOFFLE Now Onlinen");
  92.        PrintMessage(Important,"WWWOFFLE Online."); /* Used in audit-usage.pl */
  93.        CycleLastTimeSpoolFile();
  94.        ForkRunModeScript(RunOnline,"online",client);
  95.       }
  96.     OnlineTime=time(NULL);
  97.     online=1;
  98.    }
  99.  else if(!strncmp(&line[9],"AUTODIAL",8))
  100.    {
  101.     if(online==-1)
  102.        write_string(client,"WWWOFFLE Already in Autodial Moden");
  103.     else
  104.       {
  105.        write_string(client,"WWWOFFLE Now In Autodial Moden");
  106.        PrintMessage(Important,"WWWOFFLE In Autodial Mode."); /* Used in audit-usage.pl */
  107.        ForkRunModeScript(RunAutodial,"autodial",client);
  108.       }
  109.     OnlineTime=time(NULL);
  110.     online=-1;
  111.    }
  112.  else if(!strncmp(&line[9],"OFFLINE",7))
  113.    {
  114.     if(!online)
  115.        write_string(client,"WWWOFFLE Already Offlinen");
  116.     else
  117.       {
  118.        write_string(client,"WWWOFFLE Now Offlinen");
  119.        PrintMessage(Important,"WWWOFFLE Offline."); /* Used in audit-usage.pl */
  120.        ForkRunModeScript(RunOffline,"offline",client);
  121.       }
  122.     online=0;
  123.    }
  124.  else if(!strncmp(&line[9],"FETCH",5))
  125.    {
  126.     if(fetch_fd!=-1)
  127.        write_string(client,"WWWOFFLE Already fetching.n");
  128.     else if(online!=1)
  129.        write_string(client,"WWWOFFLE Must be online to fetch.n");
  130.     else
  131.       {
  132.        write_string(client,"WWWOFFLE Now Fetching.n");
  133.        PrintMessage(Important,"WWWOFFLE Fetch."); /* Used in audit-usage.pl */
  134.       }
  135.     
  136.     if(fetch_fd==-1 && online==1)
  137.       {
  138.        RequestMonitoredPages();
  139.        fetch_fd=client;
  140.        fetching=1;
  141.       }
  142.    }
  143.  else if(!strncmp(&line[9],"CONFIG",6))
  144.    {
  145.     write_string(client,"WWWOFFLE Reading Configuration File.n");
  146.     PrintMessage(Important,"WWWOFFLE Re-reading Configuration File."); /* Used in audit-usage.pl */
  147.     if(ReadConfigFile(client))
  148.       {
  149.        PrintMessage(Warning,"Error in configuration file; keeping old values.");
  150.        write_string(client,"WWWOFFLE Error Reading Configuration File.n");
  151.       }
  152.     else
  153.        write_string(client,"WWWOFFLE Read Configuration File.n");
  154.     PrintMessage(Important,"WWWOFFLE Finished Re-reading Configuration File.");
  155.    }
  156.  else if(!strncmp(&line[9],"PURGE",5))
  157.    {
  158.     pid_t pid;
  159.     if((pid=fork())==-1)
  160.       {PrintMessage(Warning,"Cannot fork to do a purge [%!s].");return;}
  161.     else if(!pid)
  162.       {
  163.        if(fetch_fd!=-1)
  164.           CloseSocket(fetch_fd);
  165.        CloseSocket(http_fd);
  166.        CloseSocket(wwwoffle_fd);
  167.        write_string(client,"WWWOFFLE Purge Starting.n");
  168.        PrintMessage(Important,"WWWOFFLE Purge."); /* Used in audit-usage.pl */
  169.        PurgeCache(client);
  170.        write_string(client,"WWWOFFLE Purge Finished.n");
  171.        PrintMessage(Important,"WWWOFFLE Purge finished.");
  172.        exit(0);
  173.       }
  174.    }
  175.  else if(!strncmp(&line[9],"KILL",4))
  176.    {
  177.     write_string(client,"WWWOFFLE Kill Signalled.n");
  178.     PrintMessage(Important,"WWWOFFLE Kill."); /* Used in audit-usage.pl */
  179.     kill(getpid(),SIGTERM);
  180.    }
  181.  else
  182.    {
  183.     while(line[strlen(line)-1]=='r' || line[strlen(line)-1]=='n')
  184.        line[strlen(line)-1]=0;
  185.     write_formatted(client,"WWWOFFLE Unknown Command '%s'.",line);
  186.     PrintMessage(Warning,"WWWOFFLE Unknown control command '%s'.",line); /* Used in audit-usage.pl */
  187.    }
  188.  if(line)
  189.     free(line);
  190. }
  191. /*++++++++++++++++++++++++++++++++++++++
  192.   Run the associated program when changing mode.
  193.   char *filename The name of the program to run or NULL if none.
  194.   char *mode The new mode to use as the argument.
  195.   int client The current client socket to be closed.
  196.   ++++++++++++++++++++++++++++++++++++++*/
  197. static void ForkRunModeScript(char *filename,char *mode,int client)
  198. {
  199.  pid_t pid;
  200.  if(!filename)
  201.     return;
  202.  if((pid=fork())==-1)
  203.    {PrintMessage(Warning,"Cannot fork to run the run-%s program [%!s].",mode);return;}
  204.  else if(!pid) /* The child */
  205.    {
  206.     if(fetch_fd!=-1)
  207.        CloseSocket(fetch_fd);
  208.     CloseSocket(http_fd);
  209.     CloseSocket(wwwoffle_fd);
  210.     CloseSocket(client);
  211.     execl(filename,filename,mode,NULL);
  212.     PrintMessage(Warning,"Cannot exec the run-%s program '%s' [%!s].",filename);
  213.     exit(1);
  214.    }
  215. }
  216. /*++++++++++++++++++++++++++++++++++++++
  217.   Fork a wwwoffles server.
  218.   int client The file descriptor that the data comes in on.
  219.   int browser Set to true if there is a browser connection.
  220.   ++++++++++++++++++++++++++++++++++++++*/
  221. void ForkServer(int client,int browser)
  222. {
  223.  pid_t pid;
  224.  int i;
  225.  if((pid=fork())==-1)
  226.     PrintMessage(Warning,"Cannot fork a server [%!s].");
  227.  else if(pid) /* The parent */
  228.    {
  229.     for(i=0;i<MaxServers;i++)
  230.        if(server_pids[i]==0)
  231.          {server_pids[i]=pid;break;}
  232.     n_servers++;
  233.     if(online==1 && !browser)
  234.       {
  235.        for(i=0;i<MaxFetchServers;i++)
  236.           if(fetch_pids[i]==0)
  237.             {fetch_pids[i]=pid;break;}
  238.        n_fetch_servers++;
  239.       }
  240.     /* Used in audit-usage.pl */
  241.     PrintMessage(Inform,"Forked wwwoffles -%s (pid=%d).",online==1?browser?"real":"fetch":online==-1?"autodial":"spool",pid);
  242.    }
  243.  else /* The child */
  244.    {
  245.     int status;
  246.     if(fetch_fd!=-1 && fetch_fd!=client)
  247.        CloseSocket(fetch_fd);
  248.     CloseSocket(http_fd);
  249.     CloseSocket(wwwoffle_fd);
  250.     status=wwwoffles(online,browser,client);
  251.     exit(status);
  252.    }
  253. }