Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

IPSerializer Class Reference

#include <IPSerializer.h>

List of all members.


Detailed Description

Converts between IPDatagram and binary (network byte order) IP header.


Public Member Functions

 IPSerializer ()
int serialize (IPDatagram *dgram, unsigned char *buf, unsigned int bufsize)
void parse (unsigned char *buf, unsigned int bufsize, IPDatagram *dest)


Constructor & Destructor Documentation

IPSerializer::IPSerializer  )  [inline]
 

00031 {}


Member Function Documentation

void IPSerializer::parse unsigned char *  buf,
unsigned int  bufsize,
IPDatagram dest
 

Puts a packet sniffed from the wire into an IPDatagram. Does NOT verify the checksum.

00093 {
00094     const struct ip *ip = (struct ip *) buf;
00095     unsigned int totalLength, headerLength;
00096 
00097     dest->setVersion(ip->ip_v);
00098     dest->setHeaderLength(IP_HEADER_BYTES);
00099     dest->setSrcAddress(ntohl(ip->ip_src.s_addr));
00100     dest->setDestAddress(ntohl(ip->ip_dst.s_addr));
00101     dest->setTransportProtocol(ip->ip_p);
00102     dest->setTimeToLive(ip->ip_ttl);
00103     dest->setIdentification(ntohs(ip->ip_id));
00104     dest->setMoreFragments((ip->ip_off) & !IP_OFFMASK & IP_MF);
00105     dest->setDontFragment((ip->ip_off) & !IP_OFFMASK & IP_DF);
00106     dest->setFragmentOffset((ntohs(ip->ip_off)) & IP_OFFMASK);
00107     dest->setDiffServCodePoint(ip->ip_tos);
00108     totalLength = ntohs(ip->ip_len);
00109     headerLength = ip->ip_hl << 2;
00110 
00111     if (headerLength > IP_HEADER_BYTES)
00112         EV << "Handling an captured IP packet with options. Dropping the options.\n";
00113     if (totalLength > bufsize)
00114         EV << "Can not handle IP packet of total length " << totalLength << "(captured only " << bufsize << " bytes).\n";
00115     dest->setByteLength(IP_HEADER_BYTES);
00116 
00117     cMessage *encapPacket = NULL;
00118     switch (dest->transportProtocol())
00119     {
00120       case IP_PROT_ICMP:
00121         encapPacket = new ICMPMessage("icmp-from-wire");
00122         ICMPSerializer().parse(buf + headerLength, min(totalLength, bufsize) - headerLength, (ICMPMessage *)encapPacket);
00123         break;
00124       case IP_PROT_UDP:
00125         encapPacket = new UDPPacket("udp-from-wire");
00126         UDPSerializer().parse(buf + headerLength, min(totalLength, bufsize) - headerLength, (UDPPacket *)encapPacket);
00127         break;
00128       default:
00129         opp_error("IPSerializer: cannot serialize protocol %d", dest->transportProtocol());
00130     }
00131 
00132     ASSERT(encapPacket);
00133     dest->encapsulate(encapPacket);
00134     dest->setName(encapPacket->name());
00135 }

int IPSerializer::serialize IPDatagram dgram,
unsigned char *  buf,
unsigned int  bufsize
 

Serializes an IPDatagram for transmission on the wire. The checksum is NOT filled in. (The kernel does that when sending the frame over a raw socket.) Returns the length of data written into buffer.

00049 {
00050     int packetLength;
00051     struct ip *ip = (struct ip *) buf;
00052 
00053     ip->ip_hl         = IP_HEADER_BYTES >> 2;
00054     ip->ip_v          = dgram->version();
00055     ip->ip_tos        = dgram->diffServCodePoint();
00056     ip->ip_id         = htons(dgram->identification());
00057     ip->ip_off        = htons(dgram->fragmentOffset());
00058     ip->ip_ttl        = dgram->timeToLive();
00059     ip->ip_p          = dgram->transportProtocol();
00060     ip->ip_src.s_addr = htonl(dgram->srcAddress().getInt());
00061     ip->ip_dst.s_addr = htonl(dgram->destAddress().getInt());
00062     ip->ip_sum        = 0;
00063 
00064     if (dgram->headerLength() > IP_HEADER_BYTES)
00065         EV << "Serializing an IP packet with options. Dropping the options.\n";
00066 
00067     packetLength = IP_HEADER_BYTES;
00068 
00069     cMessage *encapPacket = dgram->encapsulatedMsg();
00070     switch (dgram->transportProtocol())
00071     {
00072       case IP_PROT_ICMP:
00073         packetLength += ICMPSerializer().serialize(check_and_cast<ICMPMessage *>(encapPacket),
00074                                                    buf+IP_HEADER_BYTES, bufsize-IP_HEADER_BYTES);
00075         break;
00076       case IP_PROT_UDP:
00077         packetLength += UDPSerializer().serialize(check_and_cast<UDPPacket *>(encapPacket),
00078                                                    buf+IP_HEADER_BYTES, bufsize-IP_HEADER_BYTES);
00079         break;
00080       default:
00081         opp_error("IPSerializer: cannot serialize protocol %d", dgram->transportProtocol());
00082     }
00083 
00084 #ifdef linux
00085         ip->ip_len = htons(packetLength);
00086 #else
00087         ip->ip_len = packetLength;
00088 #endif
00089     return packetLength;
00090 }


The documentation for this class was generated from the following files:
Generated on Thu Oct 19 18:22:24 2006 for INET Framework for OMNeT++/OMNEST by  doxygen 1.4.0