source: examples/tutorial/gettcpdemo.c @ e78e408

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since e78e408 was 8835f5a, checked in by Shane Alcock <salcock@…>, 11 years ago
  • Added the code examples from the PDCAT tutorial to the bundled examples
  • Property mode set to 100644
File size: 2.5 KB
Line 
1/* Trivial libtrace program that counts the number of HTTP packets in a trace.
2 * Designed to demonstrate the use of trace_get_tcp()
3 */
4#include "libtrace.h"
5#include <stdio.h>
6#include <inttypes.h>
7#include <assert.h>
8#include <getopt.h>
9
10uint64_t count = 0;
11
12static void per_packet(libtrace_packet_t *packet)
13{
14        libtrace_tcp_t *tcp;
15
16        /* Get the TCP header using trace_get_tcp() */
17        tcp = trace_get_tcp(packet);
18       
19        /* If the packet does not have a TCP header, skip it */
20        if (tcp == NULL)
21                return;
22
23        /* Check if either port in the TCP header is 80. Note that we
24         * have to byteswap the port numbers because we are reading directly
25         * out of the zero-copied packet */
26        if ((ntohs(tcp->source) == 80) || (ntohs(tcp->dest) == 80))
27                count += 1;
28}
29
30static void libtrace_cleanup(libtrace_t *trace, libtrace_packet_t *packet) {
31
32        /* It's very important to ensure that we aren't trying to destroy
33         * a NULL structure, so each of the destroy calls will only occur
34         * if the structure exists */
35        if (trace)
36                trace_destroy(trace);
37
38        if (packet)
39                trace_destroy_packet(packet);
40
41}
42
43int main(int argc, char *argv[])
44{
45        /* This is essentially the same main function from readdemo.c */
46
47        libtrace_t *trace = NULL;
48        libtrace_packet_t *packet = NULL;
49
50        /* Ensure we have at least one argument after the program name */
51        if (argc < 2) {
52                fprintf(stderr, "Usage: %s inputURI\n", argv[0]);
53                return 1;
54        }
55
56        packet = trace_create_packet();
57
58        if (packet == NULL) {
59                perror("Creating libtrace packet");
60                libtrace_cleanup(trace, packet);
61                return 1;
62        }
63
64        trace = trace_create(argv[1]);
65
66        if (trace_is_err(trace)) {
67                trace_perror(trace,"Opening trace file");
68                libtrace_cleanup(trace, packet);
69                return 1;
70        }
71
72        if (trace_start(trace) == -1) {
73                trace_perror(trace,"Starting trace");
74                libtrace_cleanup(trace, packet);
75                return 1;
76        }
77
78
79        while (trace_read_packet(trace,packet)>0) {
80                per_packet(packet);
81        }
82
83
84        if (trace_is_err(trace)) {
85                trace_perror(trace,"Reading packets");
86                libtrace_cleanup(trace, packet);
87                return 1;
88        }
89
90        printf("Packet Count = %" PRIu64 "\n", count);
91
92        libtrace_cleanup(trace, packet);
93        return 0;
94}
95
Note: See TracBrowser for help on using the repository browser.