1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
43 #include <anna/core/functions.hpp>
44 #include <anna/core/core.hpp>
45 #include <anna/core/RuntimeException.hpp>
50 void _exit(const std::string & msg) {
51 std::cout << std::endl << msg << std::endl;
56 #define TRY_PRINT(d,a) { try { std::cout << (d) << (a) << std::endl; } catch (anna::RuntimeException &ex) { ex.trace(); };}
60 int main(int argc, char** argv) {
61 Logger::setLevel(Logger::Information);
62 // Logger::setLevel(Logger::Debug);
63 Logger::initialize("IP Managing", new TraceWriter("file.trace", 2048000));
64 std::string ip1, ip2, preffixLength;
65 std::cout << "Let's read two IP addresses:\n\n";
66 std::cout << "Input ip: " ;
68 std::cout << "Input ip: " ;
70 std::cout << "Input ipv6 preffix length for last input: " ;
71 std::cin >> preffixLength;
72 std::cout << std::endl;
73 std::cout << anna::functions::highlightJustify("Input 1");
74 std::cout << "Address family: ";
76 if (anna::functions::isIPv4(ip1)) std::cout << "Pure IPv4" << std::endl;
78 if (anna::functions::isIPv4(ip1, anna::functions::IPv4Type::Compatible)) std::cout << "IPv4 - compatible" << std::endl;
80 if (anna::functions::isIPv4(ip1, anna::functions::IPv4Type::Mapped)) std::cout << "IPv4 - mapped" << std::endl;
82 if (anna::functions::isIPv6(ip1)) std::cout << "IPv6" << std::endl;
84 TRY_PRINT("IPv4 to IPv6 conversion: ", anna::functions::IPv4To6(ip1));
85 TRY_PRINT("Normalization: ", anna::functions::normalizeIP(ip1));
86 anna::DataBlock db1 = anna::functions::ipAsRaw(ip1);
87 db1 = anna::functions::rawIpPresentationAsRaw("AABBCCDD");
88 TRY_PRINT("Datablock encoding: ", anna::functions::asString(db1));
89 std::string hrRawPresentation1 = anna::functions::rawIpAsRawIpPresentation(db1);
90 TRY_PRINT("Datablock as Raw Presentation (human-readable): ", hrRawPresentation1);
92 TRY_PRINT("Datablock encoding from former: ", anna::functions::asString(db1));
93 TRY_PRINT("Former decoded and normalized: ", anna::functions::rawIpAsString(db1, true));
94 TRY_PRINT("Former decoded not normalized: ", anna::functions::rawIpAsString(db1));
95 TRY_PRINT("Abbreviated: ", anna::functions::abbreviateIP(ip1));
96 std::cout << std::endl;
97 std::cout << anna::functions::highlightJustify("Input 2");
98 std::cout << "Address family: ";
100 if (anna::functions::isIPv4(ip2)) std::cout << "Pure IPv4" << std::endl;
102 if (anna::functions::isIPv4(ip2, anna::functions::IPv4Type::Compatible)) std::cout << "IPv4 - compatible" << std::endl;
104 if (anna::functions::isIPv4(ip2, anna::functions::IPv4Type::Mapped)) std::cout << "IPv4 - mapped" << std::endl;
106 if (anna::functions::isIPv6(ip2)) std::cout << "IPv6" << std::endl;
108 TRY_PRINT("IPv4 to IPv6 conversion: ", anna::functions::IPv4To6(ip2));
109 TRY_PRINT("Normalization: ", anna::functions::normalizeIP(ip2));
110 anna::DataBlock db2 = anna::functions::ipAsRaw(ip2);
111 TRY_PRINT("Datablock encoding: ", anna::functions::asString(db2));
112 std::string hrRawPresentation2 = anna::functions::rawIpAsRawIpPresentation(db2);
113 db2 = anna::functions::rawIpPresentationAsRaw(hrRawPresentation2);
114 TRY_PRINT("Datablock as Raw Presentation (human-readable): ", hrRawPresentation2);
115 TRY_PRINT("Datablock encoding from former: ", anna::functions::asString(db2));
116 TRY_PRINT("Former decoded and normalized: ", anna::functions::rawIpAsString(db2, true));
117 TRY_PRINT("Former decoded not normalized: ", anna::functions::rawIpAsString(db2));
118 TRY_PRINT("Abbreviated: ", anna::functions::abbreviateIP(ip2));
119 std::cout << std::endl;
120 std::cout << anna::functions::highlightJustify("Input 1 and 2 ARE EQUIVALENT ?");
121 TRY_PRINT("", anna::functions::sameIP(ip1, ip2) ? "YES" : "NO");
122 std::cout << anna::functions::highlightJustify("Input 1 and preffixed-2 ARE EQUIVALENT ?");
123 std::string preffixedIp2 = ip2;
125 preffixedIp2 += preffixLength;
126 TRY_PRINT("", anna::functions::matchIPv6(ip1, preffixedIp2) ? "YES" : "NO");
127 std::cout << std::endl;
128 std::cout << std::endl;
129 std::cout << std::endl;