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