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
ip6/udptcp6.c
- Committer:
- andrewboyson
- Date:
- 2018-10-23
- Revision:
- 73:43e3d7fb3d60
- Parent:
- 71:736a5747ade1
- Child:
- 74:c3756bfa960e
File content as of revision 73:43e3d7fb3d60:
#include <stdint.h> #include <string.h> #include "log.h" #include "net.h" #include "action.h" #include "eth.h" #include "dhcp.h" #include "ns.h" #include "ip.h" #include "ip6addr.h" #include "slaac.h" #include "tcp.h" #include "udp.h" #include "tcphdr.h" static uint16_t calculateChecksum(uint8_t pro, char* pSrcIp, char* pDstIp, int size, void* pPacket) { __packed struct pseudo { char src[16]; char dst[16]; uint8_t zer; uint8_t pro; uint16_t len; } pseudo; memcpy(pseudo.src, pSrcIp, 16); memcpy(pseudo.dst, pDstIp, 16); pseudo.zer = 0; pseudo.pro = pro; pseudo.len = NetToHost16(size); return NetCheckSumTwo(sizeof(pseudo), &pseudo, size, pPacket); } static void finalisePacket(uint8_t pro, int action, int scope, void* pPacket, int size, char* pSrcIp, char* pDstIp) { if (!action) return; Ip6AddressFromDest (ActionGetDestPart(action), pDstIp); SlaacAddressFromScope(scope, pSrcIp); switch (pro) { case TCP: TcpHdrWrite(size, pPacket); break; case UDP: UdpMakeHeader(size, pPacket); break; } uint16_t checksum = calculateChecksum(pro, pSrcIp, pDstIp, size, pPacket); switch (pro) { case TCP: TcpHdrAddChecksum(pPacket, checksum); break; case UDP: UdpAddChecksum(pPacket, checksum); break; } if (ActionGetTracePart(action)) { switch (pro) { case TCP: TcpHdrLog(0); break; case UDP: UdpLogHeader(0); break; } } } static void (*pTraceBack)(void); static int tracePacketProtocol; static uint16_t calculatedChecksum; static void trace() { pTraceBack(); switch(tracePacketProtocol) { case UDP: UdpLogHeader(calculatedChecksum); break; case TCP: TcpHdrLog(calculatedChecksum); break; default: LogTimeF("UdpTcp6 - traceback unrecognised protocol %d\r\n", tracePacketProtocol); break; } } int Tcp6HandleReceivedPacket(void (*traceback)(void), int scope, void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, char* pSrcIp, char* pDstIp, char* pCachedRemIp) { pTraceBack = traceback; tracePacketProtocol = TCP; calculatedChecksum = calculateChecksum(TCP, pSrcIp, pDstIp, sizeRx, pPacketRx); int action = TcpHandleReceivedPacket(trace, sizeRx, pPacketRx, pSizeTx, pPacketTx, IPV6, pCachedRemIp); Ip6AddressCopy(pDstIp, pSrcIp); finalisePacket(TCP, action, scope, pPacketTx, *pSizeTx, pSrcIp, pDstIp); return action; } int Udp6HandleReceivedPacket(void (*traceback)(void), int scope, void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, char* pSrcIp, char* pDstIp) { pTraceBack = traceback; tracePacketProtocol = UDP; calculatedChecksum = calculateChecksum(UDP, pSrcIp, pDstIp, sizeRx, pPacketRx); int action = UdpHandleReceivedPacket(trace, sizeRx, pPacketRx, pSizeTx, pPacketTx); Ip6AddressCopy(pDstIp, pSrcIp); finalisePacket(UDP, action, scope, pPacketTx, *pSizeTx, pSrcIp, pDstIp); return action; } int Tcp6PollForPacketToSend(void* pPacket, int* pSize, char* pSrcIp, char* pDstIp) { int action = TcpPollForPacketToSend(pSize, pPacket, IPV6, pDstIp); int scope = SCOPE_LOCAL; finalisePacket(TCP, action, scope, pPacket, *pSize, pSrcIp, pDstIp); return action; } int Udp6PollForPacketToSend(void* pPacket, int* pSize, char* pSrcIp, char* pDstIp) { int action = UdpPollForPacketToSend(IPV6, pSize, pPacket); int scope = SCOPE_LOCAL; finalisePacket(UDP, action, scope, pPacket, *pSize, pSrcIp, pDstIp); return action; }