Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
src/utils/utils.h
Go to the documentation of this file.
1 /*
2  * File: utils.h
3  * Author: Kirill Scherba
4  *
5  * Created on April 10, 2015, 6:13 PM
6  */
7 
8 #ifndef UTILS_H
9 #define UTILS_H
10 
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdarg.h>
14 
15 #include "rlutil.h"
16 #include "string_arr.h"
17 #include "config/conf.h"
18 
19 typedef struct addr_port {
20  char *addr;
21  uint16_t port;
22  int (*equal)(struct addr_port *, char*, uint16_t);
23 } addr_port_t;
24 
25 addr_port_t *wrap_inet_ntop(const struct sockaddr *sa);
26 void addr_port_free(addr_port_t *ap_obj);
27 
31 typedef enum ksnet_printf_type {
32 
41 
43 
44 #define _ksn_printf_type_(type) \
45  (type == MESSAGE ? "MESSAGE" : \
46  type == ERROR_M ? "ERROR" : \
47  type == DEBUG ? "DEBUG" : \
48  type == DEBUG_VV ? "DEBUG_VV" : \
49  type == DEBUG_VVV ? "DEBUG_VVV" : \
50  type == CONNECT ? "CONNECT" : "DISPLAY")
51 
52 #define _ksn_printf_format_(format) "%s %s: " _ANSI_GREY "%s:(%s:%d)" _ANSI_NONE ": " _ANSI_GREEN format _ANSI_NONE
53 #define _ksn_printf_format_display_m(format) "%s%s%s%s%d\b" format
54 
55 #define ksn_printf(ke, module, type, format, ...) \
56  ksnet_printf(&((ke)->teo_cfg), type, \
57  _ksn_printf_format_(format), \
58  _ksn_printf_type_(type), \
59  module[0] == '\0' ? (ke)->teo_cfg.app_name : module, \
60  __func__, __FILE__, __LINE__, __VA_ARGS__)
61 
62 #define ksn_puts(ke, module, type, format) \
63  ksnet_printf(&((ke)->teo_cfg), type, \
64  _ksn_printf_format_(format) "\n", \
65  _ksn_printf_type_(type), \
66  module[0] == '\0' ? (ke)->teo_cfg.app_name : module, \
67  __func__, __FILE__, __LINE__)
68 
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
74 int teoLogPuts(teonet_cfg *teo_cfg, const char* module , int type, const char* message);
75 int ksnet_printf(teonet_cfg *teo_cfg, int type, const char* format, ...);
76 char *ksnet_formatMessage(const char *fmt, ...);
77 char *ksnet_sformatMessage(char *str_to_free, const char *fmt, ...);
78 char *ksnet_vformatMessage(const char *fmt, va_list ap);
79 
80 char *getRandomHostName(void); // Implemented in enet.c module
81 
82 void *memdup(const void* d, size_t s);
83 char *trim(char *str);
84 char *trimlf(char *str);
85 char *removeTEsc(char *str);
86 //char* itoa(int ival);
87 int calculate_lines(char *str);
88 int inarray(int val, const int *arr, int size);
89 
90 int set_reuseaddr(int sd);
91 
92 char* getDataPath(void);
93 char *ksnet_getSysConfigDir(void);
94 
96 int ip_is_private(char *ip);
97 int ip_to_array(char* ip, uint8_t *arr);
98 
99 size_t get_num_of_tags(char *data, size_t data_length);
100 
101 void KSN_SET_TEST_MODE(int test_mode);
102 int KSN_GET_TEST_MODE();
103 
104 
105 char *ksn_base64_encode(const unsigned char *data,
106  size_t input_length,
107  size_t *output_length);
108 
109 unsigned char *ksn_base64_decode(const char *data,
110  size_t input_length,
111  size_t *output_length);
112 
113 void dump_bytes(char *buffer, int buffer_len, uint8_t* data, int data_len);
114 
115 void printHexDump(void *addr, size_t len);
116 void resolveDnsName(teonet_cfg *conf);
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif /* UTILS_H */
#2 Connect or Auth messages
Definition: src/utils/utils.h:35
void * memdup(const void *d, size_t s)
Duplicate memory.
Definition: utils.c:358
char * ksnet_vformatMessage(const char *fmt, va_list ap)
Definition: utils.c:263
void resolveDnsName(teonet_cfg *conf)
Definition: utils.c:815
#7 Regular messages (display only)
Definition: src/utils/utils.h:40
char * addr
Definition: src/utils/utils.h:20
int ip_is_private(char *ip)
Detect if input IP address is private.
Definition: utils.c:847
const char data[]
Make it with: gcc -o post-callback post-callback.c -lcurl.
Definition: post-callback.c:23
int calculate_lines(char *str)
Calculate number of lines in string.
Definition: utils.c:387
ksnet_printf_type
KSNet printf messages types.
Definition: src/utils/utils.h:31
int KSN_GET_TEST_MODE()
Definition: utils.c:37
Definition: src/utils/utils.h:19
#6 Debug message (extra+)
Definition: src/utils/utils.h:39
int set_reuseaddr(int sd)
Make socket reusable.
Definition: utils.c:553
char * ksn_base64_encode(const unsigned char *data, size_t input_length, size_t *output_length)
Base64 encode.
Definition: base64.c:48
char * ksnet_getSysConfigDir(void)
Getting path to ksnet configuration folder.
Definition: utils.c:569
Definition: conf.h:51
#1 Error message
Definition: src/utils/utils.h:34
int ip_to_array(char *ip, uint8_t *arr)
Convert IP to bytes array.
Definition: utils.c:739
addr_port_t * wrap_inet_ntop(const struct sockaddr *sa)
Definition: utils.c:722
#0 Log off
Definition: src/utils/utils.h:33
char * trim(char *str)
Remove trailing or leading space.
Definition: utils.c:328
char ** ksnet_stringArr
Definition: string_arr.h:11
void printHexDump(void *addr, size_t len)
Definition: utils.c:773
size_t get_num_of_tags(char *data, size_t data_length)
Calculate number of tags in json string.
Definition: utils.c:867
#5 Debug message (extra)
Definition: src/utils/utils.h:38
void addr_port_free(addr_port_t *ap_obj)
Definition: utils.c:679
ksnet_stringArr getIPs(teonet_cfg *conf)
Get IPs.
Definition: utils.c:613
#3 Regular messages
Definition: src/utils/utils.h:36
void dump_bytes(char *buffer, int buffer_len, uint8_t *data, int data_len)
uint16_t port
Definition: src/utils/utils.h:21
char * ksnet_formatMessage(const char *fmt,...)
Create formated message in new null terminated string.
Definition: utils.c:231
char * trimlf(char *str)
Remove trailing line feeds characters from null terminated string.
Definition: utils.c:308
void KSN_SET_TEST_MODE(int test_mode)
Definition: utils.c:34
#4 Debug message (normal)
Definition: src/utils/utils.h:37
char * ksnet_sformatMessage(char *str_to_free, const char *fmt,...)
Create formated message in new null terminated string, and free str_to_free.
Definition: utils.c:249
struct addr_port addr_port_t
int ksnet_printf(teonet_cfg *teo_cfg, int type, const char *format,...)
KSNet printf.
Definition: utils.c:59
char * removeTEsc(char *str)
Remove terminal escape substrings from string.
Definition: utils.c:210
int inarray(int val, const int *arr, int size)
Check if a value exist in a array.
Definition: utils.c:585
char * getRandomHostName(void)
Get random host name.
Definition: utils.c:403
unsigned char * ksn_base64_decode(const char *data, size_t input_length, size_t *output_length)
Base64decode.
Definition: base64.c:89
int teoLogPuts(teonet_cfg *teo_cfg, const char *module, int type, const char *message)
Definition: utils.c:196
int(* equal)(struct addr_port *, char *, uint16_t)
Definition: src/utils/utils.h:22
char * getDataPath(void)
Get path to teonet data folder.
Definition: utils.c:416