github.com/anacrolix/torrent@v1.61.0/peer_protocol/compactip.go (about)

     1  package peer_protocol
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/anacrolix/torrent/bencode"
     7  )
     8  
     9  // Marshals to the smallest compact byte representation.
    10  type CompactIp net.IP
    11  
    12  var _ bencode.Marshaler = CompactIp{}
    13  
    14  func (me CompactIp) MarshalBencode() ([]byte, error) {
    15  	return bencode.Marshal(func() []byte {
    16  		if ip4 := net.IP(me).To4(); ip4 != nil {
    17  			return ip4
    18  		} else {
    19  			return me
    20  		}
    21  	}())
    22  }