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:
Tue May 09 15:39:03 2017 +0000
Revision:
15:6ca6778168b1
Parent:
11:c051adb70c5a
Child:
37:793b39683406
Tidied up dns debug output

Who changed what in which revision?

UserRevisionLine numberNew 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 15:6ca6778168b1 5 #include "eth.h"
andrewboyson 15:6ca6778168b1 6 #include "ip.h"
andrewboyson 10:f0854784e960 7 #include "ip4.h"
andrewboyson 10:f0854784e960 8 #include "tcp.h"
andrewboyson 10:f0854784e960 9 #include "udp.h"
andrewboyson 10:f0854784e960 10
andrewboyson 10:f0854784e960 11 #define DEBUG false
andrewboyson 10:f0854784e960 12
andrewboyson 10:f0854784e960 13 static uint16_t calculateChecksum(uint8_t pro, uint32_t srcIp, uint32_t dstIp, int size, void* pPacket)
andrewboyson 10:f0854784e960 14 {
andrewboyson 10:f0854784e960 15 __packed struct pseudo
andrewboyson 10:f0854784e960 16 {
andrewboyson 10:f0854784e960 17 uint32_t src;
andrewboyson 10:f0854784e960 18 uint32_t dst;
andrewboyson 10:f0854784e960 19 uint8_t zer;
andrewboyson 10:f0854784e960 20 uint8_t pro;
andrewboyson 10:f0854784e960 21 uint16_t len;
andrewboyson 10:f0854784e960 22 } pseudo;
andrewboyson 10:f0854784e960 23
andrewboyson 10:f0854784e960 24 pseudo.src = srcIp;
andrewboyson 10:f0854784e960 25 pseudo.dst = dstIp;
andrewboyson 10:f0854784e960 26 pseudo.zer = 0;
andrewboyson 10:f0854784e960 27 pseudo.pro = pro;
andrewboyson 10:f0854784e960 28 pseudo.len = NetToHost16(size);
andrewboyson 10:f0854784e960 29
andrewboyson 10:f0854784e960 30 return NetCheckSumTwo(sizeof(pseudo), &pseudo, size, pPacket);
andrewboyson 10:f0854784e960 31 }
andrewboyson 10:f0854784e960 32 static void finalisePacket(uint8_t pro, int action, void* pPacket, int size, uint32_t* pSrcIp, uint32_t* pDstIp)
andrewboyson 10:f0854784e960 33 {
andrewboyson 10:f0854784e960 34 if (!action) return;
andrewboyson 10:f0854784e960 35
andrewboyson 11:c051adb70c5a 36 Ip4DestIpFromAction(action, pDstIp);
andrewboyson 10:f0854784e960 37
andrewboyson 10:f0854784e960 38 *pSrcIp = DhcpLocalIp;
andrewboyson 10:f0854784e960 39
andrewboyson 10:f0854784e960 40 switch (pro)
andrewboyson 10:f0854784e960 41 {
andrewboyson 10:f0854784e960 42 case TCP: TcpMakeHeader(size, pPacket); break;
andrewboyson 10:f0854784e960 43 case UDP: UdpMakeHeader(size, pPacket); break;
andrewboyson 10:f0854784e960 44 }
andrewboyson 10:f0854784e960 45
andrewboyson 10:f0854784e960 46 uint16_t checksum = calculateChecksum(pro, *pSrcIp, *pDstIp, size, pPacket);
andrewboyson 10:f0854784e960 47
andrewboyson 10:f0854784e960 48 switch (pro)
andrewboyson 10:f0854784e960 49 {
andrewboyson 10:f0854784e960 50 case TCP: TcpAddChecksum(pPacket, checksum); break;
andrewboyson 10:f0854784e960 51 case UDP: UdpAddChecksum(pPacket, checksum); break;
andrewboyson 10:f0854784e960 52 }
andrewboyson 10:f0854784e960 53
andrewboyson 11:c051adb70c5a 54 switch (pro)
andrewboyson 11:c051adb70c5a 55 {
andrewboyson 11:c051adb70c5a 56 case TCP: if (DEBUG)TcpLogHeader("IPv4 packet sent", pPacket, 0); break;
andrewboyson 11:c051adb70c5a 57 case UDP: if (DEBUG)UdpLogHeader("IPv4 packet sent", pPacket, 0); break;
andrewboyson 11:c051adb70c5a 58 }
andrewboyson 10:f0854784e960 59 }
andrewboyson 10:f0854784e960 60
andrewboyson 10:f0854784e960 61 int Tcp4HandleReceivedPacket(uint32_t* pSrcIp, uint32_t* pDstIp, int* pSize, void * pPacket)
andrewboyson 10:f0854784e960 62 {
andrewboyson 10:f0854784e960 63 uint16_t calculatedChecksum = calculateChecksum(TCP, *pSrcIp, *pDstIp, *pSize, pPacket);
andrewboyson 10:f0854784e960 64 if (DEBUG) TcpLogHeader("IPv4 packet received", pPacket, calculatedChecksum);
andrewboyson 10:f0854784e960 65
andrewboyson 10:f0854784e960 66 TcpReadHeader(pPacket, *pSize);
andrewboyson 10:f0854784e960 67
andrewboyson 10:f0854784e960 68 int action = TcpHandleReceivedPacket(pSize, pPacket);
andrewboyson 10:f0854784e960 69
andrewboyson 10:f0854784e960 70 *pDstIp = *pSrcIp;
andrewboyson 10:f0854784e960 71
andrewboyson 10:f0854784e960 72 finalisePacket(TCP, action, pPacket, *pSize, pSrcIp, pDstIp);
andrewboyson 10:f0854784e960 73
andrewboyson 10:f0854784e960 74 return action;
andrewboyson 10:f0854784e960 75 }
andrewboyson 10:f0854784e960 76
andrewboyson 10:f0854784e960 77 int Udp4HandleReceivedPacket(uint32_t* pSrcIp, uint32_t* pDstIp, int* pSize, void * pPacket)
andrewboyson 10:f0854784e960 78 {
andrewboyson 10:f0854784e960 79 uint16_t calculatedChecksum = calculateChecksum(UDP, *pSrcIp, *pDstIp, *pSize, pPacket);
andrewboyson 10:f0854784e960 80 if (DEBUG) UdpLogHeader("IPv4 packet received", pPacket, calculatedChecksum);
andrewboyson 10:f0854784e960 81
andrewboyson 10:f0854784e960 82 UdpReadHeader(pPacket, *pSize);
andrewboyson 10:f0854784e960 83
andrewboyson 10:f0854784e960 84 int action = UdpHandleReceivedPacket(pSize, pPacket);
andrewboyson 10:f0854784e960 85
andrewboyson 10:f0854784e960 86 *pDstIp = *pSrcIp;
andrewboyson 10:f0854784e960 87
andrewboyson 10:f0854784e960 88 finalisePacket(UDP, action, pPacket, *pSize, pSrcIp, pDstIp); //Note that the ports are reversed here
andrewboyson 10:f0854784e960 89
andrewboyson 10:f0854784e960 90 return action;
andrewboyson 10:f0854784e960 91 }
andrewboyson 10:f0854784e960 92 int Udp4PollForPacketToSend(void* pPacket, int* pSize, uint32_t* pSrcIp, uint32_t* pDstIp)
andrewboyson 10:f0854784e960 93 {
andrewboyson 10:f0854784e960 94 int action = UdpPollForPacketToSend(IPV4, pSize, pPacket);
andrewboyson 10:f0854784e960 95
andrewboyson 10:f0854784e960 96 finalisePacket(UDP, action, pPacket, *pSize, pSrcIp, pDstIp);
andrewboyson 10:f0854784e960 97
andrewboyson 10:f0854784e960 98 return action;
andrewboyson 10:f0854784e960 99 }