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:
Thu May 20 14:32:52 2021 +0000
Revision:
200:5acbc41bf469
Parent:
136:8a65abb0dc63
Increased number of arp entries from 20 to 30 to accommodate the number of WIZ devices plus a few incoming port 80 calls from the internet.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 2
andrewboyson 43:bc028d5a6424 3 #include "log.h"
andrewboyson 43:bc028d5a6424 4 #include "net.h"
andrewboyson 43:bc028d5a6424 5 #include "action.h"
andrewboyson 43:bc028d5a6424 6 #include "ip4.h"
andrewboyson 43:bc028d5a6424 7 #include "dhcp.h"
andrewboyson 43:bc028d5a6424 8 #include "echo4.h"
andrewboyson 136:8a65abb0dc63 9 #include "checksum.h"
andrewboyson 43:bc028d5a6424 10
andrewboyson 43:bc028d5a6424 11 #define ECHO_REPLY 0
andrewboyson 43:bc028d5a6424 12 #define UNREACHABLE 3
andrewboyson 43:bc028d5a6424 13 #define REDIRECT 5
andrewboyson 43:bc028d5a6424 14 #define ECHO_REQUEST 8
andrewboyson 43:bc028d5a6424 15
andrewboyson 136:8a65abb0dc63 16 static char* hdrTypePtr (char* pPacket) { return pPacket + 0; } //1
andrewboyson 136:8a65abb0dc63 17 static char* hdrCodePtr (char* pPacket) { return pPacket + 1; } //1
andrewboyson 136:8a65abb0dc63 18 static char* hdrChecksumPtr(char* pPacket) { return pPacket + 2; } //2
andrewboyson 136:8a65abb0dc63 19 #define HEADER_LENGTH 4
andrewboyson 136:8a65abb0dc63 20
andrewboyson 136:8a65abb0dc63 21 static uint8_t hdrTypeGet (char* pPacket) { return *hdrTypePtr (pPacket); }
andrewboyson 136:8a65abb0dc63 22 static uint8_t hdrCodeGet (char* pPacket) { return *hdrCodePtr (pPacket); }
andrewboyson 136:8a65abb0dc63 23 static uint16_t hdrChecksumGet(char* pPacket) { uint16_t r; NetDirect16(&r, hdrChecksumPtr(pPacket)); return r; } //Don't invert the checksum
andrewboyson 136:8a65abb0dc63 24
andrewboyson 136:8a65abb0dc63 25 static void hdrTypeSet (char* pPacket, uint8_t value) { *hdrTypePtr (pPacket) = value; }
andrewboyson 136:8a65abb0dc63 26 static void hdrCodeSet (char* pPacket, uint8_t value) { *hdrCodePtr (pPacket) = value; }
andrewboyson 136:8a65abb0dc63 27 static void hdrChecksumSet(char* pPacket, uint16_t value) { NetDirect16(hdrChecksumPtr(pPacket), &value); } //Don't invert the checksum
andrewboyson 43:bc028d5a6424 28
andrewboyson 47:73af5c0b0dc2 29 static void logType(uint16_t type)
andrewboyson 43:bc028d5a6424 30 {
andrewboyson 43:bc028d5a6424 31 switch (type)
andrewboyson 43:bc028d5a6424 32 {
andrewboyson 47:73af5c0b0dc2 33 case ECHO_REPLY: Log ("Echo Reply" ); break;
andrewboyson 47:73af5c0b0dc2 34 case ECHO_REQUEST: Log ("Echo Request" ); break;
andrewboyson 47:73af5c0b0dc2 35 default: LogF("Unknown type %d", type); break;
andrewboyson 43:bc028d5a6424 36 }
andrewboyson 43:bc028d5a6424 37 }
andrewboyson 136:8a65abb0dc63 38 static void logHeader(int size, char* pPacket)
andrewboyson 47:73af5c0b0dc2 39 {
andrewboyson 43:bc028d5a6424 40 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 41 {
andrewboyson 43:bc028d5a6424 42 Log ("ICMP4 header\r\n");
andrewboyson 136:8a65abb0dc63 43 LogF(" Type "); logType(hdrTypeGet (pPacket)); Log("\r\n");
andrewboyson 136:8a65abb0dc63 44 LogF(" Code %u\r\n", hdrCodeGet (pPacket));
andrewboyson 136:8a65abb0dc63 45 LogF(" Checksum (hex) %04hX\r\n", hdrChecksumGet(pPacket));
andrewboyson 136:8a65abb0dc63 46 LogF(" Calculated %04hX\r\n", CheckSum(size, pPacket));
andrewboyson 43:bc028d5a6424 47 }
andrewboyson 43:bc028d5a6424 48 else
andrewboyson 43:bc028d5a6424 49 {
andrewboyson 43:bc028d5a6424 50 Log("ICMP4 header ");
andrewboyson 136:8a65abb0dc63 51 logType(hdrTypeGet(pPacket));
andrewboyson 43:bc028d5a6424 52 Log("\r\n");
andrewboyson 43:bc028d5a6424 53 }
andrewboyson 43:bc028d5a6424 54 }
andrewboyson 136:8a65abb0dc63 55 static int traceSize = 0;
andrewboyson 136:8a65abb0dc63 56 static char* tracePacket;
andrewboyson 43:bc028d5a6424 57 static void (*pTraceBack)(void);
andrewboyson 43:bc028d5a6424 58 static void trace()
andrewboyson 43:bc028d5a6424 59 {
andrewboyson 43:bc028d5a6424 60 pTraceBack();
andrewboyson 136:8a65abb0dc63 61 logHeader(traceSize, tracePacket);
andrewboyson 43:bc028d5a6424 62 }
andrewboyson 136:8a65abb0dc63 63 int Icmp4HandleReceivedPacket(void (*traceback)(void), char* pPacketRx, int sizeRx, char* pPacketTx, int* pSizeTx, uint32_t* pSrcIp, uint32_t* pDstIp)
andrewboyson 43:bc028d5a6424 64 {
andrewboyson 43:bc028d5a6424 65 pTraceBack = traceback;
andrewboyson 136:8a65abb0dc63 66 tracePacket = pPacketRx;
andrewboyson 136:8a65abb0dc63 67 traceSize = sizeRx;
andrewboyson 43:bc028d5a6424 68
andrewboyson 136:8a65abb0dc63 69 uint8_t type = hdrTypeGet(pPacketRx);
andrewboyson 136:8a65abb0dc63 70 uint8_t code = hdrCodeGet(pPacketRx);
andrewboyson 59:e0e556c8bd46 71
andrewboyson 136:8a65abb0dc63 72 int dataLengthRx = sizeRx - HEADER_LENGTH;
andrewboyson 136:8a65abb0dc63 73 int dataLengthTx = *pSizeTx - HEADER_LENGTH;
andrewboyson 136:8a65abb0dc63 74 char* pPayloadRx = (char*)pPacketRx + HEADER_LENGTH;
andrewboyson 136:8a65abb0dc63 75 char* pPayloadTx = (char*)pPacketTx + HEADER_LENGTH;
andrewboyson 43:bc028d5a6424 76
andrewboyson 43:bc028d5a6424 77 int action = DO_NOTHING;
andrewboyson 43:bc028d5a6424 78 switch (type)
andrewboyson 43:bc028d5a6424 79 {
andrewboyson 43:bc028d5a6424 80 case ECHO_REQUEST:
andrewboyson 59:e0e556c8bd46 81 action = Echo4HandleRequest(trace, &type, &code, pPayloadRx, dataLengthRx, pPayloadTx, &dataLengthTx);
andrewboyson 43:bc028d5a6424 82 break;
andrewboyson 43:bc028d5a6424 83 case UNREACHABLE:
andrewboyson 43:bc028d5a6424 84 return DO_NOTHING;
andrewboyson 43:bc028d5a6424 85 case REDIRECT:
andrewboyson 43:bc028d5a6424 86 return DO_NOTHING;
andrewboyson 43:bc028d5a6424 87 default:
andrewboyson 43:bc028d5a6424 88 LogTimeF("ICMP4 packet type %d unknown\r\n", type);
andrewboyson 43:bc028d5a6424 89 return DO_NOTHING;
andrewboyson 43:bc028d5a6424 90 }
andrewboyson 43:bc028d5a6424 91 if (!action) return DO_NOTHING;
andrewboyson 43:bc028d5a6424 92
andrewboyson 43:bc028d5a6424 93 *pDstIp = *pSrcIp;
andrewboyson 43:bc028d5a6424 94 *pSrcIp = DhcpLocalIp;
andrewboyson 43:bc028d5a6424 95
andrewboyson 136:8a65abb0dc63 96 *pSizeTx = HEADER_LENGTH + dataLengthTx;
andrewboyson 43:bc028d5a6424 97
andrewboyson 136:8a65abb0dc63 98 hdrTypeSet (pPacketTx, type );
andrewboyson 136:8a65abb0dc63 99 hdrCodeSet (pPacketTx, code );
andrewboyson 136:8a65abb0dc63 100 hdrChecksumSet(pPacketTx, 0 );
andrewboyson 136:8a65abb0dc63 101 uint16_t checksum = CheckSum(*pSizeTx, pPacketTx);
andrewboyson 136:8a65abb0dc63 102 hdrChecksumSet(pPacketTx, checksum);
andrewboyson 43:bc028d5a6424 103
andrewboyson 136:8a65abb0dc63 104 if (ActionGetTracePart(action)) logHeader(*pSizeTx, pPacketTx);
andrewboyson 43:bc028d5a6424 105
andrewboyson 43:bc028d5a6424 106 return action;
andrewboyson 43:bc028d5a6424 107 }