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:
143:8cec8f08dc54
Child:
171:f708d6776752
Modified resolution cache ajaxs to include the index

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 2 #include <string.h>
andrewboyson 61:aad055f1b0d1 3
andrewboyson 49:1a6336f2b3f9 4 #include "log.h"
andrewboyson 49:1a6336f2b3f9 5 #include "ip6addr.h"
andrewboyson 49:1a6336f2b3f9 6 #include "http.h"
andrewboyson 49:1a6336f2b3f9 7 #include "action.h"
andrewboyson 49:1a6336f2b3f9 8 #include "ndp.h"
andrewboyson 112:f8694d0b8858 9 #include "ntpclient.h"
andrewboyson 49:1a6336f2b3f9 10 #include "slaac.h"
andrewboyson 57:e0fb648acf48 11 #include "tftp.h"
andrewboyson 49:1a6336f2b3f9 12
andrewboyson 49:1a6336f2b3f9 13 void Ip6AddressClear(char* ip)
andrewboyson 49:1a6336f2b3f9 14 {
andrewboyson 107:cc58b4c2090b 15 *ip = 0; //Just set the first byte to zero
andrewboyson 49:1a6336f2b3f9 16 }
andrewboyson 107:cc58b4c2090b 17 bool Ip6AddressIsEmpty(const char* ip)
andrewboyson 49:1a6336f2b3f9 18 {
andrewboyson 49:1a6336f2b3f9 19 return !*ip; //Just check for the first byte being non zero
andrewboyson 49:1a6336f2b3f9 20 }
andrewboyson 49:1a6336f2b3f9 21 static void addHexNibble(bool* pAdded, int number, int index, char** pp)
andrewboyson 49:1a6336f2b3f9 22 {
andrewboyson 49:1a6336f2b3f9 23 int nibble = number;
andrewboyson 49:1a6336f2b3f9 24 if (index) nibble >>= 4;
andrewboyson 49:1a6336f2b3f9 25 nibble &= 0xF;
andrewboyson 49:1a6336f2b3f9 26
andrewboyson 49:1a6336f2b3f9 27 if (nibble || *pAdded)
andrewboyson 49:1a6336f2b3f9 28 {
andrewboyson 49:1a6336f2b3f9 29 **pp = nibble < 10 ? nibble + '0' : nibble - 10 + 'a';
andrewboyson 49:1a6336f2b3f9 30 *pp += 1;
andrewboyson 49:1a6336f2b3f9 31 *pAdded = true;
andrewboyson 49:1a6336f2b3f9 32 }
andrewboyson 49:1a6336f2b3f9 33 }
andrewboyson 143:8cec8f08dc54 34 int Ip6AddressToString(const char* pIp, int size, char* pText)
andrewboyson 49:1a6336f2b3f9 35 {
andrewboyson 143:8cec8f08dc54 36 const char* pIpE = pIp + 16;
andrewboyson 49:1a6336f2b3f9 37 char* p = pText;
andrewboyson 49:1a6336f2b3f9 38 while (true)
andrewboyson 49:1a6336f2b3f9 39 {
andrewboyson 49:1a6336f2b3f9 40 bool added = false;
andrewboyson 49:1a6336f2b3f9 41 if (*pIp || *(pIp + 1))
andrewboyson 49:1a6336f2b3f9 42 {
andrewboyson 49:1a6336f2b3f9 43 if (p > pText + size - 2) break; addHexNibble(&added, *(pIp + 0), 1, &p);
andrewboyson 49:1a6336f2b3f9 44 if (p > pText + size - 2) break; addHexNibble(&added, *(pIp + 0), 0, &p);
andrewboyson 49:1a6336f2b3f9 45 if (p > pText + size - 2) break; addHexNibble(&added, *(pIp + 1), 1, &p);
andrewboyson 49:1a6336f2b3f9 46 if (p > pText + size - 2) break; addHexNibble(&added, *(pIp + 1), 0, &p);
andrewboyson 49:1a6336f2b3f9 47 }
andrewboyson 49:1a6336f2b3f9 48
andrewboyson 49:1a6336f2b3f9 49 pIp += 2;
andrewboyson 49:1a6336f2b3f9 50 if (pIp >= pIpE) break;
andrewboyson 49:1a6336f2b3f9 51
andrewboyson 49:1a6336f2b3f9 52 if (p > pText + size - 2) break; *p++ = ':';
andrewboyson 49:1a6336f2b3f9 53 }
andrewboyson 49:1a6336f2b3f9 54 *p = 0;
andrewboyson 49:1a6336f2b3f9 55 return p - pText;
andrewboyson 49:1a6336f2b3f9 56 }
andrewboyson 49:1a6336f2b3f9 57 static void logHexNibble(bool* pAdded, int number, int index)
andrewboyson 49:1a6336f2b3f9 58 {
andrewboyson 49:1a6336f2b3f9 59 int nibble = number;
andrewboyson 49:1a6336f2b3f9 60 if (index) nibble >>= 4;
andrewboyson 49:1a6336f2b3f9 61 nibble &= 0xF;
andrewboyson 49:1a6336f2b3f9 62
andrewboyson 49:1a6336f2b3f9 63 if (nibble || *pAdded)
andrewboyson 49:1a6336f2b3f9 64 {
andrewboyson 121:bc048b65a630 65 LogChar(nibble < 10 ? nibble + '0' : nibble - 10 + 'a');
andrewboyson 49:1a6336f2b3f9 66 *pAdded = true;
andrewboyson 49:1a6336f2b3f9 67 }
andrewboyson 49:1a6336f2b3f9 68 }
andrewboyson 143:8cec8f08dc54 69 int Ip6AddressLog(const char* pIp)
andrewboyson 49:1a6336f2b3f9 70 {
andrewboyson 49:1a6336f2b3f9 71 int count = 0;
andrewboyson 143:8cec8f08dc54 72 const char* pIpE = pIp + 16;
andrewboyson 49:1a6336f2b3f9 73 while (true)
andrewboyson 49:1a6336f2b3f9 74 {
andrewboyson 49:1a6336f2b3f9 75 bool added = false;
andrewboyson 49:1a6336f2b3f9 76 if (*pIp || *(pIp + 1))
andrewboyson 49:1a6336f2b3f9 77 {
andrewboyson 49:1a6336f2b3f9 78 logHexNibble(&added, *(pIp + 0), 1); if (added) count++;
andrewboyson 49:1a6336f2b3f9 79 logHexNibble(&added, *(pIp + 0), 0); if (added) count++;
andrewboyson 49:1a6336f2b3f9 80 logHexNibble(&added, *(pIp + 1), 1); if (added) count++;
andrewboyson 49:1a6336f2b3f9 81 logHexNibble(&added, *(pIp + 1), 0); if (added) count++;
andrewboyson 49:1a6336f2b3f9 82 }
andrewboyson 49:1a6336f2b3f9 83
andrewboyson 49:1a6336f2b3f9 84 pIp += 2;
andrewboyson 49:1a6336f2b3f9 85 if (pIp >= pIpE) break;
andrewboyson 49:1a6336f2b3f9 86
andrewboyson 121:bc048b65a630 87 LogChar(':'); count++;
andrewboyson 49:1a6336f2b3f9 88 }
andrewboyson 49:1a6336f2b3f9 89 return count;
andrewboyson 49:1a6336f2b3f9 90 }
andrewboyson 49:1a6336f2b3f9 91 static void httpHexNibble(bool* pAdded, int number, int index)
andrewboyson 49:1a6336f2b3f9 92 {
andrewboyson 49:1a6336f2b3f9 93 int nibble = number;
andrewboyson 49:1a6336f2b3f9 94 if (index) nibble >>= 4;
andrewboyson 49:1a6336f2b3f9 95 nibble &= 0xF;
andrewboyson 49:1a6336f2b3f9 96
andrewboyson 49:1a6336f2b3f9 97 if (nibble || *pAdded)
andrewboyson 49:1a6336f2b3f9 98 {
andrewboyson 54:84ef2b29cf7e 99 HttpAddChar(nibble < 10 ? nibble + '0' : nibble - 10 + 'a');
andrewboyson 49:1a6336f2b3f9 100 *pAdded = true;
andrewboyson 49:1a6336f2b3f9 101 }
andrewboyson 49:1a6336f2b3f9 102 }
andrewboyson 143:8cec8f08dc54 103 int Ip6AddressHttp(const char* pIp)
andrewboyson 49:1a6336f2b3f9 104 {
andrewboyson 49:1a6336f2b3f9 105 int count = 0;
andrewboyson 143:8cec8f08dc54 106 const char* pIpE = pIp + 16;
andrewboyson 49:1a6336f2b3f9 107 while (true)
andrewboyson 49:1a6336f2b3f9 108 {
andrewboyson 49:1a6336f2b3f9 109 bool added = false;
andrewboyson 49:1a6336f2b3f9 110 if (*pIp || *(pIp + 1))
andrewboyson 49:1a6336f2b3f9 111 {
andrewboyson 49:1a6336f2b3f9 112 httpHexNibble(&added, *(pIp + 0), 1); if (added) count++;
andrewboyson 49:1a6336f2b3f9 113 httpHexNibble(&added, *(pIp + 0), 0); if (added) count++;
andrewboyson 49:1a6336f2b3f9 114 httpHexNibble(&added, *(pIp + 1), 1); if (added) count++;
andrewboyson 49:1a6336f2b3f9 115 httpHexNibble(&added, *(pIp + 1), 0); if (added) count++;
andrewboyson 49:1a6336f2b3f9 116 }
andrewboyson 49:1a6336f2b3f9 117
andrewboyson 49:1a6336f2b3f9 118 pIp += 2;
andrewboyson 49:1a6336f2b3f9 119 if (pIp >= pIpE) break;
andrewboyson 49:1a6336f2b3f9 120
andrewboyson 54:84ef2b29cf7e 121 HttpAddChar(':'); count++;
andrewboyson 49:1a6336f2b3f9 122 }
andrewboyson 49:1a6336f2b3f9 123 return count;
andrewboyson 49:1a6336f2b3f9 124 }
andrewboyson 107:cc58b4c2090b 125 bool Ip6AddressIsSame(const char* ipA, const char* ipB)
andrewboyson 49:1a6336f2b3f9 126 {
andrewboyson 49:1a6336f2b3f9 127 return !memcmp(ipA, ipB, 16); //Though about optimising by doing a reverse loop but unlikely to be faster than an optimised assembly coded library function
andrewboyson 49:1a6336f2b3f9 128 }
andrewboyson 107:cc58b4c2090b 129 void Ip6AddressCopy(char* ipTo, const char* ipFrom)
andrewboyson 49:1a6336f2b3f9 130 {
andrewboyson 49:1a6336f2b3f9 131 memcpy(ipTo, ipFrom, 16);
andrewboyson 49:1a6336f2b3f9 132 }
andrewboyson 49:1a6336f2b3f9 133
andrewboyson 143:8cec8f08dc54 134 bool Ip6AddrIsSolicited(const char* p)
andrewboyson 136:8a65abb0dc63 135 {
andrewboyson 136:8a65abb0dc63 136 if (*p++ != 0xff) return false;
andrewboyson 136:8a65abb0dc63 137 if (*p++ != 0x02) return false;
andrewboyson 136:8a65abb0dc63 138
andrewboyson 136:8a65abb0dc63 139 if (*p++ != 0x00) return false;
andrewboyson 136:8a65abb0dc63 140 if (*p++ != 0x00) return false;
andrewboyson 136:8a65abb0dc63 141
andrewboyson 136:8a65abb0dc63 142 if (*p++ != 0x00) return false;
andrewboyson 136:8a65abb0dc63 143 if (*p++ != 0x00) return false;
andrewboyson 136:8a65abb0dc63 144
andrewboyson 136:8a65abb0dc63 145 if (*p++ != 0x00) return false;
andrewboyson 136:8a65abb0dc63 146 if (*p++ != 0x00) return false;
andrewboyson 136:8a65abb0dc63 147
andrewboyson 136:8a65abb0dc63 148 if (*p++ != 0x00) return false;
andrewboyson 136:8a65abb0dc63 149 if (*p++ != 0x00) return false;
andrewboyson 136:8a65abb0dc63 150
andrewboyson 136:8a65abb0dc63 151 if (*p++ != 0x00) return false;
andrewboyson 136:8a65abb0dc63 152 if (*p++ != 0x01) return false;
andrewboyson 136:8a65abb0dc63 153
andrewboyson 136:8a65abb0dc63 154 if (*p++ != 0xff) return false;
andrewboyson 136:8a65abb0dc63 155
andrewboyson 136:8a65abb0dc63 156 return true;
andrewboyson 136:8a65abb0dc63 157 }
andrewboyson 143:8cec8f08dc54 158 bool Ip6AddrIsMulticast(const char *p)
andrewboyson 136:8a65abb0dc63 159 {
andrewboyson 136:8a65abb0dc63 160 return *p == 0xFF;
andrewboyson 136:8a65abb0dc63 161 }
andrewboyson 143:8cec8f08dc54 162 bool Ip6AddrIsSameGroup(const char* pA, const char* pB)
andrewboyson 136:8a65abb0dc63 163 {
andrewboyson 136:8a65abb0dc63 164 pA += 13;
andrewboyson 136:8a65abb0dc63 165 pB += 13;
andrewboyson 136:8a65abb0dc63 166 if (*pA++ != *pB++) return false;
andrewboyson 136:8a65abb0dc63 167 if (*pA++ != *pB++) return false;
andrewboyson 136:8a65abb0dc63 168 return *pA == *pB;
andrewboyson 136:8a65abb0dc63 169 }
andrewboyson 136:8a65abb0dc63 170
andrewboyson 107:cc58b4c2090b 171 const char Ip6AddressAllNodes [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
andrewboyson 107:cc58b4c2090b 172 const char Ip6AddressAllRouters[] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02};
andrewboyson 107:cc58b4c2090b 173 const char Ip6AddressMdns [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb};
andrewboyson 107:cc58b4c2090b 174 const char Ip6AddressLlmnr [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03};
andrewboyson 107:cc58b4c2090b 175 const char Ip6AddressNtp [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01};
andrewboyson 49:1a6336f2b3f9 176
andrewboyson 49:1a6336f2b3f9 177 void Ip6AddressFromDest(int dest, char* pDstIp)
andrewboyson 49:1a6336f2b3f9 178 {
andrewboyson 49:1a6336f2b3f9 179 switch (dest)
andrewboyson 49:1a6336f2b3f9 180 {
andrewboyson 107:cc58b4c2090b 181 case UNICAST: /*No change*/ break;
andrewboyson 113:904b40231907 182 case UNICAST_DNS: Ip6AddressCopy(pDstIp, NdpDnsServer ); break;
andrewboyson 113:904b40231907 183 case UNICAST_NTP: Ip6AddressCopy(pDstIp, NtpClientQueryServerIp6 ); break;
andrewboyson 57:e0fb648acf48 184 case UNICAST_TFTP: Ip6AddressCopy(pDstIp, TftpServerIp6 ); break;
andrewboyson 49:1a6336f2b3f9 185 case MULTICAST_NODE: Ip6AddressCopy(pDstIp, Ip6AddressAllNodes ); break;
andrewboyson 49:1a6336f2b3f9 186 case MULTICAST_ROUTER: Ip6AddressCopy(pDstIp, Ip6AddressAllRouters); break;
andrewboyson 49:1a6336f2b3f9 187 case MULTICAST_MDNS: Ip6AddressCopy(pDstIp, Ip6AddressMdns ); break;
andrewboyson 49:1a6336f2b3f9 188 case MULTICAST_LLMNR: Ip6AddressCopy(pDstIp, Ip6AddressLlmnr ); break;
andrewboyson 107:cc58b4c2090b 189 case MULTICAST_NTP: Ip6AddressCopy(pDstIp, Ip6AddressNtp ); break;
andrewboyson 49:1a6336f2b3f9 190 default:
andrewboyson 52:fbc5a46b5e16 191 LogTimeF("Ip6AddressFromDest unknown destination %d\r\n", dest);
andrewboyson 49:1a6336f2b3f9 192 break;
andrewboyson 49:1a6336f2b3f9 193 }
andrewboyson 49:1a6336f2b3f9 194 }