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/icmp/ndp/rs.cpp@46:40d33e9037e4, 2017-10-24 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Oct 24 07:01:35 2017 +0000
- Revision:
- 46:40d33e9037e4
- Child:
- 47:73af5c0b0dc2
Tidied up the RA module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 46:40d33e9037e4 | 1 | #include "mbed.h" |
andrewboyson | 46:40d33e9037e4 | 2 | #include "log.h" |
andrewboyson | 46:40d33e9037e4 | 3 | #include "net.h" |
andrewboyson | 46:40d33e9037e4 | 4 | #include "ndp.h" |
andrewboyson | 46:40d33e9037e4 | 5 | #include "ip6.h" |
andrewboyson | 46:40d33e9037e4 | 6 | #include "slaac.h" |
andrewboyson | 46:40d33e9037e4 | 7 | #include "clock.h" |
andrewboyson | 46:40d33e9037e4 | 8 | #include "action.h" |
andrewboyson | 46:40d33e9037e4 | 9 | #include "mac.h" |
andrewboyson | 46:40d33e9037e4 | 10 | |
andrewboyson | 46:40d33e9037e4 | 11 | bool RsTrace = false; |
andrewboyson | 46:40d33e9037e4 | 12 | |
andrewboyson | 46:40d33e9037e4 | 13 | bool go = false; |
andrewboyson | 46:40d33e9037e4 | 14 | |
andrewboyson | 46:40d33e9037e4 | 15 | #define REPEAT_DELAY_TIME 60 |
andrewboyson | 46:40d33e9037e4 | 16 | static uint32_t delayTime = 10000000; //Reset whenever a message is sent and blocks another send until count exceeds REPEAT_DELAY_TIME |
andrewboyson | 46:40d33e9037e4 | 17 | |
andrewboyson | 46:40d33e9037e4 | 18 | int RsGetWaitingSolicitation(void* pPacket, int* pSize, uint8_t* pType, uint8_t* pCode) |
andrewboyson | 46:40d33e9037e4 | 19 | { |
andrewboyson | 46:40d33e9037e4 | 20 | if (!go) return DO_NOTHING; |
andrewboyson | 46:40d33e9037e4 | 21 | go = false; |
andrewboyson | 46:40d33e9037e4 | 22 | |
andrewboyson | 46:40d33e9037e4 | 23 | __packed struct header |
andrewboyson | 46:40d33e9037e4 | 24 | { |
andrewboyson | 46:40d33e9037e4 | 25 | uint32_t reserved; |
andrewboyson | 46:40d33e9037e4 | 26 | }; |
andrewboyson | 46:40d33e9037e4 | 27 | struct header* pHeader = (header*)pPacket; |
andrewboyson | 46:40d33e9037e4 | 28 | pHeader->reserved = 0; |
andrewboyson | 46:40d33e9037e4 | 29 | *pSize = sizeof(struct header); |
andrewboyson | 46:40d33e9037e4 | 30 | *pType = 133; //Router solicitation |
andrewboyson | 46:40d33e9037e4 | 31 | *pCode = 0; |
andrewboyson | 46:40d33e9037e4 | 32 | |
andrewboyson | 46:40d33e9037e4 | 33 | char* pData = (char*)pHeader + sizeof(struct header); |
andrewboyson | 46:40d33e9037e4 | 34 | char* p = pData; |
andrewboyson | 46:40d33e9037e4 | 35 | |
andrewboyson | 46:40d33e9037e4 | 36 | p += NdpAddOptionSourceMac(p, MacLocal); |
andrewboyson | 46:40d33e9037e4 | 37 | |
andrewboyson | 46:40d33e9037e4 | 38 | *pSize = sizeof(struct header) + p - pData; |
andrewboyson | 46:40d33e9037e4 | 39 | |
andrewboyson | 46:40d33e9037e4 | 40 | if (RsTrace) |
andrewboyson | 46:40d33e9037e4 | 41 | { |
andrewboyson | 46:40d33e9037e4 | 42 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 46:40d33e9037e4 | 43 | LogTime("NDP send router solicit\r\n"); |
andrewboyson | 46:40d33e9037e4 | 44 | if (NetTraceVerbose) |
andrewboyson | 46:40d33e9037e4 | 45 | { |
andrewboyson | 46:40d33e9037e4 | 46 | Log("RS header\r\n"); |
andrewboyson | 46:40d33e9037e4 | 47 | NdpLogOptions(pData, *pSize); |
andrewboyson | 46:40d33e9037e4 | 48 | } |
andrewboyson | 46:40d33e9037e4 | 49 | else |
andrewboyson | 46:40d33e9037e4 | 50 | { |
andrewboyson | 46:40d33e9037e4 | 51 | Log("RS header\r\n"); |
andrewboyson | 46:40d33e9037e4 | 52 | } |
andrewboyson | 46:40d33e9037e4 | 53 | } |
andrewboyson | 46:40d33e9037e4 | 54 | |
andrewboyson | 46:40d33e9037e4 | 55 | return ActionMakeFromDestAndTrace(MULTICAST_ROUTER, RsTrace && NetTraceStack); |
andrewboyson | 46:40d33e9037e4 | 56 | |
andrewboyson | 46:40d33e9037e4 | 57 | } |
andrewboyson | 46:40d33e9037e4 | 58 | |
andrewboyson | 46:40d33e9037e4 | 59 | static void checkIfGo() |
andrewboyson | 46:40d33e9037e4 | 60 | { |
andrewboyson | 46:40d33e9037e4 | 61 | if (delayTime < REPEAT_DELAY_TIME) return; //Don't retry within the delay time |
andrewboyson | 46:40d33e9037e4 | 62 | |
andrewboyson | 46:40d33e9037e4 | 63 | if (NdpLifetime && NdpElapsedTime < (NdpLifetime >> 1)) return; //Do nothing if within half the life |
andrewboyson | 46:40d33e9037e4 | 64 | |
andrewboyson | 46:40d33e9037e4 | 65 | if (!NdpLifetime || NdpElapsedTime >= NdpLifetime) |
andrewboyson | 46:40d33e9037e4 | 66 | { |
andrewboyson | 46:40d33e9037e4 | 67 | if (RsTrace) |
andrewboyson | 46:40d33e9037e4 | 68 | { |
andrewboyson | 46:40d33e9037e4 | 69 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 46:40d33e9037e4 | 70 | LogTime("NDP lifetime has expired: clear global prefix\r\n"); |
andrewboyson | 46:40d33e9037e4 | 71 | } |
andrewboyson | 46:40d33e9037e4 | 72 | NdpLifetime = 0; |
andrewboyson | 46:40d33e9037e4 | 73 | Ip6Clear(NdpPrefix); |
andrewboyson | 46:40d33e9037e4 | 74 | SlaacMakeGlobal(NdpPrefix); |
andrewboyson | 46:40d33e9037e4 | 75 | Ip6Clear(NdpDnsServer); |
andrewboyson | 46:40d33e9037e4 | 76 | } |
andrewboyson | 46:40d33e9037e4 | 77 | |
andrewboyson | 46:40d33e9037e4 | 78 | delayTime = 0; |
andrewboyson | 46:40d33e9037e4 | 79 | go = true; |
andrewboyson | 46:40d33e9037e4 | 80 | } |
andrewboyson | 46:40d33e9037e4 | 81 | |
andrewboyson | 46:40d33e9037e4 | 82 | void RsMain() |
andrewboyson | 46:40d33e9037e4 | 83 | { |
andrewboyson | 46:40d33e9037e4 | 84 | if (ClockTicked) |
andrewboyson | 46:40d33e9037e4 | 85 | { |
andrewboyson | 46:40d33e9037e4 | 86 | NdpElapsedTime++; |
andrewboyson | 46:40d33e9037e4 | 87 | delayTime++; |
andrewboyson | 46:40d33e9037e4 | 88 | } |
andrewboyson | 46:40d33e9037e4 | 89 | checkIfGo(); |
andrewboyson | 46:40d33e9037e4 | 90 | } |