-
Notifications
You must be signed in to change notification settings - Fork 63
/
tuntap_log.c
150 lines (132 loc) · 3.57 KB
/
tuntap_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
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
/**
* Copyright (c) 2012, PICHOT Fabien Paul Leonard <pichot.fabienATgmail.com>
* Copyright (c) 2012, Tristan Le Guern <tleguern@bouledef.eu>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
**/
#if defined Windows
# include <windows.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include "tuntap.h"
#include "private.h"
t_tuntap_log tuntap_log = NULL;
void
tuntap_log_set_cb(t_tuntap_log cb) {
if (cb == NULL)
tuntap_log = tuntap_log_default;
tuntap_log = cb;
}
void
tuntap_log_default(int level, const char *errmsg) {
char *name;
switch(level) {
case TUNTAP_LOG_DEBUG:
name = "Debug";
break;
case TUNTAP_LOG_INFO:
name = "Info";
break;
case TUNTAP_LOG_NOTICE:
name = "Notice";
break;
case TUNTAP_LOG_WARN:
name = "Warning";
break;
case TUNTAP_LOG_ERR:
name = "Error";
break;
case TUNTAP_LOG_NONE:
default:
name = NULL;
break;
}
if (name == NULL) {
(void)fprintf(stderr, "%s\n", errmsg);
} else {
(void)fprintf(stderr, "%s: %s\n", name, errmsg);
}
}
void
tuntap_log_hexdump(void *data, size_t size) {
unsigned char *p = (unsigned char *)data;
unsigned int c;
size_t n;
char bytestr[4] = {0};
char addrstr[10] = {0};
char hexstr[16 * 3 + 5] = {0};
char charstr[16 * 1 + 5] = {0};
char buf[1024];
for (n = 1; n <= size; n++) {
if (n % 16 == 1) {
/* store address for this line */
snprintf(addrstr, sizeof(addrstr), "%.4zx",
(p - (unsigned char *)data) );
}
c = *p;
if (isalnum(c) == 0) {
c = '.';
}
/* store hex str (for left side) */
snprintf(bytestr, sizeof(bytestr), "%02X ", *p);
strncat(hexstr, bytestr, sizeof(hexstr)-strlen(hexstr)-1);
/* store char str (for right side) */
snprintf(bytestr, sizeof(bytestr), "%c", c);
strncat(charstr, bytestr, sizeof(charstr)-strlen(charstr)-1);
if (n % 16 == 0) {
/* line completed */
(void)memset(buf, 0, sizeof buf);
(void)snprintf(buf, sizeof buf,
"[%4.4s] %-50.50s %s", addrstr, hexstr, charstr);
tuntap_log(TUNTAP_LOG_NONE, buf);
hexstr[0] = 0;
charstr[0] = 0;
} else if (n % 8 == 0) {
/* half line: add whitespaces */
strncat(hexstr, " ", sizeof(hexstr)-strlen(hexstr)-1);
strncat(charstr, " ", sizeof(charstr)-strlen(charstr)-1);
}
p++; /* next byte */
}
/* print the rest of the buffer if not empty */
if (strlen(hexstr) > 0) {
(void)memset(buf, 0, sizeof buf);
(void)snprintf(buf, sizeof buf, "[%4.4s] %-50.50s %s",
addrstr, hexstr, charstr);
tuntap_log(TUNTAP_LOG_NONE, buf);
}
}
void
tuntap_log_chksum(void *addr, int count) {
int sum;
short *sptr;
char buf[32];
sum = 0;
sptr = (short *)addr;
while (count > 1)
{
sum = sum + *sptr;
count = count - 2;
sptr++;
}
addr = (char *)sptr;
if (count > 0)
sum = sum + *((char *) addr);
sum = ~sum;
(void)memset(buf, 0, sizeof buf);
(void)snprintf(buf, sizeof buf, "Checksum of this block: %0#4x", sum);
tuntap_log(TUNTAP_LOG_NONE, buf);
}