Updated license
[anna.git] / include / anna / core / util / defines.hpp
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 #ifndef anna_core_util_defines_hpp
38 #define anna_core_util_defines_hpp
39
40 // STL
41 #include <algorithm>
42 #include <string>
43 #include <vector>
44
45 #include <stdio.h>
46
47
48
49 // Decoding helpers
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))
53
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)
57
58
59 namespace anna {
60
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
67 //
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)
71 //
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'.
75 //
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.
80 //
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
85 //
86 //      unsigned long int       32/64 (4/8)        lu
87 //      long int                32/64 (4/8)        ld
88 //
89 //      unsigned long long int  64 (8)             llu     Unsigned64      U64
90 //      long long int           64 (8)             lld     Integer64       S64
91 //
92 //      float                   32 (4)             f
93 //      double                  64 (8)             lf
94 //      long double             80 (10)            Lf
95
96
97 /** Alias for unsigned char: unsigned integer with 8 bits */
98 typedef unsigned char U8;
99
100 /** Alias for char: signed integer with 8 bits */
101 typedef char S8;
102
103 /** Alias for unsigned short int: unsigned integer with 16 bits */
104 typedef unsigned short int U16;
105
106 /** Alias for short int: signed integer with 16 bits */
107 typedef short int S16;
108
109 /** Alias for unsigned int: unsigned integer with 32 bits */
110 typedef unsigned int U32;
111
112 /** Alias for int: signed integer with 32 bits */
113 typedef int S32;
114
115 ///** Alias for unsigned long long: unsigned integer with 64 bits */
116 //typedef unsigned long long int U64;
117 //
118 ///** Alias for long long: signed integer with 64 bits */
119 //typedef long long int S64;
120 #ifndef __x86_64__
121 #undef  __anna64__
122 /** Alias for long long: signed integer with 64 bits */
123 typedef int64_t S64
124 /** Alias for unsigned long long: unsigned integer with 64 bits */
125 typedef u_int64_t U64;
126 #else
127 #define __anna64__
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;
132 #endif
133
134 /** Alias for float: floating number with 32 bits (1-8-23) */
135 typedef float F32;
136
137 /** Alias for double: floating number with 64 bits (1-11-52) */
138 typedef double F64;
139
140 /** Alias for unsigned int: unsigned integer with 32 bits used to contain 24 bits */
141 typedef U32 U24;
142
143
144 // Communication
145
146 /**
147 * Typedef for socket literal representation: <ip|hostname>:<port>
148 */
149 typedef std::pair <std::string, int> socket_t;
150
151 /**
152 * Typedef for sockets (socket_t) vector
153 */
154 typedef std::vector<socket_t> socket_v;
155 typedef std::vector<socket_t>::const_iterator socket_v_it;
156
157
158
159
160 /**
161    Struct for called and calling party number from Q763 Signalling System No. 7 \96 ISDN user part formats and codes
162
163    <pre>
164
165    Called Party Number
166               8         7         6         5         4         3         2         1
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          |---------|---------|---------|---------|---------|---------|---------|---------|
178
179
180
181    Calling Party Number
182               8         7         6         5         4         3         2         1
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          |---------|---------|---------|---------|---------|---------|---------|---------|
194
195    </pre>
196 */
197 typedef struct {
198   /**  odd/even (1/0) indicator */
199   short OddEven;
200
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(); }
205
206
207   /** <pre>
208      Nature of address indicator
209      0 0 0 0 0 0 0 spare
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
214
215    Called:
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
222      1 1 1 1 1 1 1 spare
223
224    Calling:
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
227      1 1 1 1 1 1 1 spare
228      </pre>
229    */
230   short NatureOfAddress;
231
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); }
240
241
242   /** <pre>
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
246      </pre>
247    */
248   short InternalNetworkNumber;
249
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); }
254
255
256   /** <pre>
257      Number Incomplete indicator (NI) (only for calling party number)
258      0 complete
259      1 incomplete
260      </pre>
261    */
262   short NumberIncomplete;
263
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); }
268
269
270   /** <pre>
271      Numbering plan indicator
272      0 0 0 spare
273      0 0 1 ISDN (Telephony) numbering plan (ITU-T Recommendation E.164)
274      0 1 0 spare
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
279      1 1 1 spare
280      </pre>
281    */
282   short NumberingPlan;
283
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); }
290
291
292   /** <pre>
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.
301      </pre>
302    */
303   short AddressPresentationRestricted;
304
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); }
313
314
315   /** <pre>
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)
320      1 1 network provided
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.
323      </pre>
324    */
325   short Screening;
326
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); }
331
332
333   /** <pre>
334      BCD digit
335      Address signal
336      0 0 0 0 digit 0
337      0 0 0 1 digit 1
338      0 0 1 0 digit 2
339      0 0 1 1 digit 3
340      0 1 0 0 digit 4
341      0 1 0 1 digit 5
342      0 1 1 0 digit 6
343      0 1 1 1 digit 7
344      1 0 0 0 digit 8
345      1 0 0 1 digit 9
346      1 0 1 0 spare
347      1 0 1 1 code 11
348      1 1 0 0 code 12
349      1 1 0 1 spare
350      1 1 1 0 spare
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.
354
355      Filler: In case of an odd number of address signals, the filler code 0000 is inserted after the last
356              address signal.
357      </pre>
358    */
359   std::string Digits;
360
361   void reset() {
362     OddEven = 0;
363     NatureOfAddress = 0;
364     InternalNetworkNumber = 0;
365     NumberIncomplete = 0;
366     NumberingPlan = 0;
367     AddressPresentationRestricted = 0;
368     Screening = 0;
369     Digits = "";
370   }
371
372   /**
373   * Class string representation
374   *
375   * @param calledOrCalling Boolean about being called party number or calling one
376   *
377   * @return String with class content
378   */
379   std::string asString(bool calledOrCalling) {
380     std::string result;
381     char aux [16];
382     result = "OddEven: ";
383     sprintf(aux, "%d", OddEven); result += std::string(aux);
384     result += " | NatureOfAddress: ";
385
386     if(calledOrCalling) {
387       sprintf(aux, "%d", NatureOfAddress); result += std::string(aux);
388       result += " | InternalNetworkNumber: ";
389     } else {
390       sprintf(aux, "%d", InternalNetworkNumber); result += std::string(aux);
391       result += " | NumberIncomplete: ";
392     }
393
394     sprintf(aux, "%d", NumberIncomplete); result += std::string(aux);
395     result += " | NumberingPlan: ";
396     sprintf(aux, "%d", NumberingPlan); result += std::string(aux);
397
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);
403     }
404
405     result += " | Digits: ";
406     result += (Digits != "") ? Digits : "<null>";
407     return result;
408   }
409
410 //   int getEncodedLength() {
411 //
412 //      bool filler = OddEven;
413 //      bool hasDigits = (Digits.size() > 0);
414 //      return (2 + Digits.size()/2 + ((hasDigits && filler) ? 1:0));
415 //   }
416
417 } isup_number_t; // ISDN user part parameters
418
419
420 /**
421 * IANA Address Family Numbers
422 * @see http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xml
423 */
424 typedef struct {
425   enum _v {
426     //Number    Description                                          Reference
427     //------    ---------------------------------------------------- ---------
428     //     0    Reserved
429     IPv4 = 1, //IP (IP version 4)
430     IPv6 = 2, //IP6 (IP version 6)
431     //     3    NSAP
432     //     4    HDLC (8-bit multidrop)
433     //     5    BBN 1822
434     //     6    802 (includes all 802 media plus Ethernet "canonical format")
435     //     7    E.163
436     E164 = 8  //E.164 (SMDS, Frame Relay, ATM)
437     //     9    F.69 (Telex)
438     //    10    X.121 (X.25, Frame Relay)
439     //    11    IPX
440     //    12    Appletalk
441     //    13    Decnet IV
442     //    14    Banyan Vines
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
461     //    65535 Reserved
462   };
463
464   /**
465   * Version description
466   * @param v Version type
467   * @return Version description
468   */
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";
471
472     if(v == IPv6) return "IPv6";
473
474     if(v == E164) return "E164";
475
476     return NULL;
477   }
478
479 } iana_address_version_t;
480
481
482 /**
483    Struct for IANA Addresses
484 */
485 typedef struct {
486
487   /** address version */
488   U16 Version;
489
490   /** address printable value. No checkings are done regarding specific version (application responsability) */
491   std::string Value;
492
493
494   /** Gets the address version */
495   const U16 & getVersion() const throw() { return Version; }
496
497   /** Gets the address printable value */
498   const char * getValue() const throw() { return Value.c_str(); }
499
500
501   // Helpers
502
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); }
509
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 : ""; }
512
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 : ""; }
515
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 : ""; }
518
519
520   /**
521   * Class string representation
522   *
523   * @return String with class content
524   */
525   std::string asString() const throw() {
526     std::string result;
527     result += Value.c_str(); // assume that all IANA addresses have a printable representation
528     result += " (";
529     const char *versionAsText = iana_address_version_t::asText((iana_address_version_t::_v)Version);
530
531     if(versionAsText) {
532       result += versionAsText;
533       result += ", ";
534     }
535
536     result += "IANA version '";
537     char aux [16];
538     sprintf(aux, "%d", Version);
539     result += aux;
540     result += "')";
541     return result;
542   }
543
544 } iana_address_t;
545
546
547 };
548
549
550 #endif