UDPSerializer Class Reference

#include <UDPSerializer.h>

List of all members.


Detailed Description

Converts between UDPPacket and binary (network byte order) UDP header.

Public Member Functions

 UDPSerializer ()
int serialize (UDPPacket *pkt, unsigned char *buf, unsigned int bufsize)
void parse (unsigned char *buf, unsigned int bufsize, UDPPacket *pkt)

Static Public Member Functions

static unsigned short checksum (unsigned char *addr, unsigned int count)

Constructor & Destructor Documentation

UDPSerializer::UDPSerializer (  )  [inline]

00031 {}


Member Function Documentation

int UDPSerializer::serialize ( UDPPacket *  pkt,
unsigned char *  buf,
unsigned int  bufsize 
)

Serializes an UDPPacket for transmission on the wire. Returns the length of data written into buffer.

00038 {
00039     struct udphdr *udphdr = (struct udphdr *) (buf);
00040     int packetLength;
00041 
00042     packetLength = pkt->getByteLength();
00043     udphdr->uh_sport = htons(pkt->getSourcePort());
00044     udphdr->uh_dport = htons(pkt->getDestinationPort());
00045     udphdr->uh_ulen  = htons(packetLength);
00046     udphdr->uh_sum   = checksum(buf, packetLength);
00047     return packetLength;
00048 }

void UDPSerializer::parse ( unsigned char *  buf,
unsigned int  bufsize,
UDPPacket *  pkt 
)

Puts a packet sniffed from the wire into an UDPPacket.

00051 {
00052 
00053     struct udphdr *udphdr = (struct udphdr*) buf;
00054 
00055     dest->setSourcePort(ntohs(udphdr->uh_sport));
00056     dest->setDestinationPort(ntohs(udphdr->uh_dport));
00057     dest->setByteLength(8);
00058     cPacket *encapPacket = new cPacket("Payload-from-wire");
00059     encapPacket->setByteLength(ntohs(udphdr->uh_ulen) - sizeof(struct udphdr));
00060     dest->encapsulate(encapPacket);
00061     dest->setName(encapPacket->getName());
00062 }

unsigned short UDPSerializer::checksum ( unsigned char *  addr,
unsigned int  count 
) [static]

Helper: calculate checksum

Referenced by serialize().

00065 {
00066     long sum = 0;
00067 
00068     while (count > 1)  {
00069         sum += *((unsigned short *&)addr)++;
00070         if (sum & 0x80000000)
00071             sum = (sum & 0xFFFF) + (sum >> 16);
00072         count -= 2;
00073     }
00074 
00075     if (count)
00076         sum += *(unsigned char *)addr;
00077 
00078     while (sum >> 16)
00079         sum = (sum & 0xffff) + (sum >> 16);
00080 
00081     return ~sum;
00082 }


The documentation for this class was generated from the following files:

Generated on Fri Mar 20 18:51:23 2009 for INET Framework for OMNeT++/OMNEST by  doxygen 1.5.5