-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisited.c
2972 lines (2731 loc) · 82.5 KB
/
visited.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* visited -- fast winroute proxy logs analyzer.
*
* Copyright (C) 2011-2012 Camilo E. Hidalgo Estevez <[email protected]>
* Based on Visitors by Salvatore Sanfilippo <[email protected]>
* for more information visit http://www.hping.org/visitors
* All Rights Reserved.
*
* This software is released under the terms of the BSD license.
* Read the COPYING file in this distribution for more details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <errno.h>
#include <locale.h>
#include <ctype.h>
#include "aht.h"
#include "antigetopt.h"
#include "sleep.h"
#include "blacklist.h"
/* Max length of an error stored in the visitors handle */
#define VI_ERROR_MAX 1024
/* Max length of a log line */
#define VI_LINE_MAX 4096
/* Max number of filenames in the command line */
#define VI_FILENAMES_MAX 1024
/* Max number of prefixes in the command line */
#define VI_PREFIXES_MAX 1024
/* Max number of --grep --exclude patterns in the command line */
#define VI_GREP_PATTERNS_MAX 1024
/* Abbreviation length for HTML outputs */
#define VI_HTML_ABBR_LEN 100
/* Max length of a log entry date */
#define VI_DATE_MAX 64
/* Version as a string */
#define VI_VERSION_STR "0.25"
/*------------------------------- data structures ----------------------------*/
/* visited handle */
struct vih {
int startt;
int endt;
int processed;
int invalid;
int blacklisted;
int hour_hits[24];
int hour_size[24];
int weekday_hits[7];
int weekday_size[7];
int weekdayhour_hits[7][24]; /* hour and weekday combined data */
int weekdayhour_size[7][24]; /* hour and weekday combined data */
int monthday_hits[12][31]; /* month and day combined data */
int monthday_size[12][31]; /* month and day combined data */
struct hashtable pages_hits;
struct hashtable pages_size;
struct hashtable sites_hits;
struct hashtable sites_size;
struct hashtable users_hits;
struct hashtable users_size;
struct hashtable hosts_hits;
struct hashtable hosts_size;
struct hashtable codes_hits;
struct hashtable codes_size;
struct hashtable verbs_hits;
struct hashtable verbs_size;
struct hashtable types_hits;
struct hashtable types_size;
struct hashtable month_hits;
struct hashtable month_size;
struct hashtable error404;
struct hashtable date;
char *error;
};
/* info associated with a line of log */
struct logline {
char *host;
char *user;
char *date;
char *hour;
char *timezone;
char *req;
char *code;
char *verb;
long size;
time_t time;
struct tm tm;
};
/* output module structure. See below for the definition of
* the text and html output modules. */
struct outputmodule {
void (*print_header)(FILE *fp);
void (*print_footer)(FILE *fp);
void (*print_title)(FILE *fp, char *title);
void (*print_subtitle)(FILE *fp, char *title);
void (*print_numkey_info)(FILE *fp, char *key, int val);
void (*print_keykey_entry)(FILE *fp, char *key1, char *key2, int num);
void (*print_numkey_entry)(FILE *fp, char *key, int val, char *link,
int num);
void (*print_numkeybar_entry)(FILE *fp, char *key, int max, int tot,
int this);
void (*print_numkeycomparativebar_entry)(FILE *fp, char *key, int tot,
int this);
void (*print_bidimentional_map)(FILE *fp, int xlen, int ylen,
char **xlabel, char **ylabel, int *value);
void (*print_hline)(FILE *fp);
void (*print_credits)(FILE *fp);
void (*print_report_link)(FILE *fp, char *report);
};
/* Just a string with cached length */
struct vistring {
char *str;
int len;
};
/* Grep pattern for --grep --exclude */
#define VI_PATTERNTYPE_GREP 0
#define VI_PATTERNTYPE_EXCLUDE 1
struct greppat {
int type;
char *pattern;
};
/* ---------------------- global configuration parameters ------------------- */
int Config_debug = 0;
int Config_max_requests = 50;
int Config_max_pages = 50;
int Config_max_images = 50;
int Config_max_error404 = 50;
int Config_max_codes = 50;
int Config_max_sites = 50;
int Config_max_types = 50;
int Config_max_hosts = 50;
int Config_process_codes = 0;
int Config_process_weekdayhour_map = 0;
int Config_process_monthday_map = 0;
int Config_process_users = 0;
int Config_process_verbs = 0;
int Config_process_sites = 0;
int Config_process_types = 0;
int Config_process_hosts = 0;
int Config_process_error404 = 0;
int Config_process_monthly_hits = 1;
int Config_tail_mode = 0;
int Config_stream_mode = 0;
int Config_update_every = 60*10; /* update every 10 minutes for default. */
int Config_reset_every = 0; /* never reset for default */
int Config_time_delta = 0; /* adjustable time difference */
int Config_filter_spam = 0;
int Config_ignore_404 = 0;
char *Config_output_file = NULL; /* stdout if not set. */
struct outputmodule *Output = NULL; /* intialized to 'text' in main() */
/* Prefixes */
int Config_prefix_num = 0; /* number of set prefixes */
struct vistring Config_prefix[VI_PREFIXES_MAX];
/* Grep/Exclude array */
struct greppat Config_grep_pattern[VI_GREP_PATTERNS_MAX];
int Config_grep_pattern_num = 0; /* number of set patterns */
/*----------------------------------- Tables ---------------------------------*/
static char *vi_wdname[7] = {"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"};
#if 0
static int vi_monthdays[12] = {31, 29, 31, 30, 31, 30 , 31, 31, 30, 31, 30, 31};
#endif
/* -------------------------------- prototypes ------------------------------ */
void vi_clear_error(struct vih *vih);
void vi_tail(int filec, char **filev);
/*------------------- Options parsing help functions ------------------------ */
void ConfigAddGrepPattern(char *pattern, int type) {
char *s;
int len = strlen(pattern);
if (Config_grep_pattern_num == VI_GREP_PATTERNS_MAX) {
fprintf(stderr, "Too many grep/exclude options specified\n");
exit(1);
}
s = malloc(strlen(pattern)+3);
s[0] = '*';
memcpy(s+1, pattern, len);
s[len+1] = '*';
s[len+2] = '\0';
Config_grep_pattern[Config_grep_pattern_num].type = type;
Config_grep_pattern[Config_grep_pattern_num].pattern = s;
Config_grep_pattern_num++;
}
/*------------------------------ support functions -------------------------- */
/* Returns non-zero if the link seems like a google link, zero otherwise.
* Note that this function only checks for a prefix of www.google.<something>.
* so may be fooled. */
int vi_is_google_link(char *s) {
return !strncmp(s, "http://www.google.", 18);
}
/* Returns non-zero if the url matches some user-specified prefix.
* being a link "internal" to the site. Otherwise zero is returned.
*
* When there is a match, the value returned is the length of
* the matching prefix. */
int vi_is_internal_link(char *url) {
int i, l;
if (!Config_prefix_num) return 0; /* no prefixes set? */
l = strlen(url);
for (i = 0; i < Config_prefix_num; i++) {
if (Config_prefix[i].len <= l &&
!strncasecmp(url, Config_prefix[i].str,
Config_prefix[i].len)) {
return Config_prefix[i].len;
}
}
return 0;
}
/* returns non-zero if the URL 's' seems a real page. */
int vi_is_pageview(char *s) {
int l = strlen(s);
char *end = s + l; /* point to the nul term */
char *dot, *slash;
if (s[l-1] == '/') return 1;
if (l >= 6 &&
(!memcmp(end-5, ".html", 5) ||
!memcmp(end-4, ".htm", 4) ||
!memcmp(end-4, ".php", 4) ||
!memcmp(end-4, ".asp", 4) ||
!memcmp(end-4, ".jsp", 4) ||
!memcmp(end-4, ".xdl", 4) ||
!memcmp(end-5, ".xhtml", 5) ||
!memcmp(end-4, ".xml", 4) ||
!memcmp(end-4, ".cgi", 4) ||
!memcmp(end-3, ".pl", 3) ||
!memcmp(end-6, ".shtml", 6) ||
!memcmp(end-5, ".HTML", 5) ||
!memcmp(end-4, ".HTM", 4) ||
!memcmp(end-4, ".PHP", 4) ||
!memcmp(end-4, ".ASP", 4) ||
!memcmp(end-4, ".JSP", 4) ||
!memcmp(end-4, ".XDL", 4) ||
!memcmp(end-6, ".XHTML", 6) ||
!memcmp(end-4, ".XML", 4) ||
!memcmp(end-4, ".CGI", 4) ||
!memcmp(end-3, ".PL", 3) ||
!memcmp(end-6, ".SHTML", 6))) return 1;
dot = strrchr(s, '.');
if (!dot) return 1;
slash = strrchr(s, '/');
if (slash && slash > dot) return 1;
return 0;
}
/* returns non-zero if 'ip' seems a string representing an IP address
* like "1.2.3.4". Note that 'ip' is always an IP or an hostname
* so this function actually test if the string pointed by 'ip' only
* contains characters in the "[0-9.]" set */
int vi_is_numeric_address(char *ip) {
unsigned int l = strlen(ip);
return strspn(ip, "0123456789.") == l;
}
/* returns the time converted into a time_t value.
* On error (time_t) -1 is returned.
* Note that this function is specific for the following format:
* "10/May/2004:04:15:33". Works if the month is not an abbreviation, or if the
* year is abbreviated to only the last two digits.
* The time can be omitted like in "10/May/2004". */
time_t parse_date(char *s, struct tm *tmptr) {
struct tm tm;
time_t t;
char *months[] = {
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec",
};
char *day, *month, *year, *time = NULL;
char monthaux[32];
int i, len;
/* make a copy to mess with it */
len = strlen(s);
if (len >= 32) goto fmterr;
memcpy(monthaux, s, len);
monthaux[len] = '\0';
/* Inizialize the tm structure. We just fill three fields */
tm.tm_sec = 0;
tm.tm_min = 0;
tm.tm_hour = 0;
tm.tm_mday = 0;
tm.tm_mon = 0;
tm.tm_year = 0;
tm.tm_wday = 0;
tm.tm_yday = 0;
tm.tm_isdst = -1;
/* search delimiters */
day = monthaux;
if ((month = strchr(day, '/')) == NULL) goto fmterr;
*month++ = '\0';
if ((year = strchr(month, '/')) == NULL) goto fmterr;
*year++ = '\0';
/* time, optional for this parser. */
if ((time = strchr(year, ':')) != NULL) {
*time++ = '\0';
}
/* convert day */
tm.tm_mday = atoi(day);
if (tm.tm_mday < 1 || tm.tm_mday > 31) goto fmterr;
/* convert month */
if (strlen(month) < 3) goto fmterr;
month[0] = tolower(month[0]);
month[1] = tolower(month[1]);
month[2] = tolower(month[2]);
for (i = 0; i < 12; i++) {
if (memcmp(month, months[i], 3) == 0) break;
}
if (i == 12) goto fmterr;
tm.tm_mon = i;
/* convert year */
tm.tm_year = atoi(year);
if (tm.tm_year > 100) {
if (tm.tm_year < 1900 || tm.tm_year > 2500) goto fmterr;
tm.tm_year -= 1900;
} else {
/* if the year is in two-digits form, the 0 - 68 range
* is converted to 2000 - 2068 */
if (tm.tm_year < 69)
tm.tm_year += 100;
}
/* convert time */
if (time) { /* format is HH:MM:SS */
if (strlen(time) < 8) goto fmterr;
tm.tm_hour = ((time[0]-'0')*10)+(time[1]-'0');
if (tm.tm_hour < 0 || tm.tm_hour > 23) goto fmterr;
tm.tm_min = ((time[3]-'0')*10)+(time[4]-'0');
if (tm.tm_min < 0 || tm.tm_min > 59) goto fmterr;
tm.tm_sec = ((time[6]-'0')*10)+(time[7]-'0');
if (tm.tm_sec < 0 || tm.tm_sec > 60) goto fmterr;
}
t = mktime(&tm);
if (t == (time_t)-1) goto fmterr;
t += (Config_time_delta*3600);
if (tmptr) {
struct tm *auxtm;
if ((auxtm = localtime(&t)) != NULL)
*tmptr = *auxtm;
}
return t;
fmterr: /* format error */
return (time_t) -1;
}
/* returns 1 if the given date is Saturday or Sunday.
* Zero is otherwise returned. */
int vi_is_weekend(char *s) {
struct tm tm;
if (parse_date(s, &tm) != (time_t)-1) {
if (tm.tm_wday == 0 || tm.tm_wday == 6)
return 1;
}
return 0;
}
#if 0
/* Returns true if 'year' is a leap year. */
int isleap(int year) {
int conda, condb, condc;
conda = (year%4) == 0;
condb = (year%100) == 0;
condc = (year%400) == 0;
return conda && !(condb && !condc);
}
#endif
/* URL decoding and white spaces trimming function.
* Input: the encoded string 's'.
* Output: the decoded string written at 'd' that has room for at least 'n'
* bytes of data. */
void vi_urldecode(char *d, char *s, int n) {
char *start = d;
if (n < 1) return;
while(*s && n > 1) {
int c = *s;
switch(c) {
case '+':
c = ' ';
break;
case '%':
if (*(s+1) && *(s+2)) {
int high = toupper(*(s+1));
int low = toupper(*(s+2));
if (high <= '9') high -= '0';
else high = (high - 'A') + 10;
if (low <= '9') low -= '0';
else low = (low - 'A') + 10;
c = (high << 4)+low;
s += 2;
}
break;
}
if (c != ' ' || d != start) {
*d++ = c;
n--;
}
s++;
}
/* Right trim */
*d = '\0';
d--;
while (d >= start && *d == ' ') {
*d = '\0';
d--;
}
}
/* URL encoding function
* Input: the unencoded string 's'.
* Output: the url-encoded string written at 'd' that has room for at least 'n'
* bytes of data. */
void vi_urlencode(char *d, char *s, int n) {
if (n < 1) return;
n--;
while(*s && n > 0) {
int c = *s;
if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9')) {
*d++ = c;
n--;
} else if (c == ' ') {
*d++ = '+';
n--;
} else if (c == '\n') {
if (n < 6) break;
memcpy(d, "%0d%0a", 6);
d += 6;
n -= 6;
} else {
unsigned int t;
char *hexset = "0123456789abcdef";
if (n < 3) break;
t = (unsigned) c;
*d++ = '%';
*d++ = hexset [(t & 0xF0) >> 4];
*d++ = hexset [(t & 0x0F)];
n -= 3;
}
s++;
}
*d = '\0';
}
/* Convert a nul-term string to lowercase in place */
void vi_strtolower(char *s) {
while (*s) {
*s = tolower(*s);
s++;
}
}
/* Note: the following function strlcat and strlcpy are (possibly) modified
* version of OpenBSD's functions. Original copyright notice:
* Copyright (c) 1998 Todd C. Miller <[email protected]>
* Originally under the BSD license. */
int vi_strlcpy(char *dst, char *src, int siz) {
char *d = dst;
const char *s = src;
int n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
int vi_strlcat(char *dst, const char *src, int siz) {
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
/* Returns non-zero if the url matches one of the keywords in
* blacklist.h, otherwise zero is returned. Warning!!! This function
* run time is proportional to the size of blacklist.h, so it is
* very slow. */
int vi_is_blacklisted_url(struct vih *vih, char *url) {
unsigned int i;
for (i = 0; i < VI_BLACKLIST_LEN; i++) {
if (strstr(url, vi_blacklist[i])) {
vih->blacklisted++;
return 1;
}
}
return 0;
}
/* Glob-style pattern matching. */
int vi_match_len(const char *pattern, int patternLen,
const char *string, int stringLen, int nocase) {
while(patternLen) {
switch(pattern[0]) {
case '*':
while (pattern[1] == '*') {
pattern++;
patternLen--;
}
if (patternLen == 1)
return 1; /* match */
while(stringLen) {
if (vi_match_len(pattern+1, patternLen-1,
string, stringLen, nocase))
return 1; /* match */
string++;
stringLen--;
}
return 0; /* no match */
break;
case '?':
if (stringLen == 0)
return 0; /* no match */
string++;
stringLen--;
break;
case '[': {
int not, match;
pattern++;
patternLen--;
not = pattern[0] == '^';
if (not) {
pattern++;
patternLen--;
}
match = 0;
while(1) {
if (pattern[0] == '\\') {
pattern++;
patternLen--;
if (pattern[0] == string[0])
match = 1;
} else if (pattern[0] == ']') {
break;
} else if (patternLen == 0) {
pattern--;
patternLen++;
break;
} else if (pattern[1] == '-' && patternLen >= 3) {
int start = pattern[0];
int end = pattern[2];
int c = string[0];
if (start > end) {
int t = start;
start = end;
end = t;
}
if (nocase) {
start = tolower(start);
end = tolower(end);
c = tolower(c);
}
pattern += 2;
patternLen -= 2;
if (c >= start && c <= end)
match = 1;
} else {
if (!nocase) {
if (pattern[0] == string[0])
match = 1;
} else {
if (tolower((int)pattern[0]) == tolower((int)string[0]))
match = 1;
}
}
pattern++;
patternLen--;
}
if (not)
match = !match;
if (!match)
return 0; /* no match */
string++;
stringLen--;
break;
}
case '\\':
if (patternLen >= 2) {
pattern++;
patternLen--;
}
/* fall through */
default:
if (!nocase) {
if (pattern[0] != string[0])
return 0; /* no match */
} else {
if (tolower((int)pattern[0]) != tolower((int)string[0]))
return 0; /* no match */
}
string++;
stringLen--;
break;
}
pattern++;
patternLen--;
if (stringLen == 0) {
while(*pattern == '*') {
pattern++;
patternLen--;
}
break;
}
}
if (patternLen == 0 && stringLen == 0)
return 1;
return 0;
}
/* Like vi_match_len but more handly if used against nul-term strings. */
int vi_match(const char *pattern, const char *string, int nocase) {
int patternLen = strlen(pattern);
int stringLen = strlen(string);
return vi_match_len(pattern, patternLen, string, stringLen, nocase);
}
/*-------------------------- visited handler functions --------------------- */
/* Init the hashtable with methods suitable for an "occurrences counter" */
void vi_ht_init(struct hashtable *ht) {
ht_init(ht);
ht_set_hash(ht, ht_hash_string);
ht_set_key_destructor(ht, ht_destructor_free);
ht_set_val_destructor(ht, ht_no_destructor);
ht_set_key_compare(ht, ht_compare_string);
}
/* Reset the weekday/hour info in the visited handler. */
void vi_reset_combined_maps(struct vih *vih) {
int i, j;
for (i = 0; i < 24; i++) {
vih->hour_hits[i] = 0;
vih->hour_size[i] = 0;
for (j = 0; j < 7; j++) {
vih->weekdayhour_hits[j][i] = 0;
vih->weekdayhour_size[j][i] = 0;
}
}
for (i = 0; i < 7; i++) vih->weekday_hits[i] = 0;
for (i = 0; i < 7; i++) vih->weekday_size[i] = 0;
for (i = 0; i < 31; i++)
for (j = 0; j < 12; j++) {
vih->monthday_hits[j][i] = 0;
vih->monthday_size[j][i] = 0;
}
}
/* Reset the hashtables from the handler, that are left
* in a reusable state (but all empty). */
void vi_reset_hashtables(struct vih *vih) {
ht_destroy(&vih->users_hits);
ht_destroy(&vih->users_size);
ht_destroy(&vih->hosts_hits);
ht_destroy(&vih->hosts_size);
ht_destroy(&vih->pages_hits);
ht_destroy(&vih->pages_size);
ht_destroy(&vih->sites_hits);
ht_destroy(&vih->sites_size);
ht_destroy(&vih->codes_hits);
ht_destroy(&vih->codes_size);
ht_destroy(&vih->verbs_hits);
ht_destroy(&vih->verbs_size);
ht_destroy(&vih->types_hits);
ht_destroy(&vih->types_size);
ht_destroy(&vih->month_hits);
ht_destroy(&vih->month_size);
ht_destroy(&vih->error404);
ht_destroy(&vih->date);
}
/* Reset handler informations to support --reset option in
* stream mode. */
void vi_reset(struct vih *vih) {
vi_reset_combined_maps(vih);
vi_reset_hashtables(vih);
}
/* Return a new visitors handle.
* On out of memory NULL is returned.
* The handle obtained with this call must be released with vi_free()
* when no longer useful. */
struct vih *vi_new(void) {
struct vih *vih;
if ((vih = malloc(sizeof(*vih))) == NULL)
return NULL;
/* Initialization */
vih->startt = vih->endt = time(NULL);
vih->processed = 0;
vih->invalid = 0;
vih->blacklisted = 0;
vi_reset_combined_maps(vih);
vih->error = NULL;
vi_ht_init(&vih->users_hits);
vi_ht_init(&vih->users_size);
vi_ht_init(&vih->hosts_hits);
vi_ht_init(&vih->hosts_size);
vi_ht_init(&vih->pages_hits);
vi_ht_init(&vih->pages_size);
vi_ht_init(&vih->sites_hits);
vi_ht_init(&vih->sites_size);
vi_ht_init(&vih->codes_hits);
vi_ht_init(&vih->codes_size);
vi_ht_init(&vih->verbs_hits);
vi_ht_init(&vih->verbs_size);
vi_ht_init(&vih->types_hits);
vi_ht_init(&vih->types_size);
vi_ht_init(&vih->month_hits);
vi_ht_init(&vih->month_size);
vi_ht_init(&vih->error404);
vi_ht_init(&vih->date);
return vih;
}
/* Free an handle created with vi_new(). */
void vi_free(struct vih *vih) {
if (!vih) return;
vi_reset_hashtables(vih);
vi_clear_error(vih);
free(vih);
}
/* Add a new entry in the counter hashtable. If the key does not
* exists creates a new entry with "1" as number of hits, otherwise
* increment the old value.
*
* Return the value of hits after the increment or creation. If the
* returned value is greater than one, the key was already seen.
*
* Return 0 on out of memory.
*
* NOTE: the pointer of the "value" part of the hashtable entry is
* used as a counter casting it to a "long" integer. */
int vi_counter_incr(struct hashtable *ht, char *key) {
char *k;
unsigned int idx;
int r;
long val;
r = ht_search(ht, key, &idx);
if (r == HT_NOTFOUND) {
k = strdup(key);
if (k == NULL) return 0;
if (ht_add(ht, k, (void*)1) != HT_OK) {
free(k);
return 0;
}
return 1;
} else {
val = (long) ht_value(ht, idx);
val++;
ht_value(ht, idx) = (void*) val;
return val;
}
}
/* Similar to vi_counter_incr, but only read the old value of
* the counter without to alter it. If the specified key does not
* exists zero is returned. */
int vi_counter_val(struct hashtable *ht, char *key) {
unsigned int idx;
int r;
long val;
r = ht_search(ht, key, &idx);
if (r == HT_NOTFOUND) {
return 0;
} else {
val = (long) ht_value(ht, idx);
return val;
}
}
/* Add a new entry in the traffic hashtable. If the key does not
* exists creates a new entry with the size, otherwise adds to
* the old value.
*
* Return the value of size after the increment or creation. If the
* returned value is greater than zero, the key was already seen.
*
* Return 0 on out of memory.
*
* NOTE: the pointer of the "value" part of the hashtable entry is
* used as a total casting it to a "long" integer. */
int vi_traffic_incr(struct hashtable *ht, char *key, long size) {
char *k;
unsigned int idx;
int r;
long val;
r = ht_search(ht, key, &idx);
if (r == HT_NOTFOUND) {
k = strdup(key);
if (k == NULL) return 0;
if (ht_add(ht, k, (void*)1) != HT_OK) {
free(k);
return 0;
}
return 1;
} else {
val = (long) ht_value(ht, idx);
val += size;
ht_value(ht, idx) = (void*) val;
return val;
}
}
/* Similar to vi_traffic_incr, but only read the old value of
* the size without altering it. If the specified key does not
* exists zero is returned. */
int vi_traffic_val(struct hashtable *ht, char *key) {
unsigned int idx;
int r;
long val;
r = ht_search(ht, key, &idx);
if (r == HT_NOTFOUND) {
return 0;
} else {
val = (long) ht_value(ht, idx);
return val;
}
}
/* Set a key/value pair inside the hash table with
* a create-else-replace semantic.
*
* Return non-zero on out of memory. */
int vi_replace(struct hashtable *ht, char *key, char *value) {
char *k, *v;
k = strdup(key);
v = strdup(value);
if (!k || !v) goto err;
if (ht_replace(ht, k, v) != HT_OK)
goto err;
return 0;
err:
if (k) free(k);
if (v) free(v);
return 1;
}
/* Replace the time value of the given key with the new one if this
* is newer/older of the old one. If the key is new, it's just added
* to the hash table with the specified time as value.
*
* If the 'ifolder' flag is set, values are replaced with older one,
* otherwise with newer.
* This function is only used by wrappers replace_if_older() and
* replace_if_newer().
*
* Return 0 on success, non-zero on out of memory. */
int vi_replace_time(struct hashtable *ht, char *key, time_t time, int ifolder) {
char *k = NULL;
unsigned int idx;
int r;
r = ht_search(ht, key, &idx);
if (r == HT_NOTFOUND) {
k = strdup(key);
if (!k) goto err;
if (ht_add(ht, k, (void*)time) != HT_OK) goto err;
} else {
time_t oldt = (time_t) ht_value(ht, idx);
/* Update the date if this one is older/nwer. */
if (ifolder) {
if (time < oldt)
ht_value(ht, idx) = (void*) time;
} else {
if (time > oldt)
ht_value(ht, idx) = (void*) time;
}
}
return 0;
err:
if (k) free(k);
return 1;
}
/* see vi_replace_time */
int vi_replace_if_older(struct hashtable *ht, char *key, time_t time) {
return vi_replace_time(ht, key, time, 1);
}
/* see vi_replace_time */
int vi_replace_if_newer(struct hashtable *ht, char *key, time_t time) {
return vi_replace_time(ht, key, time, 0);
}
/* Set an error in the visitors handle */
void vi_set_error(struct vih *vih, char *fmt, ...) {
va_list ap;
char buf[VI_ERROR_MAX];
va_start(ap, fmt);
vsnprintf(buf, VI_ERROR_MAX, fmt, ap);
buf[VI_ERROR_MAX-1] = '\0';
free(vih->error);
vih->error = strdup(buf);
va_end(ap);
}
/* Get the error */
char *vi_get_error(struct vih *vih) {
if (!vih->error) {
return "No error";
}
return vih->error;
}
/* Clear the error */
void vi_clear_error(struct vih *vih) {
free(vih->error);
vih->error = NULL;
}
/*----------------------------------- parsing ----------------------------- */
/* Parse a line of log, and fill the logline structure with
* appropriate values. On error (bad line format) non-zero is returned.
* sample line from apache http log
* 192.168.206.108 - - [19/Apr/2011:12:17:53 -0500] "GET /images/Ola.gif HTTP/1.1" 200 586 "http://web.cmc.com.cu/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
* host - - [date:time timezone] "verb url ver" code size "referrer" "agent"
* sample line from kerio winroute http log
* 192.168.206.15 - Admin [16/Feb/2011:17:19:57 -0500] "GET http://wc.cmc.com.cu:3000/WorldClient.dll?View=Logout HTTP/1.1" 200 10870 +3
* host - user [date:time timezone] "verb url ver" code size +connections
*/
int vi_parse_line(struct logline *ll, char *l) {
char *date, *hour, *timezone, *host, *req, *p, *user = NULL, *code, *size, *verb = NULL;
char *req_end = NULL, *user_end = NULL; //, *code_end = NULL, *size_end = NULL;
/* Seek the start of the different components */
/* host */
host = l;
/* date */
if ((date = strchr(l, '[')) == NULL) return 1;