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