proto_ipv4: don't trim length of pkt_buff
[netsniff-ng.git] / src / proto_udp.c
blob36dc9ada41895d2308de83e7a6492428d1007c0c
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 #include <stdio.h>
9 #include <stdint.h>
10 #include <netinet/in.h> /* for ntohs() */
12 #include "proto.h"
13 #include "protos.h"
14 #include "dissector_eth.h"
15 #include "pkt_buff.h"
17 struct udphdr {
18 uint16_t source;
19 uint16_t dest;
20 uint16_t len;
21 uint16_t check;
22 } __attribute__((packed));
24 static uint16_t udp_port(uint16_t src, uint16_t dst)
26 char *tmp1, *tmp2;
28 src = ntohs(src);
29 dst = ntohs(dst);
31 /* XXX: Is there a better way to determine? */
32 if (src < dst && src < 1024) {
33 return src;
34 } else if (dst < src && dst < 1024) {
35 return dst;
36 } else {
37 tmp1 = lookup_port_udp(src);
38 tmp2 = lookup_port_udp(dst);
39 if (tmp1 && !tmp2) {
40 return src;
41 } else if (!tmp1 && tmp2) {
42 return dst;
43 } else {
44 if (src < dst)
45 return src;
46 else
47 return dst;
52 static void udp(struct pkt_buff *pkt)
54 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
56 if (udp == NULL)
57 return;
59 tprintf(" [ UDP ");
60 tprintf("Port (%u => %u, %s%s%s), ",
61 ntohs(udp->source), ntohs(udp->dest),
62 colorize_start(bold),
63 lookup_port_udp(udp_port(udp->source, udp->dest)),
64 colorize_end());
65 tprintf("Len (%u), ", ntohs(udp->len));
66 tprintf("CSum (0x%.4x)", ntohs(udp->check));
67 tprintf(" ]\n");
69 pkt_set_proto(pkt, &eth_lay4, udp_port(udp->source, udp->dest));
72 static void udp_less(struct pkt_buff *pkt)
74 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
76 if (udp == NULL)
77 return;
79 tprintf(" UDP %s%s%s %u/%u",
80 colorize_start(bold),
81 lookup_port_udp(udp_port(udp->source, udp->dest)),
82 colorize_end(), ntohs(udp->source), ntohs(udp->dest));
84 pkt_set_proto(pkt, &eth_lay4, udp_port(udp->source, udp->dest));
87 struct protocol udp_ops = {
88 .key = 0x11,
89 .print_full = udp,
90 .print_less = udp_less,
93 EXPORT_SYMBOL(udp_ops);