TunOutDevice Class Reference

#include <TunOutDevice.h>

Inheritance diagram for TunOutDevice:

RealworldDevice RealworldConnector List of all members.

Detailed Description

TunOutDevice is a pseudo interface that allows communcation with the real world through the TunOutScheduler.

WARNING: This does ONLY work with the combination IPv4|UDP|OverlayMessage


Public Member Functions

 Module_Class_Members (TunOutDevice, RealworldDevice, 0)

Protected Member Functions

virtual char * encapsulate (cMessage *msg, unsigned int *length, sockaddr **addr, socklen_t *addrlen)
 Converts an IP datagram to a data block for sending it to the (realworld) network.
virtual cMessage * decapsulate (char *buf, uint32_t length, sockaddr *addr, socklen_t addrlen)
 Parses data received from the (realworld) network and converts it into a cMessage.


Member Function Documentation

char * TunOutDevice::encapsulate ( cMessage *  msg,
unsigned int *  length,
sockaddr **  addr,
socklen_t *  addrlen 
) [protected, virtual]

Converts an IP datagram to a data block for sending it to the (realworld) network.

Parameters:
msg A pointer to the message to be converted
length A pointer to an int that will hold the length of the converted data
addr Ignored (set to 0)
addrlen Ignored (set to 0)
Returns:
A pointer to the converted data

Implements RealworldConnector.

00038 {
00039     *addr = 0;
00040     *addrlen = 0;
00041 
00042     struct udppseudohdr {
00043         uint32_t saddr;
00044         uint32_t daddr;
00045         uint8_t zero;
00046         uint8_t protocol;
00047         uint16_t lenght;
00048     }
00049     * pseudohdr;
00050 
00051     unsigned int payloadlen;
00052     static unsigned int iplen = 20; // we don't generate IP options
00053     static unsigned int udplen = 8;
00054     cMessage* payloadMsg = NULL;
00055     char* buf = NULL, *payload = NULL;
00056     uint32_t saddr, daddr; 
00057     iphdr* ip_buf;
00058     udphdr* udp_buf;
00059 
00060     IPDatagram* IP = check_and_cast<IPDatagram*>(msg);
00061     // FIXME: Cast ICMP-Messages
00062     UDPPacket* UDP = dynamic_cast<UDPPacket*>(IP->decapsulate());
00063     if (!UDP) {
00064         EV << "Can't parse non-UDP packets (e.g. ICMP) (yet)...\n";
00065         goto parse_error;
00066     }
00067     payloadMsg = UDP->decapsulate();
00068 
00069     // parse payload
00070     payload = parser->encapsulatePayload(payloadMsg, &payloadlen);
00071     if (!payload ) {
00072         EV << "Can't parse packet payload, dropping packet\n";
00073         goto parse_error;
00074     }
00075 
00076     *length = payloadlen + iplen + udplen;
00077     if( *length > mtu ) {
00078         EV << "TunOutDevice::encapsulate: Error: Packet too big! Size = " << *length << " MTU = " << mtu << "\n";
00079         goto parse_error;
00080     }
00081 
00082     buf = new char[*length];
00083 
00084     // We use the buffer to build an ip packet.
00085     // To minimise unnecessary copying, we start with the payload
00086     // and write it to the end of the buffer
00087     memcpy( (buf + iplen + udplen), payload, payloadlen);
00088 
00089     // write udp header in front of the payload
00090     udp_buf = (udphdr*) (buf + iplen);
00091     udp_buf->source = htons(UDP->sourcePort());
00092     udp_buf->dest = htons(UDP->destinationPort());
00093     udp_buf->len = htons(udplen + payloadlen);
00094     udp_buf->check = 0;
00095 
00096     // Write udp pseudoheader in from of udp header
00097     // this will be overwritten by ip header
00098     pseudohdr = (udppseudohdr*) (buf + iplen - sizeof(struct udppseudohdr));
00099     saddr = htonl(IP->srcAddress().getInt());
00100     daddr = htonl(IP->destAddress().getInt());
00101     pseudohdr->saddr = saddr;
00102     pseudohdr->daddr = daddr;
00103     pseudohdr->zero = 0;
00104     pseudohdr->protocol = IPPROTO_UDP;
00105     pseudohdr->lenght = udp_buf->len;
00106 
00107     // compute UDP checksum
00108     udp_buf->check = cksum((uint16_t*) pseudohdr, sizeof(struct udppseudohdr) + udplen + payloadlen);
00109 
00110     // write ip header to begin of buffer
00111     ip_buf = (iphdr*) buf;
00112     ip_buf->version = 4; // IPv4
00113     ip_buf->ihl = iplen / 4;
00114     ip_buf->tos = IP->diffServCodePoint();
00115     ip_buf->tot_len = htons(*length);
00116     ip_buf->id = htons(IP->identification());
00117     ip_buf->frag_off = htons(IP_DF); // DF, no fragments
00118     ip_buf->ttl = IP->timeToLive();
00119     ip_buf->protocol = IPPROTO_UDP;
00120     ip_buf->saddr = saddr;
00121     ip_buf->daddr = daddr;
00122     ip_buf->check = 0;
00123     ip_buf->check = cksum((uint16_t*) ip_buf, iplen);
00124 
00125     delete IP;
00126     delete UDP;
00127     delete payloadMsg;
00128     delete payload;
00129 
00130     return buf;
00131 
00132 parse_error:
00133     delete IP;
00134     delete UDP;
00135     delete payloadMsg;
00136     delete payload;
00137     return NULL;
00138 
00139 }

