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:
Wed Mar 20 11:20:44 2019 +0000
Revision:
133:a37eb35a03f1
Parent:
132:db2174b36a6d
Child:
137:cf6e7db0e985
Updated clock library

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 47:73af5c0b0dc2 23 __packed struct header
andrewboyson 47:73af5c0b0dc2 24 {
andrewboyson 47:73af5c0b0dc2 25 uint32_t reserved;
andrewboyson 47:73af5c0b0dc2 26 };
andrewboyson 46:40d33e9037e4 27
andrewboyson 47:73af5c0b0dc2 28 static void logHeader(void* pPacket, int size)
andrewboyson 47:73af5c0b0dc2 29 {
andrewboyson 61:aad055f1b0d1 30 struct header* pHeader = (struct header*)pPacket;
andrewboyson 47:73af5c0b0dc2 31 char* pData = (char*)pHeader + sizeof(struct header);
andrewboyson 47:73af5c0b0dc2 32 int dataLength = size - sizeof(struct header);
andrewboyson 47:73af5c0b0dc2 33
andrewboyson 47:73af5c0b0dc2 34 if (NetTraceVerbose)
andrewboyson 47:73af5c0b0dc2 35 {
andrewboyson 47:73af5c0b0dc2 36 Log("RS header\r\n");
andrewboyson 47:73af5c0b0dc2 37 LogF(" Size %d\r\n", size);
andrewboyson 47:73af5c0b0dc2 38 NdpLogOptionsVerbose(pData, dataLength);
andrewboyson 47:73af5c0b0dc2 39 }
andrewboyson 47:73af5c0b0dc2 40 else
andrewboyson 47:73af5c0b0dc2 41 {
andrewboyson 47:73af5c0b0dc2 42 Log("RS header");
andrewboyson 47:73af5c0b0dc2 43 NdpLogOptionsQuiet(pData, dataLength);
andrewboyson 47:73af5c0b0dc2 44 Log("\r\n");
andrewboyson 47:73af5c0b0dc2 45 }
andrewboyson 47:73af5c0b0dc2 46 }
andrewboyson 46:40d33e9037e4 47 int RsGetWaitingSolicitation(void* pPacket, int* pSize, uint8_t* pType, uint8_t* pCode)
andrewboyson 46:40d33e9037e4 48 {
andrewboyson 119:8e1a7805b801 49 //Check if time to update
andrewboyson 119:8e1a7805b801 50 if (NdpIsFresh())
andrewboyson 119:8e1a7805b801 51 {
andrewboyson 119:8e1a7805b801 52 delayMs = MIN_REPEAT_DELAY_TIME_MS; //Set the delay time back to minimum
andrewboyson 119:8e1a7805b801 53 return DO_NOTHING;
andrewboyson 119:8e1a7805b801 54 }
andrewboyson 119:8e1a7805b801 55
andrewboyson 119:8e1a7805b801 56 //Limit retries with a backoff delay
andrewboyson 133:a37eb35a03f1 57 if (!MsTimerRelative(repeatDelayMsTimer, delayMs)) return DO_NOTHING; //Don't retry within the delay time
andrewboyson 119:8e1a7805b801 58 delayMs <<= 1; //Backoff (double) the delay time after each attempt
andrewboyson 119:8e1a7805b801 59 if (delayMs > MAX_REPEAT_DELAY_TIME_MS) delayMs = MAX_REPEAT_DELAY_TIME_MS; //Don't go beyond a maximum
andrewboyson 119:8e1a7805b801 60 repeatDelayMsTimer = MsTimerCount; //Start the delay timer
andrewboyson 119:8e1a7805b801 61
andrewboyson 119:8e1a7805b801 62 //Send the renewal request
andrewboyson 46:40d33e9037e4 63 *pType = 133; //Router solicitation
andrewboyson 46:40d33e9037e4 64 *pCode = 0;
andrewboyson 46:40d33e9037e4 65
andrewboyson 61:aad055f1b0d1 66 struct header* pHeader = (struct header*)pPacket;
andrewboyson 47:73af5c0b0dc2 67 pHeader->reserved = 0;
andrewboyson 47:73af5c0b0dc2 68
andrewboyson 46:40d33e9037e4 69 char* pData = (char*)pHeader + sizeof(struct header);
andrewboyson 46:40d33e9037e4 70 char* p = pData;
andrewboyson 46:40d33e9037e4 71 p += NdpAddOptionSourceMac(p, MacLocal);
andrewboyson 46:40d33e9037e4 72
andrewboyson 46:40d33e9037e4 73 *pSize = sizeof(struct header) + p - pData;
andrewboyson 46:40d33e9037e4 74
andrewboyson 46:40d33e9037e4 75 if (RsTrace)
andrewboyson 46:40d33e9037e4 76 {
andrewboyson 46:40d33e9037e4 77 if (NetTraceNewLine) Log("\r\n");
andrewboyson 46:40d33e9037e4 78 LogTime("NDP send router solicit\r\n");
andrewboyson 47:73af5c0b0dc2 79 logHeader(pPacket, *pSize);
andrewboyson 46:40d33e9037e4 80 }
andrewboyson 46:40d33e9037e4 81
andrewboyson 46:40d33e9037e4 82 return ActionMakeFromDestAndTrace(MULTICAST_ROUTER, RsTrace && NetTraceStack);
andrewboyson 46:40d33e9037e4 83
andrewboyson 46:40d33e9037e4 84 }