Main Page | Alphabetical List | Class List | File List | Class Members | File Members

anon.h File Reference

#include "ip_structs.h"
#include "tcp_structs.h"

Include dependency graph for anon.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  sandnb
struct  nb_table
struct  dialog

Defines

#define min(a, b)   ((a)<(b)?(a):(b))
#define max(a, b)   ((a)>=(b)?(a):(b))

Typedefs

typedef sandnb string_and_nb

Functions

nb_tableclone_nb_table (nb_table table)
void packet_handler (u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
 The packet handler function, which is called for every processed packet.
void display_packet_infos (int nb, struct timeval *ts, char *ip_src, char *ip_dst, int sport, int dport)
 Displays infos about a IP packet.
void ts_print (struct timeval *tvp_)
 Prints a timestamp following this format: 16:21:48.970264.
void ts_print_diff (struct timeval *tvp_)
 Prints a time difference following this format: 14.95ms.
char * strptime (const char *s, const char *format, struct tm *tm)
 Libc function, which converts a character string to values which are stored in a tm structure, using a specified format.
int32_t gmt2local (time_t t)
 Returns the difference between gmt and local time in seconds. This function is borrowed from tcpdump (www.tcpdump.org/).
void convert_to_small (char *ptr)
 Converts a string to lowercase.
dialogbuffer_request (char *request, int len, int nb, char *ip_src, char *ip_dst, int port_src, int port_dst)
 Buffers a request in memory.
dialogfind_request (char *ip_src, char *ip_dst, int port_src, int port_dst)
 Finds a request in a dialog, matching some criteria.
char * generate_anonymised_url (char *s)
 Generates an anonymised URL from a genuine URL.
void add_anonymised_url (char *url, char *newurl)
 Adds an anonymised URL the the table.
char * find_url (char *s, int offset)
 Finds an URL (anonymised or not) in the table.
char * find_anonymised_url (char *s)
 Finds an anonymised URL in the table, corresponding to the specified non anonymised URL.
void free_anonymised_url_table ()
 Frees the table of anonymised URLs.
int parse_size (char *string)
 : Parses a size in the form: "10m" or "1g", and returns the number of bytes
char * get_extension (char *s)
 Returns the file extension of an URL, without any extra parameter.
void anon_sequence (u_int32_t *seq)
 Anonymises a sequence number (4 bytes) in memory and adds it to a table (if the -u option is used).
void anon_port (u_int16_t *port)
 Anonymises a port (2 bytes) in memory and adds it to a table.
void anon_ip (struct in_addr *ip)
 Anonymises an IP (4 bytes) in memory and adds it to a table.
char * find_anonymised_ip (struct in_addr *ip)
 Finds an anonymised IP in the table.
void anon_url (char *s)
 Anonymises an URL (a string) in memory.
void anon_timestamp (register struct timeval *tvp)
 Anonymises an timestamp (8 bytes) in memory.
void anon_date (char *date)
 Anonymises a date in memory.
void anon_field (char *field, char *s)
 Anonymises a HTTP header field.
void anon_http (char *data, int len)
 Anonymises a HTTP header in memory.
void scan_http (struct dialog *d)
 Scans two HTTP headers in memory (request and response) and gathers statistics.
char * process_http (char *data, int len, int scan, int anon)
 This a wrapper to call either scan_http() or anon_http().
void content_length_stats ()
 Makes and prints statistics about content_lengh fields.
void date_stats ()
string_and_nbadd_and_count_string_with_data (string_and_nb *table, char *s, void *data)
 Adds a string in a table if not yet present. If the string is already present, a counter is incremented.
string_and_nbfind_item (string_and_nb *table, char *s)
 Lookup a string in a string table. If found, the item is returned, Null otherwise.
void display_string_table_with_data (char *title, string_and_nb *table, char *data_title, int sorted)
 Displays a string table and associated counters and data.
void free_string_table (string_and_nb *table)
 Frees a string table.
void process_dialog (struct dialog *d)
 Scans a HTTP dialog (request+response) and displays it if needed.
void free_dialog (struct dialog *d)
 Frees a HTTP dialog.
pcap_if_t * get_pcap_interface (char *name)
 Gets a network interface given a name such as "eth0".
void stats_add_server (char *s)
void stats_add_server_simple (char *s)
void stats_add_http_response_code_with_data (char *s, char *data)
void stats_add_http_version (char *s)
void stats_add_user_agent (char *s)
void stats_add_last_modified (char *s)
void stats_add_url (char *s)
void stats_add_extension (char *s)
void stats_add_suffix (char *s)
void stats_add_content_type (char *s)
void stats_add_content_type_simple (char *s)
void stats_add_content_length (char *s)
void stats_add_header (char *s)
void stats_add_url_with_data (char *s, void *data)
void stats_add_new_url_with_data (char *s, void *data)
void stats_add_sequence_number (char *s)
void stats_add_freshness (int nb)
void * xmalloc (size_t size)
 A wrapper to malloc() which quits the program if the memory allocation fails.
void * xrealloc (void *ptr, size_t size)
 A wrapper to realloc() which quits the program if the memory allocation fails.
char * is_in (char **string_table, char *string)
 Checks if a string is contained in a string array.
char * base_name (register char *f)
 Returns the base name of a file (ie: /tmp/anon ==> anon).
int is_ip_in_a_string (char *s)
 Returns 1 if the provided string is in the form "xxx.xxx.xxx.xxx".
void memset_range (char *start, char *end, int c)
 Fills a memory area with a specified character (this is a wrapper for memset).
char * strstr_limited (char *ptr, char *end, char *str)
 Wrapper for strstr, which limits the memory area to search.
u_int32_t getip32 (char *ip)
 Converts an IP address of the form XXX.XXX.XXX.XXX to an unsigned integer (32 bits).
char get_random_char ()
 Gets a random letter from the alphabet.
void catch_ctrl_c (int signum)
 Signal Handler to catch CTRL+C.
char * compute_flags (u_int8_t flags)
 Builds a string containing a textual representation of the flags present in a TCP header.
char * add_flag (char *s, char *flag)
 It is a small function called by compute_flags().
char * get_time (double t)
 Returns a human readable string representing a time.
char * get_size (int s)
 Returns a human readable string representing a size in bytes.
char * get_speed (double speed)
 Returns a human readable string representing a speed in bytes/s.
char * get_pct (double pct)
 Returns a human readable string representing a percentage.
char * string_append (char *s, const char *fmt,...)
 Appends a string to another.


Define Documentation

#define max a,
 )     ((a)>=(b)?(a):(b))
 

Definition at line 5 of file anon.h.

Referenced by content_length_stats(), and display_string_table_with_data().

#define min a,
 )     ((a)<(b)?(a):(b))
 

Definition at line 4 of file anon.h.


Typedef Documentation

typedef struct sandnb string_and_nb
 


Function Documentation

string_and_nb* add_and_count_string_with_data string_and_nb table,
char *  s,
void *  data
 

Adds a string in a table if not yet present. If the string is already present, a counter is incremented.

Parameters:
string_and_nb *table: The table to add the string to
char *s: the string to add
void *data: an optional data associated with the string
Returns:
The table containing the string

Definition at line 1761 of file anon.c.

References sandnb::data, display_raw_stats, sandnb::nb, nbpackets, sandnb::next, sandnb::s, string_append(), and xmalloc().

Referenced by stats_add_http_response_code_with_data(), and stats_add_sequence_number_with_data().

01762 {
01763         string_and_nb *ptr=table;
01764         string_and_nb *tmp;
01765         string_and_nb *prev=NULL;
01766 
01767         int ret;
01768                 
01769         //No need to sort and group results, if displaying raw stats
01770         if(!display_raw_stats)
01771         {
01772                 
01773                 while(ptr)
01774                 {
01775                         ret=strcmp(s, ptr->s);
01776                         
01777                         if(ret<0)
01778                         {
01779                                 prev=ptr;
01780                         }
01781                         else if (ret>0) break; //We have to insert the string here
01782                         else //ret=0, we've found the string
01783                         {
01784                                 if(data) 
01785                                 {
01786                                         if(ptr->data) free(ptr->data);
01787                                         ptr->data=data;
01788                                 }
01789                                 ptr->nb++;
01790                                 return table;
01791                         }
01792                         ptr=ptr->next;
01793                 }
01794         }
01795         
01796         tmp=xmalloc(sizeof(string_and_nb));
01797         
01798         if(display_raw_stats)
01799         {
01801                 tmp->s=string_append(NULL, "%i:%s",nbpackets, s);
01802         }
01803         else
01804         {
01805                 tmp->s=strdup(s);
01806         }
01807         
01808         tmp->nb=1;
01809 
01810         if(data) tmp->data=data;
01811         else tmp->data=NULL;
01812         
01813         //If this is the first string in the list
01814         if(table==NULL)
01815         {
01816                 tmp->next=NULL;
01817                 return tmp;
01818         }
01819         else
01820         {
01821                 if(prev) 
01822                 {
01823                         tmp->next=prev->next;
01824                         prev->next=tmp;
01825                         return table;
01826                 }
01827                 else
01828                 {
01829                         //We insert the new struct at the beginning of the table
01830                         tmp->next=table;
01831                         return tmp;
01832                 }
01833         }
01834 }

Here is the call graph for this function:

void add_anonymised_url char *  url_,
char *  newurl_
 

Adds an anonymised URL the the table.

Parameters:
char *url: the non anonymised url
char *newurl: the corresponding anonymised url

Definition at line 792 of file anon.c.

References anon_url_table, debug, nburl, print, and xrealloc().

Referenced by anon_url(), and generate_anonymised_url().

00793 {
00794         char *url=strdup(url_);
00795         char *newurl=strdup(newurl_);
00796         if(debug) print("ADDING URL:%s \n--> %s\n",url,newurl);
00797         nburl++;
00798         anon_url_table=(char*)xrealloc(anon_url_table, nburl*2*4);
00799         memcpy(anon_url_table+2*(nburl-1)*4,&url,4);
00800         memcpy(anon_url_table+2*(nburl-1)*4+4,&newurl,4);
00801 }

Here is the call graph for this function:

char* add_flag char *  s,
char *  flag
 

It is a small function called by compute_flags().

Parameters:
char *s: The string containing the previous flags
char *flag: The flag to add to the string
Returns:
A string containing the new flag and the previous ones

Definition at line 370 of file anon.c.

References xrealloc().

Referenced by compute_flags().

00371 {
00372         int len=0;
00373         if(s) len=strlen(s);
00374         s=xrealloc(s, len+strlen(flag)+2);
00375         if(len==0) sprintf(s+len, "%s", flag);
00376         else sprintf(s+len, ".%s", flag);
00377         return s;
00378 }

Here is the call graph for this function:

void anon_date char *  date  ) 
 

Anonymises a date in memory.

Parameters:
char *date: the non anonymised date (a string)

Definition at line 886 of file anon.c.

References date_offset, and strptime().

Referenced by anon_field().

