leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16 #include <stdio.h>
leothedragon 0:8f0bb79ddd48 17 #include <string.h>
leothedragon 0:8f0bb79ddd48 18 #include "common_functions.h"
leothedragon 0:8f0bb79ddd48 19 #include "ip6string.h"
leothedragon 0:8f0bb79ddd48 20
leothedragon 0:8f0bb79ddd48 21 /**
leothedragon 0:8f0bb79ddd48 22 * Print binary IPv6 address to a string.
leothedragon 0:8f0bb79ddd48 23 * String must contain enough room for full address, 40 bytes exact.
leothedragon 0:8f0bb79ddd48 24 * IPv4 tunneling addresses are not covered.
leothedragon 0:8f0bb79ddd48 25 * \param addr IPv6 address.
leothedragon 0:8f0bb79ddd48 26 * \p buffer to write string to.
leothedragon 0:8f0bb79ddd48 27 */
leothedragon 0:8f0bb79ddd48 28 uint_fast8_t ip6tos(const void *ip6addr, char *p)
leothedragon 0:8f0bb79ddd48 29 {
leothedragon 0:8f0bb79ddd48 30 char *p_orig = p;
leothedragon 0:8f0bb79ddd48 31 uint_fast8_t zero_start = 255, zero_len = 1;
leothedragon 0:8f0bb79ddd48 32 const uint8_t *addr = ip6addr;
leothedragon 0:8f0bb79ddd48 33 uint_fast16_t part;
leothedragon 0:8f0bb79ddd48 34
leothedragon 0:8f0bb79ddd48 35 /* Follow RFC 5952 - pre-scan for longest run of zeros */
leothedragon 0:8f0bb79ddd48 36 for (uint_fast8_t n = 0; n < 8; n++) {
leothedragon 0:8f0bb79ddd48 37 part = *addr++;
leothedragon 0:8f0bb79ddd48 38 part = (part << 8) | *addr++;
leothedragon 0:8f0bb79ddd48 39 if (part != 0) {
leothedragon 0:8f0bb79ddd48 40 continue;
leothedragon 0:8f0bb79ddd48 41 }
leothedragon 0:8f0bb79ddd48 42
leothedragon 0:8f0bb79ddd48 43 /* We're at the start of a run of zeros - scan to non-zero (or end) */
leothedragon 0:8f0bb79ddd48 44 uint_fast8_t n0 = n;
leothedragon 0:8f0bb79ddd48 45 for (n = n0 + 1; n < 8; n++) {
leothedragon 0:8f0bb79ddd48 46 part = *addr++;
leothedragon 0:8f0bb79ddd48 47 part = (part << 8) | *addr++;
leothedragon 0:8f0bb79ddd48 48 if (part != 0) {
leothedragon 0:8f0bb79ddd48 49 break;
leothedragon 0:8f0bb79ddd48 50 }
leothedragon 0:8f0bb79ddd48 51 }
leothedragon 0:8f0bb79ddd48 52
leothedragon 0:8f0bb79ddd48 53 /* Now n0->initial zero of run, n->after final zero in run. Is this the
leothedragon 0:8f0bb79ddd48 54 * longest run yet? If equal, we stick with the previous one - RFC 5952
leothedragon 0:8f0bb79ddd48 55 * S4.2.3. Note that zero_len being initialised to 1 stops us
leothedragon 0:8f0bb79ddd48 56 * shortening a 1-part run (S4.2.2.)
leothedragon 0:8f0bb79ddd48 57 */
leothedragon 0:8f0bb79ddd48 58 if (n - n0 > zero_len) {
leothedragon 0:8f0bb79ddd48 59 zero_start = n0;
leothedragon 0:8f0bb79ddd48 60 zero_len = n - n0;
leothedragon 0:8f0bb79ddd48 61 }
leothedragon 0:8f0bb79ddd48 62
leothedragon 0:8f0bb79ddd48 63 /* Continue scan for initial zeros from part n+1 - we've already
leothedragon 0:8f0bb79ddd48 64 * consumed part n, and know it's non-zero. */
leothedragon 0:8f0bb79ddd48 65 }
leothedragon 0:8f0bb79ddd48 66
leothedragon 0:8f0bb79ddd48 67 /* Now go back and print, jumping over any zero run */
leothedragon 0:8f0bb79ddd48 68 addr = ip6addr;
leothedragon 0:8f0bb79ddd48 69 for (uint_fast8_t n = 0; n < 8;) {
leothedragon 0:8f0bb79ddd48 70 if (n == zero_start) {
leothedragon 0:8f0bb79ddd48 71 if (n == 0) {
leothedragon 0:8f0bb79ddd48 72 *p++ = ':';
leothedragon 0:8f0bb79ddd48 73 }
leothedragon 0:8f0bb79ddd48 74 *p++ = ':';
leothedragon 0:8f0bb79ddd48 75 addr += 2 * zero_len;
leothedragon 0:8f0bb79ddd48 76 n += zero_len;
leothedragon 0:8f0bb79ddd48 77 continue;
leothedragon 0:8f0bb79ddd48 78 }
leothedragon 0:8f0bb79ddd48 79
leothedragon 0:8f0bb79ddd48 80 part = *addr++;
leothedragon 0:8f0bb79ddd48 81 part = (part << 8) | *addr++;
leothedragon 0:8f0bb79ddd48 82 n++;
leothedragon 0:8f0bb79ddd48 83
leothedragon 0:8f0bb79ddd48 84 p += sprintf(p, "%"PRIxFAST16, part);
leothedragon 0:8f0bb79ddd48 85
leothedragon 0:8f0bb79ddd48 86 /* One iteration writes "part:" rather than ":part", and has the
leothedragon 0:8f0bb79ddd48 87 * explicit check for n == 8 below, to allow easy extension for
leothedragon 0:8f0bb79ddd48 88 * IPv4-in-IPv6-type addresses ("xxxx::xxxx:a.b.c.d"): we'd just
leothedragon 0:8f0bb79ddd48 89 * run the same loop for 6 parts, and output would then finish with the
leothedragon 0:8f0bb79ddd48 90 * required : or ::, ready for "a.b.c.d" to be tacked on.
leothedragon 0:8f0bb79ddd48 91 */
leothedragon 0:8f0bb79ddd48 92 if (n != 8) {
leothedragon 0:8f0bb79ddd48 93 *p++ = ':';
leothedragon 0:8f0bb79ddd48 94 }
leothedragon 0:8f0bb79ddd48 95 }
leothedragon 0:8f0bb79ddd48 96 *p = '\0';
leothedragon 0:8f0bb79ddd48 97
leothedragon 0:8f0bb79ddd48 98 // Return length of generated string, excluding the terminating null character
leothedragon 0:8f0bb79ddd48 99 return p - p_orig;
leothedragon 0:8f0bb79ddd48 100 }
leothedragon 0:8f0bb79ddd48 101
leothedragon 0:8f0bb79ddd48 102 uint_fast8_t ip6_prefix_tos(const void *prefix, uint_fast8_t prefix_len, char *p)
leothedragon 0:8f0bb79ddd48 103 {
leothedragon 0:8f0bb79ddd48 104 char *wptr = p;
leothedragon 0:8f0bb79ddd48 105 uint8_t addr[16] = {0};
leothedragon 0:8f0bb79ddd48 106
leothedragon 0:8f0bb79ddd48 107 if (prefix_len > 128) {
leothedragon 0:8f0bb79ddd48 108 return 0;
leothedragon 0:8f0bb79ddd48 109 }
leothedragon 0:8f0bb79ddd48 110
leothedragon 0:8f0bb79ddd48 111 // Generate prefix part of the string
leothedragon 0:8f0bb79ddd48 112 bitcopy(addr, prefix, prefix_len);
leothedragon 0:8f0bb79ddd48 113 wptr += ip6tos(addr, wptr);
leothedragon 0:8f0bb79ddd48 114 // Add the prefix length part of the string
leothedragon 0:8f0bb79ddd48 115 wptr += sprintf(wptr, "/%"PRIuFAST8, prefix_len);
leothedragon 0:8f0bb79ddd48 116
leothedragon 0:8f0bb79ddd48 117 // Return total length of generated string
leothedragon 0:8f0bb79ddd48 118 return wptr - p;
leothedragon 0:8f0bb79ddd48 119 }