volta.h
changeset 14 51eb85ae4de4
parent 13 23a242d7b7fa
child 15 2706fc514dea
equal deleted inserted replaced
13:23a242d7b7fa 14:51eb85ae4de4
    39 #include <stdio.h>
    39 #include <stdio.h>
    40 #include <stdlib.h>
    40 #include <stdlib.h>
    41 #include <stdarg.h>
    41 #include <stdarg.h>
    42 #include <errno.h>
    42 #include <errno.h>
    43 #include <string.h>
    43 #include <string.h>
       
    44 #include <strings.h>
    44 #include <unistd.h>
    45 #include <unistd.h>
       
    46 #include <ctype.h>
    45 #include <time.h>
    47 #include <time.h>
    46 #include <sys/stat.h>
    48 #include <sys/stat.h>
    47 #include <signal.h>
    49 #include <signal.h>
       
    50 #include <fcntl.h>
       
    51 #include <sys/types.h>
       
    52 #include <regex.h>
    48 
    53 
    49 #include <sys/types.h>
    54 /*
    50 #include <sys/socket.h>
    55 #include <sys/socket.h>
    51 #include <netinet/in.h>
    56 #include <netinet/in.h>
    52 #include <arpa/inet.h>
    57 #include <arpa/inet.h>
       
    58 */
       
    59 
       
    60 #include <cdb.h>
    53 
    61 
    54 #ifdef DEBUG
    62 #ifdef DEBUG
    55 #include <google/profiler.h>
    63 #include <google/profiler.h>
    56 #endif
    64 #endif
    57 
    65 
    58 /* Default line size we accept from squid, longer lines (huge URLs?) malloc. */
    66 /* Default line size we accept from squid, longer lines (huge URLs?) malloc. */
    59 #define LINE_BUFSIZE 2048
    67 #define LINE_BUFSIZE 2048
    60 /* Ceiling for how many bytes can be allocated at one for a single line. */
    68 /* Ceiling for how many bytes can be allocated at once for a single line. */
    61 #define LINE_MAX 256000 /* 250k */
    69 #define LINE_MAX 256000 /* 250k */
       
    70 /* Maximum DB results for a single query */
       
    71 #define DB_RESULTS_MAX 1000
    62 
    72 
    63 /* Redirect types */
    73 /* Parsed line types */
    64 #define REDIR_TEMPORARY   0
    74 #define REQUEST 1
    65 #define REDIR_PERMANENT   1
    75 #define RULE 2
    66 #define REDIR_TRANSPARENT 2
       
    67 
    76 
    68 /* Aid debugging */
    77 /* Aid debugging */
    69 #define LOC __FILE__, __LINE__
    78 #define LOC __FILE__, __LINE__
       
    79 
    70 
    80 
    71 /*
    81 /*
    72  * a global struct for easy access to common vars 
    82  * a global struct for easy access to common vars 
    73  *
    83  *
    74  */
    84  */
    75 struct v_globals {
    85 struct v_globals {
    76 	unsigned short int debugmode; /* debug level */
    86 	unsigned short int debugmode; /* debug level */
    77 	char dbname[128];             /* path to database file */
    87 	char dbname[128];             /* path to database file */
    78 	struct sqlite3 *db;           /* database handle */
    88 	short int db_fd;              /* opened db file descriptor */
    79 
       
    80 	/* prepared statements */
       
    81 	struct {
       
    82 		struct sqlite3_stmt *match_request;
       
    83 		struct sqlite3_stmt *get_rewrite_rule;
       
    84 	} db_stmt;
       
    85 
    89 
    86 	struct {
    90 	struct {
    87 		time_t start;            /* start time */
    91 		time_t start;             /* start time */
    88 		unsigned long int lines; /* line count for determining speed */
    92 		unsigned long int lines;  /* line count for determining speed */
       
    93 		time_t db_lastcheck;      /* reopen db time */
    89 	} timer;
    94 	} timer;
    90 };
    95 };
    91 extern struct v_globals v;        /* defined in main.c */
    96 extern struct v_globals v;        /* defined in main.c */
    92 
    97 
       
    98 
    93 /*
    99 /*
    94  * The parsed attributes from the request line, as given to us by squid.
   100  * A line of parsed ascii DB input, for conversion into cdb.
    95  *
   101  *
    96  */
   102  */
    97 typedef struct request {
   103 struct db_input {
       
   104 	unsigned int klen;
       
   105 	unsigned int vlen;
       
   106 	char *key;
       
   107 	char *val;
       
   108 	char *kstart;
       
   109 	char *vstart;
       
   110 };
       
   111 
       
   112 
       
   113 /*
       
   114  * The parsed attributes from the request line as given to us by squid,
       
   115  * or from the rule string found in the database.  Unparsed
       
   116  * members are just left NULL.
       
   117  *
       
   118  */
       
   119 typedef struct parsed {
       
   120 	unsigned short int type;
       
   121 	char   *path_re;
       
   122 	char   *redir;
    98 	char   *scheme;
   123 	char   *scheme;
    99 	char   *host;
   124 	char   *host;
   100 	char   *tld;
   125 	char   *tld;
   101 	char   *path;
   126 	char   *path;
   102 	unsigned short int port;
   127 	char   *port;
   103 	struct in_addr *client_ip;
   128 	/* struct in_addr *client_ip; */
       
   129 	char   *client_ip;
   104 	char   *user;
   130 	char   *user;
   105 	char   *method;
   131 	char   *method;
   106 
   132 
   107 	struct {
   133 	struct {
       
   134 		char *path_re_start;
       
   135 		char *redir_start;
   108 		char *scheme_start;
   136 		char *scheme_start;
   109 		char *host_start;
   137 		char *host_start;
   110 		char *port_start;
   138 		char *port_start;
   111 		char *path_start;
   139 		char *path_start;
   112 		char *meth_start;
   140 		char *meth_start;
   113 		char *c_ip_start;
   141 		char *c_ip_start;
       
   142 		unsigned short int path_re_length;
       
   143 		unsigned short int redir_length;
   114 		unsigned short int scheme_length;
   144 		unsigned short int scheme_length;
   115 		unsigned short int host_length;
   145 		unsigned short int host_length;
   116 		unsigned short int port_length;
   146 		unsigned short int port_length;
   117 		unsigned short int path_length;
   147 		unsigned int       path_length;
   118 		unsigned short int meth_length;
   148 		unsigned short int meth_length;
   119 		unsigned short int c_ip_length;
   149 		unsigned short int c_ip_length;
   120 	} tokens;
   150 	} tokens;
   121 } request;
   151 } parsed;
   122 
       
   123 /*
       
   124  * The URL elements to rewrite a user's request into.
       
   125  *
       
   126  */
       
   127 typedef struct rewrite {
       
   128 	char *scheme;
       
   129 	char *host;
       
   130 	char *path;
       
   131 	unsigned short int port;
       
   132 	unsigned short int redir;
       
   133 } rewrite;
       
   134 
   152 
   135 
   153 
   136 /*
   154 /*
   137  *
   155  *
   138  * Function prototypes
   156  * Function prototypes
   139  *
   157  *
   140  */
   158  */
   141 int getopt( int, char * const [], const char *);
   159 int  getopt( int, char * const [], const char *);
       
   160 void shutdown_handler( int );
       
   161 void shutdown_actions( void );
   142 
   162 
   143 void usage( char * );
   163 void usage( char * );
   144 void debug( int, char *, int, const char *, ... );
   164 void debug( int, char *, int, const char *, ... );
   145 void out( const char * );
   165 void out( const char * );
   146 void reverse_str( char * );
   166 void reverse_str( char * );
       
   167 void lowercase_str( char *, unsigned short int );
   147 void report_speed( void );
   168 void report_speed( void );
   148 char *slurp_file( char * );
   169 char *slurp_file( char * );
   149 char *extend_line( char *, const char * );
   170 char *extend_line( char *, const char * );
   150 char *copy_string_token( char *, unsigned short int );
   171 char *copy_string_token( char *, unsigned short int );
   151 struct in_addr *copy_ipv4_token( char *, unsigned short int );
   172 /* struct in_addr *copy_ipv4_token( char *, unsigned short int ); */
   152 
   173 
   153 void shutdown_handler( int );
   174 int  accept_loop( void );
   154 void shutdown_actions( void );
       
   155 int accept_loop( void );
       
   156 void process( char * );
   175 void process( char * );
   157 request *parse( char * );
   176 parsed *init_parsed( void );
   158 request *init_request( void );
   177 parsed *parse_request( char * );
   159 void populate_request( request * );
   178 parsed *parse_rule( char * );
   160 void parse_tld( request * );
   179 void populate_parsed( parsed * );
   161 void parse_port( request * );
   180 void parse_tld( parsed * );
   162 void finish_request( request * );
   181 void finish_parsed( parsed * );
       
   182 void reset_results( parsed **, unsigned int );
       
   183 parsed *find_matching_rule( parsed **, unsigned int, parsed * );
       
   184 void rewrite( parsed *, parsed * );
   163 
   185 
   164 #endif
   186 #endif
   165 
   187