00887 {
00888         struct tm time;
00889         char *ret;
00890         time_t nb_seconds;
00891         char buffer[100];
00892         int len=strlen(date);
00893         
00894         ret=strptime(date, "%a, %d %b %Y %T GMT", &time);
00895         nb_seconds=mktime(&time);
00896         
00897         
00898         //The first date seen in the capture will be anonymised to: Tue, 01 Jan 1980 00:00:00 GMT
00899         if(date_offset==0) date_offset=nb_seconds-(3600*24)*(365*10+2);
00900         nb_seconds-=date_offset;
00901 
00902         gmtime_r(&nb_seconds, &time);
00903         
00904         strftime(buffer, 99, "%a, %d %b %Y %T GMT", &time);
00905         
00906         //print("DATE NEW:%s\n",asctime(&time));
00907         //print("DDATE:%i\n",ttime);
00908         //strcpy(date, buffer);
00909         strncpy(date, buffer, len);
00910         date[len]='\0';
00911         //print("DATE NEW:%s\n",buffer);
00912 }

Here is the call graph for this function:

void anon_field char *  field,
char *  s
 

Anonymises a HTTP header field.

Parameters:
char *field: the field to anonymise (ie: Location:)
char *s: the field's content to anonymise (ie: www.google.com)

Definition at line 914 of file anon.c.

References anon_date(), anon_url(), anonymise, anonymise_date, anonymise_url, and is_ip_in_a_string().

Referenced by process_http().

00915 {
00916         if(strcmp(field, "Host: ")==0 || strcmp(field, "Referer: ")==0 
00917                 || strcmp(field, "Location: ")==0 || strcmp(field, "Content-Location: ")==0)
00918         {
00919                 
00920                 if(anonymise && anonymise_url)
00921                 {
00922                         //Some webservers send an IP in the Host field!
00923                         if(strcmp(field, "Host: ")==0 && is_ip_in_a_string(s+strlen(field)))
00924                         {
00925                                 snprintf(s+strlen(field), strlen(s+strlen(field)), "000.000.000.000");
00926                         }
00927                         else
00928                         {
00929                                 anon_url(s+strlen(field));
00930                         }
00931                 }
00932                 return;
00933         }
00934         
00935         if(strcmp(field, "GET ")==0)
00936         {
00937                 if(anonymise && anonymise_url)
00938                 {
00939                         char backup_char;
00940                         char *http_version=strstr(s, " HTTP/");
00941                         if(!http_version)
00942                         {
00943                                 http_version=s+strlen(s);
00944                         }
00945                         backup_char=*http_version;
00946                         *http_version='\0';
00947                         anon_url(s+strlen(field));
00948                         *http_version=backup_char;
00949                 }
00950                 return;
00951         }
00952         
00953         if(strcmp(field, "Date: ")==0 || strcmp(field, "Last-Modified: ")==0 || strcmp(field, "If-Modified-Since: ")==0 || strcmp(field, "Expires: ")==0)
00954         {
00955                 if(anonymise && anonymise_date) anon_date(s+strlen(field));
00956                 return;
00957         }
00958 
00959         memset(s+strlen(field), 'X', strlen(s)-strlen(field));
00960         return;
00961 }

Here is the call graph for this function:

void anon_http char *  data,
int  len
 

Anonymises a HTTP header in memory.

Parameters:
char *data: the HTTP header data
int len: its length

Definition at line 1253 of file anon.c.

References process_http().

Referenced by packet_handler().

01254 {
01255         process_http(data, len, 0, 1);
01256 }

Here is the call graph for this function:

void anon_ip struct in_addr *  ip  ) 
 

Anonymises an IP (4 bytes) in memory and adds it to a table.

Parameters:
struct in_addr *ip: the non anonymised IP

Definition at line 2010 of file anon.c.

References find_anonymised_ip(), FIRST_ANON_IP, firstanonip, getip32(), ip_table, nbip, and xrealloc().

Referenced by packet_handler().

02011 {
02012         char *ptr = NULL;
02013         struct in_addr newip;
02014 
02015         ptr=find_anonymised_ip(ip);
02016         if(ptr)
02017         {
02018                 memcpy (&newip, ptr + 4, 4);
02019                 
02020         }
02021         else /* We have a new IP to anonymise */
02022         {
02023                 u_int32_t tmp;
02024                 if (firstanonip == 0)
02025                         firstanonip = getip32 (FIRST_ANON_IP);
02026                 tmp = firstanonip += nbip * 16777216;
02027                 memcpy (&newip, &tmp, 4);
02028 
02029                 /* We add the original and anonymised IPs to the table */
02030 
02031                 ip_table = xrealloc (ip_table, (nbip + 1) * 8);
02032 
02033                 memcpy (ip_table + nbip * 8, ip, 4);
02034                 memcpy (ip_table + nbip * 8 + 4, &newip, 4);
02035                 nbip++;
02036         }
02037         memcpy (ip, &newip, 4);
02038 }

Here is the call graph for this function:

void anon_port u_int16_t *  port  ) 
 

Anonymises a port (2 bytes) in memory and adds it to a table.

Parameters:
struct u_int16_t *port: the non anonymised port

Definition at line 1960 of file anon.c.

References FIRST_ANON_PORT, nbports, port_table, and xrealloc().

Referenced by packet_handler().

01961 {
01962         int i;
01963         u_int16_t *port2=(u_int16_t*)port_table;
01964         u_int16_t newport = 0;
01965         /* We look for the port in the table, if it has already been anonymised */
01966         for (i = 0; i < nbports; i++)
01967         {
01968                 /* If the IP is found, we get the IP to replace it */
01969                 if(*port==*port2)
01970                 {
01971                         memcpy (&newport, port_table+i*4+ 2, 2);
01972                         break;
01973                 }
01974                 port2+=2;
01975         }
01976         if (newport == 0)       /* We have a new port to anonymise */
01977         {
01978                 newport = htons (nbports + FIRST_ANON_PORT);
01979 
01980                 /* We add the original and anonymised ports to the table */
01981                 port_table = xrealloc (port_table, (nbports + 1) * 4);
01982 
01983                 memcpy (port_table + nbports * 4, port, 2);
01984                 memcpy (port_table + nbports * 4 + 2, &newport, 2);
01985 
01986                 port_table[nbports] = *port;
01987                 port_table[nbports + 1] = newport;
01988                 nbports++;
01989         }
01990         memcpy (port, &newport, 2);
01991 }

Here is the call graph for this function:

void anon_sequence u_int32_t *  seq  ) 
 

Anonymises a sequence number (4 bytes) in memory and adds it to a table (if the -u option is used).

Parameters:
u_int32_t *seq: the non anonymised sequence number

Definition at line 880 of file anon.c.

References rand_seq_nb.

Referenced by packet_handler().

00881 {
00882         if(rand_seq_nb==0) rand_seq_nb=(u_int32_t)*seq;
00883         *seq-=(u_int32_t)rand_seq_nb%65536;
00884 }

void anon_timestamp register struct timeval *  tvp  ) 
 

Anonymises an timestamp (8 bytes) in memory.

Parameters:
register struct timeval *tvp: the non anonymised timestamp

Definition at line 415 of file anon.c.

References nbpackets, timeoffset, and timeoffset_usec.

Referenced by packet_handler().

00416 {
00417         //This if the first packet.
00418         //We take the timestamp and substract it from each following timestamp, 
00419         //so the first packet's timestamp will be 0.
00420         if(nbpackets==0) 
00421         {
00422                 timeoffset=tvp->tv_sec;
00423                 timeoffset_usec=tvp->tv_usec;
00424 
00425         }
00426         tvp->tv_sec-=timeoffset;
00427          
00428         if(timeoffset_usec <= tvp->tv_usec) tvp->tv_usec-=timeoffset_usec;
00429         else
00430         {
00431                 tvp->tv_usec-=timeoffset_usec;
00432                 tvp->tv_usec+=1000000;
00433                 tvp->tv_sec--;
00434         }
00435 }

void anon_url char *  s  ) 
 

Anonymises an URL (a string) in memory.

Parameters:
char *s: the non anonymised url

Definition at line 847 of file anon.c.

References add_anonymised_url(), find_anonymised_url, generate_anonymised_url(), and look_for_anonymised_url.

Referenced by anon_field().

00848 {
00849         char *start=strstr(s, "://");
00850         char *newurl;
00851         int found=0;
00852         
00853         if(!start) start=s;
00854         else start=start+3;
00855         
00856         newurl=find_anonymised_url(s);
00857         if(!newurl) 
00858         {
00859                 int count=0;
00860                 found=0;
00861 generate:
00862                 newurl=generate_anonymised_url(s);
00863                 
00864                 //We make sure our anonymised url is not duplicated
00865                 //After 3 tries, we give up, if the URL is "/" for example
00866                 if(look_for_anonymised_url(newurl) && count<3) 
00867                 {
00868                         count++;
00869                         free(newurl);
00870                         goto generate;
00871                 }
00872                         
00873                 add_anonymised_url(s, newurl);
00874         }
00875         else found=1;
00876         memcpy(s, newurl, strlen(s));
00877         if(found==0) free(newurl);
00878 }

Here is the call graph for this function:

char* base_name register char *  f  ) 
 

Returns the base name of a file (ie: /tmp/anon ==> anon).

Parameters:
char *f: The filename
Returns:
a string containing the base name

Definition at line 212 of file anon.c.

Referenced by main().

00213 {
00214   char *file = strrchr(f, '/');
00215   if (file == NULL) return f;
00216   return(file+1);
00217 }

struct dialog* buffer_request char *  request,
int  len,
int  nb,
char *  ip_src,
char *  ip_dst,
int  port_src,
int  port_dst
 

Buffers a request in memory.

Parameters:
char *request : A string containing the HTTP request (several lines)
int len : The len of this string
int nb : The number of the packet containing the request
char *ip_src : a string representation of the source ip, in this format: 10.0.0.1
char *ip_dst : the same for the destination ip
int port_src : the source port of the packet containing the request
int port_dst : the destination port
Returns:
a pointer on a newly allocated dialog struct, filled with the above informations

Definition at line 463 of file anon.c.

References display, dialog::ip_dst, dialog::ip_src, dialog::next, dialog::port_dst, dialog::port_src, dialog::prev, print, dialog::req_nb, dialog::request, dialog::request_len, and xmalloc().

Referenced by packet_handler().

00464 {
00465         struct dialog *newdialog;
00466 
00467         newdialog = xmalloc (sizeof (struct dialog));
00468         strcpy (newdialog->ip_src, ip_src);
00469         strcpy (newdialog->ip_dst, ip_dst);
00470         newdialog->req_nb=nb;
00471         newdialog->request_len=len;
00472         newdialog->port_src = port_src;
00473         newdialog->port_dst = port_dst;
00474         newdialog->request = request;
00475         newdialog->next=NULL;
00476         if(dialog_table==NULL)
00477         {
00478                 //We have no request buffered yet
00479                 dialog_table=newdialog;
00480                 newdialog->prev=NULL;
00481         }
00482         else
00483         {
00484                 
00485                 //We jump at the end of the list, 
00486                 //and we count the requests already buffered
00487                 int i=0;
00488                 struct dialog *ptr=dialog_table;
00489                 while(ptr && ptr->next!=NULL) 
00490                 {
00491                         if(ptr->request) i++; //We don't count the discarded responses
00492                         
00493                         ptr=ptr->next;
00494                 }
00495                 ptr->next=newdialog;
00496                 newdialog->prev=ptr;            
00497                 if(request && display) print("[%d buffered] ",i+1);
00498         }
00499 
00500         return newdialog;
00501 }

