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 Apr 18 19:07:05 2017 +0000
Revision:
11:c051adb70c5a
Parent:
10:f0854784e960
Child:
15:6ca6778168b1
Tidied IPv6 code

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 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 finalisePacket(uint8_t pro, int action, void* pPacket, int size, uint32_t* pSrcIp, uint32_t* pDstIp)
andrewboyson 10:f0854784e960 31 {
andrewboyson 10:f0854784e960 32 if (!action) return;
andrewboyson 10:f0854784e960 33
andrewboyson 11:c051adb70c5a 34 Ip4DestIpFromAction(action, pDstIp);
andrewboyson 10:f0854784e960 35
andrewboyson 10:f0854784e960 36 *pSrcIp = DhcpLocalIp;
andrewboyson 10:f0854784e960 37
andrewboyson 10:f0854784e960 38 switch (pro)
andrewboyson 10:f0854784e960 39 {
andrewboyson 10:f0854784e960 40 case TCP: TcpMakeHeader(size, pPacket); break;
andrewboyson 10:f0854784e960 41 case UDP: UdpMakeHeader(size, pPacket); break;
andrewboyson 10:f0854784e960 42 }
andrewboyson 10:f0854784e960 43
andrewboyson 10:f0854784e960 44 uint16_t checksum = calculateChecksum(pro, *pSrcIp, *pDstIp, size, pPacket);
andrewboyson 10:f0854784e960 45
andrewboyson 10:f0854784e960 46 switch (pro)
andrewboyson 10:f0854784e960 47 {
andrewboyson 10:f0854784e960 48 case TCP: TcpAddChecksum(pPacket, checksum); break;
andrewboyson 10:f0854784e960 49 case UDP: UdpAddChecksum(pPacket, checksum); break;
andrewboyson 10:f0854784e960 50 }
andrewboyson 10:f0854784e960 51
andrewboyson 11:c051adb70c5a 52 switch (pro)
andrewboyson 11:c051adb70c5a 53 {
andrewboyson 11:c051adb70c5a 54 case TCP: if (DEBUG)TcpLogHeader("IPv4 packet sent", pPacket, 0); break;
andrewboyson 11:c051adb70c5a 55 case UDP: if (DEBUG)UdpLogHeader("IPv4 packet sent", pPacket, 0); break;
andrewboyson 11:c051adb70c5a 56 }
andrewboyson 10:f0854784e960 57 }
andrewboyson 10:f0854784e960 58
andrewboyson 10:f0854784e960 59 int Tcp4HandleReceivedPacket(uint32_t* pSrcIp, uint32_t* pDstIp, int* pSize, void * pPacket)
andrewboyson 10:f0854784e960 60 {
andrewboyson 10:f0854784e960 61 uint16_t calculatedChecksum = calculateChecksum(TCP, *pSrcIp, *pDstIp, *pSize, pPacket);
andrewboyson 10:f0854784e960 62 if (DEBUG) TcpLogHeader("IPv4 packet received", pPacket, calculatedChecksum);
andrewboyson 10:f0854784e960 63
andrewboyson 10:f0854784e960 64 TcpReadHeader(pPacket, *pSize);
andrewboyson 10:f0854784e960 65
andrewboyson 10:f0854784e960 66 int action = TcpHandleReceivedPacket(pSize, pPacket);
andrewboyson 10:f0854784e960 67
andrewboyson 10:f0854784e960 68 *pDstIp = *pSrcIp;
andrewboyson 10:f0854784e960 69
andrewboyson 10:f0854784e960 70 finalisePacket(TCP, action, pPacket, *pSize, pSrcIp, pDstIp);
andrewboyson 10:f0854784e960 71
andrewboyson 10:f0854784e960 72 return action;
andrewboyson 10:f0854784e960 73 }
andrewboyson 10:f0854784e960 74
andrewboyson 10:f0854784e960 75 int Udp4HandleReceivedPacket(uint32_t* pSrcIp, uint32_t* pDstIp, int* pSize, void * pPacket)
andrewboyson 10:f0854784e960 76 {
andrewboyson 10:f0854784e960 77 uint16_t calculatedChecksum = calculateChecksum(UDP, *pSrcIp, *pDstIp, *pSize, pPacket);
andrewboyson 10:f0854784e960 78 if (DEBUG) UdpLogHeader("IPv4 packet received", pPacket, calculatedChecksum);
andrewboyson 10:f0854784e960 79
andrewboyson 10:f0854784e960 80 UdpReadHeader(pPacket, *pSize);
andrewboyson 10:f0854784e960 81
andrewboyson 10:f0854784e960 82 int action = UdpHandleReceivedPacket(pSize, pPacket);
andrewboyson 10:f0854784e960 83
andrewboyson 10:f0854784e960 84 *pDstIp = *pSrcIp;
andrewboyson 10:f0854784e960 85
andrewboyson 10:f0854784e960 86 finalisePacket(UDP, action, pPacket, *pSize, pSrcIp, pDstIp); //Note that the ports are reversed here
andrewboyson 10:f0854784e960 87
andrewboyson 10:f0854784e960 88 return action;
andrewboyson 10:f0854784e960 89 }
andrewboyson 10:f0854784e960 90 int Udp4PollForPacketToSend(void* pPacket, int* pSize, uint32_t* pSrcIp, uint32_t* pDstIp)
andrewboyson 10:f0854784e960 91 {
andrewboyson 10:f0854784e960 92 int action = UdpPollForPacketToSend(IPV4, pSize, pPacket);
andrewboyson 10:f0854784e960 93
andrewboyson 10:f0854784e960 94 finalisePacket(UDP, action, pPacket, *pSize, pSrcIp, pDstIp);
andrewboyson 10:f0854784e960 95
andrewboyson 10:f0854784e960 96 return action;
andrewboyson 10:f0854784e960 97 }