First commit
[anna.git] / include / anna / diameter / codec / basetypes / Address.hpp
1 // ANNA - Anna is Not 'N' 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 #ifndef anna_diameter_codec_basetypes_Address_hpp
38 #define anna_diameter_codec_basetypes_Address_hpp
39
40
41 // Local
42 #include <anna/diameter/codec/basetypes/OctetString.hpp>
43
44 #include <anna/config/defines.hpp>
45
46
47 namespace anna {
48
49 namespace diameter {
50
51 namespace codec {
52
53 class Avp;
54
55 namespace basetypes {
56
57 /**
58 * Diameter Address container
59 *
60 * Diameter address includes 2 bytes with ip version and 4/16 more for IPv4 and IPv6 respectively
61 *
62 * <pre>
63 *    IPv6 ADDRESS FORMAT CONSIDERATIONS
64 *
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:
68 *
69 *      A typical example of an IPv6 address is
70 *
71 *          2001:0db8:85a3:0000:0000:8a2e:0370:7334.
72 *
73 *      The hexadecimal digits are case-insensitive.
74 *
75 *      The 128-bit IPv6 address can be abbreviated with the following rules:
76 *
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
79 *
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
82 *
83 *
84 *    RULE ONE IS AUTOMATICALLY APPLIED BY sscanf and %X parsing
85 *    RULE TWO IS NOT APPLIED OVER THIS CLASS METHODS
86 *
87 *
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.
91 *
92 *      The address ::ffff:1.2.3.4 is IPv4-mapped, different than address ::1.2.3.4 which is IPV4-compatible.
93 * </pre>
94 */
95 class Address : OctetString {
96
97   iana_address_t a_address;
98   bool a_abbreviatePresentation; // specially for IPv6 address type
99
100
101   // Only for derived diameter type:
102   void updateBasic() throw(anna::RuntimeException);
103
104   void setPrintableString(const char * printableString) throw(anna::RuntimeException);
105
106
107 public:
108
109   /**
110   * Default constructor
111   */
112   Address() : a_abbreviatePresentation(true) {};
113
114   /**
115   * Presentation mode: abbreviate (i.e. two standard rules applied for IPv6) or expanded (i.e. groups of zeroes on IP address)
116   *
117   * @param abb Abbreviate mode boolean indicator
118   */
119   void setAbbreviatePresentation(bool abb) throw() { a_abbreviatePresentation = abb; };
120
121
122
123   // Class-specific ////////////////////////////////////////////////////////////////////////////////////
124   //
125   /**
126   * Gets the IANA address
127   *
128   * @return IANA address
129   */
130   const iana_address_t& getIANAAddress() const throw() { return a_address; }
131
132   /**
133   * Sets the IANA address
134   *
135   * @param address IANA address
136   */
137   void setIANAAddress(const iana_address_t& address) throw(anna::RuntimeException) { a_address = address; updateBasic(); }
138   //
139   //////////////////////////////////////////////////////////////////////////////////////////////////////
140
141
142   // gets
143
144   std::string getFormatName() const throw() { return "Address"; }
145
146
147   // helpers
148
149   /**
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
152   *
153   * @return Natural/smart string representation for IANA address (only IPv4 and IPv6 are printable-supported)
154   */
155   std::string asPrintableString() throw(anna::RuntimeException);
156
157
158   std::string asString() throw(anna::RuntimeException) { return a_address.asString(); }
159
160
161   // sets
162
163   void decode(const char* buffer, const int size) throw(anna::RuntimeException);
164
165
166   // exports /////////////////////////////
167   using AvpData::getSize;
168   using AvpData::code;
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;
176 };
177
178 }
179 }
180 }
181 }
182
183 #endif