#include <UdpOutDevice.h>
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. |
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.
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 |
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 << "[UdpOutDevice::encapsulate()]\n" 00050 << " Can't parse non-UDP packets (e.g. ICMP) (yet)" 00051 << endl; 00052 goto parse_error; 00053 } 00054 payloadMsg = UDP->decapsulate(); 00055 00056 // parse payload 00057 payload = parser->encapsulatePayload(payloadMsg, &payloadLen); 00058 if (!payload ) { 00059 EV << "[UdpOutDevice::encapsulate()]\n" 00060 << " Can't parse packet payload, dropping packet" 00061 << endl; 00062 goto parse_error; 00063 } 00064 00065 if(payloadLen > mtu) { 00066 EV << "[UdpOutDevice::encapsulate()]\n" 00067 << " Encapsulating packet failed: packet too long" 00068 << endl; 00069 goto parse_error; 00070 } 00071 *length = payloadLen; 00072 00073 // create sockaddr 00074 addrbuf = new sockaddr_in; 00075 addrbuf->sin_family = AF_INET; 00076 addrbuf->sin_port = htons(UDP->destinationPort()); 00077 addrbuf->sin_addr.s_addr = htonl(IP->destAddress().getInt()); 00078 *addrlen = sizeof(sockaddr_in); 00079 *addr = (sockaddr*) addrbuf; 00080 00081 delete IP; 00082 delete UDP; 00083 delete payloadMsg; 00084 return payload; 00085 00086 parse_error: 00087 delete IP; 00088 delete UDP; 00089 delete payloadMsg; 00090 delete payload; 00091 return NULL; 00092 00093 }
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.
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 |
Implements RealworldConnector.
00099 { 00100 if (!addr) { 00101 opp_error("UdpOutDevice::decapsulate called without providing sockaddr (addr = NULL)"); 00102 } 00103 if ( addrlen != sizeof(sockaddr_in) ) { 00104 opp_error("UdpOutDevice::decapsulate called with wron sockaddr length. " 00105 "Only IPv4 is supported at the moment!"); 00106 } 00107 sockaddr_in* addrbuf = (sockaddr_in*) addr; 00108 00109 IPDatagram* IP = new IPDatagram; 00110 UDPPacket* UDP = new UDPPacket; 00111 cMessage* payload = 0; 00112 const char* myaddr = 0; 00113 00114 // Parse Payload 00115 payload = parser->decapsulatePayload( buf, length); 00116 if (!payload) { 00117 EV << "[UdpOutDevice::decapsulate()]\n" 00118 << " Parsing of payload failed, dropping packet" 00119 << endl; 00120 goto parse_error; 00121 } 00122 00123 myaddr = parentModule()->parentModule()->submodule("underlayConfigurator", 0)->par("nodeIP").stringValue(); 00124 00125 // Create IP + UDP header 00126 IP->setSrcAddress( IPAddress( ntohl(addrbuf->sin_addr.s_addr) )); 00127 IP->setDestAddress( IPAddress( myaddr )); 00128 IP->setTransportProtocol( IPPROTO_UDP ); 00129 IP->setTimeToLive( 42 ); // Does not matter, packet ends here 00130 IP->setIdentification( 42 ); // Faked: There is no way to get the real ID 00131 IP->setMoreFragments( false ); 00132 IP->setDontFragment( true ); 00133 IP->setFragmentOffset( 0 ); 00134 IP->setDiffServCodePoint( 0 ); // Faked... 00135 IP->setLength( 160 ); 00136 00137 UDP->setSourcePort( ntohs( addrbuf->sin_port )); 00138 UDP->setDestinationPort( 00139 parentModule()->submodule("overlay", 0)->par("localPort").longValue() 00140 ); 00141 UDP->setByteLength( 8 ); 00142 00143 // Done... 00144 UDP->encapsulate( payload ); 00145 IP->encapsulate( UDP ); 00146 delete buf; 00147 delete addr; 00148 return IP; 00149 00150 parse_error: 00151 delete IP; 00152 delete UDP; 00153 delete addr; 00154 delete payload; 00155 return NULL; 00156 }
UdpOutDevice::Module_Class_Members | ( | UdpOutDevice | , | |
RealworldDevice | , | |||
0 | ||||
) |