From 5d786cebd0c4fa43033a3e323190ab365ec4326e Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Mon, 29 May 2006 21:05:27 +0000 Subject: [PATCH] IPHeader. --- practicas/pipi/src/Makefile | 6 ++- practicas/pipi/src/ipheader.h | 85 ++++++++++++++++++++++++++++++ practicas/pipi/src/test_ipaddr.cpp | 15 ++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 practicas/pipi/src/ipheader.h diff --git a/practicas/pipi/src/Makefile b/practicas/pipi/src/Makefile index a6852d2..eafc85e 100644 --- a/practicas/pipi/src/Makefile +++ b/practicas/pipi/src/Makefile @@ -68,7 +68,11 @@ dev.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h dev.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h dev.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h /usr/include/bits/msq.h ipaddr.o: ipaddr.h -test_ipaddr.o: ipaddr.h +ipin.o: ipin.h dev.h +test_ipaddr.o: ipaddr.h ipheader.h /usr/include/stdint.h +test_ipaddr.o: /usr/include/features.h /usr/include/sys/cdefs.h +test_ipaddr.o: /usr/include/gnu/stubs.h /usr/include/bits/wchar.h +test_ipaddr.o: /usr/include/bits/wordsize.h test_recv.o: dev.h /usr/include/unistd.h /usr/include/features.h test_recv.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h test_recv.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h diff --git a/practicas/pipi/src/ipheader.h b/practicas/pipi/src/ipheader.h new file mode 100644 index 0000000..98893be --- /dev/null +++ b/practicas/pipi/src/ipheader.h @@ -0,0 +1,85 @@ +#ifndef _IPHEADER_H_ +#define _IPHEADER_H_ + +#include "ipaddr.h" +#include +#include +#include + +/// Dispositivo de red (capa de enlace) +struct IPHeader +{ + + // Campos + uint8_t version; + //TODO IHL + //TODO TOS + uint16_t total_len; + uint16_t id; + uint16_t reserved_flag: 1; + uint16_t df: 1; + uint16_t mf: 1; + uint16_t offset: 13; + uint8_t ttl; + uint8_t proto; + uint16_t checksum; + uint32_t src; + uint32_t dst; + + IPHeader(uint8_t version, uint16_t total_len, uint16_t id, bool df, + bool mf, uint16_t offset, uint8_t ttl, uint8_t proto, + const IPAddr& src, const IPAddr& dst): + version(version), total_len(total_len), id(id), reserved_flag(0), + df(df), mf(mf), offset(offset), ttl(ttl), proto(proto), checksum(0), + src(src), dst(dst) + { + do_checksum(); + } + + IPHeader(const std::string& s) + { + *this = *((IPHeader*)s.c_str()); + } + + bool check_checksum() const + { + IPHeader iph = *this; + iph.checksum = 0; + char* raw = (char*) &iph; + uint16_t sum = 0; + for (unsigned i = 0; i < sizeof(IPHeader); ++i) + sum += raw[i]; + return sum == checksum; + } + + void do_checksum() + { + checksum = 0; + char* raw = (char*) this; + uint16_t sum = 0; + for (unsigned i = 0; i < sizeof(IPHeader); ++i) + sum += raw[i]; + checksum = sum; + } + +}; + +std::ostream& operator<<(std::ostream& os, const IPHeader& iph) +{ + return os + << "version=" << iph.version + << " total_len=" << iph.total_len + << " id=" << iph.id + << " DF=" << bool(iph.df) + << " MF=" << bool(iph.mf) + << " offset=" << unsigned(iph.offset) + << " TTL=" << unsigned(iph.ttl) + << " proto=" << unsigned(iph.proto) + << " checksum=" << iph.checksum + << " src=" << IPAddr(iph.src) + << " dst=" << IPAddr(iph.dst); +} + +#endif // _IPHEADER_H_ + +// vim: set et sw=4 sts=4 : diff --git a/practicas/pipi/src/test_ipaddr.cpp b/practicas/pipi/src/test_ipaddr.cpp index b3a84d9..a34cb28 100644 --- a/practicas/pipi/src/test_ipaddr.cpp +++ b/practicas/pipi/src/test_ipaddr.cpp @@ -1,14 +1,29 @@ #include "ipaddr.h" +#include "ipheader.h" #include int main() { + // Addr IPAddr ip1(0x0a0a0a05); IPAddr ip2("10.10.10.1"); IPAddr ip3(10, 10, 10, 2); std::cout << "IP1 = " << ip1 << "\n"; std::cout << "IP2 = " << ip2 << "\n"; std::cout << "IP3 = " << ip3 << "\n"; + // Header + IPHeader h1(4, 20, 1, 1, 0, 0, 64, 0x11, ip1, ip2); + std::cout << "Header1 = " << h1 << "\n"; + if (h1.check_checksum()) + std::cout << "Checksum OK\n"; + else + std::cout << "Checksum MAL!\n"; + h1.checksum = 1; + if (h1.check_checksum()) + std::cout << "Checksum OK\n"; + else + std::cout << "Checksum MAL!\n"; + return 0; } // vim: set et sw=4 sts=4 : -- 2.43.0