The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
misc.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/** Various miscellaneous utility functions
19 *
20 * @file src/lib/util/misc.h
21 *
22 * @copyright 2000,2006 The FreeRADIUS server project
23 */
24RCSIDH(misc_h, "$Id: d2e9a86a91be24694369706337825464c52551e4 $")
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#include <freeradius-devel/build.h>
31#include <freeradius-devel/missing.h>
32#include <freeradius-devel/util/debug.h>
33#include <freeradius-devel/util/print.h>
34#include <freeradius-devel/util/strerror.h>
35#include <freeradius-devel/util/talloc.h>
36#include <freeradius-devel/util/time.h>
37
38#include <signal.h>
39
40typedef fr_cmp_ret_t (*fr_cmp_t)(void const *a, void const *b);
41typedef void (*fr_free_t)(void *);
42
43/*
44 * Define TALLOC_DEBUG to check overflows with talloc.
45 * we can't use valgrind, because the memory used by
46 * talloc is valid memory... just not for us.
47 */
48#ifdef TALLOC_DEBUG
49void fr_talloc_verify_cb(const void *ptr, int depth,
50 int max_depth, int is_ref,
51 void *private_data);
52# define VERIFY_ALL_TALLOC talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL)
53#else
54# define VERIFY_ALL_TALLOC
55#endif
56
57/** Zero out any whitespace with nul bytes
58 *
59 * @param[in,out] _p string to process
60 */
61#define fr_zero_whitespace(_p) while (isspace((uint8_t) *_p)) *(_p++) = '\0'
62
63/** Check whether the string is all whitespace
64 *
65 * @return
66 * - true if the entirety of the string is whitespace.
67 * - false if the string contains non whitespace.
68 */
69static inline bool is_whitespace(char const *value)
70{
71#ifdef STATIC_ANALYZER
72 if (*value == '\0') return false; /* clang analyzer doesn't seem to know what isspace does */
73#endif
74 do {
75 if (!isspace((uint8_t) *value)) return false;
76 } while (*++value);
77
78 return true;
79}
80
81/** Check whether the string is made up of printable UTF8 chars
82 *
83 * @param value to check.
84 * @param len of value.
85 *
86 * @return
87 * - true if the string is printable.
88 * - false if the string contains non printable chars
89 */
90 static inline bool is_printable(void const *value, size_t len)
91 {
92 uint8_t const *p = value;
93 size_t clen;
94 size_t i;
95
96 for (i = 0; i < len; i++) {
97 clen = fr_utf8_char(p, len - i);
98 if (clen == 0) return false;
99 i += (size_t)clen;
100 p += clen;
101 }
102 return true;
103 }
104
105/** Check whether the string is all numbers
106 *
107 * @return
108 * - true if the entirety of the string is number chars.
109 * - false if string contains non-numeric chars or is empty.
110 */
111static inline bool is_integer(char const *value)
112{
113#ifdef STATIC_ANALYZER
114 if (*value == '\0') return false; /* clang analyzer doesn't seem to know what isdigit does */
115#endif
116 do {
117 if (!isdigit((uint8_t) *value)) return false;
118 } while (*++value);
119
120 return true;
121}
122
123/** Check whether the string is all zeros
124 *
125 * @return
126 * - true if the entirety of the string is all zeros.
127 * - false if string contains non-zero chars or is empty.
128 */
129static inline bool is_zero(char const *value)
130{
131 do {
132 if (*value != '0') return false;
133 } while (*++value);
134
135 return true;
136}
137
138int fr_set_signal(int sig, sig_t func);
139int fr_unset_signal(int sig);
140int rad_lockfd(int fd, int lock_len);
141int rad_lockfd_nonblock(int fd, int lock_len);
142int rad_unlockfd(int fd, int lock_len);
143int fr_strtoull(uint64_t *out, char **end, char const *value);
144int fr_strtoll(int64_t *out, char **end, char const *value);
145char *fr_trim(char const *str, size_t size);
146char *fr_tolower(char *str);
147
148int fr_nonblock(int fd);
149int fr_blocking(int fd);
150int fr_cloexec(int fd);
151
152ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen);
153size_t fr_snprint_uint128(char *out, size_t outlen, uint128_t const num);
154
155fr_cmp_ret_t fr_pointer_cmp(void const *a, void const *b);
156int fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp);
157int fr_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length) CC_HINT(nonnull);
158
159/*
160 * Some libraries need to call suid up/down, except that those are functions in the server, and we don't
161 * want to link everything to the server library. As a result, we include trampoline functions which do
162 * nothing, but which can be over-written by the server when is starts.
163 */
164typedef void (*fr_suid_t)(void);
165
166void fr_suid_noop(void);
167extern fr_suid_t fr_suid_up;
169
170#ifdef __cplusplus
171}
172#endif
#define RCSIDH(h, id)
Definition build.h:604
fr_cmp_ret_t
Result of an ordering comparison.
Definition build.h:113
Test enumeration values.
Definition dict_test.h:92
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
int fr_unset_signal(int sig)
Uninstall a signal for a specific handler.
Definition misc.c:76
int fr_strtoull(uint64_t *out, char **end, char const *value)
Consume the integer (or hex) portion of a value string.
Definition misc.c:160
fr_cmp_ret_t fr_pointer_cmp(void const *a, void const *b)
Compares two pointers.
Definition misc.c:449
int fr_set_signal(int sig, sig_t func)
Sets a signal handler using sigaction if available, else signal.
Definition misc.c:47
int fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp)
Quick sort an array of pointers using a comparator.
Definition misc.c:495
int fr_cloexec(int fd)
int rad_unlockfd(int fd, int lock_len)
Definition misc.c:141
static bool is_printable(void const *value, size_t len)
Check whether the string is made up of printable UTF8 chars.
Definition misc.h:90
void fr_suid_noop(void)
Definition misc.c:529
static bool is_whitespace(char const *value)
Check whether the string is all whitespace.
Definition misc.h:69
ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen)
Convert UTF8 string to UCS2 encoding.
Definition misc.c:347
int fr_blocking(int fd)
int fr_strtoll(int64_t *out, char **end, char const *value)
Consume the integer (or hex) portion of a value string.
Definition misc.c:191
char * fr_trim(char const *str, size_t size)
Trim whitespace from the end of a string.
Definition misc.c:213
static bool is_integer(char const *value)
Check whether the string is all numbers.
Definition misc.h:111
fr_suid_t fr_suid_up
Definition misc.c:533
void(* fr_suid_t)(void)
Definition misc.h:164
int fr_nonblock(int fd)
char * fr_tolower(char *str)
Definition misc.c:225
int rad_lockfd(int fd, int lock_len)
Definition misc.c:119
int rad_lockfd_nonblock(int fd, int lock_len)
Definition misc.c:129
fr_suid_t fr_suid_down
Definition misc.c:534
size_t fr_snprint_uint128(char *out, size_t outlen, uint128_t const num)
Write 128bit unsigned integer to buffer.
Definition misc.c:401
void(* fr_free_t)(void *)
Definition misc.h:41
fr_cmp_ret_t(* fr_cmp_t)(void const *a, void const *b)
Definition misc.h:40
int fr_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length)
Do a comparison of two authentication digests by comparing the FULL data.
Definition misc.c:516
static bool is_zero(char const *value)
Check whether the string is all zeros.
Definition misc.h:129
size_t fr_utf8_char(uint8_t const *str, ssize_t inlen)
Checks for utf-8, taken from http://www.w3.org/International/questions/qa-forms-utf-8.
Definition print.c:39
Signals that can be sent to a request.
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
static fr_sbuff_err_t char ** out
Definition value.h:1061
static fr_sbuff_err_t char size_t * len
Definition value.h:1061
int nonnull(2, 5))