Bitcoin Forum
December 07, 2016, 04:46:01 PM *
News: To be able to use the next phase of the beta forum software, please ensure that your email address is correct/functional.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Can't seem to build the version message correctly to talk to a bitcoin peer  (Read 1920 times)
btc_artist
Full Member
***
Offline Offline

Activity: 154


Bitcoin!


View Profile WWW
February 09, 2012, 11:56:01 PM
 #1

So I'm trying to learn more about the Bitcoin protocol, and I'm writing some code to try to talk with a Bitcoin instance as a peer. My problem is that Bitcoin won't respond to any of the messages I try to send.  Here's what I have:

An example of the raw "version" message I'm sending:
Code:
F9BEB4D976657273696F6E000000000055000000B4C3000001000000000000009E5A344F00000000010000000000000000000000000000000000FFFF7F000010208D010000000000000000000000000000000000FFFF7F00001061A8CB0170F938EC13C700D6880200

The above message broken apart:
Code:
Start    : F9 BE B4 D9
Command  : 76 65 72 73 69 6F 6E 00 00 00 00 00
Length   : 55 00 00 00
Checksum :
Payload  :
    Version          : B4 C3 00 00
    Services         : 01 00 00 00 00 00 00 00
    Timestamp        : 9E 5A 34 4F 00 00 00 00
    RecipientAddress :
        Services : 01 00 00 00 00 00 00 00
        Ip       : 00 00 00 00 00 00 00 00 00 00 FF FF 7F 00 00 10
        Port     : 20 8D
    SenderAddress    :
        Services : 01 00 00 00 00 00 00 00
        Ip       : 00 00 00 00 00 00 00 00 00 00 FF FF 7F 00 00 10
        Port     : 61 A8
    Nonce            : CB 01 70 F9 38 EC 13 C7
    SubVersion       : 00
    LastBlock        : D6 88 02 00


Here's what I get in the Bitcoin debug.log file:
Code:
accepted connection 127.0.0.1:63714
...
PROCESSMESSAGE MESSAGESTART NOT FOUND
...
socket no message in first 60 seconds, 1 0
disconnecting node 127.0.0.1:63714

"PROCESSMESSAGE MESSAGESTART NOT FOUND" comes from main.cpp around line 2342.
"socket no message in first 60 seconds" comes from net.cpp, right near the end.

I've tried to follow the spec as outlined here: https://en.bitcoin.it/wiki/Protocol_specification (particularly the "Message structure", "Network address" and "Version" sections), but when I send the message, I never get a response, and Bitcoin complains about the already mentioned errors (above) in the log file.

Any ideas as to what I'm doing wrong in building the message?

BTC: 1CDCLDBHbAzHyYUkk1wYHPYmrtDZNhk8zf
LTC: LMS7SqZJnqzxo76iDSEua33WCyYZdjaQoE
1481129161
Hero Member
*
Offline Offline

Posts: 1481129161

View Profile Personal Message (Offline)

Ignore
1481129161
Reply with quote  #2

1481129161
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1481129161
Hero Member
*
Offline Offline

Posts: 1481129161

View Profile Personal Message (Offline)

Ignore
1481129161
Reply with quote  #2

1481129161
Report to moderator
1481129161
Hero Member
*
Offline Offline

Posts: 1481129161

View Profile Personal Message (Offline)

Ignore
1481129161
Reply with quote  #2

1481129161
Report to moderator
1481129161
Hero Member
*
Offline Offline

Posts: 1481129161

View Profile Personal Message (Offline)

Ignore
1481129161
Reply with quote  #2

1481129161
Report to moderator
btc_artist
Full Member
***
Offline Offline

Activity: 154


Bitcoin!


View Profile WWW
February 10, 2012, 04:53:04 AM
 #2

Solved the first part of my problem.  I no longer get the "PROCESSMESSAGE MESSAGESTART NOT FOUND" error in the debug.log file, but when I send the "version" message to bitcoin, it doesn't respond with anything, and in the debug file, it closes the connection with "socket no message in first 60 seconds".

Any ideas?  Thanks in advance.

Edit: Note, I get a socket error in my code saying: "An existing connection was forcibly closed by the remote host".

BTC: 1CDCLDBHbAzHyYUkk1wYHPYmrtDZNhk8zf
LTC: LMS7SqZJnqzxo76iDSEua33WCyYZdjaQoE
theymos
Administrator
Legendary
*
expert
Offline Offline

Activity: 2492


View Profile
February 10, 2012, 05:02:56 AM
 #3

