netsniff-ng: Allow to compile without libnl
[netsniff-ng-new.git] / proto_udp.c
blobf607b1426a45896f17934f12ad20dec19bef21d2
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 "protos.h"
13 #include "lookup.h"
14 #include "pkt_buff.h"
16 struct udphdr {
17 uint16_t source;
18 uint16_t dest;
19 uint16_t len;
20 uint16_t check;
21 } __packed;
23 static void udp(struct pkt_buff *pkt)
25 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
26 ssize_t len;
27 uint16_t src, dest;
28 const char *src_name, *dest_name;
30 if (udp == NULL)
31 return;
33 len = ntohs(udp->len) - sizeof(*udp);
34 src = ntohs(udp->source);
35 dest = ntohs(udp->dest);
37 src_name = lookup_port_udp(src);
38 dest_name = lookup_port_udp(dest);
40 tprintf(" [ UDP ");
41 tprintf("Port (%u", src);
42 if (src_name)
43 tprintf(" (%s%s%s)", colorize_start(bold), src_name,
44 colorize_end());
45 tprintf(" => %u", dest);
46 if (dest_name)
47 tprintf(" (%s%s%s)", colorize_start(bold), dest_name,
48 colorize_end());
49 tprintf("), ");
50 if(len > pkt_len(pkt) || len < 0){
51 tprintf("Len (%u) %s, ", ntohs(udp->len),
52 colorize_start_full(black, red)
53 "invalid" colorize_end());
55 tprintf("Len (%u Bytes, %zd Bytes Data), ", ntohs(udp->len), len);
56 tprintf("CSum (0x%.4x)", ntohs(udp->check));
57 tprintf(" ]\n");
60 static void udp_less(struct pkt_buff *pkt)
62 struct udphdr *udp = (struct udphdr *) pkt_pull(pkt, sizeof(*udp));
63 uint16_t src, dest;
64 const char *src_name, *dest_name;
66 if (udp == NULL)
67 return;
69 src = ntohs(udp->source);
70 dest = ntohs(udp->dest);
72 src_name = lookup_port_udp(src);
73 dest_name = lookup_port_udp(dest);
75 tprintf(" UDP %u", src);
76 if(src_name)
77 tprintf("(%s%s%s)", colorize_start(bold), src_name,
78 colorize_end());
79 tprintf("/%u", dest);
80 if (dest_name)
81 tprintf("(%s%s%s)", colorize_start(bold), dest_name,
82 colorize_end());
85 struct protocol udp_ops = {
86 .key = 0x11,
87 .print_full = udp,
88 .print_less = udp_less,