leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2014-2018 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16 #include <string.h>
leothedragon 0:8f0bb79ddd48 17 #include <stdlib.h>
leothedragon 0:8f0bb79ddd48 18 #include <stdint.h>
leothedragon 0:8f0bb79ddd48 19 #include "common_functions.h"
leothedragon 0:8f0bb79ddd48 20 #include "ip4string.h"
leothedragon 0:8f0bb79ddd48 21
leothedragon 0:8f0bb79ddd48 22 /**
leothedragon 0:8f0bb79ddd48 23 * Convert numeric IPv4 address string to a binary.
leothedragon 0:8f0bb79ddd48 24 * \param ip4addr IPv4 address in string format.
leothedragon 0:8f0bb79ddd48 25 * \param len Length of IPv4 string, maximum of 16..
leothedragon 0:8f0bb79ddd48 26 * \param dest buffer for address. MUST be 4 bytes.
leothedragon 0:8f0bb79ddd48 27 * \return boolean set to true if conversion succeded, false if it didn't
leothedragon 0:8f0bb79ddd48 28 */
leothedragon 0:8f0bb79ddd48 29 bool stoip4(const char *ip4addr, size_t len, void *dest)
leothedragon 0:8f0bb79ddd48 30 {
leothedragon 0:8f0bb79ddd48 31 uint8_t *addr = dest;
leothedragon 0:8f0bb79ddd48 32
leothedragon 0:8f0bb79ddd48 33 if (len > 16) { // Too long, not possible
leothedragon 0:8f0bb79ddd48 34 return false;
leothedragon 0:8f0bb79ddd48 35 }
leothedragon 0:8f0bb79ddd48 36
leothedragon 0:8f0bb79ddd48 37 uint_fast8_t stringLength = 0, byteCount = 0;
leothedragon 0:8f0bb79ddd48 38
leothedragon 0:8f0bb79ddd48 39 //Iterate over each component of the IP. The exit condition is in the middle of the loop
leothedragon 0:8f0bb79ddd48 40 while (true) {
leothedragon 0:8f0bb79ddd48 41
leothedragon 0:8f0bb79ddd48 42 //No valid character (IPv4 addresses don't have implicit 0, that is x.y..z being read as x.y.0.z)
leothedragon 0:8f0bb79ddd48 43 if (stringLength == len || ip4addr[stringLength] < '0' || ip4addr[stringLength] > '9') {
leothedragon 0:8f0bb79ddd48 44 return false;
leothedragon 0:8f0bb79ddd48 45 }
leothedragon 0:8f0bb79ddd48 46
leothedragon 0:8f0bb79ddd48 47 //For each component, we convert it to the raw value
leothedragon 0:8f0bb79ddd48 48 uint_fast16_t byte = 0;
leothedragon 0:8f0bb79ddd48 49 while (stringLength < len && ip4addr[stringLength] >= '0' && ip4addr[stringLength] <= '9') {
leothedragon 0:8f0bb79ddd48 50 byte *= 10;
leothedragon 0:8f0bb79ddd48 51 byte += ip4addr[stringLength++] - '0';
leothedragon 0:8f0bb79ddd48 52
leothedragon 0:8f0bb79ddd48 53 //We go over the maximum value for an IPv4 component
leothedragon 0:8f0bb79ddd48 54 if (byte > 0xff) {
leothedragon 0:8f0bb79ddd48 55 return false;
leothedragon 0:8f0bb79ddd48 56 }
leothedragon 0:8f0bb79ddd48 57 }
leothedragon 0:8f0bb79ddd48 58
leothedragon 0:8f0bb79ddd48 59 //Append the component
leothedragon 0:8f0bb79ddd48 60 addr[byteCount++] = (uint8_t) byte;
leothedragon 0:8f0bb79ddd48 61
leothedragon 0:8f0bb79ddd48 62 //If we're at the end, we leave the loop. It's the only way to reach the `true` output
leothedragon 0:8f0bb79ddd48 63 if (byteCount == 4) {
leothedragon 0:8f0bb79ddd48 64 break;
leothedragon 0:8f0bb79ddd48 65 }
leothedragon 0:8f0bb79ddd48 66
leothedragon 0:8f0bb79ddd48 67 //If the next character is invalid, we return false
leothedragon 0:8f0bb79ddd48 68 if (stringLength == len || ip4addr[stringLength++] != '.') {
leothedragon 0:8f0bb79ddd48 69 return false;
leothedragon 0:8f0bb79ddd48 70 }
leothedragon 0:8f0bb79ddd48 71 }
leothedragon 0:8f0bb79ddd48 72
leothedragon 0:8f0bb79ddd48 73 return stringLength == len || ip4addr[stringLength] == '\0';
leothedragon 0:8f0bb79ddd48 74 }