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