anacrolix / dht

dht is used by anacrolix/torrent, and is intended for use as a library in other projects both torrent related and otherwise
Mozilla Public License 2.0
313 stars 66 forks source link

add IP.To4 mapping to CompactIPv4NodeAddrs.MarshalBinary #54

Closed yarikk closed 2 years ago

yarikk commented 2 years ago

CompactIPv4NodeAddrs.MarshalBinary can not serialise results of net.IPv4 directly, requiring additional To4 call.

The included test flags the issue:

--- FAIL: TestMarshalCompactIPv4NodeAddrs (0.00s)
panic: marshalled 18 bytes, but expected 6 [recovered]
    panic: marshalled 18 bytes, but expected 6

The proposed fix is to take same approach as in CompactIPv4NodeInfo.MarshalBinary.

Fixes #33.

anacrolix commented 2 years ago

Looks good. This was the case I'm worried about, but it fails as I hoped. Does that make sense? If so I'll merge.

yarikk commented 2 years ago

Let me rebase and add a length check as a condition for the conversion.

Update: no need for length check: that's part of IP.To4 already.

yarikk commented 2 years ago

Rebased, added a boilerplate "if" (based on To4 usage examples in the Go's net packages itself), added panic tests.