This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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