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.c@113:904b40231907, 2019-01-22 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Jan 22 15:41:12 2019 +0000
- Revision:
- 113:904b40231907
- Parent:
- 112:f8694d0b8858
- Child:
- 121:bc048b65a630
Incorporated ntp server module
Who changed what in which revision?
User | Revision | Line number | New 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 | 49:1a6336f2b3f9 | 34 | int Ip6AddressToString(char* pIp, int size, char* pText) |
andrewboyson | 49:1a6336f2b3f9 | 35 | { |
andrewboyson | 49:1a6336f2b3f9 | 36 | 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 | 49:1a6336f2b3f9 | 65 | LogPush(nibble < 10 ? nibble + '0' : nibble - 10 + 'a'); |
andrewboyson | 49:1a6336f2b3f9 | 66 | *pAdded = true; |
andrewboyson | 49:1a6336f2b3f9 | 67 | } |
andrewboyson | 49:1a6336f2b3f9 | 68 | } |
andrewboyson | 49:1a6336f2b3f9 | 69 | int Ip6AddressLog(char* pIp) |
andrewboyson | 49:1a6336f2b3f9 | 70 | { |
andrewboyson | 49:1a6336f2b3f9 | 71 | int count = 0; |
andrewboyson | 49:1a6336f2b3f9 | 72 | 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 | 49:1a6336f2b3f9 | 87 | LogPush(':'); 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 | 49:1a6336f2b3f9 | 103 | int Ip6AddressHttp(char* pIp) |
andrewboyson | 49:1a6336f2b3f9 | 104 | { |
andrewboyson | 49:1a6336f2b3f9 | 105 | int count = 0; |
andrewboyson | 49:1a6336f2b3f9 | 106 | 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 | 107:cc58b4c2090b | 134 | const char Ip6AddressAllNodes [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; |
andrewboyson | 107:cc58b4c2090b | 135 | const char Ip6AddressAllRouters[] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}; |
andrewboyson | 107:cc58b4c2090b | 136 | const char Ip6AddressMdns [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb}; |
andrewboyson | 107:cc58b4c2090b | 137 | const char Ip6AddressLlmnr [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03}; |
andrewboyson | 107:cc58b4c2090b | 138 | const char Ip6AddressNtp [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}; |
andrewboyson | 49:1a6336f2b3f9 | 139 | |
andrewboyson | 49:1a6336f2b3f9 | 140 | void Ip6AddressFromDest(int dest, char* pDstIp) |
andrewboyson | 49:1a6336f2b3f9 | 141 | { |
andrewboyson | 49:1a6336f2b3f9 | 142 | switch (dest) |
andrewboyson | 49:1a6336f2b3f9 | 143 | { |
andrewboyson | 107:cc58b4c2090b | 144 | case UNICAST: /*No change*/ break; |
andrewboyson | 113:904b40231907 | 145 | case UNICAST_DNS: Ip6AddressCopy(pDstIp, NdpDnsServer ); break; |
andrewboyson | 113:904b40231907 | 146 | case UNICAST_NTP: Ip6AddressCopy(pDstIp, NtpClientQueryServerIp6 ); break; |
andrewboyson | 57:e0fb648acf48 | 147 | case UNICAST_TFTP: Ip6AddressCopy(pDstIp, TftpServerIp6 ); break; |
andrewboyson | 49:1a6336f2b3f9 | 148 | case MULTICAST_NODE: Ip6AddressCopy(pDstIp, Ip6AddressAllNodes ); break; |
andrewboyson | 49:1a6336f2b3f9 | 149 | case MULTICAST_ROUTER: Ip6AddressCopy(pDstIp, Ip6AddressAllRouters); break; |
andrewboyson | 49:1a6336f2b3f9 | 150 | case MULTICAST_MDNS: Ip6AddressCopy(pDstIp, Ip6AddressMdns ); break; |
andrewboyson | 49:1a6336f2b3f9 | 151 | case MULTICAST_LLMNR: Ip6AddressCopy(pDstIp, Ip6AddressLlmnr ); break; |
andrewboyson | 107:cc58b4c2090b | 152 | case MULTICAST_NTP: Ip6AddressCopy(pDstIp, Ip6AddressNtp ); break; |
andrewboyson | 49:1a6336f2b3f9 | 153 | default: |
andrewboyson | 52:fbc5a46b5e16 | 154 | LogTimeF("Ip6AddressFromDest unknown destination %d\r\n", dest); |
andrewboyson | 49:1a6336f2b3f9 | 155 | break; |
andrewboyson | 49:1a6336f2b3f9 | 156 | } |
andrewboyson | 49:1a6336f2b3f9 | 157 | } |