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