forked from xdp-project/xdp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace_read.c
71 lines (55 loc) · 1.32 KB
/
trace_read.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
/* SPDX-License-Identifier: GPL-2.0 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../common/common_defines.h"
#include <netinet/ether.h>
#define TRACEFS_PIPE "/sys/kernel/debug/tracing/trace_pipe"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
static void print_ether_addr(const char *type, char *str)
{
__u64 addr;
if (1 != sscanf(str, "%llu", &addr))
return;
printf("%s: %s ", type, ether_ntoa((struct ether_addr *) &addr));
}
int main(int argc, char **argv)
{
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
stream = fopen(TRACEFS_PIPE, "r");
if (stream == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
while ((nread = getline(&line, &len, stream)) != -1) {
char *tok, *saveptr;
unsigned int proto;
tok = strtok_r(line, " ", &saveptr);
while (tok) {
if (!strncmp(tok, "src:", 4)) {
tok = strtok_r(NULL, " ", &saveptr);
print_ether_addr("src", tok);
}
if (!strncmp(tok, "dst:", 4)) {
tok = strtok_r(NULL, " ", &saveptr);
print_ether_addr("dst", tok);
}
if (!strncmp(tok, "proto:", 5)) {
tok = strtok_r(NULL, " ", &saveptr);
if (1 == sscanf(tok, "%u", &proto))
printf("proto: %u", proto);
}
tok = strtok_r(NULL, " ", &saveptr);
}
printf("\n");
}
free(line);
fclose(stream);
return EXIT_OK;
}