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

Committer:
andrewboyson
Date:
Thu Dec 14 20:55:40 2017 +0000
Revision:
59:e0e556c8bd46
Parent:
57:e0fb648acf48
Added buffer length to help avoid over runs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 37:793b39683406 1 #include "mbed.h"
andrewboyson 37:793b39683406 2 #include "log.h"
andrewboyson 37:793b39683406 3 #include "net.h"
andrewboyson 37:793b39683406 4 #include "action.h"
andrewboyson 37:793b39683406 5 #include "udp.h"
andrewboyson 37:793b39683406 6 #include "ntp.h"
andrewboyson 57:e0fb648acf48 7 #include "tftp.h"
andrewboyson 37:793b39683406 8 #include "dhcp.h"
andrewboyson 37:793b39683406 9 #include "dns.h"
andrewboyson 37:793b39683406 10 #include "eth.h"
andrewboyson 37:793b39683406 11 #include "ip4.h"
andrewboyson 37:793b39683406 12 #include "ip6.h"
andrewboyson 37:793b39683406 13 #include "slaac.h"
andrewboyson 37:793b39683406 14 #include "ns.h"
andrewboyson 37:793b39683406 15 #include "io.h"
andrewboyson 10:f0854784e960 16
andrewboyson 52:fbc5a46b5e16 17 bool UdpTrace = true;
andrewboyson 10:f0854784e960 18
andrewboyson 10:f0854784e960 19 __packed struct header
andrewboyson 10:f0854784e960 20 {
andrewboyson 10:f0854784e960 21 uint16_t srcPort;
andrewboyson 10:f0854784e960 22 uint16_t dstPort;
andrewboyson 10:f0854784e960 23 uint16_t totalLength;
andrewboyson 10:f0854784e960 24 uint16_t checksum;
andrewboyson 10:f0854784e960 25 };
andrewboyson 37:793b39683406 26 static uint16_t srcPort;
andrewboyson 37:793b39683406 27 static uint16_t dstPort;
andrewboyson 10:f0854784e960 28 static uint16_t checksum;
andrewboyson 10:f0854784e960 29 static uint16_t totalLength;
andrewboyson 10:f0854784e960 30
andrewboyson 59:e0e556c8bd46 31 static void readHeader(void* pPacket, uint16_t size)
andrewboyson 59:e0e556c8bd46 32 {
andrewboyson 59:e0e556c8bd46 33 struct header* pHeader = (header*)pPacket;
andrewboyson 59:e0e556c8bd46 34
andrewboyson 59:e0e556c8bd46 35 srcPort = NetToHost16(pHeader->srcPort);
andrewboyson 59:e0e556c8bd46 36 dstPort = NetToHost16(pHeader->dstPort);
andrewboyson 59:e0e556c8bd46 37 totalLength = NetToHost16(pHeader->totalLength);
andrewboyson 59:e0e556c8bd46 38 checksum = NetToHost16(pHeader->checksum);
andrewboyson 59:e0e556c8bd46 39 }
andrewboyson 59:e0e556c8bd46 40 static int handlePort(void (*traceback)(void), int dataLengthRx, void* pDataRx, int* pPataLengthTx, void* pDataTx)
andrewboyson 10:f0854784e960 41 {
andrewboyson 37:793b39683406 42 switch (dstPort)
andrewboyson 10:f0854784e960 43 {
andrewboyson 10:f0854784e960 44 //Handle these
andrewboyson 59:e0e556c8bd46 45 case DHCP_CLIENT_PORT: return DhcpHandleResponse (traceback, dataLengthRx, pDataRx, pPataLengthTx, pDataTx); // 68
andrewboyson 59:e0e556c8bd46 46 case NTP_PORT: return NtpHandlePacketReceived(traceback, dataLengthRx, pDataRx, pPataLengthTx, pDataTx); // 123
andrewboyson 59:e0e556c8bd46 47 case DNS_UNICAST_CLIENT_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_UDNS, dataLengthRx, pDataRx, pPataLengthTx, pDataTx); //53053
andrewboyson 59:e0e556c8bd46 48 case DNS_MDNS_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_MDNS, dataLengthRx, pDataRx, pPataLengthTx, pDataTx); // 5353
andrewboyson 59:e0e556c8bd46 49 case DNS_LLMNR_CLIENT_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_LLMNR, dataLengthRx, pDataRx, pPataLengthTx, pDataTx); //53055
andrewboyson 59:e0e556c8bd46 50 case DNS_LLMNR_SERVER_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_LLMNR, dataLengthRx, pDataRx, pPataLengthTx, pDataTx); // 5355
andrewboyson 59:e0e556c8bd46 51 case TFTP_CLIENT_PORT: return TftpHandlePacketReceived(traceback, dataLengthRx, pDataRx, pPataLengthTx, pDataTx); //60690
andrewboyson 10:f0854784e960 52
andrewboyson 10:f0854784e960 53 //Quietly drop these
andrewboyson 42:222a4f45f916 54 case DHCP_SERVER_PORT: //67
andrewboyson 57:e0fb648acf48 55 case TFTP_SERVER_PORT: //69
andrewboyson 42:222a4f45f916 56 case 137: //NETBIOS name service
andrewboyson 42:222a4f45f916 57 case 138: //NETBIOS datagram service
andrewboyson 42:222a4f45f916 58 case 139: //NETBIOS session service
andrewboyson 42:222a4f45f916 59 case 500: //Key exchange - Xbox live
andrewboyson 42:222a4f45f916 60 case 1900: //SSDP Simple Service Discovery Protocol (uPnP)
andrewboyson 42:222a4f45f916 61 case 3074: //Xbox live
andrewboyson 42:222a4f45f916 62 case 3076: //Call of Duty - Xbox
andrewboyson 52:fbc5a46b5e16 63 case 5050: //Don't know but have been sent by Kate and my android phones.
andrewboyson 42:222a4f45f916 64 case 5224: //Don't know but a burst was broadcast by Kate's phone containing '_logitech-reverse-bonjour._tcp.local.5446 192.168.1.36 string'
andrewboyson 52:fbc5a46b5e16 65 case 9956: //Alljoyn part of Allseen IoT services
andrewboyson 42:222a4f45f916 66 case 9997: //VLC
andrewboyson 42:222a4f45f916 67 case 9998: //VLC
andrewboyson 42:222a4f45f916 68 case 9999: //VLC
andrewboyson 42:222a4f45f916 69 case 17500: //Dropbox LAN sync
andrewboyson 10:f0854784e960 70 return DO_NOTHING;
andrewboyson 10:f0854784e960 71
andrewboyson 10:f0854784e960 72 //Report anything else
andrewboyson 10:f0854784e960 73 default:
andrewboyson 52:fbc5a46b5e16 74 if (UdpTrace)
andrewboyson 52:fbc5a46b5e16 75 {
andrewboyson 52:fbc5a46b5e16 76 LogTimeF("UDP unknown port %d\r\n", dstPort);
andrewboyson 52:fbc5a46b5e16 77 traceback(); //This will already include the UDP header
andrewboyson 52:fbc5a46b5e16 78 }
andrewboyson 10:f0854784e960 79 return DO_NOTHING;
andrewboyson 10:f0854784e960 80 }
andrewboyson 10:f0854784e960 81 }
andrewboyson 59:e0e556c8bd46 82 int UdpHandleReceivedPacket(void (*traceback)(void), int sizeRx, void* pPacketRx, int* pSizeTx, void* pPacketTx)
andrewboyson 10:f0854784e960 83 {
andrewboyson 59:e0e556c8bd46 84 readHeader(pPacketRx, sizeRx);
andrewboyson 59:e0e556c8bd46 85
andrewboyson 59:e0e556c8bd46 86 void* pDataRx = (char*)pPacketRx + sizeof(header);
andrewboyson 59:e0e556c8bd46 87 void* pDataTx = (char*)pPacketTx + sizeof(header);
andrewboyson 59:e0e556c8bd46 88 int dataLengthRx = sizeRx - sizeof(header);
andrewboyson 59:e0e556c8bd46 89 int dataLengthTx = *pSizeTx - sizeof(header);
andrewboyson 10:f0854784e960 90
andrewboyson 59:e0e556c8bd46 91 int action = handlePort(traceback, dataLengthRx, pDataRx, &dataLengthTx, pDataTx);
andrewboyson 10:f0854784e960 92
andrewboyson 59:e0e556c8bd46 93 *pSizeTx = dataLengthTx + sizeof(header);
andrewboyson 10:f0854784e960 94
andrewboyson 37:793b39683406 95 uint16_t tmpPort = dstPort;
andrewboyson 37:793b39683406 96 dstPort = srcPort;
andrewboyson 37:793b39683406 97 srcPort = tmpPort;
andrewboyson 10:f0854784e960 98
andrewboyson 10:f0854784e960 99 return action;
andrewboyson 10:f0854784e960 100 }
andrewboyson 10:f0854784e960 101 static int pollForPacketToSend(int type, int* pDataLength, void* pData)
andrewboyson 10:f0854784e960 102 {
andrewboyson 37:793b39683406 103 int action = DO_NOTHING;
andrewboyson 42:222a4f45f916 104 uint16_t preferredPolledIpType = NetPreferIp4Polled ? IPV4 : IPV6;
andrewboyson 10:f0854784e960 105
andrewboyson 10:f0854784e960 106 if (!action && type == IPV4) //DHCP only works under IPv4
andrewboyson 10:f0854784e960 107 {
andrewboyson 10:f0854784e960 108 action = DhcpPollForRequestToSend(pData, pDataLength);
andrewboyson 10:f0854784e960 109 if (action)
andrewboyson 10:f0854784e960 110 {
andrewboyson 37:793b39683406 111 srcPort = DHCP_CLIENT_PORT;
andrewboyson 37:793b39683406 112 dstPort = DHCP_SERVER_PORT;
andrewboyson 10:f0854784e960 113 }
andrewboyson 10:f0854784e960 114 }
andrewboyson 10:f0854784e960 115
andrewboyson 42:222a4f45f916 116 if (!action && type == preferredPolledIpType) //DNS is agnostic
andrewboyson 10:f0854784e960 117 {
andrewboyson 13:9cd54f7db57a 118 action = DnsPollForPacketToSend(pData, pDataLength);
andrewboyson 37:793b39683406 119 int dest = ActionGetDestPart(action);
andrewboyson 37:793b39683406 120 if (dest)
andrewboyson 10:f0854784e960 121 {
andrewboyson 37:793b39683406 122 switch (dest)
andrewboyson 10:f0854784e960 123 {
andrewboyson 37:793b39683406 124 case UNICAST_DNS: srcPort = DNS_UNICAST_CLIENT_PORT; dstPort = DNS_UNICAST_SERVER_PORT; break; //53053, 53
andrewboyson 37:793b39683406 125 case MULTICAST_MDNS: srcPort = DNS_MDNS_PORT; dstPort = DNS_MDNS_PORT; break; // 5353, 5353
andrewboyson 37:793b39683406 126 case MULTICAST_LLMNR: srcPort = DNS_LLMNR_CLIENT_PORT; dstPort = DNS_LLMNR_SERVER_PORT; break; //53055, 5355
andrewboyson 10:f0854784e960 127
andrewboyson 10:f0854784e960 128 //Report anything else
andrewboyson 10:f0854784e960 129 default:
andrewboyson 37:793b39683406 130 LogTimeF("DNS unknown dest %d\r\n", dest);
andrewboyson 10:f0854784e960 131 return DO_NOTHING;
andrewboyson 10:f0854784e960 132 }
andrewboyson 10:f0854784e960 133 }
andrewboyson 10:f0854784e960 134 }
andrewboyson 42:222a4f45f916 135 if (!action && type == preferredPolledIpType) //NTP is agnostic
andrewboyson 22:914b970356f0 136 {
andrewboyson 22:914b970356f0 137 action = NtpPollForPacketToSend(type, pData, pDataLength);
andrewboyson 22:914b970356f0 138 if (action)
andrewboyson 22:914b970356f0 139 {
andrewboyson 37:793b39683406 140 srcPort = NTP_PORT;
andrewboyson 37:793b39683406 141 dstPort = NTP_PORT;
andrewboyson 22:914b970356f0 142 }
andrewboyson 22:914b970356f0 143 }
andrewboyson 57:e0fb648acf48 144 if (!action && type == preferredPolledIpType) //TFTP is agnostic
andrewboyson 57:e0fb648acf48 145 {
andrewboyson 57:e0fb648acf48 146 action = TftpPollForPacketToSend(type, pData, pDataLength);
andrewboyson 57:e0fb648acf48 147 if (action)
andrewboyson 57:e0fb648acf48 148 {
andrewboyson 57:e0fb648acf48 149 srcPort = TFTP_CLIENT_PORT;
andrewboyson 57:e0fb648acf48 150 dstPort = TFTP_SERVER_PORT;
andrewboyson 57:e0fb648acf48 151 }
andrewboyson 57:e0fb648acf48 152 }
andrewboyson 37:793b39683406 153
andrewboyson 10:f0854784e960 154 return action;
andrewboyson 10:f0854784e960 155 }
andrewboyson 10:f0854784e960 156 int UdpPollForPacketToSend(int type, int* pSize, void* pPacket)
andrewboyson 10:f0854784e960 157 {
andrewboyson 59:e0e556c8bd46 158 void* pData = (char*)pPacket + sizeof(header);
andrewboyson 59:e0e556c8bd46 159 int dataLength = *pSize - sizeof(header);
andrewboyson 10:f0854784e960 160
andrewboyson 10:f0854784e960 161 int action = pollForPacketToSend(type, &dataLength, pData);
andrewboyson 10:f0854784e960 162
andrewboyson 59:e0e556c8bd46 163 *pSize = dataLength + sizeof(header);
andrewboyson 10:f0854784e960 164 return action;
andrewboyson 10:f0854784e960 165 }
andrewboyson 10:f0854784e960 166
andrewboyson 37:793b39683406 167 void UdpLogHeader(uint16_t calculatedChecksum)
andrewboyson 43:bc028d5a6424 168 {
andrewboyson 43:bc028d5a6424 169 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 170 {
andrewboyson 43:bc028d5a6424 171 Log ("UDP header\r\n");
andrewboyson 43:bc028d5a6424 172 LogF(" Source port %hu\r\n", srcPort);
andrewboyson 43:bc028d5a6424 173 LogF(" Destination port %hu\r\n", dstPort);
andrewboyson 43:bc028d5a6424 174 LogF(" Total length %hu\r\n", totalLength);
andrewboyson 43:bc028d5a6424 175 LogF(" Checksum (hex) %04hX\r\n", checksum);
andrewboyson 43:bc028d5a6424 176 LogF(" Calculated %04hX\r\n", calculatedChecksum);
andrewboyson 43:bc028d5a6424 177 }
andrewboyson 43:bc028d5a6424 178 else
andrewboyson 43:bc028d5a6424 179 {
andrewboyson 44:83ce5ace337b 180 LogF("UDP header %hu >>> %hu\r\n", srcPort, dstPort);
andrewboyson 43:bc028d5a6424 181 }
andrewboyson 10:f0854784e960 182 }
andrewboyson 10:f0854784e960 183
andrewboyson 10:f0854784e960 184 void UdpMakeHeader(int size, void* pPacket)
andrewboyson 10:f0854784e960 185 {
andrewboyson 10:f0854784e960 186 struct header* pHeader = (header*)pPacket;
andrewboyson 10:f0854784e960 187
andrewboyson 37:793b39683406 188 pHeader->dstPort = NetToHost16(dstPort);
andrewboyson 37:793b39683406 189 pHeader->srcPort = NetToHost16(srcPort);
andrewboyson 10:f0854784e960 190 pHeader->totalLength = NetToHost16(size);
andrewboyson 10:f0854784e960 191 pHeader->checksum = 0;
andrewboyson 10:f0854784e960 192
andrewboyson 10:f0854784e960 193 }
andrewboyson 10:f0854784e960 194 void UdpAddChecksum(void* pPacket, uint16_t checksum)
andrewboyson 10:f0854784e960 195 {
andrewboyson 10:f0854784e960 196 struct header* pHeader = (header*)pPacket;
andrewboyson 10:f0854784e960 197 pHeader->checksum = checksum;
andrewboyson 10:f0854784e960 198 }