#include <UDPSerializer.h>
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) |
|
00032 {}
|
|
Helper: calculate checksum 00066 { 00067 long sum = 0; 00068 00069 while (count > 1) { 00070 sum += *((unsigned short *&)addr)++; 00071 if (sum & 0x80000000) 00072 sum = (sum & 0xFFFF) + (sum >> 16); 00073 count -= 2; 00074 } 00075 00076 if (count) 00077 sum += *(unsigned char *)addr; 00078 00079 while (sum >> 16) 00080 sum = (sum & 0xffff) + (sum >> 16); 00081 00082 return ~sum; 00083 }
|
|
Puts a packet sniffed from the wire into an UDPPacket. 00052 { 00053 00054 struct udphdr *udphdr = (struct udphdr*) buf; 00055 00056 dest->setSourcePort(ntohs(udphdr->uh_sport)); 00057 dest->setDestinationPort(ntohs(udphdr->uh_dport)); 00058 dest->setByteLength(8); 00059 cMessage *encapPacket = new cMessage("Payload-from-wire"); 00060 encapPacket->setByteLength(ntohs(udphdr->uh_ulen) - sizeof(struct udphdr)); 00061 dest->encapsulate(encapPacket); 00062 dest->setName(encapPacket->name()); 00063 }
|
|
Serializes an UDPPacket for transmission on the wire. Returns the length of data written into buffer. 00039 { 00040 struct udphdr *udphdr = (struct udphdr *) (buf); 00041 int packetLength; 00042 00043 packetLength = pkt->byteLength(); 00044 udphdr->uh_sport = htons(pkt->sourcePort()); 00045 udphdr->uh_dport = htons(pkt->destinationPort()); 00046 udphdr->uh_ulen = htons(packetLength); 00047 udphdr->uh_sum = checksum(buf, packetLength); 00048 return packetLength; 00049 }
|