Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
utils.c File Reference
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <libgen.h>
#include <syslog.h>
#include <errno.h>
#include "config/config.h"
#include "config/conf.h"
#include "rlutil.h"
#include "utils.h"
#include "ev_mgr.h"
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>

Macros

#define ADDRSTRLEN   128
 

Functions

void teoLoggingClientSend (void *ke, const char *message)
 Send log data to logging servers. More...
 
unsigned char teoFilterFlagCheck (void *ke)
 
unsigned char teoLogCheck (void *ke, void *log)
 
void KSN_SET_TEST_MODE (int test_mode)
 
int KSN_GET_TEST_MODE ()
 
int ksnet_printf (teonet_cfg *teo_cfg, int type, const char *format,...)
 KSNet printf. More...
 
int teoLogPuts (teonet_cfg *teo_cfg, const char *module, int type, const char *message)
 
char * removeTEsc (char *str)
 Remove terminal escape substrings from string. More...
 
char * ksnet_formatMessage (const char *fmt,...)
 Create formated message in new null terminated string. More...
 
char * ksnet_sformatMessage (char *str_to_free, const char *fmt,...)
 Create formated message in new null terminated string, and free str_to_free. More...
 
char * ksnet_vformatMessage (const char *fmt, va_list ap)
 
char * trimlf (char *str)
 Remove trailing line feeds characters from null terminated string. More...
 
char * trim (char *str)
 Remove trailing or leading space. More...
 
void * memdup (const void *d, size_t s)
 Duplicate memory. More...
 
int calculate_lines (char *str)
 Calculate number of lines in string. More...
 
char * getRandomHostName (void)
 Get random host name. More...
 
char * getDataPath (void)
 Get path to teonet data folder. More...
 
int set_reuseaddr (int sd)
 Make socket reusable. More...
 
char * ksnet_getSysConfigDir (void)
 Getting path to ksnet configuration folder. More...
 
int inarray (int val, const int *arr, int size)
 Check if a value exist in a array. More...
 
ksnet_stringArr getIPs (teonet_cfg *conf)
 Get IPs. More...
 
int addr_port_equal (addr_port_t *ap_obj, char *addr, uint16_t port)
 
addr_port_taddr_port_init ()
 
void addr_port_free (addr_port_t *ap_obj)
 
addr_port_twr__ntop (const struct sockaddr *sa)
 
addr_port_twrap_inet_ntop (const struct sockaddr *sa)
 
int ip_to_array (char *ip, uint8_t *arr)
 Convert IP to bytes array. More...
 
void printHexDump (void *addr, size_t len)
 
void resolveDnsName (teonet_cfg *conf)
 
int ip_is_private (char *ip)
 Detect if input IP address is private. More...
 
size_t get_num_of_tags (char *data, size_t data_length)
 Calculate number of tags in json string. More...
 

Macro Definition Documentation

#define ADDRSTRLEN   128

Referenced by addr_port_init(), and wr__ntop().

Function Documentation

int addr_port_equal ( addr_port_t ap_obj,
char *  addr,
uint16_t  port 
)

References addr_port::addr, and addr_port::port.

Referenced by addr_port_init().

int calculate_lines ( char *  str)

Calculate number of lines in string.

Calculate number of line with line feed \n at the end in null input terminated string

Parameters
strInput null terminated string
Returns
Number of line with linefeed \n at the end

Referenced by ksnetArpShow(), and ksnTRUDPstatShow().

size_t get_num_of_tags ( char *  data,
size_t  data_length 
)

Calculate number of tags in json string.

Parameters
data
data_length
Returns
char* getDataPath ( void  )

Get path to teonet data folder.

Returns
NULL terminated static string

References free(), KSN_BUFFER_SIZE, and ksnet_formatMessage().

Referenced by teo::Teonet::getPath(), ksnet_optRead(), ksnetEvMgrRun(), read_config(), teowebConfigRead(), and uconfigFileName().

ksnet_stringArr getIPs ( teonet_cfg conf)

Get IPs.

Get IP address of this host

Returns
Pointer to ksnet_stringArr

References ksnet_stringArrAdd(), ksnet_stringArrCreate(), and teonet_cfg::vpn_ip.

Referenced by createCmdConnectRPacketUdp(), and open_local_port().

char* getRandomHostName ( void  )

Get random host name.

Create random host name. Should be free after use.

Returns
String with random host name. Should be free after use.

References ksnet_formatMessage().

Referenced by set_defaults().

int inarray ( int  val,
const int *  arr,
int  size 
)

Check if a value exist in a array.

Parameters
valInteger value
arrInteger array
sizeArray size
Returns
int ip_is_private ( char *  ip)

Detect if input IP address is private.

Parameters
ip
Returns

References ip_to_array().

Referenced by createCmdConnectRPacketUdp(), and open_local_port().

int ip_to_array ( char *  ip,
uint8_t *  arr 
)

Convert IP to bytes array.

Parameters
ip
arr
Returns

References free().

Referenced by ip_is_private(), and open_local_port().

