]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2017 IPFire Development Team <info@ipfire.org> | |
5 | ||
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. | |
10 | ||
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. | |
15 | */ | |
16 | ||
17 | #ifndef LIBLOC_PRIVATE_H | |
18 | #define LIBLOC_PRIVATE_H | |
19 | ||
20 | #ifdef LIBLOC_PRIVATE | |
21 | ||
22 | #include <netinet/in.h> | |
23 | #include <stdbool.h> | |
24 | #include <stdio.h> | |
25 | #include <syslog.h> | |
26 | ||
27 | #include <loc/libloc.h> | |
28 | ||
29 | static inline void __attribute__((always_inline, format(printf, 2, 3))) | |
30 | loc_log_null(struct loc_ctx *ctx, const char *format, ...) {} | |
31 | ||
32 | #define loc_log_cond(ctx, prio, arg...) \ | |
33 | do { \ | |
34 | if (loc_get_log_priority(ctx) >= prio) \ | |
35 | loc_log(ctx, prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \ | |
36 | } while (0) | |
37 | ||
38 | #ifdef ENABLE_DEBUG | |
39 | # define DEBUG(ctx, arg...) loc_log_cond(ctx, LOG_DEBUG, ## arg) | |
40 | #else | |
41 | # define DEBUG(ctx, arg...) loc_log_null(ctx, ## arg) | |
42 | #endif | |
43 | ||
44 | #define INFO(ctx, arg...) loc_log_cond(ctx, LOG_INFO, ## arg) | |
45 | #define ERROR(ctx, arg...) loc_log_cond(ctx, LOG_ERR, ## arg) | |
46 | ||
47 | #ifndef HAVE_SECURE_GETENV | |
48 | # ifdef HAVE___SECURE_GETENV | |
49 | # define secure_getenv __secure_getenv | |
50 | # else | |
51 | # define secure_getenv getenv | |
52 | # endif | |
53 | #endif | |
54 | ||
55 | #define LOC_EXPORT __attribute__ ((visibility("default"))) | |
56 | ||
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))); | |
60 | ||
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]) | |
64 | return 1; | |
65 | ||
66 | else if (a1->s6_addr[i] < a2->s6_addr[i]) | |
67 | return -1; | |
68 | } | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
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); | |
75 | } | |
76 | ||
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))); | |
79 | } | |
80 | ||
81 | static inline void hexdump(struct loc_ctx* ctx, const void* addr, size_t len) { | |
82 | char buffer_hex[16 * 3 + 6]; | |
83 | char buffer_ascii[17]; | |
84 | ||
85 | unsigned int i = 0; | |
86 | unsigned char* p = (unsigned char*)addr; | |
87 | ||
88 | DEBUG(ctx, "Dumping %zu byte(s)\n", len); | |
89 | ||
90 | // Process every byte in the data | |
91 | for (i = 0; i < len; i++) { | |
92 | // Multiple of 16 means new line (with line offset) | |
93 | if ((i % 16) == 0) { | |
94 | // Just don't print ASCII for the zeroth line | |
95 | if (i != 0) | |
96 | DEBUG(ctx, " %s %s\n", buffer_hex, buffer_ascii); | |
97 | ||
98 | // Output the offset. | |
99 | sprintf(buffer_hex, "%04x ", i); | |
100 | } | |
101 | ||
102 | // Now the hex code for the specific character | |
103 | sprintf(buffer_hex + 5 + ((i % 16) * 3), " %02x", p[i]); | |
104 | ||
105 | // And store a printable ASCII character for later | |
106 | if ((p[i] < 0x20) || (p[i] > 0x7e)) | |
107 | buffer_ascii[i % 16] = '.'; | |
108 | else | |
109 | buffer_ascii[i % 16] = p[i]; | |
110 | ||
111 | // Terminate string | |
112 | buffer_ascii[(i % 16) + 1] = '\0'; | |
113 | } | |
114 | ||
115 | // Pad out last line if not exactly 16 characters | |
116 | while ((i % 16) != 0) { | |
117 | sprintf(buffer_hex + 5 + ((i % 16) * 3), " "); | |
118 | i++; | |
119 | } | |
120 | ||
121 | // And print the final bit | |
122 | DEBUG(ctx, " %s %s\n", buffer_hex, buffer_ascii); | |
123 | } | |
124 | ||
125 | #endif | |
126 | #endif |