trafficlog.c (1665B)
1 /************************************************************************* 2 * This file is part of Crosroads 1.23, a load balancer and fail over 3 * utility for TCP. Copyright (c) Karel Kubat, distributed under GPL. 4 * Visit http://crossroads.e-tunity.com for information. 5 *************************************************************************/ 6 #include "crossroads.h" 7 8 void trafficlog (unsigned char const *buf, int len, CopyDirection dir) { 9 int i, j, n = 0; 10 char disp[17]; 11 FILE *f; 12 13 if (current_backend < 0 || 14 ! activeservice->backend[current_backend].dumpfile) 15 return; 16 17 if ( (! (f = fopen (activeservice->backend[current_backend].dumpfile, 18 "a"))) && 19 (! (f = fopen (activeservice->backend[current_backend].dumpfile, 20 "w"))) ) { 21 warning ("Cannot write %s: %s", 22 activeservice->backend[current_backend].dumpfile, 23 strerror(errno)); 24 return; 25 } 26 27 #if defined HAVE_FLOCK 28 flock (fileno(f), LOCK_EX); 29 #elif defined HAVE_LOCKF 30 lockf (fileno(f), F_LOCK, 0); 31 #endif 32 33 for (i = 0; i < len; i++) { 34 if (! n) 35 fprintf (f, "%4.4d %c %4.4x ", getpid(), 36 dir == dir_client_to_server ? 'C' : 'B', i); 37 fprintf (f, " %2.2x", buf[i]); 38 disp[n] = isprint(buf[i]) ? buf[i] : '.'; 39 40 n++; 41 42 if (n == 16) { 43 fputc (' ', f); 44 for (j = 0; j < n; j++) 45 fputc (disp[j], f); 46 fputc ('\n', f); 47 n = 0; 48 } 49 } 50 51 for (j = n; j < 16; j++) 52 fprintf (f, " "); 53 fputc (' ', f); 54 for (j = 0; j < n; j++) 55 fputc (disp[j], f); 56 fputc ('\n', f); 57 58 #if defined HAVE_FLOCK 59 flock (fileno(f), LOCK_UN); 60 #elif defined HAVE_LOCKF 61 lockf (fileno(f), F_ULOCK, 0); 62 #endif 63 64 fclose (f); 65 }