trafgen: remove pointer fiddling, improve code
[netsniff-ng-old.git] / src / proto_ipv6_hop_by_hop.h
blob240aac7f03a19faadf2413fac9e9a0d4fc6e2996
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2012 Markus Amend <markus@netsniff-ng.org>
4 * Subject to the GPL, version 2.
6 * IPv6 Hop-By-Hop Header described in RFC2460
7 */
9 #ifndef PROTO_IPV6_HOP_BY_HOP_H
10 #define PROTO_IPV6_HOP_BY_HOP_H
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <netinet/in.h> /* for ntohs() */
16 #include "proto_struct.h"
17 #include "dissector_eth.h"
18 #include "built_in.h"
20 struct hop_by_hophdr {
21 uint8_t h_next_header;
22 uint8_t hdr_len;
23 } __packed;
25 static inline void dissect_opt_hop (struct pkt_buff *pkt, size_t *opt_len)
27 /* Have to been upgraded.
28 * http://tools.ietf.org/html/rfc2460#section-4.2
29 * Look also for proto_ipv6_dest_opts.h, it needs
30 * dissect_opt(), too.
32 if (*opt_len)
33 tprintf(", Option(s) recognized ");
35 /* If adding dissector reduce opt_len for each using of pkt_pull
36 * to the same size.
40 static inline void hop_by_hop(struct pkt_buff *pkt)
42 uint16_t hdr_ext_len;
43 size_t opt_len;
44 struct hop_by_hophdr *hop_ops;
46 hop_ops = (struct hop_by_hophdr *) pkt_pull(pkt, sizeof(*hop_ops));
48 /* Total Header Length in Bytes */
49 hdr_ext_len = (hop_ops->hdr_len + 1) * 8;
50 /* Options length in Bytes */
51 opt_len = hdr_ext_len - sizeof(*hop_ops);
52 if (hop_ops == NULL || opt_len > pkt_len(pkt))
53 return;
55 tprintf("\t [ Hop-by-Hop Options ");
56 tprintf("NextHdr (%u), ", hop_ops->h_next_header);
57 tprintf("HdrExtLen (%u, %u Bytes)", hop_ops->hdr_len,
58 hdr_ext_len);
60 dissect_opt_hop(pkt, &opt_len);
62 tprintf(" ]\n");
64 pkt_pull(pkt, opt_len);
65 pkt_set_proto(pkt, &eth_lay3, hop_ops->h_next_header);
68 static inline void hop_by_hop_less(struct pkt_buff *pkt)
70 uint16_t hdr_ext_len;
71 size_t opt_len;
72 struct hop_by_hophdr *hop_ops;
74 hop_ops = (struct hop_by_hophdr *) pkt_pull(pkt, sizeof(*hop_ops));
76 /* Total Header Length in Bytes */
77 hdr_ext_len = (hop_ops->hdr_len + 1) * 8;
78 /* Options length in Bytes */
79 opt_len = hdr_ext_len - sizeof(*hop_ops);
80 if (hop_ops == NULL || opt_len > pkt_len(pkt))
81 return;
83 tprintf(" Hop Ops");
85 pkt_pull(pkt, opt_len);
86 pkt_set_proto(pkt, &eth_lay3, hop_ops->h_next_header);
89 struct protocol ipv6_hop_by_hop_ops = {
90 .key = 0x0,
91 .print_full = hop_by_hop,
92 .print_less = hop_by_hop_less,
95 #endif /* PROTO_IPV6_HOP_BY_HOP_H */