Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
src/utils/utils.h File Reference
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include "rlutil.h"
#include "string_arr.h"
#include "config/conf.h"

Go to the source code of this file.

Data Structures

struct  addr_port
 

Macros

#define _ksn_printf_type_(type)
 
#define _ksn_printf_format_(format)   "%s %s: " _ANSI_GREY "%s:(%s:%d)" _ANSI_NONE ": " _ANSI_GREEN format _ANSI_NONE
 
#define _ksn_printf_format_display_m(format)   "%s%s%s%s%d\b" format
 
#define ksn_printf(ke, module, type, format,...)
 
#define ksn_puts(ke, module, type, format)
 

Typedefs

typedef struct addr_port addr_port_t
 
typedef enum ksnet_printf_type ksnet_printf_type
 KSNet printf messages types. More...
 

Enumerations

enum  ksnet_printf_type {
  NONE_LOG, ERROR_M, CONNECT, MESSAGE,
  DEBUG, DEBUG_VV, DEBUG_VVV, DISPLAY_M
}
 KSNet printf messages types. More...
 

Functions

addr_port_twrap_inet_ntop (const struct sockaddr *sa)
 
void addr_port_free (addr_port_t *ap_obj)
 
int teoLogPuts (teonet_cfg *teo_cfg, const char *module, int type, const char *message)
 
int ksnet_printf (teonet_cfg *teo_cfg, int type, const char *format,...)
 KSNet printf. 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 * getRandomHostName (void)
 Get random host name. More...
 
void * memdup (const void *d, size_t s)
 Duplicate memory. More...
 
char * trim (char *str)
 Remove trailing or leading space. More...
 
char * trimlf (char *str)
 Remove trailing line feeds characters from null terminated string. More...
 
char * removeTEsc (char *str)
 Remove terminal escape substrings from string. More...
 
int calculate_lines (char *str)
 Calculate number of lines in string. More...
 
int inarray (int val, const int *arr, int size)
 Check if a value exist in a array. More...
 
int set_reuseaddr (int sd)
 Make socket reusable. More...
 
char * getDataPath (void)
 Get path to teonet data folder. More...
 
char * ksnet_getSysConfigDir (void)
 Getting path to ksnet configuration folder. More...
 
ksnet_stringArr getIPs (teonet_cfg *conf)
 Get IPs. More...
 
int ip_is_private (char *ip)
 Detect if input IP address is private. More...
 
int ip_to_array (char *ip, uint8_t *arr)
 Convert IP to bytes array. More...
 
size_t get_num_of_tags (char *data, size_t data_length)
 Calculate number of tags in json string. More...
 
void KSN_SET_TEST_MODE (int test_mode)
 
int KSN_GET_TEST_MODE ()
 
char * ksn_base64_encode (const unsigned char *data, size_t input_length, size_t *output_length)
 Base64 encode. More...
 
unsigned char * ksn_base64_decode (const char *data, size_t input_length, size_t *output_length)
 Base64decode. More...
 
void dump_bytes (char *buffer, int buffer_len, uint8_t *data, int data_len)
 
void printHexDump (void *addr, size_t len)
 
void resolveDnsName (teonet_cfg *conf)
 

Macro Definition Documentation

#define _ksn_printf_format_ (   format)    "%s %s: " _ANSI_GREY "%s:(%s:%d)" _ANSI_NONE ": " _ANSI_GREEN format _ANSI_NONE
#define _ksn_printf_format_display_m (   format)    "%s%s%s%s%d\b" format
#define _ksn_printf_type_ (   type)
Value:
(type == MESSAGE ? "MESSAGE" : \
type == ERROR_M ? "ERROR" : \
type == DEBUG ? "DEBUG" : \
type == DEBUG_VV ? "DEBUG_VV" : \
type == DEBUG_VVV ? "DEBUG_VVV" : \
type == CONNECT ? "CONNECT" : "DISPLAY")
#2 Connect or Auth messages
Definition: src/utils/utils.h:35
#6 Debug message (extra+)
Definition: src/utils/utils.h:39
#1 Error message
Definition: src/utils/utils.h:34
#5 Debug message (extra)
Definition: src/utils/utils.h:38
#3 Regular messages
Definition: src/utils/utils.h:36
#4 Debug message (normal)
Definition: src/utils/utils.h:37

Referenced by teoLogPuts().

#define ksn_printf (   ke,
  module,
  type,
  format,
  ... 
)
Value:
ksnet_printf(&((ke)->teo_cfg), type, \
module[0] == '\0' ? (ke)->teo_cfg.app_name : module, \
__func__, __FILE__, __LINE__, __VA_ARGS__)
#define _ksn_printf_format_(format)
Definition: src/utils/utils.h:52
#define _ksn_printf_type_(type)
Definition: src/utils/utils.h:44
#define ke
int ksnet_printf(teonet_cfg *teo_cfg, int type, const char *format,...)
KSNet printf.
Definition: utils.c:59