Here is the call graph for this function:

void catch_ctrl_c int  signum  ) 
 

Signal Handler to catch CTRL+C.

Parameters:
int signum: signal number with which the function has been called Here, it is SIGINT, received when the user presses CTRL+C

Definition at line 360 of file anon.c.

References adhandle.

Referenced by main().

00361 {
00362         //We break the loop and restore the initial signal handler
00363         struct sigaction sa;
00364         pcap_breakloop(adhandle);
00365         sa.sa_handler = SIG_DFL;
00366         sa.sa_flags = 0;
00367         sigaction(SIGINT, &sa, NULL);
00368 }

nb_table* clone_nb_table nb_table  table  ) 
 

Definition at line 1444 of file anon.c.

References display_raw_stats, nb_table::len, nb_table::nbpackets, nb_table::start, and xmalloc().

Referenced by content_length_stats(), and latency_stats().

01445 {
01446         nb_table *new_table=xmalloc(sizeof(nb_table));
01447         new_table->len=table.len;
01448         new_table->start=xmalloc(table.len*sizeof(int));
01449         memcpy(new_table->start, table.start, table.len*sizeof(int));
01450         if(display_raw_stats)
01451         {
01452                 new_table->nbpackets=xmalloc(table.len*sizeof(int));
01453                 memcpy(new_table->nbpackets, table.nbpackets, table.len*sizeof(int));
01454         }
01455         return new_table;
01456 }

Here is the call graph for this function:

char* compute_flags u_int8_t  flags  ) 
 

Builds a string containing a textual representation of the flags present in a TCP header.

Parameters:
u_int8_t flags: The byte in memory containing the flags
Returns:
A string in the form "PSH.ACK" etc.

Definition at line 379 of file anon.c.

References add_flag(), TCP_ACK, TCP_FIN, TCP_PUSH, TCP_RST, TCP_SYN, and TCP_URG.

Referenced by packet_handler().

00380 {
00381         char *s=NULL;
00382         if(flags & TCP_FIN)  s=add_flag(s, "FIN");
00383         if(flags & TCP_SYN)  s=add_flag(s, "SYN");
00384         if(flags & TCP_RST)  s=add_flag(s, "RST");
00385         if(flags & TCP_PUSH) s=add_flag(s, "PSH");
00386         if(flags & TCP_ACK)  s=add_flag(s, "ACK");
00387         if(flags & TCP_URG)  s=add_flag(s, "URG");
00388         if(s==NULL) s=strdup("");
00389         return s;
00390 }

Here is the call graph for this function:

void content_length_stats  ) 
 

Makes and prints statistics about content_lengh fields.

Definition at line 1568 of file anon.c.

References clone_nb_table(), display_deviation(), display_string_table_unsorted, free_nb_table(), free_string_table(), max, and summarize_stats().

Referenced by main().

01569 {
01570         string_and_nb *new_content_length_table=NULL;
01571         int max;
01572         int i;
01573         char *title_s;
01574         
01575         
01576         nb_table *cloned_cl_table=clone_nb_table(cl_table);
01577         title_s=display_deviation(cloned_cl_table, "Content-Length:");
01578         free_nb_table(*cloned_cl_table);
01579         free(cloned_cl_table);
01580         
01581         
01582         max=1000;
01583         for(i=0;i<7;i++)
01584         {
01585                 new_content_length_table=summarize_stats(&cl_table, new_content_length_table, max, NULL);
01586                 max*=10;
01587         }
01588         
01589         new_content_length_table=summarize_stats(&cl_table, new_content_length_table, -1, NULL);
01590 
01591                 
01592         display_string_table_unsorted(title_s,new_content_length_table);
01593         free(title_s);
01594         
01595         free_string_table(new_content_length_table);
01596 }

Here is the call graph for this function:

void convert_to_small char *  ptr  ) 
 

Converts a string to lowercase.

Parameters:
char *ptr: a pointer to the string to convert

Definition at line 1258 of file anon.c.

Referenced by process_http().

01259 {
01260         char c;
01261         while((c=*ptr))
01262         {
01263                 if(c>='A' && c<='Z') *ptr=c-'A'+'a';
01264                 ptr++;
01265         }
01266 }

void date_stats  ) 
 

void display_packet_infos int  nb,
struct timeval *  ts,
char *  ip_src,
char *  ip_dst,
int  sport,
int  dport
 

Displays infos about a IP packet.

Definition at line 392 of file anon.c.

References display, display_timestamp, max_display_space, print, and ts_print().

Referenced by packet_handler().

00393 {
00394         //We format the output to be more readable
00395         char display_space[100];
00396         int len=0;
00397         len=sprintf(display_space, "%s:%d",ip_src, sport);
00398         if(len>max_display_space) max_display_space=len;
00399         len=sprintf(display_space, "%s:%d",ip_dst, dport);
00400         if(len>max_display_space) max_display_space=len;
00401                 
00402         memset(display_space,' ',100);
00403         sprintf(display_space, "%s:%d                ",ip_src, sport);
00404         sprintf(display_space+max_display_space, " --> %s:%d                ",ip_dst, dport);
00405         display_space[max_display_space*2+5]='\0';
00406         
00407         if(ts)
00408         {
00409                 print ("[%5i] ", nb);
00410                 if(display && display_timestamp) ts_print(ts);          
00411         }
00412         print("%s ",display_space);
00413 }

Here is the call graph for this function:

void display_string_table_with_data char *  title,
string_and_nb table,
char *  data_title,
int  sorted
 

Displays a string table and associated counters and data.

Parameters:
char *title: The title of the table
string_and_nb *table: the table to display
char *data_title: a string to display before displaying the data
sorted,: 1 if the table has to be sorted before displaying it, 0 otherwise

Definition at line 1598 of file anon.c.

References sandnb::data, display_raw_stats, get_pct(), max, sandnb::nb, sandnb::next, pct_treshold, print, sandnb::s, string_append(), and xmalloc().

01599 {
01600         string_and_nb *ptr=table;
01601         string_and_nb *ptr_bak2;
01602         int i,j;
01603         int nb=0;
01604         int max=0;
01605         char buffer[100];
01606         int len;
01607         int maxlen;
01608         int total=0;
01609         double pct;
01610         char *pct_s=NULL;
01611         int *sorted_table=NULL;
01612         int max_pos=0;
01613         int pos;
01614         string_and_nb *tmp;
01615         char *d_title=NULL;
01616 
01617         while(ptr)
01618         {
01619                 nb++;
01620                 max=max(ptr->nb , max);
01621                 total+=ptr->nb;
01622                 ptr=ptr->next;
01623         }
01624         print("#%s ",title);
01625         
01626         if(display_raw_stats) sorted=0;
01627         
01628         if(sorted) print("%i different strings ",nb);
01629         print("\n|Total count: %i\n",total);
01630         //if(sorted) print("\n");
01631         //else print("\n");
01632         maxlen=snprintf(buffer,99,"%i",max);
01633         
01634         ptr=table;
01635         
01636         sorted_table=xmalloc(nb*sizeof(nb));
01637 
01638         if(data_title && strcmp(data_title,"")!=0) d_title=strdup(data_title);
01639         else d_title=strdup(" DATA:");
01640 
01641         if(sorted)
01642         {               
01643                 //Sorting the results
01644                 for(j=0;j<nb;j++)
01645                 {
01646                         pos=0;
01647                         max=0;
01648                         ptr=table;
01649                         while(ptr)
01650                         {
01651                                 if(ptr->nb > max)
01652                                 {
01653                                         max=ptr->nb;
01654                                         max_pos=pos;
01655                                         tmp=ptr;
01656                                 }
01657                                 ptr=ptr->next;
01658                                 pos++;
01659                         }
01660                         tmp->nb*=-1;
01661                         sorted_table[j]=max_pos;
01662                         
01663                 }
01664         }
01665         else //not sorted
01666         {
01667                 if(display_raw_stats)
01668                 {
01669                         for(j=0;j<nb;j++) sorted_table[j]=nb-j-1;       
01670                 }
01671                 else
01672                 {
01673                         for(j=0;j<nb;j++) sorted_table[j]=j;
01674                 }
01675         }
01676 
01677         ptr=table;
01678         ptr_bak2=table; //used if not sorted
01679         for(j=0;j<nb;j++)
01680         {
01681                 if(sorted)
01682                 {
01683                         ptr=table;
01684                         for(i=0;i<sorted_table[j];i++) ptr=ptr->next;
01685                         ptr->nb*=-1;
01686                 }
01687                 else
01688                 {
01689                         ptr=ptr_bak2;
01690                 }
01691                 pct=(double)ptr->nb/total;
01692                 
01693                 //We group the values below the specified percentage
01694                 if(pct_treshold!=0 && pct<pct_treshold/100.0 && sorted && j<nb-1
01695                         && table!=http_response_code_table) //We should pass a value to the function instead...
01696                 {
01697                         string_and_nb *ptr_bak=ptr;
01698                         char *new_string=strdup("Various: ");
01699                         int nb_total=0;
01700                         int k;
01701                         char *comma;
01702                         ptr->nb*=-1; //This nb already have been reversed
01703                         for(k=j;k<nb;k++)
01704                         {
01705                                 ptr=table;
01706                                 for(i=0;i<sorted_table[k];i++) ptr=ptr->next;
01707                                 ptr->nb*=-1;
01708                                 nb_total+=ptr->nb;
01709                                 
01710                                 if(k==nb-1) comma="";
01711                                 else comma=",";
01712                                         
01713                                 if(ptr->data==NULL)
01714                                 {
01715                                         new_string=string_append(new_string, "%s%s",ptr->s, comma);
01716                                 }
01717                                 else
01718                                 {
01719                                         new_string=string_append(new_string, "%s%s%s%s",ptr->s,d_title,(char*)ptr->data,comma);
01720                                 }
01721                         }
01722                         ptr=ptr_bak;
01723                         ptr->nb=nb_total;
01724                         free(ptr->s);
01725                         ptr->s=new_string;
01726                         if(ptr->data) {free(ptr->data);ptr->data=NULL;}
01727                         j=nb;
01728                         pct=(double)ptr->nb/total;
01729                         
01730                 }
01731                         
01732                 if(display_raw_stats)
01733                 {
01734                         print("%s\n",ptr->s);
01735                 }
01736                 else
01737                 {
01738                         pct_s=get_pct(pct);
01739                         if(!pct_s) pct_s=strdup("[??]");
01740                         
01741                         len=snprintf(buffer,99, "%i",ptr->nb);
01742                         memset(buffer, ' ', 99);
01743                         snprintf(buffer+maxlen-len,99, "%i",ptr->nb);
01744                         buffer[maxlen+1]='\0';
01745                         if(ptr->data) print("[%7s] %s %s%s%s\n",pct_s,buffer,ptr->s,d_title,(char*)ptr->data);
01746                         else print("[%7s] %s %s\n",pct_s,buffer, ptr->s);
01747                         free(pct_s);
01748                 }
01749                 if(!sorted)
01750                 {
01751                         ptr_bak2=ptr->next;
01752                 }
01753         }
01754         
01755         if(d_title) free(d_title);
01756         if(sorted_table) free(sorted_table);
01757 
01758         print("\n");
01759 }

