blob: 37752ccc74cb844eb50b6edffa31db93caa8893d [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package netutil
import (
"context"
"fmt"
"net"
"time"
"go.fuchsia.dev/fuchsia/tools/lib/retry"
"go.fuchsia.dev/fuchsia/tools/net/netboot"
constants "go.fuchsia.dev/fuchsia/tools/net/netutilconstants"
)
// GetNodeAddress returns the UDP address corresponding to a given node, specifically
// the netsvc or fuchsia address dependending on the value of `fuchsia`.
func GetNodeAddress(ctx context.Context, nodename string, fuchsia bool) (*net.UDPAddr, error) {
// Retry, as the netstack might not yet be up.
var addr *net.UDPAddr
var err error
n := netboot.NewClient(time.Second)
err = retry.Retry(ctx, retry.WithMaxDuration(&retry.ZeroBackoff{}, time.Minute), func() error {
addr, err = n.Discover(ctx, nodename, fuchsia)
return err
}, nil)
if err != nil {
return nil, fmt.Errorf("%s %q: %v", constants.CannotFindNodeErrMsg, nodename, err)
}
return addr, nil
}
// GetAdvertisement returns the netsvc address for the given node along with
// the advertisement message sent from the node.
func GetAdvertisement(ctx context.Context, nodename string) (*net.UDPAddr, *netboot.Advertisement, *net.UDPConn, error) {
// Retry, as the netstack might not yet be up.
var addr *net.UDPAddr
var msg *netboot.Advertisement
var conn *net.UDPConn
var err error
n := netboot.NewClient(time.Second)
err = retry.Retry(ctx, retry.WithMaxDuration(retry.NewConstantBackoff(5*time.Second), time.Minute), func() error {
addr, msg, conn, err = n.BeaconForNodename(ctx, nodename, nodename != netboot.NodenameWildcard)
return err
}, nil)
if err != nil {
return addr, msg, conn, fmt.Errorf("%s %q: %v", constants.CannotFindNodeErrMsg, nodename, err)
}
return addr, msg, conn, nil
}