Mbed library for ENC28J60 Ethernet modules. Full support for TCP/IP and UDP Server, Client and HTTP server (webserver). DHCP and DNS is included.

Dependents:   mBuino_ENC28_MQTT Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC Serial_over_Ethernet ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers os2_ip6tos.c Source File

os2_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 "mbed_version.h"
00017 
00018 #if MBED_MAJOR_VERSION == 2
00019 
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include "common_functions.h"
00023 #include "ip6string.h"
00024 
00025 /**
00026  * Print binary IPv6 address to a string.
00027  * String must contain enough room for full address, 40 bytes exact.
00028  * IPv4 tunneling addresses are not covered.
00029  * \param addr IPv6 address.
00030  * \p buffer to write string to.
00031  */
00032 uint_fast8_t ip6tos(const void *ip6addr, char *p)
00033 {
00034     char *p_orig = p;
00035     uint_fast8_t zero_start = 255, zero_len = 1;
00036     const uint8_t *addr = ip6addr;
00037     uint_fast16_t part;
00038 
00039     /* Follow RFC 5952 - pre-scan for longest run of zeros */
00040     for (uint_fast8_t n = 0; n < 8; n++) {
00041         part = *addr++;
00042         part = (part << 8) | *addr++;
00043         if (part != 0) {
00044             continue;
00045         }
00046 
00047         /* We're at the start of a run of zeros - scan to non-zero (or end) */
00048         uint_fast8_t n0 = n;
00049         for (n = n0 + 1; n < 8; n++) {
00050             part = *addr++;
00051             part = (part << 8) | *addr++;
00052             if (part != 0) {
00053                 break;
00054             }
00055         }
00056 
00057         /* Now n0->initial zero of run, n->after final zero in run. Is this the
00058          * longest run yet? If equal, we stick with the previous one - RFC 5952
00059          * S4.2.3. Note that zero_len being initialised to 1 stops us
00060          * shortening a 1-part run (S4.2.2.)
00061          */
00062         if (n - n0 > zero_len) {
00063             zero_start = n0;
00064             zero_len = n - n0;
00065         }
00066 
00067         /* Continue scan for initial zeros from part n+1 - we've already
00068          * consumed part n, and know it's non-zero. */
00069     }
00070 
00071     /* Now go back and print, jumping over any zero run */
00072     addr = ip6addr;
00073     for (uint_fast8_t n = 0; n < 8;) {
00074         if (n == zero_start) {
00075             if (n == 0) {
00076                 *p++ = ':';
00077             }
00078             *p++ = ':';
00079             addr += 2 * zero_len;
00080             n += zero_len;
00081             continue;
00082         }
00083 
00084         part = *addr++;
00085         part = (part << 8) | *addr++;
00086         n++;
00087 
00088         p += sprintf(p, "%"PRIxFAST16, part);
00089 
00090         /* One iteration writes "part:" rather than ":part", and has the
00091          * explicit check for n == 8 below, to allow easy extension for
00092          * IPv4-in-IPv6-type addresses ("xxxx::xxxx:a.b.c.d"): we'd just
00093          * run the same loop for 6 parts, and output would then finish with the
00094          * required : or ::, ready for "a.b.c.d" to be tacked on.
00095          */
00096         if (n != 8) {
00097             *p++ = ':';
00098         }
00099     }
00100     *p = '\0';
00101 
00102     // Return length of generated string, excluding the terminating null character
00103     return p - p_orig;
00104 }
00105 
00106 uint_fast8_t ip6_prefix_tos(const void *prefix, uint_fast8_t prefix_len, char *p)
00107 {
00108     char *wptr = p;
00109     uint8_t addr[16] = {0};
00110 
00111     if (prefix_len > 128) {
00112         return 0;
00113     }
00114 
00115     // Generate prefix part of the string
00116     bitcopy(addr, prefix, prefix_len);
00117     wptr += ip6tos(addr, wptr);
00118     // Add the prefix length part of the string
00119     wptr += sprintf(wptr, "/%"PRIuFAST8, prefix_len);
00120 
00121     // Return total length of generated string
00122     return wptr - p;
00123 }
00124 #endif