Here is the call graph for this function:

char* find_anonymised_ip struct in_addr *  ip  ) 
 

Finds an anonymised IP in the table.

Parameters:
struct in_addr *ip: The non-anonymised IP
Returns:
a string containing the corresponding anonymised IP, or NULL if not found

Definition at line 1993 of file anon.c.

References ip_table, and nbip.

Referenced by anon_ip().

01994 {
01995         char *ptr = ip_table;
01996         int i;
01997         for (i = 0; i < nbip; i++)
01998         {
01999                 /* If the IP is found, we get the IP to replace it */
02000                 if (memcmp (ptr, ip, 4) == 0)
02001                 {
02002                         return ptr;
02003                 }
02004                 ptr += 8;
02005         }       
02006         return NULL;
02007 }

char * find_anonymised_url char *  s  ) 
 

Finds an anonymised URL in the table, corresponding to the specified non anonymised URL.

Parameters:
char *s: The non-anonymised URL
Returns:
a string containing the corresponding anonymised URL, or NULL if not found

string_and_nb* find_item string_and_nb table,
char *  s
 

Lookup a string in a string table. If found, the item is returned, Null otherwise.

Parameters:
string_and_nb *table: the table to search
char *s: the string to look for
Returns:
a pointer on a string_and_nb, giving access to the string, the counter and the associated data. Null is returned if the item is not found

Definition at line 1836 of file anon.c.

References sandnb::next, and sandnb::s.

Referenced by packet_handler(), and summarize_stats().

01837 {
01838         string_and_nb *ptr=table;
01839         int ret;
01840         while(ptr)
01841         {
01842                 ret=strcmp(s, ptr->s);
01843                 if(ret>0) return NULL;
01844                 else if(ret==0) return ptr;
01845                 ptr=ptr->next;
01846         }
01847         return NULL;
01848 }

struct dialog* find_request char *  ip_src,
char *  ip_dst,
int  port_src,
int  port_dst
 

Finds a request in a dialog, matching some criteria.

Parameters:
char *ip_src : a string representation of the source ip, in this format: 10.0.0.1
char *ip_dst : the same for the destination ip
int port_src : the source port of the packet containing the request
int port_dst : the destination port
Returns:
a pointer on the first matching dialog struct if found, NULL otherwise

Definition at line 503 of file anon.c.

References dialog::ip_dst, dialog::ip_src, dialog::next, dialog::port_dst, dialog::port_src, and dialog::request.

Referenced by packet_handler().

00504 {
00505         struct dialog *ptr = dialog_table;
00506         while (ptr)
00507         {
00508                 if (strcmp (ptr->ip_src, ip_dst) == 0
00509                     && strcmp (ptr->ip_dst, ip_src) == 0
00510                     && ptr->port_src == port_dst && ptr->port_dst == port_src
00511                         && ptr->request!=NULL)
00512                 {
00513                         return ptr;
00514                 }
00515                 ptr = ptr->next;
00516         }
00517         return NULL;
00518 }

char* find_url char *  s,
int  offset
 

Finds an URL (anonymised or not) in the table.

Parameters:
char *s: The non-anonymised URL
int offset: if offset=0: finds a non anonymised URL and returns the corresponding anonymised one if offset=4: finds an anonymised URL and returns it
Returns:
a string containing the corresponding anonymised URL, or NULL if not found

Definition at line 826 of file anon.c.

References anon_url_table, and nburl.

00827 {
00828         //offset=0: finds a non anonymised URL and returns the corresponding anonymised one
00829         //offset=4: finds an anonymised URL and returns it
00830         //print("looking for -%s-\n",s);
00831         int i;
00832         char *ptr=anon_url_table;
00833         char *tmp;
00834         for(i=0;i<nburl;i++)
00835         {
00836                 memcpy(&tmp, ptr+offset, 4);
00837                 if(strcmp(tmp,s)==0)
00838                 {
00839                         memcpy(&tmp, ptr+4, 4);
00840                         return tmp;
00841                 }
00842                 ptr+=8;
00843         }
00844         return NULL;
00845 }

void free_anonymised_url_table  ) 
 

Frees the table of anonymised URLs.

Definition at line 2557 of file anon.c.

References anon_url_table, and nburl.

Referenced by main().

02558 {
02559         char *ptr=anon_url_table;
02560         char *tmp;
02561         
02562         int i;
02563         for(i=0;i<nburl;i++)
02564         {
02565                 memcpy(&tmp, ptr, 4);
02566                 free(tmp);
02567                 memcpy(&tmp, ptr+4, 4);
02568                 free(tmp);
02569                 ptr+=8;
02570         }
02571         free(anon_url_table);
02572 }

void free_dialog struct dialog d  ) 
 

Frees a HTTP dialog.

Parameters:
struct dialog *d: the dialog structure to free

Definition at line 1921 of file anon.c.

References dialog::next, dialog::prev, dialog::request, and dialog::response.

Referenced by main(), and packet_handler().

01922 {
01923         if(d->request) free(d->request);
01924         if(d->response) free(d->response);
01925                 
01926         if(d==dialog_table)
01927         {
01928                 //If we are freeing the first dialog, we have to update dialog_table
01929                 dialog_table=d->next;
01930         }
01931         if(d->next) d->next->prev=d->prev;
01932         if(d->prev) d->prev->next=d->next;
01933         free(d);
01934 }

void free_string_table string_and_nb table  ) 
 

Frees a string table.

Parameters:
string_and_nb *table: the table to free

Definition at line 1284 of file anon.c.

References sandnb::data, sandnb::next, and sandnb::s.

Referenced by content_length_stats(), freshness_stats(), latency_stats(), and main().

01285 {
01286         string_and_nb *next=NULL;
01287         while(table)
01288         {
01289                 next=table->next;
01290                 if(table->s) free(table->s);
01291                 if(table->data) free(table->data);
01292                 free(table);
01293                 table=next;
01294         }       
01295 }

char* generate_anonymised_url char *  s  ) 
 

Generates an anonymised URL from a genuine URL.

Parameters:
char *s: the non anonymised url
Returns:
a string containing the anonymised url

Definition at line 545 of file anon.c.

References add_anonymised_url(), debug, find_anonymised_url, find_end(), get_random_char(), memset_range(), and print.

Referenced by anon_url().

00546 {
00547         char *newurl=strdup(s);
00548         char *afterproto=strstr(newurl, "://");
00549 
00550         char *first_slash;
00551         char *last_dot;
00552         char *ptr;
00553         int has_hostname=1;
00554         
00555         if(afterproto) 
00556         {
00557                 if(strncasecmp(newurl,"file:///",8)==0)
00558                 {
00559                         ptr=afterproto+4;
00560                 }
00561                 else
00562                 {
00563                         ptr=afterproto+3;
00564                 }
00565         }
00566         else 
00567         {
00568                 //This is an url without a hostname (eg GET [url])
00569                 if(*newurl=='/')
00570                 {
00571                         has_hostname=0;
00572                 }
00573                 afterproto=newurl;
00574                 ptr=newurl;
00575         }
00576         
00577         first_slash=strstr(ptr, "/");
00578 
00579         // http://hostname.com/bla.ext?xxxx
00580         //        ^
00581         //        |
00582         //       ptr
00583         
00584 
00585 
00586         //Anonymising the host name
00587         if(has_hostname)
00588         {
00589                 char *ptr_orig=ptr;
00590                 //char *hostname;
00591                 char *new_hostname;
00592                 char bak;
00593                 int found=0;
00594                 if(first_slash)
00595                 {
00596                         bak=*first_slash;
00597                         *first_slash='\0';
00598                 }
00599                 new_hostname=find_anonymised_url(ptr);
00600                 
00601                         
00602                 if(!new_hostname)
00603                 {
00604                         found=0;
00605                         new_hostname=strdup(ptr);
00606                         last_dot=rindex(new_hostname,'.');
00607                         if(!last_dot) last_dot=new_hostname+strlen(new_hostname);
00608                 
00609                         ptr=new_hostname;
00610                         while(ptr<last_dot)
00611                         {
00612                                 *ptr=get_random_char();
00613                                 ptr++;
00614                         }
00615                 
00616                         //If the url is only a hostname, we won't add it twice! (since full_url=hostname)
00617                         if(strlen(ptr_orig)<strlen(s))
00618                         {
00619                                 add_anonymised_url(ptr_orig,new_hostname);
00620                         }
00621                         
00622                 }
00623                 else found=1;
00624                         
00625                 memcpy(ptr_orig, new_hostname, strlen(new_hostname));
00626                 if(found==0) free(new_hostname);
00627                 
00628                 if(first_slash) *first_slash=bak;
00629         }
00630         else
00631         {
00632                 first_slash=strstr(ptr,"/");
00633         }
00634         if(!first_slash) first_slash=newurl;
00635                 
00636         //Anonymising the part after the hostname, and before the filename
00637         if(first_slash)
00638         {
00639                 char bak;
00640                 char *ptr=first_slash+1;
00641                 char *end;
00642                 char *slash;
00643 
00644                 end=find_end(ptr);
00645                 if(!end) end=newurl+strlen(newurl);
00646                 
00647                 bak=*end;
00648                 *end='\0';
00649                 
00650                 
00651                 
00652                 slash=rindex(ptr,'/');
00653                 if(slash) 
00654                 {
00655                         memset_range(ptr, slash, 'i');
00656                         ptr=slash+1;
00657                 }
00658                 *end=bak;
00659 
00660                 //Anonymising the base file name, keeping the extension
00661                 bak=*end;
00662                 *end='\0';
00663                 last_dot=rindex(ptr,'.');
00664                 *end=bak;
00665                 if(!last_dot) last_dot=end;
00666                 //while(ptr<end)
00667                 
00668                 while(ptr<last_dot)
00669                 {
00670                         *ptr=get_random_char();
00671                         ptr++;
00672                 }
00673 
00674                 //Anonymising optional parameters (e.g. ?cat=5)
00675                 ptr=end;
00676                 
00677                 while(*ptr)
00678                 {
00679                         if(*ptr=='?' || *ptr=='&' || *ptr==';' || *ptr=='%' || *ptr=='+' || *ptr=='=' || *ptr=='!' || *ptr=='$' || *ptr=='@' || *ptr=='"' || *ptr==','){} else *ptr='z';
00680                         ptr++;
00681                 }
00682         }
00683         if(debug) print("anon URL:%s --> %s\n",s,newurl);
00684         return newurl;
00685 }

Here is the call graph for this function:

char * get_extension char *  s  ) 
 

Returns the file extension of an URL, without any extra parameter.

Parameters:
char *s: the URL
Returns:
a string containing the file extension

pcap_if_t* get_pcap_interface char *  name  ) 
 

Gets a network interface given a name such as "eth0".

Parameters:
char *name: The name of the interface to look for. If this name is "show", then a list of available interfaces is displayed.
Returns:
a pointer to a pcap_if_t, or NULL is he interface is not available.

Definition at line 2040 of file anon.c.

References alldevs, and print.

Referenced by main().

