UdpOutDevice Class Reference

#include <UdpOutDevice.h>

Inheritance diagram for UdpOutDevice:

RealworldDevice RealworldConnector List of all members.

Detailed Description

UdpOutDevice is a pseudo interface that allows communcation with the real world through the UdpOutScheduler.

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


Public Member Functions

 Module_Class_Members (UdpOutDevice, 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 * UdpOutDevice::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 The destination address
addrlen The length of the address
Returns:
A pointer to the converted data

Implements RealworldConnector.

00038 {
00039     cMessage* payloadMsg = NULL;
00040     char* payload = NULL;
00041     sockaddr_in* addrbuf = NULL;
00042 
00043     unsigned int payloadLen;
00044     
00045     IPDatagram* IP = check_and_cast<IPDatagram*>(msg);
00046     // FIXME: Cast ICMP-Messages
00047     UDPPacket* UDP = dynamic_cast<UDPPacket*>(IP->decapsulate());
00048     if (!UDP) {
00049         EV << "Can't parse non-UDP packets (e.g. ICMP) (yet)...\n";
00050         goto parse_error;
00051     }
00052     payloadMsg = UDP->decapsulate();
00053 
00054     // parse payload
00055     payload = parser->encapsulatePayload(payloadMsg, &payloadLen);
00056     if (!payload ) {
00057         EV << "Can't parse packet payload, dropping packet\n";
00058         goto parse_error;
00059     }
00060 
00061     if(payloadLen > mtu) {
00062         EV << "RealworldApp: Encapsulating packet failed: packet too long\n";
00063         goto parse_error;
00064     }
00065     *length = payloadLen;
00066 
00067     // create sockaddr
00068     addrbuf = new sockaddr_in;
00069     addrbuf->sin_family = AF_INET;
00070     addrbuf->sin_port = htons(UDP->destinationPort());
00071     addrbuf->sin_addr.s_addr = htonl(IP->destAddress().getInt());
00072     *addrlen = sizeof(sockaddr_in);
00073     *addr = (sockaddr*) addrbuf;
00074 
00075     delete IP;
00076     delete UDP;
00077     delete payloadMsg;
00078     return payload;
00079 
00080 parse_error:
00081     delete IP;
00082     delete UDP;
00083     delete payloadMsg;
00084     delete payload;
00085     return NULL;
00086 
00087 }

cMessage * UdpOutDevice::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 The destination address
addrlen The length of the address
Returns:
The parsed message

Implements RealworldConnector.

00093 {
00094     if (!addr) {
00095         opp_error("UdpOutDevice::decapsulate called without providing sockaddr (addr = NULL)");
00096     }
00097     if ( addrlen != sizeof(sockaddr_in) ) {
00098         opp_error("UdpOutDevice::decapsulate called with wron sockaddr length. "
00099                 "Only IPv4 is supported at the moment!");
00100     }
00101     sockaddr_in* addrbuf = (sockaddr_in*) addr;
00102 
00103     IPDatagram* IP = new IPDatagram;
00104     UDPPacket* UDP = new UDPPacket;
00105     cMessage* payload = 0; 
00106     const char* myaddr = 0;
00107 
00108     // Parse Payload
00109     payload = parser->decapsulatePayload( buf, length);
00110     if (!payload) {
00111         ev << "UdpOutDevice: Parsing of Payload failed, dropping packet.\n";
00112         goto parse_error;
00113     }
00114 
00115     myaddr = parentModule()->parentModule()->submodule("underlayConfigurator", 0)->par("nodeIP").stringValue();
00116 
00117     // Create IP + UDP header
00118     IP->setSrcAddress( IPAddress( ntohl(addrbuf->sin_addr.s_addr) ));
00119     IP->setDestAddress( IPAddress( myaddr ));
00120     IP->setTransportProtocol( IPPROTO_UDP );
00121     IP->setTimeToLive( 42 ); // Does not matter, packet ends here
00122     IP->setIdentification( 42 ); // Faked: There is no way to get the real ID
00123     IP->setMoreFragments( false );
00124     IP->setDontFragment( true );
00125     IP->setFragmentOffset( 0 );
00126     IP->setDiffServCodePoint( 0 ); // Faked...
00127     IP->setLength( 160 );
00128 
00129     UDP->setSourcePort( ntohs( addrbuf->sin_port ));
00130     UDP->setDestinationPort(
00131             parentModule()->submodule("overlay", 0)->par("localPort").longValue()
00132     );
00133     UDP->setByteLength( 8 );
00134 
00135     // Done...
00136     UDP->encapsulate( payload );
00137     IP->encapsulate( UDP );
00138     delete buf;
00139     delete addr;
00140     return IP;
00141 
00142 parse_error:
00143     delete IP;
00144     delete UDP;
00145     delete addr;
00146     delete payload;
00147     return NULL;
00148 }

UdpOutDevice::Module_Class_Members ( UdpOutDevice  ,
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