docs: fixed minor typo
[netsniff-ng.git] / src / proto_udp.h
blob2343d3f04aec480822be0605cd03d64acc8abdc9
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"
18 struct udphdr {
19 uint16_t source;
20 uint16_t dest;
21 uint16_t len;
22 uint16_t check;
23 } __attribute__((packed));
25 static inline uint16_t udp_port(uint16_t src, uint16_t dst)
27 char *tmp1, *tmp2;
29 src = ntohs(src);
30 dst = ntohs(dst);
32 /* XXX: Is there a better way to determine? */
33 if (src < dst && src < 1024) {
34 return src;
35 } else if (dst < src && dst < 1024) {
36 return dst;
37 } else {
38 tmp1 = lookup_port_udp(src);
39 tmp2 = lookup_port_udp(dst);
40 if (tmp1 && !tmp2) {
41 return src;
42 } else if (!tmp1 && tmp2) {
43 return dst;
44 } else {
45 if (src < dst)
46 return src;
47 else
48 return dst;
53 static inline void udp(uint8_t *packet, size_t len)
55 struct udphdr *udp = (struct udphdr *) packet;
57 if (len < sizeof(struct udphdr))
58 return;
60 tprintf(" [ UDP ");
61 tprintf("Port (%u => %u, %s%s%s), ",
62 ntohs(udp->source), ntohs(udp->dest),
63 colorize_start(bold),
64 lookup_port_udp(udp_port(udp->source, udp->dest)),
65 colorize_end());
66 tprintf("Len (%u), ", ntohs(udp->len));
67 tprintf("CSum (0x%.4x)", ntohs(udp->check));
68 tprintf(" ]\n");
71 static inline void udp_less(uint8_t *packet, size_t len)
73 struct udphdr *udp = (struct udphdr *) packet;
75 if (len < sizeof(struct udphdr))
76 return;
78 tprintf(" UDP %s%s%s %u/%u",
79 colorize_start(bold),
80 lookup_port_udp(udp_port(udp->source, udp->dest)),
81 colorize_end(), ntohs(udp->source), ntohs(udp->dest));
84 static inline void udp_next(uint8_t *packet, size_t len,
85 struct hash_table **table,
86 unsigned int *key, size_t *off)
88 struct udphdr *udp = (struct udphdr *) packet;
90 if (len < sizeof(struct udphdr))
91 goto invalid;
93 (*off) = sizeof(struct udphdr);
94 (*key) = udp_port(udp->source, udp->dest);
95 (*table) = &eth_lay4;
97 return;
98 invalid:
99 (*off) = 0;
100 (*key) = 0;
101 (*table) = NULL;
104 struct protocol udp_ops = {
105 .key = 0x11,
106 .offset = sizeof(struct udphdr),
107 .print_full = udp,
108 .print_less = udp_less,
109 .print_pay_ascii = empty,
110 .print_pay_hex = empty,
111 .print_pay_none = udp,
112 .print_all_cstyle = __hex2,
113 .print_all_hex = __hex,
114 .proto_next = udp_next,
117 #endif /* UDP_H */