cMessage * TunOutDevice::decapsulate ( char *  buf,
uint32_t  length,
sockaddr *  addr,
socklen_t  addrlen 
) [protected, virtual]

Parses data received from the (realworld) network and converts it into a cMessage.

Parameters:
buf A pointer to the data to be parsed
length The lenght of the buffer in bytes
addr Ignored (deleted)
addrlen Ignored
Returns:
The parsed message

Implements RealworldConnector.

00145 {
00146     // Message starts with IP header
00147     iphdr* ip_buf = (iphdr*) buf;
00148     udphdr* udp_buf;
00149     IPDatagram* IP = new IPDatagram;
00150     UDPPacket* UDP = new UDPPacket;
00151     cMessage* payload = 0;
00152     unsigned int payloadLen, datagramlen;
00153     unsigned int packetlen = ntohs(ip_buf->tot_len);
00154 
00155     // Parsing of IP header, sanity checks
00156     if (  packetlen != length ) {
00157         ev << "TunOutDevice: Dropping bogus packet, header says: length = " << packetlen << " but actual length = " << length <<".\n";
00158         goto parse_error;
00159     }
00160     if (  packetlen > mtu ) {
00161         ev << "TunOutDevice: Dropping bogus packet, length = " << packetlen << " but mtu = " << mtu <<".\n";
00162         goto parse_error;
00163     }
00164     if ( ip_buf->version != 4 ) {
00165         ev << "TunOutDevice: Dropping Packet: Packet is not IPv4.\n";
00166         goto parse_error;
00167     }
00168     if ( ntohs(ip_buf->frag_off) & 0xBFFF ) { // mask DF bit
00169         ev << "TunOutDevice: Dropping Packet: Can't handle fragmented packets.\n";
00170         goto parse_error;
00171     }
00172     if ( ip_buf->protocol != IPPROTO_UDP ) { // FIXME: allow ICMP packets
00173         ev << "TunOutDevice: Dropping Packet: Packet is not UDP.\n";
00174         goto parse_error;
00175     }
00176     IP->setSrcAddress( IPAddress( ntohl(ip_buf->saddr) ));
00177     IP->setDestAddress( IPAddress( ntohl(ip_buf->daddr) ));
00178     IP->setTransportProtocol( ip_buf->protocol );
00179     IP->setTimeToLive( ip_buf->ttl );
00180     IP->setIdentification( ntohs(ip_buf->id) );
00181     IP->setMoreFragments( false );
00182     IP->setDontFragment( true );
00183     IP->setFragmentOffset( 0 );
00184     IP->setDiffServCodePoint( ip_buf->tos );
00185     IP->setLength( ip_buf->ihl*32 );
00186     // FIXME: check IP and UDP checksum...
00187 
00188     // Parse UDP header, sanity checks
00189     udp_buf = (udphdr*)( ((uint32_t *)ip_buf) + ip_buf->ihl );
00190     datagramlen = ntohs(udp_buf->len);
00191     if ( (datagramlen != packetlen - ip_buf->ihl*4) ) {
00192         ev << "TunOutDevice: Dropping Packet: Bogus UDP datagram length: len = " << datagramlen << " packetlen = " << packetlen << " ihl*4 " << ip_buf->ihl*4 << ".\n";
00193         goto parse_error;
00194     }
00195     UDP->setSourcePort( ntohs( udp_buf->source ));
00196     UDP->setDestinationPort( ntohs( udp_buf->dest ));
00197     UDP->setByteLength( sizeof( struct udphdr ) );
00198 
00199     // parse payload
00200     payloadLen = datagramlen - sizeof( struct udphdr );
00201     payload = parser->decapsulatePayload( ((char*) udp_buf) + sizeof( struct udphdr ), payloadLen );
00202     if (!payload) {
00203         ev << "TunOutDevice: Parsing of Payload failed, dropping packet.\n";
00204         goto parse_error;
00205     }
00206     // encapsulate messages
00207     UDP->encapsulate( payload );
00208     IP->encapsulate( UDP );
00209 
00210     delete buf;
00211     delete addr;
00212     return IP;
00213 
00214     // In case the parsing of the packet failed, free allocated memory
00215 parse_error:
00216     delete buf;
00217     delete addr;
00218     delete IP;
00219     delete UDP;
00220     return NULL;
00221 }

TunOutDevice::Module_Class_Members ( TunOutDevice  ,
RealworldDevice  ,
 
)


The documentation for this class was generated from the following files:
Generated on Tue Jul 24 16:51:19 2007 for ITM OverSim by  doxygen 1.5.1