From a15d867fd8c999c33891089019551fea00617969 Mon Sep 17 00:00:00 2001 From: Arunkumar Mourougappane Date: Wed, 13 Nov 2019 23:32:37 -0600 Subject: [PATCH] Added doxygen code documentation, Updated Markdown README with build information, API data and its use. gtest has been updated with more test cases for IPv4 and IPv6. --- Makefile | 4 +- README.md | 47 ++++- images/ip_address.png | Bin 0 -> 23172 bytes include/ip_address.h | 167 +++++++++++++++++- .../gtest_ip_address.cpp | 40 ++++- .../gtest_mini_ip_utilities.cpp | 8 +- src/app/include_app/gtest_ip_address.h | 14 +- src/lib/ip_address/ip_address.cpp | 37 +++- 8 files changed, 296 insertions(+), 21 deletions(-) create mode 100644 images/ip_address.png diff --git a/Makefile b/Makefile index 682ec46..c0984c6 100644 --- a/Makefile +++ b/Makefile @@ -28,8 +28,8 @@ APPS = \ build/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities GTEST_MINI_IP_UTILITIES_OBJS = \ - src/app/gtest_mini_ip_utilities/gtest_ip_address.o \ - src/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities.o + build/app/gtest_mini_ip_utilities/gtest_ip_address.o \ + build/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities.o all: gtest tree libs apps diff --git a/README.md b/README.md index e607f1f..8f736d8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,46 @@ -# miniIPutils +# miniIPutils -Ip Utility tools wrapper read, process and manipulate Ethernet interfaces. +[![Build Status](https://travis-ci.com/arunkumar-mourougappane/miniIPutils.svg?branch=master)](https://travis-ci.com/arunkumar-mourougappane/miniIPutils) + +An advanced self managing IP Address Utility tool capable of managing both managing IPv4 and IPv6 Address. + +[CIPAddress](https://github.com/arunkumar-mourougappane/miniIPutils/blob/master/src/lib/ip_address/ip_address.cpp "Source Code for CIPAddress API") + +CIPAddress wrapper library can process any IP address format from one among the following: + +- std::string +- const char* +- struct in_addr_t +- struct in_addr +- struct in6_addr +- struct sockaddr_storage + +**APIs:** + +- GetIPFamily() returns the IP Address family. +- IsValidIP() returns if IP is valid or not. +- GetString() returns a string for the IP Address. +- GetIPAddressBytes() returns a sockaddr_storage structure. +- GetIPAddress(struct in_addr & byteAddress ) returns 0 on success with valid IPv4 address populated to byte address. +- GetIPAddress(struct in6_addr & byteAddress ) returns 0 on success with valid IPv6 address populated to byte address. +- Compare(const CIPAddress& IpAddress) compare a CIPAddress instance with current insance. +- to_string() provides a string representation of a class. + +**Example of Use:** + +Here are some examples of the use for CIPAddress class implementation. + +```cpp +#include + +CIPAddress obj("127.0.01"); +// Checking for validty +if (obj) // Also obj.IsValidIP() can be use to check validity. +{ + std::cout << "Valid IP Address"; +} +// to print ip Address string or send it to stream +stream_obj << obj; +// to get string value; +std::cout << obj.GetString(); +``` diff --git a/images/ip_address.png b/images/ip_address.png new file mode 100644 index 0000000000000000000000000000000000000000..197b03078d7329a085ef32ac95d4656427309734 GIT binary patch literal 23172 zcmc%xi9gic`v;D{W(>x@%amnoBTJUCWQ#$vQ==?JQDdZtkX?;z6=m$kT6ZJ2o3chC z*$pO%61P&yo>WMb@;$HVzCXYF{rmm_UynzR&g*(z=enNPb-|Is;vm5P&}@JrK7277{D%l3TA4%l1|=831IpLT!3=_G(s;JK*&&DxI%t2` z643v@`_qOXG6c~ehzUX45O~tohP26$HVx8dLfYH#OR_dZCPQQzL}o(dZJ3v)4bjLD zjRw(}5N#W-%G8FKWQa+Fm`sSd4J1L^+R!!`+NMF;60G;JDFo3;&u0=!JJHj}2!WNI_F0Y*?& zdz-AiP1D|HYHtIr05uQ>Km(Kj7I+5~1KB_{kOeAl!v?|6;EzToGs)y_*gl{ZJke-m z8k0=hhRp>iz)L2L%w&?8+rWAN3h{Hmo|sG;a~t>-mDX-K7ie!L`P@6<`2Ju3!I0ODqfwk=*(Dscl%8L(U@A>6r=zB zL_P?cECLEwkhOaV1o3ym{}8=_f07``W!z@J+2M%eA73w+zhczt4kZncaHkIZXxWZG zKJjQm{U-NcbZS<{==ZODvD|3O*)W@a=A~bpAHseHhh5lSTl=}bHjs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>n&+B`aq0Eo~B|6vx#hC+faH(Dg8`r+)w9YdMNN;(TZ$ol`YS z;0MpoNM+rA@C1bduO=&dY<@i_)4(1<<`=JaQ6~hrl^Xcr{J5xi%u^;KVvkfxAG-BQpG+F?wO$9SM%_2)%>%jmm1u|@C<&jw;r%u7W%^+3o!rE8d*w!(l!0z zVp3F&m;Db*1tj`VvL`WBW5rnVg7sv$m~Zr8T}u0m1#lPwAAO$xsnvV# z2-_LK&#TV=@G08{9d^YN=K6z!pH0n*`M4Cl(voejbQ!Q0UK$uG&$tlYtdmT{`X}Z0 zZZA{y@=FP5_~)o#So1xl8}WpIn@o0iv}m57j(Nz@wDK5#;m<@Y@tt+jUp>Kd8bg72 z!m|bz=XP|_d?bm0-j2&{PaGr9mr9f2qHavEH?&{o2z^vJ5BfQkQpYsS+X7OFg=gMTwXBbJ+`Cj|ANm+{(o)G`9Y1~R68#v-a`hCM1(9n1tzcOC+e16V! Y+{LW<#d9NDwHB_ohwtWV49>}a13_WcRsaA1 literal 0 HcmV?d00001 diff --git a/include/ip_address.h b/include/ip_address.h index 9038023..17738a4 100644 --- a/include/ip_address.h +++ b/include/ip_address.h @@ -1,3 +1,15 @@ +/** + * @file ip_address.h + * @author Arunkumar (amouroug@buffalo.edu) + * @brief A C++ IP Address Wrapper to handle both IPv4 as + * well as IPv6 Addresses without the need for + * separate APIs to handle them. + * @version 0.1 + * @date 2019-11-13 + * + * @copyright Copyright (c) 2019 + * + */ #include #include #include @@ -7,6 +19,10 @@ #define IP_ADDRESS_H namespace mini_ip_utilities { + /** + * @brief CIPAddress wrapper to handle IPv4 and IPv6. + * + */ class CIPAddress { private: @@ -15,22 +31,171 @@ namespace mini_ip_utilities bool mValidIP; struct sockaddr_storage mIPAddressBytes; public: + /** + * @brief Construct a new CIPAddress object + * + * @param ipAddress - a std::string of IPv4 or + * IPv6 Address. All instance variables are + * populated for a valid IP. + */ CIPAddress(std::string ipAddress); + + /** + * @brief Construct a new CIPAddress object + * + * @param byteAddress take a reference to byte + * address value and if valid populates + * all instance variables. + */ CIPAddress(in_addr_t& byteAddress); + + /** + * @brief Construct a new CIPAddress object + * + * @param byteAddressData a reference to the + * IPv6 in6_addr and if valid populates + * all the instance variables of the class + * with appropriate information. + */ CIPAddress(const struct in6_addr & byteAddressData); + + /** + * @brief Construct a new CIPAddress object + * + * @param byteAddressData a reference to the IPv4 + * byte address is taken and if found valid is + * used to populate all instance variable + * information. + */ CIPAddress(const struct in_addr & byteAddressData); + + /** + * @brief Construct a new CIPAddress object + * + * @param byteAddressData a refernce to sockaddr_storage + * structure, the ip address is detected from the + * structure and is then used to populate all + * instance variable information. + */ CIPAddress(const struct sockaddr_storage& byteAddressData); + + /** + * @brief Construct a new CIPAddress object + * + */ CIPAddress(); - + + /** + * @brief GetIPFamily() gives the IP Address + * family of the IP Address saved in the + * instance. + * + * @return sa_family_t of values AF_INET, + * AF_INET6 or AF_UNSPEC + */ sa_family_t GetIPFamily() const; + + /** + * @brief IsValidIP() checks if IP from + * instance is valid or not. + * + * @return true if IP is valid + * @return false if IP is invalid + */ bool IsValidIP() const; + + /** + * @brief Get the String the IP Address instance. + * + * @return const std::string + */ const std::string GetString() const; + + /** + * @brief Get Byte Address information in the form of + * a sockaddr_storage structure. + * + * @return const struct sockaddr_storage + */ const struct sockaddr_storage GetIPAddressBytes() const; + + /** + * @brief Gets the byte address of IPv4 address from the + * instance and saves to argument reference. + * + * @param byteAddress a byte address for IPv4 is generated + * and populated to the reference provided as argument. + * + * @return int_least32_t 0 on success with the correct byte + * address and -1 for failure with IN_ADDR_ANY populated + * for reference argument. + */ int_least32_t GetIPAddress(struct in_addr & byteAddress ) const; + + /** + * @brief Gets the byte address of IPv6 address from the + * instance and saves to argument reference. + * + * @param byteAddress a byte address for IPv6 is generated + * and populated to the reference provided as argument. + * + * @return int_least32_t 0 on success with the correct byte + * address and -1 for failure with in6_addr_any populated + * for reference argument. + */ int_least32_t GetIPAddress(struct in6_addr & byteAddress ) const; + + /** + * @brief Compares current CIPAddess instance with another. + * + * @param IpAddress CIPAddress instance that needs to be comapred + * with. + * @return int_least32_t returns 0 on success -1 if they are not + * the same. + */ int_least32_t Compare(const CIPAddress& IpAddress) const; + + /** + * @brief operator overloading for equality. + * + * @param IpAddress to be compared with. + * + * @return true if the CIPAddress instance compared + * is the same. + * @return false if CIPAddress instances are + * different. + */ bool operator== (const CIPAddress &IpAddress); + + /** + * @brief Operator overloading for inequality + * + * @param IpAddress to be compared with. + * + * @return true f the CIPAddress instance compared + * is not the same. + * @return false if the CIPAddress instance compared + * is the same. + */ bool operator!= (const CIPAddress &IpAddress); + + /** + * @brief gives a string representation + * + * @return std::string a string representation of + * the IP Address. + */ + std::string to_string() const; + /** + * @brief Provides a boolean state of the instance + * + * @return true if IP Address is valid + * @return false if IP Address is invalid. + */ + explicit operator bool() const + { + return IsValidIP(); + } }; } #endif \ No newline at end of file diff --git a/src/app/gtest_mini_ip_utilities/gtest_ip_address.cpp b/src/app/gtest_mini_ip_utilities/gtest_ip_address.cpp index 65b2af7..6522de4 100644 --- a/src/app/gtest_mini_ip_utilities/gtest_ip_address.cpp +++ b/src/app/gtest_mini_ip_utilities/gtest_ip_address.cpp @@ -1,10 +1,38 @@ #include #include -TEST_F(TestCIPAddress, CheckifValidIPIsAccepted) +TEST_F(TestCIPAddress, CheckifValidIPv4IsAccepted) { - mini_ip_utilities::CIPAddress ipAddressObj(ipAddressString); - EXPECT_EQ(true, ipAddressObj.IsValidIP()) << "Failed to accept valid IP Addres argument, IsValidIP() returned false."; - EXPECT_EQ(std::string("127.0.0.1"), ipAddressObj.GetString()) << "Failed to accept valid IP Addres argument, ipAddr.GetString() returned empty string."; - EXPECT_EQ(AF_INET, ipAddressObj.GetIPFamily()) << "Failed to accept valid IP Addres argument, ipAddr.GetIPFamily() returned AF_UNSPEC"; -} \ No newline at end of file + mini_ip_utilities::CIPAddress ipAddressObj(ipv4AddressString); + ASSERT_EQ(true, ipAddressObj.IsValidIP()) << "Failed to accept valid IP Address argument, IsValidIP() returned false."; + EXPECT_EQ(std::string("127.0.0.1"), ipAddressObj.GetString()) << "Failed to accept valid IPv4 Address argument, ipAddr.GetString() returned empty string."; + EXPECT_EQ(AF_INET, ipAddressObj.GetIPFamily()) << "Failed to accept valid IPv4 Address argument, ipAddr.GetIPFamily() returned AF_UNSPEC"; + EXPECT_TRUE(ipAddressObj) << "Failed to overload bool operator valid IPv4 Address argument,it returned false."; +} + +TEST_F(TestCIPAddress, CheckifInvalidIPv4IsAccepted) +{ + mini_ip_utilities::CIPAddress ipAddressObj(invalidIPv4AddressString); + ASSERT_EQ(false, ipAddressObj.IsValidIP()) << "Failed to accept invalid IPv4 Address argument, IsValidIP() returned true."; + EXPECT_EQ(std::string(""), ipAddressObj.GetString()) << "Failed to accept invalid IPv4 Addres argument, ipAddr.GetString() returned a string."; + EXPECT_EQ(AF_UNSPEC, ipAddressObj.GetIPFamily()) << "Failed to accept valid IPv4 Address argument, ipAddr.GetIPFamily() returned AF_INET"; + EXPECT_FALSE(ipAddressObj) << "Failed to overload bool operator invalid IPv4 Address argument, it returned false."; +} + +TEST_F(TestCIPAddress, CheckifValidIPv6IsAccepted) +{ + mini_ip_utilities::CIPAddress ipAddressObj(ipv6AddressString); + ASSERT_EQ(true, ipAddressObj.IsValidIP()) << "Failed to accept valid IPv6 Address argument, IsValidIP() returned false."; + EXPECT_EQ(ipv6AddressString, ipAddressObj.GetString()) << "Failed to accept valid IPv6 Address argument, ipAddr.GetString() returned empty string."; + EXPECT_EQ(AF_INET6, ipAddressObj.GetIPFamily()) << "Failed to accept valid IPv6 Address argument, ipAddr.GetIPFamily() returned AF_UNSPEC or AF_INET"; + EXPECT_TRUE(ipAddressObj) << "Failed to overload bool operator valid IPv6 Address argument,it returned false."; +} + +TEST_F(TestCIPAddress, CheckifInvalidIPv6IsAccepted) +{ + mini_ip_utilities::CIPAddress ipAddressObj(invalidIPv6AddressString); + ASSERT_EQ(false, ipAddressObj.IsValidIP()) << "Failed to accept invalid IPv6 Address argument, IsValidIP() returned true."; + EXPECT_EQ(std::string(""), ipAddressObj.GetString()) << "Failed to accept invalid IPv6 Address argument, ipAddr.GetString() returned a string."; + EXPECT_EQ(AF_UNSPEC, ipAddressObj.GetIPFamily()) << "Failed to accept valid IPv6 Address argument, ipAddr.GetIPFamily() returned AF_INET6 or AF_INET"; + EXPECT_FALSE(ipAddressObj) << "Failed to overload bool operator invalid IPv6 Address argument, it returned false."; +} diff --git a/src/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities.cpp b/src/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities.cpp index 33dff98..88be788 100644 --- a/src/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities.cpp +++ b/src/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities.cpp @@ -1,6 +1,12 @@ #include - +/** + * @brief Main function for getests + * + * @param argc + * @param argv + * @return int returns 0 on success. + */ int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/src/app/include_app/gtest_ip_address.h b/src/app/include_app/gtest_ip_address.h index fea0614..ce72baa 100644 --- a/src/app/include_app/gtest_ip_address.h +++ b/src/app/include_app/gtest_ip_address.h @@ -5,15 +5,21 @@ #include #include - +/** + * @brief TestCIPAddress to test for instance variables. + * + */ class TestCIPAddress : public :: testing :: Test { public: - std::string ipAddressString, invalidIPAddressString; + std::string ipv4AddressString, invalidIPv4AddressString; + std::string ipv6AddressString, invalidIPv6AddressString; virtual void SetUp() { - ipAddressString = "127.0.0.1"; - invalidIPAddressString = "123456A"; + ipv4AddressString = "127.0.0.1"; + invalidIPv4AddressString = "123456A"; + ipv6AddressString="a060:9006:b0a2:5200:2a1c:d979:4276:836f"; + invalidIPv6AddressString="a060:9006:b0a2:ghdiss:2a1c:d#979:42276:836f"; } virtual void TearDown() diff --git a/src/lib/ip_address/ip_address.cpp b/src/lib/ip_address/ip_address.cpp index ec98af9..91ac331 100644 --- a/src/lib/ip_address/ip_address.cpp +++ b/src/lib/ip_address/ip_address.cpp @@ -11,20 +11,28 @@ namespace mini_ip_utilities { CIPAddress::CIPAddress() { + // Asssign default values to all. mIPFamily = AF_UNSPEC; mValidIP=false; - mIPAddressBytes = {}; + mIPAddressBytes = {0}; mIpAddress.clear(); } + CIPAddress::CIPAddress(std::string ipAddress) { mIPFamily=AF_UNSPEC; mValidIP=false; mIPAddressBytes={0}; + + // Needed for parsed byte address. struct sockaddr_in v4Address = {0}; struct sockaddr_in6 v6Address = {0}; + + // If the IP Address is an empty string + // we reject it. However, we allow 0.0.0.0. if(ipAddress.length() > 0) { + // verify if conversion to IPv4 worked, if yes then save the address information if (inet_pton(AF_INET, ipAddress.c_str(), &v4Address.sin_addr) == 1) { char ipv4Buffer[INET_ADDRSTRLEN]; @@ -34,6 +42,7 @@ namespace mini_ip_utilities std::memcpy(&mIPAddressBytes, &v4Address, sizeof(v4Address)); mIpAddress = std::string(inet_ntop(AF_INET, &(((struct sockaddr_in *)&mIPAddressBytes)->sin_addr), ipv4Buffer, INET_ADDRSTRLEN)); } + // verify if address is IPv6, if yes then save all to instance. else if (inet_pton(AF_INET6, ipAddress.c_str(), &v6Address.sin6_addr) == 1) { char ipv6Buffer[INET6_ADDRSTRLEN]; @@ -43,11 +52,12 @@ namespace mini_ip_utilities std::memcpy(&mIPAddressBytes, &v6Address, sizeof(v6Address)); mIpAddress = std::string(inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&mIPAddressBytes)->sin6_addr, ipv6Buffer, INET6_ADDRSTRLEN)); } + // if IP was invalid, then reset all variables. else { mIpAddress.clear(); mIPFamily = AF_UNSPEC; - mIPAddressBytes = {}; + mIPAddressBytes = {0}; } } } @@ -61,13 +71,14 @@ namespace mini_ip_utilities char ipv4Buffer[INET_ADDRSTRLEN]; mIpAddress.clear(); mIPAddressBytes = {0}; - + // if given IPv4 byte address is valid, save to the instance! if (inet_ntop(AF_INET, &v4Address.sin_addr, ipv4Buffer, INET_ADDRSTRLEN)) { mIPFamily = AF_INET; mValidIP = true; v4Address.sin_family = AF_INET; std::memcpy(&mIPAddressBytes, &v4Address, sizeof(v4Address)); + // The IP Address string is canonicalized, save for use to represent. mIpAddress = std::string(ipv4Buffer); } } @@ -81,13 +92,15 @@ namespace mini_ip_utilities mIpAddress.clear(); char ipv4Buffer[INET_ADDRSTRLEN]; mIPAddressBytes = {0}; - + // if given IPv4 byte address is valid, save to the instance! if (inet_ntop(AF_INET, &byteAddressData, ipv4Buffer, INET_ADDRSTRLEN)) { mIPFamily = AF_INET; mValidIP = true; v4Address.sin_family = AF_INET; std::memcpy(&mIPAddressBytes, &v4Address, sizeof(v4Address)); + + // The IP Address string is canonicalized, save for use to represent. mIpAddress = std::string(ipv4Buffer); } } @@ -102,6 +115,7 @@ namespace mini_ip_utilities mIpAddress.clear(); mIPAddressBytes = {0}; + // if given IPv6 byte address is valid, save to the instance! if (inet_ntop(AF_INET6, &v6Address.sin6_addr, ipv6Buffer, INET6_ADDRSTRLEN)) { mIPFamily = AF_INET6; @@ -119,7 +133,7 @@ namespace mini_ip_utilities mValidIP = false; mIPAddressBytes = byteAddressData; mIpAddress.clear(); - + // Verify for IPv4 Compatibility, and if valid save instance. if (byteAddressData.ss_family == AF_INET) { char ipv4Buffer[INET_ADDRSTRLEN]; @@ -130,6 +144,7 @@ namespace mini_ip_utilities mIpAddress = std::string(ipv4Buffer); } } + // Verify for IPv6 Compatibility, and if valid save instance. else if (byteAddressData.ss_family == AF_INET6) { char ipv6Buffer[INET6_ADDRSTRLEN]; @@ -140,6 +155,7 @@ namespace mini_ip_utilities mIpAddress = std::string(ipv6Buffer); } } + // No compatibility for IPv4v6 was found, so reset to defaults. else { mIPFamily = AF_UNSPEC; @@ -170,6 +186,7 @@ namespace mini_ip_utilities } int_least32_t CIPAddress::GetIPAddress(struct in_addr& byteAddress ) const { + // if the saved instance is IPv4, we save byte address to argument. if(GetIPFamily() == AF_INET) { byteAddress = ((struct sockaddr_in*)&mIPAddressBytes)->sin_addr; @@ -183,6 +200,7 @@ namespace mini_ip_utilities } int_least32_t CIPAddress::GetIPAddress(struct in6_addr& byteAddress ) const { + // if the saved instance is IPv6, we save byte address to argument. if(GetIPFamily() == AF_INET6) { byteAddress = ((struct sockaddr_in6*)&mIPAddressBytes)->sin6_addr; @@ -198,16 +216,20 @@ namespace mini_ip_utilities int_least32_t CIPAddress::Compare(const CIPAddress& IpAddress) const { + // if one of the IPs are invalid, we don't need to comapare. if((!IpAddress.IsValidIP()) || (!IsValidIP())) { return -1; } else { + // If the IP family doesn't match, no need to compare. if (IpAddress.GetIPFamily() != mIPFamily) { return -2; } + // The IPAddress strings are canonicalized, so they can be compared + // to be identical. else if(IpAddress.GetString().compare(GetString()) == 0) { return 0; @@ -244,4 +266,9 @@ namespace mini_ip_utilities return !(*this == IpAddress); } + std::string CIPAddress::to_string() const + { + return mIpAddress; + } + } \ No newline at end of file