1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
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 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.
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_core_util_defines_hpp
38 #define anna_core_util_defines_hpp
50 #define DECODE2BYTES_INDX_VALUETYPE(buffer,indx,value_type) ((((value_type)buffer[indx] << 8) & 0xFF00) + ((value_type)buffer[indx+1] & 0x00FF))
51 #define DECODE3BYTES_INDX_VALUETYPE(buffer,indx,value_type) ((((value_type)buffer[indx] << 16) & 0xFF0000) + (((value_type)buffer[indx+1] << 8) & 0x00FF00) + ((value_type)buffer[indx+2] & 0x0000FF))
52 #define DECODE4BYTES_INDX_VALUETYPE(buffer,indx,value_type) ((((value_type)buffer[indx] << 24) & 0xFF000000) + (((value_type)buffer[indx+1] << 16) & 0x00FF0000) + (((value_type)buffer[indx+2] << 8) & 0x0000FF00) + ((value_type)buffer[indx+3] & 0x000000FF))
54 //#define DECODE2BYTES_VALUETYPE(buffer,value_type) DECODE2BYTES_INDX_VALUETYPE(buffer,0,value_type)
55 //#define DECODE3BYTES_VALUETYPE(buffer,value_type) DECODE3BYTES_INDX_VALUETYPE(buffer,0,value_type)
56 //#define DECODE4BYTES_VALUETYPE(buffer,value_type) DECODE4BYTES_INDX_VALUETYPE(buffer,0,value_type)
61 // type bits (bytes) % Diameter Data typedef
62 // -----------------------------------------------------------------------
63 // unsigned short int 16 (2) hu U16
64 // short int 16 (2) hd S16
65 // unsigned int 32 (4) u Unsigned32 (*) U32
66 // int 32 (4) d Integer32 (*) S32
68 // Integer. Its length traditionally depends on the length of the system's Word type, thus in MSDOS
69 // it is 16 bits long, whereas in 32 bit systems (like Windows 9x/2000/NT and systems that work under
70 // protected mode in x86 systems) it is 32 bits long (4 bytes)
72 // Como está previsto que en algunas máquinas la palabra sea de 16 bits, los enteros serían
73 // de 16 y por ello C contempla: int (serían 16 bits), long int (32), long long int (64). Sin embargo
74 // en la práctica, 'int = long int = 32' y 'long long int = 64'.
76 // (*) Por un mal hábito, representamos enteros de 32 bits con el tipo 'int'/'unsigned int' de
77 // toda la vida, sin darnos cuenta de que en alguna máquina antigua, no tendría 32 bits.
78 // Corregir lo anterior sería tan sencillo como poner S32 = long int (no int),
79 // y U32 = unsigned long int (no unsigned int). Pero no lo vamos a hacer.
81 // El tipo 'long' tiene un tamaño que corresponde con el ancho de palabra del S.O.
82 // En Solaris (palabra de 64) tenemos long = 64 = long long
83 // En linux (palabra de 32) tenemos long = 32, long long = 64
84 // En linux64 (palabra de 64) tenemos long = 64 = long long
86 // unsigned long int 32/64 (4/8) lu
87 // long int 32/64 (4/8) ld
89 // unsigned long long int 64 (8) llu Unsigned64 U64
90 // long long int 64 (8) lld Integer64 S64
94 // long double 80 (10) Lf
97 /** Alias for unsigned char: unsigned integer with 8 bits */
98 typedef unsigned char U8;
100 /** Alias for char: signed integer with 8 bits */
103 /** Alias for unsigned short int: unsigned integer with 16 bits */
104 typedef unsigned short int U16;
106 /** Alias for short int: signed integer with 16 bits */
107 typedef short int S16;
109 /** Alias for unsigned int: unsigned integer with 32 bits */
110 typedef unsigned int U32;
112 /** Alias for int: signed integer with 32 bits */
115 /** Alias for unsigned long long: unsigned integer with 64 bits */
116 typedef unsigned long long int U64;
118 /** Alias for long long: signed integer with 64 bits */
119 typedef long long int S64;
121 /** Alias for float: floating number with 32 bits (1-8-23) */
124 /** Alias for double: floating number with 64 bits (1-11-52) */
127 /** Alias for unsigned int: unsigned integer with 32 bits used to contain 24 bits */
134 * Typedef for socket literal representation: <ip|hostname>:<port>
136 typedef std::pair <std::string, int> socket_t;
139 * Typedef for sockets (socket_t) vector
141 typedef std::vector<socket_t> socket_v;
142 typedef std::vector<socket_t>::const_iterator socket_v_it;
148 Struct for called and calling party number from Q763 Signalling System No. 7
\96 ISDN user part formats and codes
154 |---------|---------|---------|---------|---------|---------|---------|---------|
155 1 | O/E | Nature of address indicator |
156 |---------|---------|---------|---------|---------|---------|---------|---------|
157 2 | INN | Numbering plan indicator | spare |
158 |---------|---------|---------|---------|---------|---------|---------|---------|
159 3 | 2nd address signal | 1st address signal |
160 |---------|---------|---------|---------|---------|---------|---------|---------|
161 ... ... ... ... ... ... ... ... ...
162 |---------|---------|---------|---------|---------|---------|---------|---------|
163 m | Filler (if necessary) | nth address signal |
164 |---------|---------|---------|---------|---------|---------|---------|---------|
170 |---------|---------|---------|---------|---------|---------|---------|---------|
171 1 | O/E | Nature of address indicator |
172 |---------|---------|---------|---------|---------|---------|---------|---------|
173 2 | NI | Numbering plan indicator |Add.Pres.Restr.Ind | Screening Ind |
174 |---------|---------|---------|---------|---------|---------|---------|---------|
175 3 | 2nd address signal | 1st address signal |
176 |---------|---------|---------|---------|---------|---------|---------|---------|
177 ... ... ... ... ... ... ... ... ...
178 |---------|---------|---------|---------|---------|---------|---------|---------|
179 m | Filler (if necessary) | nth address signal |
180 |---------|---------|---------|---------|---------|---------|---------|---------|
185 /** odd/even (1/0) indicator */
188 /** Return true when have odd number of digits */
189 bool isOdd() { return OddEven; }
190 /** Return true when have even number of digits */
191 bool isEven() { return !isOdd(); }
195 Nature of address indicator
197 0 0 0 0 0 0 1 subscriber number (national use)
198 0 0 0 0 0 1 0 unknown (national use)
199 0 0 0 0 0 1 1 national (significant) number (national use)
200 0 0 0 0 1 0 0 international number
203 0 0 0 0 1 0 1 network-specific number (national use) ITU-T Q.763 (12/1999) 23
204 0 0 0 0 1 1 0 network routing number in national (significant) number format (national use)
205 0 0 0 0 1 1 1 network routing number in network-specific number format (national use)
206 0 0 0 1 0 0 0 network routing number concatenated with Called Directory Number (national use)
207 1 1 0 1 1 1 1 to 0 0 0 1 0 0 1 spare
208 1 1 1 1 1 1 0 to 1 1 1 0 0 0 0 reserved for national use
212 0 0 0 0 1 1 0 to 1 1 0 1 1 1 1 spare
213 1 1 1 0 0 0 0 to 1 1 1 1 1 1 0 reserved for national use
217 short NatureOfAddress;
219 /** Return true when Nature Of Address is 'subscriber number (national use)' */
220 bool NatureOfAddress_SubscriberNumber() { return (NatureOfAddress == 1); }
221 /** Return true when Nature Of Address is 'unknown (national use)' */
222 bool NatureOfAddress_Unknown() { return (NatureOfAddress == 2); }
223 /** Return true when Nature Of Address is 'national (significant) number (national use)' */
224 bool NatureOfAddress_NationalNumber() { return (NatureOfAddress == 3); }
225 /** Return true when Nature Of Address is 'international number' */
226 bool NatureOfAddress_InternationalNumber() { return (NatureOfAddress == 4); }
230 Internal Network Number indicator (INN) (only for called party number)
231 0 routing to internal network number allowed
232 1 routing to internal network number not allowed
235 short InternalNetworkNumber;
237 /** Return true when Internal Network Number Indicator is 'routing to internal network number allowed' */
238 bool InternalNetworkNumber_RoutingToInternalNetworkNumberAllowed() { return (InternalNetworkNumber == 0); }
239 /** Return true when Internal Network Number Indicator is 'routing to internal network number not allowed' */
240 bool InternalNetworkNumber_RoutingToInternalNetworkNumberNotAllowed() { return (InternalNetworkNumber == 1); }
244 Number Incomplete indicator (NI) (only for calling party number)
249 short NumberIncomplete;
251 /** Return true when Number Incomplete Indicator is 'complete' */
252 bool NumberIncomplete_Complete() { return (NumberIncomplete == 0); }
253 /** Return true when Number Incomplete Indicator is 'incomplete' */
254 bool NumberIncomplete_Incomplete() { return (NumberIncomplete == 1); }
258 Numbering plan indicator
260 0 0 1 ISDN (Telephony) numbering plan (ITU-T Recommendation E.164)
262 0 1 1 Data numbering plan (ITU-T Recommendation X.121) (national use)
263 1 0 0 Telex numbering plan (ITU-T Recommendation F.69) (national use)
264 1 0 1 reserved for national use
265 1 1 0 reserved for national use
271 /** Return true when Numbering Plan is 'ISDN (Telephony) numbering plan (ITU-T Recommendation E.164)' */
272 bool NumberingPlan_ISDN() { return (NumberingPlan == 1); }
273 /** Return true when Numbering Plan is 'Data numbering plan (ITU-T Recommendation X.121) (national use)' */
274 bool NumberingPlan_Data() { return (NumberingPlan == 3); }
275 /** Return true when Numbering Plan is 'Telex numbering plan (ITU-T Recommendation F.69) (national use)' */
276 bool NumberingPlan_Telex() { return (NumberingPlan == 4); }
280 Address presentation restricted indicator (only for calling party number)
281 0 0 presentation allowed
282 0 1 presentation restricted
283 1 0 address not available (Note 1) (national use)
284 1 1 reserved for restriction by the network
285 NOTE 1
\96 If the parameter is included and the address presentation restricted indicator indicates
286 address not available, octets 3 to n are omitted, the subfields in items 'OddEven', 'NatureOfAddress',
287 'NumberIncomplete' and 'NumberingPlan' are coded with 0's, and the subfield 'Screening' is coded with 11.
290 short AddressPresentationRestricted;
292 /** Return true when Address Presentation Restricted is 'presentation allowed' */
293 bool AddressPresentationRestricted_PresentationAllowed() { return (AddressPresentationRestricted == 0); }
294 /** Return true when Address Presentation Restricted is 'presentation restricted' */
295 bool AddressPresentationRestricted_PresentationRestricted() { return (AddressPresentationRestricted == 1); }
296 /** Return true when Address Presentation Restricted is 'address not available (Note 1) (national use)' */
297 bool AddressPresentationRestricted_AddressNotAvailable() { return (AddressPresentationRestricted == 2); }
298 /** Return true when Address Presentation Restricted is 'reserved for restriction by the network' */
299 bool AddressPresentationRestricted_ReservedForRestrictionByTheNetwork() { return (AddressPresentationRestricted == 3); }
303 Screening indicator (only for calling party number)
304 0 0 reserved (Note 2)
305 0 1 user provided, verified and passed
306 1 0 reserved (Note 2)
308 NOTE 2
\96 Code 00 and 10 are reserved for "user provided, not verified" and "user provided, verified
309 and failed" respectively. Codes 00 and 10 are for national use.
314 /** Return true when Screening is 'user provided, verified and passed' */
315 bool Screening_UserProvidedVerifiedAndPassed() { return (Screening == 1); }
316 /** Return true when Screening is 'network provided' */
317 bool Screening_NetworkProvided() { return (Screening == 3); }
338 1 1 1 1 ST (for called party number, spare in calling party number)
339 The most significant address signal is sent first. Subsequent address signals are sent in
340 successive 4-bit fields.
342 Filler: In case of an odd number of address signals, the filler code 0000 is inserted after the last
351 InternalNetworkNumber = 0;
352 NumberIncomplete = 0;
354 AddressPresentationRestricted = 0;
360 * Class string representation
362 * @param calledOrCalling Boolean about being called party number or calling one
364 * @return String with class content
366 std::string asString(bool calledOrCalling) {
369 result = "OddEven: ";
370 sprintf(aux, "%d", OddEven); result += std::string(aux);
371 result += " | NatureOfAddress: ";
373 if(calledOrCalling) {
374 sprintf(aux, "%d", NatureOfAddress); result += std::string(aux);
375 result += " | InternalNetworkNumber: ";
377 sprintf(aux, "%d", InternalNetworkNumber); result += std::string(aux);
378 result += " | NumberIncomplete: ";
381 sprintf(aux, "%d", NumberIncomplete); result += std::string(aux);
382 result += " | NumberingPlan: ";
383 sprintf(aux, "%d", NumberingPlan); result += std::string(aux);
385 if(!calledOrCalling) {
386 result += " | AddressPresentationRestricted: ";
387 sprintf(aux, "%d", AddressPresentationRestricted); result += std::string(aux);
388 result += " | Screening: ";
389 sprintf(aux, "%d", Screening); result += std::string(aux);
392 result += " | Digits: ";
393 result += (Digits != "") ? Digits : "<null>";
397 // int getEncodedLength() {
399 // bool filler = OddEven;
400 // bool hasDigits = (Digits.size() > 0);
401 // return (2 + Digits.size()/2 + ((hasDigits && filler) ? 1:0));
404 } isup_number_t; // ISDN user part parameters
408 * IANA Address Family Numbers
409 * @see http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xml
413 //Number Description Reference
414 //------ ---------------------------------------------------- ---------
416 IPv4 = 1, //IP (IP version 4)
417 IPv6 = 2, //IP6 (IP version 6)
419 // 4 HDLC (8-bit multidrop)
421 // 6 802 (includes all 802 media plus Ethernet "canonical format")
423 E164 = 8 //E.164 (SMDS, Frame Relay, ATM)
425 // 10 X.121 (X.25, Frame Relay)
430 // 15 E.164 with NSAP format subaddress [UNI-3.1] [Andy_Malis]
431 // 16 DNS (Domain Name System)
432 // 17 Distinguished Name [Charles_Lynn]
433 // 18 AS Number [Charles_Lynn]
434 // 19 XTP over IP version 4 [Mike_Saul]
435 // 20 XTP over IP version 6 [Mike_Saul]
436 // 21 XTP native mode XTP [Mike_Saul]
437 // 22 Fibre Channel World-Wide Port Name [Mark_Bakke]
438 // 23 Fibre Channel World-Wide Node Name [Mark_Bakke]
439 // 24 GWID [Subra_Hegde]
440 // 25 AFI for L2VPN information [RFC4761][RFC6074]
441 // 26-16383 Unassigned
442 // 16384 EIGRP Common Service Family [Donnie_Savage] 2008-05-13
443 // 16385 EIGRP IPv4 Service Family [Donnie_Savage] 2008-05-13
444 // 16386 EIGRP IPv6 Service Family [Donnie_Savage] 2008-05-13
445 // 16387 LISP Canonical Address Format (LCAF) [David_Meyer] 2009-11-12
446 // 16388-32767 Unassigned
447 // 32768-65534 Unassigned
452 * Version description
453 * @param v Version type
454 * @return Version description
456 static const char* asText(const _v v) throw() { // anna_declare_enum is not safe, because labels don't have to match a sequence
457 if(v == IPv4) return "IPv4";
459 if(v == IPv6) return "IPv6";
461 if(v == E164) return "E164";
466 } iana_address_version_t;
470 Struct for IANA Addresses
474 /** address version */
477 /** address printable value. No checkings are done regarding specific version (application responsability) */
481 /** Gets the address version */
482 const U16 & getVersion() const throw() { return Version; }
484 /** Gets the address printable value */
485 const char * getValue() const throw() { return Value.c_str(); }
490 /** Return true when is an IPv4 address */
491 bool isIPv4() const throw() { return ((iana_address_version_t::_v)Version == iana_address_version_t::IPv4); }
492 /** Return true when is an IPv6 address */
493 bool isIPv6() const throw() { return ((iana_address_version_t::_v)Version == iana_address_version_t::IPv6); }
494 /** Return true when is an E164 (SMDS, Frame Relay, ATM) address */
495 bool isE164() const throw() { return ((iana_address_version_t::_v)Version == iana_address_version_t::E164); }
497 /** Sets version for IPv4 address and address itself. Checking is not performed (could assign IPv6 instead ...) */
498 void setIPv4(const char *value) throw() { Version = iana_address_version_t::IPv4; Value = value ? value : ""; }
500 /** Sets version for IPv6 address and address itself. Checking is not performed (could assign IPv4 instead ...) */
501 void setIPv6(const char *value) throw() { Version = iana_address_version_t::IPv6; Value = value ? value : ""; }
503 /** Sets version for E164 address and address itself. Checking is not performed ... */
504 void setE164(const char *value) throw() { Version = iana_address_version_t::E164; Value = value ? value : ""; }
508 * Class string representation
510 * @return String with class content
512 std::string asString() const throw() {
514 result += Value.c_str(); // assume that all IANA addresses have a printable representation
516 const char *versionAsText = iana_address_version_t::asText((iana_address_version_t::_v)Version);
519 result += versionAsText;
523 result += "IANA version '";
525 sprintf(aux, "%d", Version);