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 Apr 02 19:08:25 2020 +0000
Revision:
167:3ba4e3c49631
Parent:
137:cf6e7db0e985
Modified resolution cache ajaxs to include the index

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 2 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 3
andrewboyson 46:40d33e9037e4 4 #include "log.h"
andrewboyson 46:40d33e9037e4 5 #include "net.h"
andrewboyson 46:40d33e9037e4 6 #include "ndp.h"
andrewboyson 46:40d33e9037e4 7 #include "ip6.h"
andrewboyson 119:8e1a7805b801 8 #include "ip6addr.h"
andrewboyson 46:40d33e9037e4 9 #include "slaac.h"
andrewboyson 46:40d33e9037e4 10 #include "action.h"
andrewboyson 46:40d33e9037e4 11 #include "mac.h"
andrewboyson 119:8e1a7805b801 12 #include "mstimer.h"
andrewboyson 46:40d33e9037e4 13
andrewboyson 46:40d33e9037e4 14 bool RsTrace = false;
andrewboyson 46:40d33e9037e4 15
andrewboyson 47:73af5c0b0dc2 16 bool RsSendSolicitation = false;
andrewboyson 47:73af5c0b0dc2 17
andrewboyson 119:8e1a7805b801 18 #define MAX_REPEAT_DELAY_TIME_MS 60000
andrewboyson 119:8e1a7805b801 19 #define MIN_REPEAT_DELAY_TIME_MS 800
andrewboyson 119:8e1a7805b801 20 static uint32_t repeatDelayMsTimer = (uint32_t)-MIN_REPEAT_DELAY_TIME_MS; //Initial value ensures no delay at startup
andrewboyson 119:8e1a7805b801 21 static uint32_t delayMs = MIN_REPEAT_DELAY_TIME_MS; //Doubles on failure up to max; reset to min whenever an IP address request has been acknowledged
andrewboyson 119:8e1a7805b801 22
andrewboyson 137:cf6e7db0e985 23 static void hdrSetReserved(char* pPacket, uint32_t value) { NetInvert32(pPacket + 0, &value); }
andrewboyson 137:cf6e7db0e985 24 static const int HEADER_LENGTH = 4;
andrewboyson 46:40d33e9037e4 25
andrewboyson 137:cf6e7db0e985 26 static void logHeader(char* pPacket, int size)
andrewboyson 47:73af5c0b0dc2 27 {
andrewboyson 137:cf6e7db0e985 28 char* pData = pPacket + HEADER_LENGTH;
andrewboyson 137:cf6e7db0e985 29 int dataLength = size - HEADER_LENGTH;
andrewboyson 47:73af5c0b0dc2 30
andrewboyson 47:73af5c0b0dc2 31 if (NetTraceVerbose)
andrewboyson 47:73af5c0b0dc2 32 {
andrewboyson 47:73af5c0b0dc2 33 Log("RS header\r\n");
andrewboyson 47:73af5c0b0dc2 34 LogF(" Size %d\r\n", size);
andrewboyson 47:73af5c0b0dc2 35 NdpLogOptionsVerbose(pData, dataLength);
andrewboyson 47:73af5c0b0dc2 36 }
andrewboyson 47:73af5c0b0dc2 37 else
andrewboyson 47:73af5c0b0dc2 38 {
andrewboyson 47:73af5c0b0dc2 39 Log("RS header");
andrewboyson 47:73af5c0b0dc2 40 NdpLogOptionsQuiet(pData, dataLength);
andrewboyson 47:73af5c0b0dc2 41 Log("\r\n");
andrewboyson 47:73af5c0b0dc2 42 }
andrewboyson 47:73af5c0b0dc2 43 }
andrewboyson 137:cf6e7db0e985 44 int RsGetWaitingSolicitation(char* pPacket, int* pSize, uint8_t* pType, uint8_t* pCode)
andrewboyson 46:40d33e9037e4 45 {
andrewboyson 119:8e1a7805b801 46 //Check if time to update
andrewboyson 119:8e1a7805b801 47 if (NdpIsFresh())
andrewboyson 119:8e1a7805b801 48 {
andrewboyson 119:8e1a7805b801 49 delayMs = MIN_REPEAT_DELAY_TIME_MS; //Set the delay time back to minimum
andrewboyson 119:8e1a7805b801 50 return DO_NOTHING;
andrewboyson 119:8e1a7805b801 51 }
andrewboyson 119:8e1a7805b801 52
andrewboyson 119:8e1a7805b801 53 //Limit retries with a backoff delay
andrewboyson 133:a37eb35a03f1 54 if (!MsTimerRelative(repeatDelayMsTimer, delayMs)) return DO_NOTHING; //Don't retry within the delay time
andrewboyson 119:8e1a7805b801 55 delayMs <<= 1; //Backoff (double) the delay time after each attempt
andrewboyson 119:8e1a7805b801 56 if (delayMs > MAX_REPEAT_DELAY_TIME_MS) delayMs = MAX_REPEAT_DELAY_TIME_MS; //Don't go beyond a maximum
andrewboyson 119:8e1a7805b801 57 repeatDelayMsTimer = MsTimerCount; //Start the delay timer
andrewboyson 119:8e1a7805b801 58
andrewboyson 119:8e1a7805b801 59 //Send the renewal request
andrewboyson 46:40d33e9037e4 60 *pType = 133; //Router solicitation
andrewboyson 46:40d33e9037e4 61 *pCode = 0;
andrewboyson 46:40d33e9037e4 62
andrewboyson 137:cf6e7db0e985 63 hdrSetReserved(pPacket, 0);
andrewboyson 47:73af5c0b0dc2 64
andrewboyson 137:cf6e7db0e985 65 char* pData = (char*)pPacket + HEADER_LENGTH;
andrewboyson 46:40d33e9037e4 66 char* p = pData;
andrewboyson 46:40d33e9037e4 67 p += NdpAddOptionSourceMac(p, MacLocal);
andrewboyson 46:40d33e9037e4 68
andrewboyson 137:cf6e7db0e985 69 *pSize = HEADER_LENGTH + p - pData;
andrewboyson 46:40d33e9037e4 70
andrewboyson 46:40d33e9037e4 71 if (RsTrace)
andrewboyson 46:40d33e9037e4 72 {
andrewboyson 46:40d33e9037e4 73 if (NetTraceNewLine) Log("\r\n");
andrewboyson 46:40d33e9037e4 74 LogTime("NDP send router solicit\r\n");
andrewboyson 47:73af5c0b0dc2 75 logHeader(pPacket, *pSize);
andrewboyson 46:40d33e9037e4 76 }
andrewboyson 46:40d33e9037e4 77
andrewboyson 46:40d33e9037e4 78 return ActionMakeFromDestAndTrace(MULTICAST_ROUTER, RsTrace && NetTraceStack);
andrewboyson 46:40d33e9037e4 79
andrewboyson 46:40d33e9037e4 80 }