Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
ip4tos.c
00001 /* 00002 * Copyright (c) 2014-2018 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 "ip4string.h" 00020 00021 static void ipv4_itoa(char *string, uint8_t byte); 00022 00023 /** 00024 * Print binary IPv4 address to a string. 00025 * String must contain enough room for full address, 16 bytes exact. 00026 * \param addr IPv4 address. 00027 * \p buffer to write string to. 00028 */ 00029 uint_fast8_t ip4tos(const void *ip4addr, char *p) 00030 { 00031 uint_fast8_t outputPos = 0; 00032 const uint8_t *byteArray = ip4addr; 00033 00034 for (uint_fast8_t component = 0; component < 4; ++component) { 00035 //Convert the byte to string 00036 ipv4_itoa(&p[outputPos], byteArray[component]); 00037 00038 //Move outputPos to the end of the string 00039 while (p[outputPos] != '\0') { 00040 outputPos += 1; 00041 } 00042 00043 //Append a dot if this is not the last digit 00044 if (component < 3) { 00045 p[outputPos++] = '.'; 00046 } 00047 } 00048 00049 // Return length of generated string, excluding the terminating null character 00050 return outputPos; 00051 } 00052 00053 static void ipv4_itoa(char *string, uint8_t byte) 00054 { 00055 char *baseString = string; 00056 00057 //Write the digits to the buffer from the least significant to the most 00058 // This is the incorrect order but we will swap later 00059 do { 00060 *string++ = '0' + byte % 10; 00061 byte /= 10; 00062 } while (byte); 00063 00064 //We put the final \0, then go back one step on the last digit for the swap 00065 *string-- = '\0'; 00066 00067 //We now swap the digits 00068 while (baseString < string) { 00069 uint8_t tmp = *string; 00070 *string-- = *baseString; 00071 *baseString++ = tmp; 00072 } 00073 }
Generated on Tue Jul 12 2022 13:54:25 by
