Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / include / anna / core / util / defines.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_core_util_defines_hpp
10 #define anna_core_util_defines_hpp
11
12 // STL
13 #include <algorithm>
14 #include <string>
15 #include <vector>
16
17 #include <stdio.h>
18
19
20
21 // Decoding helpers
22 #define DECODE2BYTES_INDX_VALUETYPE(buffer,indx,value_type) ((((value_type)buffer[indx] << 8) & 0xFF00) + ((value_type)buffer[indx+1] & 0x00FF))
23 #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))
24 #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))
25
26 //#define DECODE2BYTES_VALUETYPE(buffer,value_type) DECODE2BYTES_INDX_VALUETYPE(buffer,0,value_type)
27 //#define DECODE3BYTES_VALUETYPE(buffer,value_type) DECODE3BYTES_INDX_VALUETYPE(buffer,0,value_type)
28 //#define DECODE4BYTES_VALUETYPE(buffer,value_type) DECODE4BYTES_INDX_VALUETYPE(buffer,0,value_type)
29
30
31 namespace anna {
32
33 //      type                    bits (bytes)       %       Diameter Data   typedef
34 //      -----------------------------------------------------------------------
35 //      unsigned short int      16 (2)             hu                      U16
36 //      short int               16 (2)             hd                      S16
37 //      unsigned int            32 (4)             u       Unsigned32 (*)  U32
38 //      int                     32 (4)             d       Integer32 (*)   S32
39 //
40 // Integer. Its length traditionally depends on the length of the system's Word type, thus in MSDOS
41 // it is 16 bits long, whereas in 32 bit systems (like Windows 9x/2000/NT and systems that work under
42 // protected mode in x86 systems) it is 32 bits long (4 bytes)
43 //
44 // Como está previsto que en algunas máquinas la palabra sea de 16 bits, los enteros serían
45 // de 16 y por ello C contempla: int (serían 16 bits), long int (32), long long int (64). Sin embargo
46 // en la práctica, 'int = long int = 32' y 'long long int = 64'.
47 //
48 // (*) Por un mal hábito, representamos enteros de 32 bits con el tipo 'int'/'unsigned int' de
49 // toda la vida, sin darnos cuenta de que en alguna máquina antigua, no tendría 32 bits.
50 // Corregir lo anterior sería tan sencillo como poner S32 = long int (no int),
51 //  y U32 = unsigned long int (no unsigned int). Pero no lo vamos a hacer.
52 //
53 // El tipo 'long' tiene un tamaño que corresponde con el ancho de palabra del S.O.
54 // En Solaris (palabra de 64) tenemos long = 64 = long long
55 // En linux   (palabra de 32) tenemos long = 32,  long long = 64
56 // En linux64 (palabra de 64) tenemos long = 64 = long long
57 //
58 //      unsigned long int       32/64 (4/8)        lu
59 //      long int                32/64 (4/8)        ld
60 //
61 //      unsigned long long int  64 (8)             llu     Unsigned64      U64
62 //      long long int           64 (8)             lld     Integer64       S64
63 //
64 //      float                   32 (4)             f
65 //      double                  64 (8)             lf
66 //      long double             80 (10)            Lf
67
68
69 /** Alias for unsigned char: unsigned integer with 8 bits */
70 typedef unsigned char U8;
71
72 /** Alias for char: signed integer with 8 bits */
73 typedef char S8;
74
75 /** Alias for unsigned short int: unsigned integer with 16 bits */
76 typedef unsigned short int U16;
77
78 /** Alias for short int: signed integer with 16 bits */
79 typedef short int S16;
80
81 /** Alias for unsigned int: unsigned integer with 32 bits */
82 typedef unsigned int U32;
83
84 /** Alias for int: signed integer with 32 bits */
85 typedef int S32;
86
87 ///** Alias for unsigned long long: unsigned integer with 64 bits */
88 //typedef unsigned long long int U64;
89 //
90 ///** Alias for long long: signed integer with 64 bits */
91 //typedef long long int S64;
92 #ifndef __x86_64__
93 #undef  __anna64__
94 /** Alias for long long: signed integer with 64 bits */
95 typedef int64_t S64
96 /** Alias for unsigned long long: unsigned integer with 64 bits */
97 typedef u_int64_t U64;
98 #else
99 #define __anna64__
100 /** Alias for long long: signed integer with 64 bits */
101 typedef long long int S64;
102 /** Alias for unsigned long long: unsigned integer with 64 bits */
103 typedef unsigned long long int U64;
104 #endif
105
106 /** Alias for float: floating number with 32 bits (1-8-23) */
107 typedef float F32;
108
109 /** Alias for double: floating number with 64 bits (1-11-52) */
110 typedef double F64;
111
112 /** Alias for unsigned int: unsigned integer with 32 bits used to contain 24 bits */
113 typedef U32 U24;
114
115
116 // Communication
117
118 /**
119 * Typedef for socket literal representation: <ip|hostname>:<port>
120 */
121 typedef std::pair <std::string, int> socket_t;
122
123 /**
124 * Typedef for sockets (socket_t) vector
125 */
126 typedef std::vector<socket_t> socket_v;
127 typedef std::vector<socket_t>::const_iterator socket_v_it;
128
129
130
131
132 /**
133    Struct for called and calling party number from Q763 Signalling System No. 7 \96 ISDN user part formats and codes
134
135    <pre>
136
137    Called Party Number
138               8         7         6         5         4         3         2         1
139          |---------|---------|---------|---------|---------|---------|---------|---------|
140    1     |   O/E   |              Nature of address indicator                            |
141          |---------|---------|---------|---------|---------|---------|---------|---------|
142    2     |   INN   |   Numbering plan indicator  |                 spare                 |
143          |---------|---------|---------|---------|---------|---------|---------|---------|
144    3     |          2nd address signal           |           1st address signal          |
145          |---------|---------|---------|---------|---------|---------|---------|---------|
146         ...       ...       ...       ...       ...       ...       ...       ...       ...
147          |---------|---------|---------|---------|---------|---------|---------|---------|
148    m     |         Filler (if necessary)         |           nth address signal          |
149          |---------|---------|---------|---------|---------|---------|---------|---------|
150
151
152
153    Calling Party Number
154               8         7         6         5         4         3         2         1
155          |---------|---------|---------|---------|---------|---------|---------|---------|
156    1     |   O/E   |              Nature of address indicator                            |
157          |---------|---------|---------|---------|---------|---------|---------|---------|
158    2     |   NI    |   Numbering plan indicator  |Add.Pres.Restr.Ind |   Screening Ind   |
159          |---------|---------|---------|---------|---------|---------|---------|---------|
160    3     |          2nd address signal           |           1st address signal          |
161          |---------|---------|---------|---------|---------|---------|---------|---------|
162         ...       ...       ...       ...       ...       ...       ...       ...       ...
163          |---------|---------|---------|---------|---------|---------|---------|---------|
164    m     |         Filler (if necessary)         |           nth address signal          |
165          |---------|---------|---------|---------|---------|---------|---------|---------|
166
167    </pre>
168 */
169 typedef struct {
170   /**  odd/even (1/0) indicator */
171   short OddEven;
172
173   /** Return true when have odd number of digits */
174   bool isOdd() { return OddEven; }
175   /** Return true when have even number of digits */
176   bool isEven() { return !isOdd(); }
177
178
179   /** <pre>
180      Nature of address indicator
181      0 0 0 0 0 0 0 spare
182      0 0 0 0 0 0 1 subscriber number (national use)
183      0 0 0 0 0 1 0 unknown (national use)
184      0 0 0 0 0 1 1 national (significant) number (national use)
185      0 0 0 0 1 0 0 international number
186
187    Called:
188      0 0 0 0 1 0 1 network-specific number (national use) ITU-T Q.763 (12/1999) 23
189      0 0 0 0 1 1 0 network routing number in national (significant) number format (national use)
190      0 0 0 0 1 1 1 network routing number in network-specific number format (national use)
191      0 0 0 1 0 0 0 network routing number concatenated with Called Directory Number (national use)
192      1 1 0 1 1 1 1 to 0 0 0 1 0 0 1 spare
193      1 1 1 1 1 1 0 to 1 1 1 0 0 0 0 reserved for national use
194      1 1 1 1 1 1 1 spare
195
196    Calling:
197      0 0 0 0 1 1 0 to 1 1 0 1 1 1 1 spare
198      1 1 1 0 0 0 0 to 1 1 1 1 1 1 0 reserved for national use
199      1 1 1 1 1 1 1 spare
200      </pre>
201    */
202   short NatureOfAddress;
203
204   /** Return true when Nature Of Address is 'subscriber number (national use)' */
205   bool NatureOfAddress_SubscriberNumber() { return (NatureOfAddress == 1); }
206   /** Return true when Nature Of Address is 'unknown (national use)' */
207   bool NatureOfAddress_Unknown() { return (NatureOfAddress == 2); }
208   /** Return true when Nature Of Address is 'national (significant) number (national use)' */
209   bool NatureOfAddress_NationalNumber() { return (NatureOfAddress == 3); }
210   /** Return true when Nature Of Address is 'international number' */
211   bool NatureOfAddress_InternationalNumber() { return (NatureOfAddress == 4); }
212
213
214   /** <pre>
215      Internal Network Number indicator (INN) (only for called party number)
216      0 routing to internal network number allowed
217      1 routing to internal network number not allowed
218      </pre>
219    */
220   short InternalNetworkNumber;
221
222   /** Return true when Internal Network Number Indicator is 'routing to internal network number allowed' */
223   bool InternalNetworkNumber_RoutingToInternalNetworkNumberAllowed() { return (InternalNetworkNumber == 0); }
224   /** Return true when Internal Network Number Indicator is 'routing to internal network number not allowed' */
225   bool InternalNetworkNumber_RoutingToInternalNetworkNumberNotAllowed() { return (InternalNetworkNumber == 1); }
226
227
228   /** <pre>
229      Number Incomplete indicator (NI) (only for calling party number)
230      0 complete
231      1 incomplete
232      </pre>
233    */
234   short NumberIncomplete;
235
236   /** Return true when Number Incomplete Indicator is 'complete' */
237   bool NumberIncomplete_Complete() { return (NumberIncomplete == 0); }
238   /** Return true when Number Incomplete Indicator is 'incomplete' */
239   bool NumberIncomplete_Incomplete() { return (NumberIncomplete == 1); }
240
241
242   /** <pre>
243      Numbering plan indicator
244      0 0 0 spare
245      0 0 1 ISDN (Telephony) numbering plan (ITU-T Recommendation E.164)
246      0 1 0 spare
247      0 1 1 Data numbering plan (ITU-T Recommendation X.121) (national use)
248      1 0 0 Telex numbering plan (ITU-T Recommendation F.69) (national use)
249      1 0 1 reserved for national use
250      1 1 0 reserved for national use
251      1 1 1 spare
252      </pre>
253    */
254   short NumberingPlan;
255
256   /** Return true when Numbering Plan is 'ISDN (Telephony) numbering plan (ITU-T Recommendation E.164)' */
257   bool NumberingPlan_ISDN() { return (NumberingPlan == 1); }
258   /** Return true when Numbering Plan is 'Data numbering plan (ITU-T Recommendation X.121) (national use)' */
259   bool NumberingPlan_Data() { return (NumberingPlan == 3); }
260   /** Return true when Numbering Plan is 'Telex numbering plan (ITU-T Recommendation F.69) (national use)' */
261   bool NumberingPlan_Telex() { return (NumberingPlan == 4); }
262
263
264   /** <pre>
265      Address presentation restricted indicator (only for calling party number)
266      0 0 presentation allowed
267      0 1 presentation restricted
268      1 0 address not available (Note 1) (national use)
269      1 1 reserved for restriction by the network
270      NOTE 1 \96 If the parameter is included and the address presentation restricted indicator indicates
271      address not available, octets 3 to n are omitted, the subfields in items 'OddEven', 'NatureOfAddress',
272      'NumberIncomplete' and 'NumberingPlan' are coded with 0's, and the subfield 'Screening' is coded with 11.
273      </pre>
274    */
275   short AddressPresentationRestricted;
276
277   /** Return true when Address Presentation Restricted is 'presentation allowed' */
278   bool AddressPresentationRestricted_PresentationAllowed() { return (AddressPresentationRestricted == 0); }
279   /** Return true when Address Presentation Restricted is 'presentation restricted' */
280   bool AddressPresentationRestricted_PresentationRestricted() { return (AddressPresentationRestricted == 1); }
281   /** Return true when Address Presentation Restricted is 'address not available (Note 1) (national use)' */
282   bool AddressPresentationRestricted_AddressNotAvailable() { return (AddressPresentationRestricted == 2); }
283   /** Return true when Address Presentation Restricted is 'reserved for restriction by the network' */
284   bool AddressPresentationRestricted_ReservedForRestrictionByTheNetwork() { return (AddressPresentationRestricted == 3); }
285
286
287   /** <pre>
288      Screening indicator (only for calling party number)
289      0 0 reserved (Note 2)
290      0 1 user provided, verified and passed
291      1 0 reserved (Note 2)
292      1 1 network provided
293      NOTE 2 \96 Code 00 and 10 are reserved for "user provided, not verified" and "user provided, verified
294      and failed" respectively. Codes 00 and 10 are for national use.
295      </pre>
296    */
297   short Screening;
298
299   /** Return true when Screening is 'user provided, verified and passed' */
300   bool Screening_UserProvidedVerifiedAndPassed() { return (Screening == 1); }
301   /** Return true when Screening is 'network provided' */
302   bool Screening_NetworkProvided() { return (Screening == 3); }
303
304
305   /** <pre>
306      BCD digit
307      Address signal
308      0 0 0 0 digit 0
309      0 0 0 1 digit 1
310      0 0 1 0 digit 2
311      0 0 1 1 digit 3
312      0 1 0 0 digit 4
313      0 1 0 1 digit 5
314      0 1 1 0 digit 6
315      0 1 1 1 digit 7
316      1 0 0 0 digit 8
317      1 0 0 1 digit 9
318      1 0 1 0 spare
319      1 0 1 1 code 11
320      1 1 0 0 code 12
321      1 1 0 1 spare
322      1 1 1 0 spare
323      1 1 1 1 ST (for called party number, spare in calling party number)
324      The most significant address signal is sent first. Subsequent address signals are sent in
325      successive 4-bit fields.
326
327      Filler: In case of an odd number of address signals, the filler code 0000 is inserted after the last
328              address signal.
329      </pre>
330    */
331   std::string Digits;
332
333   void reset() {
334     OddEven = 0;
335     NatureOfAddress = 0;
336     InternalNetworkNumber = 0;
337     NumberIncomplete = 0;
338     NumberingPlan = 0;
339     AddressPresentationRestricted = 0;
340     Screening = 0;
341     Digits = "";
342   }
343
344   /**
345   * Class string representation
346   *
347   * @param calledOrCalling Boolean about being called party number or calling one
348   *
349   * @return String with class content
350   */
351   std::string asString(bool calledOrCalling) {
352     std::string result;
353     char aux [16];
354     result = "OddEven: ";
355     sprintf(aux, "%d", OddEven); result += std::string(aux);
356     result += " | NatureOfAddress: ";
357
358     if(calledOrCalling) {
359       sprintf(aux, "%d", NatureOfAddress); result += std::string(aux);
360       result += " | InternalNetworkNumber: ";
361     } else {
362       sprintf(aux, "%d", InternalNetworkNumber); result += std::string(aux);
363       result += " | NumberIncomplete: ";
364     }
365
366     sprintf(aux, "%d", NumberIncomplete); result += std::string(aux);
367     result += " | NumberingPlan: ";
368     sprintf(aux, "%d", NumberingPlan); result += std::string(aux);
369
370     if(!calledOrCalling) {
371       result += " | AddressPresentationRestricted: ";
372       sprintf(aux, "%d", AddressPresentationRestricted); result += std::string(aux);
373       result += " | Screening: ";
374       sprintf(aux, "%d", Screening); result += std::string(aux);
375     }
376
377     result += " | Digits: ";
378     result += (Digits != "") ? Digits : "<null>";
379     return result;
380   }
381
382 //   int getEncodedLength() {
383 //
384 //      bool filler = OddEven;
385 //      bool hasDigits = (Digits.size() > 0);
386 //      return (2 + Digits.size()/2 + ((hasDigits && filler) ? 1:0));
387 //   }
388
389 } isup_number_t; // ISDN user part parameters
390
391
392 /**
393 * IANA Address Family Numbers
394 * @see http://www.iana.org/assignments/address-family-numbers/address-family-numbers.xml
395 */
396 typedef struct {
397   enum _v {
398     //Number    Description                                          Reference
399     //------    ---------------------------------------------------- ---------
400     //     0    Reserved
401     IPv4 = 1, //IP (IP version 4)
402     IPv6 = 2, //IP6 (IP version 6)
403     //     3    NSAP
404     //     4    HDLC (8-bit multidrop)
405     //     5    BBN 1822
406     //     6    802 (includes all 802 media plus Ethernet "canonical format")
407     //     7    E.163
408     E164 = 8  //E.164 (SMDS, Frame Relay, ATM)
409     //     9    F.69 (Telex)
410     //    10    X.121 (X.25, Frame Relay)
411     //    11    IPX
412     //    12    Appletalk
413     //    13    Decnet IV
414     //    14    Banyan Vines
415     //    15    E.164 with NSAP format subaddress           [UNI-3.1] [Andy_Malis]
416     //    16    DNS (Domain Name System)
417     //    17    Distinguished Name                                    [Charles_Lynn]
418     //    18    AS Number                                             [Charles_Lynn]
419     //    19    XTP over IP version 4                                 [Mike_Saul]
420     //    20    XTP over IP version 6                                 [Mike_Saul]
421     //    21    XTP native mode XTP                                   [Mike_Saul]
422     //    22    Fibre Channel World-Wide Port Name                   [Mark_Bakke]
423     //    23    Fibre Channel World-Wide Node Name                   [Mark_Bakke]
424     //    24    GWID                                                 [Subra_Hegde]
425     //    25    AFI for L2VPN information [RFC4761][RFC6074]
426     //    26-16383 Unassigned
427     //    16384 EIGRP Common Service Family [Donnie_Savage] 2008-05-13
428     //    16385 EIGRP IPv4 Service Family [Donnie_Savage] 2008-05-13
429     //    16386 EIGRP IPv6 Service Family [Donnie_Savage] 2008-05-13
430     //    16387 LISP Canonical Address Format (LCAF) [David_Meyer] 2009-11-12
431     //    16388-32767 Unassigned
432     //    32768-65534 Unassigned
433     //    65535 Reserved
434   };
435
436   /**
437   * Version description
438   * @param v Version type
439   * @return Version description
440   */
441   static const char* asText(const _v v) throw() { // anna_declare_enum is not safe, because labels don't have to match a sequence
442     if(v == IPv4) return "IPv4";
443
444     if(v == IPv6) return "IPv6";
445
446     if(v == E164) return "E164";
447
448     return NULL;
449   }
450
451 } iana_address_version_t;
452
453
454 /**
455    Struct for IANA Addresses
456 */
457 typedef struct {
458
459   /** address version */
460   U16 Version;
461
462   /** address printable value. No checkings are done regarding specific version (application responsability) */
463   std::string Value;
464
465
466   /** Gets the address version */
467   const U16 & getVersion() const throw() { return Version; }
468
469   /** Gets the address printable value */
470   const char * getValue() const throw() { return Value.c_str(); }
471
472
473   // Helpers
474
475   /** Return true when is an IPv4 address */
476   bool isIPv4() const throw() { return ((iana_address_version_t::_v)Version == iana_address_version_t::IPv4); }
477   /** Return true when is an IPv6 address */
478   bool isIPv6() const throw() { return ((iana_address_version_t::_v)Version == iana_address_version_t::IPv6); }
479   /** Return true when is an E164 (SMDS, Frame Relay, ATM) address */
480   bool isE164() const throw() { return ((iana_address_version_t::_v)Version == iana_address_version_t::E164); }
481
482   /** Sets version for IPv4 address and address itself. Checking is not performed (could assign IPv6 instead ...) */
483   void setIPv4(const char *value) throw() { Version = iana_address_version_t::IPv4; Value = value ? value : ""; }
484
485   /** Sets version for IPv6 address and address itself. Checking is not performed (could assign IPv4 instead ...) */
486   void setIPv6(const char *value) throw() { Version = iana_address_version_t::IPv6; Value = value ? value : ""; }
487
488   /** Sets version for E164 address and address itself. Checking is not performed ... */
489   void setE164(const char *value) throw() { Version = iana_address_version_t::E164; Value = value ? value : ""; }
490
491
492   /**
493   * Class string representation
494   *
495   * @return String with class content
496   */
497   std::string asString() const throw() {
498     std::string result;
499     result += Value.c_str(); // assume that all IANA addresses have a printable representation
500     result += " (";
501     const char *versionAsText = iana_address_version_t::asText((iana_address_version_t::_v)Version);
502
503     if(versionAsText) {
504       result += versionAsText;
505       result += ", ";
506     }
507
508     result += "IANA version '";
509     char aux [16];
510     sprintf(aux, "%d", Version);
511     result += aux;
512     result += "')";
513     return result;
514   }
515
516 } iana_address_t;
517
518
519 };
520
521
522 #endif