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 <stdio.h>
maclobdell 0:f7c60d3e7b8a 17 #include <inttypes.h>
maclobdell 0:f7c60d3e7b8a 18 #include "ip6string.h"
maclobdell 0:f7c60d3e7b8a 19
maclobdell 0:f7c60d3e7b8a 20 /**
maclobdell 0:f7c60d3e7b8a 21 * Print binary IPv6 address to a string.
maclobdell 0:f7c60d3e7b8a 22 * String must contain enough room for full address, 40 bytes exact.
maclobdell 0:f7c60d3e7b8a 23 * IPv4 tunneling addresses are not covered.
maclobdell 0:f7c60d3e7b8a 24 * \param addr IPv6 address.
maclobdell 0:f7c60d3e7b8a 25 * \p buffer to write string to.
maclobdell 0:f7c60d3e7b8a 26 */
maclobdell 0:f7c60d3e7b8a 27 void ip6tos(const void *ip6addr, char *p)
maclobdell 0:f7c60d3e7b8a 28 {
maclobdell 0:f7c60d3e7b8a 29 uint_fast8_t zero_start = 255, zero_len = 1;
maclobdell 0:f7c60d3e7b8a 30 const uint8_t *addr = ip6addr;
maclobdell 0:f7c60d3e7b8a 31 uint_fast16_t part;
maclobdell 0:f7c60d3e7b8a 32
maclobdell 0:f7c60d3e7b8a 33 /* Follow RFC 5952 - pre-scan for longest run of zeros */
maclobdell 0:f7c60d3e7b8a 34 for (uint_fast8_t n = 0; n < 8; n++) {
maclobdell 0:f7c60d3e7b8a 35 part = *addr++;
maclobdell 0:f7c60d3e7b8a 36 part = (part << 8) | *addr++;
maclobdell 0:f7c60d3e7b8a 37 if (part != 0) {
maclobdell 0:f7c60d3e7b8a 38 continue;
maclobdell 0:f7c60d3e7b8a 39 }
maclobdell 0:f7c60d3e7b8a 40
maclobdell 0:f7c60d3e7b8a 41 /* We're at the start of a run of zeros - scan to non-zero (or end) */
maclobdell 0:f7c60d3e7b8a 42 uint_fast8_t n0 = n;
maclobdell 0:f7c60d3e7b8a 43 for (n = n0 + 1; n < 8; n++) {
maclobdell 0:f7c60d3e7b8a 44 part = *addr++;
maclobdell 0:f7c60d3e7b8a 45 part = (part << 8) | *addr++;
maclobdell 0:f7c60d3e7b8a 46 if (part != 0) {
maclobdell 0:f7c60d3e7b8a 47 break;
maclobdell 0:f7c60d3e7b8a 48 }
maclobdell 0:f7c60d3e7b8a 49 }
maclobdell 0:f7c60d3e7b8a 50
maclobdell 0:f7c60d3e7b8a 51 /* Now n0->initial zero of run, n->after final zero in run. Is this the
maclobdell 0:f7c60d3e7b8a 52 * longest run yet? If equal, we stick with the previous one - RFC 5952
maclobdell 0:f7c60d3e7b8a 53 * S4.2.3. Note that zero_len being initialised to 1 stops us
maclobdell 0:f7c60d3e7b8a 54 * shortening a 1-part run (S4.2.2.)
maclobdell 0:f7c60d3e7b8a 55 */
maclobdell 0:f7c60d3e7b8a 56 if (n - n0 > zero_len) {
maclobdell 0:f7c60d3e7b8a 57 zero_start = n0;
maclobdell 0:f7c60d3e7b8a 58 zero_len = n - n0;
maclobdell 0:f7c60d3e7b8a 59 }
maclobdell 0:f7c60d3e7b8a 60
maclobdell 0:f7c60d3e7b8a 61 /* Continue scan for initial zeros from part n+1 - we've already
maclobdell 0:f7c60d3e7b8a 62 * consumed part n, and know it's non-zero. */
maclobdell 0:f7c60d3e7b8a 63 }
maclobdell 0:f7c60d3e7b8a 64
maclobdell 0:f7c60d3e7b8a 65 /* Now go back and print, jumping over any zero run */
maclobdell 0:f7c60d3e7b8a 66 addr = ip6addr;
maclobdell 0:f7c60d3e7b8a 67 for (uint_fast8_t n = 0; n < 8;) {
maclobdell 0:f7c60d3e7b8a 68 if (n == zero_start) {
maclobdell 0:f7c60d3e7b8a 69 if (n == 0) {
maclobdell 0:f7c60d3e7b8a 70 *p++ = ':';
maclobdell 0:f7c60d3e7b8a 71 }
maclobdell 0:f7c60d3e7b8a 72 *p++ = ':';
maclobdell 0:f7c60d3e7b8a 73 addr += 2 * zero_len;
maclobdell 0:f7c60d3e7b8a 74 n += zero_len;
maclobdell 0:f7c60d3e7b8a 75 continue;
maclobdell 0:f7c60d3e7b8a 76 }
maclobdell 0:f7c60d3e7b8a 77
maclobdell 0:f7c60d3e7b8a 78 part = *addr++;
maclobdell 0:f7c60d3e7b8a 79 part = (part << 8) | *addr++;
maclobdell 0:f7c60d3e7b8a 80 n++;
maclobdell 0:f7c60d3e7b8a 81
maclobdell 0:f7c60d3e7b8a 82 p += sprintf(p, "%"PRIxFAST16, part);
maclobdell 0:f7c60d3e7b8a 83
maclobdell 0:f7c60d3e7b8a 84 /* One iteration writes "part:" rather than ":part", and has the
maclobdell 0:f7c60d3e7b8a 85 * explicit check for n == 8 below, to allow easy extension for
maclobdell 0:f7c60d3e7b8a 86 * IPv4-in-IPv6-type addresses ("xxxx::xxxx:a.b.c.d"): we'd just
maclobdell 0:f7c60d3e7b8a 87 * run the same loop for 6 parts, and output would then finish with the
maclobdell 0:f7c60d3e7b8a 88 * required : or ::, ready for "a.b.c.d" to be tacked on.
maclobdell 0:f7c60d3e7b8a 89 */
maclobdell 0:f7c60d3e7b8a 90 if (n != 8) {
maclobdell 0:f7c60d3e7b8a 91 *p++ = ':';
maclobdell 0:f7c60d3e7b8a 92 }
maclobdell 0:f7c60d3e7b8a 93 }
maclobdell 0:f7c60d3e7b8a 94 *p++ = '\0';
maclobdell 0:f7c60d3e7b8a 95 }