1 /*
2  * Copyright (c) 2009 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 /**
28  *
29  * Utility tools.
30  */
31 
32 #ifndef UTIL_UTIL_H
33 #define UTIL_UTIL_H
34 
35 #include "config.h"
36 #include "status.h"
37 
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 
45 #include <ldns/ldns.h>
46 
47 #define SE_SOA_RDATA_SERIAL  2
48 #define SE_SOA_RDATA_EXPIRE 5
49 #define SE_SOA_RDATA_MINIMUM 6
50 
51 /* copycode: This define is taken from BIND9 */
52 #define DNS_SERIAL_GT(a, b) ((int)(((a) - (b)) & 0xFFFFFFFF) > 0)
53 
54 /**
55  * Check if a RR is a DNSSEC RR (RRSIG, NSEC, NSEC3 or NSEC3PARAMS).
56  * \param[in] rr RR
57  * \return int 1 on true, 0 on false
58  *
59  */
60 int util_is_dnssec_rr(ldns_rr* rr);
61 
62 /**
63  * Compare SERIALs.
64  * \param serial_new new SERIAL value
65  * \param serial_old old SERIAL value
66  * \return int 0 if the new SERIAL <= old SERIAL, non-zero otherwise
67  *
68  */
69 int util_serial_gt(uint32_t serial_new, uint32_t serial_old);
70 
71 /**
72  * Compare RRs only on RDATA.
73  * \param[in] rr1 RR
74  * \param[in] rr2 another RR
75  * \param[out] cmp compare value
76  * \return status compare status
77  *
78  */
79 ldns_status util_dnssec_rrs_compare(ldns_rr* rr1, ldns_rr* rr2, int* cmp);
80 
81 /**
82  * Check process id file.
83  * \param[in] pidfile pid filename
84  * \return int status (0 if process id in pidfile is running)
85  *
86  */
87 int util_check_pidfile(const char* pidfile);
88 
89 /**
90  * Write process id to file.
91  * \param[in] pidfile pid filename
92  * \param[in] pid process id
93  * \return int status
94  *
95  */
96 int util_write_pidfile(const char* pidfile, pid_t pid);
97 
98 /**
99  * Print an LDNS RR, check status.
100  * \param[in] fd file descriptor
101  * \param[in] rr RR
102  * \return ods_status status
103  *
104  */
105 ods_status util_rr_print(FILE* fd, const ldns_rr* rr);
106 
107 /**
108  * Calculates the size needed to store the result of b64_pton.
109  * \param[in] len strlen
110  * \return size of b64_pton
111  *
112  */
113 size_t util_b64_pton_calculate_size(size_t srcsize);
114 
115 /**
116  * Check pidfile
117  *
118  * Try to read PID file to see if an other instance is already running.
119  * If pidfile not found or process is not running exit success. Note:
120  * upon failures reading the file
121  *
122  * \param pidfile: file to check.
123  * \return 1 pidfile does not exist or process not running. 0 otherwise.
124  */
125 int util_pidfile_avail(const char* pidfile);
126 
127 /**
128  * Clamp an integer value between a lower and an upper bound.
129  *
130  * In effect a combination of a min() and max() call this function
131  * will return the value as long as it lies between the lower and
132  * upper bound.  If smaller (or equal) to the lower bound it will
133  * return the lower bound and likewise if larger or equal to the
134  * upper, the upper bound.  The result may be either lower or
135  * upper bound if the upper bound is smaller than the lower bound.
136  */
137 int clamp(int value, int lbnd, int ubnd);
138 
139 
140 #endif /* UTIL_UTIL_H */
141