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