Updated license
[anna.git] / example / core / ipManaging / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 // Standard
38 #include <iostream>
39
40 // STL
41 #include <string>
42
43 #include <anna/core/functions.hpp>
44 #include <anna/core/core.hpp>
45 #include <anna/core/RuntimeException.hpp>
46
47 using namespace anna;
48
49
50 void _exit(const std::string & msg) {
51    std::cout << std::endl << msg << std::endl;
52    exit(-1);
53 }
54
55
56 #define TRY_PRINT(d,a)   { try { std::cout << (d) << (a) << std::endl; } catch (anna::RuntimeException &ex) { ex.trace(); };}
57
58
59
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: " ;
67    std::cin >> ip1;
68    std::cout << "Input ip: " ;
69    std::cin >> ip2;
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: ";
75
76    if (anna::functions::isIPv4(ip1)) std::cout << "Pure IPv4" << std::endl;
77
78    if (anna::functions::isIPv4(ip1, anna::functions::IPv4Type::Compatible)) std::cout << "IPv4 - compatible" << std::endl;
79
80    if (anna::functions::isIPv4(ip1, anna::functions::IPv4Type::Mapped)) std::cout << "IPv4 - mapped" << std::endl;
81
82    if (anna::functions::isIPv6(ip1)) std::cout << "IPv6" << std::endl;
83
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);
91    exit(-1);
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: ";
99
100    if (anna::functions::isIPv4(ip2)) std::cout << "Pure IPv4" << std::endl;
101
102    if (anna::functions::isIPv4(ip2, anna::functions::IPv4Type::Compatible)) std::cout << "IPv4 - compatible" << std::endl;
103
104    if (anna::functions::isIPv4(ip2, anna::functions::IPv4Type::Mapped)) std::cout << "IPv4 - mapped" << std::endl;
105
106    if (anna::functions::isIPv6(ip2)) std::cout << "IPv6" << std::endl;
107
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;
124    preffixedIp2 += "/";
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;
130 }
131