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
37 #ifndef anna_diameter_codec_basetypes_Address_hpp
38 #define anna_diameter_codec_basetypes_Address_hpp
42 #include <anna/diameter/codec/basetypes/OctetString.hpp>
44 #include <anna/config/defines.hpp>
58 * Diameter Address container
60 * Diameter address includes 2 bytes with ip version and 4/16 more for IPv4 and IPv6 respectively
63 * IPv6 ADDRESS FORMAT CONSIDERATIONS
65 * IPv6 addresses have two logical parts: a 64-bit network prefix, and a 64-bit host address part.
66 * The host address is often automatically generated from the interface MAC address.
67 * An IPv6 address is represented by 8 groups of 16-bit hexadecimal values separated by colons (:) shown as follows:
69 * A typical example of an IPv6 address is
71 * 2001:0db8:85a3:0000:0000:8a2e:0370:7334.
73 * The hexadecimal digits are case-insensitive.
75 * The 128-bit IPv6 address can be abbreviated with the following rules:
77 * * Rule one: Leading zeroes within a 16-bit value may be omitted. For example, the address fe80:0000:0000:0000:0202:b3ff:fe1e:8329
78 * may be written as fe80:0:0:0:202:b3ff:fe1e:8329
80 * * Rule two: A single occurrence of consecutive groups of zeroes within an address may be replaced by a double colon.
81 * For example, fe80:0:0:0:202:b3ff:fe1e:8329 becomes fe80::202:b3ff:fe1e:8329
84 * RULE ONE IS AUTOMATICALLY APPLIED BY sscanf and %X parsing
85 * RULE TWO IS NOT APPLIED OVER THIS CLASS METHODS
88 * In environments of dual-stack hosts that support both IPv4 and IPv6, an IPv4 address is expressed as an IPv6 address in an IPv6-mapped address.
89 * For example, the IPv4-mapped IPv6 address ::ffff:c000:280 is usually written as ::ffff:192.0.2.128, thus expressing clearly the original IPv4
90 * address that was mapped to IPv6.
92 * The address ::ffff:1.2.3.4 is IPv4-mapped, different than address ::1.2.3.4 which is IPV4-compatible.
95 class Address : OctetString {
97 iana_address_t a_address;
98 bool a_abbreviatePresentation; // specially for IPv6 address type
101 // Only for derived diameter type:
102 void updateBasic() throw(anna::RuntimeException);
104 void setPrintableString(const char * printableString) throw(anna::RuntimeException);
110 * Default constructor
112 Address() : a_abbreviatePresentation(true) {};
115 * Presentation mode: abbreviate (i.e. two standard rules applied for IPv6) or expanded (i.e. groups of zeroes on IP address)
117 * @param abb Abbreviate mode boolean indicator
119 void setAbbreviatePresentation(bool abb) throw() { a_abbreviatePresentation = abb; };
123 // Class-specific ////////////////////////////////////////////////////////////////////////////////////
126 * Gets the IANA address
128 * @return IANA address
130 const iana_address_t& getIANAAddress() const throw() { return a_address; }
133 * Sets the IANA address
135 * @param address IANA address
137 void setIANAAddress(const iana_address_t& address) throw(anna::RuntimeException) { a_address = address; updateBasic(); }
139 //////////////////////////////////////////////////////////////////////////////////////////////////////
144 std::string getFormatName() const throw() { return "Address"; }
150 * Gets the natural/smart string representation for IANA address
151 * Launch exception when data is not printable: only IPv4 and IPv6 are printable-supported
153 * @return Natural/smart string representation for IANA address (only IPv4 and IPv6 are printable-supported)
155 std::string asPrintableString() throw(anna::RuntimeException);
158 std::string asString() throw(anna::RuntimeException) { return a_address.asString(); }
163 void decode(const char* buffer, const int size) throw(anna::RuntimeException);
166 // exports /////////////////////////////
167 using AvpData::getSize;
169 //using OctetString::asPrintableString;
170 using AvpData::asDataBlockString;
171 using OctetString::asString;
172 using AvpData::asHexString;
173 //using OctetString::decode;
174 using AvpData::fromPrintableString;
175 using AvpData::fromHexString;