]> git.ipfire.org Git - people/ms/libloc.git/blame - src/loc/private.h
Refactor compating IP addresses
[people/ms/libloc.git] / src / loc / private.h
CommitLineData
46aded9a
MT
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
1a3cb1b4
MT
20#ifdef LIBLOC_PRIVATE
21
2a30e4de 22#include <netinet/in.h>
46aded9a
MT
23#include <stdbool.h>
24#include <syslog.h>
25
26#include <loc/libloc.h>
27
28static inline void __attribute__((always_inline, format(printf, 2, 3)))
29loc_log_null(struct loc_ctx *ctx, const char *format, ...) {}
30
31#define loc_log_cond(ctx, prio, arg...) \
32 do { \
33 if (loc_get_log_priority(ctx) >= prio) \
34 loc_log(ctx, prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \
35 } while (0)
36
37#ifdef ENABLE_DEBUG
38# define DEBUG(ctx, arg...) loc_log_cond(ctx, LOG_DEBUG, ## arg)
39#else
40# define DEBUG(ctx, arg...) loc_log_null(ctx, ## arg)
41#endif
42
43#define INFO(ctx, arg...) loc_log_cond(ctx, LOG_INFO, ## arg)
44#define ERROR(ctx, arg...) loc_log_cond(ctx, LOG_ERR, ## arg)
45
46#ifndef HAVE_SECURE_GETENV
47# ifdef HAVE___SECURE_GETENV
48# define secure_getenv __secure_getenv
49# else
50# error neither secure_getenv nor __secure_getenv is available
51# endif
52#endif
53
54#define LOC_EXPORT __attribute__ ((visibility("default")))
55
56void loc_log(struct loc_ctx *ctx,
57 int priority, const char *file, int line, const char *fn,
58 const char *format, ...) __attribute__((format(printf, 6, 7)));
59
2a30e4de 60static inline int in6_addr_cmp(const struct in6_addr* a1, const struct in6_addr* a2) {
e14564aa
MT
61 for (unsigned int i = 0; i < 16; i++) {
62 if (a1->s6_addr[i] > a2->s6_addr[i])
63 return 1;
64
65 else if (a1->s6_addr[i] < a2->s6_addr[i])
66 return -1;
67 }
68
69 return 0;
2a30e4de
MT
70}
71
72static inline int in6_addr_get_bit(const struct in6_addr* address, unsigned int i) {
73 return ((address->s6_addr[i / 8] >> (i % 8)) & 1);
74}
75
76static inline void in6_addr_set_bit(struct in6_addr* address, unsigned int i, unsigned int val) {
77 address->s6_addr[i / 8] ^= (-val ^ address->s6_addr[i / 8]) & (1 << (i % 8));
78}
79
46aded9a 80#endif
1a3cb1b4 81#endif