BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
Loading...
Searching...
No Matches
utils.h File Reference
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include "log.h"

Go to the source code of this file.

Macros

#define TIMESTAMP_LENGTH   20
 
#define BUFFER_SIZE   8096
 

Functions

void usleep (unsigned int usec)
 
char * strdup (const char *str1)
 
char * strndup (const char *src, size_t size)
 
char * strtok (char *str, const char *delimiters)
 
int fileno (FILE *__stream)
 
struct tm * localtime_r (const time_t *timer, struct tm *buf)
 
void flockfile (FILE *filehandle)
 
void funlockfile (FILE *file)
 
FILE * popen (const char *command, const char *type)
 
int pclose (FILE *stream)
 
void get_timestamp (char *buffer, size_t buffer_size)
 Get the timestamp.
 
void get_formatted_timestamp (char *buffer, size_t buffer_size)
 Get the formatted timestamp.
 
void clear_cli ()
 Clear the CLI window.
 
int init_config_dir ()
 Initialize the configuration directory.
 
void guarded_print (const char *format,...)
 Guarded print function.
 
void guarded_print_line (const char *format,...)
 Guarded print line function.
 
void log_to_file (const char *filename, const char *format,...)
 Log to file function.
 
uint64_t ntohll (uint64_t value)
 Convert a 64-bit integer from host byte order to network byte order.
 
size_t read_var_int (const unsigned char *data, uint64_t *value)
 Convert a 64-bit integer from network byte order to host byte order.
 
int is_valid_ipv4 (const char *ip_str)
 Check if the IP address is valid.
 

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   8096

Definition at line 12 of file utils.h.

◆ TIMESTAMP_LENGTH

#define TIMESTAMP_LENGTH   20

Definition at line 11 of file utils.h.

Function Documentation

◆ clear_cli()

void clear_cli ( )

Clear the CLI window.

Definition at line 33 of file utils.c.

34{
35 guarded_print("\033[H\033[J");
36}
void guarded_print(const char *format,...)
Guarded print function.
Definition utils.c:55

References guarded_print().

Referenced by cli_clear().

◆ fileno()

int fileno ( FILE *  __stream)

Referenced by log_message().

◆ flockfile()

void flockfile ( FILE *  filehandle)

◆ funlockfile()

void funlockfile ( FILE *  file)

◆ get_formatted_timestamp()

void get_formatted_timestamp ( char *  buffer,
size_t  buffer_size 
)

Get the formatted timestamp.

This function is used to get the formatted timestamp in a YYYY-MM-DD HH:MM:SS format.

Parameters
bufferThe buffer to store the formatted timestamp.
buffer_sizeThe size of the buffer.

Definition at line 25 of file utils.c.

26{
27 time_t now = time(NULL);
28 struct tm tm_now;
29 localtime_r(&now, &tm_now);
30 strftime(buffer, buffer_size, "%Y-%m-%d %H:%M:%S", &tm_now);
31}
struct tm * localtime_r(const time_t *timer, struct tm *buf)

References localtime_r().

Referenced by log_message().

◆ get_timestamp()

void get_timestamp ( char *  buffer,
size_t  buffer_size 
)

Get the timestamp.

This function is used to get the timestamp in a YYYYMMDDHHMMSS format.

Parameters
bufferThe buffer to store the timestamp.
buffer_sizeThe size of the buffer.

Definition at line 17 of file utils.c.

18{
19 time_t now = time(NULL);
20 struct tm tm_now;
21 localtime_r(&now, &tm_now);
22 strftime(buffer, buffer_size, "%Y%m%d%H%M%S", &tm_now);
23}

References localtime_r().

◆ guarded_print()

void guarded_print ( const char *  format,
  ... 
)

Guarded print function.

This function is used to lock the stdout file and print the formatted string.

Parameters
formatThe format string.

Definition at line 55 of file utils.c.

56{
57 flockfile(stdout);
58 va_list args;
59 va_start(args, format);
60 vprintf(format, args);
61 va_end(args);
62 funlockfile(stdout);
63}
void flockfile(FILE *filehandle)
void funlockfile(FILE *file)

