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.c@136:8a65abb0dc63, 2019-04-06 (annotated)
- Committer:
- andrewboyson
- Date:
- Sat Apr 06 11:20:20 2019 +0000
- Revision:
- 136:8a65abb0dc63
- Parent:
- 121:bc048b65a630
- Child:
- 137:cf6e7db0e985
Part way through replacing __packed with byte copy access to the headers.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdint.h> |
andrewboyson | 61:aad055f1b0d1 | 2 | |
andrewboyson | 97:d91f7db00235 | 3 | #include "log.h" |
andrewboyson | 97:d91f7db00235 | 4 | #include "net.h" |
andrewboyson | 97:d91f7db00235 | 5 | #include "action.h" |
andrewboyson | 97:d91f7db00235 | 6 | #include "dhcp.h" |
andrewboyson | 97:d91f7db00235 | 7 | #include "eth.h" |
andrewboyson | 97:d91f7db00235 | 8 | #include "ip.h" |
andrewboyson | 49:1a6336f2b3f9 | 9 | #include "ip4addr.h" |
andrewboyson | 74:c3756bfa960e | 10 | #include "tcp.h" |
andrewboyson | 74:c3756bfa960e | 11 | #include "tcprecv.h" |
andrewboyson | 74:c3756bfa960e | 12 | #include "tcpsend.h" |
andrewboyson | 74:c3756bfa960e | 13 | #include "tcphdr.h" |
andrewboyson | 74:c3756bfa960e | 14 | #include "udp.h" |
andrewboyson | 86:55bc5ddac16c | 15 | #include "ar4.h" |
andrewboyson | 86:55bc5ddac16c | 16 | #include "led.h" |
andrewboyson | 97:d91f7db00235 | 17 | #include "fault.h" |
andrewboyson | 136:8a65abb0dc63 | 18 | #include "checksum.h" |
andrewboyson | 10:f0854784e960 | 19 | |
andrewboyson | 10:f0854784e960 | 20 | static uint16_t calculateChecksum(uint8_t pro, uint32_t srcIp, uint32_t dstIp, int size, void* pPacket) |
andrewboyson | 10:f0854784e960 | 21 | { |
andrewboyson | 136:8a65abb0dc63 | 22 | uint32_t sum = 0; |
andrewboyson | 136:8a65abb0dc63 | 23 | uint16_t pro16 = pro; |
andrewboyson | 136:8a65abb0dc63 | 24 | sum = CheckSumAddDirect(sum, 4, &srcIp ); |
andrewboyson | 136:8a65abb0dc63 | 25 | sum = CheckSumAddDirect(sum, 4, &dstIp ); |
andrewboyson | 136:8a65abb0dc63 | 26 | sum = CheckSumAddInvert(sum, 2, &pro16 ); |
andrewboyson | 136:8a65abb0dc63 | 27 | sum = CheckSumAddInvert(sum, 2, &size ); |
andrewboyson | 136:8a65abb0dc63 | 28 | return CheckSumFinDirect(sum, 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 | 49:1a6336f2b3f9 | 34 | Ip4AddressFromDest(ActionGetDestPart(action), pDstIp); |
andrewboyson | 10:f0854784e960 | 35 | *pSrcIp = DhcpLocalIp; |
andrewboyson | 10:f0854784e960 | 36 | |
andrewboyson | 10:f0854784e960 | 37 | switch (pro) |
andrewboyson | 10:f0854784e960 | 38 | { |
andrewboyson | 74:c3756bfa960e | 39 | case TCP: TcpHdrWriteToPacket(pPacket); break; |
andrewboyson | 10:f0854784e960 | 40 | case UDP: UdpMakeHeader(size, pPacket); break; |
andrewboyson | 10:f0854784e960 | 41 | } |
andrewboyson | 10:f0854784e960 | 42 | |
andrewboyson | 10:f0854784e960 | 43 | uint16_t checksum = calculateChecksum(pro, *pSrcIp, *pDstIp, size, pPacket); |
andrewboyson | 10:f0854784e960 | 44 | |
andrewboyson | 10:f0854784e960 | 45 | switch (pro) |
andrewboyson | 10:f0854784e960 | 46 | { |
andrewboyson | 74:c3756bfa960e | 47 | case TCP: TcpHdrAddChecksumToPacket(pPacket, checksum); break; |
andrewboyson | 10:f0854784e960 | 48 | case UDP: UdpAddChecksum(pPacket, checksum); break; |
andrewboyson | 10:f0854784e960 | 49 | } |
andrewboyson | 10:f0854784e960 | 50 | |
andrewboyson | 37:793b39683406 | 51 | if (ActionGetTracePart(action)) |
andrewboyson | 11:c051adb70c5a | 52 | { |
andrewboyson | 37:793b39683406 | 53 | switch (pro) |
andrewboyson | 37:793b39683406 | 54 | { |
andrewboyson | 73:43e3d7fb3d60 | 55 | case TCP: TcpHdrLog(0); break; |
andrewboyson | 56:35117a8b5c65 | 56 | case UDP: UdpLogHeader(0); break; |
andrewboyson | 37:793b39683406 | 57 | } |
andrewboyson | 11:c051adb70c5a | 58 | } |
andrewboyson | 10:f0854784e960 | 59 | } |
andrewboyson | 10:f0854784e960 | 60 | |
andrewboyson | 37:793b39683406 | 61 | static void (*pTraceBack)(void); |
andrewboyson | 37:793b39683406 | 62 | static int tracePacketProtocol; |
andrewboyson | 37:793b39683406 | 63 | static uint16_t calculatedChecksum; |
andrewboyson | 37:793b39683406 | 64 | static void trace() |
andrewboyson | 37:793b39683406 | 65 | { |
andrewboyson | 37:793b39683406 | 66 | pTraceBack(); |
andrewboyson | 37:793b39683406 | 67 | switch(tracePacketProtocol) |
andrewboyson | 37:793b39683406 | 68 | { |
andrewboyson | 56:35117a8b5c65 | 69 | case UDP: UdpLogHeader(calculatedChecksum); break; |
andrewboyson | 73:43e3d7fb3d60 | 70 | case TCP: TcpHdrLog(calculatedChecksum); break; |
andrewboyson | 37:793b39683406 | 71 | default: LogTimeF("UdpTcp4 - traceback unrecognised protocol %d\r\n", tracePacketProtocol); break; |
andrewboyson | 37:793b39683406 | 72 | } |
andrewboyson | 37:793b39683406 | 73 | } |
andrewboyson | 74:c3756bfa960e | 74 | int Tcp4HandleReceivedPacket(void (*traceback)(void), void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, uint32_t* pSrcIp, uint32_t* pDstIp, int remArIndex) |
andrewboyson | 97:d91f7db00235 | 75 | { |
andrewboyson | 98:b977424ec7f7 | 76 | int lastFaultPoint = FaultPoint; |
andrewboyson | 97:d91f7db00235 | 77 | FaultPoint = FAULT_POINT_Tcp4HandleReceivedPacket; |
andrewboyson | 97:d91f7db00235 | 78 | |
andrewboyson | 37:793b39683406 | 79 | pTraceBack = traceback; |
andrewboyson | 37:793b39683406 | 80 | tracePacketProtocol = TCP; |
andrewboyson | 59:e0e556c8bd46 | 81 | calculatedChecksum = calculateChecksum(TCP, *pSrcIp, *pDstIp, sizeRx, pPacketRx); |
andrewboyson | 10:f0854784e960 | 82 | |
andrewboyson | 80:4ef1500fca1d | 83 | int action = TcpHandleReceivedPacket(trace, sizeRx, pPacketRx, pSizeTx, pPacketTx, IPV4, remArIndex, 0); |
andrewboyson | 10:f0854784e960 | 84 | |
andrewboyson | 10:f0854784e960 | 85 | *pDstIp = *pSrcIp; |
andrewboyson | 10:f0854784e960 | 86 | |
andrewboyson | 59:e0e556c8bd46 | 87 | finalisePacket(TCP, action, pPacketTx, *pSizeTx, pSrcIp, pDstIp); |
andrewboyson | 10:f0854784e960 | 88 | |
andrewboyson | 98:b977424ec7f7 | 89 | FaultPoint = lastFaultPoint; |
andrewboyson | 10:f0854784e960 | 90 | return action; |
andrewboyson | 10:f0854784e960 | 91 | } |
andrewboyson | 10:f0854784e960 | 92 | |
andrewboyson | 59:e0e556c8bd46 | 93 | int Udp4HandleReceivedPacket(void (*traceback)(void), void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, uint32_t* pSrcIp, uint32_t* pDstIp) |
andrewboyson | 10:f0854784e960 | 94 | { |
andrewboyson | 121:bc048b65a630 | 95 | int lastFaultPoint = FaultPoint; |
andrewboyson | 121:bc048b65a630 | 96 | FaultPoint = FAULT_POINT_Udp4HandleReceivedPacket; |
andrewboyson | 121:bc048b65a630 | 97 | |
andrewboyson | 37:793b39683406 | 98 | pTraceBack = traceback; |
andrewboyson | 37:793b39683406 | 99 | tracePacketProtocol = UDP; |
andrewboyson | 59:e0e556c8bd46 | 100 | calculatedChecksum = calculateChecksum(UDP, *pSrcIp, *pDstIp, sizeRx, pPacketRx); |
andrewboyson | 59:e0e556c8bd46 | 101 | |
andrewboyson | 59:e0e556c8bd46 | 102 | int action = UdpHandleReceivedPacket(trace, sizeRx, pPacketRx, pSizeTx, pPacketTx); |
andrewboyson | 10:f0854784e960 | 103 | |
andrewboyson | 10:f0854784e960 | 104 | *pDstIp = *pSrcIp; |
andrewboyson | 10:f0854784e960 | 105 | |
andrewboyson | 59:e0e556c8bd46 | 106 | finalisePacket(UDP, action, pPacketTx, *pSizeTx, pSrcIp, pDstIp); //Note that the ports are reversed here |
andrewboyson | 86:55bc5ddac16c | 107 | |
andrewboyson | 121:bc048b65a630 | 108 | FaultPoint = lastFaultPoint; |
andrewboyson | 10:f0854784e960 | 109 | return action; |
andrewboyson | 10:f0854784e960 | 110 | } |
andrewboyson | 71:736a5747ade1 | 111 | int Tcp4PollForPacketToSend(void* pPacket, int* pSize, uint32_t* pSrcIp, uint32_t* pDstIp) |
andrewboyson | 71:736a5747ade1 | 112 | { |
andrewboyson | 74:c3756bfa960e | 113 | int remArIndex = -1; |
andrewboyson | 80:4ef1500fca1d | 114 | int action = TcpPollForPacketToSend(pSize, pPacket, IPV4, &remArIndex, NULL); |
andrewboyson | 74:c3756bfa960e | 115 | if (action && remArIndex >= 0) *pDstIp = Ar4IndexToIp(remArIndex); |
andrewboyson | 71:736a5747ade1 | 116 | |
andrewboyson | 71:736a5747ade1 | 117 | finalisePacket(TCP, action, pPacket, *pSize, pSrcIp, pDstIp); |
andrewboyson | 71:736a5747ade1 | 118 | |
andrewboyson | 71:736a5747ade1 | 119 | return action; |
andrewboyson | 71:736a5747ade1 | 120 | } |
andrewboyson | 10:f0854784e960 | 121 | int Udp4PollForPacketToSend(void* pPacket, int* pSize, uint32_t* pSrcIp, uint32_t* pDstIp) |
andrewboyson | 10:f0854784e960 | 122 | { |
andrewboyson | 10:f0854784e960 | 123 | int action = UdpPollForPacketToSend(IPV4, pSize, pPacket); |
andrewboyson | 10:f0854784e960 | 124 | |
andrewboyson | 10:f0854784e960 | 125 | finalisePacket(UDP, action, pPacket, *pSize, pSrcIp, pDstIp); |
andrewboyson | 10:f0854784e960 | 126 | |
andrewboyson | 10:f0854784e960 | 127 | return action; |
andrewboyson | 10:f0854784e960 | 128 | } |