-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathprint-hooklog.c
165 lines (136 loc) · 3.13 KB
/
print-hooklog.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ctype.h>
struct record {
size_t recsz;
uint8_t type;
socklen_t saddrlen;
struct sockaddr saddr;
socklen_t paddrlen;
struct sockaddr paddr;
int num;
char *buf;
};
#define RTYPE_READ 0
#define RTYPE_WRITE 1
// returns -1 on error, 0 on eof, 1 on read record
static int record_read(struct record *r, FILE *in) {
if (fread(&r->recsz, sizeof(r->recsz), 1, in) != 1) {
if (feof(in)) {
return 0;
} else {
return -1;
}
}
if (fread(&r->type, sizeof(r->type), 1, in) != 1) {
return -1;
}
if (fread(&r->saddrlen, sizeof(r->saddrlen), 1, in) != 1) {
return -1;
}
if (fread(&r->saddr, r->saddrlen, 1, in) != 1) {
return -1;
}
if (fread(&r->paddrlen, sizeof(r->paddrlen), 1, in) != 1) {
return -1;
}
if (fread(&r->paddr, r->paddrlen, 1, in) != 1) {
return -1;
}
if (fread(&r->num, sizeof(r->num), 1, in) != 1) {
return -1;
}
if (r->num <= 0) {
r->buf = NULL;
} else {
r->buf = malloc(r->num);
if (r->buf == NULL) {
return -1;
}
if (fread(r->buf, r->num, 1, in) != 1) {
return -1;
}
}
return 1;
}
static void record_clean(struct record *r) {
if (r->buf != NULL) {
free(r->buf);
r->buf = NULL;
}
}
#define ISPRINT(c) \
(isascii((c)) && (c) != '\r' && (c) != '\n')
static void record_print(struct record *r, FILE *out) {
char addr[128], port[32], peeraddr[128], peerport[32];
int i, j, nrowbytes;
if ((getnameinfo(&r->saddr, r->saddrlen, addr, sizeof(addr),
port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV) < 0) ||
(getnameinfo(&r->paddr, r->paddrlen, peeraddr, sizeof(peeraddr),
peerport, sizeof(peerport), NI_NUMERICHOST|NI_NUMERICSERV) < 0)) {
fprintf(out, "<invalid addr record>\n\n");
return;
}
if (r->type == RTYPE_READ) {
fprintf(out, "read - %s %s -> %s %s\n", peeraddr, peerport, addr, port);
} else if (r->type == RTYPE_WRITE) {
fprintf(out, "write - %s %s -> %s %s\n", addr, port, peeraddr, peerport);
} else {
fprintf(out, "unknown record type (%hhu) local: %s:%s peer: %s:%s\n",
r->type, addr, port, peeraddr, peerport);
}
i=0;
while (i < r->num) {
fprintf(out, "%08x ", i);
nrowbytes = r->num-i;
if (nrowbytes > 16) {
nrowbytes = 16;
}
for(j=0; j<nrowbytes; j++) {
fprintf(out, "%02x ", (int)r->buf[i+j]);
if (j == 7) {
fprintf(out, " ");
}
}
for(j=nrowbytes; j < 16; j++) {
fprintf(out, " ");
}
fprintf(out, " |");
for(j=0; j < nrowbytes; j++) {
fprintf(out, "%c", ISPRINT(r->buf[i+j]) ? r->buf[i+j] : '.');
}
fprintf(out, "|\n");
i+=nrowbytes;
}
fprintf(out, "%08x\n\n", r->num);
}
int main(int argc, char *argv[]) {
FILE *fp;
struct record r;
int ret;
if (argc != 2) {
fprintf(stderr, "print-hooklog <dump-file>\n");
return EXIT_FAILURE;
}
fp = fopen(argv[1], "rb");
if (fp == NULL) {
perror(argv[1]);
return EXIT_FAILURE;
}
while ((ret = record_read(&r, fp)) > 0) {
record_print(&r, stdout);
record_clean(&r);
}
if (ret < 0) {
perror("record_read");
fclose(fp);
exit(EXIT_FAILURE);
}
fclose(fp);
return EXIT_SUCCESS;
}