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/ip6addr.cpp@54:84ef2b29cf7e, 2017-11-13 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon Nov 13 08:06:55 2017 +0000
- Revision:
- 54:84ef2b29cf7e
- Parent:
- 52:fbc5a46b5e16
- Child:
- 57:e0fb648acf48
Tidied HTTP files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 49:1a6336f2b3f9 | 1 | #include "mbed.h" |
andrewboyson | 49:1a6336f2b3f9 | 2 | #include "log.h" |
andrewboyson | 49:1a6336f2b3f9 | 3 | #include "ip6addr.h" |
andrewboyson | 49:1a6336f2b3f9 | 4 | #include "http.h" |
andrewboyson | 49:1a6336f2b3f9 | 5 | #include "action.h" |
andrewboyson | 49:1a6336f2b3f9 | 6 | #include "ndp.h" |
andrewboyson | 49:1a6336f2b3f9 | 7 | #include "ntp.h" |
andrewboyson | 49:1a6336f2b3f9 | 8 | #include "slaac.h" |
andrewboyson | 49:1a6336f2b3f9 | 9 | |
andrewboyson | 49:1a6336f2b3f9 | 10 | void Ip6AddressClear(char* ip) |
andrewboyson | 49:1a6336f2b3f9 | 11 | { |
andrewboyson | 49:1a6336f2b3f9 | 12 | *ip = 0; |
andrewboyson | 49:1a6336f2b3f9 | 13 | } |
andrewboyson | 49:1a6336f2b3f9 | 14 | bool Ip6AddressIsEmpty(char* ip) |
andrewboyson | 49:1a6336f2b3f9 | 15 | { |
andrewboyson | 49:1a6336f2b3f9 | 16 | return !*ip; //Just check for the first byte being non zero |
andrewboyson | 49:1a6336f2b3f9 | 17 | } |
andrewboyson | 49:1a6336f2b3f9 | 18 | static void addHexNibble(bool* pAdded, int number, int index, char** pp) |
andrewboyson | 49:1a6336f2b3f9 | 19 | { |
andrewboyson | 49:1a6336f2b3f9 | 20 | int nibble = number; |
andrewboyson | 49:1a6336f2b3f9 | 21 | if (index) nibble >>= 4; |
andrewboyson | 49:1a6336f2b3f9 | 22 | nibble &= 0xF; |
andrewboyson | 49:1a6336f2b3f9 | 23 | |
andrewboyson | 49:1a6336f2b3f9 | 24 | if (nibble || *pAdded) |
andrewboyson | 49:1a6336f2b3f9 | 25 | { |
andrewboyson | 49:1a6336f2b3f9 | 26 | **pp = nibble < 10 ? nibble + '0' : nibble - 10 + 'a'; |
andrewboyson | 49:1a6336f2b3f9 | 27 | *pp += 1; |
andrewboyson | 49:1a6336f2b3f9 | 28 | *pAdded = true; |
andrewboyson | 49:1a6336f2b3f9 | 29 | } |
andrewboyson | 49:1a6336f2b3f9 | 30 | } |
andrewboyson | 49:1a6336f2b3f9 | 31 | int Ip6AddressToString(char* pIp, int size, char* pText) |
andrewboyson | 49:1a6336f2b3f9 | 32 | { |
andrewboyson | 49:1a6336f2b3f9 | 33 | char* pIpE = pIp + 16; |
andrewboyson | 49:1a6336f2b3f9 | 34 | char* p = pText; |
andrewboyson | 49:1a6336f2b3f9 | 35 | while (true) |
andrewboyson | 49:1a6336f2b3f9 | 36 | { |
andrewboyson | 49:1a6336f2b3f9 | 37 | bool added = false; |
andrewboyson | 49:1a6336f2b3f9 | 38 | if (*pIp || *(pIp + 1)) |
andrewboyson | 49:1a6336f2b3f9 | 39 | { |
andrewboyson | 49:1a6336f2b3f9 | 40 | if (p > pText + size - 2) break; addHexNibble(&added, *(pIp + 0), 1, &p); |
andrewboyson | 49:1a6336f2b3f9 | 41 | if (p > pText + size - 2) break; addHexNibble(&added, *(pIp + 0), 0, &p); |
andrewboyson | 49:1a6336f2b3f9 | 42 | if (p > pText + size - 2) break; addHexNibble(&added, *(pIp + 1), 1, &p); |
andrewboyson | 49:1a6336f2b3f9 | 43 | if (p > pText + size - 2) break; addHexNibble(&added, *(pIp + 1), 0, &p); |
andrewboyson | 49:1a6336f2b3f9 | 44 | } |
andrewboyson | 49:1a6336f2b3f9 | 45 | |
andrewboyson | 49:1a6336f2b3f9 | 46 | pIp += 2; |
andrewboyson | 49:1a6336f2b3f9 | 47 | if (pIp >= pIpE) break; |
andrewboyson | 49:1a6336f2b3f9 | 48 | |
andrewboyson | 49:1a6336f2b3f9 | 49 | if (p > pText + size - 2) break; *p++ = ':'; |
andrewboyson | 49:1a6336f2b3f9 | 50 | } |
andrewboyson | 49:1a6336f2b3f9 | 51 | *p = 0; |
andrewboyson | 49:1a6336f2b3f9 | 52 | return p - pText; |
andrewboyson | 49:1a6336f2b3f9 | 53 | } |
andrewboyson | 49:1a6336f2b3f9 | 54 | static void logHexNibble(bool* pAdded, int number, int index) |
andrewboyson | 49:1a6336f2b3f9 | 55 | { |
andrewboyson | 49:1a6336f2b3f9 | 56 | int nibble = number; |
andrewboyson | 49:1a6336f2b3f9 | 57 | if (index) nibble >>= 4; |
andrewboyson | 49:1a6336f2b3f9 | 58 | nibble &= 0xF; |
andrewboyson | 49:1a6336f2b3f9 | 59 | |
andrewboyson | 49:1a6336f2b3f9 | 60 | if (nibble || *pAdded) |
andrewboyson | 49:1a6336f2b3f9 | 61 | { |
andrewboyson | 49:1a6336f2b3f9 | 62 | LogPush(nibble < 10 ? nibble + '0' : nibble - 10 + 'a'); |
andrewboyson | 49:1a6336f2b3f9 | 63 | *pAdded = true; |
andrewboyson | 49:1a6336f2b3f9 | 64 | } |
andrewboyson | 49:1a6336f2b3f9 | 65 | } |
andrewboyson | 49:1a6336f2b3f9 | 66 | int Ip6AddressLog(char* pIp) |
andrewboyson | 49:1a6336f2b3f9 | 67 | { |
andrewboyson | 49:1a6336f2b3f9 | 68 | int count = 0; |
andrewboyson | 49:1a6336f2b3f9 | 69 | char* pIpE = pIp + 16; |
andrewboyson | 49:1a6336f2b3f9 | 70 | while (true) |
andrewboyson | 49:1a6336f2b3f9 | 71 | { |
andrewboyson | 49:1a6336f2b3f9 | 72 | bool added = false; |
andrewboyson | 49:1a6336f2b3f9 | 73 | if (*pIp || *(pIp + 1)) |
andrewboyson | 49:1a6336f2b3f9 | 74 | { |
andrewboyson | 49:1a6336f2b3f9 | 75 | logHexNibble(&added, *(pIp + 0), 1); if (added) count++; |
andrewboyson | 49:1a6336f2b3f9 | 76 | logHexNibble(&added, *(pIp + 0), 0); if (added) count++; |
andrewboyson | 49:1a6336f2b3f9 | 77 | logHexNibble(&added, *(pIp + 1), 1); if (added) count++; |
andrewboyson | 49:1a6336f2b3f9 | 78 | logHexNibble(&added, *(pIp + 1), 0); if (added) count++; |
andrewboyson | 49:1a6336f2b3f9 | 79 | } |
andrewboyson | 49:1a6336f2b3f9 | 80 | |
andrewboyson | 49:1a6336f2b3f9 | 81 | pIp += 2; |
andrewboyson | 49:1a6336f2b3f9 | 82 | if (pIp >= pIpE) break; |
andrewboyson | 49:1a6336f2b3f9 | 83 | |
andrewboyson | 49:1a6336f2b3f9 | 84 | LogPush(':'); count++; |
andrewboyson | 49:1a6336f2b3f9 | 85 | } |
andrewboyson | 49:1a6336f2b3f9 | 86 | return count; |
andrewboyson | 49:1a6336f2b3f9 | 87 | } |
andrewboyson | 49:1a6336f2b3f9 | 88 | static void httpHexNibble(bool* pAdded, int number, int index) |
andrewboyson | 49:1a6336f2b3f9 | 89 | { |
andrewboyson | 49:1a6336f2b3f9 | 90 | int nibble = number; |
andrewboyson | 49:1a6336f2b3f9 | 91 | if (index) nibble >>= 4; |
andrewboyson | 49:1a6336f2b3f9 | 92 | nibble &= 0xF; |
andrewboyson | 49:1a6336f2b3f9 | 93 | |
andrewboyson | 49:1a6336f2b3f9 | 94 | if (nibble || *pAdded) |
andrewboyson | 49:1a6336f2b3f9 | 95 | { |
andrewboyson | 54:84ef2b29cf7e | 96 | HttpAddChar(nibble < 10 ? nibble + '0' : nibble - 10 + 'a'); |
andrewboyson | 49:1a6336f2b3f9 | 97 | *pAdded = true; |
andrewboyson | 49:1a6336f2b3f9 | 98 | } |
andrewboyson | 49:1a6336f2b3f9 | 99 | } |
andrewboyson | 49:1a6336f2b3f9 | 100 | int Ip6AddressHttp(char* pIp) |
andrewboyson | 49:1a6336f2b3f9 | 101 | { |
andrewboyson | 49:1a6336f2b3f9 | 102 | int count = 0; |
andrewboyson | 49:1a6336f2b3f9 | 103 | char* pIpE = pIp + 16; |
andrewboyson | 49:1a6336f2b3f9 | 104 | while (true) |
andrewboyson | 49:1a6336f2b3f9 | 105 | { |
andrewboyson | 49:1a6336f2b3f9 | 106 | bool added = false; |
andrewboyson | 49:1a6336f2b3f9 | 107 | if (*pIp || *(pIp + 1)) |
andrewboyson | 49:1a6336f2b3f9 | 108 | { |
andrewboyson | 49:1a6336f2b3f9 | 109 | httpHexNibble(&added, *(pIp + 0), 1); if (added) count++; |
andrewboyson | 49:1a6336f2b3f9 | 110 | httpHexNibble(&added, *(pIp + 0), 0); if (added) count++; |
andrewboyson | 49:1a6336f2b3f9 | 111 | httpHexNibble(&added, *(pIp + 1), 1); if (added) count++; |
andrewboyson | 49:1a6336f2b3f9 | 112 | httpHexNibble(&added, *(pIp + 1), 0); if (added) count++; |
andrewboyson | 49:1a6336f2b3f9 | 113 | } |
andrewboyson | 49:1a6336f2b3f9 | 114 | |
andrewboyson | 49:1a6336f2b3f9 | 115 | pIp += 2; |
andrewboyson | 49:1a6336f2b3f9 | 116 | if (pIp >= pIpE) break; |
andrewboyson | 49:1a6336f2b3f9 | 117 | |
andrewboyson | 54:84ef2b29cf7e | 118 | HttpAddChar(':'); count++; |
andrewboyson | 49:1a6336f2b3f9 | 119 | } |
andrewboyson | 49:1a6336f2b3f9 | 120 | return count; |
andrewboyson | 49:1a6336f2b3f9 | 121 | } |
andrewboyson | 49:1a6336f2b3f9 | 122 | bool Ip6AddressIsSame(char* ipA, char* ipB) |
andrewboyson | 49:1a6336f2b3f9 | 123 | { |
andrewboyson | 49:1a6336f2b3f9 | 124 | 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 | 125 | } |
andrewboyson | 49:1a6336f2b3f9 | 126 | void Ip6AddressCopy(char* ipTo, char* ipFrom) |
andrewboyson | 49:1a6336f2b3f9 | 127 | { |
andrewboyson | 49:1a6336f2b3f9 | 128 | memcpy(ipTo, ipFrom, 16); |
andrewboyson | 49:1a6336f2b3f9 | 129 | } |
andrewboyson | 49:1a6336f2b3f9 | 130 | |
andrewboyson | 49:1a6336f2b3f9 | 131 | char Ip6AddressAllNodes [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; |
andrewboyson | 49:1a6336f2b3f9 | 132 | char Ip6AddressAllRouters[] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}; |
andrewboyson | 49:1a6336f2b3f9 | 133 | char Ip6AddressMdns [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb}; |
andrewboyson | 49:1a6336f2b3f9 | 134 | char Ip6AddressLlmnr [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03}; |
andrewboyson | 49:1a6336f2b3f9 | 135 | |
andrewboyson | 49:1a6336f2b3f9 | 136 | void Ip6AddressFromDest(int dest, char* pDstIp) |
andrewboyson | 49:1a6336f2b3f9 | 137 | { |
andrewboyson | 49:1a6336f2b3f9 | 138 | switch (dest) |
andrewboyson | 49:1a6336f2b3f9 | 139 | { |
andrewboyson | 49:1a6336f2b3f9 | 140 | case UNICAST: break; |
andrewboyson | 49:1a6336f2b3f9 | 141 | case UNICAST_DNS: Ip6AddressCopy(pDstIp, NdpDnsServer ); break; |
andrewboyson | 49:1a6336f2b3f9 | 142 | case UNICAST_NTP: Ip6AddressCopy(pDstIp, NtpServerIp6 ); break; |
andrewboyson | 49:1a6336f2b3f9 | 143 | case MULTICAST_NODE: Ip6AddressCopy(pDstIp, Ip6AddressAllNodes ); break; |
andrewboyson | 49:1a6336f2b3f9 | 144 | case MULTICAST_ROUTER: Ip6AddressCopy(pDstIp, Ip6AddressAllRouters); break; |
andrewboyson | 49:1a6336f2b3f9 | 145 | case MULTICAST_MDNS: Ip6AddressCopy(pDstIp, Ip6AddressMdns ); break; |
andrewboyson | 49:1a6336f2b3f9 | 146 | case MULTICAST_LLMNR: Ip6AddressCopy(pDstIp, Ip6AddressLlmnr ); break; |
andrewboyson | 49:1a6336f2b3f9 | 147 | default: |
andrewboyson | 52:fbc5a46b5e16 | 148 | LogTimeF("Ip6AddressFromDest unknown destination %d\r\n", dest); |
andrewboyson | 49:1a6336f2b3f9 | 149 | break; |
andrewboyson | 49:1a6336f2b3f9 | 150 | } |
andrewboyson | 49:1a6336f2b3f9 | 151 | } |