DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Micelaneous utilities.

Utilities commonly used. More...

Modules

 Hashing and digest functions
 MD5/SHA1/SHA2(256/512) Hashing checking and HMAC Functions.
 

Files

file  util.c
 Utilities commonly used.
 

Functions

void seedrand (void)
 Seed openssl random number generator. More...
 
int genrand (void *buf, int len)
 Generate random sequence. More...
 
int strlenzero (const char *str)
 Check if a string is zero length. More...
 
char * ltrim (char *str)
 Trim white space at the begining of a string. More...
 
char * rtrim (const char *str)
 Trim white space at the end of a string. More...
 
char * trim (const char *str)
 Trim whitesapce from the beggining and end of a string. More...
 
uint64_t tvtontp64 (struct timeval *tv)
 Convert a timeval struct to 64bit NTP time. More...
 
uint16_t checksum (const void *data, int len)
 Obtain the checksum for a buffer. More...
 
uint16_t checksum_add (const uint16_t checksum, const void *data, int len)
 Obtain the checksum for a buffer adding a checksum. More...
 
uint16_t verifysum (const void *data, int len, const uint16_t check)
 Verify a checksum. More...
 
void touch (const char *filename, uid_t user, gid_t group)
 Create a file and set user and group. More...
 
char * b64enc_buf (const char *message, uint32_t len, int nonl)
 Base 64 encode a buffer. More...
 
char * b64enc (const char *message, int nonl)
 Base 64 encode a string. More...
 

Detailed Description

Utilities commonly used.

Function Documentation

char* b64enc ( const char *  message,
int  nonl 
)

Base 64 encode a string.

Parameters
messageString to encode.
nonlEncode the data all on one line if non zero.
Returns
Reference to base64 encoded string.

Definition at line 539 of file util.c.

References b64enc_buf().

539  {
540  return b64enc_buf(message, strlen(message), nonl);
541 }
char * b64enc_buf(const char *message, uint32_t len, int nonl)
Base 64 encode a buffer.
Definition: util.c:506
char* b64enc_buf ( const char *  message,
uint32_t  len,
int  nonl 
)

Base 64 encode a buffer.

Parameters
messageBuffer to encode.
lenLength of the buffer.
nonlEncode the data all on one line if non zero.
Returns
Reference to base64 encoded string.

Definition at line 506 of file util.c.

References objalloc().

Referenced by b64enc().

506  {
507  BIO *bmem, *b64;
508  BUF_MEM *ptr;
509  char *buffer;
510  double encodedSize;
511 
512  encodedSize = 1.36*len;
513  buffer = objalloc(encodedSize+1, NULL);
514 
515  b64 = BIO_new(BIO_f_base64());
516  bmem = BIO_new(BIO_s_mem());
517  b64 = BIO_push(b64, bmem);
518  if (nonl) {
519  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
520  }
521  BIO_write(b64, message, len);
522  BIO_flush(b64);
523  BIO_get_mem_ptr(b64, &ptr);
524 
525  buffer = objalloc(ptr->length+1, NULL);
526  memcpy(buffer, ptr->data, ptr->length);
527 
528 
529  BIO_free_all(b64);
530 
531  return buffer;
532 }
unsigned short len
Packet length.
Definition: radius.c:52
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
uint16_t checksum ( const void *  data,
int  len 
)

Obtain the checksum for a buffer.

Parameters
dataBuffer to create checksum of.
lenBuffer length.
Returns
Chechsum of data.

Definition at line 452 of file util.c.

Referenced by ipv4checksum(), ipv4icmpchecksum(), ipv4tcpchecksum(), ipv4udpchecksum(), and rfc6296_map_add().

452  {
453  return (_checksum(data, len, 0));
454 }
unsigned short len
Packet length.
Definition: radius.c:52
uint16_t checksum_add ( const uint16_t  checksum,
const void *  data,
int  len 
)

Obtain the checksum for a buffer adding a checksum.

Parameters
checksumChecksum to add to generated checksum.
dataBuffer to create checksum of.
lenBuffer length.
Returns
Chechsum of data.

Definition at line 463 of file util.c.

Referenced by ipv4tcpchecksum(), and ipv4udpchecksum().

463  {
464  return (_checksum(data, len, ~checksum));
465 }
uint16_t checksum(const void *data, int len)
Obtain the checksum for a buffer.
Definition: util.c:452
unsigned short len
Packet length.
Definition: radius.c:52
int genrand ( void *  buf,
int  len 
)

Generate random sequence.

Parameters
bufBuffer to write random data.
lenLength to write.
Returns
1 on success 0 otherwise.