I think you're sending an empty vector as the checksum, but there should be no checksum field at all. (For 10 more days, at least.)

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
btc_artist
Full Member
***
Offline Offline

Activity: 154


Bitcoin!


View Profile WWW
February 10, 2012, 05:14:28 AM
 #4

Hmmm, am I?

I have the Checksum field in my debugging output, but as far as I can tell, I'm not sending it. In the raw data, I have the 4 bytes for length "55 00 00 00", then it goes directly to version, which is "B4 C3 00 00".

Thanks for the response.

EDIT: It just occurred to me that I might not be using the correct protocol.  I'm using TCP, but I can't find anywhere that it is actually specified.

BTC: 1CDCLDBHbAzHyYUkk1wYHPYmrtDZNhk8zf
LTC: LMS7SqZJnqzxo76iDSEua33WCyYZdjaQoE
theymos
Administrator
Legendary
*
expert
Offline Offline

Activity: 2492


View Profile
February 10, 2012, 05:24:18 AM
 #5

Ah, you're right. I miscounted the bytes.

Bitcoin does use TCP.

Solved the first part of my problem.  I no longer get the "PROCESSMESSAGE MESSAGESTART NOT FOUND" error in the debug.log file

What did you change to fix it?

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
btc_artist
Full Member
***
Offline Offline

Activity: 154


Bitcoin!


View Profile WWW
February 10, 2012, 05:44:07 AM
 #6

Solved the first part of my problem.  I no longer get the "PROCESSMESSAGE MESSAGESTART NOT FOUND" error in the debug.log file

What did you change to fix it?
I made the elementary mistake of sending the message as ASCII characters 0-9A-F representing the hex-encoded message, instead of the actual binary hex message.  After changing that, Bitcoin stopped complaining about "MESSAGESTART NOT FOUND".

I still can't get bitcoin to respond to my message though, and it closes the socket one minute later with the "socket no message in first 60 seconds" error.

I thought after it received my initial "version" message, it would respond in kind?

BTC: 1CDCLDBHbAzHyYUkk1wYHPYmrtDZNhk8zf
LTC: LMS7SqZJnqzxo76iDSEua33WCyYZdjaQoE
btc_artist
Full Member
***
Offline Offline

Activity: 154


Bitcoin!


View Profile WWW
February 10, 2012, 06:12:00 AM
 #7

This is strange... if I duplicate my entire binary "version" message, and and send two copies of it together over the socket, then I get different output in the debug.log file.

Code:
accepted connection 127.0.0.1:52191
...
version message: version 50100, blocks=17777
...
PROCESSMESSAGE MESSAGESTART NOT FOUND
...
socket no message in first 60 seconds, 1 0
disconnecting node 127.0.0.1:52191

Whereas, if I don't duplicate it, I only get:

Code:
accepted connection 127.0.0.1:52191
...
socket no message in first 60 seconds, 1 0
disconnecting node 127.0.0.1:52191

The "version message: version 50100, blocks=17777" is from the message I sent.

Very strange.

EDIT: Yup, if I duplicate the message and send it double, then I immediately start getting version, verack, inv, addr messages back.

BTC: 1CDCLDBHbAzHyYUkk1wYHPYmrtDZNhk8zf
LTC: LMS7SqZJnqzxo76iDSEua33WCyYZdjaQoE
btc_artist
Full Member
***
Offline Offline

Activity: 154


Bitcoin!


View Profile WWW
February 10, 2012, 03:58:09 PM
 #8

Update:  It's not the duplicated message that's making it work.  The full message I'm sending is 105 bytes long.  If I add at least 71 bytes of any data to the end of it then it works. 70 bytes or less and it doesn't work. 71 bytes or more (for a total message length of 176 bytes or more written to the socket) and it works.  The extra data can be anything (letters, numbers, null byte, etc) and it works.

I still can't explain why this is.... Sad

Update #2: Finally figured this out.  There was a bug in my code where some code was getting called twice, which caused the payload length (in the header) to get calculated wrong.  After fixing that, the extra bytes are no longer needed.

Thanks for your help theymos!

BTC: 1CDCLDBHbAzHyYUkk1wYHPYmrtDZNhk8zf
LTC: LMS7SqZJnqzxo76iDSEua33WCyYZdjaQoE
moneyart
Newbie
*
Offline Offline

Activity: 26


View Profile
September 27, 2015, 09:28:09 AM
 #9

Can you explain how you are sending the version message. I also want to do this but I have no clue. Do you use the addnode or connect command?
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!