2019-02-25 22:44:14 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/wire/protocol"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/wire/util"
|
|
|
|
)
|
|
|
|
|
2019-03-17 18:26:35 +00:00
|
|
|
//NetAddr is an abstraction for the IP layer
|
|
|
|
type NetAddr struct {
|
2019-02-25 22:44:14 +00:00
|
|
|
Timestamp uint32
|
|
|
|
IP [16]byte
|
|
|
|
Port uint16
|
|
|
|
Service protocol.ServiceFlag
|
|
|
|
}
|
|
|
|
|
2019-03-17 18:26:35 +00:00
|
|
|
//NewNetAddr returns a NetAddr object
|
|
|
|
func NewNetAddr(time uint32, ip [16]byte, port uint16, service protocol.ServiceFlag) (*NetAddr, error) {
|
|
|
|
return &NetAddr{time, ip, port, service}, nil
|
2019-02-25 22:44:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 18:26:35 +00:00
|
|
|
//NewAddrFromVersionMessage returns a NetAddr object from a version message
|
|
|
|
func NewAddrFromVersionMessage(version VersionMessage) (*NetAddr, error) {
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
var ip [16]byte
|
|
|
|
|
|
|
|
copy(ip[:], []byte(version.IP)[:16])
|
|
|
|
|
|
|
|
return NewNetAddr(version.Timestamp, ip, version.Port, version.Services)
|
|
|
|
}
|
|
|
|
|
2019-03-17 18:26:35 +00:00
|
|
|
// EncodePayload Implements messager interface
|
|
|
|
func (n *NetAddr) EncodePayload(bw *util.BinWriter) {
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
bw.Write(uint32(time.Now().Unix()))
|
|
|
|
bw.Write(protocol.NodePeerService)
|
|
|
|
bw.WriteBigEnd(n.IP)
|
|
|
|
bw.WriteBigEnd(n.Port)
|
|
|
|
}
|
2019-03-17 18:26:35 +00:00
|
|
|
|
|
|
|
// DecodePayload Implements Messager interface
|
|
|
|
func (n *NetAddr) DecodePayload(br *util.BinReader) {
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
br.Read(&n.Timestamp)
|
|
|
|
br.Read(&n.Service)
|
|
|
|
br.ReadBigEnd(&n.IP)
|
|
|
|
br.ReadBigEnd(&n.Port)
|
|
|
|
}
|
2019-03-17 18:26:35 +00:00
|
|
|
|
|
|
|
//IPPort returns the IPPort from the NetAddr
|
|
|
|
func (n *NetAddr) IPPort() string {
|
2019-02-25 22:44:14 +00:00
|
|
|
ip := net.IP(n.IP[:]).String()
|
|
|
|
port := strconv.Itoa(int(n.Port))
|
|
|
|
ipport := ip + ":" + port
|
|
|
|
return ipport
|
|
|
|
}
|