25 #include <platdep/sockets.h>
27 #include "IPDatagram_m.h"
28 #include "UDPPacket.h"
29 #include "IPAddressResolver.h"
41 cPacket* payloadMsg = NULL;
43 sockaddr_in* addrbuf = NULL;
45 unsigned int payloadLen;
47 IPDatagram* IP = check_and_cast<IPDatagram*>(msg);
49 UDPPacket* UDP =
dynamic_cast<UDPPacket*
>(IP->decapsulate());
52 EV <<
"[UdpOutDevice::encapsulate()]\n"
53 <<
" Can't parse non-UDP packets (e.g. ICMP) (yet)"
60 if( IP->getMoreFragments() ) {
61 EV <<
"[UdpOutDevice::encapsulate()]\n"
62 <<
" Can't parse fragmented packets"
67 payloadMsg = UDP->decapsulate();
72 EV <<
"[UdpOutDevice::encapsulate()]\n"
73 <<
" Can't parse packet payload, dropping packet"
78 if (payloadLen >
mtu) {
79 EV <<
"[UdpOutDevice::encapsulate()]\n"
80 <<
" Encapsulating packet failed: packet too long"
88 addrbuf =
new sockaddr_in;
89 addrbuf->sin_family = AF_INET;
90 addrbuf->sin_port = htons(UDP->getDestinationPort());
91 addrbuf->sin_addr.s_addr = htonl(IP->getDestAddress().getInt());
92 *addrlen =
sizeof(sockaddr_in);
93 *addr = (sockaddr*) addrbuf;
115 opp_error(
"UdpOutDevice::decapsulate called without providing "
116 "sockaddr (addr = NULL)");
119 if (addrlen !=
sizeof(sockaddr_in) ) {
120 opp_error(
"UdpOutDevice::decapsulate called with wrong sockaddr length. "
121 "Only IPv4 is supported at the moment!");
123 sockaddr_in* addrbuf = (sockaddr_in*) addr;
125 IPDatagram* IP =
new IPDatagram;
126 UDPPacket* UDP =
new UDPPacket;
127 cPacket* payload = 0;
133 EV <<
"[UdpOutDevice::decapsulate()]\n"
134 <<
" Parsing of payload failed, dropping packet"
140 IP->setSrcAddress(IPAddress(ntohl(addrbuf->sin_addr.s_addr)));
141 IP->setDestAddress(IPAddressResolver().addressOf(getParentModule()).get4());
142 IP->setTransportProtocol(IPPROTO_UDP);
143 IP->setTimeToLive(42);
144 IP->setIdentification(42);
145 IP->setMoreFragments(
false);
146 IP->setDontFragment(
false);
147 IP->setFragmentOffset(0);
148 IP->setDiffServCodePoint(0);
149 IP->setBitLength(160);
151 UDP->setSourcePort(ntohs(addrbuf->sin_port));
152 UDP->setDestinationPort(getParentModule()->getSubmodule(
"overlay", 0)->
153 gate(
"appIn")->getNextGate()->getOwnerModule()->
154 par(
"localPort").longValue());
156 UDP->setByteLength(8);
159 UDP->encapsulate(payload);
160 IP->encapsulate(UDP);