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