1 /* 2 Copyright 2021 Northern.tech AS 3 4 This file is part of CFEngine 3 - written and maintained by Northern.tech AS. 5 6 This program is free software; you can redistribute it and/or modify it 7 under the terms of the GNU General Public License as published by the 8 Free Software Foundation; version 3. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 18 19 To the extent this program is licensed as part of the Enterprise 20 versions of CFEngine, the applicable Commercial Open Source License 21 (COSL) may apply to this file if you as a licensee so wish it. See 22 included file COSL.txt. 23 */ 24 25 #ifndef CFENGINE_IP_ADDRESS_H 26 #define CFENGINE_IP_ADDRESS_H 27 28 #include <buffer.h> 29 30 typedef struct IPAddress IPAddress; 31 typedef enum 32 { 33 IP_ADDRESS_TYPE_IPV4, 34 IP_ADDRESS_TYPE_IPV6 35 } IPAddressVersion; 36 37 /** 38 @brief Creates a new IPAddress object from a string. 39 @param source Buffer containing the string representation of the ip address. 40 @return A fully formed IPAddress object or NULL if there was an error parsing the source. 41 */ 42 IPAddress *IPAddressNew(Buffer *source); 43 44 /** 45 @brief Creates a new IPAddress object from a hex string (as in procfs). 46 @param source Buffer containing the string representation of the ip address. 47 @return A fully formed IPAddress object or NULL if there was an error parsing the source. 48 */ 49 IPAddress *IPAddressNewHex(Buffer *source); 50 51 /** 52 @brief Destroys an IPAddress object. 53 @param address IPAddress object to be destroyed. 54 */ 55 int IPAddressDestroy(IPAddress **address); 56 /** 57 @brief Returns the type of address. 58 @param address Address object. 59 @return The type of address or -1 in case of error. 60 */ 61 int IPAddressType(IPAddress *address); 62 /** 63 @brief Produces a fully usable IPV6 or IPV4 address string representation. 64 @param address IPAddress object. 65 @return A buffer containing an IPV4 or IPV6 address or NULL in case the given address was invalid. 66 */ 67 Buffer *IPAddressGetAddress(IPAddress *address); 68 /** 69 @brief Recovers the appropriate port from the given address. 70 @param address IPAddress object. 71 @return A valid port for connections or -1 if it was not available. 72 */ 73 int IPAddressGetPort(IPAddress *address); 74 /** 75 @brief Compares two IP addresses. 76 @param a IP address of the first object. 77 @param b IP address of the second object. 78 @return 1 if both addresses are equal, 0 if they are not and -1 in case of error. 79 */ 80 int IPAddressIsEqual(IPAddress *a, IPAddress *b); 81 /** 82 @brief Checks if a given string is a properly formed IP Address. 83 @param source Buffer containing the string. 84 @param address Optional parameter. If given and not NULL then an IPAdress structure will be created from the string. 85 @return Returns true if the string is a valid IP Address and false if not. The address parameter is populated accordingly. 86 */ 87 bool IPAddressIsIPAddress(Buffer *source, IPAddress **address); 88 /** 89 @brief Compares two IP addresses for sorting. 90 @param a IP address of the first object. 91 @param b IP address of the second object. 92 @return true if a < b, false otherwise. 93 */ 94 bool IPAddressCompareLess(IPAddress *a, IPAddress *b); 95 /** 96 * @brief Check if string is localhost IPv4-/IPv6 addresses. 97 * 98 * For IPv4, any address in the range 127.0.0.0/8 is local host. E.g. 99 * '127.0.0.1', '127.1.2.3', 127.0.0.0' or '127.255.255.255'. 100 * 101 * For IPv6, '::1' is the one and only local host address. IPv6 102 * addresses can be written in long- and short-form, as well as 103 * something in-between (e.g. 0:0:0:0:0:0:0:1 / ::1 / 0:0::0:1). All of 104 * these forms are accepted. 105 * @param str The string to check. 106 * @return True if string is localhost, else false. 107 */ 108 bool StringIsLocalHostIP(const char *str); 109 110 #endif // CFENGINE_IP_ADDRESS_H 111