Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Fri Nov 03 18:14:06 2017 +0000
Revision:
51:007bd938f2c3
Parent:
49:1a6336f2b3f9
Child:
52:fbc5a46b5e16
Corrected address resolution http routine

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 46:40d33e9037e4 1 #include "mbed.h"
andrewboyson 46:40d33e9037e4 2 #include "log.h"
andrewboyson 47:73af5c0b0dc2 3 #include "net.h"
andrewboyson 46:40d33e9037e4 4 #include "mac.h"
andrewboyson 49:1a6336f2b3f9 5 #include "ip6addr.h"
andrewboyson 46:40d33e9037e4 6 #include "slaac.h"
andrewboyson 46:40d33e9037e4 7 #include "clock.h"
andrewboyson 46:40d33e9037e4 8 #include "rs.h"
andrewboyson 46:40d33e9037e4 9
andrewboyson 47:73af5c0b0dc2 10 int NdpHopLimit = 0;
andrewboyson 47:73af5c0b0dc2 11 bool NdpManagedConfiguration = false;
andrewboyson 47:73af5c0b0dc2 12 bool NdpOtherConfiguration = false;
andrewboyson 47:73af5c0b0dc2 13 int NdpLifetime = 0;
andrewboyson 46:40d33e9037e4 14
andrewboyson 46:40d33e9037e4 15 char NdpRouterMac[6];
andrewboyson 46:40d33e9037e4 16
andrewboyson 46:40d33e9037e4 17 int NdpPrefixLength = 0;
andrewboyson 46:40d33e9037e4 18 uint8_t NdpPrefixLA = 0;
andrewboyson 46:40d33e9037e4 19 uint32_t NdpPrefixValidLifetime = 0;
andrewboyson 46:40d33e9037e4 20 uint32_t NdpPrefixPreferredLifetime = 0;
andrewboyson 46:40d33e9037e4 21 char NdpPrefix[16];
andrewboyson 46:40d33e9037e4 22
andrewboyson 46:40d33e9037e4 23 uint32_t NdpDnsLifetime = 0;
andrewboyson 46:40d33e9037e4 24 char NdpDnsServer[16];
andrewboyson 46:40d33e9037e4 25
andrewboyson 46:40d33e9037e4 26 int NdpMtu = 0;
andrewboyson 46:40d33e9037e4 27
andrewboyson 46:40d33e9037e4 28 uint32_t NdpElapsedTime = 0; //Reset whenever an IP address request has been acknowledged
andrewboyson 46:40d33e9037e4 29
andrewboyson 46:40d33e9037e4 30 bool NdpIpNeedsToBeRouted(char* ip)
andrewboyson 46:40d33e9037e4 31 {
andrewboyson 46:40d33e9037e4 32 //Check address is assigned to internet
andrewboyson 46:40d33e9037e4 33 if (*(ip + 0) != 0x20) return false;
andrewboyson 46:40d33e9037e4 34 if (*(ip + 1) != 0x00) return false;
andrewboyson 46:40d33e9037e4 35
andrewboyson 46:40d33e9037e4 36 //Check it is not our own prefix
andrewboyson 46:40d33e9037e4 37 if (memcmp(ip, NdpPrefix, 8) == 0) return false;
andrewboyson 46:40d33e9037e4 38
andrewboyson 46:40d33e9037e4 39 return true;
andrewboyson 46:40d33e9037e4 40 }
andrewboyson 46:40d33e9037e4 41
andrewboyson 46:40d33e9037e4 42 static uint32_t decodeUint32(char* p)
andrewboyson 46:40d33e9037e4 43 {
andrewboyson 46:40d33e9037e4 44 uint32_t value = 0;
andrewboyson 46:40d33e9037e4 45 value |= *p++ << 24;
andrewboyson 46:40d33e9037e4 46 value |= *p++ << 12;
andrewboyson 46:40d33e9037e4 47 value |= *p++ << 8;
andrewboyson 46:40d33e9037e4 48 value |= *p++ << 0;
andrewboyson 46:40d33e9037e4 49 return value;
andrewboyson 46:40d33e9037e4 50 }
andrewboyson 47:73af5c0b0dc2 51 static int decodeOption(char* p, char* srcMac, char* dstMac)
andrewboyson 46:40d33e9037e4 52 {
andrewboyson 46:40d33e9037e4 53 int type = *p++;
andrewboyson 46:40d33e9037e4 54 int size = *p++;
andrewboyson 46:40d33e9037e4 55 switch (type)
andrewboyson 46:40d33e9037e4 56 {
andrewboyson 46:40d33e9037e4 57 case 1:
andrewboyson 47:73af5c0b0dc2 58 if (srcMac) MacCopy(srcMac, p);
andrewboyson 46:40d33e9037e4 59 break;
andrewboyson 46:40d33e9037e4 60 case 2:
andrewboyson 47:73af5c0b0dc2 61 if (dstMac) MacCopy(dstMac, p);
andrewboyson 46:40d33e9037e4 62 break;
andrewboyson 46:40d33e9037e4 63 case 3:
andrewboyson 46:40d33e9037e4 64 NdpPrefixLength = *p++;
andrewboyson 46:40d33e9037e4 65 NdpPrefixLA = *p++;
andrewboyson 46:40d33e9037e4 66 NdpPrefixValidLifetime = decodeUint32(p);
andrewboyson 46:40d33e9037e4 67 p += 4;
andrewboyson 46:40d33e9037e4 68 NdpPrefixPreferredLifetime = decodeUint32(p);
andrewboyson 46:40d33e9037e4 69 p += 4;
andrewboyson 46:40d33e9037e4 70 p += 4; //Skip gracefully past the Reserved2 field
andrewboyson 49:1a6336f2b3f9 71 Ip6AddressCopy(NdpPrefix, p);
andrewboyson 46:40d33e9037e4 72 SlaacMakeGlobal(NdpPrefix);
andrewboyson 46:40d33e9037e4 73 break;
andrewboyson 46:40d33e9037e4 74 case 5:
andrewboyson 46:40d33e9037e4 75 p += 2; //Skip past the reserved field
andrewboyson 46:40d33e9037e4 76 NdpMtu = decodeUint32(p);
andrewboyson 46:40d33e9037e4 77 break;
andrewboyson 46:40d33e9037e4 78 case 25:
andrewboyson 46:40d33e9037e4 79 p += 2; //Skip past the reserved field
andrewboyson 46:40d33e9037e4 80 NdpDnsLifetime = decodeUint32(p);
andrewboyson 46:40d33e9037e4 81 p += 4;
andrewboyson 49:1a6336f2b3f9 82 Ip6AddressCopy(NdpDnsServer, p);
andrewboyson 46:40d33e9037e4 83 break;
andrewboyson 46:40d33e9037e4 84 }
andrewboyson 46:40d33e9037e4 85 return size * 8;
andrewboyson 46:40d33e9037e4 86 }
andrewboyson 46:40d33e9037e4 87 int NdpAddOptionSourceMac(char* p, char* pMac)
andrewboyson 46:40d33e9037e4 88 {
andrewboyson 46:40d33e9037e4 89 *p++ = 1; //Source MAC option
andrewboyson 46:40d33e9037e4 90 *p++ = 1; //8 bytes
andrewboyson 46:40d33e9037e4 91 MacCopy(p, pMac);
andrewboyson 46:40d33e9037e4 92 return 8;
andrewboyson 46:40d33e9037e4 93 }
andrewboyson 47:73af5c0b0dc2 94 int NdpAddOptionTargetMac(char* p, char* pMac)
andrewboyson 46:40d33e9037e4 95 {
andrewboyson 47:73af5c0b0dc2 96 *p++ = 2; //Target MAC option
andrewboyson 47:73af5c0b0dc2 97 *p++ = 1; //8 bytes
andrewboyson 47:73af5c0b0dc2 98 MacCopy(p, pMac);
andrewboyson 47:73af5c0b0dc2 99 return 8;
andrewboyson 47:73af5c0b0dc2 100 }
andrewboyson 47:73af5c0b0dc2 101 static int logOptionVerbose(char* p)
andrewboyson 47:73af5c0b0dc2 102 {
andrewboyson 46:40d33e9037e4 103 uint32_t value;
andrewboyson 46:40d33e9037e4 104 int type = *p++;
andrewboyson 46:40d33e9037e4 105 int size = *p++;
andrewboyson 46:40d33e9037e4 106 switch (type)
andrewboyson 46:40d33e9037e4 107 {
andrewboyson 46:40d33e9037e4 108 case 1:
andrewboyson 47:73af5c0b0dc2 109 Log(" Src MAC "); MacLog(p); Log("\r\n");
andrewboyson 46:40d33e9037e4 110 break;
andrewboyson 46:40d33e9037e4 111 case 2:
andrewboyson 47:73af5c0b0dc2 112 Log(" Dst MAC "); MacLog(p); Log("\r\n");
andrewboyson 46:40d33e9037e4 113 break;
andrewboyson 46:40d33e9037e4 114 case 3:
andrewboyson 46:40d33e9037e4 115 LogF(" Prefix length %d\r\n", *p++);
andrewboyson 46:40d33e9037e4 116 LogF(" Prefix LA %X\r\n", *p++);
andrewboyson 46:40d33e9037e4 117 value = decodeUint32(p); p += 4;
andrewboyson 46:40d33e9037e4 118 LogF(" Prefix valid %u seconds\r\n", value);
andrewboyson 46:40d33e9037e4 119 value = decodeUint32(p); p += 4;
andrewboyson 46:40d33e9037e4 120 LogF(" Prefix preferred %u seconds\r\n", value);
andrewboyson 46:40d33e9037e4 121 p += 4; //Skip gracefully past the Reserved2 field
andrewboyson 47:73af5c0b0dc2 122 LogF(" Prefix "); Ip6AddressLog(p); Log("\r\n");
andrewboyson 46:40d33e9037e4 123 break;
andrewboyson 46:40d33e9037e4 124 case 5:
andrewboyson 46:40d33e9037e4 125 p += 2; //Skip past the reserved field
andrewboyson 46:40d33e9037e4 126 value = decodeUint32(p);
andrewboyson 46:40d33e9037e4 127 p += 4;
andrewboyson 46:40d33e9037e4 128 LogF(" MTU %u\r\n", value);
andrewboyson 46:40d33e9037e4 129 break;
andrewboyson 46:40d33e9037e4 130 case 25:
andrewboyson 46:40d33e9037e4 131 p += 2; //Skip past the reserved field
andrewboyson 46:40d33e9037e4 132 value = decodeUint32(p);
andrewboyson 46:40d33e9037e4 133 p += 4;
andrewboyson 46:40d33e9037e4 134 LogF(" DNS lifetime %u\r\n", value);
andrewboyson 47:73af5c0b0dc2 135 LogF(" DNS Server "); Ip6AddressLog(p); Log("\r\n");
andrewboyson 46:40d33e9037e4 136 break;
andrewboyson 46:40d33e9037e4 137 default:
andrewboyson 46:40d33e9037e4 138 LogF(" Unknown option %d\r\n", type);
andrewboyson 46:40d33e9037e4 139 break;
andrewboyson 46:40d33e9037e4 140 }
andrewboyson 46:40d33e9037e4 141 return size * 8;
andrewboyson 46:40d33e9037e4 142 }
andrewboyson 47:73af5c0b0dc2 143 static int logOptionQuiet(char* p)
andrewboyson 47:73af5c0b0dc2 144 {
andrewboyson 47:73af5c0b0dc2 145 uint32_t value;
andrewboyson 47:73af5c0b0dc2 146 int type = *p++;
andrewboyson 47:73af5c0b0dc2 147 int size = *p++;
andrewboyson 47:73af5c0b0dc2 148 switch (type)
andrewboyson 47:73af5c0b0dc2 149 {
andrewboyson 47:73af5c0b0dc2 150 case 1:
andrewboyson 47:73af5c0b0dc2 151 Log(" src ");
andrewboyson 47:73af5c0b0dc2 152 MacLog(p);
andrewboyson 47:73af5c0b0dc2 153 break;
andrewboyson 47:73af5c0b0dc2 154 case 2:
andrewboyson 47:73af5c0b0dc2 155 Log(" dst ");
andrewboyson 47:73af5c0b0dc2 156 MacLog(p);
andrewboyson 47:73af5c0b0dc2 157 break;
andrewboyson 47:73af5c0b0dc2 158 case 3:
andrewboyson 47:73af5c0b0dc2 159 *p++; //Length
andrewboyson 47:73af5c0b0dc2 160 *p++; //LA
andrewboyson 47:73af5c0b0dc2 161 p += 4; //Valid lifetime
andrewboyson 47:73af5c0b0dc2 162 p += 4; //Preferred lifetime
andrewboyson 47:73af5c0b0dc2 163 p += 4; //Reserved 2
andrewboyson 47:73af5c0b0dc2 164 Log(" prefix ");
andrewboyson 47:73af5c0b0dc2 165 Ip6AddressLog(p); //IP6 address
andrewboyson 47:73af5c0b0dc2 166 break;
andrewboyson 47:73af5c0b0dc2 167 case 5:
andrewboyson 47:73af5c0b0dc2 168 p += 2; //Skip past the reserved field
andrewboyson 47:73af5c0b0dc2 169 value = decodeUint32(p);
andrewboyson 47:73af5c0b0dc2 170 p += 4;
andrewboyson 47:73af5c0b0dc2 171 LogF(" MTU %u", value);
andrewboyson 47:73af5c0b0dc2 172 break;
andrewboyson 47:73af5c0b0dc2 173 case 25:
andrewboyson 47:73af5c0b0dc2 174 p += 2; //Skip past the reserved field
andrewboyson 47:73af5c0b0dc2 175 p += 4; //DNS lifetime
andrewboyson 47:73af5c0b0dc2 176 Log(" DNS ");
andrewboyson 47:73af5c0b0dc2 177 Ip6AddressLog(p);
andrewboyson 47:73af5c0b0dc2 178 break;
andrewboyson 47:73af5c0b0dc2 179 default:
andrewboyson 47:73af5c0b0dc2 180 LogF(" ? %d", type);
andrewboyson 47:73af5c0b0dc2 181 break;
andrewboyson 47:73af5c0b0dc2 182 }
andrewboyson 47:73af5c0b0dc2 183 return size * 8;
andrewboyson 47:73af5c0b0dc2 184 }
andrewboyson 47:73af5c0b0dc2 185 void NdpDecodeOptions(char* pData, int dataLength, char* srcMac, char* dstMac)
andrewboyson 46:40d33e9037e4 186 {
andrewboyson 46:40d33e9037e4 187 char* p = pData;
andrewboyson 46:40d33e9037e4 188 char* pE = pData + dataLength;
andrewboyson 47:73af5c0b0dc2 189 while(p < pE) p += decodeOption(p, srcMac, dstMac);
andrewboyson 46:40d33e9037e4 190 }
andrewboyson 47:73af5c0b0dc2 191 void NdpLogOptionsVerbose(char* pData, int dataLength)
andrewboyson 47:73af5c0b0dc2 192 {
andrewboyson 47:73af5c0b0dc2 193 char* p = pData;
andrewboyson 47:73af5c0b0dc2 194 char* pE = pData + dataLength;
andrewboyson 47:73af5c0b0dc2 195 while(p < pE) p += logOptionVerbose(p);
andrewboyson 47:73af5c0b0dc2 196 }
andrewboyson 47:73af5c0b0dc2 197 void NdpLogOptionsQuiet(char* pData, int dataLength)
andrewboyson 46:40d33e9037e4 198 {
andrewboyson 46:40d33e9037e4 199 char* p = pData;
andrewboyson 46:40d33e9037e4 200 char* pE = pData + dataLength;
andrewboyson 47:73af5c0b0dc2 201 while(p < pE) p += logOptionQuiet(p);
andrewboyson 46:40d33e9037e4 202 }
andrewboyson 47:73af5c0b0dc2 203
andrewboyson 47:73af5c0b0dc2 204 #define INITIAL_DELAY 1
andrewboyson 47:73af5c0b0dc2 205 #define REPEAT_DELAY 60
andrewboyson 47:73af5c0b0dc2 206 static uint32_t delayTime = INITIAL_DELAY; //Set to REPEAT_DELAY_TIME whenever a message is sent and blocks another send until count is back at zero
andrewboyson 47:73af5c0b0dc2 207 void NdpMain()
andrewboyson 47:73af5c0b0dc2 208 {
andrewboyson 47:73af5c0b0dc2 209 if (ClockTicked)
andrewboyson 47:73af5c0b0dc2 210 {
andrewboyson 47:73af5c0b0dc2 211 NdpElapsedTime++;
andrewboyson 47:73af5c0b0dc2 212 if (delayTime > 0) delayTime--;
andrewboyson 47:73af5c0b0dc2 213 }
andrewboyson 47:73af5c0b0dc2 214 if (delayTime) return; //Don't retry within the delay time
andrewboyson 47:73af5c0b0dc2 215
andrewboyson 47:73af5c0b0dc2 216 if (NdpLifetime && NdpElapsedTime < (NdpLifetime >> 1)) return; //Do nothing if within half the life
andrewboyson 47:73af5c0b0dc2 217
andrewboyson 47:73af5c0b0dc2 218 if (!NdpLifetime || NdpElapsedTime >= NdpLifetime)
andrewboyson 47:73af5c0b0dc2 219 {
andrewboyson 47:73af5c0b0dc2 220 if (NetTraceNewLine) Log("\r\n");
andrewboyson 47:73af5c0b0dc2 221 LogTime("NDP lifetime has expired\r\n");
andrewboyson 47:73af5c0b0dc2 222 NdpLifetime = 0;
andrewboyson 49:1a6336f2b3f9 223 Ip6AddressClear(NdpPrefix);
andrewboyson 47:73af5c0b0dc2 224 SlaacMakeGlobal(NdpPrefix);
andrewboyson 49:1a6336f2b3f9 225 Ip6AddressClear(NdpDnsServer);
andrewboyson 47:73af5c0b0dc2 226 }
andrewboyson 47:73af5c0b0dc2 227
andrewboyson 47:73af5c0b0dc2 228 delayTime = REPEAT_DELAY;
andrewboyson 47:73af5c0b0dc2 229 RsSendSolicitation = true;
andrewboyson 47:73af5c0b0dc2 230 }