02041 {
02042         char errbuf[PCAP_ERRBUF_SIZE];
02043         pcap_if_t *d;
02044         int i = 0;
02045         int show = 0;
02046 
02047         /*
02048          * Retrieve the device list 
02049          */
02050         if (pcap_findalldevs (&alldevs, errbuf) == -1)
02051         {
02052                 print("Error in pcap_findalldevs: %s\n", errbuf);
02053                 exit (1);
02054         }
02055 
02056         if (name && strcasecmp (name, "show") == 0)
02057                 show = 1;
02058 
02059         /*
02060          * Print the list 
02061          */
02062         if (show)
02063                 print ("Available interfaces for live capture:\n");
02064         for (d = alldevs; d; d = d->next)
02065         {
02066                 if (name == NULL)
02067                         return d;
02068                 if (show)
02069                 {
02070                         print ("    %s", d->name);
02071                         if (d->description)
02072                                 print (" (%s)\n", d->description);
02073                         else
02074                                 print (" (No description available)\n");
02075                 }
02076                 else
02077                 {
02078                         if (name && strcmp (d->name, name) == 0)
02079                                 return d;
02080                 }
02081                 i++;
02082         }
02083         if(alldevs) 
02084         {
02085                 pcap_freealldevs (alldevs);
02086                 alldevs=NULL;
02087         }
02088 
02089         if (i == 0)
02090         {
02091                 print ("\nNo interfaces found!\n");
02092                 exit (1);
02093         }
02094         return NULL;
02095 }

char* get_pct double  pct  )  [inline]
 

Returns a human readable string representing a percentage.

Definition at line 248 of file anon.c.

References string_append().

Referenced by display_string_table_with_data(), and packet_handler().

00249 {
00250         return string_append(NULL,"%.2f%c",(double)pct*100,'%');
00251 }

Here is the call graph for this function:

char get_random_char  )  [inline]
 

Gets a random letter from the alphabet.

Returns:
a random character (a letter)

Definition at line 539 of file anon.c.

Referenced by generate_anonymised_url().

00540 {
00541         return 'a' + (int)(rand()/(RAND_MAX+1.0)*26);
00542 }

char* get_size int  s  ) 
 

Returns a human readable string representing a size in bytes.

Definition at line 279 of file anon.c.

References string_append().

Referenced by main(), packet_handler(), and summarize_stats().

00280 {
00281         double size;
00282         
00283         if(s==-1)
00284         {
00285                 return strdup("???");
00286         }
00287         if(s>999999)
00288         {
00289                 size=(double) s / 1000000;
00290                 if(size==(int)s/1000000) return string_append(NULL, "%i MB",(int)size);
00291                 return string_append(NULL, "%.2f MB",size);
00292         }
00293         else if(s>999)
00294         {
00295                 size=(double) s / 1000;
00296                 if(size==(int)s/1000) return string_append(NULL, "%i KB",(int)size);
00297                 return string_append(NULL, "%.2f KB",size);
00298         }
00299         else
00300         {
00301                 size=(double) s;
00302                 if(size==(int)s) return string_append(NULL, "%i B",(int)size);
00303                 return string_append(NULL, "%.2f B",size);
00304         }
00305         return NULL;
00306 }

Here is the call graph for this function:

char* get_speed double  speed  ) 
 

Returns a human readable string representing a speed in bytes/s.

Definition at line 253 of file anon.c.

References string_append().

Referenced by main(), and packet_handler().

00254 {
00255         char *tmp;
00256         if(speed>999999)
00257         {
00258                 speed=(double) speed / 1000000;
00259                 tmp=string_append(NULL, "%.2f MB/s",speed);
00260                 
00261                 if(tmp && strncmp(tmp, "inf", 3)==0) 
00262                 {
00263                         free(tmp);
00264                         return NULL;
00265                 }
00266                 return tmp;
00267         }
00268         else if(speed>999)
00269         {
00270                 speed=(double) speed / 1000;
00271                 return string_append(NULL, "%.2f KB/s",speed);
00272         }
00273         else
00274         {
00275                 return string_append(NULL, "%.2f B/s",speed);
00276         }
00277 }

Here is the call graph for this function:

char* get_time double  t  ) 
 

Returns a human readable string representing a time.

Definition at line 308 of file anon.c.

References string_append().

Referenced by main().

00309 {
00310                 int years=0;
00311                 int months=0;
00312         int days=0;
00313         int hours=0;
00314         int minutes=0;
00315         int seconds=0;
00316         
00317         char *tmp=NULL;
00318 
00319         seconds=t;
00320         if(seconds>59)
00321         {
00322                 minutes=seconds/60;
00323                 seconds=seconds%60;
00324         }
00325         if(minutes>59)
00326         {
00327                 hours=minutes/60;
00328                 minutes=minutes%60;
00329         }
00330         if(hours>23)
00331         {
00332                 days=hours/24;
00333                 hours=hours%24;
00334         }
00335                 if(days>364)
00336                 {
00337                         years=days/365;
00338                         days=days%365;
00339                 }
00340         if(days>29)
00341         {
00342                 months=days/30;
00343                 days=days%30;
00344         }               
00345 /*        if(months>11)
00346         {
00347                 years+=months/12;
00348                 months=months%12;
00349         }               */
00350         if(years>0)             tmp=string_append(NULL, "%iy%im%id%ih%im%is",years, months, days, hours, minutes, seconds);
00351         else if(months>0)   tmp=string_append(NULL, "%im%id%ih%im%is",months, days, hours, minutes, seconds);
00352         else if(days>0)     tmp=string_append(NULL, "%id%ih%im%is",days, hours, minutes, seconds);
00353         else if(hours>0)    tmp=string_append(NULL, "%ih%im%is",hours, minutes, seconds);
00354         else if(minutes>0)  tmp=string_append(NULL, "%im%is",minutes, seconds);
00355         else                tmp=string_append(NULL, "%is",seconds);
00356         return tmp;
00357 }

Here is the call graph for this function:

u_int32_t getip32 char *  ip  ) 
 

Converts an IP address of the form XXX.XXX.XXX.XXX to an unsigned integer (32 bits).

Parameters:
char *ip: The IP address to convert
Returns:
an unsigned 32-bit integer

Definition at line 1950 of file anon.c.

Referenced by anon_ip().

01951 {
01952         int c1, c2, c3, c4;
01953         u_int32_t tmp;
01954         sscanf (ip, "%i.%i.%i.%i", &c1, &c2, &c3, &c4);
01955         tmp = c4 * 16777216 + c3 * 65536 + c2 * 256 + c1;
01956         return tmp;
01957 }

int32_t gmt2local time_t  t  ) 
 

Returns the difference between gmt and local time in seconds. This function is borrowed from tcpdump (www.tcpdump.org/).

Parameters:
time_t t: contains the specified time
Returns:
a 32-bit integer containing a number of seconds

Definition at line 29 of file gmt2local.c.

Referenced by main().

00030 {
00031         register int dt, dir;
00032         register struct tm *gmt, *loc;
00033         struct tm sgmt;
00034 
00035         if (t == 0)
00036                 t = time(NULL);
00037         gmt = &sgmt;
00038         *gmt = *gmtime(&t);
00039         loc = localtime(&t);
00040         dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
00041             (loc->tm_min - gmt->tm_min) * 60;
00042 
00043         /*
00044          * If the year or julian day is different, we span 00:00 GMT
00045          * and must add or subtract a day. Check the year first to
00046          * avoid problems when the julian day wraps.
00047          */
00048         dir = loc->tm_year - gmt->tm_year;
00049         if (dir == 0)
00050                 dir = loc->tm_yday - gmt->tm_yday;
00051         dt += dir * 24 * 60 * 60;
00052 
00053         return (dt);
00054 }

char* is_in char **  string_table,
char *  string
 

Checks if a string is contained in a string array.

Parameters:
char **string_table : An array of strings
char *string : The string to look for
Returns:
a char * containing the matched string in the array, if present NULL otherwise

Definition at line 521 of file anon.c.

References nb_fields_to_anonymise.

Referenced by process_http().

00522 {
00523         int i;
00524         for(i=0;i<nb_fields_to_anonymise;i++)
00525         {
00526                 if(strncmp(string_table[i], string, strlen(string_table[i]))==0) 
00527                 {
00528                         return string_table[i];
00529                 }
00530         }
00531         return NULL;
00532 }

int is_ip_in_a_string char *  s  ) 
 

Returns 1 if the provided string is in the form "xxx.xxx.xxx.xxx".

Parameters:
char *s: The string to analyse
Returns:
1 if the string contains an IP, 0 otherwise

Definition at line 219 of file anon.c.

Referenced by anon_field(), and get_infos().

00220 {
00221         int nb,tmp1,tmp2,tmp3,tmp4;
00222         if(!s) return(0);
00223         nb=sscanf(s, "%i.%i.%i.%i",&tmp1,&tmp2,&tmp3,&tmp4);
00224         if(nb==4) return(1);
00225         return(0);
00226 }

void memset_range char *  start,
char *  end,
int  c
 

Fills a memory area with a specified character (this is a wrapper for memset).

Parameters:
char *start : Start of the memory area
char *end : End of the memory area
int c : The character to fill the area with

Definition at line 534 of file anon.c.

Referenced by generate_anonymised_url(), and packet_handler().

00535 {
00536         if(end-start>0) memset(start, c, end-start);
00537 }

void packet_handler u_char *  param,
const struct pcap_pkthdr *  header,
const u_char *  packet
 

The packet handler function, which is called for every processed packet.

valid

Definition at line 2575 of file anon.c.

References adhandle, anon_http(), anon_ip(), anon_port(), anon_sequence(), anon_timestamp(), anonymise, anonymise_checksum, anonymise_content, anonymise_http, anonymise_ip, anonymise_mac, anonymise_port, anonymise_sequence, anonymise_timestamp, buffer_request(), compute_flags(), sandnb::data, display, display_additional_info, display_flags, display_http, display_http_only, display_packet_infos(), display_progress, display_tcp_infos, dumphandle, ETHER_HDRLEN, find_item(), find_request(), free_dialog(), get_pct(), get_size(), get_speed(), ifile, input_file_last, input_file_read, input_file_size, input_file_size_1pct, input_file_size_s, ip_s::ip_dst, IP_HL, ip_s::ip_sum, IP_V, maxfilesize, memset_range(), nb_request, nb_response, nbpackets, nbpacketstocapture, nbvalidpackets, ofile, output_file_written, output_stream, print, process_dialog(), dialog::req_nb, dialog::resp_nb, dialog::response, dialog::response_len, sandnb::s, start_time, stats_add_latency(), stats_add_sequence_number_with_data(), strstr_limited(), tcp_s::th_chksum, tcp_s::th_dport, tcp_s::th_flag, tcp_s::th_hlen_reserved, tcp_s::th_sport, truncate_packet, ts_print_diff(), dialog::ts_request, dialog::ts_response, use_sequence_numbers, and xmalloc().

Referenced by main().

