BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
Loading...
Searching...
No Matches
utils.c File Reference
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netinet/in.h>

Go to the source code of this file.

Functions

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.
 
uint64_t ntohll (uint64_t value)
 Convert a 64-bit integer from host byte order to network byte order.
 
uint64_t read_var_int (const unsigned char *data, size_t *offset)
 
int is_valid_ipv4 (const char *ip_str)
 Check if the IP address is valid.
 

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

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

◆ 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}

◆ read_var_int()

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

Definition at line 88 of file utils.c.

89{
90 uint64_t result = 0;
91 unsigned char first_byte = data[*offset];
92 *offset += 1;
93
94 if (first_byte < 0xfd)
95 {
96 result = first_byte;
97 }
98 else if (first_byte == 0xfd)
99 {
100 result = *(uint16_t*)(data + *offset);
101 *offset += 2;
102 }
103 else if (first_byte == 0xfe)
104 {
105 result = *(uint32_t*)(data + *offset);
106 *offset += 4;
107 }
108 else if (first_byte == 0xff)
109 {
110 result = *(uint64_t*)(data + *offset);
111 *offset += 8;
112 }
113 return result;
114}