Definition at line 82 of file util.c.

Referenced by mcast4_ip(), mcast6_ip(), new_radpacket(), randhwaddr(), and sslstartup().

82  {
83  return (RAND_bytes(buf, len));
84 }
unsigned short len
Packet length.
Definition: radius.c:52
char* ltrim ( char *  str)

Trim white space at the begining of a string.

Parameters
strString to trim.
Returns
Pointer to trimed string.

Definition at line 353 of file util.c.

References strlenzero().

Referenced by trim().

353  {
354  char *cur = str;
355 
356  if (strlenzero(str)) {
357  return (str);
358  }
359 
360  while(isspace(cur[0])) {
361  cur++;
362  }
363 
364  return (cur);
365 }
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
char* rtrim ( const char *  str)

Trim white space at the end of a string.

Parameters
strString to trim.
Returns
Pointer to trimed string.

Definition at line 372 of file util.c.

References strlenzero().

Referenced by trim().

372  {
373  int len;
374  char *cur = (char *)str;
375 
376  if (strlenzero(str)) {
377  return (cur);
378  }
379 
380  len = strlen(str) - 1;
381  while(len && isspace(cur[len])) {
382  cur[len] = '\0';
383  len--;
384  }
385 
386  return (cur);
387 }
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
unsigned short len
Packet length.
Definition: radius.c:52
void seedrand ( void  )

Seed openssl random number generator.

This should be run at application startup

Todo:
This wont work on WIN32

Definition at line 68 of file util.c.

Referenced by framework_init(), and mcast_socket().

68  {
69  int fd = open("/dev/random", O_RDONLY);
70  int len;
71  char buf[64];
72 
73  len = read(fd, buf, 64);
74  RAND_seed(buf, len);
75 }
unsigned short len
Packet length.
Definition: radius.c:52
int strlenzero ( const char *  str)

Check if a string is zero length.

strlen can not be used on a NULL string this is a quick and dirty util to check it.

Parameters
strString to check.
Returns
1 if the string is null or zero length

Definition at line 341 of file util.c.

Referenced by create_kernmac(), create_kernvlan(), get_category_next(), get_ifinfo(), get_ifipaddr(), ifhwaddr(), ltrim(), process_config(), rtrim(), and unixsocket_client().

341  {
342  if (str && strlen(str)) {
343  return (0);
344  }
345  return (1);
346 }
void touch ( const char *  filename,
uid_t  user,
gid_t  group 
)

Create a file and set user and group.

Todo:
WIN32 does not use uid/gid and move to file utils module.
Parameters
filenameFile to create.
userUser ID to set ownership.
groupGroup ID to set ownership.

Definition at line 484 of file util.c.

References touch().

Referenced by touch(), and xslt_apply().

484  {
485  int res;
486 #else
487 extern void touch(const char *filename) {
488 #endif
489  int fd;
490 
491  fd = creat(filename, 0600);
492  close(fd);
493 #ifndef __WIN32__
494  res = chown(filename, user, group);
495  res++;
496 #endif
497  return;
498 }
void touch(const char *filename, uid_t user, gid_t group)
Create a file and set user and group.
Definition: util.c:484
char* trim ( const char *  str)

Trim whitesapce from the beggining and end of a string.

Parameters
strString to trim.
Returns
Trimed string.

Definition at line 393 of file util.c.

References ltrim(), and rtrim().

Referenced by process_config().

393  {
394  char *cur = (char *)str;
395 
396  cur = ltrim(cur);
397  cur = rtrim(cur);
398  return (cur);
399 }
char * rtrim(const char *str)
Trim white space at the end of a string.
Definition: util.c:372
char * ltrim(char *str)
Trim white space at the begining of a string.
Definition: util.c:353
uint64_t tvtontp64 ( struct timeval *  tv)

Convert a timeval struct to 64bit NTP time.

Parameters
tvTimeval struct to convert.
Returns
64 bit NTP time value.

Definition at line 405 of file util.c.

Referenced by get_ip6_addrprefix().

405  {
406  return ((((uint64_t)tv->tv_sec + 2208988800u) << 32) + ((uint32_t)tv->tv_usec * 4294.967296));
407 }
uint16_t verifysum ( const void *  data,
int  len,
const uint16_t  check 
)

Verify a checksum.

Parameters
dataData to generate checksum.
lenLength of data.
checkChecksum to check against.
Returns
0 when checksum is verified.

Definition at line 473 of file util.c.

473  {
474  return (_checksum(data, len, check));
475 }
unsigned short len
Packet length.
Definition: radius.c:52