02576 {
02577         const u_char *data = packet;
02578         char *eop=(char*)packet+header->caplen; //End of packet (in memory)
02579         
02580         struct ether_header *eptr;      /* net/ethernet.h */
02581         struct ip_s *ip;
02582         u_int tcp_len;
02583         u_int len=header->caplen;
02584         u_int ip_hlen;
02585         
02586         char ip_src[] = "xxx.xxx.xxx.xxx";
02587         char ip_dst[] = "xxx.xxx.xxx.xxx";
02588         char real_ip_src[] = "xxx.xxx.xxx.xxx";
02589         char real_ip_dst[] = "xxx.xxx.xxx.xxx";
02590         u_short dport, sport, realdport, realsport;
02591 
02592         struct dialog *d = NULL;
02593         char *buffer;
02594         struct tcp_s *tcp;
02595         char *flags=NULL;
02596         u_int32_t seq;
02597         char seq_nb[12];
02598         char next_seq_nb[12];
02599         string_and_nb *item=NULL;       
02600         int expected=0;
02601         char *eoh=NULL;
02602         
02603         *eop='\0';
02604         input_file_read+=header->caplen+16;
02605         input_file_last+=header->caplen+16;
02606         if(!display && display_progress)
02607         {
02608                 //stats are displayed every 1Mb (or 1%) for offline mode, or every 50kb for online mode
02609                 if((ifile && input_file_last>=input_file_size_1pct && input_file_last>1000000) || (!ifile && input_file_read-input_file_last>=50000))
02610                 {
02611                         time_t t;
02612                         double diff;
02613                         input_file_last=0;
02614                         
02615                         time(&t);
02616                         diff=input_file_read/difftime(t, start_time);
02617                         
02618                         buffer=get_size(input_file_read);
02619                         if(buffer)
02620                         {
02621                                 if(input_file_size_s)
02622                                 {
02623                                         char *buffer2=get_pct((double)input_file_read/input_file_size);
02624                                         if(buffer2)
02625                                         {
02626                                                 print("[%s] ",buffer2);
02627                                                 free(buffer2);
02628                                         }
02629                                         print("Processed %s/%s",buffer,input_file_size_s);
02630                                 }
02631                                 else print("Processed %s",buffer);
02632                                 
02633                                 free(buffer);
02634                         }
02635                                 
02636                         buffer=get_speed(diff);
02637                         if(buffer)
02638                         {
02639                                 print(" at %s",buffer);
02640                                 free(buffer);
02641                         }
02642                         if(ofile)
02643                         {
02644                                 char *tmp=get_size(output_file_written);
02645                                 if(tmp)
02646                                 {
02647                                         print(" (Written %s)",tmp);
02648                                         free(tmp);
02649                                 }
02650                         }
02651                         print("\n");
02652                         fflush(output_stream);
02653                 }
02654         }
02655         if(anonymise && anonymise_timestamp) anon_timestamp((struct timeval*)&header->ts);
02656         
02657         // Packet is smaller than ethernet header length
02658         if ((header->caplen) < ETHER_HDRLEN) goto function_end;
02659 
02660         // Anoymise source & dest MAC address 
02661         if (anonymise && anonymise_mac) memset ((char *) packet, 0, 12);
02662 
02663         eptr = (struct ether_header *) data;
02664 
02665         // We only process the packet if its type it is an IP packet.
02666         if (ntohs (eptr->ether_type) != ETHERTYPE_IP) goto function_end;
02667         if (len < sizeof (struct ip_s)) goto function_end;
02668 
02669         // We now have an IP packet
02670         data += sizeof (struct ether_header);
02671         len -= sizeof (struct ether_header);
02672                 
02673         ip = (struct ip_s *) (data);
02674 
02675         // If the ip packet hasn't been truncated and it has a correct header length
02676         if (len < sizeof (struct ip_s) || IP_HL (ip) < 5) goto function_end;
02677         if (ip->ip_p != IPPROTO_TCP) goto function_end;
02678                 
02679         if(anonymise && anonymise_checksum) ip->ip_sum=(u_int16_t)0;
02680         
02681         //We check that the used version of IP is 4
02682         if (IP_V (ip) != 4) goto function_end;
02683         
02684         ip_hlen = (ip->ip_vhl & 0x0f) * 4;
02685 
02686         // We have a TCP packet
02687         data += ip_hlen;
02688         len -= ip_hlen;
02689         
02690         if (len < sizeof (struct tcp_s)) goto function_end;
02691 
02692         //The TCP packet is valid
02693         tcp = (struct tcp_s*) data;
02694         if(display && display_tcp_infos && display_flags) flags=compute_flags(tcp->th_flag);    
02695         
02696         if(anonymise && anonymise_checksum) tcp->th_chksum=(u_int16_t)0;
02697         
02698         //Erasing tcp timestamp
02699         if(anonymise && anonymise_timestamp)
02700         {
02701                 if (tcp->th_hlen_reserved > 80)
02702                 {
02703                         int offset=0;
02704                         if (tcp-> th_hlen_reserved == 128)
02705                         {
02706                                 if ((data[20] == 0x01) && (data[21] == 0x01) && (data[22] == 0x08) && (data[23] == 0x0a)) 
02707                                         offset=24;
02708                         }
02709                         else if (tcp->th_hlen_reserved == 160)
02710                         {
02711                                 if ((data[26] == 0x08) && (data[27] == 0x0a)) 
02712                                         offset=28;
02713                         }
02714                         if(offset)
02715                         {
02716                                         //print("TIME ");
02717                                         //ts_print((struct timeval*)data+offset);
02718                                         //print("\n");
02719                                         //anon_timestamp((struct timeval*)data+offset);
02720                                         //print("TIME2 ");
02721                                         //ts_print((struct timeval*)data+offset);
02722                                         //print("\n\n");
02723                                         memset((void*)data+offset,0,8);
02724                         }
02725                 }
02726         }
02727 
02728         /* Anonymising IP src & dest */
02729         strcpy (real_ip_src,inet_ntoa(ip->ip_src));
02730         strcpy (real_ip_dst,inet_ntoa(ip->ip_dst));
02731         
02732         if (anonymise && anonymise_ip)
02733         {
02734                 anon_ip(&(ip->ip_src));
02735                 anon_ip(&(ip->ip_dst));
02736         }
02737 
02738         strcpy (ip_src,inet_ntoa(ip->ip_src));
02739         strcpy (ip_dst,inet_ntoa(ip->ip_dst));
02740 
02741         /* Anonymising source and destination ports */
02742         realdport = ntohs (tcp->th_dport);      // destination port number 
02743         realsport = ntohs (tcp->th_sport);      // source port number
02744         if (anonymise && anonymise_port)
02745         {
02746                 anon_port(&(tcp->th_sport));
02747                 anon_port(&(tcp->th_dport));
02748         }
02749         dport = ntohs (tcp->th_dport);  // destination port number 
02750         sport = ntohs (tcp->th_sport);  // source port number
02751 
02752         nbpackets++;
02753         data += tcp->th_hlen_reserved/4;
02754         len -= tcp->th_hlen_reserved/4;
02755         
02756         tcp_len=header->len-sizeof(struct ether_header)-ip_hlen-tcp->th_hlen_reserved/4;
02757 
02758         if(anonymise && anonymise_sequence) anon_sequence(&(tcp->th_seq));
02759                 
02760         seq=htonl(tcp->th_seq);
02761         sprintf(seq_nb, "%u", seq);
02762 
02763         if(use_sequence_numbers) sprintf(next_seq_nb,"%u",seq+tcp_len);
02764 
02765         if (/*(already_anonymised ||realsport == 80 || realdport ==80)
02766                 &&*/ len >sizeof(struct tcp_s) && (strncmp(data,"HTTP",4) == 0 || strncmp(data,"GET ",4) == 0))
02767         {
02768                 //We look for the end of the headers (CRLF CRLF)
02769                         
02770                 eoh= strstr_limited ((char*)data, eop, "\r\n\r\n");     //End of headers
02771                 if (eoh) 
02772                 {
02773                         //We only keep a final "\r\n"
02774                         eoh+=2;
02775                         len=eoh-(char*)data;
02776                 }
02777                 else eoh = eop;
02778 
02779                 if(anonymise && anonymise_content) memset_range(eoh, eop,0);
02780                 if(anonymise && anonymise_http) anon_http((char*)data,len);
02781                 
02782                 if(display) display_packet_infos(nbpackets, (struct timeval*)&(header->ts), ip_src, ip_dst, sport, dport);
02783                 
02784                 nbvalidpackets++;
02785 
02786                 buffer = xmalloc (len + 1);
02787                 memcpy (buffer, data, len);
02788                 buffer[len] = '\0';
02789 
02790                 if (*data=='G') //This is a request
02791                 {
02792                         if(display)
02793                         {
02794                                 print ("REQ ");
02795                                 display_additional_info();
02796                         }
02797                         nb_request++;
02798                         
02799                         d=buffer_request(buffer,len,nbpackets,ip_src,ip_dst,sport,dport);
02800                         memcpy(&(d->ts_request), (struct timeval*)&(header->ts), sizeof(struct timeval));
02801                 }
02802                 else // This is a response
02803                 {
02804                         if(display)
02805                         {
02806                                 print ("ANS ");
02807                                 display_additional_info();
02808                         }
02809                         nb_response++;
02810 
02811                         d = find_request (ip_src, ip_dst, sport, dport);
02812                         if (d) //We have a buffered request corresponing to this response
02813                         {
02814                                 int lat;
02815                                 struct timeval time_diff;
02816                                 memcpy(&(d->ts_response), (struct timeval*)&(header->ts), sizeof(struct timeval));
02817                                 time_diff.tv_sec=d->ts_response.tv_sec-d->ts_request.tv_sec;
02818                                 
02819                                 if(d->ts_request.tv_usec <= d->ts_response.tv_usec) time_diff.tv_usec=d->ts_response.tv_usec - d->ts_request.tv_usec;
02820                                 else
02821                                 {
02822                                         time_diff.tv_usec=d->ts_response.tv_usec - d->ts_request.tv_usec + 1000000;
02823                                         time_diff.tv_sec--;
02824                                 }
02825                                 
02826                                 lat=time_diff.tv_sec*1000000.0+time_diff.tv_usec;
02827                                 stats_add_latency(lat);
02828                                 
02829                                 if(display)
02830                                 {
02831                                         print("lat=");
02832                                         ts_print_diff(&time_diff);
02833                                         print("[%i]",d->req_nb);
02834                                 }
02835                         }
02836                         else //We have found no corresponding request to this reply
02837                         {
02838                                 d=buffer_request(NULL,0,0,real_ip_src,real_ip_dst,realsport,realdport);
02839                                 memcpy(&(d->ts_response), (struct timeval*)&(header->ts), sizeof(struct timeval));
02840                                 if(display) print ("[??]");
02841                         }
02842                         d->resp_nb=nbpackets;
02843                         d->response = buffer;
02844                         d->response_len=len;
02845                                 
02846                         if(display && display_http) print ("\n-----------------\n");    
02847                         process_dialog(d);
02848                         free_dialog(d);
02849                         
02850                         if(display && display_http) print ("-----------------");
02851                 }
02852                 if(display) print("\n");
02853         } //end if is_valid
02854         else 
02855         {
02856                 if(truncate_packet) eoh=(char*)data;
02857                 if(anonymise && anonymise_content) memset_range((char*)data, eop,'\0');
02858 
02859                 if(tcp_len>0)
02860                 {
02861                         if (use_sequence_numbers)
02862                         {
02863                                 if((item=find_item(sequence_number_table, seq_nb)))
02864                                 {
02865                                         //If were expecting this sequence number
02866                                         //This is a continuation packet, containing HTTP data
02867                                         goto exp;
02868                                 }
02869                         }
02870                         else
02871                         {
02872 exp:
02873                                 nbvalidpackets++;
02874                                 expected=1;     
02875                         }
02876                 }
02877                 if(!display_http_only && display)
02878                 {
02879                         display_packet_infos(nbpackets, (struct timeval*)&(header->ts), ip_src, ip_dst, sport, dport);
02880                         if(expected) 
02881                         {
02882                                 //if(item && item->data) print("CNT %s ",(char*)item->data);
02883                                 //else 
02884                                 print("CNT ");
02885                         }
02886                         else print("    ");
02887                         display_additional_info();
02888                         print("\n");
02889                 }
02890         }
02891         if(use_sequence_numbers)
02892         {
02893                 char tmp[12];
02894                 char *tmp2;
02895                 sprintf(tmp, "%i", nbpackets);
02896                 tmp2=strdup(tmp);
02897 
02898                 if(expected)
02899                 {
02900                         free(item->s);
02901                         item->s=strdup(next_seq_nb);
02902                         if(item->data)
02903                         {
02904                                 free(item->data);
02905                                 item->data=tmp2;
02906                         }
02907                 }
02908                 else stats_add_sequence_number_with_data(next_seq_nb, tmp2);
02909         }
02910         
02911         if(nbpackets>=nbpacketstocapture && nbpacketstocapture>0) pcap_breakloop(adhandle);
02912         
02913 function_end:   
02914         //We dump the (anonymised) packet to the output file, if needed
02915         if (ofile) 
02916         {
02917                 if(maxfilesize==0 || output_file_written<maxfilesize)
02918                 {
02919                         if(truncate_packet)
02920                         {
02921                                 int diff;
02922                                 //if(eoh==NULL) eoh = eop;
02923                                 if(eoh==NULL) eoh = (char *)data;
02924                                 diff=eoh-(char*)packet;
02925                                 if(header->caplen!=diff) 
02926                                 {
02927                                         //new_header=xmalloc(sizeof(struct pcap_pkthdr));
02928                                         //memcpy(new_header, header, sizeof(struct pcap_pkthdr));
02929                                         //new_header->caplen=diff+1;
02930                                         ((struct pcap_pkthdr*)header)->caplen=diff;
02931                                         //*((char*)packet+diff)='\0';
02932                                         //printf("diff:%i\n",diff);
02933                                         //header=new_header;
02934                                         //printf("aaa\n");
02935                                         //printf("%i %i\n",header->caplen,eoh-(char*)packet);
02936                                 }
02937                         }
02938                         pcap_dump ((u_char *) dumphandle, header, packet);
02939                         output_file_written+=header->caplen+16;
02940                 }
02941         }
02942         if(flags) free(flags);  
02943 }

