Austin Blackstone / Mbed 2 deprecated mbed-client-classic-example-lwip

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ip6tos.c Source File

ip6tos.c

00001 /*
00002  * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include <stdio.h>
00017 #include <inttypes.h>
00018 #include "ip6string.h"
00019 
00020 /**
00021  * Print binary IPv6 address to a string.
00022  * String must contain enough room for full address, 40 bytes exact.
00023  * IPv4 tunneling addresses are not covered.
00024  * \param addr IPv6 address.
00025  * \p buffer to write string to.
00026  */
00027 void ip6tos(const void *ip6addr, char *p)
00028 {
00029     uint_fast8_t zero_start = 255, zero_len = 1;
00030     const uint8_t *addr = ip6addr;
00031     uint_fast16_t part;
00032 
00033     /* Follow RFC 5952 - pre-scan for longest run of zeros */
00034     for (uint_fast8_t n = 0; n < 8; n++) {
00035         part = *addr++;
00036         part = (part << 8) | *addr++;
00037         if (part != 0) {
00038             continue;
00039         }
00040 
00041         /* We're at the start of a run of zeros - scan to non-zero (or end) */
00042         uint_fast8_t n0 = n;
00043         for (n = n0 + 1; n < 8; n++) {
00044             part = *addr++;
00045             part = (part << 8) | *addr++;
00046             if (part != 0) {
00047                 break;
00048             }
00049         }
00050 
00051         /* Now n0->initial zero of run, n->after final zero in run. Is this the
00052          * longest run yet? If equal, we stick with the previous one - RFC 5952
00053          * S4.2.3. Note that zero_len being initialised to 1 stops us
00054          * shortening a 1-part run (S4.2.2.)
00055          */
00056         if (n - n0 > zero_len) {
00057             zero_start = n0;
00058             zero_len = n - n0;
00059         }
00060 
00061         /* Continue scan for initial zeros from part n+1 - we've already
00062          * consumed part n, and know it's non-zero. */
00063     }
00064 
00065     /* Now go back and print, jumping over any zero run */
00066     addr = ip6addr;
00067     for (uint_fast8_t n = 0; n < 8;) {
00068         if (n == zero_start) {
00069             if (n == 0) {
00070                 *p++ = ':';
00071             }
00072             *p++ = ':';
00073             addr += 2 * zero_len;
00074             n += zero_len;
00075             continue;
00076         }
00077 
00078         part = *addr++;
00079         part = (part << 8) | *addr++;
00080         n++;
00081 
00082         p += sprintf(p, "%"PRIxFAST16, part);
00083 
00084         /* One iteration writes "part:" rather than ":part", and has the
00085          * explicit check for n == 8 below, to allow easy extension for
00086          * IPv4-in-IPv6-type addresses ("xxxx::xxxx:a.b.c.d"): we'd just
00087          * run the same loop for 6 parts, and output would then finish with the
00088          * required : or ::, ready for "a.b.c.d" to be tacked on.
00089          */
00090         if (n != 8) {
00091             *p++ = ':';
00092         }
00093     }
00094     *p++ = '\0';
00095 }