00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org> 00002 // 00003 // This program is free software; you can redistribute it and/or 00004 // modify it under the terms of the GNU General Public License 00005 // as published by the Free Software Foundation; either version 2 00006 // of the License, or (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 00016 00017 /*! 00018 * @defgroup addr Internet address conversion 00019 * 00020 * The addr_htos(), addr_hton(), addr_stoh() and addr_ntoh() functions convert 00021 * from host- to network-byteorder, and vice versa, respectively. 00022 * 00023 * The addr_from_str() function converts the Internet host address in standard 00024 * numbers-and-dots notation pointed to by the string str into binary data and 00025 * stores result in the ip/mask pair of pointers. addr_from_str() returns 0 if 00026 * no argument was converted, 1 if ip was converted, 2 for mask and 3 for both. 00027 * 00028 * The addr_to_str() function converts the Internet host address given in the 00029 * ip/mask pair of pointers to a string in standard numbers-and-dots notation. 00030 * The returned string is obtained by malloc and should be free(3)'d by the 00031 * caller. 00032 * 00033 * @{ 00034 */ 00035 00036 #ifndef _LUCID_ADDR_H 00037 #define _LUCID_ADDR_H 00038 00039 #include <stdint.h> 00040 00041 /*! 00042 * @brief convert address from host to network byte order 00043 * 00044 * @param[in] addr address in host byte order 00045 * 00046 * @return address in network byte order 00047 */ 00048 uint16_t addr_htos(uint16_t addr); 00049 00050 /*! 00051 * @brief convert address from host to network byte order 00052 * 00053 * @param[in] addr address in host byte order 00054 * 00055 * @return address in network byte order 00056 */ 00057 uint32_t addr_hton(uint32_t addr); 00058 00059 /*! 00060 * @brief convert address from network to host byte order 00061 * 00062 * @param[in] addr address in network byte order 00063 * 00064 * @return address in host byte order 00065 */ 00066 uint32_t addr_ntoh(uint32_t addr); 00067 00068 /*! 00069 * @brief convert address from network to host byte order 00070 * 00071 * @param[in] addr address in network byte order 00072 * 00073 * @return address in host byte order 00074 */ 00075 uint16_t addr_stoh(uint16_t addr); 00076 00077 /*! 00078 * @brief convert string to IP address and netmask 00079 * 00080 * @param[in] str string in CIDR or netmask notation 00081 * @param[out] ip pointer to store IP address in network byte order 00082 * @param[out] mask pointer to store netmask in network byte order 00083 * 00084 * @return 0 if no argument was converted, 1 if ip was 00085 * converted, 2 for mask and 3 for both. 00086 */ 00087 int addr_from_str(const char *str, uint32_t *ip, uint32_t *mask); 00088 00089 /*! 00090 * @brief convert IP address and netmask to string 00091 * 00092 * @param[in] ip IP adress to convert in network byte order 00093 * @param[in] mask netmask to convert in network byte order 00094 * 00095 * @return string in netmask notation (obtained with malloc(3)) 00096 * 00097 * @note The caller should free obtained memory using free(3) 00098 * 00099 * @see malloc(3) 00100 * @see free(3) 00101 */ 00102 char *addr_to_str(uint32_t ip, uint32_t mask); 00103 00104 #endif 00105 00106 /*! @} addr */