19
19
20
20
#include "peer_queue.h"
21
21
#include "utils.h"
22
+ #include "log.h"
22
23
#include "ip.h"
23
24
24
25
// Global array to hold connected nodes
@@ -226,7 +227,7 @@ size_t write_var_int(unsigned char* buffer, uint64_t value)
226
227
return 9 ;
227
228
}
228
229
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 );
230
231
231
232
if (value < 0xfd )
232
233
{
@@ -1694,6 +1695,11 @@ void handle_inv_message(int idx, const unsigned char* payload, size_t payload_le
1694
1695
}
1695
1696
}
1696
1697
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
+
1697
1703
size_t build_getdata_message (unsigned char * buffer , size_t buffer_size , const unsigned char * hashes , size_t hash_count )
1698
1704
{
1699
1705
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
1782
1788
log_message (LOG_INFO , log_filename , __FILE__ , "Received response to 'getdata' message." );
1783
1789
node -> operation_in_progress = 0 ;
1784
1790
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
+
1785
1810
// Save the response to a file
1786
1811
char filename [256 ];
1787
1812
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
1817
1842
printf ("inv_count: %zu, payload_size: %zu, buffer_size: %zu\n" , inv_count , payload_size , buffer_size );
1818
1843
1819
1844
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 );
1820
1847
return 0 ;
1848
+ }
1821
1849
1822
1850
unsigned char * payload = (unsigned char * )malloc (payload_size );
1823
1851
if (!payload )
1852
+ {
1853
+ printf ("Failed to allocate memory for payload\n" );
1824
1854
return 0 ;
1855
+ }
1825
1856
1826
1857
// Set inventory count (var_int encoding)
1827
1858
size_t offset = 0 ;
1828
1859
offset += write_var_int (payload + offset , inv_count );
1860
+ log_message (LOG_DEBUG , BITLAB_LOG , __FILE__ , "After write_var_int: offset=%zu" , offset );
1829
1861
1830
1862
// Copy inventory vectors (type + hash)
1831
1863
for (size_t i = 0 ; i < inv_count ; i ++ )
1832
1864
{
1833
1865
memcpy (payload + offset , inv_data + (i * 36 ), 36 );
1834
1866
offset += 36 ;
1835
1867
}
1868
+ printf ("After copying inventory vectors: offset=%zu\n" , offset );
1836
1869
1837
1870
// Build final message
1838
1871
size_t message_size = build_message (buffer , buffer_size , "inv" , payload , payload_size );
1872
+ printf ("Built message size: %zu\n" , message_size );
1839
1873
1840
1874
free (payload );
1841
1875
return message_size ;
@@ -1870,7 +1904,10 @@ void send_inv_and_wait(int idx, const unsigned char* inv_data, size_t inv_count)
1870
1904
char log_filename [256 ];
1871
1905
snprintf (log_filename , sizeof (log_filename ), "peer_connection_%s.log" , node -> ip_address );
1872
1906
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
+
1874
1911
printf ("inv_count: %zu, inv_msg_size: %zu\n" , inv_count , inv_msg_size );
1875
1912
1876
1913
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)
1900
1937
}
1901
1938
1902
1939
log_message (LOG_INFO , log_filename , __FILE__ , "Sent 'inv' message." );
1940
+ printf ("Sent 'inv' message.\n" );
1903
1941
node -> operation_in_progress = 1 ;
1904
1942
1905
1943
// 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)
1921
1959
}
1922
1960
1923
1961
log_message (LOG_INFO , log_filename , __FILE__ , "Received response to 'inv' message." );
1962
+ printf ("Received response to 'inv' message.\n" );
1924
1963
node -> operation_in_progress = 0 ;
1925
1964
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
+ }
1928
1996
1929
1997
free (inv_msg );
1930
1998
}
0 commit comments