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.

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

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.

00112 {
00113     if (!addr) {
00114         opp_error("UdpOutDevice::decapsulate called without providing "
00115                   "sockaddr (addr = NULL)");
00116     }
00117 
00118     if (addrlen != sizeof(sockaddr_in) ) {
00119         opp_error("UdpOutDevice::decapsulate called with wrong sockaddr length. "
00120                   "Only IPv4 is supported at the moment!");
00121     }
00122     sockaddr_in* addrbuf = (sockaddr_in*) addr;
00123 
00124     IPDatagram* IP = new IPDatagram;
00125     UDPPacket* UDP = new UDPPacket;
00126     cMessage* payload = 0;
00127 
00128     // Parse Payload
00129     payload = parser->decapsulatePayload(buf, length);
00130 
00131     if (!payload) {
00132         EV << "[UdpOutDevice::decapsulate()]\n"
00133            << "    Parsing of payload failed, dropping packet"
00134            << endl;
00135         goto parse_error;
00136     }
00137 
00138     // Create IP + UDP header
00139     IP->setSrcAddress(IPAddress(ntohl(addrbuf->sin_addr.s_addr)));
00140     IP->setDestAddress(IPAddressResolver().addressOf(parentModule()).get4());
00141     IP->setTransportProtocol(IPPROTO_UDP);
00142     IP->setTimeToLive(42); // Does not matter, packet ends here
00143     IP->setIdentification(42); // Faked: There is no way to get the real ID
00144     IP->setMoreFragments(false);
00145     IP->setDontFragment(false);
00146     IP->setFragmentOffset(0);
00147     IP->setDiffServCodePoint(0); // Faked...
00148     IP->setLength(160);
00149 
00150     UDP->setSourcePort(ntohs(addrbuf->sin_port));
00151     UDP->setDestinationPort(parentModule()->submodule("overlay", 0)->
00152                             gate("from_app")->toGate()->ownerModule()->
00153                             par("localPort").longValue());
00154 
00155     UDP->setByteLength(8);
00156 
00157     // Done...
00158     UDP->encapsulate(payload);
00159     IP->encapsulate(UDP);
00160     delete[] buf;
00161     delete addr;
00162     return IP;
00163 
00164 parse_error:
00165     delete IP;
00166     delete UDP;
00167     delete addr;
00168     delete payload;
00169     return NULL;
00170 }

UdpOutDevice::Module_Class_Members ( UdpOutDevice  ,
RealworldDevice  ,
 
)


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

Generated on Fri Sep 19 13:05:08 2008 for ITM OverSim by  doxygen 1.5.5