build: fixed PREFIX usage
[netsniff-ng.git] / proto_udp.c
blobf9f052c84c9efe1d6a18ea1f31ebff7d023fa00e
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <netinet/in.h> /* for ntohs() */
11 #include "proto.h"
12 #include "dissector_eth.h"
13 #include "pkt_buff.h"
15 struct udphdr {
16 uint16_t source;
17 uint16_t dest;
18 uint16_t len;
19 uint16_t check;
20 } __packed;
22 static void udp(struct pkt_buff *pkt)
24 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
25 ssize_t len;
26 uint16_t src, dest;
27 char *src_name, *dest_name;
29 if (udp == NULL)
30 return;
32 len = ntohs(udp->len) - sizeof(*udp);
33 src = ntohs(udp->source);
34 dest = ntohs(udp->dest);
36 src_name = lookup_port_udp(src);
37 dest_name = lookup_port_udp(dest);
39 tprintf(" [ UDP ");
40 tprintf("Port (%u", src);
41 if (src_name)
42 tprintf(" (%s%s%s)", colorize_start(bold), src_name,
43 colorize_end());
44 tprintf(" => %u", dest);
45 if (dest_name)
46 tprintf(" (%s%s%s)", colorize_start(bold), dest_name,
47 colorize_end());
48 tprintf("), ");
49 if(len > pkt_len(pkt) || len < 0){
50 tprintf("Len (%u) %s, ", ntohs(udp->len),
51 colorize_start_full(black, red)
52 "invalid" colorize_end());
54 tprintf("Len (%u Bytes, %zd Bytes Data), ", ntohs(udp->len), len);
55 tprintf("CSum (0x%.4x)", ntohs(udp->check));
56 tprintf(" ]\n");
59 static void udp_less(struct pkt_buff *pkt)
61 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
62 uint16_t src, dest;
63 char *src_name, *dest_name;
65 if (udp == NULL)
66 return;
68 src = ntohs(udp->source);
69 dest = ntohs(udp->dest);
71 src_name = lookup_port_udp(src);
72 dest_name = lookup_port_udp(dest);
74 tprintf(" UDP %u", src);
75 if(src_name)
76 tprintf("(%s%s%s)", colorize_start(bold), src_name,
77 colorize_end());
78 tprintf("/%u", dest);
79 if (dest_name)
80 tprintf("(%s%s%s)", colorize_start(bold), dest_name,
81 colorize_end());
84 struct protocol udp_ops = {
85 .key = 0x11,
86 .print_full = udp,
87 .print_less = udp_less,