BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
|
#include "cli.h"
#include <readline/readline.h>
#include <readline/history.h>
#include <errno.h>
#include <pthread.h>
#include <sys/stat.h>
#include "peer_queue.h"
#include "peer_connection.h"
#include "state.h"
#include "utils.h"
#include "ip.h"
Go to the source code of this file.
Functions | |
void | print_help () |
Prints CLI command help. | |
void | print_usage (const char *command_name) |
Prints CLI command usage. | |
void | print_commands () |
Prints CLI commands. | |
int | cli_exit (char **args) |
Exits BitLab CLI command. | |
int | cli_history (char **args) |
Prints CLI command history. | |
int | cli_help (char **args) |
Prints CLI command help. | |
int | cli_echo (char **args) |
Echoes the input. | |
int | cli_whoami (char **args) |
Prints the user name. | |
int | cli_get_ip (char **args) |
Gets remote IP address on an URL or host machine if no URL is provided. | |
int | cli_info (char **args) |
Prints program information. | |
int | cli_peer_discovery (char **args) |
Discovers Bitcoin peers. | |
int | cli_ping (char **args) |
Pings the specified IP address. | |
int | cli_connect (char **args) |
Connects to the specified IP address. | |
int | cli_getaddr (char **args) |
Sends a 'getaddr' message to the specified peer. | |
int | cli_disconnect (char **args) |
Disconnects from the specified node. | |
int | cli_getheaders (char **args) |
Sends a 'getheaders' message to specified peer. | |
int | cli_getblocks (char **args) |
Sends a 'getblocks' message to specified peer. | |
int | cli_getdata (char **args) |
Sends a 'getdata' message to specified peer. | |
int | cli_inv (char **args) |
Sends a 'inv' message to specified peer. | |
int | cli_tx (char **args) |
Sends a 'tx' message to specified peer. | |
int | cli_list (char **args) |
Lists all connected nodes. | |
int | cli_clear (char **args) |
Clears CLI window. | |
int | cli_get_line (char **lineptr, size_t *n, FILE *stream) |
Gets the line from the file stream. | |
char * | cli_read_line (void) |
Reads input from user. | |
int | cli_exec_line (char *line) |
Execute command. | |
char ** | cli_completion (const char *text, int start, int end) |
CLI completion function. | |
char * | cli_command_generator (const char *text, int state) |
CLI command generator. | |
void * | handle_cli (void *arg) |
CLI handler thread. | |
const char * | create_history_dir () |
Create history directory. | |
Variables | |
static const char * | cli_history_dir = NULL |
pthread_mutex_t | cli_mutex = PTHREAD_MUTEX_INITIALIZER |
static cli_command | cli_commands [] |
int cli_clear | ( | char ** | args | ) |
Clears CLI window.
args | The arguments passed to the function should be empty. |
Definition at line 1098 of file cli.c.
References BITLAB_LOG, clear_cli(), cli_mutex, log_message(), LOG_WARN, and print_usage().
char * cli_command_generator | ( | const char * | text, |
int | state | ||
) |
CLI command generator.
text | The text to generate. |
state | The state of the generator. |
Definition at line 1258 of file cli.c.
References cli_command::cli_command_name, cli_commands, CLI_COMMANDS_NUM, state, and strdup().
Referenced by cli_completion().
char ** cli_completion | ( | const char * | text, |
int | start, | ||
int | end | ||
) |
CLI completion function.
text | The text to complete. |
start | The start index. |
end | The end index. |
Definition at line 1225 of file cli.c.
References cli_command_generator(), CLI_PREFIX, guarded_print(), and print_help().
Referenced by handle_cli().
int cli_connect | ( | char ** | args | ) |
Connects to the specified IP address.
args | The IP address. |
Definition at line 768 of file cli.c.
References BITLAB_LOG, BUFFER_SIZE, cli_mutex, connect_to_peer(), guarded_print_line(), is_numeric_address(), log_message(), LOG_WARN, and print_usage().
int cli_disconnect | ( | char ** | args | ) |
Disconnects from the specified node.
This function disconnects from the node specified by the given node ID. It closes the socket, terminates the thread, and logs the disconnection.
args | The arguments passed to the function should contain the node ID. |
Definition at line 830 of file cli.c.
References BITLAB_LOG, cli_mutex, disconnect(), guarded_print_line(), log_message(), LOG_WARN, and print_usage().
int cli_echo | ( | char ** | args | ) |
Echoes the input.
args | Input that will be echoed. |
Definition at line 367 of file cli.c.
References BITLAB_LOG, cli_mutex, guarded_print(), log_message(), LOG_WARN, and print_usage().
int cli_exec_line | ( | char * | line | ) |
Execute command.
This function executes a command from line.
line | The string containing input from user. |
Definition at line 1163 of file cli.c.
References BITLAB_LOG, CLI_BUFSIZE, cli_command::cli_command, cli_commands, CLI_COMMANDS_NUM, CLI_DELIM, guarded_print_line(), LOG_FATAL, LOG_INFO, log_message(), and strtok().
Referenced by handle_cli(), and run_bitlab().
int cli_exit | ( | char ** | args | ) |
Exits BitLab CLI command.
args | The arguments passed to the function should be empty. |
Definition at line 281 of file cli.c.
References BITLAB_LOG, cli_mutex, LOG_INFO, log_message(), LOG_WARN, print_usage(), and set_exit_flag().
int cli_get_ip | ( | char ** | args | ) |
Gets remote IP address on an URL or host machine if no URL is provided.
args | The URL. |
Definition at line 437 of file cli.c.
References BUFFER_SIZE, cli_mutex, get_remote_ip_address(), guarded_print_line(), is_valid_domain_address(), and lookup_address().
int cli_get_line | ( | char ** | lineptr, |
size_t * | n, | ||
FILE * | stream | ||
) |
Gets the line from the file stream.
lineptr | The pointer to the line. |
n | The size of the line. |
stream | The file stream. |
Definition at line 1114 of file cli.c.
References MAX_LINE_LEN.
int cli_getaddr | ( | char ** | args | ) |
Sends a 'getaddr' message to the specified peer.
args | The index of the peer. |
Definition at line 804 of file cli.c.
References BITLAB_LOG, cli_mutex, guarded_print_line(), log_message(), LOG_WARN, print_usage(), and send_getaddr_and_wait().
int cli_getblocks | ( | char ** | args | ) |
Sends a 'getblocks' message to specified peer.
args | The index of the peer. |
Definition at line 882 of file cli.c.
References BITLAB_LOG, cli_mutex, guarded_print_line(), log_message(), LOG_WARN, print_usage(), and send_getblocks_and_wait().
int cli_getdata | ( | char ** | args | ) |
Sends a 'getdata' message to specified peer.
args | The index of the peer. |
Definition at line 908 of file cli.c.
References BITLAB_LOG, cli_mutex, guarded_print_line(), LOG_ERROR, log_message(), LOG_WARN, MAX_NODES, print_usage(), and send_getdata_and_wait().
int cli_getheaders | ( | char ** | args | ) |
Sends a 'getheaders' message to specified peer.
args | The index of the peer. |
Definition at line 856 of file cli.c.
References BITLAB_LOG, cli_mutex, guarded_print_line(), log_message(), LOG_WARN, print_usage(), and send_getheaders_and_wait().
int cli_help | ( | char ** | args | ) |
Prints CLI command help.
args | The arguments passed to this function can be empty to get full program help or argument should specify one comand to get help for. |
Definition at line 333 of file cli.c.
References BITLAB_LOG, cli_commands, cli_mutex, guarded_print_line(), log_message(), LOG_WARN, print_help(), and print_usage().
int cli_history | ( | char ** | args | ) |
Prints CLI command history.
args | The arguments passed to the function should be empty. |
Definition at line 312 of file cli.c.
References BITLAB_LOG, cli_mutex, guarded_print_line(), log_message(), LOG_WARN, and print_usage().
int cli_info | ( | char ** | args | ) |
Prints program information.
args | The arguments passed to the function should be empty. |
Definition at line 481 of file cli.c.
References BITLAB_LOG, BITLAB_VERSION, cli_mutex, guarded_print_line(), log_message(), LOG_WARN, operation, program_operation::peer_discovery, program_operation::peer_discovery_daemon, program_operation::peer_discovery_dns_lookup, program_operation::peer_discovery_hardcoded_seeds, program_operation::peer_discovery_in_progress, program_operation::peer_discovery_succeeded, print_program_state(), and print_usage().
int cli_inv | ( | char ** | args | ) |
Sends a 'inv' message to specified peer.
args | The index of the peer. |
Definition at line 992 of file cli.c.
References BITLAB_LOG, cli_mutex, load_blocks_from_file(), LOG_ERROR, log_message(), LOG_WARN, print_usage(), and send_inv_and_wait().
int cli_list | ( | char ** | args | ) |
Lists all connected nodes.
args | The arguments passed to the function should be empty. |
Definition at line 1082 of file cli.c.
References BITLAB_LOG, cli_mutex, list_connected_nodes(), log_message(), LOG_WARN, and print_usage().
int cli_peer_discovery | ( | char ** | args | ) |
Discovers Bitcoin peers.
args | The arguments passed to the function should match those specified in 'help peerdiscovery'. |
Definition at line 528 of file cli.c.
References BITLAB_LOG, cli_mutex, get_peer_discovery(), get_peer_discovery_in_progress(), get_peer_discovery_succeeded(), guarded_print_line(), LOG_INFO, log_message(), LOG_WARN, PEER_DISCOVERY_DEFAULT_DAEMON, PEER_DISCOVERY_DEFAULT_DNS_LOOKUP, PEER_DISCOVERY_DEFAULT_HARDCODED_SEEDS, print_peer_queue(), print_usage(), set_peer_discovery(), set_peer_discovery_daemon(), set_peer_discovery_dns_domain(), set_peer_discovery_dns_lookup(), set_peer_discovery_hardcoded_seeds(), and usleep().
int cli_ping | ( | char ** | args | ) |
Pings the specified IP address.
args | The IP address. |
Definition at line 694 of file cli.c.
References BITLAB_LOG, BUFFER_SIZE, cli_mutex, guarded_print_line(), log_message(), LOG_WARN, and print_usage().
char * cli_read_line | ( | void | ) |
Reads input from user.
WARNING: char* returned from this function must be freed.
Definition at line 1153 of file cli.c.
References cli_mutex, and CLI_PREFIX.
Referenced by handle_cli().
int cli_tx | ( | char ** | args | ) |
Sends a 'tx' message to specified peer.
args | The index of the peer. |
Definition at line 1036 of file cli.c.
References BITLAB_LOG, cli_mutex, guarded_print_line(), LOG_ERROR, log_message(), LOG_WARN, MAX_NODES, print_usage(), and send_tx().
int cli_whoami | ( | char ** | args | ) |
Prints the user name.
args | The arguments passed to the function should be empty. |
Definition at line 385 of file cli.c.
References BITLAB_LOG, BUFFER_SIZE, cli_mutex, get_local_ip_address(), get_remote_ip_address(), guarded_print_line(), log_message(), LOG_WARN, and print_usage().
const char * create_history_dir | ( | ) |
Create history directory.
Definition at line 1328 of file cli.c.
References logs_dir.
Referenced by handle_cli().
void * handle_cli | ( | void * | arg | ) |
CLI handler thread.
arg | Not used. Write dummy code for no warning. |
Definition at line 1278 of file cli.c.
References BITLAB_LOG, BUFFER_SIZE, cli_completion(), cli_exec_line(), cli_history_dir, CLI_HISTORY_FILE, cli_read_line(), create_history_dir(), get_exit_flag(), LOG_INFO, log_message(), LOG_WARN, and usleep().
Referenced by run_bitlab().
void print_commands | ( | ) |
Prints CLI commands.
Definition at line 275 of file cli.c.
References cli_commands, CLI_COMMANDS_NUM, and guarded_print_line().
void print_help | ( | ) |
Prints CLI command help.
Definition at line 171 of file cli.c.
References cli_command::cli_command_brief_desc, cli_command::cli_command_usage, cli_commands, CLI_COMMANDS_NUM, and guarded_print_line().
Referenced by cli_completion(), and cli_help().
void print_usage | ( | const char * | command_name | ) |
Prints CLI command usage.
command_name | The name of the command. |
Definition at line 262 of file cli.c.
References cli_commands, CLI_COMMANDS_NUM, and guarded_print_line().
Referenced by cli_clear(), cli_connect(), cli_disconnect(), cli_echo(), cli_exit(), cli_getaddr(), cli_getblocks(), cli_getdata(), cli_getheaders(), cli_help(), cli_history(), cli_info(), cli_inv(), cli_list(), cli_peer_discovery(), cli_ping(), cli_tx(), and cli_whoami().
|
static |
Definition at line 22 of file cli.c.
Referenced by cli_command_generator(), cli_exec_line(), cli_help(), print_commands(), print_help(), and print_usage().
|
static |
Definition at line 16 of file cli.c.
Referenced by handle_cli().
pthread_mutex_t cli_mutex = PTHREAD_MUTEX_INITIALIZER |
Definition at line 19 of file cli.c.
Referenced by cli_clear(), cli_connect(), cli_disconnect(), cli_echo(), cli_exit(), cli_get_ip(), cli_getaddr(), cli_getblocks(), cli_getdata(), cli_getheaders(), cli_help(), cli_history(), cli_info(), cli_inv(), cli_list(), cli_peer_discovery(), cli_ping(), cli_read_line(), cli_tx(), and cli_whoami().