Surprising length calculation
thomas-mangin opened this issue · 2 comments
thomas-mangin commented
// Serialize serializes the handshake to a buffer
func (h *Handshake) Serialize() []byte {
buf := make([]byte, len(h.Pstr)+49)
...
}
Len(h.Ptr)
is always >=1, the string being 19 bytes, so it will never be a problem but reading your article I would just have expected 50. 1 + 19 + 20 + 20. This allocates 19 + 49 bytes.
veggiedefender commented
It's been a while since I've thought about this project, so you may very well be correct. My understanding of where len(h.Pstr) + 49
comes from is the fact that the handshake has:
- 1 byte for pstrlen
len(h.Pstr)
bytes for the actual pstr- 8 bytes reserved for extensions
- 20 bytes for infohash
- 20 bytes for peer id
So we always have 1 + 8 + 20 + 20 = 49 bytes, plus the variable sized pstr. So the total buffer should be len(h.Pstr) + 49
bytes long. Does that make sense?
thomas-mangin commented
Reading things late :-)