Add NodeInfo field to PeerEntry and PeerInfo structures, and update related handlers to include NodeInfo in peer data retrieval and handshake processes.

This commit is contained in:
Andy Oknen 2025-08-15 15:17:49 +00:00
parent c0a9bc802a
commit 1f8f36860f
4 changed files with 63 additions and 3 deletions

View file

@ -64,9 +64,10 @@ type link struct {
linkType linkType // Type of link, i.e. outbound/inbound, persistent/ephemeral
linkProto string // Protocol carrier of link, e.g. TCP, AWDL
// The remaining fields can only be modified safely from within the links actor
_conn *linkConn // Connected link, if any, nil if not connected
_err error // Last error on the connection, if any
_errtime time.Time // Last time an error occurred
_conn *linkConn // Connected link, if any, nil if not connected
_err error // Last error on the connection, if any
_errtime time.Time // Last time an error occurred
_nodeInfo []byte // NodeInfo received from peer during handshake
}
type linkOptions struct {
@ -246,6 +247,7 @@ func (l *links) add(u *url.URL, sintf string, linkType linkType) error {
linkType: linkType,
linkProto: strings.ToUpper(u.Scheme),
kick: make(chan struct{}),
_nodeInfo: nil, // Initialize NodeInfo field
}
state.ctx, state.cancel = context.WithCancel(l.core.ctx)
@ -524,6 +526,7 @@ func (l *links) listen(u *url.URL, sintf string, local bool) (*Listener, error)
linkType: linkTypeIncoming,
linkProto: strings.ToUpper(u.Scheme),
kick: make(chan struct{}),
_nodeInfo: nil, // Initialize NodeInfo field
}
}
if state._conn != nil {
@ -605,6 +608,16 @@ func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, s
meta := version_getBaseMetadata()
meta.publicKey = l.core.public
meta.priority = options.priority
// Add our NodeInfo to handshake if available
phony.Block(&l.core.proto.nodeinfo, func() {
nodeInfo := l.core.proto.nodeinfo._getNodeInfo()
if len(nodeInfo) > 0 {
meta.nodeInfo = make([]byte, len(nodeInfo))
copy(meta.nodeInfo, nodeInfo)
}
})
metaBytes, err := meta.encode(l.core.secret, options.password)
if err != nil {
return fmt.Errorf("failed to generate handshake: %w", err)
@ -661,6 +674,20 @@ func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, s
}
}
// Store the received NodeInfo in the link state
if len(meta.nodeInfo) > 0 {
phony.Block(l, func() {
// Find the link state for this connection
for _, state := range l._links {
if state._conn != nil && state._conn.Conn == conn {
state._nodeInfo = make([]byte, len(meta.nodeInfo))
copy(state._nodeInfo, meta.nodeInfo)
break
}
}
})
}
dir := "outbound"
if linkType == linkTypeIncoming {
dir = "inbound"