Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of webserverBlinky by
Diff: PPP-Blinky/ppp-blinky.cpp
- Revision:
- 149:969d98f6fb88
- Parent:
- 148:4225c3608b80
- Child:
- 150:3366bf3d294e
--- a/PPP-Blinky/ppp-blinky.cpp Wed Aug 30 15:18:56 2017 +0000 +++ b/PPP-Blinky/ppp-blinky.cpp Wed Aug 30 16:13:34 2017 +0000 @@ -211,7 +211,6 @@ { led1 = (ppp.ledState >> 1) & 1; // use second bit, in other words toggle LED only every second packet ppp.ledState++; - } /// returns 1 after a connect messasge, 0 at startup or after a disconnect message @@ -529,6 +528,18 @@ memcpy(ppp.pkt.buf+20, tempHold,4); // dest <- tempHold } +/// swap the IP source and destination ports +void swapIpPorts() +{ + int headerSizeIP = 4*(ppp.pkt.buf[4]&0xf); // calculate size of IP header + char * ipSrcPort = 4 + ppp.pkt.buf + headerSizeIP + 0; // ip source port location + char * ipDstPort = 4 + ppp.pkt.buf + headerSizeIP + 2; // ip destin port location + char tempHold[2]; + memcpy(tempHold, ipSrcPort,2); // tempHold <- source + memcpy(ipSrcPort,ipDstPort,2); // source <- dest + memcpy(ipDstPort,tempHold, 2); // dest <- tempHold +} + /// Process an incoming UDP packet. /// If the packet starts with the string "echo " or "test" we echo back a special packet void UDPpacket() @@ -537,8 +548,6 @@ char * ipSizeBuf = ipHeader+2; // size of IP packet int headerSizeIP = 4*(ipHeader[0]&0xf); // size of IP header char * udpHeader = ipHeader + headerSizeIP; // udp info start - char * udpSrcPort = udpHeader+0; // source port - char * udpDstPort = udpHeader+2; // destination port char * udpLen = udpHeader+4; // udp data length char * udpCheckSum = udpHeader+6; // udp checksum char * udpData = udpHeader+8; // start of UDP data @@ -548,6 +557,8 @@ #ifdef SERIAL_PORT_MONITOR_YES char * srcIP = ipHeader+12; // IP source char * dstIP = ipHeader+16; // IP destination + char * udpSrcPort = udpHeader+0; // source port + char * udpDstPort = udpHeader+2; // destination port int udpSrc = (udpSrcPort[0]<<8)|udpSrcPort[1]; // integer of UDP source port int udpDst = (udpDstPort[0]<<8)|udpDstPort[1]; // integer of UDP dest port if(v0) debugPrintf("UDP %d.%d.%d.%d:%d ", srcIP[0],srcIP[1],srcIP[2],srcIP[3],udpSrc); @@ -571,10 +582,7 @@ int testFound = !strncmp(udpData,"test" ,4); // true if UDP message starts with "test" if ( (echoFound) || (testFound)) { // if the UDP message starts with "echo " or "test" we answer back swapIpAddresses(); // swap IP source and destination - char tempHold[4]; - memcpy(tempHold, udpSrcPort,2); - memcpy(udpSrcPort, udpDstPort,2); - memcpy(udpDstPort, tempHold,2); // swap udp port source/dest + swapIpPorts(); // swap IP source and destination ports if (echoFound) { memcpy(udpData,"Got{",4); // in the UDP data modify "echo" to "Got:" char endString[] = "} UDP Server: PPP-Blinky\n"; // an appendix @@ -931,16 +939,16 @@ char * pktLen = ipPkt+2; // 2 bytes char * ident = ipPkt+4; // 2 bytes char * protocol = ipPkt+9; // 1 byte -// char * headercheck= ipPkt+10; // 2 bytes + //char * headercheck= ipPkt+10; // 2 bytes char * srcAdr = ipPkt+12; // 4 bytes -// char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes + //char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes int headerSizeIP = (ihl[0]&0xf)*4; int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length // TCP header char * tcp = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet - char * srctcp = tcp + 0; // 2 bytes - char * dsttcp = tcp + 2; // 2 bytes + //char * srctcp = tcp + 0; // 2 bytes + //char * dsttcp = tcp + 2; // 2 bytes char * seqtcp = tcp + 4; // 4 bytes char * acktcp = tcp + 8; // 4 bytes char * offset = tcp + 12; // 4 bits @@ -963,6 +971,7 @@ // first we shorten the TCP response header to only 20 bytes. This means we ignore all TCP option requests tcpSize = 20; // shorten total TCP packet size to 20 bytes (no data) headerSizeTCP = 20; // shorten outgoing TCP header size 20 bytes + ipPkt[0] = 0x45; // short IP packet size to 45 offset[0] = (headerSizeTCP/4)<<4; // shorten tcp header size to 20 bytes packetLength = 40; // shorten total packet size to 40 bytes (20 ip + 20 tcp) pktLen[1] = 40; // set total packet size to 40 bytes (20 ip + 20 tcp) @@ -1004,11 +1013,8 @@ // The TCP flag handling is now done // first we swap source and destination TCP addresses and insert the new ack and seq numbers - char tempHold[12]; // it's 12 long because we later reuse it when building the TCP pseudo-header swapIpAddresses(); // swap IP source and destination addresses - memcpy(tempHold,srctcp,2); - memcpy(srctcp, dsttcp,2); - memcpy(dsttcp, tempHold,2); // swap ip port source/dest + swapIpPorts(); // swap IP source and destination ports acktcp[0]=ack_out>>24; acktcp[1]=ack_out>>16; @@ -1036,6 +1042,7 @@ // now we have to build the so-called 12-byte TCP "pseudo-header" in front of the TCP header (containing some IP header values) in order to correctly calculate the TCP checksum // this header contains the most important parts of the IP header, i.e. source and destination address, protocol number and data length. + char tempHold[12]; // it's 12 long because we later reuse it when building the TCP pseudo-header char * pseudoHeader = tcp-12; // mark the start of the TCP pseudo-header memcpy(tempHold, pseudoHeader, 12); // preserve the 12 bytes of the IP header where the TCP pseudo-Header will be built memcpy( pseudoHeader+0, srcAdr, 8); // IP source and destination addresses from IP header