source: test/test-ports.c @ 17f954f

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragofflibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 17f954f was 4cc8499, checked in by Shane Alcock <salcock@…>, 7 years ago

Add test for port access functions

Tests whether trace_get_source_port and trace_get_destination_port
work correctly.

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand.
5 * Authors: Daniel Lawson
6 *          Perry Lorier
7 *         
8 * All rights reserved.
9 *
10 * This code has been developed by the University of Waikato WAND
11 * research group. For further information please see http://www.wand.net.nz/
12 *
13 * libtrace is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * libtrace is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with libtrace; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 *
27 * $Id$
28 *
29 */
30#ifndef WIN32
31#  include <sys/time.h>
32#  include <netinet/in.h>
33#  include <netinet/in_systm.h>
34#  include <netinet/tcp.h>
35#  include <netinet/ip.h>
36#  include <netinet/ip_icmp.h>
37#  include <arpa/inet.h>
38#  include <sys/socket.h>
39#endif
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <assert.h>
44#include <string.h>
45#include <sys/types.h>
46#include <time.h>
47#include <string.h>
48
49#include "libtrace.h"
50
51struct libtrace_t *trace;
52
53void iferr(libtrace_t *trace)
54{
55        libtrace_err_t err = trace_get_err(trace);
56        if (err.err_num==0)
57                return;
58        printf("Error: %s\n",err.problem);
59        exit(1);
60}
61
62int main(int argc, char *argv[]) {
63        char *uri = "pcap:traces/100_packets.pcap";
64        int error = 0;
65        int srccount = 0;
66        int dstcount = 0;
67        int count = 0;
68        int psize = 0;
69        struct libtrace_packet_t *packet;
70
71        trace = trace_create(uri);
72        iferr(trace);
73
74        if (trace_start(trace)==-1) {
75                iferr(trace);
76        }
77       
78        packet=trace_create_packet();
79        for (;;) {
80                if ((psize = trace_read_packet(trace, packet)) <=0) {
81                        if (psize != 0) error = 1;
82                        break;
83                }
84                if (psize == 0) {
85                        error = 0;
86                        break;
87                }
88       
89                if (trace_get_source_port(packet) == 80) {
90                        srccount ++;
91                }
92                if (trace_get_destination_port(packet) == 53) {
93                        dstcount ++;
94                }
95               
96                count ++;
97        }
98        trace_destroy_packet(packet);
99        if (error == 0) {
100                if (count == 100) {
101                        printf("success: 100 packets read\n");
102                } else {
103                        printf("fail: 100 packets expected, %d seen\n",count);
104                        error = 1;
105                }
106
107                if (srccount == 27) {
108                        printf("success: 27 src port 80 packets observed\n");
109                } else {
110                        printf("fail: 27 src port 80 packets expected, %u seen\n", srccount);
111                        error = 1;
112                }
113
114                if (dstcount == 5) {
115                        printf("success: 5 dst port 53 packets observed\n");
116                } else {
117                        printf("fail: 5 dst port 53 packets expected, %u seen\n", dstcount);
118                        error = 1;
119                }
120
121        } else {
122                iferr(trace);
123        }
124        trace_destroy(trace);
125        return error;
126}
Note: See TracBrowser for help on using the repository browser.