#include <IPSerializer.h>
Public Member Functions | |
IPSerializer () | |
int | serialize (IPDatagram *dgram, unsigned char *buf, unsigned int bufsize) |
void | parse (unsigned char *buf, unsigned int bufsize, IPDatagram *dest) |
|
00031 {}
|
|
Puts a packet sniffed from the wire into an IPDatagram. Does NOT verify the checksum. 00079 { 00080 const struct ip *ip = (struct ip *) buf; 00081 unsigned int totalLength, headerLength; 00082 00083 dest->setVersion(ip->ip_v); 00084 dest->setHeaderLength(IP_HEADER_BYTES); 00085 dest->setSrcAddress(ntohl(ip->ip_src.s_addr)); 00086 dest->setDestAddress(ntohl(ip->ip_dst.s_addr)); 00087 dest->setTransportProtocol(ip->ip_p); 00088 dest->setTimeToLive(ip->ip_ttl); 00089 dest->setIdentification(ntohs(ip->ip_id)); 00090 dest->setMoreFragments((ip->ip_off) & !IP_OFFMASK & IP_MF); 00091 dest->setDontFragment((ip->ip_off) & !IP_OFFMASK & IP_DF); 00092 dest->setFragmentOffset((ntohs(ip->ip_off)) & IP_OFFMASK); 00093 dest->setDiffServCodePoint(ip->ip_tos); 00094 totalLength = ntohs(ip->ip_len); 00095 headerLength = ip->ip_hl << 2; 00096 00097 if (headerLength > IP_HEADER_BYTES) 00098 EV << "Handling an captured IP packet with options. Dropping the options.\n"; 00099 if (totalLength > bufsize) 00100 EV << "Can not handle IP packet of total length " << totalLength << "(captured only " << bufsize << " bytes).\n"; 00101 dest->setByteLength(IP_HEADER_BYTES); 00102 00103 cMessage *encapPacket = NULL; 00104 switch (dest->transportProtocol()) 00105 { 00106 case IP_PROT_ICMP: 00107 encapPacket = new ICMPMessage("icmp-from-wire"); 00108 ICMPSerializer().parse(buf + headerLength, min(totalLength, bufsize) - headerLength, (ICMPMessage *)encapPacket); 00109 break; 00110 default: 00111 opp_error("IPSerializer: cannot serialize protocol %d", dest->transportProtocol()); 00112 } 00113 00114 ASSERT(encapPacket); 00115 dest->encapsulate(encapPacket); 00116 dest->setName(encapPacket->name()); 00117 }
|
|
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. 00043 { 00044 int packetLength; 00045 struct ip *ip = (struct ip *) buf; 00046 00047 ip->ip_hl = IP_HEADER_BYTES >> 2; 00048 ip->ip_v = dgram->version(); 00049 ip->ip_tos = dgram->diffServCodePoint(); 00050 ip->ip_id = htons(dgram->identification()); 00051 ip->ip_off = htons(dgram->fragmentOffset()); 00052 ip->ip_ttl = dgram->timeToLive(); 00053 ip->ip_p = dgram->transportProtocol(); 00054 ip->ip_src.s_addr = htonl(dgram->srcAddress().getInt()); 00055 ip->ip_dst.s_addr = htonl(dgram->destAddress().getInt()); 00056 ip->ip_sum = 0; 00057 00058 if (dgram->headerLength() > IP_HEADER_BYTES) 00059 EV << "Serializing an IP packet with options. Dropping the options.\n"; 00060 00061 packetLength = IP_HEADER_BYTES; 00062 00063 cMessage *encapPacket = dgram->encapsulatedMsg(); 00064 switch (dgram->transportProtocol()) 00065 { 00066 case IP_PROT_ICMP: 00067 packetLength += ICMPSerializer().serialize(check_and_cast<ICMPMessage *>(encapPacket), 00068 buf+IP_HEADER_BYTES, bufsize-IP_HEADER_BYTES); 00069 break; 00070 default: 00071 opp_error("IPSerializer: cannot serialize protocol %d", dgram->transportProtocol()); 00072 } 00073 00074 ip->ip_len = htons(packetLength); 00075 return packetLength; 00076 }
|