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