defines.h 3.56 KB
Newer Older
1
/*  Copyright (C) 2014-2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2

Marek Vavruša's avatar
Marek Vavruša committed
3 4 5 6
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
7

Marek Vavruša's avatar
Marek Vavruša committed
8 9 10 11
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
12

Marek Vavruša's avatar
Marek Vavruša committed
13
    You should have received a copy of the GNU General Public License
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
Marek Vavruša's avatar
Marek Vavruša committed
15
 */
16 17 18

#pragma once

19
#include <errno.h>
Marek Vavruša's avatar
Marek Vavruša committed
20 21 22 23
#include <libknot/errcode.h>
#include <libknot/dname.h>
#include <libknot/rrset.h>

24 25 26 27 28
/* Function attributes */
#if __GNUC__ >= 4
#define KR_EXPORT __attribute__ ((visibility ("default")))
#define KR_CONST __attribute__((__const__))
#define KR_PURE __attribute__((__pure__))
29
#define KR_NORETURN __attribute__((__noreturn__))
30
#define KR_COLD __attribute__((__cold__))
31
#define KR_PRINTF(n) __attribute__((format (printf, n, (n+1))))
32 33 34 35
#else
#define KR_EXPORT
#define KR_CONST
#define KR_PURE
36
#define KR_NORETURN
37
#define KR_COLD
38
#define KR_PRINTF(n)
39 40
#endif

41 42 43 44 45
#ifndef uint /* Redefining typedef is a C11 feature. */
typedef unsigned int uint;
#define uint uint
#endif

46 47 48 49
/*
 * Error codes.
 */
#define kr_ok() 0
50
/* Mark as cold to mark all branches as unlikely. */
51
static inline int KR_COLD kr_error(int x) {
52
    return x <= 0 ? x : -x;
53
}
54 55
#define kr_strerror(x) strerror(abs(x))

56 57
/*
 * Connection limits.
58
 * @cond internal
59
 */
Vladimír Čunát's avatar
Vladimír Čunát committed
60 61
#define KR_CONN_RTT_MAX 2000 /* Timeout for network activity */
#define KR_CONN_RETRY 200    /* Retry interval for network activity */
62
#define KR_ITER_LIMIT 100    /* Built-in iterator limit */
63
#define KR_RESOLVE_TIME_LIMIT 10000 /* Upper limit for resolution time of single query, ms */
64
#define KR_CNAME_CHAIN_LIMIT 40 /* Built-in maximum CNAME chain length */
65
#define KR_TIMEOUT_LIMIT 4   /* Maximum number of retries after timeout. */
66
#define KR_QUERY_NSRETRY_LIMIT 4 /* Maximum number of retries per query. */
67

Marek Vavruša's avatar
Marek Vavruša committed
68 69 70 71
/*
 * Defines.
 */
#define KR_DNS_PORT   53
72
#define KR_DNS_TLS_PORT 853
73
#define KR_EDNS_VERSION 0
74
#define KR_EDNS_PAYLOAD 4096 /* Default UDP payload (max unfragmented UDP is 1452B) */
Vladimír Čunát's avatar
Vladimír Čunát committed
75 76
#define KR_CACHE_DEFAULT_TTL_MIN (5) /* avoid bursts of queries */
#define KR_CACHE_DEFAULT_TTL_MAX (6 * 24 * 3600) /* 6 days, like the root NS TTL */
77

78 79 80
#define KR_DNAME_STR_MAXLEN (KNOT_DNAME_TXT_MAXLEN + 1)
#define KR_RRTYPE_STR_MAXLEN (16 + 1)

81 82 83 84 85 86 87 88 89 90 91 92 93
/*
 * Address sanitizer hints.
 */
#if !defined(__SANITIZE_ADDRESS__) && defined(__has_feature)
# if __has_feature(address_sanitizer)
#  define __SANITIZE_ADDRESS__ 1
# endif
#endif
#if defined(__SANITIZE_ADDRESS__)
void __asan_poison_memory_region(void const volatile *addr, size_t size);
void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#define kr_asan_poison(addr, size) __asan_poison_memory_region((addr), (size))
#define kr_asan_unpoison(addr, size) __asan_unpoison_memory_region((addr), (size))
94 95
#define kr_asan_custom_poison(fn, addr) fn ##_poison((addr))
#define kr_asan_custom_unpoison(fn, addr) fn ##_unpoison((addr))
96 97 98
#else
#define kr_asan_poison(addr, size)
#define kr_asan_unpoison(addr, size)
99 100
#define kr_asan_custom_poison(fn, addr)
#define kr_asan_custom_unpoison(fn, addr)
101
#endif
102 103 104 105 106 107

#if defined(__SANITIZE_ADDRESS__) && defined(_FORTIFY_SOURCE)
	#error "You can't use address sanitizer with _FORTIFY_SOURCE"
	// https://github.com/google/sanitizers/issues/247
#endif

108
/* @endcond */