dissector: simplified code in dissector, removed duplicate code
[netsniff-ng.git] / src / proto_vlan.h
blob56196bd6d087fa62b8d608cbe510f3f33ee31b17
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
7 */
9 #ifndef VLAN_H
10 #define VLAN_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"
19 struct vlanhdr {
20 uint16_t h_vlan_TCI;
21 uint16_t h_vlan_encapsulated_proto;
22 } __attribute__((packed));
24 static inline void vlan(uint8_t *packet, size_t len)
26 uint16_t tci;
27 struct vlanhdr *vlan = (struct vlanhdr *) packet;
29 if (len < sizeof(struct vlanhdr))
30 return;
32 tci = ntohs(vlan->h_vlan_TCI);
34 tprintf(" [ VLAN ");
35 tprintf("Prio (%d), ", (tci & 0xE000) >> 13);
36 tprintf("CFI (%d), ", (tci & 0x1000) >> 12);
37 tprintf("ID (%d), ", (tci & 0x0FFF));
38 tprintf("Proto (0x%.4x)", ntohs(vlan->h_vlan_encapsulated_proto));
39 tprintf(" ]\n");
42 static inline void vlan_less(uint8_t *packet, size_t len)
44 uint16_t tci;
45 struct vlanhdr *vlan = (struct vlanhdr *) packet;
47 if (len < sizeof(struct vlanhdr))
48 return;
50 tci = ntohs(vlan->h_vlan_TCI);
52 tprintf(" VLAN%d", (tci & 0x0FFF));
55 static inline void vlan_next(uint8_t *packet, size_t len,
56 struct hash_table **table,
57 unsigned int *key, size_t *off)
59 struct vlanhdr *vlan = (struct vlanhdr *) packet;
61 if (len < sizeof(struct vlanhdr))
62 return;
64 (*off) = sizeof(struct vlanhdr);
65 (*key) = ntohs(vlan->h_vlan_encapsulated_proto);
66 (*table) = &eth_lay2;
69 struct protocol vlan_ops = {
70 .key = 0x8100,
71 .offset = sizeof(struct vlanhdr),
72 .print_full = vlan,
73 .print_less = vlan_less,
74 .print_pay_ascii = empty,
75 .print_pay_hex = empty,
76 .print_pay_none = vlan,
77 .print_all_hex = hex,
78 .proto_next = vlan_next,
81 #endif /* VLAN_H */