-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_log.c
executable file
·111 lines (90 loc) · 2.98 KB
/
io_log.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
#include <stdarg.h> //va_list, va_start, ...
#include <stdio.h>
#include <string.h>
#include "io_log.h"
#include "utils_net.h"
#include "util.h" //LOG_{} constants
#include "main.h"//conf.debug application specific
static FILE* DF = NULL;
void __attribute__ ((format (printf, 2, 3))) printlog_(const char *appender, const char *fmt, ...){
char buf[128];
va_list args;
va_start(args, fmt);
vsnprintf(buf, 128, fmt, args);
va_end(args);
if(strcmp(appender, "")==0){
fprintf(stderr, "%s", buf);
}else if( strcmp(appender, "[DBG]")==0 ){
if(conf.debug)
fprintf(stderr, "%s", buf);
}else if ( strcmp(appender, "DBG")==0 ){
if(conf.debug)
fprintf(stderr, "[%s]:%s",appender, buf);
}else
fprintf(stderr, "[%s]:%s",appender, buf);
}
void __attribute__ ((format (printf, 2, 3))) printlog(int level, const char *fmt, ...){
char buf[128];
va_list args;
va_start(args, fmt);
vsnprintf(buf, 128, fmt, args);
va_end(args);
char appender[4];
switch(level){
case LOG_DEBUG: strncpy(appender,"DBG",4); break;
case LOG_INFO: strncpy(appender,"INF",4); break;
case LOG_WARNING: strncpy(appender,"WRN",4); break;
case LOG_ERR: strncpy(appender,"ERR",4); break;
case LOG_CRIT: strncpy(appender,"CRT",4); break;
}
if(level <= LOG_LEVEL)
fprintf(stderr, "[%s]:%s",appender, buf);
}
void dumpfile_open(const char* name){
if (DF != NULL) {
fclose(DF);
DF = NULL;
}
if (name == NULL || strlen(name) == 0) {
printinf("- Not writing outfile");
conf.dumpfile[0] = '\0';
return;
}
strncpy(conf.dumpfile, name, MAX_CONF_VALUE_STRLEN);
conf.dumpfile[MAX_CONF_VALUE_STRLEN] = '\0';
DF = fopen(conf.dumpfile, "w");
if (DF == NULL)
printwrn("Couldn't open dump file %s", name);
fprintf(DF, "TIME, WLAN TYPE, MAC SRC, MAC DST, BSSID, PACKET TYPES, SIGNAL, ");
fprintf(DF, "LENGTH, PHY RATE, FREQUENCY, TSF, ESSID, MODE, CHANNEL, ");
fprintf(DF, "WEP, WPA1, RSN (WPA2), IP SRC, IP DST\n");
printinf("- Writing to outfile %s", conf.dumpfile);
}
void dumpfile_close(){
if (DF != NULL) {
fclose(DF);
DF = NULL;
}
}
void dumpfile_write(struct uwifi_packet* p){
char buf[40];
int i;
struct tm* ltm = localtime(&the_time.tv_sec);
//timestamp, e.g. 2015-05-16 15:05:44.338806 +0300
i = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ltm);
i += snprintf(buf + i, sizeof(buf) - i, ".%06ld", (long)(the_time.tv_nsec / 1000));
i += strftime(buf + i, sizeof(buf) - i, " %z", ltm);
fprintf(DF, "%s, ", buf);
fprintf(DF, "%s, %s, ",
get_packet_type_name(p->wlan_type), ether_sprintf(p->wlan_src));
fprintf(DF, "%s, ", ether_sprintf(p->wlan_dst));
fprintf(DF, "%s, ", ether_sprintf(p->wlan_bssid));
fprintf(DF, "%x, %d, %d, %d, %d, ",
p->pkt_types, p->phy_signal, p->wlan_len, p->phy_rate, p->phy_freq);
fprintf(DF, "%016llx, ", (unsigned long long)p->wlan_tsf);
fprintf(DF, "%s, %d, %d, %d, %d, %d, ",
p->wlan_essid, p->wlan_mode, p->wlan_channel,
p->wlan_wep, p->wlan_wpa, p->wlan_rsn);
fprintf(DF, "%s, %s\n", ip_sprintf(p->ip_src), ip_sprintf(p->ip_dst));
fflush(DF);
}