]> git.llucax.com Git - z.facultad/75.74/practicos.git/commitdiff
IPHeader.
authorLeandro Lucarella <llucax@gmail.com>
Mon, 29 May 2006 21:05:27 +0000 (21:05 +0000)
committerLeandro Lucarella <llucax@gmail.com>
Mon, 29 May 2006 21:05:27 +0000 (21:05 +0000)
practicas/pipi/src/Makefile
practicas/pipi/src/ipheader.h [new file with mode: 0644]
practicas/pipi/src/test_ipaddr.cpp

index a6852d25094298606ed714287973eebcc44a800c..eafc85e2f68ac79677e73783e2fb4e55d071079c 100644 (file)
@@ -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 (file)
index 0000000..98893be
--- /dev/null
@@ -0,0 +1,85 @@
+#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 :
index b3a84d9b46d9f51083fabf167414cc75264c603d..a34cb28c9f8b7af4e9dcc672cb4b6a76aef44d1d 100644 (file)
@@ -1,14 +1,29 @@
 #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 :