BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
|
#include <stdint.h>
#include <pthread.h>
Go to the source code of this file.
Data Structures | |
struct | bitcoin_msg_header |
struct | Node |
The structure to store information about a connected peer. More... | |
Macros | |
#define | MAX_NODES 100 |
#define | BITCOIN_MAINNET_MAGIC 0xD9B4BEF9 |
#define | BITCOIN_MAINNET_PORT 8333 |
#define | htole16(x) ((uint16_t)((((x) & 0xFF) << 8) | (((x) >> 8) & 0xFF))) |
#define | htole32(x) ((uint32_t)((((x) & 0xFF) << 24) | (((x) >> 8) & 0xFF00) | (((x) >> 16) & 0xFF) | (((x) >> 24) & 0xFF000000))) |
#define | htole64(x) ((uint64_t)((((x) & 0xFF) << 56) | (((x) >> 8) & 0xFF00) | (((x) >> 16) & 0xFF0000) | (((x) >> 24) & 0xFF000000) | (((x) >> 32) & 0xFF00000000) | (((x) >> 40) & 0xFF0000000000) | (((x) >> 48) & 0xFF000000000000) | (((x) >> 56) & 0xFF00000000000000))) |
#define | MAX_LOCATOR_COUNT 10 |
#define | GENESIS_BLOCK_HASH "0000000000000000000000000000000000000000000000000000000000000000" |
#define | HEADERS_FILE "headers.dat" |
#define | MAX_HEADERS_COUNT 2000 |
Functions | |
void | list_connected_nodes () |
Lists all connected nodes and their details. | |
int | get_idx (const char *ip_address) |
Get the index of the node with the given IP address. | |
void | send_getaddr_and_wait (int idx) |
Sends a 'getaddr' message to the peer and waits for a response. | |
int | connect_to_peer (const char *ip_addr) |
Connects to a peer using the specified IP address. | |
void | disconnect (int node_id) |
Disconnects from the node specified by the node ID. | |
unsigned char * | load_blocks_from_file (const char *filename, size_t *payload_len) |
void | send_getheaders_and_wait (int idx) |
Sends a 'getheaders' message to the peer and waits for a response. | |
void | send_headers (int idx, const unsigned char *start_hash, const unsigned char *stop_hash) |
Sends a 'headers' message to the peer. | |
void | send_getblocks_and_wait (int idx) |
Sends a 'getblocks' message to the peer and waits for a response. | |
void | send_getdata_and_wait (int idx, const unsigned char *hashes, size_t hash_count) |
Sends a 'getdata' message to the peer and waits for a response. | |
void | send_inv_and_wait (int idx, const unsigned char *inv_data, size_t inv_count) |
Sends an 'inv' message to the peer and waits for a response. | |
void | send_tx (int idx, const unsigned char *tx_data, size_t tx_size) |
Sends a 'tx' message to the specified node. | |
Variables | |
Node | nodes [MAX_NODES] |
#define BITCOIN_MAINNET_MAGIC 0xD9B4BEF9 |
Definition at line 11 of file peer_connection.h.
#define BITCOIN_MAINNET_PORT 8333 |
Definition at line 14 of file peer_connection.h.
#define GENESIS_BLOCK_HASH "0000000000000000000000000000000000000000000000000000000000000000" |
Definition at line 21 of file peer_connection.h.
#define HEADERS_FILE "headers.dat" |
Definition at line 22 of file peer_connection.h.
#define htole16 | ( | x | ) | ((uint16_t)((((x) & 0xFF) << 8) | (((x) >> 8) & 0xFF))) |
Definition at line 16 of file peer_connection.h.
#define htole32 | ( | x | ) | ((uint32_t)((((x) & 0xFF) << 24) | (((x) >> 8) & 0xFF00) | (((x) >> 16) & 0xFF) | (((x) >> 24) & 0xFF000000))) |
Definition at line 17 of file peer_connection.h.
#define htole64 | ( | x | ) | ((uint64_t)((((x) & 0xFF) << 56) | (((x) >> 8) & 0xFF00) | (((x) >> 16) & 0xFF0000) | (((x) >> 24) & 0xFF000000) | (((x) >> 32) & 0xFF00000000) | (((x) >> 40) & 0xFF0000000000) | (((x) >> 48) & 0xFF000000000000) | (((x) >> 56) & 0xFF00000000000000))) |
Definition at line 18 of file peer_connection.h.
#define MAX_HEADERS_COUNT 2000 |
Definition at line 23 of file peer_connection.h.
#define MAX_LOCATOR_COUNT 10 |
Definition at line 20 of file peer_connection.h.
#define MAX_NODES 100 |
Definition at line 8 of file peer_connection.h.
int connect_to_peer | ( | const char * | ip_addr | ) |
Connects to a peer using the specified IP address.
This function attempts to establish a connection to a peer using the given IP address. It returns a socket file descriptor if the connection is successful, or -1 if it fails.
ip_addr | The IP address of the peer to connect to. |
Definition at line 1032 of file peer_connection.c.
References BITCOIN_MAINNET_MAGIC, BITCOIN_MAINNET_PORT, build_message(), build_version_payload(), bitcoin_msg_header::command, create_peer_thread(), guarded_print_line(), init_logging(), initialize_node(), bitcoin_msg_header::length, log_message(), LOG_WARN, bitcoin_msg_header::magic, MAX_NODES, nodes, and send_verack().
Referenced by cli_connect().
void disconnect | ( | int | node_id | ) |
Disconnects from the node specified by the node ID.
This function disconnects from the node specified by the given node ID. It closes the socket, terminates the thread, and logs the disconnection.
node_id | The ID of the node in the nodes array to disconnect from. |
Definition at line 1236 of file peer_connection.c.
References Node::ip_address, Node::is_connected, LOG_INFO, log_message(), MAX_NODES, nodes, Node::port, Node::socket_fd, and Node::thread.
Referenced by cli_disconnect().
int get_idx | ( | const char * | ip_address | ) |
Get the index of the node with the given IP address.
ip_address | The IP address of the node. |
Definition at line 291 of file peer_connection.c.
References MAX_NODES, and nodes.
Referenced by peer_communication().
void list_connected_nodes | ( | ) |
Lists all connected nodes and their details.
This function iterates through the list of nodes and prints the details of each node that is currently connected.
Definition at line 269 of file peer_connection.c.
References guarded_print_line(), MAX_NODES, and nodes.
Referenced by cli_list().
unsigned char * load_blocks_from_file | ( | const char * | filename, |
size_t * | payload_len | ||
) |
Definition at line 1491 of file peer_connection.c.
References guarded_print().
Referenced by cli_inv(), and peer_communication().
void send_getaddr_and_wait | ( | int | idx | ) |
Sends a 'getaddr' message to the peer and waits for a response.
This function sends a 'getaddr' message to the peer identified by the given index and waits for a response. It is used to request a list of known peers from the connected peer.
idx | The index of the peer in the nodes array. |
Definition at line 303 of file peer_connection.c.
References add_peer_to_queue(), BITCOIN_MAINNET_MAGIC, build_message(), bitcoin_msg_header::command, guarded_print_line(), Node::ip_address, Node::is_connected, is_in_private_network(), is_valid_ipv4(), bitcoin_msg_header::length, LOG_INFO, log_message(), LOG_WARN, bitcoin_msg_header::magic, MAX_NODES, nodes, Node::operation_in_progress, read_var_int(), and Node::socket_fd.
Referenced by cli_getaddr().
void send_getblocks_and_wait | ( | int | idx | ) |
Sends a 'getblocks' message to the peer and waits for a response.
This function sends a 'getblocks' message to the peer identified by the given index and waits for a response. It is used to request a list of blocks from the connected peer. The response is processed and the blocks are saved to a file.
idx | The index of the peer in the nodes array. |
Definition at line 1518 of file peer_connection.c.
References build_getblocks_message(), guarded_print(), Node::ip_address, load_latest_known_block_hash(), LOG_INFO, log_message(), MAX_LOCATOR_COUNT, MAX_NODES, nodes, Node::operation_in_progress, parse_inv_message(), save_blocks_to_file(), and Node::socket_fd.
Referenced by cli_getblocks().
void send_getdata_and_wait | ( | int | idx, |
const unsigned char * | hashes, | ||
size_t | hash_count | ||
) |
Sends a 'getdata' message to the peer and waits for a response.
This function sends a 'getdata' message to the peer identified by the given index and waits for a response. It is used to request specific blocks or transactions from the connected peer based on the provided hashes. The response is saved to a file and logged to the Bitlab logs.
idx | The index of the peer in the nodes array. |
hashes | An array of hashes representing the blocks or transactions to request. |
hash_count | The number of hashes in the array. |
Definition at line 1728 of file peer_connection.c.
References BITCOIN_MAINNET_MAGIC, build_getdata_message(), bitcoin_msg_header::command, decode_transactions(), Node::ip_address, bitcoin_msg_header::length, LOG_INFO, log_message(), bitcoin_msg_header::magic, MAX_NODES, nodes, Node::operation_in_progress, and Node::socket_fd.
Referenced by cli_getdata(), and handle_inv_message().
void send_getheaders_and_wait | ( | int | idx | ) |
Sends a 'getheaders' message to the peer and waits for a response.
This function sends a 'getheaders' message to the peer identified by the given index and waits for a response. It is used to request a list of known peers from the connected peer.
idx | The index of the peer in the nodes array. |
Definition at line 1330 of file peer_connection.c.
References build_getheaders_message(), guarded_print(), Node::ip_address, load_latest_known_block_hash(), LOG_INFO, log_message(), MAX_LOCATOR_COUNT, MAX_NODES, nodes, Node::operation_in_progress, parse_headers_message(), and Node::socket_fd.
Referenced by cli_getheaders().
void send_headers | ( | int | idx, |
const unsigned char * | start_hash, | ||
const unsigned char * | stop_hash | ||
) |
Sends a 'headers' message to the peer.
This function sends a 'headers' message to the peer identified by the given index. It retrieves the block headers from the local storage starting from the specified start hash up to the stop hash or the maximum number of headers allowed.
idx | The index of the peer in the nodes array. |
start_hash | The hash of the first block header to send. |
stop_hash | The hash of the last block header to send. |
Definition at line 1395 of file peer_connection.c.
References BITCOIN_MAINNET_MAGIC, compute_checksum(), HEADERS_FILE, htole32, Node::ip_address, LOG_INFO, log_message(), bitcoin_msg_header::magic, MAX_HEADERS_COUNT, MAX_NODES, nodes, and Node::socket_fd.
Referenced by peer_communication().
void send_inv_and_wait | ( | int | idx, |
const unsigned char * | inv_data, | ||
size_t | inv_count | ||
) |
Sends an 'inv' message to the peer and waits for a response.
This function sends an 'inv' message to the peer identified by the given index and waits for a response. It is used to advertise the knowledge of one or more objects (blocks or transactions). The inventory data is provided as input to the function.
idx | The index of the peer in the nodes array. |
inv_data | An array of inventory vectors (type + hash). |
inv_count | The number of inventory vectors in the array. |
Sends an 'inv' message to the peer and waits for a response.
This function sends an 'inv' message to the peer identified by the given index. It is used to advertise the knowledge of one or more objects (blocks or transactions). The inventory data is provided as input to the function.
idx | The index of the peer in the nodes array. |
inv_data | An array of inventory vectors (type + hash). |
inv_count | The number of inventory vectors in the array. |
Definition at line 1866 of file peer_connection.c.
References BITCOIN_MAINNET_MAGIC, build_inv_message(), bitcoin_msg_header::command, handle_inv_message(), Node::ip_address, LOG_INFO, log_message(), bitcoin_msg_header::magic, MAX_NODES, nodes, Node::operation_in_progress, Node::socket_fd, and write_var_int().
Referenced by cli_inv().
void send_tx | ( | int | idx, |
const unsigned char * | tx_data, | ||
size_t | tx_size | ||
) |
Sends a 'tx' message to the specified node.
This function sends a 'tx' message to the node identified by the given index with the provided transaction data. It constructs the message with the appropriate Bitcoin protocol header and sends it over the network socket associated with the node.
idx | The index of the node in the nodes array. |
tx_data | A pointer to the transaction data in hexadecimal format. |
tx_size | The size of the transaction data in bytes. |
Definition at line 2047 of file peer_connection.c.
References BITCOIN_MAINNET_MAGIC, compute_checksum(), htole32, Node::ip_address, LOG_INFO, log_message(), bitcoin_msg_header::magic, MAX_NODES, nodes, and Node::socket_fd.
Referenced by cli_tx().
Definition at line 26 of file peer_connection.c.
Referenced by connect_to_peer(), disconnect(), get_idx(), list_connected_nodes(), send_getaddr_and_wait(), send_getblocks_and_wait(), send_getdata_and_wait(), send_getheaders_and_wait(), send_headers(), send_inv_and_wait(), and send_tx().