xmalloc: put __hidden behind signature
[netsniff-ng.git] / proto_udp.c
blob96b4e9fc17b6169a316201053fa04113a9298b3d
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 void udp(struct pkt_buff *pkt)
26 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
27 ssize_t len;
28 uint16_t src, dest;
29 char *src_name, *dest_name;
31 if (udp == NULL)
32 return;
34 len = ntohs(udp->len) - sizeof(*udp);
35 src = ntohs(udp->source);
36 dest = ntohs(udp->dest);
38 src_name = lookup_port_udp(src);
39 dest_name = lookup_port_udp(dest);
41 tprintf(" [ UDP ");
42 tprintf("Port (%u", src);
43 if (src_name)
44 tprintf(" (%s%s%s)", colorize_start(bold), src_name,
45 colorize_end());
46 tprintf(" => %u", dest);
47 if (dest_name)
48 tprintf(" (%s%s%s)", colorize_start(bold), dest_name,
49 colorize_end());
50 tprintf("), ");
51 if(len > pkt_len(pkt) || len < 0){
52 tprintf("Len (%u) %s, ", ntohs(udp->len),
53 colorize_start_full(black, red)
54 "invalid" colorize_end());
56 tprintf("Len (%u Bytes, %zd Bytes Data), ", ntohs(udp->len), len);
57 tprintf("CSum (0x%.4x)", ntohs(udp->check));
58 tprintf(" ]\n");
61 static void udp_less(struct pkt_buff *pkt)
63 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
64 uint16_t src, dest;
65 char *src_name, *dest_name;
67 if (udp == NULL)
68 return;
70 src = ntohs(udp->source);
71 dest = ntohs(udp->dest);
73 src_name = lookup_port_udp(src);
74 dest_name = lookup_port_udp(dest);
76 tprintf(" UDP %u", src);
77 if(src_name)
78 tprintf("(%s%s%s)", colorize_start(bold), src_name,
79 colorize_end());
80 tprintf("/%u", dest);
81 if (dest_name)
82 tprintf("(%s%s%s)", colorize_start(bold), dest_name,
83 colorize_end());
86 struct protocol udp_ops = {
87 .key = 0x11,
88 .print_full = udp,
89 .print_less = udp_less,