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