source: libpacketdump/eth_34525.c

cachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformance
Last change on this file was f398c61, checked in by Shane Alcock <salcock@…>, 4 years ago

Libpacketdump now recognises truncated IPv6 headers

This should fix #48, as well as avoid displaying bogus IPv6
header fields when the captured packet is too short to actually
contain useful values for them.

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 *
3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4 * All rights reserved.
5 *
6 * This file is part of libtrace.
7 *
8 * This code has been developed by the University of Waikato WAND
9 * research group. For further information please see http://www.wand.net.nz/
10 *
11 * libtrace is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * libtrace is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 *
25 */
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <stdio.h>
30#include "libpacketdump.h"
31#ifndef WIN32
32        #include <netinet/in_systm.h>
33#endif
34#include <arpa/inet.h>
35#include <netdb.h>
36
37DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
38{
39        libtrace_ip6_t *ip = (libtrace_ip6_t*)packet;
40        uint32_t tmp = ntohl(*(uint32_t*)ip);
41
42        int truncated = 0;
43        char ipstr[INET6_ADDRSTRLEN];
44
45        do {
46                if (len < 4) {
47                        truncated = 1;
48                        break;
49                }
50
51                printf(" IPv6: Version %u\n", (tmp >> 28) & 0x000000f);
52                printf(" IPv6: Class %u\n", (tmp >> 20) & 0x000000ff);
53                printf(" IPv6: Flow Label %u\n", tmp & 0x000fffff);
54
55                if (len < 6) {
56                        truncated = 1;
57                        break;
58                }
59                printf(" IPv6: Payload Length %u\n", ntohs(ip->plen));
60
61                if (len < 7) {
62                        truncated = 1;
63                        break;
64                }
65                printf(" IPv6: Next Header %u\n", ip->nxt);
66                if (len < 8) {
67                        truncated = 1;
68                        break;
69                }
70                printf(" IPv6: Hop Limit %u\n", ip->hlim);
71
72                if (len < 24) {
73                        truncated = 1;
74                        break;
75                }
76
77                inet_ntop(AF_INET6, &(ip->ip_src), ipstr, INET6_ADDRSTRLEN);
78                printf(" IPv6: Source IP %s\n", ipstr);
79
80                if (len < 40) {
81                        truncated = 1;
82                        break;
83                }
84
85                inet_ntop(AF_INET6, &(ip->ip_dst), ipstr, INET6_ADDRSTRLEN);
86                printf(" IPv6: Destination IP %s\n", ipstr);
87        } while (0);
88
89        if (truncated) {
90                printf(" IPv6: [Truncated]\n");
91                return;
92        }
93
94        decode_next(packet+sizeof(libtrace_ip6_t),len-sizeof(libtrace_ip6_t),"ip",ip->nxt);
95        return;
96}
Note: See TracBrowser for help on using the repository browser.