Referenced by _check_connected(), _cmd_tcpp_read_cb(), _decrypt(), cmd_disconnected_cb(), cmd_l0_cb(), cmd_l0_check_cb(), cmd_l0_kick_client(), cmd_l0_to_cb(), cmd_reconnect_cb(), cmd_stream_cb(), cmd_udpp_read_cb(), connect_r_host_cb(), event_cb(), idle_activity_cb(), idle_cb(), idle_stdin_cb(), kq_connect_cb(), ksnCommandCheck(), ksnCommandSendCmdConnect(), ksnCommandSendCmdConnectA(), ksnCoreBind(), ksnCoreCheckNewPeer(), ksnCoreProcessPacket(), ksnCoreSendCmdto(), ksnCoreSendCmdtoA(), ksnCoreSendto(), ksnDecryptPackage(), ksnEncryptPackage(), ksnetEvMgrFree(), ksnetEvMgrRun(), ksnLNullClientDisconnect(), ksnLNullPacketSend(), ksnLNullSendToL0(), ksnLNulltrudpCheckPaket(), ksnSplitCombine(), ksnSplitPacket(), ksnStreamClose(), ksnStreamCreate(), ksnTCPProxyClientConnect(), ksnTCPProxyServerClientConnect(), ksnTCPProxyServerClientDisconnect(), ksnTCPProxyServerStart(), ksnTRUDPsendto(), open_local_port(), sendCmdAnswerToBinaryA(), sendCmdT(), sigusr2_cb(), stream_out_cb(), teo_recvfrom(), teo_sendto(), teoBroadcastSend(), teoLNullPacketCheckMiscrypted(), teoLogReaderOpenCbPP(), teoMetricInit(), teoSScrSend(), teoSScrSendA(), teoSScrSubscribeA(), teoSScrSubscription(), teoSScrUnSubscription(), timer_cb(), and trudp_event_cb().

#define ksn_puts (   ke,
  module,
  type,
  format 
)
Value:
ksnet_printf(&((ke)->teo_cfg), type, \
_ksn_printf_format_(format) "\n", \
module[0] == '\0' ? (ke)->teo_cfg.app_name : module, \
__func__, __FILE__, __LINE__)
#define _ksn_printf_format_(format)
Definition: src/utils/utils.h:52
#define _ksn_printf_type_(type)
Definition: src/utils/utils.h:44
#define ke
int ksnet_printf(teonet_cfg *teo_cfg, int type, const char *format,...)
KSNet printf.
Definition: utils.c:59
Examples:
teodb_ex.c.

Referenced by event_cb(), ksnetEvMgrRun(), ksnReconnectInit(), ksnSplitCombine(), main(), sig_async_cb(), sigint_cb(), stdin_cb(), teoAsyncDestroy(), teoAsyncInit(), teoAsyncTest(), teoLoggingClientDestroy(), teoLoggingClientInit(), teoLoggingServerDestroy(), teoLoggingServerInit(), teoLogReaderDestroy(), teoLogReaderInit(), teoWSInit(), and trudp_event_cb().

Typedef Documentation

typedef struct addr_port addr_port_t

KSNet printf messages types.

Enumeration Type Documentation

KSNet printf messages types.

Enumerator
NONE_LOG 

#0 Log off

ERROR_M 

#1 Error message

CONNECT 

#2 Connect or Auth messages

MESSAGE 

#3 Regular messages

DEBUG 

#4 Debug message (normal)

DEBUG_VV 

#5 Debug message (extra)

DEBUG_VVV 

#6 Debug message (extra+)

DISPLAY_M 

#7 Regular messages (display only)

Function Documentation

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().

void dump_bytes ( char *  buffer,
int  buffer_len,
uint8_t *  data,
int  data_len 
)
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().

unsigned char* ksn_base64_decode ( const char *  data,
size_t  input_length,
size_t *  output_length 
)

Base64decode.

Parameters
dataInput encoded data
input_lengthInput data length
output_lengthOutput data length
Returns
Decoded data, should be free after use

References build_decoding_table(), and malloc().

char* ksn_base64_encode ( const unsigned char *  data,
size_t  input_length,
size_t *  output_length 
)

Base64 encode.

Parameters
[in]dataInput data
[in]input_lengthInput data length
[out]output_lengthOutput data length
Returns
Encoded data, should be free after use

References malloc().

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

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

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

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
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().