source: examples/sample/sample.c @ a79ddbe

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since a79ddbe was a79ddbe, checked in by Daniel Lawson <dlawson@…>, 17 years ago

added license information

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2004 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
31//
32// This program takes a trace and outputs every packet that it sees to standard
33// out, decoding source/dest IP's, protocol type, and the timestamp of this
34// packet.
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <assert.h>
39#include <string.h>
40#include <sys/time.h>
41#include <time.h>
42
43#include <netinet/in.h>
44#include <netinet/tcp.h>
45#include <netinet/ip.h>
46#include <netinet/ip_icmp.h>
47#include <arpa/inet.h>
48#include <sys/socket.h>
49#include "dagformat.h"
50#include "libtrace.h"
51
52struct libtrace_t *trace;
53
54#define SCANSIZE 4096
55
56char *buffer[SCANSIZE];
57
58int main(int argc, char *argv[]) {
59
60        char *uri = "rtclient:chasm.cs.waikato.ac.nz";
61        int psize = 0;
62        int status = 0;
63        struct libtrace_ip *ipptr = 0;
64
65        if (argc == 2) {
66                uri = strdup(argv[1]);
67        }
68
69        // open a trace
70        trace = create_trace(uri);
71
72        for (;;) {
73                unsigned char *x;
74                int i;
75                if ((psize = libtrace_read_packet(trace, buffer, SCANSIZE, &status)) <1) {
76                        break;
77                }
78
79                printf("TS %f: ",get_seconds(trace,buffer,SCANSIZE));
80
81                ipptr = get_ip(trace,buffer,SCANSIZE);
82                if (!ipptr) {
83                        printf("Non IP\n");
84                        continue;
85                }
86
87                printf("%s -> ",inet_ntoa(ipptr->ip_src));
88                printf("%s protocol %02x\n",
89                                        inet_ntoa(ipptr->ip_dst),
90                                        ipptr->ip_p);
91                x=(void*)ipptr;
92                for(i=0;i<get_capture_length(trace,buffer,SCANSIZE);i++) {
93                        if (i%4==0 && i!=0)
94                                printf("\n");
95                        printf("%02x ",x[i]);
96                }
97                printf("\n\n");
98        }
99
100        destroy_trace(trace);
101        return 0;
102}
Note: See TracBrowser for help on using the repository browser.