Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

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