home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Magazine / wwwoffle-2.1.tar.gz / wwwoffle-2.1 / connect.c < prev    next >
C/C++ Source or Header  |  1998-03-02  |  7KB  |  260 lines

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