WORKS
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
mbd_os/features/FEATURE_COMMON_PAL/nanostack-libservice/source/libip6string/stoip6.c@9:6bb35cef007d, 2016-10-22 (annotated)
- Committer:
- cyberjoey
- Date:
- Sat Oct 22 01:31:58 2016 +0000
- Revision:
- 9:6bb35cef007d
- Parent:
- 1:55a6170b404f
WORKING
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | /* |
nexpaq | 1:55a6170b404f | 2 | * Copyright (c) 2014-2015 ARM Limited. All rights reserved. |
nexpaq | 1:55a6170b404f | 3 | * SPDX-License-Identifier: Apache-2.0 |
nexpaq | 1:55a6170b404f | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
nexpaq | 1:55a6170b404f | 5 | * not use this file except in compliance with the License. |
nexpaq | 1:55a6170b404f | 6 | * You may obtain a copy of the License at |
nexpaq | 1:55a6170b404f | 7 | * |
nexpaq | 1:55a6170b404f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 1:55a6170b404f | 9 | * |
nexpaq | 1:55a6170b404f | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 1:55a6170b404f | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
nexpaq | 1:55a6170b404f | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 1:55a6170b404f | 13 | * See the License for the specific language governing permissions and |
nexpaq | 1:55a6170b404f | 14 | * limitations under the License. |
nexpaq | 1:55a6170b404f | 15 | */ |
nexpaq | 1:55a6170b404f | 16 | #include <string.h> |
nexpaq | 1:55a6170b404f | 17 | #include <stdlib.h> |
nexpaq | 1:55a6170b404f | 18 | #include <stdint.h> |
nexpaq | 1:55a6170b404f | 19 | #include "common_functions.h" |
nexpaq | 1:55a6170b404f | 20 | #include "ip6string.h" |
nexpaq | 1:55a6170b404f | 21 | |
nexpaq | 1:55a6170b404f | 22 | static uint16_t hex(const char *p); |
nexpaq | 1:55a6170b404f | 23 | |
nexpaq | 1:55a6170b404f | 24 | /** |
nexpaq | 1:55a6170b404f | 25 | * Convert numeric IPv6 address string to a binary. |
nexpaq | 1:55a6170b404f | 26 | * IPv4 tunnelling addresses are not covered. |
nexpaq | 1:55a6170b404f | 27 | * \param ip6addr IPv6 address in string format. |
nexpaq | 1:55a6170b404f | 28 | * \param len Length of ipv6 string. |
nexpaq | 1:55a6170b404f | 29 | * \param dest buffer for address. MUST be 16 bytes. |
nexpaq | 1:55a6170b404f | 30 | */ |
nexpaq | 1:55a6170b404f | 31 | void stoip6(const char *ip6addr, size_t len, void *dest) |
nexpaq | 1:55a6170b404f | 32 | { |
nexpaq | 1:55a6170b404f | 33 | uint8_t *addr; |
nexpaq | 1:55a6170b404f | 34 | const char *p, *q; |
nexpaq | 1:55a6170b404f | 35 | int_fast8_t field_no, coloncolon = -1; |
nexpaq | 1:55a6170b404f | 36 | |
nexpaq | 1:55a6170b404f | 37 | addr = dest; |
nexpaq | 1:55a6170b404f | 38 | |
nexpaq | 1:55a6170b404f | 39 | if (len > 39) { // Too long, not possible. We do not support IPv4-mapped IPv6 addresses |
nexpaq | 1:55a6170b404f | 40 | return; |
nexpaq | 1:55a6170b404f | 41 | } |
nexpaq | 1:55a6170b404f | 42 | |
nexpaq | 1:55a6170b404f | 43 | // First go forward the string, until end, noting :: position if any |
nexpaq | 1:55a6170b404f | 44 | for (field_no = 0, p = ip6addr; (len > (p - ip6addr)) && *p && field_no < 8; p = q + 1) { |
nexpaq | 1:55a6170b404f | 45 | q = p; |
nexpaq | 1:55a6170b404f | 46 | // Seek for ':' or end |
nexpaq | 1:55a6170b404f | 47 | while (*q && (*q != ':')) { |
nexpaq | 1:55a6170b404f | 48 | q++; |
nexpaq | 1:55a6170b404f | 49 | } |
nexpaq | 1:55a6170b404f | 50 | //Convert and write this part, (high-endian AKA network byte order) |
nexpaq | 1:55a6170b404f | 51 | addr = common_write_16_bit(hex(p), addr); |
nexpaq | 1:55a6170b404f | 52 | field_no++; |
nexpaq | 1:55a6170b404f | 53 | //Check if we reached "::" |
nexpaq | 1:55a6170b404f | 54 | if ((len > (q - ip6addr)) && *q && (q[0] == ':') && (q[1] == ':')) { |
nexpaq | 1:55a6170b404f | 55 | coloncolon = field_no; |
nexpaq | 1:55a6170b404f | 56 | q++; |
nexpaq | 1:55a6170b404f | 57 | } |
nexpaq | 1:55a6170b404f | 58 | } |
nexpaq | 1:55a6170b404f | 59 | |
nexpaq | 1:55a6170b404f | 60 | if (coloncolon != -1) { |
nexpaq | 1:55a6170b404f | 61 | /* Insert zeros in the appropriate place */ |
nexpaq | 1:55a6170b404f | 62 | uint_fast8_t head_size = 2 * coloncolon; |
nexpaq | 1:55a6170b404f | 63 | uint_fast8_t inserted_size = 2 * (8 - field_no); |
nexpaq | 1:55a6170b404f | 64 | uint_fast8_t tail_size = 16 - head_size - inserted_size; |
nexpaq | 1:55a6170b404f | 65 | addr = dest; |
nexpaq | 1:55a6170b404f | 66 | memmove(addr + head_size + inserted_size, addr + head_size, tail_size); |
nexpaq | 1:55a6170b404f | 67 | memset(addr + head_size, 0, inserted_size); |
nexpaq | 1:55a6170b404f | 68 | } else if (field_no != 8) { |
nexpaq | 1:55a6170b404f | 69 | /* Should really report an error if we didn't get 8 fields */ |
nexpaq | 1:55a6170b404f | 70 | memset(addr, 0, 16 - field_no * 2); |
nexpaq | 1:55a6170b404f | 71 | } |
nexpaq | 1:55a6170b404f | 72 | } |
nexpaq | 1:55a6170b404f | 73 | unsigned char sipv6_prefixlength(const char *ip6addr) |
nexpaq | 1:55a6170b404f | 74 | { |
nexpaq | 1:55a6170b404f | 75 | char *ptr = strchr(ip6addr, '/'); |
nexpaq | 1:55a6170b404f | 76 | if (ptr) { |
nexpaq | 1:55a6170b404f | 77 | return (unsigned char)strtoul(ptr + 1, 0, 10); |
nexpaq | 1:55a6170b404f | 78 | } |
nexpaq | 1:55a6170b404f | 79 | return 0; |
nexpaq | 1:55a6170b404f | 80 | } |
nexpaq | 1:55a6170b404f | 81 | static uint16_t hex(const char *p) |
nexpaq | 1:55a6170b404f | 82 | { |
nexpaq | 1:55a6170b404f | 83 | uint16_t val = 0; |
nexpaq | 1:55a6170b404f | 84 | |
nexpaq | 1:55a6170b404f | 85 | for (;;) { |
nexpaq | 1:55a6170b404f | 86 | char c = *p++; |
nexpaq | 1:55a6170b404f | 87 | if ((c >= '0') && (c <= '9')) { |
nexpaq | 1:55a6170b404f | 88 | val = (val << 4) | (c - '0'); |
nexpaq | 1:55a6170b404f | 89 | } else if ((c >= 'A') && (c <= 'F')) { |
nexpaq | 1:55a6170b404f | 90 | val = (val << 4) | (10 + (c - 'A')); |
nexpaq | 1:55a6170b404f | 91 | } else if ((c >= 'a') && (c <= 'f')) { |
nexpaq | 1:55a6170b404f | 92 | val = (val << 4) | (10 + (c - 'a')); |
nexpaq | 1:55a6170b404f | 93 | } else { |
nexpaq | 1:55a6170b404f | 94 | break; // Non hex character |
nexpaq | 1:55a6170b404f | 95 | } |
nexpaq | 1:55a6170b404f | 96 | } |
nexpaq | 1:55a6170b404f | 97 | return val; |
nexpaq | 1:55a6170b404f | 98 | } |