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