All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Functions | Variables
misc.c File Reference
#include <freeradius-devel/libradius.h>
#include <ctype.h>
#include <sys/file.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <sys/uio.h>
+ Include dependency graph for misc.c:

Go to the source code of this file.

Data Structures

Macros

#define FR_PUT_LE16(a, val)
 
#define NSEC   1000000000
 
#define USEC   1000000
 

Typedefs

typedef struct fr_talloc_link fr_talloc_link_t
 

Functions

static int _fr_disarm_talloc_ctx_free (bool **armed)
 
static int _fr_trigger_talloc_ctx_free (fr_talloc_link_t *trigger)
 
static int _quick_partition (void const *to_sort[], int min, int max, fr_cmp_t cmp)
 
int closefrom (int fd)
 
char * fr_abin2hex (TALLOC_CTX *ctx, uint8_t const *bin, size_t inlen)
 Convert binary data to a hex string. More...
 
size_t fr_bin2hex (char *hex, uint8_t const *bin, size_t inlen)
 Convert binary data to a hex string. More...
 
int fr_blocking (UNUSED int fd)
 
int fr_get_time (char const *date_str, time_t *date)
 Convert string in various formats to a time_t. More...
 
size_t fr_hex2bin (uint8_t *bin, size_t outlen, char const *hex, size_t inlen)
 Convert hex strings to binary data. More...
 
int fr_nonblock (UNUSED int fd)
 
int8_t fr_pointer_cmp (void const *a, void const *b)
 Compares two pointers. More...
 
void 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. More...
 
int fr_set_signal (int sig, sig_t func)
 Sets a signal handler using sigaction if available, else signal. More...
 
size_t fr_snprint_uint128 (char *out, size_t outlen, uint128_t const num)
 Write 128bit unsigned integer to buffer. More...
 
uint32_t fr_strtoul (char const *value, char **end)
 Consume the integer (or hex) portion of a value string. More...
 
int fr_talloc_link_ctx (TALLOC_CTX *parent, TALLOC_CTX *child)
 Link a parent and a child context, so the child is freed before the parent. More...
 
void fr_timespec_subtract (struct timespec *out, struct timespec const *end, struct timespec const *start)
 Subtract one timespec from another. More...
 
int fr_timeval_from_str (struct timeval *out, char const *in)
 Create timeval from a string. More...
 
void fr_timeval_subtract (struct timeval *out, struct timeval const *end, struct timeval const *start)
 Subtract one timeval from another. More...
 
ssize_t fr_utf8_to_ucs2 (uint8_t *out, size_t outlen, char const *in, size_t inlen)
 Convert UTF8 string to UCS2 encoding. More...
 
ssize_t fr_writev (int fd, struct iovec vector[], int iovcnt, struct timeval *timeout)
 Write out a vector to a file descriptor. More...
 
bool is_integer (char const *value)
 Check whether the string is all numbers. More...
 
bool is_printable (void const *value, size_t len)
 Check whether the string is made up of printable UTF8 chars. More...
 
bool is_whitespace (char const *value)
 Check whether the string is all whitespace. More...
 
bool is_zero (char const *value)
 Check whether the string is all zeros. More...
 
static char * mystrtok (char **ptr, char const *sep)
 
int rad_lockfd (int fd, int lock_len)
 
int rad_lockfd_nonblock (int fd, int lock_len)
 
int rad_unlockfd (int fd, int lock_len)
 

Variables

int fr_debug_lvl = 0
 
static char const hextab [] = "0123456789abcdef"
 
static char const * months []
 

Data Structure Documentation

struct fr_talloc_link

Definition at line 46 of file misc.c.

Data Fields
bool armed
TALLOC_CTX * child

Macro Definition Documentation

#define FR_PUT_LE16 (   a,
  val 
)
Value:
do {\
a[1] = ((uint16_t) (val)) >> 8;\
a[0] = ((uint16_t) (val)) & 0xff;\
} while (0)
#define val(x)
Definition: timestr.c:37

Definition at line 34 of file misc.c.

#define NSEC   1000000000

Definition at line 874 of file misc.c.

#define USEC   1000000

Definition at line 849 of file misc.c.

