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.cpp@37:793b39683406, 2017-10-04 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Oct 04 07:51:02 2017 +0000
- Revision:
- 37:793b39683406
- Parent:
- 33:714a0345e59b
- Child:
- 42:222a4f45f916
Added trace back and trace forward to log messages
Who changed what in which revision?
User | Revision | Line number | New 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 | 37:793b39683406 | 7 | #include "dhcp.h" |
andrewboyson | 37:793b39683406 | 8 | #include "dns.h" |
andrewboyson | 37:793b39683406 | 9 | #include "eth.h" |
andrewboyson | 37:793b39683406 | 10 | #include "ip4.h" |
andrewboyson | 37:793b39683406 | 11 | #include "ip6.h" |
andrewboyson | 37:793b39683406 | 12 | #include "slaac.h" |
andrewboyson | 37:793b39683406 | 13 | #include "ns.h" |
andrewboyson | 37:793b39683406 | 14 | #include "io.h" |
andrewboyson | 10:f0854784e960 | 15 | |
andrewboyson | 15:6ca6778168b1 | 16 | #define UNKNOWN true |
andrewboyson | 10:f0854784e960 | 17 | |
andrewboyson | 10:f0854784e960 | 18 | #define HEADER_SIZE 8 |
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 | 37:793b39683406 | 31 | static int handlePort(void (*traceback)(void), int* pDataLength, void* pData) |
andrewboyson | 10:f0854784e960 | 32 | { |
andrewboyson | 37:793b39683406 | 33 | switch (dstPort) |
andrewboyson | 10:f0854784e960 | 34 | { |
andrewboyson | 10:f0854784e960 | 35 | //Handle these |
andrewboyson | 37:793b39683406 | 36 | case DHCP_CLIENT_PORT: return DhcpHandleResponse (traceback, pDataLength, pData); // 68 |
andrewboyson | 37:793b39683406 | 37 | case NTP_PORT: return NtpHandlePacketReceived(traceback, pDataLength, pData); // 123 |
andrewboyson | 37:793b39683406 | 38 | case DNS_UNICAST_CLIENT_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_UDNS, pDataLength, pData); //53053 |
andrewboyson | 37:793b39683406 | 39 | case DNS_MDNS_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_MDNS, pDataLength, pData); // 5353 |
andrewboyson | 37:793b39683406 | 40 | case DNS_LLMNR_CLIENT_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_LLMNR, pDataLength, pData); //53055 |
andrewboyson | 37:793b39683406 | 41 | case DNS_LLMNR_SERVER_PORT: return DnsHandlePacketReceived(traceback, DNS_PROTOCOL_LLMNR, pDataLength, pData); // 5355 |
andrewboyson | 10:f0854784e960 | 42 | |
andrewboyson | 10:f0854784e960 | 43 | //Quietly drop these |
andrewboyson | 10:f0854784e960 | 44 | case DHCP_SERVER_PORT: //67 |
andrewboyson | 15:6ca6778168b1 | 45 | case 137: //NETBIOS name service |
andrewboyson | 15:6ca6778168b1 | 46 | case 138: //NETBIOS datagram service |
andrewboyson | 15:6ca6778168b1 | 47 | case 139: //NETBIOS session service |
andrewboyson | 10:f0854784e960 | 48 | case 1900: //SSDP Simple Service Discovery Protocol (uPnP) |
andrewboyson | 10:f0854784e960 | 49 | case 3076: //Call of Duty - Xbox |
andrewboyson | 33:714a0345e59b | 50 | 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 | 15:6ca6778168b1 | 51 | case 9956: //Alljoyn part af Allseen IoT services |
andrewboyson | 10:f0854784e960 | 52 | case 9997: //VLC |
andrewboyson | 10:f0854784e960 | 53 | case 9998: //VLC |
andrewboyson | 10:f0854784e960 | 54 | case 9999: //VLC |
andrewboyson | 10:f0854784e960 | 55 | case 17500: //Dropbox LAN sync |
andrewboyson | 10:f0854784e960 | 56 | return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 57 | |
andrewboyson | 10:f0854784e960 | 58 | //Report anything else |
andrewboyson | 10:f0854784e960 | 59 | default: |
andrewboyson | 37:793b39683406 | 60 | if (UNKNOWN) LogTimeF("UDP unknown port %d\r\n", dstPort); |
andrewboyson | 10:f0854784e960 | 61 | return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 62 | } |
andrewboyson | 10:f0854784e960 | 63 | } |
andrewboyson | 37:793b39683406 | 64 | int UdpHandleReceivedPacket(void (*traceback)(void), int* pSize, void* pPacket) |
andrewboyson | 10:f0854784e960 | 65 | { |
andrewboyson | 10:f0854784e960 | 66 | void* pData = (char*)pPacket + HEADER_SIZE; |
andrewboyson | 10:f0854784e960 | 67 | int dataLength = *pSize - HEADER_SIZE; |
andrewboyson | 10:f0854784e960 | 68 | |
andrewboyson | 37:793b39683406 | 69 | int action = handlePort(traceback, &dataLength, pData); |
andrewboyson | 10:f0854784e960 | 70 | |
andrewboyson | 10:f0854784e960 | 71 | *pSize = dataLength + HEADER_SIZE; |
andrewboyson | 10:f0854784e960 | 72 | |
andrewboyson | 37:793b39683406 | 73 | uint16_t tmpPort = dstPort; |
andrewboyson | 37:793b39683406 | 74 | dstPort = srcPort; |
andrewboyson | 37:793b39683406 | 75 | srcPort = tmpPort; |
andrewboyson | 10:f0854784e960 | 76 | |
andrewboyson | 10:f0854784e960 | 77 | return action; |
andrewboyson | 10:f0854784e960 | 78 | } |
andrewboyson | 10:f0854784e960 | 79 | static int pollForPacketToSend(int type, int* pDataLength, void* pData) |
andrewboyson | 10:f0854784e960 | 80 | { |
andrewboyson | 37:793b39683406 | 81 | int action = DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 82 | |
andrewboyson | 10:f0854784e960 | 83 | if (!action && type == IPV4) //DHCP only works under IPv4 |
andrewboyson | 10:f0854784e960 | 84 | { |
andrewboyson | 10:f0854784e960 | 85 | action = DhcpPollForRequestToSend(pData, pDataLength); |
andrewboyson | 10:f0854784e960 | 86 | if (action) |
andrewboyson | 10:f0854784e960 | 87 | { |
andrewboyson | 37:793b39683406 | 88 | srcPort = DHCP_CLIENT_PORT; |
andrewboyson | 37:793b39683406 | 89 | dstPort = DHCP_SERVER_PORT; |
andrewboyson | 10:f0854784e960 | 90 | } |
andrewboyson | 10:f0854784e960 | 91 | } |
andrewboyson | 10:f0854784e960 | 92 | |
andrewboyson | 10:f0854784e960 | 93 | if (!action) //DNS is agnostic |
andrewboyson | 10:f0854784e960 | 94 | { |
andrewboyson | 13:9cd54f7db57a | 95 | action = DnsPollForPacketToSend(pData, pDataLength); |
andrewboyson | 37:793b39683406 | 96 | int dest = ActionGetDestPart(action); |
andrewboyson | 37:793b39683406 | 97 | if (dest) |
andrewboyson | 10:f0854784e960 | 98 | { |
andrewboyson | 37:793b39683406 | 99 | switch (dest) |
andrewboyson | 10:f0854784e960 | 100 | { |
andrewboyson | 37:793b39683406 | 101 | case UNICAST_DNS: srcPort = DNS_UNICAST_CLIENT_PORT; dstPort = DNS_UNICAST_SERVER_PORT; break; //53053, 53 |
andrewboyson | 37:793b39683406 | 102 | case MULTICAST_MDNS: srcPort = DNS_MDNS_PORT; dstPort = DNS_MDNS_PORT; break; // 5353, 5353 |
andrewboyson | 37:793b39683406 | 103 | case MULTICAST_LLMNR: srcPort = DNS_LLMNR_CLIENT_PORT; dstPort = DNS_LLMNR_SERVER_PORT; break; //53055, 5355 |
andrewboyson | 10:f0854784e960 | 104 | |
andrewboyson | 10:f0854784e960 | 105 | //Report anything else |
andrewboyson | 10:f0854784e960 | 106 | default: |
andrewboyson | 37:793b39683406 | 107 | LogTimeF("DNS unknown dest %d\r\n", dest); |
andrewboyson | 10:f0854784e960 | 108 | return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 109 | } |
andrewboyson | 10:f0854784e960 | 110 | } |
andrewboyson | 10:f0854784e960 | 111 | } |
andrewboyson | 22:914b970356f0 | 112 | if (!action) //NTP needs to choose depending of what address is specified |
andrewboyson | 22:914b970356f0 | 113 | { |
andrewboyson | 22:914b970356f0 | 114 | action = NtpPollForPacketToSend(type, pData, pDataLength); |
andrewboyson | 22:914b970356f0 | 115 | if (action) |
andrewboyson | 22:914b970356f0 | 116 | { |
andrewboyson | 37:793b39683406 | 117 | srcPort = NTP_PORT; |
andrewboyson | 37:793b39683406 | 118 | dstPort = NTP_PORT; |
andrewboyson | 22:914b970356f0 | 119 | } |
andrewboyson | 22:914b970356f0 | 120 | } |
andrewboyson | 37:793b39683406 | 121 | |
andrewboyson | 10:f0854784e960 | 122 | return action; |
andrewboyson | 10:f0854784e960 | 123 | } |
andrewboyson | 10:f0854784e960 | 124 | int UdpPollForPacketToSend(int type, int* pSize, void* pPacket) |
andrewboyson | 10:f0854784e960 | 125 | { |
andrewboyson | 10:f0854784e960 | 126 | void* pData = (char*)pPacket + HEADER_SIZE; |
andrewboyson | 10:f0854784e960 | 127 | int dataLength = *pSize - HEADER_SIZE; |
andrewboyson | 10:f0854784e960 | 128 | |
andrewboyson | 10:f0854784e960 | 129 | int action = pollForPacketToSend(type, &dataLength, pData); |
andrewboyson | 10:f0854784e960 | 130 | |
andrewboyson | 10:f0854784e960 | 131 | *pSize = dataLength + HEADER_SIZE; |
andrewboyson | 10:f0854784e960 | 132 | return action; |
andrewboyson | 10:f0854784e960 | 133 | } |
andrewboyson | 10:f0854784e960 | 134 | |
andrewboyson | 37:793b39683406 | 135 | void UdpLogHeader(uint16_t calculatedChecksum) |
andrewboyson | 10:f0854784e960 | 136 | { |
andrewboyson | 37:793b39683406 | 137 | Log ("UDP header\r\n"); |
andrewboyson | 37:793b39683406 | 138 | LogF(" Source port %hu\r\n", srcPort); |
andrewboyson | 37:793b39683406 | 139 | LogF(" Destination port %hu\r\n", dstPort); |
andrewboyson | 10:f0854784e960 | 140 | LogF(" Total length %hu\r\n", totalLength); |
andrewboyson | 10:f0854784e960 | 141 | LogF(" Checksum (hex) %04hX\r\n", checksum); |
andrewboyson | 10:f0854784e960 | 142 | LogF(" Calculated %04hX\r\n", calculatedChecksum); |
andrewboyson | 10:f0854784e960 | 143 | } |
andrewboyson | 10:f0854784e960 | 144 | |
andrewboyson | 10:f0854784e960 | 145 | void UdpMakeHeader(int size, void* pPacket) |
andrewboyson | 10:f0854784e960 | 146 | { |
andrewboyson | 10:f0854784e960 | 147 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 148 | |
andrewboyson | 37:793b39683406 | 149 | pHeader->dstPort = NetToHost16(dstPort); |
andrewboyson | 37:793b39683406 | 150 | pHeader->srcPort = NetToHost16(srcPort); |
andrewboyson | 10:f0854784e960 | 151 | pHeader->totalLength = NetToHost16(size); |
andrewboyson | 10:f0854784e960 | 152 | pHeader->checksum = 0; |
andrewboyson | 10:f0854784e960 | 153 | |
andrewboyson | 10:f0854784e960 | 154 | } |
andrewboyson | 10:f0854784e960 | 155 | void UdpAddChecksum(void* pPacket, uint16_t checksum) |
andrewboyson | 10:f0854784e960 | 156 | { |
andrewboyson | 10:f0854784e960 | 157 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 158 | pHeader->checksum = checksum; |
andrewboyson | 10:f0854784e960 | 159 | } |
andrewboyson | 10:f0854784e960 | 160 | void UdpReadHeader(void* pPacket, uint16_t size) |
andrewboyson | 10:f0854784e960 | 161 | { |
andrewboyson | 10:f0854784e960 | 162 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 163 | |
andrewboyson | 37:793b39683406 | 164 | srcPort = NetToHost16(pHeader->srcPort); |
andrewboyson | 37:793b39683406 | 165 | dstPort = NetToHost16(pHeader->dstPort); |
andrewboyson | 10:f0854784e960 | 166 | totalLength = NetToHost16(pHeader->totalLength); |
andrewboyson | 10:f0854784e960 | 167 | checksum = NetToHost16(pHeader->checksum); |
andrewboyson | 10:f0854784e960 | 168 | } |