"Fossies" - the Fresh Open Source Software Archive

Member "srg-1.3.6/src/filterSites.cc" (5 Aug 2009, 3719 Bytes) of package /linux/privat/old/srg-1.3.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2     SRG - Squid Report Generator
    3     Destination site filtering.
    4     
    5     Copyright (C) 2008 Matt Brown <matt@mattb.net.nz>
    6 
    7     This file is part of SRG.
    8 
    9     SRG is free software; you can redistribute it and/or modify
   10     it under the terms of the GNU General Public License as published by
   11     the Free Software Foundation version 2.
   12 
   13     SRG is distributed in the hope that it will be useful,
   14     but WITHOUT ANY WARRANTY; without even the implied warranty of
   15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16     GNU General Public License for more details.
   17 
   18     You should have received a copy of the GNU General Public License
   19     along with SRG; if not, write to the Free Software
   20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   21 
   22 */
   23 
   24 #include "srg.h"
   25 #include "prototypes.h"
   26 
   27 extern Resolver *dnscache;
   28 
   29 /* Global Data Structures */
   30 list<char *> exclude_hostnames;
   31 list<exclude_netblock> exclude_netblocks;
   32 
   33 /* Initialise the IP address to user mappings */
   34 void init_sitefilter(void) {
   35 
   36     /* Exit if no site filter file specified */
   37     if (!srg.sitefilter)
   38         return;
   39 
   40     /* Read the file */
   41     Line *iLine = new Line(srg.sitefilter);
   42     if (iLine->getError() || iLine->eof()) {
   43         fprintf(stderr, "%s: Could not open the destination site "
   44                 "filter file: %s\n", progname, srg.sitefilter);
   45         exit(1);
   46     }
   47 
   48     /* Process each line of the file */
   49     while(!iLine->eof()) {
   50         char *excludeLine = iLine->getline();
   51 
   52         /* Check for comment lines */
   53         if (excludeLine[0] == '\0' || excludeLine[0] == '#')
   54             continue;
   55 
   56         /* Parse the exclusion type and data */
   57         char hostname[1024] = {'\0'};
   58         char network[1024] = {'\0'};
   59         char netmask[1024] = {'\0'};
   60         if (sscanf(excludeLine, "host %1023s", hostname) != 1) {
   61             if (sscanf(excludeLine, "subnet %1023s %1023s",
   62                         network, netmask) != 2) {
   63                 fprintf(stderr, "%s: Invalid line in destination site "
   64                         "filter file: %s\n", progname, srg.sitefilter);
   65                 exit(1);
   66             }
   67         }
   68 
   69         /* If a hostname was set, store it and continue */
   70         if (hostname[0] != '\0') {
   71             exclude_hostnames.push_back(strdup(hostname));
   72             continue;
   73         }
   74 
   75         /* If no hostname was set, try and parse the network/netmask */
   76         exclude_netblock this_netblock;
   77         if (inet_aton(network, &this_netblock.network)==0) {
   78             fprintf(stderr, "Invalid network Address in destination "
   79                     "site filter file: %s\n", network);
   80             exit(1);
   81         }
   82         if (inet_aton(netmask, &this_netblock.netmask)==0) {
   83             fprintf(stderr, "Invalid netmask in destination site "
   84                     "filter file: %s\n", netmask);
   85             exit(1);
   86         }
   87 
   88         /* Add this entry to the list */
   89         exclude_netblocks.push_back(this_netblock);
   90     }
   91 
   92     /* Free memory */
   93     delete iLine;
   94 
   95 }
   96 
   97 /* Returns true if the specified destination site matches one of the entries in
   98  * the destination site filter file. */
   99 bool destination_is_excluded(const char *site_hostname) {
  100 
  101     /* Check destination against excluded hostnames first */
  102     list<char *>::const_iterator iter;
  103     for (iter=exclude_hostnames.begin(); iter != exclude_hostnames.end();
  104             iter++) {
  105         if (strcasecmp((const char *)(*iter), site_hostname) == 0) {
  106             return true;
  107         }
  108     }
  109 
  110     /* Don't continue if there are not netblocks, resolving is slow. */
  111     if (exclude_netblocks.empty()) {
  112         return false;
  113     }
  114 
  115     /* Didn't match any hostnames, resolve to an IP and check netblocks */
  116     in_addr dest_ip;
  117     if (dnscache->get_ip(site_hostname, &dest_ip) != 0) {
  118         /* Could not resolve site_hostname to an IP */
  119         return false;
  120     }
  121     list<exclude_netblock>::const_iterator nb_iter;
  122     for (nb_iter=exclude_netblocks.begin();
  123             nb_iter != exclude_netblocks.end();
  124             nb_iter++) {
  125         if ((dest_ip.s_addr&(*nb_iter).netmask.s_addr)==
  126                 (*nb_iter).network.s_addr) {
  127             return true;
  128         }
  129     }
  130 
  131     /*  Not found in either list*/
  132     return false;
  133 }