[squid-users] suggestions on my redirector code

From: <Theresa.hilding@dont-contact.us>
Date: Mon, 11 Aug 2003 12:26:35 -0700

Hello,

I am running squid version 2.5.STABLE3 on a RedHat 9 box. In my environment
I need to redirect users to a default page if there has been no activity
from their ip address for a predetermined timeout interval (now set to 15
minutes). I have modified the redirector program that came with squid. It
works well in my test environment but I am no expert on the http protocol or
LINUX and know there is some error checking that I should probably add to
the code but don't know where to go from here. For example, should I
redirect GET requests only? Is there a more efficient way to update a file's
modification date than using system("touch <fn>")?

I have chosen to use file modification dates to determine the last time an
ip address issued an http request. The filename is the ip address. This
seems to work well for my environment with less than 200 users.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>

#define BUFFER_SIZE (16*1024)
#define INACTIVITY_TIMER (60*15)
#define FULL_PATH_START 6
#define IPADDRESS_START 27

int
main()
{
    char buf[BUFFER_SIZE+1];
    char *s, *t;
    int tlu = 0, touchfile;
    
    /* 0123456789012345678901234567 */
    char fn[100] = "touch /usr/local/redir-gcc/xxx.xxx.xxx.xxx";
    struct stat sbp;
 
    /* make standard output line buffered */
    if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
        return 1;
   
    /* scan standard input */
    while (fgets(buf, BUFFER_SIZE, stdin) != NULL) {
        /* check for too long urls */
        if (strchr(buf,'\n') == NULL) {
            tlu = 1;
            continue;
        }
        touchfile = 0;
        if ( tlu ) goto dont_redirect;

        /* determine end of url */
        if ((s = strchr(buf, ' ')) == NULL) goto dont_redirect;
        s++;
        /* get ip address of client */
        if ((t = strchr(s,'/')) == NULL) goto dont_redirect;
        *t = '\0';
        touchfile = 1;

        sprintf(&fn[IPADDRESS_START],"%s",s);

        if ( stat( (const char*) &fn[FULL_PATH_START], &sbp ) == -1 ) {
            /* errno == 2 mean file does not exist */
            if ( errno != 2 ) {
                goto dont_redirect;
            }
        } else {
            if ( sbp.st_mtime >= (time(NULL) - INACTIVITY_TIMER)) {
                goto dont_redirect;
            }
        }
        /* update file modification date or create file */
        system(&fn[0]);

        /* default wireless home page (set to google for testing) */
        (void) printf("302:http://www.google.com/\n");
        continue;

        dont_redirect:
            if ( touchfile ) system(&fn[0]);
            tlu = 0;
            (void) printf("\n");
   }
   return 0;
}

Thanks for any tips,

Theresa Hilding
Received on Mon Aug 11 2003 - 13:25:56 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 17:18:49 MST