Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maygup01 0:11cc2b7889af 1 /*
maygup01 0:11cc2b7889af 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
maygup01 0:11cc2b7889af 3 * SPDX-License-Identifier: Apache-2.0
maygup01 0:11cc2b7889af 4 * Licensed under the Apache License, Version 2.0 (the License); you may
maygup01 0:11cc2b7889af 5 * not use this file except in compliance with the License.
maygup01 0:11cc2b7889af 6 * You may obtain a copy of the License at
maygup01 0:11cc2b7889af 7 *
maygup01 0:11cc2b7889af 8 * http://www.apache.org/licenses/LICENSE-2.0
maygup01 0:11cc2b7889af 9 *
maygup01 0:11cc2b7889af 10 * Unless required by applicable law or agreed to in writing, software
maygup01 0:11cc2b7889af 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
maygup01 0:11cc2b7889af 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maygup01 0:11cc2b7889af 13 * See the License for the specific language governing permissions and
maygup01 0:11cc2b7889af 14 * limitations under the License.
maygup01 0:11cc2b7889af 15 */
maygup01 0:11cc2b7889af 16 #include <string.h>
maygup01 0:11cc2b7889af 17 #include <stdlib.h>
maygup01 0:11cc2b7889af 18 #include <stdint.h>
maygup01 0:11cc2b7889af 19 #include "common_functions.h"
maygup01 0:11cc2b7889af 20 #include "ip6string.h"
maygup01 0:11cc2b7889af 21
maygup01 0:11cc2b7889af 22 static uint16_t hex(const char *p);
maygup01 0:11cc2b7889af 23 static bool is_hex(char c);
maygup01 0:11cc2b7889af 24
maygup01 0:11cc2b7889af 25 /**
maygup01 0:11cc2b7889af 26 * Convert numeric IPv6 address string to a binary.
maygup01 0:11cc2b7889af 27 * IPv4 tunnelling addresses are not covered.
maygup01 0:11cc2b7889af 28 * \param ip6addr IPv6 address in string format.
maygup01 0:11cc2b7889af 29 * \param len Length of ipv6 string.
maygup01 0:11cc2b7889af 30 * \param dest buffer for address. MUST be 16 bytes.
maygup01 0:11cc2b7889af 31 * \return boolean set to true if conversion succeed, false if it didn't
maygup01 0:11cc2b7889af 32 */
maygup01 0:11cc2b7889af 33 bool stoip6(const char *ip6addr, size_t len, void *dest)
maygup01 0:11cc2b7889af 34 {
maygup01 0:11cc2b7889af 35 uint8_t *addr;
maygup01 0:11cc2b7889af 36 const char *p, *q;
maygup01 0:11cc2b7889af 37 int_fast8_t field_no, coloncolon = -1;
maygup01 0:11cc2b7889af 38
maygup01 0:11cc2b7889af 39 addr = dest;
maygup01 0:11cc2b7889af 40
maygup01 0:11cc2b7889af 41 if (len > 39) { // Too long, not possible. We do not support IPv4-mapped IPv6 addresses
maygup01 0:11cc2b7889af 42 goto error;
maygup01 0:11cc2b7889af 43 }
maygup01 0:11cc2b7889af 44
maygup01 0:11cc2b7889af 45 // First go forward the string, until end, noting :: position if any
maygup01 0:11cc2b7889af 46 // We're decrementing `len` as we go forward, and stop when it reaches 0
maygup01 0:11cc2b7889af 47 for (field_no = 0, p = ip6addr; len && *p; p = q + 1) {
maygup01 0:11cc2b7889af 48
maygup01 0:11cc2b7889af 49 for (q = p; len && *q && (*q != ':'); len -= 1) { // Seek for ':' or end
maygup01 0:11cc2b7889af 50 if (!is_hex(*q++)) { // There must only be hex characters besides ':'
maygup01 0:11cc2b7889af 51 goto error;
maygup01 0:11cc2b7889af 52 }
maygup01 0:11cc2b7889af 53 }
maygup01 0:11cc2b7889af 54
maygup01 0:11cc2b7889af 55 if ((q - p) > 4) { // We can't have more than 4 hex digits per segment
maygup01 0:11cc2b7889af 56 goto error;
maygup01 0:11cc2b7889af 57 }
maygup01 0:11cc2b7889af 58
maygup01 0:11cc2b7889af 59 if (field_no == 8) { // If the address goes farther than 8 segments
maygup01 0:11cc2b7889af 60 goto error;
maygup01 0:11cc2b7889af 61 }
maygup01 0:11cc2b7889af 62
maygup01 0:11cc2b7889af 63 // Convert and write this part, (high-endian AKA network byte order)
maygup01 0:11cc2b7889af 64 addr = common_write_16_bit(hex(p), addr);
maygup01 0:11cc2b7889af 65 field_no++;
maygup01 0:11cc2b7889af 66
maygup01 0:11cc2b7889af 67 // We handle the colons
maygup01 0:11cc2b7889af 68 if (len) {
maygup01 0:11cc2b7889af 69 // Check if we reached "::"
maygup01 0:11cc2b7889af 70 if (q[0] == ':' && q[1] == ':') {
maygup01 0:11cc2b7889af 71 if (coloncolon != -1) { // We are not supposed to see "::" more than once per address
maygup01 0:11cc2b7889af 72 goto error;
maygup01 0:11cc2b7889af 73 }
maygup01 0:11cc2b7889af 74 coloncolon = field_no;
maygup01 0:11cc2b7889af 75 q++;
maygup01 0:11cc2b7889af 76 len -= 2;
maygup01 0:11cc2b7889af 77 } else {
maygup01 0:11cc2b7889af 78 len -= 1;
maygup01 0:11cc2b7889af 79 }
maygup01 0:11cc2b7889af 80 }
maygup01 0:11cc2b7889af 81 }
maygup01 0:11cc2b7889af 82
maygup01 0:11cc2b7889af 83 if (coloncolon != -1) {
maygup01 0:11cc2b7889af 84 /* Insert zeros in the appropriate place */
maygup01 0:11cc2b7889af 85 uint_fast8_t head_size = 2 * coloncolon;
maygup01 0:11cc2b7889af 86 uint_fast8_t inserted_size = 2 * (8 - field_no);
maygup01 0:11cc2b7889af 87 uint_fast8_t tail_size = 16 - head_size - inserted_size;
maygup01 0:11cc2b7889af 88 addr = dest;
maygup01 0:11cc2b7889af 89 memmove(addr + head_size + inserted_size, addr + head_size, tail_size);
maygup01 0:11cc2b7889af 90 memset(addr + head_size, 0, inserted_size);
maygup01 0:11cc2b7889af 91 } else if (field_no != 8) { // Report an error if we didn't get 8 fields
maygup01 0:11cc2b7889af 92 goto error;
maygup01 0:11cc2b7889af 93 }
maygup01 0:11cc2b7889af 94 return true;
maygup01 0:11cc2b7889af 95
maygup01 0:11cc2b7889af 96 error:
maygup01 0:11cc2b7889af 97 // Fill the output buffer with 0 so we stick to the old failure behavior.
maygup01 0:11cc2b7889af 98 // We are however more agressive and wipe the entire address, and do so more often.
maygup01 0:11cc2b7889af 99 memset(dest, 0, 16);
maygup01 0:11cc2b7889af 100 return false;
maygup01 0:11cc2b7889af 101 }
maygup01 0:11cc2b7889af 102
maygup01 0:11cc2b7889af 103 unsigned char sipv6_prefixlength(const char *ip6addr)
maygup01 0:11cc2b7889af 104 {
maygup01 0:11cc2b7889af 105 char *ptr = strchr(ip6addr, '/');
maygup01 0:11cc2b7889af 106 if (ptr) {
maygup01 0:11cc2b7889af 107 return (unsigned char)strtoul(ptr + 1, 0, 10);
maygup01 0:11cc2b7889af 108 }
maygup01 0:11cc2b7889af 109 return 0;
maygup01 0:11cc2b7889af 110 }
maygup01 0:11cc2b7889af 111
maygup01 0:11cc2b7889af 112 int stoip6_prefix(const char *ip6addr, void *dest, int_fast16_t *prefix_len_out)
maygup01 0:11cc2b7889af 113 {
maygup01 0:11cc2b7889af 114 size_t addr_len, total_len;
maygup01 0:11cc2b7889af 115 int_fast16_t prefix_length;
maygup01 0:11cc2b7889af 116
maygup01 0:11cc2b7889af 117 if (prefix_len_out) {
maygup01 0:11cc2b7889af 118 *prefix_len_out = -1;
maygup01 0:11cc2b7889af 119 }
maygup01 0:11cc2b7889af 120
maygup01 0:11cc2b7889af 121 total_len = addr_len = strlen(ip6addr);
maygup01 0:11cc2b7889af 122 const char *ptr = strchr(ip6addr, '/');
maygup01 0:11cc2b7889af 123 if (ptr) {
maygup01 0:11cc2b7889af 124 addr_len = ptr - ip6addr;
maygup01 0:11cc2b7889af 125 if (prefix_len_out) {
maygup01 0:11cc2b7889af 126 if (total_len - addr_len > 3) {
maygup01 0:11cc2b7889af 127 /* too many digits in prefix */
maygup01 0:11cc2b7889af 128 return -1;
maygup01 0:11cc2b7889af 129 }
maygup01 0:11cc2b7889af 130
maygup01 0:11cc2b7889af 131 prefix_length = strtoul(ptr + 1, 0, 10);
maygup01 0:11cc2b7889af 132 if (prefix_length < 0 || prefix_length > 128) {
maygup01 0:11cc2b7889af 133 /* prefix value illegal */
maygup01 0:11cc2b7889af 134 return -1;
maygup01 0:11cc2b7889af 135 }
maygup01 0:11cc2b7889af 136
maygup01 0:11cc2b7889af 137 *prefix_len_out = prefix_length;
maygup01 0:11cc2b7889af 138 }
maygup01 0:11cc2b7889af 139 }
maygup01 0:11cc2b7889af 140
maygup01 0:11cc2b7889af 141 if (!stoip6(ip6addr, addr_len, dest)) {
maygup01 0:11cc2b7889af 142 /* parser failure */
maygup01 0:11cc2b7889af 143 return -1;
maygup01 0:11cc2b7889af 144 }
maygup01 0:11cc2b7889af 145
maygup01 0:11cc2b7889af 146 return 0;
maygup01 0:11cc2b7889af 147 }
maygup01 0:11cc2b7889af 148
maygup01 0:11cc2b7889af 149 static bool is_hex(char c)
maygup01 0:11cc2b7889af 150 {
maygup01 0:11cc2b7889af 151 // 'A' (0x41) and 'a' (0x61) are mapped in the ASCII table in such a way that masking the 0x20 bit turn 'a' in 'A'
maygup01 0:11cc2b7889af 152 if ((c & ~0x20) >= 'A' && (c & ~0x20) <= 'F') {
maygup01 0:11cc2b7889af 153 return true;
maygup01 0:11cc2b7889af 154 }
maygup01 0:11cc2b7889af 155
maygup01 0:11cc2b7889af 156 if (c >= '0' && c <= '9') {
maygup01 0:11cc2b7889af 157 return true;
maygup01 0:11cc2b7889af 158 }
maygup01 0:11cc2b7889af 159
maygup01 0:11cc2b7889af 160 return false;
maygup01 0:11cc2b7889af 161 }
maygup01 0:11cc2b7889af 162
maygup01 0:11cc2b7889af 163 static uint16_t hex(const char *p)
maygup01 0:11cc2b7889af 164 {
maygup01 0:11cc2b7889af 165 uint16_t val = 0;
maygup01 0:11cc2b7889af 166
maygup01 0:11cc2b7889af 167 for (;;) {
maygup01 0:11cc2b7889af 168 char c = *p++;
maygup01 0:11cc2b7889af 169 if ((c >= '0') && (c <= '9')) {
maygup01 0:11cc2b7889af 170 val = (val << 4) | (c - '0');
maygup01 0:11cc2b7889af 171 } else if ((c >= 'A') && (c <= 'F')) {
maygup01 0:11cc2b7889af 172 val = (val << 4) | (10 + (c - 'A'));
maygup01 0:11cc2b7889af 173 } else if ((c >= 'a') && (c <= 'f')) {
maygup01 0:11cc2b7889af 174 val = (val << 4) | (10 + (c - 'a'));
maygup01 0:11cc2b7889af 175 } else {
maygup01 0:11cc2b7889af 176 break; // Non hex character
maygup01 0:11cc2b7889af 177 }
maygup01 0:11cc2b7889af 178 }
maygup01 0:11cc2b7889af 179 return val;
maygup01 0:11cc2b7889af 180 }