*mgmt: check is parser was really done
[netsniff-ng.git] / src / proto_udp.h
blob0fc67af0cbc9072dd9a99875cda104e11db22fd8
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 UDP_H
9 #define UDP_H
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <netinet/in.h> /* for ntohs() */
15 #include "proto_struct.h"
16 #include "dissector_eth.h"
17 #include "pkt_buff.h"
19 struct udphdr {
20 uint16_t source;
21 uint16_t dest;
22 uint16_t len;
23 uint16_t check;
24 } __attribute__((packed));
26 static inline uint16_t udp_port(uint16_t src, uint16_t dst)
28 char *tmp1, *tmp2;
30 src = ntohs(src);
31 dst = ntohs(dst);
33 /* XXX: Is there a better way to determine? */
34 if (src < dst && src < 1024) {
35 return src;
36 } else if (dst < src && dst < 1024) {
37 return dst;
38 } else {
39 tmp1 = lookup_port_udp(src);
40 tmp2 = lookup_port_udp(dst);
41 if (tmp1 && !tmp2) {
42 return src;
43 } else if (!tmp1 && tmp2) {
44 return dst;
45 } else {
46 if (src < dst)
47 return src;
48 else
49 return dst;
54 static inline void udp(struct pkt_buff *pkt)
56 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
58 if (udp == NULL)
59 return;
61 tprintf(" [ UDP ");
62 tprintf("Port (%u => %u, %s%s%s), ",
63 ntohs(udp->source), ntohs(udp->dest),
64 colorize_start(bold),
65 lookup_port_udp(udp_port(udp->source, udp->dest)),
66 colorize_end());
67 tprintf("Len (%u), ", ntohs(udp->len));
68 tprintf("CSum (0x%.4x)", ntohs(udp->check));
69 tprintf(" ]\n");
71 pkt_set_proto(pkt, &eth_lay4, udp_port(udp->source, udp->dest));
74 static inline void udp_less(struct pkt_buff *pkt)
76 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
78 if (udp == NULL)
79 return;
81 tprintf(" UDP %s%s%s %u/%u",
82 colorize_start(bold),
83 lookup_port_udp(udp_port(udp->source, udp->dest)),
84 colorize_end(), ntohs(udp->source), ntohs(udp->dest));
86 pkt_set_proto(pkt, &eth_lay4, udp_port(udp->source, udp->dest));
89 struct protocol udp_ops = {
90 .key = 0x11,
91 .print_full = udp,
92 .print_less = udp_less,
95 #endif /* UDP_H */