Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / example / core / ipManaging / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 // Standard
10 #include <iostream>
11
12 // STL
13 #include <string>
14
15 #include <anna/core/functions.hpp>
16 #include <anna/core/core.hpp>
17 #include <anna/core/RuntimeException.hpp>
18
19 using namespace anna;
20
21
22 void _exit(const std::string & msg) {
23    std::cout << std::endl << msg << std::endl;
24    exit(-1);
25 }
26
27
28 #define TRY_PRINT(d,a)   { try { std::cout << (d) << (a) << std::endl; } catch (anna::RuntimeException &ex) { ex.trace(); };}
29
30
31
32 int main(int argc, char** argv) {
33    Logger::setLevel(Logger::Information);
34 //   Logger::setLevel(Logger::Debug);
35    Logger::initialize("IP Managing", new TraceWriter("file.trace", 2048000));
36    std::string ip1, ip2, preffixLength;
37    std::cout << "Let's read two IP addresses:\n\n";
38    std::cout << "Input ip: " ;
39    std::cin >> ip1;
40    std::cout << "Input ip: " ;
41    std::cin >> ip2;
42    std::cout << "Input ipv6 preffix length for last input: " ;
43    std::cin >> preffixLength;
44    std::cout << std::endl;
45    std::cout << anna::functions::highlightJustify("Input 1");
46    std::cout << "Address family: ";
47
48    if (anna::functions::isIPv4(ip1)) std::cout << "Pure IPv4" << std::endl;
49
50    if (anna::functions::isIPv4(ip1, anna::functions::IPv4Type::Compatible)) std::cout << "IPv4 - compatible" << std::endl;
51
52    if (anna::functions::isIPv4(ip1, anna::functions::IPv4Type::Mapped)) std::cout << "IPv4 - mapped" << std::endl;
53
54    if (anna::functions::isIPv6(ip1)) std::cout << "IPv6" << std::endl;
55
56    TRY_PRINT("IPv4 to IPv6 conversion: ", anna::functions::IPv4To6(ip1));
57    TRY_PRINT("Normalization: ", anna::functions::normalizeIP(ip1));
58    anna::DataBlock db1 = anna::functions::ipAsRaw(ip1);
59    db1 = anna::functions::rawIpPresentationAsRaw("AABBCCDD");
60    TRY_PRINT("Datablock encoding: ", anna::functions::asString(db1));
61    std::string hrRawPresentation1 = anna::functions::rawIpAsRawIpPresentation(db1);
62    TRY_PRINT("Datablock as Raw Presentation (human-readable): ", hrRawPresentation1);
63    exit(-1);
64    TRY_PRINT("Datablock encoding from former: ", anna::functions::asString(db1));
65    TRY_PRINT("Former decoded and normalized: ", anna::functions::rawIpAsString(db1, true));
66    TRY_PRINT("Former decoded not normalized: ", anna::functions::rawIpAsString(db1));
67    TRY_PRINT("Abbreviated: ", anna::functions::abbreviateIP(ip1));
68    std::cout << std::endl;
69    std::cout << anna::functions::highlightJustify("Input 2");
70    std::cout << "Address family: ";
71
72    if (anna::functions::isIPv4(ip2)) std::cout << "Pure IPv4" << std::endl;
73
74    if (anna::functions::isIPv4(ip2, anna::functions::IPv4Type::Compatible)) std::cout << "IPv4 - compatible" << std::endl;
75
76    if (anna::functions::isIPv4(ip2, anna::functions::IPv4Type::Mapped)) std::cout << "IPv4 - mapped" << std::endl;
77
78    if (anna::functions::isIPv6(ip2)) std::cout << "IPv6" << std::endl;
79
80    TRY_PRINT("IPv4 to IPv6 conversion: ", anna::functions::IPv4To6(ip2));
81    TRY_PRINT("Normalization: ", anna::functions::normalizeIP(ip2));
82    anna::DataBlock db2 = anna::functions::ipAsRaw(ip2);
83    TRY_PRINT("Datablock encoding: ", anna::functions::asString(db2));
84    std::string hrRawPresentation2 = anna::functions::rawIpAsRawIpPresentation(db2);
85    db2 = anna::functions::rawIpPresentationAsRaw(hrRawPresentation2);
86    TRY_PRINT("Datablock as Raw Presentation (human-readable): ", hrRawPresentation2);
87    TRY_PRINT("Datablock encoding from former: ", anna::functions::asString(db2));
88    TRY_PRINT("Former decoded and normalized: ", anna::functions::rawIpAsString(db2, true));
89    TRY_PRINT("Former decoded not normalized: ", anna::functions::rawIpAsString(db2));
90    TRY_PRINT("Abbreviated: ", anna::functions::abbreviateIP(ip2));
91    std::cout << std::endl;
92    std::cout << anna::functions::highlightJustify("Input 1 and 2 ARE EQUIVALENT ?");
93    TRY_PRINT("", anna::functions::sameIP(ip1, ip2) ? "YES" : "NO");
94    std::cout << anna::functions::highlightJustify("Input 1 and preffixed-2 ARE EQUIVALENT ?");
95    std::string preffixedIp2 = ip2;
96    preffixedIp2 += "/";
97    preffixedIp2 += preffixLength;
98    TRY_PRINT("", anna::functions::matchIPv6(ip1, preffixedIp2) ? "YES" : "NO");
99    std::cout << std::endl;
100    std::cout << std::endl;
101    std::cout << std::endl;
102 }
103