Typedef Documentation

Function Documentation

static int _fr_disarm_talloc_ctx_free ( bool **  armed)
static

Definition at line 86 of file misc.c.

+ Here is the caller graph for this function:

static int _fr_trigger_talloc_ctx_free ( fr_talloc_link_t trigger)
static

Definition at line 79 of file misc.c.

+ Here is the caller graph for this function:

static int _quick_partition ( void const *  to_sort[],
int  min,
int  max,
fr_cmp_t  cmp 
)
static

Definition at line 966 of file misc.c.

+ Here is the caller graph for this function:

int closefrom ( int  fd)

Definition at line 378 of file misc.c.

+ Here is the caller graph for this function:

char* fr_abin2hex ( TALLOC_CTX *  ctx,
uint8_t const *  bin,
size_t  inlen 
)

Convert binary data to a hex string.

Ascii encoded hex string will not be prefixed with '0x'

Parameters
[in]ctxto alloc buffer in.
[in]bininput.
[in]inlenof bin input.
Returns
length of data written to buffer.

Definition at line 278 of file misc.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t fr_bin2hex ( char *  hex,
uint8_t const *  bin,
size_t  inlen 
)

Convert binary data to a hex string.

Ascii encoded hex string will not be prefixed with '0x'

Warning
If the output buffer isn't long enough, we have a buffer overflow.
Parameters
[out]hexBuffer to write hex output.
[in]bininput.
[in]inlenof bin input.
Returns
length of data written to buffer.

Definition at line 254 of file misc.c.

+ Here is the caller graph for this function:

int fr_blocking ( UNUSED int  fd)

Definition at line 467 of file misc.c.

+ Here is the call graph for this function:

int fr_get_time ( char const *  date_str,
time_t *  date 
)

Convert string in various formats to a time_t.

Parameters
date_strinput date string.
datetime_t to write result to.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 716 of file misc.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t fr_hex2bin ( uint8_t *  bin,
size_t  outlen,
char const *  hex,
size_t  inlen 
)

Convert hex strings to binary data.

Parameters
binBuffer to write output to.
outlenlength of output buffer (or length of input string / 2).
hexinput string.
inlenlength of the input string
Returns
length of data written to buffer.

Definition at line 220 of file misc.c.

+ Here is the caller graph for this function:

int fr_nonblock ( UNUSED int  fd)

Definition at line 462 of file misc.c.

+ Here is the call graph for this function:

int8_t fr_pointer_cmp ( void const *  a,
void const *  b 
)

Compares two pointers.

Parameters
afirst pointer to compare.
bsecond pointer to compare.
Returns
  • -1 if a < b.
  • +1 if b > a.
  • 0 if both equal.

Definition at line 958 of file misc.c.

+ Here is the caller graph for this function:

void 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.

Parameters
to_sortarray of pointers to sort.
min_idxthe lowest index (usually 0).
max_idxthe highest index (usually length of array - 1).
cmpthe comparison function to use to sort the array elements.

Definition at line 997 of file misc.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_set_signal ( int  sig,
sig_t  func 
)

Sets a signal handler using sigaction if available, else signal.

Parameters
sigto set handler for.
funchandler to set.

Definition at line 56 of file misc.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t fr_snprint_uint128 ( char *  out,
size_t  outlen,
uint128_t const  num 
)

Write 128bit unsigned integer to buffer.

Author
Alexey Frunze
Parameters
outwhere to write result to.
outlensize of out.
num128 bit integer.

Definition at line 633 of file misc.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

uint32_t fr_strtoul ( char const *  value,
char **  end 
)

Consume the integer (or hex) portion of a value string.

Parameters
valuestring to parse.
endpointer to the first non numeric char.
Returns
integer value.

Definition at line 296 of file misc.c.

+ Here is the caller graph for this function:

int fr_talloc_link_ctx ( TALLOC_CTX *  parent,
TALLOC_CTX *  child 
)

Link a parent and a child context, so the child is freed before the parent.

Note
This is not thread safe. Do not free parent before threads are joined, do not call from a child thread.
It's OK to free the child before threads are joined, but this will leak memory until the parent is freed.
Parameters
parentwho's fate the child should share.
childbound to parent's lifecycle.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 105 of file misc.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void fr_timespec_subtract ( struct timespec *  out,
struct timespec const *  end,
struct timespec const *  start 
)

