Commit | Line | Data |
---|---|---|
e015f748 CE |
1 | /* |
2 | webalizer - a web server log analysis program | |
3 | ||
4 | Copyright (C) 1997-2011 Bradford L. Barrett | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version, and provided that the above | |
10 | copyright and permission notice is included with all distributed | |
11 | copies of this or derived software. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | |
21 | ||
22 | */ | |
23 | ||
24 | /*********************************************/ | |
25 | /* STANDARD INCLUDES */ | |
26 | /*********************************************/ | |
27 | ||
28 | #include <time.h> | |
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | #include <unistd.h> /* normal stuff */ | |
33 | #include <ctype.h> | |
34 | #include <sys/utsname.h> | |
35 | ||
36 | /* ensure sys/types */ | |
37 | #ifndef _SYS_TYPES_H | |
38 | #include <sys/types.h> | |
39 | #endif | |
40 | ||
41 | /* some need for uint* */ | |
42 | #ifdef HAVE_STDINT_H | |
43 | #include <stdint.h> | |
44 | #endif | |
45 | ||
46 | /* need socket header? */ | |
47 | #ifdef HAVE_SYS_SOCKET_H | |
48 | #include <sys/socket.h> | |
49 | #endif | |
50 | ||
51 | /* some systems need this */ | |
52 | #ifdef HAVE_MATH_H | |
53 | #include <math.h> | |
54 | #endif | |
55 | ||
56 | #include "webalizer.h" /* main header */ | |
57 | #include "lang.h" | |
58 | #include "linklist.h" | |
59 | #include "hashtab.h" | |
60 | ||
61 | /* internal function prototypes */ | |
62 | ||
63 | HNODEPTR new_hnode(char *); /* new host node */ | |
64 | UNODEPTR new_unode(char *); /* new url node */ | |
65 | RNODEPTR new_rnode(char *); /* new referrer node */ | |
66 | ANODEPTR new_anode(char *); /* new user agent node */ | |
67 | SNODEPTR new_snode(char *); /* new search string.. */ | |
68 | INODEPTR new_inode(char *); /* new ident node */ | |
69 | #ifdef USE_DNS | |
70 | DNODEPTR new_dnode(char *); /* new DNS node */ | |
71 | #endif /* USE_DNS */ | |
72 | ||
73 | void update_entry(char *); /* update entry/exit */ | |
74 | void update_exit(char *); /* page totals */ | |
75 | ||
76 | unsigned int hash(char *); /* hash function */ | |
77 | ||
78 | /* local data */ | |
79 | ||
80 | HNODEPTR sm_htab[MAXHASH]; /* hash tables */ | |
81 | HNODEPTR sd_htab[MAXHASH]; | |
82 | UNODEPTR um_htab[MAXHASH]; /* for hits, sites, */ | |
83 | RNODEPTR rm_htab[MAXHASH]; /* referrers and agents... */ | |
84 | ANODEPTR am_htab[MAXHASH]; | |
85 | SNODEPTR sr_htab[MAXHASH]; /* search string table */ | |
86 | INODEPTR im_htab[MAXHASH]; /* ident table (username) */ | |
87 | #ifdef USE_DNS | |
88 | DNODEPTR host_table[MAXHASH]; /* DNS hash table */ | |
89 | #endif /* USE_DNS */ | |
90 | ||
91 | /* Last node pointers */ | |
92 | HNODEPTR lm_hnode=NULL; | |
93 | HNODEPTR ld_hnode=NULL; | |
94 | RNODEPTR l_rnode=NULL; | |
95 | ||
96 | /*********************************************/ | |
97 | /* DEL_HTABS - clear out our hash tables */ | |
98 | /*********************************************/ | |
99 | ||
100 | void del_htabs() | |
101 | { | |
102 | del_hlist(sd_htab); /* Clear out our various */ | |
103 | del_ulist(um_htab); /* hash tables here by */ | |
104 | del_hlist(sm_htab); /* calling the appropriate */ | |
105 | del_rlist(rm_htab); /* del_* fuction for each */ | |
106 | del_alist(am_htab); | |
107 | del_slist(sr_htab); | |
108 | del_ilist(im_htab); | |
109 | #ifdef USE_DNS | |
110 | /* del_dlist(host_table); */ /* delete DNS hash table */ | |
111 | #endif /* USE_DNS */ | |
112 | } | |
113 | ||
114 | /*********************************************/ | |
115 | /* NEW_HNODE - create host node */ | |
116 | /*********************************************/ | |
117 | ||
118 | HNODEPTR new_hnode(char *str) | |
119 | { | |
120 | HNODEPTR newptr; | |
121 | char *sptr; | |
122 | ||
123 | if (strlen(str) >= MAXHOST) | |
124 | { | |
125 | if (verbose) | |
126 | { | |
127 | fprintf(stderr,"[new_hnode] %s (%d)",msg_big_one,strlen(str)); | |
128 | if (debug_mode) | |
129 | fprintf(stderr,":\n--> %s",str); | |
130 | fprintf(stderr,"\n"); | |
131 | } | |
132 | str[MAXHOST-1]=0; | |
133 | } | |
134 | ||
135 | if ( (sptr=malloc(strlen(str)+1))==NULL ) return (HNODEPTR)NULL; | |
136 | strcpy(sptr,str); | |
137 | ||
138 | if (( newptr = malloc(sizeof(struct hnode))) != NULL) | |
139 | { | |
140 | newptr->string =sptr; | |
141 | newptr->visit =0; | |
142 | newptr->tstamp =0; | |
143 | newptr->lasturl =blank_str; | |
144 | } | |
145 | else free(sptr); | |
146 | return newptr; | |
147 | } | |
148 | ||
149 | /*********************************************/ | |
150 | /* PUT_HNODE - insert/update host node */ | |
151 | /*********************************************/ | |
152 | ||
153 | int put_hnode( char *str, /* Hostname */ | |
154 | int type, /* obj type */ | |
155 | u_int64_t count, /* hit count */ | |
156 | u_int64_t file, /* File flag */ | |
157 | double xfer, /* xfer size */ | |
158 | u_int64_t *ctr, /* counter */ | |
159 | u_int64_t visit, /* visits */ | |
160 | u_int64_t tstamp,/* timestamp */ | |
161 | char *lasturl, /* lasturl */ | |
162 | HNODEPTR *htab) /* ptr>next */ | |
163 | { | |
164 | HNODEPTR cptr,nptr; | |
165 | unsigned int hval; | |
166 | ||
167 | /* check if hashed */ | |
168 | hval=hash(str); | |
169 | if ( (cptr = htab[hval]) == NULL) | |
170 | { | |
171 | /* not hashed */ | |
172 | if ( (nptr=new_hnode(str)) != NULL) | |
173 | { | |
174 | if (htab==sm_htab) lm_hnode=nptr; | |
175 | else ld_hnode=nptr; | |
176 | nptr->flag = type; | |
177 | nptr->count = count; | |
178 | nptr->files = file; | |
179 | nptr->xfer = xfer; | |
180 | nptr->next = NULL; | |
181 | htab[hval] = nptr; | |
182 | if (type!=OBJ_GRP) (*ctr)++; | |
183 | ||
184 | if (visit) | |
185 | { | |
186 | nptr->visit=(visit-1); | |
187 | nptr->lasturl=find_url(lasturl); | |
188 | nptr->tstamp=tstamp; | |
189 | return 0; | |
190 | } | |
191 | else | |
192 | { | |
193 | if (ispage(log_rec.url)) | |
194 | { | |
195 | if (htab==sm_htab) update_entry(log_rec.url); | |
196 | nptr->lasturl=find_url(log_rec.url); | |
197 | nptr->tstamp=tstamp; | |
198 | nptr->visit=1; | |
199 | } | |
200 | } | |
201 | } | |
202 | } | |
203 | else | |
204 | { | |
205 | /* hashed (SPEEDUP) */ | |
206 | if (htab==sm_htab) | |
207 | { | |
208 | if (lm_hnode!=NULL && strcmp(lm_hnode->string,str)==0) cptr=lm_hnode; | |
209 | } | |
210 | else | |
211 | { | |
212 | if (ld_hnode!=NULL && strcmp(ld_hnode->string,str)==0) cptr=ld_hnode; | |
213 | } | |
214 | ||
215 | while (cptr != NULL) | |
216 | { | |
217 | if (strcmp(cptr->string,str)==0) | |
218 | { | |
219 | if ((type==cptr->flag)||((type!=OBJ_GRP)&&(cptr->flag!=OBJ_GRP))) | |
220 | { | |
221 | /* found... bump counter */ | |
222 | cptr->count+=count; | |
223 | cptr->files+=file; | |
224 | cptr->xfer +=xfer; | |
225 | ||
226 | if (ispage(log_rec.url)) | |
227 | { | |
228 | if ((tstamp-cptr->tstamp)>=visit_timeout) | |
229 | { | |
230 | cptr->visit++; | |
231 | if (htab==sm_htab) | |
232 | { | |
233 | update_exit(cptr->lasturl); | |
234 | update_entry(log_rec.url); | |
235 | } | |
236 | } | |
237 | cptr->lasturl=find_url(log_rec.url); | |
238 | cptr->tstamp=tstamp; | |
239 | } | |
240 | if (htab==sm_htab) lm_hnode=cptr; | |
241 | else ld_hnode=cptr; | |
242 | return 0; | |
243 | } | |
244 | } | |
245 | cptr = cptr->next; | |
246 | } | |
247 | /* not found... */ | |
248 | if ( (nptr = new_hnode(str)) != NULL) | |
249 | { | |
250 | if (htab==sm_htab) lm_hnode=nptr; | |
251 | else ld_hnode=nptr; | |
252 | nptr->flag = type; | |
253 | nptr->count = count; | |
254 | nptr->files = file; | |
255 | nptr->xfer = xfer; | |
256 | nptr->next = htab[hval]; | |
257 | htab[hval]=nptr; | |
258 | if (type!=OBJ_GRP) (*ctr)++; | |
259 | ||
260 | if (visit) | |
261 | { | |
262 | nptr->visit = (visit-1); | |
263 | nptr->lasturl=find_url(lasturl); | |
264 | nptr->tstamp= tstamp; | |
265 | return 0; | |
266 | } | |
267 | else | |
268 | { | |
269 | if (ispage(log_rec.url)) | |
270 | { | |
271 | if (htab==sm_htab) update_entry(log_rec.url); | |
272 | nptr->lasturl=find_url(log_rec.url); | |
273 | nptr->tstamp= tstamp; | |
274 | nptr->visit=1; | |
275 | } | |
276 | } | |
277 | } | |
278 | } | |
279 | ||
280 | if (nptr!=NULL) | |
281 | { | |
282 | /* set object type */ | |
283 | if (type==OBJ_GRP) nptr->flag=OBJ_GRP; /* is it a grouping? */ | |
284 | else | |
285 | { | |
286 | /* check if it's a hidden object */ | |
287 | if ((hide_sites)||(isinlist(hidden_sites,nptr->string)!=NULL)) | |
288 | nptr->flag=OBJ_HIDE; | |
289 | if (htab==sm_htab) lm_hnode=nptr; | |
290 | else ld_hnode=nptr; | |
291 | } | |
292 | } | |
293 | return nptr==NULL; | |
294 | } | |
295 | ||
296 | /*********************************************/ | |
297 | /* DEL_HLIST - delete host hash table */ | |
298 | /*********************************************/ | |
299 | ||
300 | void del_hlist(HNODEPTR *htab) | |
301 | { | |
302 | /* free memory used by hash table */ | |
303 | HNODEPTR aptr,temp; | |
304 | int i; | |
305 | ||
306 | for (i=0;i<MAXHASH;i++) | |
307 | { | |
308 | if (htab[i] != NULL) | |
309 | { | |
310 | aptr = htab[i]; | |
311 | while (aptr != NULL) | |
312 | { | |
313 | temp = aptr->next; | |
314 | free (aptr->string); /* free hostname string space */ | |
315 | free (aptr); /* free hostname structure */ | |
316 | aptr = temp; | |
317 | } | |
318 | htab[i]=NULL; | |
319 | } | |
320 | } | |
321 | lm_hnode=NULL; | |
322 | ld_hnode=NULL; | |
323 | } | |
324 | ||
325 | /*********************************************/ | |
326 | /* NEW_UNODE - URL node creation */ | |
327 | /*********************************************/ | |
328 | ||
329 | UNODEPTR new_unode(char *str) | |
330 | { | |
331 | UNODEPTR newptr; | |
332 | char *sptr; | |
333 | ||
334 | if (strlen(str) >= MAXURLH) | |
335 | { | |
336 | if (verbose) | |
337 | { | |
338 | fprintf(stderr,"[new_unode] %s (%d)",msg_big_one,strlen(str)); | |
339 | if (debug_mode) | |
340 | fprintf(stderr,":\n--> %s",str); | |
341 | fprintf(stderr,"\n"); | |
342 | } | |
343 | str[MAXURLH-1]=0; | |
344 | } | |
345 | ||
346 | if ( (sptr=malloc(strlen(str)+1))==NULL) return (UNODEPTR)NULL; | |
347 | strcpy(sptr,str); | |
348 | ||
349 | if (( newptr = malloc(sizeof(struct unode))) != NULL) | |
350 | { | |
351 | newptr->string=sptr; | |
352 | newptr->count = 0; | |
353 | newptr->flag = OBJ_REG; | |
354 | } | |
355 | else free(sptr); | |
356 | return newptr; | |
357 | } | |
358 | ||
359 | /*********************************************/ | |
360 | /* PUT_UNODE - insert/update URL node */ | |
361 | /*********************************************/ | |
362 | ||
363 | int put_unode(char *str, int type, u_int64_t count, double xfer, | |
364 | u_int64_t *ctr, u_int64_t entry, u_int64_t exit, UNODEPTR *htab) | |
365 | { | |
366 | UNODEPTR cptr,nptr; | |
367 | unsigned int hval; | |
368 | ||
369 | if (str[0]=='-') return 0; | |
370 | ||
371 | hval = hash(str); | |
372 | /* check if hashed */ | |
373 | if ( (cptr = htab[hval]) == NULL) | |
374 | { | |
375 | /* not hashed */ | |
376 | if ( (nptr=new_unode(str)) != NULL) | |
377 | { | |
378 | nptr->flag = type; | |
379 | nptr->count= count; | |
380 | nptr->xfer = xfer; | |
381 | nptr->next = NULL; | |
382 | nptr->entry= entry; | |
383 | nptr->exit = exit; | |
384 | htab[hval] = nptr; | |
385 | if (type!=OBJ_GRP) (*ctr)++; | |
386 | } | |
387 | } | |
388 | else | |
389 | { | |
390 | /* hashed */ | |
391 | while (cptr != NULL) | |
392 | { | |
393 | if (strcmp(cptr->string,str)==0) | |
394 | { | |
395 | if ((type==cptr->flag)||((type!=OBJ_GRP)&&(cptr->flag!=OBJ_GRP))) | |
396 | { | |
397 | /* found... bump counter */ | |
398 | cptr->count+=count; | |
399 | cptr->xfer += xfer; | |
400 | return 0; | |
401 | } | |
402 | } | |
403 | cptr = cptr->next; | |
404 | } | |
405 | /* not found... */ | |
406 | if ( (nptr = new_unode(str)) != NULL) | |
407 | { | |
408 | nptr->flag = type; | |
409 | nptr->count= count; | |
410 | nptr->xfer = xfer; | |
411 | nptr->next = htab[hval]; | |
412 | nptr->entry= entry; | |
413 | nptr->exit = exit; | |
414 | htab[hval] = nptr; | |
415 | if (type!=OBJ_GRP) (*ctr)++; | |
416 | } | |
417 | } | |
418 | if (nptr!=NULL) | |
419 | { | |
420 | if (type==OBJ_GRP) nptr->flag=OBJ_GRP; | |
421 | else if (isinlist(hidden_urls,nptr->string)!=NULL) | |
422 | nptr->flag=OBJ_HIDE; | |
423 | } | |
424 | return nptr==NULL; | |
425 | } | |
426 | ||
427 | /*********************************************/ | |
428 | /* DEL_ULIST - delete URL hash table */ | |
429 | /*********************************************/ | |
430 | ||
431 | void del_ulist(UNODEPTR *htab) | |
432 | { | |
433 | /* free memory used by hash table */ | |
434 | UNODEPTR aptr,temp; | |
435 | int i; | |
436 | ||
437 | for (i=0;i<MAXHASH;i++) | |
438 | { | |
439 | if (htab[i] != NULL) | |
440 | { | |
441 | aptr = htab[i]; | |
442 | while (aptr != NULL) | |
443 | { | |
444 | temp = aptr->next; | |
445 | free (aptr->string); /* free up URL string memory */ | |
446 | free (aptr); /* free up URL struct node */ | |
447 | aptr = temp; | |
448 | } | |
449 | htab[i]=NULL; | |
450 | } | |
451 | } | |
452 | } | |
453 | ||
454 | /*********************************************/ | |
455 | /* NEW_RNODE - Referrer node creation */ | |
456 | /*********************************************/ | |
457 | ||
458 | RNODEPTR new_rnode(char *str) | |
459 | { | |
460 | RNODEPTR newptr; | |
461 | char *sptr; | |
462 | ||
463 | if (strlen(str) >= MAXREFH) | |
464 | { | |
465 | if (verbose) | |
466 | { | |
467 | fprintf(stderr,"[new_rnode] %s (%d)",msg_big_one,strlen(str)); | |
468 | if (debug_mode) | |
469 | fprintf(stderr,":\n--> %s",str); | |
470 | fprintf(stderr,"\n"); | |
471 | } | |
472 | str[MAXREFH-1]=0; | |
473 | } | |
474 | ||
475 | if ( (sptr=malloc(strlen(str)+1))==NULL ) return (RNODEPTR)NULL; | |
476 | strcpy(sptr,str); | |
477 | ||
478 | if (( newptr = malloc(sizeof(struct rnode))) != NULL) | |
479 | { | |
480 | newptr->string= sptr; | |
481 | newptr->count = 1; | |
482 | newptr->flag = OBJ_REG; | |
483 | } | |
484 | else free(sptr); | |
485 | return newptr; | |
486 | } | |
487 | ||
488 | /*********************************************/ | |
489 | /* PUT_RNODE - insert/update referrer node */ | |
490 | /*********************************************/ | |
491 | ||
492 | int put_rnode(char *str, int type, u_int64_t count, | |
493 | u_int64_t *ctr, RNODEPTR *htab) | |
494 | { | |
495 | RNODEPTR cptr,nptr; | |
496 | unsigned int hval; | |
497 | ||
498 | if (str[0]=='-') strcpy(str,"- (Direct Request)"); | |
499 | ||
500 | hval = hash(str); | |
501 | /* check if hashed */ | |
502 | if ( (cptr = htab[hval]) == NULL) | |
503 | { | |
504 | /* not hashed */ | |
505 | if ( (nptr=new_rnode(str)) != NULL) | |
506 | { | |
507 | nptr->flag = type; | |
508 | nptr->count = count; | |
509 | nptr->next = NULL; | |
510 | htab[hval] = nptr; | |
511 | if (type!=OBJ_GRP) (*ctr)++; | |
512 | } | |
513 | } | |
514 | else | |
515 | { | |
516 | /* hashed (SPEEDUP) */ | |
517 | if (l_rnode!=NULL && strcmp(l_rnode->string,str)==0) cptr=l_rnode; | |
518 | ||
519 | while (cptr != NULL) | |
520 | { | |
521 | if (strcmp(cptr->string,str)==0) | |
522 | { | |
523 | if ((type==cptr->flag)||((type!=OBJ_GRP)&&(cptr->flag!=OBJ_GRP))) | |
524 | { | |
525 | /* found... bump counter */ | |
526 | cptr->count+=count; | |
527 | return 0; | |
528 | } | |
529 | } | |
530 | cptr = cptr->next; | |
531 | } | |
532 | /* not found... */ | |
533 | if ( (nptr = new_rnode(str)) != NULL) | |
534 | { | |
535 | nptr->flag = type; | |
536 | nptr->count = count; | |
537 | nptr->next = htab[hval]; | |
538 | htab[hval] = nptr; | |
539 | if (type!=OBJ_GRP) (*ctr)++; | |
540 | } | |
541 | } | |
542 | if (nptr!=NULL) | |
543 | { | |
544 | if (type==OBJ_GRP) nptr->flag=OBJ_GRP; | |
545 | else if (isinlist(hidden_refs,nptr->string)!=NULL) | |
546 | nptr->flag=OBJ_HIDE; | |
547 | l_rnode=nptr; | |
548 | } | |
549 | return nptr==NULL; | |
550 | } | |
551 | ||
552 | /*********************************************/ | |
553 | /* DEL_RLIST - delete referrer hash table */ | |
554 | /*********************************************/ | |
555 | ||
556 | void del_rlist(RNODEPTR *htab) | |
557 | { | |
558 | /* free memory used by hash table */ | |
559 | RNODEPTR aptr,temp; | |
560 | int i; | |
561 | ||
562 | for (i=0;i<MAXHASH;i++) | |
563 | { | |
564 | if (htab[i] != NULL) | |
565 | { | |
566 | aptr = htab[i]; | |
567 | while (aptr != NULL) | |
568 | { | |
569 | temp = aptr->next; | |
570 | free (aptr->string); | |
571 | free (aptr); | |
572 | aptr = temp; | |
573 | } | |
574 | htab[i]=NULL; | |
575 | } | |
576 | } | |
577 | l_rnode=NULL; | |
578 | } | |
579 | ||
580 | /*********************************************/ | |
581 | /* NEW_ANODE - User Agent node creation */ | |
582 | /*********************************************/ | |
583 | ||
584 | ANODEPTR new_anode(char *str) | |
585 | { | |
586 | ANODEPTR newptr; | |
587 | char *sptr; | |
588 | ||
589 | if (strlen(str) >= MAXAGENT) | |
590 | { | |
591 | if (verbose) | |
592 | { | |
593 | fprintf(stderr,"[new_anode] %s (%d)",msg_big_one,strlen(str)); | |
594 | if (debug_mode) | |
595 | fprintf(stderr,":\n--> %s",str); | |
596 | fprintf(stderr,"\n"); | |
597 | } | |
598 | str[MAXAGENT-1]=0; | |
599 | } | |
600 | ||
601 | if ( (sptr=malloc(strlen(str)+1))==NULL ) return (ANODEPTR)NULL; | |
602 | strcpy(sptr,str); | |
603 | ||
604 | if (( newptr = malloc(sizeof(struct anode))) != NULL) | |
605 | { | |
606 | newptr->string= sptr; | |
607 | newptr->count = 1; | |
608 | newptr->flag = OBJ_REG; | |
609 | } | |
610 | else free(sptr); | |
611 | return newptr; | |
612 | } | |
613 | ||
614 | /*********************************************/ | |
615 | /* PUT_ANODE - insert/update user agent node */ | |
616 | /*********************************************/ | |
617 | ||
618 | int put_anode(char *str, int type, u_int64_t count, | |
619 | u_int64_t *ctr, ANODEPTR *htab) | |
620 | { | |
621 | ANODEPTR cptr,nptr; | |
622 | unsigned int hval; | |
623 | ||
624 | if (str[0]=='-') return 0; /* skip bad user agents */ | |
625 | ||
626 | hval = hash(str); | |
627 | /* check if hashed */ | |
628 | if ( (cptr = htab[hval]) == NULL) | |
629 | { | |
630 | /* not hashed */ | |
631 | if ( (nptr=new_anode(str)) != NULL) | |
632 | { | |
633 | nptr->flag = type; | |
634 | nptr->count= count; | |
635 | nptr->next = NULL; | |
636 | htab[hval] = nptr; | |
637 | if (type!=OBJ_GRP) (*ctr)++; | |
638 | } | |
639 | } | |
640 | else | |
641 | { | |
642 | /* hashed */ | |
643 | while (cptr != NULL) | |
644 | { | |
645 | if (strcmp(cptr->string,str)==0) | |
646 | { | |
647 | if ((type==cptr->flag)||((type!=OBJ_GRP)&&(cptr->flag!=OBJ_GRP))) | |
648 | { | |
649 | /* found... bump counter */ | |
650 | cptr->count+=count; | |
651 | return 0; | |
652 | } | |
653 | } | |
654 | cptr = cptr->next; | |
655 | } | |
656 | /* not found... */ | |
657 | if ( (nptr = new_anode(str)) != NULL) | |
658 | { | |
659 | nptr->flag = type; | |
660 | nptr->count = count; | |
661 | nptr->next = htab[hval]; | |
662 | htab[hval] = nptr; | |
663 | if (type!=OBJ_GRP) (*ctr)++; | |
664 | } | |
665 | } | |
666 | if (type==OBJ_GRP) nptr->flag=OBJ_GRP; | |
667 | else if (isinlist(hidden_agents,nptr->string)!=NULL) | |
668 | nptr->flag=OBJ_HIDE; | |
669 | return nptr==NULL; | |
670 | } | |
671 | ||
672 | /*********************************************/ | |
673 | /* DEL_ALIST - delete user agent hash table */ | |
674 | /*********************************************/ | |
675 | ||
676 | void del_alist(ANODEPTR *htab) | |
677 | { | |
678 | /* free memory used by hash table */ | |
679 | ANODEPTR aptr,temp; | |
680 | int i; | |
681 | ||
682 | for (i=0;i<MAXHASH;i++) | |
683 | { | |
684 | if (htab[i] != NULL) | |
685 | { | |
686 | aptr = htab[i]; | |
687 | while (aptr != NULL) | |
688 | { | |
689 | temp = aptr->next; | |
690 | free (aptr->string); | |
691 | free (aptr); | |
692 | aptr = temp; | |
693 | } | |
694 | htab[i]=NULL; | |
695 | } | |
696 | } | |
697 | } | |
698 | ||
699 | /*********************************************/ | |
700 | /* NEW_SNODE - Search str node creation */ | |
701 | /*********************************************/ | |
702 | ||
703 | SNODEPTR new_snode(char *str) | |
704 | { | |
705 | SNODEPTR newptr; | |
706 | char *sptr; | |
707 | ||
708 | if (strlen(str) >= MAXSRCHH) | |
709 | { | |
710 | if (verbose) | |
711 | { | |
712 | fprintf(stderr,"[new_snode] %s (%d)",msg_big_one,strlen(str)); | |
713 | if (debug_mode) | |
714 | fprintf(stderr,":\n--> %s",str); | |
715 | fprintf(stderr,"\n"); | |
716 | } | |
717 | str[MAXSRCHH-1]=0; | |
718 | } | |
719 | ||
720 | if ( (sptr=malloc(strlen(str)+1))==NULL ) return (SNODEPTR)NULL; | |
721 | strcpy(sptr,str); | |
722 | ||
723 | if (( newptr = malloc(sizeof(struct snode))) != NULL) | |
724 | { | |
725 | newptr->string= sptr; | |
726 | newptr->count = 1; | |
727 | } | |
728 | else free(sptr); | |
729 | return newptr; | |
730 | } | |
731 | ||
732 | /*********************************************/ | |
733 | /* PUT_SNODE - insert/update search str node */ | |
734 | /*********************************************/ | |
735 | ||
736 | int put_snode(char *str, u_int64_t count, SNODEPTR *htab) | |
737 | { | |
738 | SNODEPTR cptr,nptr; | |
739 | unsigned int hval; | |
740 | ||
741 | if (str[0]==0 || str[0]==' ') return 0; /* skip bad search strs */ | |
742 | ||
743 | hval=hash(str); | |
744 | /* check if hashed */ | |
745 | if ( (cptr = htab[hval]) == NULL) | |
746 | { | |
747 | /* not hashed */ | |
748 | if ( (nptr=new_snode(str)) != NULL) | |
749 | { | |
750 | nptr->count = count; | |
751 | nptr->next = NULL; | |
752 | htab[hval] = nptr; | |
753 | } | |
754 | } | |
755 | else | |
756 | { | |
757 | /* hashed */ | |
758 | while (cptr != NULL) | |
759 | { | |
760 | if (strcmp(cptr->string,str)==0) | |
761 | { | |
762 | /* found... bump counter */ | |
763 | cptr->count+=count; | |
764 | return 0; | |
765 | } | |
766 | cptr = cptr->next; | |
767 | } | |
768 | /* not found... */ | |
769 | if ( (nptr = new_snode(str)) != NULL) | |
770 | { | |
771 | nptr->count = count; | |
772 | nptr->next = htab[hval]; | |
773 | htab[hval] = nptr; | |
774 | } | |
775 | } | |
776 | return nptr==NULL; | |
777 | } | |
778 | ||
779 | /*********************************************/ | |
780 | /* DEL_SLIST - delete search str hash table */ | |
781 | /*********************************************/ | |
782 | ||
783 | void del_slist(SNODEPTR *htab) | |
784 | { | |
785 | /* free memory used by hash table */ | |
786 | SNODEPTR aptr,temp; | |
787 | int i; | |
788 | ||
789 | for (i=0;i<MAXHASH;i++) | |
790 | { | |
791 | if (htab[i] != NULL) | |
792 | { | |
793 | aptr = htab[i]; | |
794 | while (aptr != NULL) | |
795 | { | |
796 | temp = aptr->next; | |
797 | free (aptr->string); | |
798 | free (aptr); | |
799 | aptr = temp; | |
800 | } | |
801 | htab[i]=NULL; | |
802 | } | |
803 | } | |
804 | } | |
805 | ||
806 | /*********************************************/ | |
807 | /* NEW_INODE - create ident (username) node */ | |
808 | /*********************************************/ | |
809 | ||
810 | INODEPTR new_inode(char *str) | |
811 | { | |
812 | INODEPTR newptr; | |
813 | char *sptr; | |
814 | ||
815 | if (strlen(str) >= MAXIDENT) | |
816 | { | |
817 | if (verbose) | |
818 | { | |
819 | fprintf(stderr,"[new_inode] %s (%d)",msg_big_one,strlen(str)); | |
820 | if (debug_mode) | |
821 | fprintf(stderr,":\n--> %s",str); | |
822 | fprintf(stderr,"\n"); | |
823 | } | |
824 | str[MAXIDENT-1]=0; | |
825 | } | |
826 | ||
827 | if ( (sptr=malloc(strlen(str)+1))==NULL ) return (INODEPTR)NULL; | |
828 | strcpy(sptr,str); | |
829 | ||
830 | if (( newptr = malloc(sizeof(struct inode))) != NULL) | |
831 | { | |
832 | newptr->string =sptr; | |
833 | newptr->visit =1; | |
834 | newptr->tstamp =0; | |
835 | } | |
836 | else free(sptr); | |
837 | return newptr; | |
838 | } | |
839 | ||
840 | /*********************************************/ | |
841 | /* PUT_INODE - insert/update ident node */ | |
842 | /*********************************************/ | |
843 | ||
844 | int put_inode( char *str, /* ident str */ | |
845 | int type, /* obj type */ | |
846 | u_int64_t count, /* hit count */ | |
847 | u_int64_t file, /* File flag */ | |
848 | double xfer, /* xfer size */ | |
849 | u_int64_t *ctr, /* counter */ | |
850 | u_int64_t visit, /* visits */ | |
851 | u_int64_t tstamp,/* timestamp */ | |
852 | INODEPTR *htab) /* hashtable */ | |
853 | { | |
854 | INODEPTR cptr,nptr; | |
855 | unsigned int hval; | |
856 | ||
857 | if ((str[0]=='-') || (str[0]==0)) return 0; /* skip if no username */ | |
858 | ||
859 | hval = hash(str); | |
860 | /* check if hashed */ | |
861 | if ( (cptr = htab[hval]) == NULL) | |
862 | { | |
863 | /* not hashed */ | |
864 | if ( (nptr=new_inode(str)) != NULL) | |
865 | { | |
866 | nptr->flag = type; | |
867 | nptr->count = count; | |
868 | nptr->files = file; | |
869 | nptr->xfer = xfer; | |
870 | nptr->next = NULL; | |
871 | htab[hval] = nptr; | |
872 | if (type!=OBJ_GRP) (*ctr)++; | |
873 | ||
874 | if (visit) | |
875 | { | |
876 | nptr->visit=(visit-1); | |
877 | nptr->tstamp=tstamp; | |
878 | return 0; | |
879 | } | |
880 | else | |
881 | { | |
882 | if (ispage(log_rec.url)) nptr->tstamp=tstamp; | |
883 | } | |
884 | } | |
885 | } | |
886 | else | |
887 | { | |
888 | /* hashed */ | |
889 | while (cptr != NULL) | |
890 | { | |
891 | if (strcmp(cptr->string,str)==0) | |
892 | { | |
893 | if ((type==cptr->flag)||((type!=OBJ_GRP)&&(cptr->flag!=OBJ_GRP))) | |
894 | { | |
895 | /* found... bump counter */ | |
896 | cptr->count+=count; | |
897 | cptr->files+=file; | |
898 | cptr->xfer +=xfer; | |
899 | ||
900 | if (ispage(log_rec.url)) | |
901 | { | |
902 | if ((tstamp-cptr->tstamp)>=visit_timeout) | |
903 | cptr->visit++; | |
904 | cptr->tstamp=tstamp; | |
905 | } | |
906 | return 0; | |
907 | } | |
908 | } | |
909 | cptr = cptr->next; | |
910 | } | |
911 | /* not found... */ | |
912 | if ( (nptr = new_inode(str)) != NULL) | |
913 | { | |
914 | nptr->flag = type; | |
915 | nptr->count = count; | |
916 | nptr->files = file; | |
917 | nptr->xfer = xfer; | |
918 | nptr->next = htab[hval]; | |
919 | htab[hval] = nptr; | |
920 | if (type!=OBJ_GRP) (*ctr)++; | |
921 | ||
922 | if (visit) | |
923 | { | |
924 | nptr->visit = (visit-1); | |
925 | nptr->tstamp= tstamp; | |
926 | return 0; | |
927 | } | |
928 | else | |
929 | { | |
930 | if (ispage(log_rec.url)) nptr->tstamp= tstamp; | |
931 | } | |
932 | } | |
933 | } | |
934 | ||
935 | if (nptr!=NULL) | |
936 | { | |
937 | /* set object type */ | |
938 | if (type==OBJ_GRP) nptr->flag=OBJ_GRP; /* is it a grouping? */ | |
939 | else | |
940 | { | |
941 | /* check if it's a hidden object */ | |
942 | if (isinlist(hidden_users,nptr->string)!=NULL) | |
943 | nptr->flag=OBJ_HIDE; | |
944 | } | |
945 | } | |
946 | return nptr==NULL; | |
947 | } | |
948 | ||
949 | /*********************************************/ | |
950 | /* DEL_ILIST - delete ident hash table */ | |
951 | /*********************************************/ | |
952 | ||
953 | void del_ilist(INODEPTR *htab) | |
954 | { | |
955 | /* free memory used by hash table */ | |
956 | INODEPTR aptr,temp; | |
957 | int i; | |
958 | ||
959 | for (i=0;i<MAXHASH;i++) | |
960 | { | |
961 | if (htab[i] != NULL) | |
962 | { | |
963 | aptr = htab[i]; | |
964 | while (aptr != NULL) | |
965 | { | |
966 | temp = aptr->next; | |
967 | free (aptr->string); /* free ident string space */ | |
968 | free (aptr); /* free ident structure */ | |
969 | aptr = temp; | |
970 | } | |
971 | htab[i]=NULL; | |
972 | } | |
973 | } | |
974 | } | |
975 | ||
976 | #ifdef USE_DNS /* only add these for DNS */ | |
977 | ||
978 | /*********************************************/ | |
979 | /* NEW_DNODE - DNS resolver node creation */ | |
980 | /*********************************************/ | |
981 | ||
982 | DNODEPTR new_dnode(char *str) | |
983 | { | |
984 | DNODEPTR newptr; | |
985 | char *sptr; | |
986 | ||
987 | if (strlen(str) >= MAXHOST) | |
988 | { | |
989 | if (verbose) | |
990 | { | |
991 | fprintf(stderr,"[new_dnode] %s (%d)",msg_big_one,strlen(str)); | |
992 | if (debug_mode) | |
993 | fprintf(stderr,":\n--> %s",str); | |
994 | fprintf(stderr,"\n"); | |
995 | } | |
996 | str[MAXHOST-1]=0; | |
997 | } | |
998 | ||
999 | if ( (sptr=malloc(strlen(str)+1))==NULL ) return (DNODEPTR)NULL; | |
1000 | strcpy(sptr,str); | |
1001 | ||
1002 | if (( newptr = malloc(sizeof(struct dnode))) != NULL) | |
1003 | { | |
1004 | newptr->string= sptr; | |
1005 | } | |
1006 | else free(sptr); | |
1007 | return newptr; | |
1008 | } | |
1009 | ||
1010 | /*********************************************/ | |
1011 | /* PUT_DNODE - insert/update dns host node */ | |
1012 | /*********************************************/ | |
1013 | ||
1014 | int put_dnode(char *str, void *addr, int len, DNODEPTR *htab) | |
1015 | { | |
1016 | DNODEPTR cptr,nptr; | |
1017 | unsigned int hval; | |
1018 | ||
1019 | if (str[0]==0 || str[0]==' ') return 0; /* skip bad hostnames */ | |
1020 | ||
1021 | hval = hash(str); | |
1022 | /* check if hashed */ | |
1023 | if ( (cptr = htab[hval]) == NULL) | |
1024 | { | |
1025 | /* not hashed */ | |
1026 | if ( (nptr=new_dnode(str)) != NULL) | |
1027 | { | |
1028 | if (addr) memcpy(&nptr->addr, addr, len); | |
1029 | else memset(&nptr->addr, 0, sizeof(struct sockaddr_storage)); | |
1030 | nptr->addrlen = len; | |
1031 | nptr->next = NULL; | |
1032 | htab[hval] = nptr; | |
1033 | } | |
1034 | } | |
1035 | else | |
1036 | { | |
1037 | /* hashed */ | |
1038 | while (cptr != NULL) | |
1039 | { | |
1040 | if (strcmp(cptr->string,str)==0) return 0; | |
1041 | cptr = cptr->next; | |
1042 | } | |
1043 | /* not found... */ | |
1044 | if ( (nptr = new_dnode(str)) != NULL) | |
1045 | { | |
1046 | if (addr) memcpy(&nptr->addr, addr, len); | |
1047 | else memset(&nptr->addr, 0, sizeof(struct sockaddr_storage)); | |
1048 | nptr->addrlen = len; | |
1049 | nptr->next = htab[hval]; | |
1050 | htab[hval] = nptr; | |
1051 | } | |
1052 | } | |
1053 | return nptr==NULL; | |
1054 | } | |
1055 | ||
1056 | /*********************************************/ | |
1057 | /* DEL_DLIST - delete dns hash table */ | |
1058 | /*********************************************/ | |
1059 | ||
1060 | void del_dlist(DNODEPTR *htab) | |
1061 | { | |
1062 | /* free memory used by hash table */ | |
1063 | DNODEPTR dptr,temp; | |
1064 | int i; | |
1065 | ||
1066 | for (i=0;i<MAXHASH;i++) | |
1067 | { | |
1068 | if (htab[i] != NULL) | |
1069 | { | |
1070 | dptr = htab[i]; | |
1071 | while (dptr != NULL) | |
1072 | { | |
1073 | temp = dptr->next; | |
1074 | free (dptr->string); | |
1075 | free (dptr); | |
1076 | dptr = temp; | |
1077 | } | |
1078 | htab[i]=NULL; | |
1079 | } | |
1080 | } | |
1081 | } | |
1082 | ||
1083 | #endif /* USE_DNS */ | |
1084 | ||
1085 | /*********************************************/ | |
1086 | /* FIND_URL - Find URL in hash table */ | |
1087 | /*********************************************/ | |
1088 | ||
1089 | char *find_url(char *str) | |
1090 | { | |
1091 | UNODEPTR cptr; | |
1092 | ||
1093 | if ( (cptr=um_htab[hash(str)]) != NULL) | |
1094 | { | |
1095 | while (cptr != NULL) | |
1096 | { | |
1097 | if (strcmp(cptr->string,str)==0) | |
1098 | return cptr->string; | |
1099 | cptr = cptr->next; | |
1100 | } | |
1101 | } | |
1102 | return blank_str; /* shouldn't get here */ | |
1103 | } | |
1104 | ||
1105 | /*********************************************/ | |
1106 | /* UPDATE_ENTRY - update entry page total */ | |
1107 | /*********************************************/ | |
1108 | ||
1109 | void update_entry(char *str) | |
1110 | { | |
1111 | UNODEPTR uptr; | |
1112 | ||
1113 | if (str==NULL) return; | |
1114 | if ( (uptr = um_htab[hash(str)]) == NULL) return; | |
1115 | else | |
1116 | { | |
1117 | while (uptr != NULL) | |
1118 | { | |
1119 | if (strcmp(uptr->string,str)==0) | |
1120 | { | |
1121 | if (uptr->flag!=OBJ_GRP) | |
1122 | { | |
1123 | uptr->entry++; | |
1124 | return; | |
1125 | } | |
1126 | } | |
1127 | uptr=uptr->next; | |
1128 | } | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | /*********************************************/ | |
1133 | /* UPDATE_EXIT - update exit page total */ | |
1134 | /*********************************************/ | |
1135 | ||
1136 | void update_exit(char *str) | |
1137 | { | |
1138 | UNODEPTR uptr; | |
1139 | ||
1140 | if (str==NULL) return; | |
1141 | if ( (uptr = um_htab[hash(str)]) == NULL) return; | |
1142 | else | |
1143 | { | |
1144 | while (uptr != NULL) | |
1145 | { | |
1146 | if (strcmp(uptr->string,str)==0) | |
1147 | { | |
1148 | if (uptr->flag!=OBJ_GRP) | |
1149 | { | |
1150 | uptr->exit++; | |
1151 | return; | |
1152 | } | |
1153 | } | |
1154 | uptr=uptr->next; | |
1155 | } | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | /*********************************************/ | |
1160 | /* MONTH_UPDATE_EXIT - eom exit page update */ | |
1161 | /*********************************************/ | |
1162 | ||
1163 | void month_update_exit(u_int64_t tstamp) | |
1164 | { | |
1165 | HNODEPTR nptr; | |
1166 | int i; | |
1167 | ||
1168 | for (i=0;i<MAXHASH;i++) | |
1169 | { | |
1170 | nptr=sm_htab[i]; | |
1171 | while (nptr!=NULL) | |
1172 | { | |
1173 | if (nptr->flag!=OBJ_GRP) | |
1174 | { | |
1175 | if ((tstamp-nptr->tstamp)>=visit_timeout) | |
1176 | update_exit(nptr->lasturl); | |
1177 | } | |
1178 | nptr=nptr->next; | |
1179 | } | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | /*********************************************/ | |
1184 | /* TOT_VISIT - calculate total visits */ | |
1185 | /*********************************************/ | |
1186 | ||
1187 | u_int64_t tot_visit(HNODEPTR *list) | |
1188 | { | |
1189 | HNODEPTR hptr; | |
1190 | u_int64_t tot=0; | |
1191 | int i; | |
1192 | ||
1193 | for (i=0;i<MAXHASH;i++) | |
1194 | { | |
1195 | hptr=list[i]; | |
1196 | while (hptr!=NULL) | |
1197 | { | |
1198 | if (hptr->flag!=OBJ_GRP) tot+=hptr->visit; | |
1199 | hptr=hptr->next; | |
1200 | } | |
1201 | } | |
1202 | return tot; | |
1203 | } | |
1204 | ||
1205 | #ifdef USE_OLDHASH | |
1206 | /*********************************************/ | |
1207 | /* HASH - return hash value for string */ | |
1208 | /*********************************************/ | |
1209 | ||
1210 | unsigned int hash(char *str) | |
1211 | { | |
1212 | uint32_t hashval=0; | |
1213 | ||
1214 | for (hashval = 0; *str != '\0'; str++) | |
1215 | hashval = *str + (hashval << 5) - hashval; | |
1216 | ||
1217 | return hashval % MAXHASH; | |
1218 | } | |
1219 | ||
1220 | #else /* USE_OLDHASH */ | |
1221 | /*********************************************/ | |
1222 | /* HASH (SuperFastHash by Paul Hsieh) */ | |
1223 | /*********************************************/ | |
1224 | ||
1225 | #undef get16bits | |
1226 | #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ | |
1227 | || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) | |
1228 | #define get16bits(d) (*((const uint16_t *) (d))) | |
1229 | #endif | |
1230 | ||
1231 | #if !defined (get16bits) | |
1232 | #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ | |
1233 | +(uint32_t)(((const uint8_t *)(d))[0]) ) | |
1234 | #endif | |
1235 | ||
1236 | unsigned int hash(char *str) | |
1237 | { | |
1238 | int len=strlen(str); | |
1239 | uint32_t hash = len, tmp; | |
1240 | int rem; | |
1241 | ||
1242 | if (len <= 0 || str == NULL) return 0; | |
1243 | ||
1244 | rem = len & 3; | |
1245 | len >>= 2; | |
1246 | ||
1247 | /* Main loop */ | |
1248 | for (;len > 0; len--) | |
1249 | { | |
1250 | hash += get16bits (str); | |
1251 | tmp = (get16bits (str+2) << 11) ^ hash; | |
1252 | hash = (hash << 16) ^ tmp; | |
1253 | str += 2*sizeof (uint16_t); | |
1254 | hash += hash >> 11; | |
1255 | } | |
1256 | ||
1257 | /* Handle end cases */ | |
1258 | switch (rem) | |
1259 | { | |
1260 | case 3: hash += get16bits (str); | |
1261 | hash ^= hash << 16; | |
1262 | hash ^= str[sizeof (uint16_t)] << 18; | |
1263 | hash += hash >> 11; | |
1264 | break; | |
1265 | case 2: hash += get16bits (str); | |
1266 | hash ^= hash << 11; | |
1267 | hash += hash >> 17; | |
1268 | break; | |
1269 | case 1: hash += *str; | |
1270 | hash ^= hash << 10; | |
1271 | hash += hash >> 1; | |
1272 | } | |
1273 | ||
1274 | /* Force "avalanching" of final 127 bits */ | |
1275 | hash ^= hash << 3; | |
1276 | hash += hash >> 5; | |
1277 | hash ^= hash << 4; | |
1278 | hash += hash >> 17; | |
1279 | hash ^= hash << 25; | |
1280 | hash += hash >> 6; | |
1281 | ||
1282 | return hash % MAXHASH; | |
1283 | } | |
1284 | #endif /* USE_OLDHASH */ |