BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
Loading...
Searching...
No Matches
peer_connection.h
Go to the documentation of this file.
1#ifndef __PEER_CONNECTION_H
2#define __PEER_CONNECTION_H
3
4#include <stdint.h>
5#include <pthread.h>
6
7// Maximum number of peers to track
8#define MAX_NODES 100
9
10// Bitcoin mainnet magic bytes:
11#define BITCOIN_MAINNET_MAGIC 0xD9B4BEF9
12
13// Default Bitcoin mainnet port:
14#define BITCOIN_MAINNET_PORT 8333
15
16#define htole16(x) ((uint16_t)((((x) & 0xFF) << 8) | (((x) >> 8) & 0xFF)))
17#define htole32(x) ((uint32_t)((((x) & 0xFF) << 24) | (((x) >> 8) & 0xFF00) | (((x) >> 16) & 0xFF) | (((x) >> 24) & 0xFF000000)))
18#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)))
19
20#define MAX_LOCATOR_COUNT 10
21#define GENESIS_BLOCK_HASH "0000000000000000000000000000000000000000000000000000000000000000"
22#define HEADERS_FILE "headers.dat"
23#define MAX_HEADERS_COUNT 2000
24
25// Structure for Bitcoin P2P message header (24 bytes).
26// For reference: https://en.bitcoin.it/wiki/Protocol_documentation#Message_structure
27#pragma pack(push, 1)
28typedef struct
29{
30 unsigned int magic; // Magic value indicating message origin network
31 char command[12]; // ASCII command (null-padded)
32 unsigned int length; // Payload size (little-endian)
33 unsigned char checksum[4]; // First 4 bytes of double SHA-256 of the payload
35#pragma pack(pop)
36
49typedef struct
50{
51 char ip_address[64];
52 uint16_t port;
54 pthread_t thread;
58 uint64_t fee_rate;
59} Node;
60
61// global array of nodes
62extern Node nodes[MAX_NODES];
63
71
78int get_idx(const char* ip_address);
79
88void send_getaddr_and_wait(int idx);
89
98int connect_to_peer(const char* ip_addr);
99
108void disconnect(int node_id);
109
110
111unsigned char* load_blocks_from_file(const char* filename, size_t* payload_len);
112
121void send_getheaders_and_wait(int idx);
122
134void send_headers(int idx, const unsigned char* start_hash, const unsigned char* stop_hash);
135
145void send_getblocks_and_wait(int idx);
146
159void send_getdata_and_wait(int idx, const unsigned char* hashes, size_t hash_count);
160
172void send_inv_and_wait(int idx, const unsigned char* inv_data, size_t inv_count);
173
185void send_tx(int idx, const unsigned char* tx_data, size_t tx_size);
186
187#endif // __PEER_CONNECTION_H
void list_connected_nodes()
Lists all connected nodes and their details.
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_getheaders_and_wait(int idx)
Sends a 'getheaders' message to the peer and waits for a response.
void send_getblocks_and_wait(int idx)
Sends a 'getblocks' message to the peer and waits for a response.
unsigned char * load_blocks_from_file(const char *filename, size_t *payload_len)
int get_idx(const char *ip_address)
Get the index of the node with the given IP address.
void send_headers(int idx, const unsigned char *start_hash, const unsigned char *stop_hash)
Sends a 'headers' message to the peer.
void send_tx(int idx, const unsigned char *tx_data, size_t tx_size)
Sends a 'tx' message to the specified node.
Node nodes[MAX_NODES]
void send_getaddr_and_wait(int idx)
Sends a 'getaddr' message to the peer and waits for a response.
#define MAX_NODES
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.
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.
The structure to store information about a connected peer.
uint16_t port
pthread_t thread
uint64_t fee_rate
int socket_fd
uint64_t compact_blocks
int is_connected
int operation_in_progress