dissector: simplified code in dissector, removed duplicate code
[netsniff-ng.git] / src / proto_ethernet.h
blob42b81a90da9933b7422ccee11c95f3e278c1bfdc
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #ifndef ETHERNET_H
9 #define ETHERNET_H
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <netinet/in.h>
14 #include <linux/if_ether.h>
16 #include "proto_struct.h"
17 #include "dissector_eth.h"
19 static inline void ethernet(uint8_t *packet, size_t len)
21 uint8_t *src_mac, *dst_mac;
22 struct ethhdr *eth = (struct ethhdr *) packet;
24 if (len < sizeof(struct ethhdr))
25 return;
27 src_mac = eth->h_source;
28 dst_mac = eth->h_dest;
29 tprintf(" [ Eth ");
30 tprintf("MAC (%.2x:%.2x:%.2x:%.2x:%.2x:%.2x => ",
31 src_mac[0], src_mac[1], src_mac[2],
32 src_mac[3], src_mac[4], src_mac[5]);
33 tprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x), ",
34 dst_mac[0], dst_mac[1], dst_mac[2],
35 dst_mac[3], dst_mac[4], dst_mac[5]);
36 tprintf("Proto (0x%.4x, %s%s%s)",
37 ntohs(eth->h_proto), colorize_start(bold),
38 lookup_ether_type(ntohs(eth->h_proto)), colorize_end());
39 tprintf(" ]\n");
41 tprintf(" [ Vendor ");
42 tprintf("(%s => %s)",
43 lookup_vendor((src_mac[0] << 16) | (src_mac[1] << 8) |
44 src_mac[2]),
45 lookup_vendor((dst_mac[0] << 16) | (dst_mac[1] << 8) |
46 dst_mac[2]));
47 tprintf(" ]\n");
50 static inline void ethernet_hex_all(uint8_t *packet, size_t len)
52 tprintf(" ");
53 hex(packet, len);
56 static inline void ethernet_less(uint8_t *packet, size_t len)
58 uint8_t *src_mac, *dst_mac;
59 struct ethhdr *eth = (struct ethhdr *) packet;
61 if (len < sizeof(struct ethhdr))
62 return;
63 src_mac = eth->h_source;
64 dst_mac = eth->h_dest;
65 tprintf(" %s => %s ",
66 lookup_vendor((src_mac[0] << 16) | (src_mac[1] << 8) |
67 src_mac[2]),
68 lookup_vendor((dst_mac[0] << 16) | (dst_mac[1] << 8) |
69 dst_mac[2]));
70 tprintf("%s%s%s", colorize_start(bold),
71 lookup_ether_type(ntohs(eth->h_proto)), colorize_end());
74 static inline void ethernet_next(uint8_t *packet, size_t len,
75 struct hash_table **table,
76 unsigned int *key, size_t *off)
78 struct ethhdr *eth = (struct ethhdr *) packet;
80 if (len < sizeof(struct ethhdr))
81 return;
83 (*off) = sizeof(struct ethhdr);
84 (*key) = ntohs(eth->h_proto);
85 (*table) = &eth_lay2;
88 struct protocol ethernet_ops = {
89 .key = 0,
90 .offset = sizeof(struct ethhdr),
91 .print_full = ethernet,
92 .print_less = ethernet_less,
93 .print_pay_ascii = empty,
94 .print_pay_hex = empty,
95 .print_pay_none = ethernet,
96 .print_all_hex = ethernet_hex_all,
97 .proto_next = ethernet_next,
100 #endif /* ETHERNET_H */