Add support to store less than the full packet in an etherdump
[gem5.git] / dev / etherpkt.hh
index 312d98f640ec29d8b3fb012dbd575058be6c2b30..abdf301663216d69de3be99865e7df2aa4a713f8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003 The Regents of The University of Michigan
+ * Copyright (c) 2002-2004 The Regents of The University of Michigan
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #ifndef __ETHERPKT_HH__
 #define __ETHERPKT_HH__
 
+#include <iosfwd>
 #include <memory>
+#include <assert.h>
 
 #include "sim/host.hh"
 #include "base/refcnt.hh"
-
-#define EADDR_LEN 6
+#include "base/inet_hdrs.hh"
 
 class Checkpoint;
-
-struct pseudo_header
-{
-    uint32_t src_ip_addr;
-    uint32_t dest_ip_addr;
-    uint16_t protocol;
-    uint16_t len;
-};
-
-/** Ethernet header struct for casting purposes */
-struct eth_header
-{
-    uint8_t dest[EADDR_LEN];
-    uint8_t src[EADDR_LEN];
-    uint16_t type;
-};
-
-struct ip_header
-{
-    uint8_t vers_len;
-    uint8_t service_type;
-    uint16_t dgram_len;
-    uint16_t ID;
-    uint16_t flags_frag_offset;
-    uint8_t TTL;
-    uint8_t protocol;
-    uint16_t hdr_chksum;
-    uint32_t src_ip_addr;
-    uint32_t dest_ip_addr;
-    uint8_t *options;
-    uint8_t *transport_header;
-};
-
-struct tcp_header
-{
-    uint16_t src_port_num;
-    uint16_t dest_port_num;
-    uint32_t seq_num;
-    uint32_t ack_num;
-    uint8_t hdr_len;
-    uint8_t flags;
-    uint16_t rcv_window;
-    uint16_t chksum;
-    uint16_t urgent;
-    uint8_t *options;
-    uint8_t *data;
-};
-
-struct udp_header
-{
-    uint16_t src_port_num;
-    uint16_t dest_port_num;
-    uint16_t len;
-    uint16_t chksum;
-    uint8_t *data;
-};
-
 /*
  * Reference counted class containing ethernet packet data
  */
@@ -118,14 +62,39 @@ class EtherPacket : public RefCounted
     bool IsMulticast() { return data[0] == 0x01; }
     bool IsBroadcast() { return data[0] == 0xff; }
 
-    ip_header *getIpHdr() { return (ip_header *) (data + 14); }
-
-    void *getTransportHdr() {
+    bool isIpPkt() {
+        eth_header *eth = (eth_header *) data;
+        return (eth->type == 0x8);
+    }
+    bool isTcpPkt(ip_header *ip) {
+        return (ip->protocol == 0x6);
+    }
+    bool isTcpPkt() {
         ip_header *ip = getIpHdr();
-        return (void *) (ip + (ip->vers_len & 0xf));
+        return (ip->protocol == 0x6);
+    }
+    bool isUdpPkt(ip_header *ip) {
+        return (ip->protocol == 17);
+    }
+    bool isUdpPkt() {
+        ip_header *ip = getIpHdr();
+        return (ip->protocol == 17);
     }
 
+    ip_header *getIpHdr() {
+        assert(isIpPkt());
+        return (ip_header *) (data + sizeof(eth_header));
+    }
 
+    tcp_header *getTcpHdr(ip_header *ip) {
+        assert(isTcpPkt(ip));
+        return (tcp_header *) ((uint8_t *) ip + (ip->vers_len & 0xf)*4);
+    }
+
+    udp_header *getUdpHdr(ip_header *ip) {
+        assert(isUdpPkt(ip));
+        return (udp_header *) ((uint8_t *) ip + (ip->vers_len & 0xf)*4);
+    }
     typedef RefCountingPtr<EtherPacket> PacketPtr;
 
     void serialize(std::ostream &os);