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
--- /dev/null
+#ifndef _IPHEADER_H_
+#define _IPHEADER_H_
+
+#include "ipaddr.h"
+#include <string>
+#include <ostream>
+#include <stdint.h>
+
+/// 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 :
#include "ipaddr.h"
+#include "ipheader.h"
#include <iostream>
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 :