A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.
Dependents: oldheating gps motorhome heating
Diff: udp/udp.cpp
- Revision:
- 37:793b39683406
- Parent:
- 33:714a0345e59b
- Child:
- 42:222a4f45f916
diff -r 900e24b27bfb -r 793b39683406 udp/udp.cpp --- a/udp/udp.cpp Mon Sep 25 07:09:32 2017 +0000 +++ b/udp/udp.cpp Wed Oct 04 07:51:02 2017 +0000 @@ -1,16 +1,17 @@ -#include "mbed.h" -#include "log.h" -#include "net.h" -#include "udp.h" -#include "ntp.h" -#include "dhcp.h" -#include "dns.h" -#include "eth.h" -#include "ip4.h" -#include "ip6.h" -#include "slaac.h" -#include "ns.h" -#include "io.h" +#include "mbed.h" +#include "log.h" +#include "net.h" +#include "action.h" +#include "udp.h" +#include "ntp.h" +#include "dhcp.h" +#include "dns.h" +#include "eth.h" +#include "ip4.h" +#include "ip6.h" +#include "slaac.h" +#include "ns.h" +#include "io.h" #define UNKNOWN true @@ -22,22 +23,22 @@ uint16_t totalLength; uint16_t checksum; }; -uint16_t UdpSrcPort; -uint16_t UdpDstPort; +static uint16_t srcPort; +static uint16_t dstPort; static uint16_t checksum; static uint16_t totalLength; -static int handlePort(int* pDataLength, void* pData) +static int handlePort(void (*traceback)(void), int* pDataLength, void* pData) { - switch (UdpDstPort) + switch (dstPort) { //Handle these - case DHCP_CLIENT_PORT: return DhcpHandleResponse ( pDataLength, pData); // 68 - case NTP_PORT: return NtpHandlePacketReceived( pDataLength, pData); // 123 - case DNS_UNICAST_CLIENT_PORT: return DnsHandlePacketReceived(DNS_PROTOCOL_UDNS, pDataLength, pData); //53053 - case DNS_MDNS_PORT: return DnsHandlePacketReceived(DNS_PROTOCOL_MDNS, pDataLength, pData); // 5353 - case DNS_LLMNR_CLIENT_PORT: return DnsHandlePacketReceived(DNS_PROTOCOL_LLMNR, pDataLength, pData); //53055 - case DNS_LLMNR_SERVER_PORT: return DnsHandlePacketReceived(DNS_PROTOCOL_LLMNR, pDataLength, pData); // 5355 + case DHCP_CLIENT_PORT: return DhcpHandleResponse (traceback, pDataLength, pData); // 68 + case NTP_PORT: return NtpHandlePacketReceived(traceback, pDataLength, pData); // 123 + case DNS_UNICAST_CLIENT_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_UDNS, pDataLength, pData); //53053 + case DNS_MDNS_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_MDNS, pDataLength, pData); // 5353 + case DNS_LLMNR_CLIENT_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_LLMNR, pDataLength, pData); //53055 + case DNS_LLMNR_SERVER_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_LLMNR, pDataLength, pData); // 5355 //Quietly drop these case DHCP_SERVER_PORT: //67 @@ -56,53 +57,54 @@ //Report anything else default: - if (UNKNOWN) LogTimeF("UDP unknown port %d\r\n", UdpDstPort); + if (UNKNOWN) LogTimeF("UDP unknown port %d\r\n", dstPort); return DO_NOTHING; } } -int UdpHandleReceivedPacket(int* pSize, void* pPacket) +int UdpHandleReceivedPacket(void (*traceback)(void), int* pSize, void* pPacket) { void* pData = (char*)pPacket + HEADER_SIZE; int dataLength = *pSize - HEADER_SIZE; - int action = handlePort(&dataLength, pData); + int action = handlePort(traceback, &dataLength, pData); *pSize = dataLength + HEADER_SIZE; - uint16_t tmpPort = UdpDstPort; - UdpDstPort = UdpSrcPort; - UdpSrcPort = tmpPort; + uint16_t tmpPort = dstPort; + dstPort = srcPort; + srcPort = tmpPort; return action; } static int pollForPacketToSend(int type, int* pDataLength, void* pData) { - int action = 0; + int action = DO_NOTHING; if (!action && type == IPV4) //DHCP only works under IPv4 { action = DhcpPollForRequestToSend(pData, pDataLength); if (action) { - UdpSrcPort = DHCP_CLIENT_PORT; - UdpDstPort = DHCP_SERVER_PORT; + srcPort = DHCP_CLIENT_PORT; + dstPort = DHCP_SERVER_PORT; } } if (!action) //DNS is agnostic { action = DnsPollForPacketToSend(pData, pDataLength); - if (action) + int dest = ActionGetDestPart(action); + if (dest) { - switch (action) + switch (dest) { - case UNICAST_DNS: UdpSrcPort = DNS_UNICAST_CLIENT_PORT; UdpDstPort = DNS_UNICAST_SERVER_PORT; break; //53053, 53 - case MULTICAST_MDNS: UdpSrcPort = DNS_MDNS_PORT; UdpDstPort = DNS_MDNS_PORT; break; // 5353, 5353 - case MULTICAST_LLMNR: UdpSrcPort = DNS_LLMNR_CLIENT_PORT; UdpDstPort = DNS_LLMNR_SERVER_PORT; break; //53055, 5355 + case UNICAST_DNS: srcPort = DNS_UNICAST_CLIENT_PORT; dstPort = DNS_UNICAST_SERVER_PORT; break; //53053, 53 + case MULTICAST_MDNS: srcPort = DNS_MDNS_PORT; dstPort = DNS_MDNS_PORT; break; // 5353, 5353 + case MULTICAST_LLMNR: srcPort = DNS_LLMNR_CLIENT_PORT; dstPort = DNS_LLMNR_SERVER_PORT; break; //53055, 5355 //Report anything else default: - LogTimeF("DNS unknown action %d\r\n", action); + LogTimeF("DNS unknown dest %d\r\n", dest); return DO_NOTHING; } } @@ -112,10 +114,11 @@ action = NtpPollForPacketToSend(type, pData, pDataLength); if (action) { - UdpSrcPort = NTP_PORT; - UdpDstPort = NTP_PORT; + srcPort = NTP_PORT; + dstPort = NTP_PORT; } } + return action; } int UdpPollForPacketToSend(int type, int* pSize, void* pPacket) @@ -129,11 +132,11 @@ return action; } -void UdpLogHeader(char* title, void* pPacket, uint16_t calculatedChecksum) +void UdpLogHeader(uint16_t calculatedChecksum) { - LogTimeF("UDP %s\r\n", title); - LogF(" Source port %hu\r\n", UdpSrcPort); - LogF(" Destination port %hu\r\n", UdpDstPort); + Log ("UDP header\r\n"); + LogF(" Source port %hu\r\n", srcPort); + LogF(" Destination port %hu\r\n", dstPort); LogF(" Total length %hu\r\n", totalLength); LogF(" Checksum (hex) %04hX\r\n", checksum); LogF(" Calculated %04hX\r\n", calculatedChecksum); @@ -143,8 +146,8 @@ { struct header* pHeader = (header*)pPacket; - pHeader->dstPort = NetToHost16(UdpDstPort); - pHeader->srcPort = NetToHost16(UdpSrcPort); + pHeader->dstPort = NetToHost16(dstPort); + pHeader->srcPort = NetToHost16(srcPort); pHeader->totalLength = NetToHost16(size); pHeader->checksum = 0; @@ -158,8 +161,8 @@ { struct header* pHeader = (header*)pPacket; - UdpSrcPort = NetToHost16(pHeader->srcPort); - UdpDstPort = NetToHost16(pHeader->dstPort); + srcPort = NetToHost16(pHeader->srcPort); + dstPort = NetToHost16(pHeader->dstPort); totalLength = NetToHost16(pHeader->totalLength); checksum = NetToHost16(pHeader->checksum); } \ No newline at end of file