1 | /* Trivial libtrace program that counts the number of HTTP packets in a trace. |
---|
2 | * Designed to demonstrate the use of trace_get_transport() |
---|
3 | */ |
---|
4 | #include "libtrace.h" |
---|
5 | #include <stdio.h> |
---|
6 | #include <inttypes.h> |
---|
7 | #include <assert.h> |
---|
8 | #include <getopt.h> |
---|
9 | |
---|
10 | uint64_t count = 0; |
---|
11 | |
---|
12 | |
---|
13 | static void per_packet(libtrace_packet_t *packet) |
---|
14 | { |
---|
15 | void *transport; |
---|
16 | uint8_t proto; |
---|
17 | uint32_t remaining; |
---|
18 | libtrace_tcp_t *tcp; |
---|
19 | |
---|
20 | /* Remember that both proto and remaining will be set for us by |
---|
21 | * this function, so we need to pass in pointers to pre-existing |
---|
22 | * variables. Any values in those variables will be ignored and |
---|
23 | * replaced by trace_get_transport |
---|
24 | */ |
---|
25 | transport = trace_get_transport(packet, &proto, &remaining); |
---|
26 | |
---|
27 | /* If the packet does not have a transport header, skip it */ |
---|
28 | if (transport == NULL) |
---|
29 | return; |
---|
30 | |
---|
31 | /* If the transport header is not TCP, skip it */ |
---|
32 | if (proto != 6) |
---|
33 | return; |
---|
34 | |
---|
35 | /* Cast the transport header into a TCP header */ |
---|
36 | tcp = (libtrace_tcp_t *)transport; |
---|
37 | |
---|
38 | /* Check if either port in the TCP header is 80. Note that we have |
---|
39 | * to byteswap the port numbers because we are reading directly out |
---|
40 | * of the zero-copied packet */ |
---|
41 | if ((ntohs(tcp->source) == 80) || (ntohs(tcp->dest) == 80)) |
---|
42 | count += 1; |
---|
43 | } |
---|
44 | |
---|
45 | static void libtrace_cleanup(libtrace_t *trace, libtrace_packet_t *packet) { |
---|
46 | |
---|
47 | /* It's very important to ensure that we aren't trying to destroy |
---|
48 | * a NULL structure, so each of the destroy calls will only occur |
---|
49 | * if the structure exists */ |
---|
50 | if (trace) |
---|
51 | trace_destroy(trace); |
---|
52 | |
---|
53 | if (packet) |
---|
54 | trace_destroy_packet(packet); |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | int main(int argc, char *argv[]) |
---|
59 | { |
---|
60 | /* This is essentially the same main function from readdemo.c */ |
---|
61 | |
---|
62 | libtrace_t *trace = NULL; |
---|
63 | libtrace_packet_t *packet = NULL; |
---|
64 | |
---|
65 | /* Ensure we have at least one argument after the program name */ |
---|
66 | if (argc < 2) { |
---|
67 | fprintf(stderr, "Usage: %s inputURI\n", argv[0]); |
---|
68 | return 1; |
---|
69 | } |
---|
70 | |
---|
71 | packet = trace_create_packet(); |
---|
72 | |
---|
73 | if (packet == NULL) { |
---|
74 | perror("Creating libtrace packet"); |
---|
75 | libtrace_cleanup(trace, packet); |
---|
76 | return 1; |
---|
77 | } |
---|
78 | |
---|
79 | trace = trace_create(argv[1]); |
---|
80 | |
---|
81 | if (trace_is_err(trace)) { |
---|
82 | trace_perror(trace,"Opening trace file"); |
---|
83 | libtrace_cleanup(trace, packet); |
---|
84 | return 1; |
---|
85 | } |
---|
86 | |
---|
87 | if (trace_start(trace) == -1) { |
---|
88 | trace_perror(trace,"Starting trace"); |
---|
89 | libtrace_cleanup(trace, packet); |
---|
90 | return 1; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | while (trace_read_packet(trace,packet)>0) { |
---|
95 | per_packet(packet); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | if (trace_is_err(trace)) { |
---|
100 | trace_perror(trace,"Reading packets"); |
---|
101 | libtrace_cleanup(trace, packet); |
---|
102 | return 1; |
---|
103 | } |
---|
104 | |
---|
105 | printf("Packet Count = %" PRIu64 "\n", count); |
---|
106 | |
---|
107 | libtrace_cleanup(trace, packet); |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|