Subtract one timespec from another.

Parameters
[out]outWhere to write difference.
[in]endTime closest to the present.
[in]startTime furthest in the past.

Definition at line 881 of file misc.c.

int fr_timeval_from_str ( struct timeval *  out,
char const *  in 
)

Create timeval from a string.

Parameters
[out]outWhere to write timeval.
[in]inString to parse.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 907 of file misc.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void fr_timeval_subtract ( struct timeval *  out,
struct timeval const *  end,
struct timeval const *  start 
)

Subtract one timeval from another.

Parameters
[out]outWhere to write difference.
[in]endTime closest to the present.
[in]startTime furthest in the past.

Definition at line 856 of file misc.c.

+ Here is the caller graph for this function:

ssize_t fr_utf8_to_ucs2 ( uint8_t *  out,
size_t  outlen,
char const *  in,
size_t  inlen 
)

Convert UTF8 string to UCS2 encoding.

Note
Borrowed from src/crypto/ms_funcs.c of wpa_supplicant project (http://hostap.epitest.fi/wpa_supplicant/)
Parameters
[out]outWhere to write the ucs2 string.
[in]outlenSize of output buffer.
[in]inUTF8 string to convert.
[in]inlenlength of UTF8 string.
Returns
the size of the UCS2 string written to the output buffer (in bytes).

Definition at line 580 of file misc.c.

+ Here is the caller graph for this function:

ssize_t fr_writev ( int  fd,
struct iovec  vector[],
int  iovcnt,
struct timeval *  timeout 
)

Write out a vector to a file descriptor.

Wraps writev, calling it as necessary. If timeout is not NULL, timeout is applied to each call that returns EAGAIN or EWOULDBLOCK

Note
Should only be used on nonblocking file descriptors.
Socket should likely be closed on timeout.
iovec may be modified in such a way that it's not re-usable.
Leaves errno set to the last error that occurred.
Parameters
fdto write to.
vectorto write.
iovcntnumber of elements in iovec.
timeouthow long to wait for fd to become writeable before timing out.
Returns
  • Number of bytes written.
  • -1 on failure.

Definition at line 492 of file misc.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool is_integer ( char const *  value)

Check whether the string is all numbers.

Returns
  • true if the entirety of the string is number chars.
  • false if string contains no number chars.

Definition at line 350 of file misc.c.

+ Here is the caller graph for this function:

bool is_printable ( void const *  value,
size_t  len 
)

Check whether the string is made up of printable UTF8 chars.

Parameters
valueto check.
lenof value.
Returns
  • true if the string is printable.
  • false if the string contains non printable chars

Definition at line 329 of file misc.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool is_whitespace ( char const *  value)

Check whether the string is all whitespace.

Returns
  • true if the entirety of the string is whitespace.
  • false if the string contains non whitespace.

Definition at line 311 of file misc.c.

+ Here is the caller graph for this function:

bool is_zero ( char const *  value)

Check whether the string is all zeros.

Returns
  • true if the entirety of the string is all zeros.
  • false if string contains no zeros.

Definition at line 365 of file misc.c.

static char* mystrtok ( char **  ptr,
char const *  sep 
)
static

Definition at line 682 of file misc.c.

+ Here is the caller graph for this function:

int rad_lockfd ( int  fd,
int  lock_len 
)

Definition at line 141 of file misc.c.

+ Here is the caller graph for this function:

int rad_lockfd_nonblock ( int  fd,
int  lock_len 
)

Definition at line 166 of file misc.c.

+ Here is the caller graph for this function:

int rad_unlockfd ( int  fd,
int  lock_len 
)

Definition at line 191 of file misc.c.

+ Here is the caller graph for this function:

Variable Documentation

int fr_debug_lvl = 0

Definition at line 40 of file misc.c.

char const hextab[] = "0123456789abcdef"
static

Definition at line 210 of file misc.c.

char const* months[]
static
Initial value:
= {
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec" }

Definition at line 42 of file misc.c.