Here is the call graph for this function:

int parse_size char *  string  ) 
 

: Parses a size in the form: "10m" or "1g", and returns the number of bytes

Parameters:
char *string: the string to parse
Returns:
The corresponding number of bytes

Definition at line 1936 of file anon.c.

Referenced by main().

01937 {
01938         char *s;
01939         int len;
01940         int mult=1;
01941         if(string==NULL) return 0;
01942         s=strdup(string);
01943         len=strlen(s);
01944         if(*(s+len-1)=='g' || *(s+len-1)=='G') {mult=1000000000;*(s+len-1)='\0';}
01945         else if(*(s+len-1)=='m' || *(s+len-1)=='M') {mult=1000000;*(s+len-1)='\0';}
01946         else if(*(s+len-1)=='k' || *(s+len-1)=='K') {mult=1000;*(s+len-1)='\0';}
01947         return mult*atoi(s);
01948 }

void process_dialog struct dialog d  ) 
 

Scans a HTTP dialog (request+response) and displays it if needed.

Parameters:
struct dialog *d: a dialog structure containing the request, the response, and some additional information

Definition at line 1909 of file anon.c.

References display, display_http, do_stats, print, dialog::request, dialog::response, and scan_http().

Referenced by main(), and packet_handler().

01910 {
01911         if(do_stats) scan_http(d);
01912 
01913         if(display && display_http) 
01914         {
01915                 if(d->request) print ("%s\n", d->request);
01916                 print ("-----------------\n");
01917                 if(d->response) print ("%s\n", d->response);
01918         }
01919 }

Here is the call graph for this function:

char* process_http char *  data,
int  len,
int  scan,
int  anon
 

This a wrapper to call either scan_http() or anon_http().

! This is NOT an HTTP response code! This is a document beginning with "HTTP/1.1"

Definition at line 981 of file anon.c.

References anon_field(), anonymise, anonymise_http, convert_to_small(), display_raw_stats, fields_to_anonymise, find_end(), get_extension, get_suffix, is_in(), stats_add_content_length_nb(), stats_add_content_type(), stats_add_content_type_simple(), stats_add_extension(), stats_add_header(), stats_add_http_response_code_with_data(), stats_add_http_version(), stats_add_server(), stats_add_server_simple(), stats_add_suffix(), stats_add_user_agent(), string_append(), strptime(), and strstr_limited().

Referenced by anon_http(), and scan_http().

00982 {
00983         char *ptr=data;
00984         char *tmp;
00985         char *last_modified=NULL;
00986         char *date=NULL;
00987         char *end=data+len;
00988         char *nextpos=NULL;
00989         char backup_char;
00990         
00991         if(data==NULL) return NULL;
00992         while(1)
00993         {
00994                 tmp=strstr_limited(ptr, end, "\r");
00995                 if(!tmp)
00996                 {
00997                         tmp=strstr_limited(ptr, end, "\n");
00998                 }
00999                 else
01000                 {
01001                         char *tmp2=strstr_limited(ptr, tmp, "\n");
01002                         if(tmp2 && tmp2<tmp) tmp=tmp2;
01003                 }
01004                 if(!tmp) 
01005                 {
01006                         tmp=end;
01007                         nextpos=NULL;
01008                         
01009                         //We skip the headers that have been truncated due to too small snaplen
01010                         if(scan) break; 
01011                 }
01012                 else
01013                 {
01014                         if(*tmp=='\r' && *(tmp+1)=='\n')
01015                         {
01016                                 nextpos=tmp+2;
01017                         }
01018                         else if(*tmp=='\r' || *tmp=='\n')
01019                         {
01020                                 nextpos=tmp+1;
01021                         }
01022         
01023                 }
01024                 backup_char=*tmp;
01025                 *tmp='\0';
01026                 
01027                 if(anon && anonymise && anonymise_http)
01028                 {
01029                         char *field=is_in(fields_to_anonymise, ptr);
01030                         if(field)
01031                         {
01032                                 anon_field(field, ptr);
01033                         }
01034                         else
01035                         {
01036                                 //Handle special Hotmail servers headers, beginning with "<"
01037                                 //Special thanks to Microsoft...
01038                                 if(ptr && *ptr=='<')
01039                                 {
01040                                         anon_field("",ptr);
01041                                 }
01042                         }
01043                 }
01044                 if(scan)
01045                 {
01046                         //Uncomment to get headers stats
01047                         if(display_raw_stats)
01048                         {
01049                                 char *tmp=strstr(ptr, ":");
01050                                 char bak;
01051                                 char *dup;
01052                                 if(!tmp) tmp=strstr(ptr," ");
01053                                 else
01054                                 {
01055                                         char *tmp2=strstr(ptr, " ");
01056                                         if(tmp2 && tmp2<tmp) tmp=tmp2;
01057                                 }
01058                                 if(tmp)
01059                                 {
01060                                         bak=*tmp;
01061                                         *tmp='\0';
01062                                 }
01063                                 
01064                                 dup=strdup(ptr);
01065                                 //convert_to_small(dup);
01066                                 stats_add_header(dup);
01067                                 free(dup);
01068                                 if(tmp) *tmp=bak;
01069                         }
01070                         
01071                         
01072                         if(ptr+5<end && strncmp(ptr, "HTTP/",5)==0)
01073                         {
01074                                 //We only keep the response code
01075                                 char bak;
01076                                 char *tmp=NULL;
01077                                 int tmp_i;
01078                                 bak=ptr[12];
01079                                 if(ptr[12]==' ')
01080                                 {
01081                                         tmp=strdup(ptr+13);
01082                                 }
01083                                 else
01084                                 {
01085                                         //Some webservers are broken:
01086                                         //Normally, they should answer:
01087                                         //response-code [SPACE] reason-code
01088                                         //Unfortunately, that it not always the case
01089                                         tmp=strdup("");
01090                                 }
01091                                 ptr[12]='\0';
01092                                 
01093                                 if(sscanf(ptr+9,"%i ",&tmp_i)==1)
01094                                 {       
01095                                         stats_add_http_response_code_with_data(ptr+9, tmp);
01096                                 
01097                                         ptr[12]=bak;
01098                                         bak=ptr[9];
01099                                         ptr[9]='\0';
01100                                         stats_add_http_version(ptr);
01101                                         ptr[9]=bak;
01102                                 }
01103                                 else
01104                                 {
01106                                         free(tmp);                              
01107                                         ptr[12]=bak;
01108                                         nextpos=NULL;
01109                                 }
01110                         }
01111                         else if(ptr+4<end && strncmp(ptr, "GET ",4)==0)
01112                         {
01113                                 char *tmp;
01114                                 char *page=strdup(ptr+4);
01115                                 *(page+strlen(page)-8)='\0';
01116                                 tmp=get_extension(page);
01117                                 if(tmp) 
01118                                 {
01119                                         char *tmp2=find_end(tmp);
01120                                         //for URLs like:
01121                                         // ._zahi,7,2,15,108,verdenab,8,204,102,0_zahelp
01122                                         // ._zahello,7,3,18,328,verdenab,10,204,102,0_zaplease
01123                                         if(tmp2)
01124                                         {
01125                                                 *tmp2='\0';
01126                                         }
01127                                         convert_to_small(tmp);
01128                                         stats_add_extension(tmp);
01129                                         free(tmp);
01130                                 }
01131                                 free(page);
01132                         }
01133                         else if(strncmp(ptr, "Date: ",6)==0)
01134                         {
01135                                 date=strdup(ptr+6);
01136                         }
01137                         else if(strncmp(ptr, "Server: ",8)==0)
01138                         {
01139                                 char *dup=strdup(ptr+8);
01140                                 char *tmp;
01141                                 if(strncmp(dup, ".V", 2)==0) //Like .V15 Apache/xxx
01142                                 {
01143                                         char *space=index(ptr+8, ' ');
01144                                         if(space) 
01145                                         {
01146                                                 free(dup);
01147                                                 dup=strdup(space+1);
01148                                         }
01149                                 }
01150                                 tmp=index(dup,'/');
01151                                 if(tmp) *tmp='\0';
01152                                 tmp=index(dup,' ');
01153                                 if(tmp) *tmp='\0';
01154                                 tmp=strstr(dup,"Apache");
01155                                 if(tmp) *(tmp+6)='\0';
01156                                 tmp=index(dup,'-');
01157                                 if(tmp) *tmp='\0';
01158                                 stats_add_server(ptr+8);
01159 /*                              if(strncmp(ptr+8, ".V", 2)==0) //Like .V15 Apache/xxx
01160                                 {
01161                                         char *space=index(ptr+8, ' ');
01162                                         if(space) stats_add_server_simple(space+1);
01163                                         else stats_add_server_simple(dup);
01164                                 }
01165                                 else 
01166                                 {*/
01167                                         stats_add_server_simple(dup);
01168                                 //}
01169                                 free(dup);
01170                         }
01171                         else if(strncmp(ptr, "Content-Type: ",14)==0)
01172                         {
01173                                 char *dup=strdup(ptr+14);
01174                                 char *tmp=strstr(dup,";");
01175                                 char *dup2=NULL;
01176                                 if(tmp) *tmp='\0';
01177                                 convert_to_small(dup);
01178                                 dup2=strdup(dup);
01179                                 if((tmp=strstr(dup2,"/"))) *tmp='\0';
01180                                 stats_add_content_type(dup);
01181                                 stats_add_content_type_simple(dup2);
01182                                 free(dup);
01183                                 free(dup2);
01184                         }
01185                         else if(strncmp(ptr, "Host: ",6)==0)
01186                         {
01187                                 char *tmp;
01188                                 tmp=get_suffix(ptr+6);
01189                                 if(tmp) 
01190                                 {
01191                                         convert_to_small(tmp);
01192                                         stats_add_suffix(tmp);
01193                                         free(tmp);
01194                                 }
01195                         }
01196                         else if(strncmp(ptr, "Last-Modified: ",15)==0)
01197                         {
01198                                 last_modified=strdup(ptr+15);
01199                         }
01200                         else if(strncmp(ptr, "Content-Length: ",16)==0)
01201                         {
01202                                 stats_add_content_length_nb(atoi(ptr+16));
01203                         }
01204                         else if(strncmp(ptr, "User-Agent: ",12)==0)
01205                         {
01206                                 stats_add_user_agent(ptr+12);
01207                         }
01208                 }
01209                 
01210                 if(tmp) *tmp=backup_char;
01211         
01212                 if(nextpos==NULL) break;
01213                 ptr=nextpos;
01214         }       
01215         
01216         if(scan)
01217         {
01218                 if(last_modified && date) 
01219                 {
01220                         struct tm time_date;
01221                         struct tm time_lm;
01222                         char *ret_date;
01223                         char *ret_lm;
01224                         
01225                         ret_date=strptime(date, "%a, %d %b %Y %T GMT", &time_date);
01226                         ret_lm=strptime(last_modified, "%a, %d %b %Y %T GMT", &time_lm);
01227 
01228                         if(ret_date==date+strlen(date) && ret_lm==last_modified+strlen(last_modified))
01229                         {
01230                                 //The two dates have been correctly parsed
01231                                 time_t seconds_date=mktime(&time_date);
01232                                 time_t seconds_lm=mktime(&time_lm);
01233                                 double diff=difftime(seconds_date,seconds_lm);
01234                                 free(last_modified);
01235                                 free(date);
01236                                 last_modified=NULL;
01237                                 date=NULL;
01238                                 if(diff>=0) return string_append(NULL, "%i", (int)diff);
01239                         }
01240                 }
01241                 /*else 
01242                 {
01243                         if(date) printf("NOLAST LAST\n");
01244                                 else printf("NOLAST DATE\n");
01245                 }
01246                 */
01247         }
01248         if(date) free(date);
01249         if(last_modified) free(last_modified);
01250         return NULL;
01251 }

