2018-03-09 15:55:25 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
2019-09-09 14:54:38 +00:00
|
|
|
"net"
|
2019-09-13 09:03:07 +00:00
|
|
|
"strconv"
|
2018-03-09 15:55:25 +00:00
|
|
|
"time"
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-03-09 15:55:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddressAndTime payload.
|
|
|
|
type AddressAndTime struct {
|
|
|
|
Timestamp uint32
|
|
|
|
Services uint64
|
2019-09-09 14:54:38 +00:00
|
|
|
IP [16]byte
|
|
|
|
Port uint16
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAddressAndTime creates a new AddressAndTime object.
|
2019-09-09 14:54:38 +00:00
|
|
|
func NewAddressAndTime(e *net.TCPAddr, t time.Time) *AddressAndTime {
|
|
|
|
aat := AddressAndTime{
|
2018-03-09 15:55:25 +00:00
|
|
|
Timestamp: uint32(t.UTC().Unix()),
|
|
|
|
Services: 1,
|
2019-09-09 14:54:38 +00:00
|
|
|
Port: uint16(e.Port),
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2019-09-09 14:54:38 +00:00
|
|
|
copy(aat.IP[:], e.IP)
|
|
|
|
return &aat
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (p *AddressAndTime) DecodeBinary(br *io.BinReader) {
|
2019-08-28 12:43:56 +00:00
|
|
|
br.ReadLE(&p.Timestamp)
|
|
|
|
br.ReadLE(&p.Services)
|
2019-09-09 14:54:38 +00:00
|
|
|
br.ReadBE(&p.IP)
|
|
|
|
br.ReadBE(&p.Port)
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (p *AddressAndTime) EncodeBinary(bw *io.BinWriter) {
|
2019-08-28 12:43:56 +00:00
|
|
|
bw.WriteLE(p.Timestamp)
|
|
|
|
bw.WriteLE(p.Services)
|
2019-12-06 15:22:21 +00:00
|
|
|
bw.WriteBytes(p.IP[:])
|
2019-09-09 14:54:38 +00:00
|
|
|
bw.WriteBE(p.Port)
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 09:03:07 +00:00
|
|
|
// IPPortString makes a string from IP and port specified.
|
|
|
|
func (p *AddressAndTime) IPPortString() string {
|
2019-10-17 09:19:41 +00:00
|
|
|
var netip = make(net.IP, 16)
|
2019-09-13 09:03:07 +00:00
|
|
|
|
|
|
|
copy(netip, p.IP[:])
|
|
|
|
port := strconv.Itoa(int(p.Port))
|
|
|
|
return netip.String() + ":" + port
|
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
// AddressList is a list with AddrAndTime.
|
|
|
|
type AddressList struct {
|
|
|
|
Addrs []*AddressAndTime
|
|
|
|
}
|
|
|
|
|
2019-09-13 17:38:34 +00:00
|
|
|
// NewAddressList creates a list for n AddressAndTime elements.
|
|
|
|
func NewAddressList(n int) *AddressList {
|
|
|
|
alist := AddressList{
|
|
|
|
Addrs: make([]*AddressAndTime, n),
|
|
|
|
}
|
|
|
|
return &alist
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (p *AddressList) DecodeBinary(br *io.BinReader) {
|
2019-11-14 07:50:03 +00:00
|
|
|
br.ReadArray(&p.Addrs)
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (p *AddressList) EncodeBinary(bw *io.BinWriter) {
|
2019-11-13 07:36:29 +00:00
|
|
|
bw.WriteArray(p.Addrs)
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|