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_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;
122 /** Alias for long long: signed integer with 64 bits */
124 /** Alias for unsigned long long: unsigned integer with 64 bits */
125 typedef u_int64_t U64;
128 /** Alias for long long: signed integer with 64 bits */
129 typedef long long int S64;
130 /** Alias for unsigned long long: unsigned integer with 64 bits */
131 typedef unsigned long long int U64;
134 /** Alias for float: floating number with 32 bits (1-8-23) */
137 /** Alias for double: floating number with 64 bits (1-11-52) */
140 /** Alias for unsigned int: unsigned integer with 32 bits used to contain 24 bits */
147 * Typedef for socket literal representation: <ip|hostname>:<port>
149 typedef std::pair <std::string, int> socket_t;
152 * Typedef for sockets (socket_t) vector
154 typedef std::vector<socket_t> socket_v;
155 typedef std::vector<socket_t>::const_iterator socket_v_it;
161 Struct for called and calling party number from Q763 Signalling System No. 7
\96 ISDN user part formats and codes
167 |---------|---------|---------|---------|---------|---------|---------|---------|
168 1 | O/E | Nature of address indicator |
169 |---------|---------|---------|---------|---------|---------|---------|---------|
170 2 | INN | Numbering plan indicator | spare |
171 |---------|---------|---------|---------|---------|---------|---------|---------|
172 3 | 2nd address signal | 1st address signal |
173 |---------|---------|---------|---------|---------|---------|---------|---------|
174 ... ... ... ... ... ... ... ... ...
175 |---------|---------|---------|---------|---------|---------|---------|---------|
176 m | Filler (if necessary) | nth address signal |
177 |---------|---------|---------|---------|---------|---------|---------|---------|
183 |---------|---------|---------|---------|---------|---------|---------|---------|
184 1 | O/E | Nature of address indicator |
185 |---------|---------|---------|---------|---------|---------|---------|---------|
186 2 | NI | Numbering plan indicator |Add.Pres.Restr.Ind | Screening Ind |
187 |---------|---------|---------|---------|---------|---------|---------|---------|
188 3 | 2nd address signal | 1st address signal |
189 |---------|---------|---------|---------|---------|---------|---------|---------|
190 ... ... ... ... ... ... ... ... ...
191 |---------|---------|---------|---------|---------|---------|---------|---------|
192 m | Filler (if necessary) | nth address signal |
193 |---------|---------|---------|---------|---------|---------|---------|---------|
198 /** odd/even (1/0) indicator */
201 /** Return true when have odd number of digits */
202 bool isOdd() { return OddEven; }
203 /** Return true when have even number of digits */
204 bool isEven() { return !isOdd(); }
208 Nature of address indicator
210 0 0 0 0 0 0 1 subscriber number (national use)
211 0 0 0 0 0 1 0 unknown (national use)
212 0 0 0 0 0 1 1 national (significant) number (national use)
213 0 0 0 0 1 0 0 international number
216 0 0 0 0 1 0 1 network-specific number (national use) ITU-T Q.763 (12/1999) 23
217 0 0 0 0 1 1 0 network routing number in national (significant) number format (national use)
218 0 0 0 0 1 1 1 network routing number in network-specific number format (national use)
219 0 0 0 1 0 0 0 network routing number concatenated with Called Directory Number (national use)
220 1 1 0 1 1 1 1 to 0 0 0 1 0 0 1 spare
221 1 1 1 1 1 1 0 to 1 1 1 0 0 0 0 reserved for national use
225 0 0 0 0 1 1 0 to 1 1 0 1 1 1 1 spare
226 1 1 1 0 0 0 0 to 1 1 1 1 1 1 0 reserved for national use
230 short NatureOfAddress;
232 /** Return true when Nature Of Address is 'subscriber number (national use)' */
233 bool NatureOfAddress_SubscriberNumber() { return (NatureOfAddress == 1); }
234 /** Return true when Nature Of Address is 'unknown (national use)' */
235 bool NatureOfAddress_Unknown() { return (NatureOfAddress == 2); }
236 /** Return true when Nature Of Address is 'national (significant) number (national use)' */
237 bool NatureOfAddress_NationalNumber() { return (NatureOfAddress == 3); }
238 /** Return true when Nature Of Address is 'international number' */
239 bool NatureOfAddress_InternationalNumber() { return (NatureOfAddress == 4); }
243 Internal Network Number indicator (INN) (only for called party number)
244 0 routing to internal network number allowed
245 1 routing to internal network number not allowed
248 short InternalNetworkNumber;
250 /** Return true when Internal Network Number Indicator is 'routing to internal network number allowed' */
251 bool InternalNetworkNumber_RoutingToInternalNetworkNumberAllowed() { return (InternalNetworkNumber == 0); }
252 /** Return true when Internal Network Number Indicator is 'routing to internal network number not allowed' */
253 bool InternalNetworkNumber_RoutingToInternalNetworkNumberNotAllowed() { return (InternalNetworkNumber == 1); }
257 Number Incomplete indicator (NI) (only for calling party number)
262 short NumberIncomplete;
264 /** Return true when Number Incomplete Indicator is 'complete' */
265 bool NumberIncomplete_Complete() { return (NumberIncomplete == 0); }
266 /** Return true when Number Incomplete Indicator is 'incomplete' */
267 bool NumberIncomplete_Incomplete() { return (NumberIncomplete == 1); }
271 Numbering plan indicator
273 0 0 1 ISDN (Telephony) numbering plan (ITU-T Recommendation E.164)
275 0 1 1 Data numbering plan (ITU-T Recommendation X.121) (national use)
276 1 0 0 Telex numbering plan (ITU-T Recommendation F.69) (national use)
277 1 0 1 reserved for national use
278 1 1 0 reserved for national use
284 /** Return true when Numbering Plan is 'ISDN (Telephony) numbering plan (ITU-T Recommendation E.164)' */
285 bool NumberingPlan_ISDN() { return (NumberingPlan == 1); }
286 /** Return true when Numbering Plan is 'Data numbering plan (ITU-T Recommendation X.121) (national use)' */
287 bool NumberingPlan_Data() { return (NumberingPlan == 3); }
288 /** Return true when Numbering Plan is 'Telex numbering plan (ITU-T Recommendation F.69) (national use)' */
289 bool NumberingPlan_Telex() { return (NumberingPlan == 4); }
293 Address presentation restricted indicator (only for calling party number)
294 0 0 presentation allowed
295 0 1 presentation restricted
296 1 0 address not available (Note 1) (national use)
297 1 1 reserved for restriction by the network
298 NOTE 1
\96 If the parameter is included and the address presentation restricted indicator indicates
299 address not available, octets 3 to n are omitted, the subfields in items 'OddEven', 'NatureOfAddress',
300 'NumberIncomplete' and 'NumberingPlan' are coded with 0's, and the subfield 'Screening' is coded with 11.
303 short AddressPresentationRestricted;
305 /** Return true when Address Presentation Restricted is 'presentation allowed' */
306 bool AddressPresentationRestricted_PresentationAllowed() { return (AddressPresentationRestricted == 0); }
307 /** Return true when Address Presentation Restricted is 'presentation restricted' */
308 bool AddressPresentationRestricted_PresentationRestricted() { return (AddressPresentationRestricted == 1); }
309 /** Return true when Address Presentation Restricted is 'address not available (Note 1) (national use)' */
310 bool AddressPresentationRestricted_AddressNotAvailable() { return (AddressPresentationRestricted == 2); }
311 /** Return true when Address Presentation Restricted is 'reserved for restriction by the network' */
312 bool AddressPresentationRestricted_ReservedForRestrictionByTheNetwork() { return (AddressPresentationRestricted == 3); }
316 Screening indicator (only for calling party number)
317 0 0 reserved (Note 2)
318 0 1 user provided, verified and passed
319 1 0 reserved (Note 2)
321 NOTE 2
\96 Code 00 and 10 are reserved for "user provided, not verified" and "user provided, verified
322 and failed" respectively. Codes 00 and 10 are for national use.
327 /** Return true when Screening is 'user provided, verified and passed' */
328 bool Screening_UserProvidedVerifiedAndPassed() { return (Screening == 1); }
329 /** Return true when Screening is 'network provided' */
330 bool Screening_NetworkProvided() { return (Screening == 3); }
351 1 1 1 1 ST (for called party number, spare in calling party number)
352 The most significant address signal is sent first. Subsequent address signals are sent in
353 successive 4-bit fields.
355 Filler: In case of an odd number of address signals, the filler code 0000 is inserted after the last
364 InternalNetworkNumber = 0;
365 NumberIncomplete = 0;
367 AddressPresentationRestricted = 0;
373 * Class string representation
375 * @param calledOrCalling Boolean about being called party number or calling one
377 * @return String with class content
379 std::string asString(bool calledOrCalling) {
382 result = "OddEven: ";
383 sprintf(aux, "%d", OddEven); result += std::string(aux);
384 result += " | NatureOfAddress: ";
386 if(calledOrCalling) {
387 sprintf(aux, "%d", NatureOfAddress); result += std::string(aux);
388 result += " | InternalNetworkNumber: ";
390 sprintf(aux, "%d", InternalNetworkNumber); result += std::string(aux);
391 result += " | NumberIncomplete: ";
394 sprintf(aux, "%d", NumberIncomplete); result += std::string(aux);
395 result += " | NumberingPlan: ";
396 sprintf(aux, "%d", NumberingPlan); result += std::string(aux);
398 if(!calledOrCalling) {
399 result += " | AddressPresentationRestricted: ";
400 sprintf(aux, "%d", AddressPresentationRestricted); result += std::string(aux);
401 result += " | Screening: ";
402 sprintf(aux, "%d", Screening); result += std::string(aux);
405 result += " | Digits: ";
406 result += (Digits != "") ? Digits : "<null>";
410 // int getEncodedLength() {
412 // bool filler = OddEven;
413 // bool hasDigits = (Digits.size() > 0);
414 // return (2 + Digits.size()/2 + ((hasDigits && filler) ? 1:0));
417 } isup_number_t; // ISDN user part parameters
421 * IANA Address Family Numbers
422 * @see http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xml
426 //Number Description Reference
427 //------ ---------------------------------------------------- ---------
429 IPv4 = 1, //IP (IP version 4)
430 IPv6 = 2, //IP6 (IP version 6)
432 // 4 HDLC (8-bit multidrop)
434 // 6 802 (includes all 802 media plus Ethernet "canonical format")
436 E164 = 8 //E.164 (SMDS, Frame Relay, ATM)
438 // 10 X.121 (X.25, Frame Relay)
443 // 15 E.164 with NSAP format subaddress [UNI-3.1] [Andy_Malis]
444 // 16 DNS (Domain Name System)
445 // 17 Distinguished Name [Charles_Lynn]
446 // 18 AS Number [Charles_Lynn]
447 // 19 XTP over IP version 4 [Mike_Saul]
448 // 20 XTP over IP version 6 [Mike_Saul]
449 // 21 XTP native mode XTP [Mike_Saul]
450 // 22 Fibre Channel World-Wide Port Name [Mark_Bakke]
451 // 23 Fibre Channel World-Wide Node Name [Mark_Bakke]
452 // 24 GWID [Subra_Hegde]
453 // 25 AFI for L2VPN information [RFC4761][RFC6074]
454 // 26-16383 Unassigned
455 // 16384 EIGRP Common Service Family [Donnie_Savage] 2008-05-13
456 // 16385 EIGRP IPv4 Service Family [Donnie_Savage] 2008-05-13
457 // 16386 EIGRP IPv6 Service Family [Donnie_Savage] 2008-05-13
458 // 16387 LISP Canonical Address Format (LCAF) [David_Meyer] 2009-11-12
459 // 16388-32767 Unassigned
460 // 32768-65534 Unassigned
465 * Version description
466 * @param v Version type
467 * @return Version description
469 static const char* asText(const _v v) throw() { // anna_declare_enum is not safe, because labels don't have to match a sequence
470 if(v == IPv4) return "IPv4";
472 if(v == IPv6) return "IPv6";
474 if(v == E164) return "E164";
479 } iana_address_version_t;
483 Struct for IANA Addresses
487 /** address version */
490 /** address printable value. No checkings are done regarding specific version (application responsability) */
494 /** Gets the address version */
495 const U16 & getVersion() const throw() { return Version; }
497 /** Gets the address printable value */
498 const char * getValue() const throw() { return Value.c_str(); }
503 /** Return true when is an IPv4 address */
504 bool isIPv4() const throw() { return ((iana_address_version_t::_v)Version == iana_address_version_t::IPv4); }
505 /** Return true when is an IPv6 address */
506 bool isIPv6() const throw() { return ((iana_address_version_t::_v)Version == iana_address_version_t::IPv6); }
507 /** Return true when is an E164 (SMDS, Frame Relay, ATM) address */
508 bool isE164() const throw() { return ((iana_address_version_t::_v)Version == iana_address_version_t::E164); }
510 /** Sets version for IPv4 address and address itself. Checking is not performed (could assign IPv6 instead ...) */
511 void setIPv4(const char *value) throw() { Version = iana_address_version_t::IPv4; Value = value ? value : ""; }
513 /** Sets version for IPv6 address and address itself. Checking is not performed (could assign IPv4 instead ...) */
514 void setIPv6(const char *value) throw() { Version = iana_address_version_t::IPv6; Value = value ? value : ""; }
516 /** Sets version for E164 address and address itself. Checking is not performed ... */
517 void setE164(const char *value) throw() { Version = iana_address_version_t::E164; Value = value ? value : ""; }
521 * Class string representation
523 * @return String with class content
525 std::string asString() const throw() {
527 result += Value.c_str(); // assume that all IANA addresses have a printable representation
529 const char *versionAsText = iana_address_version_t::asText((iana_address_version_t::_v)Version);
532 result += versionAsText;
536 result += "IANA version '";
538 sprintf(aux, "%d", Version);