Here is the call graph for this function:

void scan_http struct dialog d  ) 
 

Scans two HTTP headers in memory (request and response) and gathers statistics.

Parameters:
struct dialog *d: a dialog structure containing the request, the response, and some additional information

Definition at line 1268 of file anon.c.

References process_http(), dialog::request, dialog::request_len, dialog::response, dialog::response_len, and stats_add_freshness().

Referenced by process_dialog().

01269 {
01270         char *freshness=NULL;
01271         //char *tmp=NULL;
01272         
01273         /*tmp=*/process_http(d->request, d->request_len, 1, 0);
01274         freshness=process_http(d->response, d->response_len, 1, 0);
01275 
01276         if(freshness)
01277         {
01278                 stats_add_freshness(atoi(freshness));
01279                 free(freshness);
01280         }
01281         //if(tmp) free(tmp);
01282 }

Here is the call graph for this function:

void stats_add_content_length char *  s  ) 
 

void stats_add_content_type char *  s  ) 
 

Definition at line 1888 of file anon.c.

References add_and_count_string.

Referenced by process_http().

void stats_add_content_type_simple char *  s  ) 
 

Definition at line 1892 of file anon.c.

References add_and_count_string.

Referenced by process_http().

void stats_add_extension char *  s  ) 
 

Definition at line 1862 of file anon.c.

References add_and_count_string.

Referenced by process_http().

void stats_add_freshness int  nb  ) 
 

Definition at line 1900 of file anon.c.

References add_nb().

Referenced by scan_http().

01901 {
01902         add_nb(&fresh_table, nb);
01903 }

Here is the call graph for this function:

void stats_add_header char *  s  ) 
 

Definition at line 1858 of file anon.c.

Referenced by process_http().

01859 {
01860 //      header_table=add_and_count_string(header_table, s);
01861 }

void stats_add_http_response_code_with_data char *  s,
char *  data
 

Definition at line 1883 of file anon.c.

References add_and_count_string_with_data().

Referenced by process_http().

Here is the call graph for this function:

void stats_add_http_version char *  s  ) 
 

Definition at line 1896 of file anon.c.

References add_and_count_string.

Referenced by process_http().

void stats_add_last_modified char *  s  ) 
 

void stats_add_new_url_with_data char *  s,
void *  data
 

void stats_add_sequence_number char *  s  ) 
 

Definition at line 1850 of file anon.c.

References add_and_count_string.

void stats_add_server char *  s  ) 
 

Definition at line 1870 of file anon.c.

References add_and_count_string.

Referenced by process_http().

void stats_add_server_simple char *  s  ) 
 

Definition at line 1874 of file anon.c.

References add_and_count_string.

Referenced by process_http().

void stats_add_suffix char *  s  ) 
 

Definition at line 1866 of file anon.c.

References add_and_count_string.

Referenced by process_http().

01867 {
01868         suffix_table=add_and_count_string(suffix_table, s);
01869 }

void stats_add_url char *  s  ) 
 

void stats_add_url_with_data char *  s,
void *  data
 

void stats_add_user_agent char *  s  ) 
 

Definition at line 1878 of file anon.c.

References add_and_count_string.

Referenced by process_http().

char* string_append char *  s,
const char *  fmt,
  ...
 

Appends a string to another.

Definition at line 228 of file anon.c.

References get_string_from_args, xmalloc(), and xrealloc().

Referenced by add_and_count_string_with_data(), display_deviation(), display_string_table_with_data(), get_pct(), get_size(), get_speed(), get_time(), latency_stats(), process_http(), and summarize_stats().

00229 {
00230         char *tmp;
00231         get_string_from_args(tmp);
00232         if(!tmp) return NULL;
00233         if(s==NULL)
00234         {
00235                 s=xmalloc( strlen(tmp)+1);
00236                 strcpy(s, tmp);
00237         }
00238         else
00239         {
00240                 s=xrealloc(s, strlen(s)+strlen(tmp)+1);
00241                 strcat(s, tmp);
00242         }
00243         free(tmp);
00244         if(s==NULL) return NULL;
00245         return s;
00246 }

Here is the call graph for this function:

char * strptime const char *  s,
const char *  format,
struct tm *  tm
 

Libc function, which converts a character string to values which are stored in a tm structure, using a specified format.

Parameters:
const char *s : The string to convert
const char *format : The format of s
struct tm *tm : The tm structure to store the values
Returns:
a pointer to the first character not processed in this function call

Referenced by anon_date(), and process_http().

char* strstr_limited char *  ptr,
char *  end,
char *  str
 

Wrapper for strstr, which limits the memory area to search.

Parameters:
char *ptr: start of the memory area
char *end: end of the memory area
char *str: the string to find
Returns:
If found, a char * pointing to the searched string. NULL otherwise.

Definition at line 963 of file anon.c.

Referenced by packet_handler(), and process_http().

00964 {
00965         char *first;
00966         if(!str) return NULL;
00967         while(1)
00968         {
00969                 first=memchr(ptr, *str, end-ptr);
00970                 if(first)
00971                 {
00972                         int len=strlen(str);
00973                         if(first+len<=end && strncmp(str, first, len)==0) return first;
00974                 }
00975                 else return NULL;
00976                 ptr=first+1;
00977                 if(ptr>end) return NULL;
00978         }
00979 }

void ts_print struct timeval *  tvp_  ) 
 

Prints a timestamp following this format: 16:21:48.970264.

Parameters:
struct timeval *tvp_: A pointer on a timeval

Definition at line 447 of file anon.c.

References anonymise, anonymise_timestamp, print, and timeoffset.

Referenced by display_packet_infos().

00448 {
00449         register int s;
00450         struct timeval tv;
00451         struct timeval *tvp=&tv;
00452         
00453         tv.tv_sec=tvp_->tv_sec;
00454         tv.tv_usec=tvp_->tv_usec;
00455         
00456         if(!anonymise && anonymise_timestamp) s = (tvp->tv_sec+timeoffset) % 86400;
00457         else s = (tvp->tv_sec) % 86400;
00458         (void)print("%02d:%02d:%02d.%06u ",
00459                          s / 3600, (s % 3600) / 60, s % 60,
00460                          (unsigned)tvp->tv_usec);
00461 }

void ts_print_diff struct timeval *  tvp  ) 
 

Prints a time difference following this format: 14.95ms.

Parameters:
struct timeval *tvp_: A pointer on a timeval

Definition at line 438 of file anon.c.

References print.

Referenced by packet_handler().

00439 {
00440         //Print the latency in miliseconds
00441         float time=tvp->tv_sec*1000.0+tvp->tv_usec/1000.0;
00442         print("%.02fms ",time);
00443 }

void* xmalloc size_t  size  ) 
 

A wrapper to malloc() which quits the program if the memory allocation fails.

Definition at line 804 of file anon.c.

References print.

Referenced by add_and_count_string_with_data(), buffer_request(), clone_nb_table(), display_string_table_with_data(), packet_handler(), and string_append().

00805 {
00806         void *tmp=malloc(size);
00807         if(!tmp)
00808         {
00809                 print("Memory allocation error!\n");
00810                 exit(EXIT_FAILURE);
00811         }
00812         return tmp;
00813 }

void* xrealloc void *  ptr,
size_t  size
 

A wrapper to realloc() which quits the program if the memory allocation fails.

Definition at line 815 of file anon.c.

References print.

Referenced by add_anonymised_url(), add_flag(), add_nb(), anon_ip(), anon_port(), and string_append().

00816 {
00817         void *tmp=realloc(ptr,size);
00818         if(!tmp)
00819         {
00820                 print("Memory allocation error!\n");
00821                 exit(1);
00822         }
00823         return tmp;
00824 }


Generated on Sat Sep 10 01:21:18 2005 for Anon by  doxygen 1.4.3-20050530