zsh: add netsniff-ng zsh auto completion support
[netsniff-ng.git] / proto_icmpv4.c
blob13f0eda74419986061580010e7e04be089585744
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 "csum.h"
14 #include "dissector_eth.h"
15 #include "pkt_buff.h"
16 #include "built_in.h"
18 struct icmphdr {
19 uint8_t type;
20 uint8_t code;
21 uint16_t checksum;
22 union {
23 struct {
24 uint16_t id;
25 uint16_t sequence;
26 } echo;
27 uint32_t gateway;
28 struct {
29 uint16_t ____unused;
30 uint16_t mtu;
31 } frag;
32 } un;
33 } __packed;
35 static void icmp(struct pkt_buff *pkt)
37 struct icmphdr *icmp = (struct icmphdr *) pkt_pull(pkt, sizeof(*icmp));
38 uint16_t csum;
40 if (icmp == NULL)
41 return;
43 csum = calc_csum(icmp, pkt_len(pkt) + sizeof(*icmp), 0);
45 tprintf(" [ ICMP ");
46 tprintf("Type (%u), ", icmp->type);
47 tprintf("Code (%u), ", icmp->code);
48 tprintf("CSum (0x%.4x) is %s", ntohs(icmp->checksum),
49 csum ? colorize_start_full(black, red) "bogus (!)"
50 colorize_end() : "ok");
51 tprintf(" ]\n");
54 static void icmp_less(struct pkt_buff *pkt)
56 struct icmphdr *icmp = (struct icmphdr *) pkt_pull(pkt, sizeof(*icmp));
58 if (icmp == NULL)
59 return;
61 tprintf(" Type %u Code %u", icmp->type, icmp->code);
64 struct protocol icmpv4_ops = {
65 .key = 0x01,
66 .print_full = icmp,
67 .print_less = icmp_less,