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
ip4/udptcp4.cpp@10:f0854784e960, 2017-04-16 (annotated)
- Committer:
- andrewboyson
- Date:
- Sun Apr 16 14:21:55 2017 +0000
- Revision:
- 10:f0854784e960
- Child:
- 11:c051adb70c5a
MDNS and LLMNR now working.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 10:f0854784e960 | 1 | #include "mbed.h" |
andrewboyson | 10:f0854784e960 | 2 | #include "log.h" |
andrewboyson | 10:f0854784e960 | 3 | #include "net.h" |
andrewboyson | 10:f0854784e960 | 4 | #include "dhcp.h" |
andrewboyson | 10:f0854784e960 | 5 | #include "ip4.h" |
andrewboyson | 10:f0854784e960 | 6 | #include "tcp.h" |
andrewboyson | 10:f0854784e960 | 7 | #include "udp.h" |
andrewboyson | 10:f0854784e960 | 8 | |
andrewboyson | 10:f0854784e960 | 9 | #define DEBUG false |
andrewboyson | 10:f0854784e960 | 10 | |
andrewboyson | 10:f0854784e960 | 11 | static uint16_t calculateChecksum(uint8_t pro, uint32_t srcIp, uint32_t dstIp, int size, void* pPacket) |
andrewboyson | 10:f0854784e960 | 12 | { |
andrewboyson | 10:f0854784e960 | 13 | __packed struct pseudo |
andrewboyson | 10:f0854784e960 | 14 | { |
andrewboyson | 10:f0854784e960 | 15 | uint32_t src; |
andrewboyson | 10:f0854784e960 | 16 | uint32_t dst; |
andrewboyson | 10:f0854784e960 | 17 | uint8_t zer; |
andrewboyson | 10:f0854784e960 | 18 | uint8_t pro; |
andrewboyson | 10:f0854784e960 | 19 | uint16_t len; |
andrewboyson | 10:f0854784e960 | 20 | } pseudo; |
andrewboyson | 10:f0854784e960 | 21 | |
andrewboyson | 10:f0854784e960 | 22 | pseudo.src = srcIp; |
andrewboyson | 10:f0854784e960 | 23 | pseudo.dst = dstIp; |
andrewboyson | 10:f0854784e960 | 24 | pseudo.zer = 0; |
andrewboyson | 10:f0854784e960 | 25 | pseudo.pro = pro; |
andrewboyson | 10:f0854784e960 | 26 | pseudo.len = NetToHost16(size); |
andrewboyson | 10:f0854784e960 | 27 | |
andrewboyson | 10:f0854784e960 | 28 | return NetCheckSumTwo(sizeof(pseudo), &pseudo, size, pPacket); |
andrewboyson | 10:f0854784e960 | 29 | } |
andrewboyson | 10:f0854784e960 | 30 | static void destIpFromAction(int action, uint32_t* pDstIp) |
andrewboyson | 10:f0854784e960 | 31 | { |
andrewboyson | 10:f0854784e960 | 32 | switch (action) |
andrewboyson | 10:f0854784e960 | 33 | { |
andrewboyson | 10:f0854784e960 | 34 | case UNICAST: |
andrewboyson | 10:f0854784e960 | 35 | break; |
andrewboyson | 10:f0854784e960 | 36 | case UNICAST_DNS: |
andrewboyson | 10:f0854784e960 | 37 | *pDstIp = DhcpDnsServer; |
andrewboyson | 10:f0854784e960 | 38 | break; |
andrewboyson | 10:f0854784e960 | 39 | case UNICAST_DHCP: |
andrewboyson | 10:f0854784e960 | 40 | *pDstIp = DhcpServer; |
andrewboyson | 10:f0854784e960 | 41 | break; |
andrewboyson | 10:f0854784e960 | 42 | case MULTICAST_NODE: |
andrewboyson | 10:f0854784e960 | 43 | *pDstIp = IP4_MULTICAST_ALL_HOSTS; |
andrewboyson | 10:f0854784e960 | 44 | break; |
andrewboyson | 10:f0854784e960 | 45 | case MULTICAST_ROUTER: |
andrewboyson | 10:f0854784e960 | 46 | *pDstIp = IP4_MULTICAST_ALL_ROUTERS; |
andrewboyson | 10:f0854784e960 | 47 | break; |
andrewboyson | 10:f0854784e960 | 48 | case MULTICAST_MDNS: |
andrewboyson | 10:f0854784e960 | 49 | *pDstIp = IP4_MULTICAST_DNS_ADDRESS; |
andrewboyson | 10:f0854784e960 | 50 | break; |
andrewboyson | 10:f0854784e960 | 51 | case MULTICAST_LLMNR: |
andrewboyson | 10:f0854784e960 | 52 | *pDstIp = IP4_MULTICAST_LLMNR_ADDRESS; |
andrewboyson | 10:f0854784e960 | 53 | break; |
andrewboyson | 10:f0854784e960 | 54 | case BROADCAST: |
andrewboyson | 10:f0854784e960 | 55 | *pDstIp = IP4_BROADCAST_ADDRESS; |
andrewboyson | 10:f0854784e960 | 56 | break; |
andrewboyson | 10:f0854784e960 | 57 | default: |
andrewboyson | 10:f0854784e960 | 58 | LogTimeF("UdpTcp4 destIpFromAction unknown action %d\r\n", action); |
andrewboyson | 10:f0854784e960 | 59 | return; |
andrewboyson | 10:f0854784e960 | 60 | } |
andrewboyson | 10:f0854784e960 | 61 | } |
andrewboyson | 10:f0854784e960 | 62 | static void finalisePacket(uint8_t pro, int action, void* pPacket, int size, uint32_t* pSrcIp, uint32_t* pDstIp) |
andrewboyson | 10:f0854784e960 | 63 | { |
andrewboyson | 10:f0854784e960 | 64 | if (!action) return; |
andrewboyson | 10:f0854784e960 | 65 | |
andrewboyson | 10:f0854784e960 | 66 | destIpFromAction(action, pDstIp); |
andrewboyson | 10:f0854784e960 | 67 | |
andrewboyson | 10:f0854784e960 | 68 | *pSrcIp = DhcpLocalIp; |
andrewboyson | 10:f0854784e960 | 69 | |
andrewboyson | 10:f0854784e960 | 70 | switch (pro) |
andrewboyson | 10:f0854784e960 | 71 | { |
andrewboyson | 10:f0854784e960 | 72 | case TCP: TcpMakeHeader(size, pPacket); break; |
andrewboyson | 10:f0854784e960 | 73 | case UDP: UdpMakeHeader(size, pPacket); break; |
andrewboyson | 10:f0854784e960 | 74 | } |
andrewboyson | 10:f0854784e960 | 75 | |
andrewboyson | 10:f0854784e960 | 76 | uint16_t checksum = calculateChecksum(pro, *pSrcIp, *pDstIp, size, pPacket); |
andrewboyson | 10:f0854784e960 | 77 | |
andrewboyson | 10:f0854784e960 | 78 | switch (pro) |
andrewboyson | 10:f0854784e960 | 79 | { |
andrewboyson | 10:f0854784e960 | 80 | case TCP: TcpAddChecksum(pPacket, checksum); break; |
andrewboyson | 10:f0854784e960 | 81 | case UDP: UdpAddChecksum(pPacket, checksum); break; |
andrewboyson | 10:f0854784e960 | 82 | } |
andrewboyson | 10:f0854784e960 | 83 | |
andrewboyson | 10:f0854784e960 | 84 | if (DEBUG) TcpLogHeader("IPv4 packet sent", pPacket, 0); |
andrewboyson | 10:f0854784e960 | 85 | } |
andrewboyson | 10:f0854784e960 | 86 | |
andrewboyson | 10:f0854784e960 | 87 | int Tcp4HandleReceivedPacket(uint32_t* pSrcIp, uint32_t* pDstIp, int* pSize, void * pPacket) |
andrewboyson | 10:f0854784e960 | 88 | { |
andrewboyson | 10:f0854784e960 | 89 | uint16_t calculatedChecksum = calculateChecksum(TCP, *pSrcIp, *pDstIp, *pSize, pPacket); |
andrewboyson | 10:f0854784e960 | 90 | if (DEBUG) TcpLogHeader("IPv4 packet received", pPacket, calculatedChecksum); |
andrewboyson | 10:f0854784e960 | 91 | |
andrewboyson | 10:f0854784e960 | 92 | TcpReadHeader(pPacket, *pSize); |
andrewboyson | 10:f0854784e960 | 93 | |
andrewboyson | 10:f0854784e960 | 94 | int action = TcpHandleReceivedPacket(pSize, pPacket); |
andrewboyson | 10:f0854784e960 | 95 | |
andrewboyson | 10:f0854784e960 | 96 | *pDstIp = *pSrcIp; |
andrewboyson | 10:f0854784e960 | 97 | |
andrewboyson | 10:f0854784e960 | 98 | finalisePacket(TCP, action, pPacket, *pSize, pSrcIp, pDstIp); |
andrewboyson | 10:f0854784e960 | 99 | |
andrewboyson | 10:f0854784e960 | 100 | return action; |
andrewboyson | 10:f0854784e960 | 101 | } |
andrewboyson | 10:f0854784e960 | 102 | |
andrewboyson | 10:f0854784e960 | 103 | int Udp4HandleReceivedPacket(uint32_t* pSrcIp, uint32_t* pDstIp, int* pSize, void * pPacket) |
andrewboyson | 10:f0854784e960 | 104 | { |
andrewboyson | 10:f0854784e960 | 105 | uint16_t calculatedChecksum = calculateChecksum(UDP, *pSrcIp, *pDstIp, *pSize, pPacket); |
andrewboyson | 10:f0854784e960 | 106 | if (DEBUG) UdpLogHeader("IPv4 packet received", pPacket, calculatedChecksum); |
andrewboyson | 10:f0854784e960 | 107 | |
andrewboyson | 10:f0854784e960 | 108 | UdpReadHeader(pPacket, *pSize); |
andrewboyson | 10:f0854784e960 | 109 | |
andrewboyson | 10:f0854784e960 | 110 | int action = UdpHandleReceivedPacket(pSize, pPacket); |
andrewboyson | 10:f0854784e960 | 111 | |
andrewboyson | 10:f0854784e960 | 112 | *pDstIp = *pSrcIp; |
andrewboyson | 10:f0854784e960 | 113 | |
andrewboyson | 10:f0854784e960 | 114 | finalisePacket(UDP, action, pPacket, *pSize, pSrcIp, pDstIp); //Note that the ports are reversed here |
andrewboyson | 10:f0854784e960 | 115 | |
andrewboyson | 10:f0854784e960 | 116 | return action; |
andrewboyson | 10:f0854784e960 | 117 | } |
andrewboyson | 10:f0854784e960 | 118 | int Udp4PollForPacketToSend(void* pPacket, int* pSize, uint32_t* pSrcIp, uint32_t* pDstIp) |
andrewboyson | 10:f0854784e960 | 119 | { |
andrewboyson | 10:f0854784e960 | 120 | int action = UdpPollForPacketToSend(IPV4, pSize, pPacket); |
andrewboyson | 10:f0854784e960 | 121 | |
andrewboyson | 10:f0854784e960 | 122 | finalisePacket(UDP, action, pPacket, *pSize, pSrcIp, pDstIp); |
andrewboyson | 10:f0854784e960 | 123 | |
andrewboyson | 10:f0854784e960 | 124 | return action; |
andrewboyson | 10:f0854784e960 | 125 | } |