References flockfile(), and funlockfile().

Referenced by clear_cli(), cli_completion(), cli_echo(), load_blocks_from_file(), parse_inv_message(), save_blocks_to_file(), send_getblocks_and_wait(), and send_getheaders_and_wait().

◆ guarded_print_line()

void guarded_print_line ( const char *  format,
  ... 
)

Guarded print line function.

This function is used to lock the stdout file and print the formatted string.

Parameters
formatThe format string.

Definition at line 65 of file utils.c.

66{
67 flockfile(stdout);
68 va_list args;
69 va_start(args, format);
70 vprintf(format, args);
71 va_end(args);
72 printf("\n");
73 funlockfile(stdout);
74}

References flockfile(), and funlockfile().

Referenced by cli_connect(), cli_disconnect(), cli_exec_line(), cli_get_ip(), cli_getaddr(), cli_getblocks(), cli_getdata(), cli_getheaders(), cli_help(), cli_history(), cli_info(), cli_peer_discovery(), cli_ping(), cli_tx(), cli_whoami(), connect_to_peer(), handle_peer_discovery(), list_connected_nodes(), print_commands(), print_help(), print_peer_queue(), print_program_state(), print_usage(), run_bitlab(), send_addr(), and send_getaddr_and_wait().

◆ init_config_dir()

int init_config_dir ( )

Initialize the configuration directory.

Definition at line 38 of file utils.c.

39{
40 const char* home = getenv("HOME");
41 if (home == NULL)
42 return 1;
43 const char* suffix = "/.bitlab";
44 char* init_dir = malloc(strlen(home) + strlen(suffix) + 1);
45 if (init_dir == NULL)
46 return 1;
47 strcpy(init_dir, home);
48 strcat(init_dir, suffix);
49
50 mkdir(init_dir, 0700);
51 free(init_dir);
52 return 0;
53}

Referenced by run_bitlab().

◆ is_valid_ipv4()

int is_valid_ipv4 ( const char *  ip_str)

Check if the IP address is valid.

Parameters
ip_strThe IP address string.
Returns
True if the IP address is valid, false otherwise.

Definition at line 116 of file utils.c.

117{
118 struct sockaddr_in sa;
119 return inet_pton(AF_INET, ip_str, &(sa.sin_addr)) == 1;
120}

Referenced by send_getaddr_and_wait().

◆ localtime_r()

struct tm * localtime_r ( const time_t *  timer,
struct tm *  buf 
)

◆ log_to_file()

void log_to_file ( const char *  filename,
const char *  format,
  ... 
)

Log to file function.

This function is used to log messages to a specified file.

Parameters
filenameThe name of the log file.
formatThe format string.

◆ ntohll()

uint64_t ntohll ( uint64_t  value)

Convert a 64-bit integer from host byte order to network byte order.

Parameters
valueThe 64-bit integer in host byte order.
Returns
The 64-bit integer in network byte order.

Definition at line 76 of file utils.c.

77{
78 if (__BYTE_ORDER == __LITTLE_ENDIAN)
79 {
80 return ((uint64_t)ntohl(value & 0xFFFFFFFF) << 32) | ntohl(value >> 32);
81 }
82 else
83 {
84 return value;
85 }
86}

◆ pclose()

int pclose ( FILE *  stream)

◆ popen()

FILE * popen ( const char *  command,
const char *  type 
)

◆ read_var_int()

size_t read_var_int ( const unsigned char *  data,
uint64_t *  value 
)

Convert a 64-bit integer from network byte order to host byte order.

Parameters
valueThe 64-bit integer in network byte order.
Returns
The 64-bit integer in host byte order.

Referenced by decode_transactions(), handle_inv_message(), parse_inv_message(), and send_getaddr_and_wait().

◆ strdup()

char * strdup ( const char *  str1)

Referenced by cli_command_generator().

◆ strndup()

char * strndup ( const char *  src,
size_t  size 
)

◆ strtok()

char * strtok ( char *  str,
const char *  delimiters 
)

◆ usleep()

void usleep ( unsigned int  usec)