Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit e0ff2f6

Browse files
committed
Add inv response processing
1 parent 5058c71 commit e0ff2f6

File tree

1 file changed

+72
-4
lines changed

1 file changed

+72
-4
lines changed

bitlab/src/peer_connection.c

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "peer_queue.h"
2121
#include "utils.h"
22+
#include "log.h"
2223
#include "ip.h"
2324

2425
// Global array to hold connected nodes
@@ -226,7 +227,7 @@ size_t write_var_int(unsigned char* buffer, uint64_t value)
226227
return 9;
227228
}
228229

229-
printf("write_var_int: buffer=%p, value=%lu\n", buffer, value);
230+
log_message(LOG_DEBUG, BITLAB_LOG, __FILE__, "write_var_int: buffer=%p, value=%lu", (void*)buffer, value);
230231

231232
if (value < 0xfd)
232233
{
@@ -1694,6 +1695,11 @@ void handle_inv_message(int idx, const unsigned char* payload, size_t payload_le
16941695
}
16951696
}
16961697

1698+
void handle_inv_response(int idx, const unsigned char* buffer, size_t bytes_received)
1699+
{
1700+
handle_inv_message(idx, buffer, bytes_received);
1701+
}
1702+
16971703
size_t build_getdata_message(unsigned char* buffer, size_t buffer_size, const unsigned char* hashes, size_t hash_count)
16981704
{
16991705
if (buffer_size < sizeof(bitcoin_msg_header) + 1 + (hash_count * 36))
@@ -1782,6 +1788,25 @@ void send_getdata_and_wait(int idx, const unsigned char* hashes, size_t hash_cou
17821788
log_message(LOG_INFO, log_filename, __FILE__, "Received response to 'getdata' message.");
17831789
node->operation_in_progress = 0;
17841790

1791+
// Check if the response is an 'inv' message
1792+
if (bytes_received >= sizeof(bitcoin_msg_header))
1793+
{
1794+
bitcoin_msg_header* hdr = (bitcoin_msg_header*)buffer;
1795+
if (hdr->magic == BITCOIN_MAINNET_MAGIC)
1796+
{
1797+
char cmd_name[13];
1798+
memset(cmd_name, 0, sizeof(cmd_name));
1799+
memcpy(cmd_name, hdr->command, 12);
1800+
1801+
if (strcmp(cmd_name, "inv") == 0)
1802+
{
1803+
printf("Received 'inv' response:\n");
1804+
handle_inv_response(idx, buffer + sizeof(bitcoin_msg_header), bytes_received - sizeof(bitcoin_msg_header));
1805+
return;
1806+
}
1807+
}
1808+
}
1809+
17851810
// Save the response to a file
17861811
char filename[256];
17871812
snprintf(filename, sizeof(filename), "getdata_response_%s.dat", node->ip_address);
@@ -1817,25 +1842,34 @@ size_t build_inv_message(unsigned char* buffer, size_t buffer_size, const unsign
18171842
printf("inv_count: %zu, payload_size: %zu, buffer_size: %zu\n", inv_count, payload_size, buffer_size);
18181843

18191844
if (buffer_size < sizeof(bitcoin_msg_header) + payload_size)
1845+
{
1846+
printf("Buffer size is too small: buffer_size=%zu, required=%zu\n", buffer_size, sizeof(bitcoin_msg_header) + payload_size);
18201847
return 0;
1848+
}
18211849

18221850
unsigned char* payload = (unsigned char*)malloc(payload_size);
18231851
if (!payload)
1852+
{
1853+
printf("Failed to allocate memory for payload\n");
18241854
return 0;
1855+
}
18251856

18261857
// Set inventory count (var_int encoding)
18271858
size_t offset = 0;
18281859
offset += write_var_int(payload + offset, inv_count);
1860+
log_message(LOG_DEBUG, BITLAB_LOG, __FILE__, "After write_var_int: offset=%zu", offset);
18291861

18301862
// Copy inventory vectors (type + hash)
18311863
for (size_t i = 0; i < inv_count; i++)
18321864
{
18331865
memcpy(payload + offset, inv_data + (i * 36), 36);
18341866
offset += 36;
18351867
}
1868+
printf("After copying inventory vectors: offset=%zu\n", offset);
18361869

18371870
// Build final message
18381871
size_t message_size = build_message(buffer, buffer_size, "inv", payload, payload_size);
1872+
printf("Built message size: %zu\n", message_size);
18391873

18401874
free(payload);
18411875
return message_size;
@@ -1870,7 +1904,10 @@ void send_inv_and_wait(int idx, const unsigned char* inv_data, size_t inv_count)
18701904
char log_filename[256];
18711905
snprintf(log_filename, sizeof(log_filename), "peer_connection_%s.log", node->ip_address);
18721906

1873-
size_t inv_msg_size = sizeof(bitcoin_msg_header) + 1 + (inv_count * 36);
1907+
size_t var_int_size = write_var_int(NULL, inv_count); // Get the size of the var_int encoding
1908+
size_t payload_size = var_int_size + (inv_count * 36); // var_int size + 36 bytes per inventory vector
1909+
size_t inv_msg_size = sizeof(bitcoin_msg_header) + payload_size;
1910+
18741911
printf("inv_count: %zu, inv_msg_size: %zu\n", inv_count, inv_msg_size);
18751912

18761913
unsigned char* inv_msg = (unsigned char*)malloc(inv_msg_size);
@@ -1900,6 +1937,7 @@ void send_inv_and_wait(int idx, const unsigned char* inv_data, size_t inv_count)
19001937
}
19011938

19021939
log_message(LOG_INFO, log_filename, __FILE__, "Sent 'inv' message.");
1940+
printf("Sent 'inv' message.\n");
19031941
node->operation_in_progress = 1;
19041942

19051943
// Wait for 10 seconds for a response
@@ -1921,10 +1959,40 @@ void send_inv_and_wait(int idx, const unsigned char* inv_data, size_t inv_count)
19211959
}
19221960

19231961
log_message(LOG_INFO, log_filename, __FILE__, "Received response to 'inv' message.");
1962+
printf("Received response to 'inv' message.\n");
19241963
node->operation_in_progress = 0;
19251964

1926-
// Process the response (this part can be expanded based on the specific requirements)
1927-
// ...
1965+
// Process the response
1966+
if (bytes_received >= sizeof(bitcoin_msg_header))
1967+
{
1968+
bitcoin_msg_header* hdr = (bitcoin_msg_header*)buffer;
1969+
if (hdr->magic == BITCOIN_MAINNET_MAGIC)
1970+
{
1971+
char cmd_name[13];
1972+
memset(cmd_name, 0, sizeof(cmd_name));
1973+
memcpy(cmd_name, hdr->command, 12);
1974+
1975+
printf("Received command: '%s'\n", cmd_name);
1976+
1977+
if (strcmp(cmd_name, "inv") == 0)
1978+
{
1979+
printf("Received 'inv' response:\n");
1980+
handle_inv_response(idx, buffer + sizeof(bitcoin_msg_header), bytes_received - sizeof(bitcoin_msg_header));
1981+
}
1982+
else
1983+
{
1984+
printf("Unhandled command: '%s'\n", cmd_name);
1985+
}
1986+
}
1987+
else
1988+
{
1989+
printf("Unexpected magic bytes (0x%08X).\n", hdr->magic);
1990+
}
1991+
}
1992+
else
1993+
{
1994+
printf("Received incomplete message.\n");
1995+
}
19281996

19291997
free(inv_msg);
19301998
}

0 commit comments

Comments
 (0)