![](/media/cache/profiles/debfdc81854eac26ec993b55a659c6e1.jpg.50x50_q85.jpg)
mbed client lightswitch demo
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
nanostack-libservice/source/libip6string/ip6tos.c@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /* |
mbedAustin | 11:cada08fc8a70 | 2 | * Copyright (c) 2014-2015 ARM Limited. All rights reserved. |
mbedAustin | 11:cada08fc8a70 | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbedAustin | 11:cada08fc8a70 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
mbedAustin | 11:cada08fc8a70 | 5 | * not use this file except in compliance with the License. |
mbedAustin | 11:cada08fc8a70 | 6 | * You may obtain a copy of the License at |
mbedAustin | 11:cada08fc8a70 | 7 | * |
mbedAustin | 11:cada08fc8a70 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 11:cada08fc8a70 | 9 | * |
mbedAustin | 11:cada08fc8a70 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 11:cada08fc8a70 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
mbedAustin | 11:cada08fc8a70 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 11:cada08fc8a70 | 13 | * See the License for the specific language governing permissions and |
mbedAustin | 11:cada08fc8a70 | 14 | * limitations under the License. |
mbedAustin | 11:cada08fc8a70 | 15 | */ |
mbedAustin | 11:cada08fc8a70 | 16 | #include <stdio.h> |
mbedAustin | 11:cada08fc8a70 | 17 | #include <inttypes.h> |
mbedAustin | 11:cada08fc8a70 | 18 | #include "ip6string.h" |
mbedAustin | 11:cada08fc8a70 | 19 | |
mbedAustin | 11:cada08fc8a70 | 20 | /** |
mbedAustin | 11:cada08fc8a70 | 21 | * Print binary IPv6 address to a string. |
mbedAustin | 11:cada08fc8a70 | 22 | * String must contain enough room for full address, 40 bytes exact. |
mbedAustin | 11:cada08fc8a70 | 23 | * IPv4 tunneling addresses are not covered. |
mbedAustin | 11:cada08fc8a70 | 24 | * \param addr IPv6 address. |
mbedAustin | 11:cada08fc8a70 | 25 | * \p buffer to write string to. |
mbedAustin | 11:cada08fc8a70 | 26 | */ |
mbedAustin | 11:cada08fc8a70 | 27 | void ip6tos(const void *ip6addr, char *p) |
mbedAustin | 11:cada08fc8a70 | 28 | { |
mbedAustin | 11:cada08fc8a70 | 29 | uint_fast8_t zero_start = 255, zero_len = 1; |
mbedAustin | 11:cada08fc8a70 | 30 | const uint8_t *addr = ip6addr; |
mbedAustin | 11:cada08fc8a70 | 31 | uint_fast16_t part; |
mbedAustin | 11:cada08fc8a70 | 32 | |
mbedAustin | 11:cada08fc8a70 | 33 | /* Follow RFC 5952 - pre-scan for longest run of zeros */ |
mbedAustin | 11:cada08fc8a70 | 34 | for (uint_fast8_t n = 0; n < 8; n++) { |
mbedAustin | 11:cada08fc8a70 | 35 | part = *addr++; |
mbedAustin | 11:cada08fc8a70 | 36 | part = (part << 8) | *addr++; |
mbedAustin | 11:cada08fc8a70 | 37 | if (part != 0) { |
mbedAustin | 11:cada08fc8a70 | 38 | continue; |
mbedAustin | 11:cada08fc8a70 | 39 | } |
mbedAustin | 11:cada08fc8a70 | 40 | |
mbedAustin | 11:cada08fc8a70 | 41 | /* We're at the start of a run of zeros - scan to non-zero (or end) */ |
mbedAustin | 11:cada08fc8a70 | 42 | uint_fast8_t n0 = n; |
mbedAustin | 11:cada08fc8a70 | 43 | for (n = n0 + 1; n < 8; n++) { |
mbedAustin | 11:cada08fc8a70 | 44 | part = *addr++; |
mbedAustin | 11:cada08fc8a70 | 45 | part = (part << 8) | *addr++; |
mbedAustin | 11:cada08fc8a70 | 46 | if (part != 0) { |
mbedAustin | 11:cada08fc8a70 | 47 | break; |
mbedAustin | 11:cada08fc8a70 | 48 | } |
mbedAustin | 11:cada08fc8a70 | 49 | } |
mbedAustin | 11:cada08fc8a70 | 50 | |
mbedAustin | 11:cada08fc8a70 | 51 | /* Now n0->initial zero of run, n->after final zero in run. Is this the |
mbedAustin | 11:cada08fc8a70 | 52 | * longest run yet? If equal, we stick with the previous one - RFC 5952 |
mbedAustin | 11:cada08fc8a70 | 53 | * S4.2.3. Note that zero_len being initialised to 1 stops us |
mbedAustin | 11:cada08fc8a70 | 54 | * shortening a 1-part run (S4.2.2.) |
mbedAustin | 11:cada08fc8a70 | 55 | */ |
mbedAustin | 11:cada08fc8a70 | 56 | if (n - n0 > zero_len) { |
mbedAustin | 11:cada08fc8a70 | 57 | zero_start = n0; |
mbedAustin | 11:cada08fc8a70 | 58 | zero_len = n - n0; |
mbedAustin | 11:cada08fc8a70 | 59 | } |
mbedAustin | 11:cada08fc8a70 | 60 | |
mbedAustin | 11:cada08fc8a70 | 61 | /* Continue scan for initial zeros from part n+1 - we've already |
mbedAustin | 11:cada08fc8a70 | 62 | * consumed part n, and know it's non-zero. */ |
mbedAustin | 11:cada08fc8a70 | 63 | } |
mbedAustin | 11:cada08fc8a70 | 64 | |
mbedAustin | 11:cada08fc8a70 | 65 | /* Now go back and print, jumping over any zero run */ |
mbedAustin | 11:cada08fc8a70 | 66 | addr = ip6addr; |
mbedAustin | 11:cada08fc8a70 | 67 | for (uint_fast8_t n = 0; n < 8;) { |
mbedAustin | 11:cada08fc8a70 | 68 | if (n == zero_start) { |
mbedAustin | 11:cada08fc8a70 | 69 | if (n == 0) { |
mbedAustin | 11:cada08fc8a70 | 70 | *p++ = ':'; |
mbedAustin | 11:cada08fc8a70 | 71 | } |
mbedAustin | 11:cada08fc8a70 | 72 | *p++ = ':'; |
mbedAustin | 11:cada08fc8a70 | 73 | addr += 2 * zero_len; |
mbedAustin | 11:cada08fc8a70 | 74 | n += zero_len; |
mbedAustin | 11:cada08fc8a70 | 75 | continue; |
mbedAustin | 11:cada08fc8a70 | 76 | } |
mbedAustin | 11:cada08fc8a70 | 77 | |
mbedAustin | 11:cada08fc8a70 | 78 | part = *addr++; |
mbedAustin | 11:cada08fc8a70 | 79 | part = (part << 8) | *addr++; |
mbedAustin | 11:cada08fc8a70 | 80 | n++; |
mbedAustin | 11:cada08fc8a70 | 81 | |
mbedAustin | 11:cada08fc8a70 | 82 | p += sprintf(p, "%"PRIxFAST16, part); |
mbedAustin | 11:cada08fc8a70 | 83 | |
mbedAustin | 11:cada08fc8a70 | 84 | /* One iteration writes "part:" rather than ":part", and has the |
mbedAustin | 11:cada08fc8a70 | 85 | * explicit check for n == 8 below, to allow easy extension for |
mbedAustin | 11:cada08fc8a70 | 86 | * IPv4-in-IPv6-type addresses ("xxxx::xxxx:a.b.c.d"): we'd just |
mbedAustin | 11:cada08fc8a70 | 87 | * run the same loop for 6 parts, and output would then finish with the |
mbedAustin | 11:cada08fc8a70 | 88 | * required : or ::, ready for "a.b.c.d" to be tacked on. |
mbedAustin | 11:cada08fc8a70 | 89 | */ |
mbedAustin | 11:cada08fc8a70 | 90 | if (n != 8) { |
mbedAustin | 11:cada08fc8a70 | 91 | *p++ = ':'; |
mbedAustin | 11:cada08fc8a70 | 92 | } |
mbedAustin | 11:cada08fc8a70 | 93 | } |
mbedAustin | 11:cada08fc8a70 | 94 | *p++ = '\0'; |
mbedAustin | 11:cada08fc8a70 | 95 | } |