|
1 | 1 | #ifndef __PEER_CONNECTION_H
|
2 | 2 | #define __PEER_CONNECTION_H
|
3 | 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) |
| 28 | +typedef 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 |
| 34 | +} bitcoin_msg_header; |
| 35 | +#pragma pack(pop) |
| 36 | + |
| 37 | +/** |
| 38 | + * @brief The structure to store information about a connected peer. |
| 39 | + * |
| 40 | + * @param ip_address The IP address of the peer. |
| 41 | + * @param port The port of the peer. |
| 42 | + * @param socket_fd The socket file descriptor for communication. |
| 43 | + * @param thread The thread assigned to this connection. |
| 44 | + * @param is_connected The connection status. |
| 45 | + * @param operation_in_progress The operation status. |
| 46 | + * @param compact_blocks Does peer want to use compact blocks. |
| 47 | + * @param fee_rate Min fee rate in sat/kB of transaction that peer allows. |
| 48 | + */ |
| 49 | +typedef struct |
| 50 | +{ |
| 51 | + char ip_address[64]; |
| 52 | + uint16_t port; |
| 53 | + int socket_fd; |
| 54 | + pthread_t thread; |
| 55 | + int is_connected; |
| 56 | + int operation_in_progress; |
| 57 | + uint64_t compact_blocks; |
| 58 | + uint64_t fee_rate; |
| 59 | +} Node; |
| 60 | + |
| 61 | +// global array of nodes |
| 62 | +extern Node nodes[MAX_NODES]; |
| 63 | + |
4 | 64 | /**
|
5 | 65 | * @brief Lists all connected nodes and their details.
|
6 | 66 | *
|
|
9 | 69 | */
|
10 | 70 | void list_connected_nodes();
|
11 | 71 |
|
| 72 | +/** |
| 73 | + * @brief Get the index of the node with the given IP address. |
| 74 | + * |
| 75 | + * @param ip_address The IP address of the node. |
| 76 | + * @return The index of the node in the global array. |
| 77 | + */ |
| 78 | +int get_idx(const char* ip_address); |
| 79 | + |
12 | 80 | /**
|
13 | 81 | * @brief Sends a 'getaddr' message to the peer and waits for a response.
|
14 | 82 | *
|
@@ -39,4 +107,68 @@ int connect_to_peer(const char* ip_addr);
|
39 | 107 | */
|
40 | 108 | void disconnect(int node_id);
|
41 | 109 |
|
| 110 | + |
| 111 | +unsigned char* load_blocks_from_file(const char* filename, size_t* payload_len); |
| 112 | + |
| 113 | +/** |
| 114 | + * @brief Sends a 'getheaders' message to the peer and waits for a response. |
| 115 | + * |
| 116 | + * This function sends a 'getheaders' message to the peer identified by the given index |
| 117 | + * and waits for a response. It is used to request a list of known peers from the connected peer. |
| 118 | + * |
| 119 | + * @param idx The index of the peer in the nodes array. |
| 120 | + */ |
| 121 | +void send_getheaders_and_wait(int idx); |
| 122 | + |
| 123 | +/** |
| 124 | + * @brief Sends a 'headers' message to the peer. |
| 125 | + * |
| 126 | + * This function sends a 'headers' message to the peer identified by the given index. |
| 127 | + * It retrieves the block headers from the local storage starting from the specified |
| 128 | + * start hash up to the stop hash or the maximum number of headers allowed. |
| 129 | + * |
| 130 | + * @param idx The index of the peer in the nodes array. |
| 131 | + * @param start_hash The hash of the first block header to send. |
| 132 | + * @param stop_hash The hash of the last block header to send. |
| 133 | + */ |
| 134 | +void send_headers(int idx, const unsigned char* start_hash, const unsigned char* stop_hash); |
| 135 | + |
| 136 | +/** |
| 137 | + * @brief Sends a 'getblocks' message to the peer and waits for a response. |
| 138 | + * |
| 139 | + * This function sends a 'getblocks' message to the peer identified by the given index |
| 140 | + * and waits for a response. It is used to request a list of blocks from the connected peer. |
| 141 | + * The response is processed and the blocks are saved to a file. |
| 142 | + * |
| 143 | + * @param idx The index of the peer in the nodes array. |
| 144 | + */ |
| 145 | +void send_getblocks_and_wait(int idx); |
| 146 | + |
| 147 | +/** |
| 148 | + * @brief Sends a 'getdata' message to the peer and waits for a response. |
| 149 | + * |
| 150 | + * This function sends a 'getdata' message to the peer identified by the given index |
| 151 | + * and waits for a response. It is used to request specific blocks or transactions |
| 152 | + * from the connected peer based on the provided hashes. The response is saved to a file |
| 153 | + * and logged to the Bitlab logs. |
| 154 | + * |
| 155 | + * @param idx The index of the peer in the nodes array. |
| 156 | + * @param hashes An array of hashes representing the blocks or transactions to request. |
| 157 | + * @param hash_count The number of hashes in the array. |
| 158 | + */ |
| 159 | +void send_getdata_and_wait(int idx, const unsigned char* hashes, size_t hash_count); |
| 160 | + |
| 161 | +/** |
| 162 | + * @brief Sends an 'inv' message to the peer and waits for a response. |
| 163 | + * |
| 164 | + * This function sends an 'inv' message to the peer identified by the given index |
| 165 | + * and waits for a response. It is used to advertise the knowledge of one or more objects |
| 166 | + * (blocks or transactions). The inventory data is provided as input to the function. |
| 167 | + * |
| 168 | + * @param idx The index of the peer in the nodes array. |
| 169 | + * @param inv_data An array of inventory vectors (type + hash). |
| 170 | + * @param inv_count The number of inventory vectors in the array. |
| 171 | + */ |
| 172 | +void send_inv_and_wait(int idx, const unsigned char* inv_data, size_t inv_count); |
| 173 | + |
42 | 174 | #endif // __PEER_CONNECTION_H
|
0 commit comments