From cc823f0b647f1e9a0007abf69609ccb70a26962f Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Sun, 4 Apr 2010 09:53:33 +0000 Subject: [PATCH] Added icmp proto template (there's more todo). git-svn-id: http://netsniff-ng.googlecode.com/svn/trunk@318 21e0ff64-9a0b-11de-825e-994487f65616 --- src/include/netsniff-ng/packet.h | 7 +++ src/include/netsniff-ng/protocols/icmp.h | 71 ++++++++++++++++++++++++++++ src/include/netsniff-ng/protocols/layers_3.h | 1 + src/lib/print.c | 4 ++ 4 files changed, 83 insertions(+) create mode 100644 src/include/netsniff-ng/protocols/icmp.h diff --git a/src/include/netsniff-ng/packet.h b/src/include/netsniff-ng/packet.h index ed2f56e7..446b6ead 100644 --- a/src/include/netsniff-ng/packet.h +++ b/src/include/netsniff-ng/packet.h @@ -37,6 +37,7 @@ typedef struct packet { /* Union l4 */ struct tcphdr *tcp_header; struct udphdr *udp_header; + struct icmphdr *icmp_header; /* > l4 */ /* Make a map of how the packet looks like */ #define MAX_STEPS 20 @@ -97,11 +98,17 @@ static inline int parse_packet(uint8_t * raw, uint32_t len, packet_t * pkt) pkt->tcp_header = get_tcphdr(buffer, &tmp_len); set_pkt_step(pkt, IPPROTO_TCP); break; + case IPPROTO_UDP: pkt->udp_header = get_udphdr(buffer, &tmp_len); set_pkt_step(pkt, IPPROTO_UDP); break; + case IPPROTO_ICMP: + pkt->icmp_header = get_icmphdr(buffer, &tmp_len); + set_pkt_step(pkt, IPPROTO_ICMP); + break; + default: break; } diff --git a/src/include/netsniff-ng/protocols/icmp.h b/src/include/netsniff-ng/protocols/icmp.h new file mode 100644 index 00000000..5c698df1 --- /dev/null +++ b/src/include/netsniff-ng/protocols/icmp.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2009, 2010 Daniel Borkmann and + * Emmanuel Roullit + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA + */ + +#ifndef __PROTO_ICMP_H__ +#define __PROTO_ICMP_H__ + +#include +#include + +#include +#include + +#include +#include + +static inline struct icmphdr *get_icmphdr(uint8_t ** pkt, uint32_t * pkt_len) +{ + struct icmphdr *icmp_header; + + assert(pkt); + assert(*pkt); + assert(*pkt_len > sizeof(*icmp_header)); + + icmp_header = (struct icmphdr *)*pkt; + + *pkt += sizeof(*icmp_header); + *pkt_len -= sizeof(*icmp_header); + + return (icmp_header); +} + +/* + * print_icmphdr - Just plain dumb formatting + * @ip: icmp header + */ + /* XXX: print codes and the whole rest */ +void print_icmphdr(struct icmphdr *icmp) +{ + assert(icmp); + + //uint16_t csum = calc_csum(icmp, sizeof(*icmp), 0); + + info(" [ ICMP "); + info("Type (%u), ", icmp->type); + info("Code (%u), ", icmp->code); + info("Chsum (0x%x)", ntohs(icmp->checksum) /* TODO:, csum ? colorize_full_str(red, black, "bogus (!)") : "ok"Ü*/); + + //if (csum) { + // info(" should be %x", csum_expected(icmp->checksum, csum)); + //} + + info(" ] \n"); +} + +#endif /* __PROTO_ICMP_H__ */ diff --git a/src/include/netsniff-ng/protocols/layers_3.h b/src/include/netsniff-ng/protocols/layers_3.h index 801708d5..143f17e6 100644 --- a/src/include/netsniff-ng/protocols/layers_3.h +++ b/src/include/netsniff-ng/protocols/layers_3.h @@ -22,5 +22,6 @@ #include #include +#include #endif /* __LAYERS_3_H__ */ diff --git a/src/lib/print.c b/src/lib/print.c index 4e72f50f..9bad94bc 100644 --- a/src/lib/print.c +++ b/src/lib/print.c @@ -287,6 +287,10 @@ static inline void __versatile_header_only_print(ring_buff_bytes_t * rbb, const print_udphdr(pkt->udp_header); break; + case IPPROTO_ICMP: + print_icmphdr(pkt->icmp_header); + break; + default: break; -- 2.11.4.GIT