]>
git.ipfire.org Git - location/libloc.git/blob - src/libloc/private.h
3b8782a1f592708c20f08199ba75cdc91221a8af
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
17 #ifndef LIBLOC_PRIVATE_H
18 #define LIBLOC_PRIVATE_H
22 #include <netinet/in.h>
27 #include <libloc/libloc.h>
29 static inline void __attribute__((always_inline
, format(printf
, 2, 3)))
30 loc_log_null(struct loc_ctx
*ctx
, const char *format
, ...) {}
32 #define loc_log_cond(ctx, prio, arg...) \
34 if (loc_get_log_priority(ctx) >= prio) \
35 loc_log(ctx, prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \
39 # define DEBUG(ctx, arg...) loc_log_cond(ctx, LOG_DEBUG, ## arg)
41 # define DEBUG(ctx, arg...) loc_log_null(ctx, ## arg)
44 #define INFO(ctx, arg...) loc_log_cond(ctx, LOG_INFO, ## arg)
45 #define ERROR(ctx, arg...) loc_log_cond(ctx, LOG_ERR, ## arg)
47 #ifndef HAVE_SECURE_GETENV
48 # ifdef HAVE___SECURE_GETENV
49 # define secure_getenv __secure_getenv
51 # define secure_getenv getenv
55 #define LOC_EXPORT __attribute__ ((visibility("default")))
57 void loc_log(struct loc_ctx
*ctx
,
58 int priority
, const char *file
, int line
, const char *fn
,
59 const char *format
, ...) __attribute__((format(printf
, 6, 7)));
61 static inline int in6_addr_cmp(const struct in6_addr
* a1
, const struct in6_addr
* a2
) {
62 for (unsigned int i
= 0; i
< 16; i
++) {
63 if (a1
->s6_addr
[i
] > a2
->s6_addr
[i
])
66 else if (a1
->s6_addr
[i
] < a2
->s6_addr
[i
])
73 static inline int in6_addr_get_bit(const struct in6_addr
* address
, unsigned int i
) {
74 return ((address
->s6_addr
[i
/ 8] >> (7 - (i
% 8))) & 1);
77 static inline void in6_addr_set_bit(struct in6_addr
* address
, unsigned int i
, unsigned int val
) {
78 address
->s6_addr
[i
/ 8] ^= (-val
^ address
->s6_addr
[i
/ 8]) & (1 << (7 - (i
% 8)));
81 static inline struct in6_addr
loc_prefix_to_bitmask(const unsigned int prefix
) {
82 struct in6_addr bitmask
;
84 for (unsigned int i
= 0; i
< 16; i
++)
85 bitmask
.s6_addr
[i
] = 0;
87 for (int i
= prefix
, j
= 0; i
> 0; i
-= 8, j
++) {
89 bitmask
.s6_addr
[j
] = 0xff;
91 bitmask
.s6_addr
[j
] = 0xff << (8 - i
);
97 static inline unsigned int loc_address_bit_length(const struct in6_addr
* address
) {
98 unsigned int length
= 128;
100 for (int octet
= 0; octet
<= 15; octet
++) {
101 if (address
->s6_addr
[octet
]) {
102 length
-= __builtin_clz(address
->s6_addr
[octet
]) - 24;
111 static inline struct in6_addr
loc_address_and(
112 const struct in6_addr
* address
, const struct in6_addr
* bitmask
) {
115 // Perform bitwise AND
116 for (unsigned int i
= 0; i
< 4; i
++)
117 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] & bitmask
->s6_addr32
[i
];
122 static inline struct in6_addr
loc_address_or(
123 const struct in6_addr
* address
, const struct in6_addr
* bitmask
) {
126 // Perform bitwise OR
127 for (unsigned int i
= 0; i
< 4; i
++)
128 a
.s6_addr32
[i
] = address
->s6_addr32
[i
] | ~bitmask
->s6_addr32
[i
];
133 static inline struct in6_addr
loc_address_sub(
134 const struct in6_addr
* address1
, const struct in6_addr
* address2
) {
139 for (int octet
= 15; octet
>= 0; octet
--) {
140 int x
= address1
->s6_addr
[octet
] - address2
->s6_addr
[octet
] + remainder
;
142 // Store remainder for the next iteration
143 remainder
= (x
>> 8);
145 a
.s6_addr
[octet
] = x
& 0xff;
151 static inline void hexdump(struct loc_ctx
* ctx
, const void* addr
, size_t len
) {
152 char buffer_hex
[16 * 3 + 6];
153 char buffer_ascii
[17];
156 unsigned char* p
= (unsigned char*)addr
;
158 DEBUG(ctx
, "Dumping %zu byte(s)\n", len
);
160 // Process every byte in the data
161 for (i
= 0; i
< len
; i
++) {
162 // Multiple of 16 means new line (with line offset)
164 // Just don't print ASCII for the zeroth line
166 DEBUG(ctx
, " %s %s\n", buffer_hex
, buffer_ascii
);
168 // Output the offset.
169 sprintf(buffer_hex
, "%04x ", i
);
172 // Now the hex code for the specific character
173 sprintf(buffer_hex
+ 5 + ((i
% 16) * 3), " %02x", p
[i
]);
175 // And store a printable ASCII character for later
176 if ((p
[i
] < 0x20) || (p
[i
] > 0x7e))
177 buffer_ascii
[i
% 16] = '.';
179 buffer_ascii
[i
% 16] = p
[i
];
182 buffer_ascii
[(i
% 16) + 1] = '\0';
185 // Pad out last line if not exactly 16 characters
186 while ((i
% 16) != 0) {
187 sprintf(buffer_hex
+ 5 + ((i
% 16) * 3), " ");
191 // And print the final bit
192 DEBUG(ctx
, " %s %s\n", buffer_hex
, buffer_ascii
);