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:
Wed Dec 23 09:52:47 2020 +0000
Revision:
173:9bc30cd82a76
Parent:
172:9bc3c7b2cca1
Child:
195:bd5b123143ca
Fixed problem with decoding 32 bit numbers in ndp

Who changed what in which revision?

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