Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Oct 26 14:50:24 2017 +0000
Revision:
47:73af5c0b0dc2
Parent:
46:40d33e9037e4
Child:
49:1a6336f2b3f9
Replaced a number of temporary buffers with direct writes to the Log or HTTP.

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 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 "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 46:40d33e9037e4 71 Ip6Copy(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 46:40d33e9037e4 82 Ip6Copy(NdpDnsServer, p);
andrewboyson 46:40d33e9037e4 83 break;
andrewboyson 46:40d33e9037e4 84 default:
andrewboyson 46:40d33e9037e4 85 LogF(" Unknown option %d\r\n", type);
andrewboyson 46:40d33e9037e4 86 break;
andrewboyson 46:40d33e9037e4 87 }
andrewboyson 46:40d33e9037e4 88 return size * 8;
andrewboyson 46:40d33e9037e4 89 }
andrewboyson 46:40d33e9037e4 90 int NdpAddOptionSourceMac(char* p, char* pMac)
andrewboyson 46:40d33e9037e4 91 {
andrewboyson 46:40d33e9037e4 92 *p++ = 1; //Source MAC option
andrewboyson 46:40d33e9037e4 93 *p++ = 1; //8 bytes
andrewboyson 46:40d33e9037e4 94 MacCopy(p, pMac);
andrewboyson 46:40d33e9037e4 95 return 8;
andrewboyson 46:40d33e9037e4 96 }
andrewboyson 47:73af5c0b0dc2 97 int NdpAddOptionTargetMac(char* p, char* pMac)
andrewboyson 46:40d33e9037e4 98 {
andrewboyson 47:73af5c0b0dc2 99 *p++ = 2; //Target MAC option
andrewboyson 47:73af5c0b0dc2 100 *p++ = 1; //8 bytes
andrewboyson 47:73af5c0b0dc2 101 MacCopy(p, pMac);
andrewboyson 47:73af5c0b0dc2 102 return 8;
andrewboyson 47:73af5c0b0dc2 103 }
andrewboyson 47:73af5c0b0dc2 104 static int logOptionVerbose(char* p)
andrewboyson 47:73af5c0b0dc2 105 {
andrewboyson 46:40d33e9037e4 106 uint32_t value;
andrewboyson 46:40d33e9037e4 107 int type = *p++;
andrewboyson 46:40d33e9037e4 108 int size = *p++;
andrewboyson 46:40d33e9037e4 109 switch (type)
andrewboyson 46:40d33e9037e4 110 {
andrewboyson 46:40d33e9037e4 111 case 1:
andrewboyson 47:73af5c0b0dc2 112 Log(" Src MAC "); MacLog(p); Log("\r\n");
andrewboyson 46:40d33e9037e4 113 break;
andrewboyson 46:40d33e9037e4 114 case 2:
andrewboyson 47:73af5c0b0dc2 115 Log(" Dst MAC "); MacLog(p); Log("\r\n");
andrewboyson 46:40d33e9037e4 116 break;
andrewboyson 46:40d33e9037e4 117 case 3:
andrewboyson 46:40d33e9037e4 118 LogF(" Prefix length %d\r\n", *p++);
andrewboyson 46:40d33e9037e4 119 LogF(" Prefix LA %X\r\n", *p++);
andrewboyson 46:40d33e9037e4 120 value = decodeUint32(p); p += 4;
andrewboyson 46:40d33e9037e4 121 LogF(" Prefix valid %u seconds\r\n", value);
andrewboyson 46:40d33e9037e4 122 value = decodeUint32(p); p += 4;
andrewboyson 46:40d33e9037e4 123 LogF(" Prefix preferred %u seconds\r\n", value);
andrewboyson 46:40d33e9037e4 124 p += 4; //Skip gracefully past the Reserved2 field
andrewboyson 47:73af5c0b0dc2 125 LogF(" Prefix "); Ip6AddressLog(p); Log("\r\n");
andrewboyson 46:40d33e9037e4 126 break;
andrewboyson 46:40d33e9037e4 127 case 5:
andrewboyson 46:40d33e9037e4 128 p += 2; //Skip past the reserved field
andrewboyson 46:40d33e9037e4 129 value = decodeUint32(p);
andrewboyson 46:40d33e9037e4 130 p += 4;
andrewboyson 46:40d33e9037e4 131 LogF(" MTU %u\r\n", value);
andrewboyson 46:40d33e9037e4 132 break;
andrewboyson 46:40d33e9037e4 133 case 25:
andrewboyson 46:40d33e9037e4 134 p += 2; //Skip past the reserved field
andrewboyson 46:40d33e9037e4 135 value = decodeUint32(p);
andrewboyson 46:40d33e9037e4 136 p += 4;
andrewboyson 46:40d33e9037e4 137 LogF(" DNS lifetime %u\r\n", value);
andrewboyson 47:73af5c0b0dc2 138 LogF(" DNS Server "); Ip6AddressLog(p); Log("\r\n");
andrewboyson 46:40d33e9037e4 139 break;
andrewboyson 46:40d33e9037e4 140 default:
andrewboyson 46:40d33e9037e4 141 LogF(" Unknown option %d\r\n", type);
andrewboyson 46:40d33e9037e4 142 break;
andrewboyson 46:40d33e9037e4 143 }
andrewboyson 46:40d33e9037e4 144 return size * 8;
andrewboyson 46:40d33e9037e4 145 }
andrewboyson 47:73af5c0b0dc2 146 static int logOptionQuiet(char* p)
andrewboyson 47:73af5c0b0dc2 147 {
andrewboyson 47:73af5c0b0dc2 148 uint32_t value;
andrewboyson 47:73af5c0b0dc2 149 int type = *p++;
andrewboyson 47:73af5c0b0dc2 150 int size = *p++;
andrewboyson 47:73af5c0b0dc2 151 switch (type)
andrewboyson 47:73af5c0b0dc2 152 {
andrewboyson 47:73af5c0b0dc2 153 case 1:
andrewboyson 47:73af5c0b0dc2 154 Log(" src ");
andrewboyson 47:73af5c0b0dc2 155 MacLog(p);
andrewboyson 47:73af5c0b0dc2 156 break;
andrewboyson 47:73af5c0b0dc2 157 case 2:
andrewboyson 47:73af5c0b0dc2 158 Log(" dst ");
andrewboyson 47:73af5c0b0dc2 159 MacLog(p);
andrewboyson 47:73af5c0b0dc2 160 break;
andrewboyson 47:73af5c0b0dc2 161 case 3:
andrewboyson 47:73af5c0b0dc2 162 *p++; //Length
andrewboyson 47:73af5c0b0dc2 163 *p++; //LA
andrewboyson 47:73af5c0b0dc2 164 p += 4; //Valid lifetime
andrewboyson 47:73af5c0b0dc2 165 p += 4; //Preferred lifetime
andrewboyson 47:73af5c0b0dc2 166 p += 4; //Reserved 2
andrewboyson 47:73af5c0b0dc2 167 Log(" prefix ");
andrewboyson 47:73af5c0b0dc2 168 Ip6AddressLog(p); //IP6 address
andrewboyson 47:73af5c0b0dc2 169 break;
andrewboyson 47:73af5c0b0dc2 170 case 5:
andrewboyson 47:73af5c0b0dc2 171 p += 2; //Skip past the reserved field
andrewboyson 47:73af5c0b0dc2 172 value = decodeUint32(p);
andrewboyson 47:73af5c0b0dc2 173 p += 4;
andrewboyson 47:73af5c0b0dc2 174 LogF(" MTU %u", value);
andrewboyson 47:73af5c0b0dc2 175 break;
andrewboyson 47:73af5c0b0dc2 176 case 25:
andrewboyson 47:73af5c0b0dc2 177 p += 2; //Skip past the reserved field
andrewboyson 47:73af5c0b0dc2 178 p += 4; //DNS lifetime
andrewboyson 47:73af5c0b0dc2 179 Log(" DNS ");
andrewboyson 47:73af5c0b0dc2 180 Ip6AddressLog(p);
andrewboyson 47:73af5c0b0dc2 181 break;
andrewboyson 47:73af5c0b0dc2 182 default:
andrewboyson 47:73af5c0b0dc2 183 LogF(" ? %d", type);
andrewboyson 47:73af5c0b0dc2 184 break;
andrewboyson 47:73af5c0b0dc2 185 }
andrewboyson 47:73af5c0b0dc2 186 return size * 8;
andrewboyson 47:73af5c0b0dc2 187 }
andrewboyson 47:73af5c0b0dc2 188 void NdpDecodeOptions(char* pData, int dataLength, char* srcMac, char* dstMac)
andrewboyson 46:40d33e9037e4 189 {
andrewboyson 46:40d33e9037e4 190 char* p = pData;
andrewboyson 46:40d33e9037e4 191 char* pE = pData + dataLength;
andrewboyson 47:73af5c0b0dc2 192 while(p < pE) p += decodeOption(p, srcMac, dstMac);
andrewboyson 46:40d33e9037e4 193 }
andrewboyson 47:73af5c0b0dc2 194 void NdpLogOptionsVerbose(char* pData, int dataLength)
andrewboyson 47:73af5c0b0dc2 195 {
andrewboyson 47:73af5c0b0dc2 196 char* p = pData;
andrewboyson 47:73af5c0b0dc2 197 char* pE = pData + dataLength;
andrewboyson 47:73af5c0b0dc2 198 while(p < pE) p += logOptionVerbose(p);
andrewboyson 47:73af5c0b0dc2 199 }
andrewboyson 47:73af5c0b0dc2 200 void NdpLogOptionsQuiet(char* pData, int dataLength)
andrewboyson 46:40d33e9037e4 201 {
andrewboyson 46:40d33e9037e4 202 char* p = pData;
andrewboyson 46:40d33e9037e4 203 char* pE = pData + dataLength;
andrewboyson 47:73af5c0b0dc2 204 while(p < pE) p += logOptionQuiet(p);
andrewboyson 46:40d33e9037e4 205 }
andrewboyson 47:73af5c0b0dc2 206
andrewboyson 47:73af5c0b0dc2 207 #define INITIAL_DELAY 1
andrewboyson 47:73af5c0b0dc2 208 #define REPEAT_DELAY 60
andrewboyson 47:73af5c0b0dc2 209 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 210 void NdpMain()
andrewboyson 47:73af5c0b0dc2 211 {
andrewboyson 47:73af5c0b0dc2 212 if (ClockTicked)
andrewboyson 47:73af5c0b0dc2 213 {
andrewboyson 47:73af5c0b0dc2 214 NdpElapsedTime++;
andrewboyson 47:73af5c0b0dc2 215 if (delayTime > 0) delayTime--;
andrewboyson 47:73af5c0b0dc2 216 }
andrewboyson 47:73af5c0b0dc2 217 if (delayTime) return; //Don't retry within the delay time
andrewboyson 47:73af5c0b0dc2 218
andrewboyson 47:73af5c0b0dc2 219 if (NdpLifetime && NdpElapsedTime < (NdpLifetime >> 1)) return; //Do nothing if within half the life
andrewboyson 47:73af5c0b0dc2 220
andrewboyson 47:73af5c0b0dc2 221 if (!NdpLifetime || NdpElapsedTime >= NdpLifetime)
andrewboyson 47:73af5c0b0dc2 222 {
andrewboyson 47:73af5c0b0dc2 223 if (NetTraceNewLine) Log("\r\n");
andrewboyson 47:73af5c0b0dc2 224 LogTime("NDP lifetime has expired\r\n");
andrewboyson 47:73af5c0b0dc2 225 NdpLifetime = 0;
andrewboyson 47:73af5c0b0dc2 226 Ip6Clear(NdpPrefix);
andrewboyson 47:73af5c0b0dc2 227 SlaacMakeGlobal(NdpPrefix);
andrewboyson 47:73af5c0b0dc2 228 Ip6Clear(NdpDnsServer);
andrewboyson 47:73af5c0b0dc2 229 }
andrewboyson 47:73af5c0b0dc2 230
andrewboyson 47:73af5c0b0dc2 231 delayTime = REPEAT_DELAY;
andrewboyson 47:73af5c0b0dc2 232 RsSendSolicitation = true;
andrewboyson 47:73af5c0b0dc2 233 }