int KSN_GET_TEST_MODE ( )
inline

Referenced by ksnet_printf().

void KSN_SET_TEST_MODE ( int  test_mode)
inline

Referenced by main().

char* ksnet_formatMessage ( const char *  fmt,
  ... 
)

Create formated message in new null terminated string.

Parameters
fmtFormat string like in printf function
...Parameter
Returns
Null terminated string, should be free after using or NULL on error
Examples:
teoackm.c, and teotcp.c.

References ksnet_vformatMessage().

Referenced by cmd_disconnected_cb(), event_cb(), teo::Teonet::formatMessage(), getDataPath(), getRandomHostName(), ksnet_optRead(), ksnetArpShowHeader(), ksnetArpShowLine(), ksnetArpShowStr(), ksnetEvMgrRestart(), ksnMultiShowListStr(), open_local_port(), read_config(), teowebConfigRead(), and uconfigFileName().

char* ksnet_getSysConfigDir ( void  )

Getting path to ksnet configuration folder.

Returns
NULL terminated string

Referenced by read_config(), teowebConfigRead(), and uconfigFileName().

int ksnet_printf ( teonet_cfg teo_cfg,
int  type,
const char *  format,
  ... 
)

KSNet printf.

See Also
vprintf

Make format output to stdout. Instead of standard printf function this function has type parameter which define type of print. It should be used this function instead of standard printf function.

Parameters
teo_cfgPointer to teonet_cfg
typeMESSAGE – print always; CONNECT – print if connect show flag connect is set; DEBUG – print if debug show flag is set on; DEBUG_VV – print if debug extra show flag is set on; ERROR_M – print always.
formatFormat like in standard printf function
...Parameters
Returns
Number of byte printed
Examples:
teocque.c, teofossa.c, teol0cli.c, teolarge.c, and teosend.c.

References _ANSI_NONE, teonet_cfg::color_output_disable_f, CONNECT, data, DEBUG, DEBUG_VV, DEBUG_VVV, DISPLAY_M, ERROR_M, free(), teonet_cfg::ke, KSN_GET_TEST_MODE(), ksnet_vformatMessage(), ksnetEvMgrGetTime(), teonet_cfg::log_prefix, teonet_cfg::log_priority, MESSAGE, removeTEsc(), teonet_cfg::show_connect_f, teonet_cfg::show_debug_f, teonet_cfg::show_debug_vv_f, teonet_cfg::show_debug_vvv_f, teoFilterFlagCheck(), teoLogCheck(), teoLoggingClientSend(), and trimlf().

Referenced by event_cb(), ksnetArpShow(), main(), start_stop_daemon(), tcp_read_cb(), teoLogPuts(), and test2().

char* ksnet_sformatMessage ( char *  str_to_free,
const char *  fmt,
  ... 
)

Create formated message in new null terminated string, and free str_to_free.

Parameters
str_to_free
fmtFormat string like in printf function
...Parameter
Returns
Null terminated string, should be free after using or NULL on error

References free(), and ksnet_vformatMessage().

Referenced by event_cb(), ksnet_optRead(), ksnetArpShowHeader(), ksnetArpShowStr(), ksnMultiShowListStr(), and teoGetFullAppTypeFromHostInfo().

char* ksnet_vformatMessage ( const char *  fmt,
va_list  ap 
)
void* memdup ( const void *  d,
size_t  s 
)

Duplicate memory.

Todo:
move this function to teonet library

Allocate memory and copy selected value to it

Parameters
dPointer to value to copy
sLength of value
Returns
Examples:
teodb_ex.c.

References malloc().

Referenced by event_cb(), and get_cb().

void printHexDump ( void *  addr,
size_t  len 
)
char* removeTEsc ( char *  str)

Remove terminal escape substrings from string.

Parameters
str
Returns

Referenced by ksnet_printf().

int set_reuseaddr ( int  sd)

Make socket reusable.

Parameters
sd
Returns
unsigned char teoFilterFlagCheck ( void *  ke)

References khv.

Referenced by ksnet_printf().

unsigned char teoLogCheck ( void *  ke,
void *  log 
)

References khv, and log_string_match().

Referenced by ksnet_printf().

void teoLoggingClientSend ( void *  ke,
const char *  message 
)

Send log data to logging servers.

Parameters
kePointer to ksnetEvMgrClass
messagePointer to cstring message

References kev, and ksnetEvMgrAsync().

Referenced by ksnet_printf().

int teoLogPuts ( teonet_cfg teo_cfg,
const char *  module,
int  type,
const char *  message 
)
char* trim ( char *  str)

Remove trailing or leading space.

Remove trailing or leading space or tabulation characters in input null terminated string

Parameters
strNull terminated input string to remove trailing and leading spaces in
Returns
Pointer to input string (the same as input string)
char* trimlf ( char *  str)

Remove trailing line feeds characters from null terminated string.

Parameters
str
Returns

Referenced by hotkeys_cb(), and ksnet_printf().

addr_port_t* wr__ntop ( const struct sockaddr *  sa)