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