1 /*
2  * Project    : ipv6calc
3  * File       : libipv6calc.h
4  * Version    : $Id: 102d525782583acf5d66046840d1116244b0c08e $
5  * Copyright  : 2001-2019 by Peter Bieringer <pb (at) bieringer.de>
6  *
7  * Information:
8  *  Header file for libipv6calc.c
9  */
10 
11 #include "ipv6calc_inttypes.h"
12 #include "ipv6calctypes.h"
13 #include "libmac.h"
14 #include "libipv4addr.h"
15 #include "libipv6addr.h"
16 #include "libipaddr.h"
17 #include "databases/lib/libipv6calc_db_wrapper.h"
18 #include "ipv6calcoptions.h"
19 #include "libipv6calcdebug.h"
20 
21 /* typedefs */
22 
23 #ifndef _libipv6calc_h
24 
25 #define _libipv6calc_h 1
26 
27 /* master filter structure */
28 typedef struct {
29 	s_ipv6calc_filter_ipv4addr filter_ipv4addr;
30 	s_ipv6calc_filter_ipv6addr filter_ipv6addr;
31 	s_ipv6calc_filter_macaddr  filter_macaddr;
32 } s_ipv6calc_filter_master;
33 
34 
35 /*@unused@*/ static const s_ipv6calc_anon_set ipv6calc_anon_set_list[] = {
36 	// name                   short  ip4 ip6 iid mac  keep-oui method
37 	{ "anonymize-standard"  , "as"  , 24, 56, 40, 24, 1       , ANON_METHOD_ANONYMIZE     },
38 	{ "anonymize-careful"   , "ac"  , 20, 48, 24, 24, 1       , ANON_METHOD_ANONYMIZE     },
39 	{ "anonymize-paranoid"  , "ap"  , 16, 40,  0, 24, 0       , ANON_METHOD_ANONYMIZE     },
40 	{ "zeroize-standard"    , "zs"  , 24, 56, 40, 24, 1       , ANON_METHOD_ZEROIZE       },
41 	{ "zeroize-careful"     , "zc"  , 20, 48, 24, 24, 1       , ANON_METHOD_ZEROIZE       },
42 	{ "zeroize-paranoid"    , "zp"  , 16, 40,  0, 24, 0       , ANON_METHOD_ZEROIZE       },
43 	{ "keep-type-asn-cc"    , "kp"  , 24, 56, 40, 24, 1       , ANON_METHOD_KEEPTYPEASNCC },
44 	{ "keep-type-geonameid" , "kg"  , 24, 56, 40, 24, 1       , ANON_METHOD_KEEPTYPEGEONAMEID }
45 };
46 
47 /*@unused@*/ static const s_ipv6calc_anon_methods ipv6calc_anon_methods[] = {
48 	{ "anonymize"        , 1, "reliable anonymization, keep as much type information as possible" },
49 	{ "zeroize"          , 2, "simple zeroizing according to given masks, probably loose type information" },
50 	{ "keep-type-asn-cc" , 3, "special reliable anonymization, keep type & Autonomous System Number and CountryCode" },
51 	{ "keep-type-geonameid", 4, "special reliable anonymization, keep type & GeonameID" }
52 };
53 
54 /* Registries (main registries must below 8 for anonymization mapping) */
55 #define REGISTRY_APNIC        0x02
56 #define REGISTRY_RIPENCC      0x03
57 #define REGISTRY_LACNIC       0x04
58 #define REGISTRY_AFRINIC      0x05
59 #define REGISTRY_ARIN         0x06
60 
61 #define REGISTRY_6BONE        0x08
62 #define REGISTRY_IANA         0x09
63 #define REGISTRY_6TO4         0x0c
64 #define REGISTRY_RESERVED     0x0e
65 #define REGISTRY_UNKNOWN      0x0f
66 
67 // new array (try to remove old arrays)
68 /*@unused@*/ static const s_type2 ipv6calc_registries[] = {
69         { REGISTRY_6BONE      , "6BONE"     , "REGISTRY_6BONE"   },
70         { REGISTRY_IANA       , "IANA"      , "REGISTRY_IANA"    },
71         { REGISTRY_APNIC      , "APNIC"     , "REGISTRY_APNIC"   },
72         { REGISTRY_ARIN       , "ARIN"      , "REGISTRY_ARIN"    },
73         { REGISTRY_RIPENCC    , "RIPENCC"   , "REGISTRY_RIPENCC" },
74         { REGISTRY_LACNIC     , "LACNIC"    , "REGISTRY_LACNIC"  },
75         { REGISTRY_AFRINIC    , "AFRINIC"   , "REGISTRY_AFRINIC" },
76         { REGISTRY_6TO4       , "6TO4"      , "REGISTRY_6TO4"    },
77         { REGISTRY_RESERVED   , "reserved"  , "REGISTRY_RESERVED"},
78         { REGISTRY_UNKNOWN    , "unknown"   , "REGISTRY_UNKNOWN" }
79 };
80 
81 /* some generic defines */
82 #define IPV6CALC_PROTO_IPV4		4
83 #define IPV6CALC_PROTO_IPV6		6
84 
85 /* some generic macros */
86 
87 // pack value using xor/mask/shift
88 #define PACK_XMS(v, x, m, s) 	((((v) ^ (x)) & (m)) << (s))
89 
90 // unpack value using xor/mask/shift
91 #define UNPACK_XMS(p, x, m, s)	((((p) >> (s)) ^ (x)) & (m))
92 
93 // return 16-bit MSB from 32-bit
94 #define U32_MSB16(v)	((v >> 16) & 0xffff)
95 
96 // return 16-bit LSB from 32-bit
97 #define U32_LSB16(v)	(v & 0xffff)
98 
99 // max entries of a const array
100 #define MAXENTRIES_ARRAY(a)	(signed long) (sizeof(a) / sizeof(a[0]))
101 
102 /* non-quiet print with args */
103 #define NONQUIETPRINT_WA(t, ...)        if (ipv6calc_quiet == 0) { fprintf(stderr, t "\n", __VA_ARGS__); };
104 
105 /* non-quiet print no args */
106 #define NONQUIETPRINT_NA(t)             if (ipv6calc_quiet == 0) { fprintf(stderr, t "\n"); };
107 
108 
109 #define IPV6CALC_LIB_VERSION_CHECK_EXIT(version_numeric, version_string) \
110 	if (version_numeric != libipv6calc_lib_version_numeric()) { \
111 		ERRORPRINT_WA("Library version is not matching: has:%s required:%s", libipv6calc_lib_version_string(), version_string); \
112 		exit(1); \
113 	};
114 
115 #endif // _libipv6calc_h
116 
117 
118 /* prototypes */
119 extern int ipv6calc_quiet;
120 extern int ipv6calc_verbose;
121 
122 extern void string_to_upcase(char *string);
123 extern void string_to_lowcase(char *string);
124 
125 extern void string_to_reverse(char *string);
126 extern void string_to_reverse_dotted(char *string, const size_t string_length);
127 
128 extern uint32_t libipv6calc_autodetectinput(const char *string);
129 
130 extern int  libipv6calc_filter_parse(const char *expression, s_ipv6calc_filter_master *filter_master);
131 extern int  libipv6calc_filter_check(s_ipv6calc_filter_master *filter_master);
132 extern void libipv6calc_filter_clear(s_ipv6calc_filter_master *filter_master);
133 extern void libipv6calc_filter_clear_db_cc(s_ipv6calc_filter_db_cc *filter_db_cc);
134 extern void libipv6calc_filter_clear_db_asn(s_ipv6calc_filter_db_asn *filter_db_asn);
135 extern void libipv6calc_filter_clear_db_registry(s_ipv6calc_filter_db_registry *filter_db_registry);
136 
137 extern int   libipv6calc_anon_set_by_name(s_ipv6calc_anon_set *ipv6calc_anon_set, const char* name);
138 extern void  libipv6calc_anon_infostring(char* string, const int stringlength, const s_ipv6calc_anon_set *ipv6calc_anon_set);
139 extern const char *libipv6calc_anon_method_name(const s_ipv6calc_anon_set *ipv6calc_anon_set);
140 extern int   libipv6calc_anon_supported(const s_ipv6calc_anon_set *ipv6calc_anon_set);
141 
142 extern const char *libipv6calc_registry_string_by_num(const int registry);
143 
144 extern int libipv6calc_bitcount_uint32_t(const uint32_t value);
145 
146 extern       uint32_t libipv6calc_lib_version_numeric(void);
147 extern const char    *libipv6calc_lib_version_string(void);
148 extern       uint32_t libipv6calc_api_version_numeric(void);
149 extern const char    *libipv6calc_api_version_string(void);
150