[Issue #1] - Added configurable NTPServer option in config.go; also added DNS name resolution so you don't have to use IPs
This commit is contained in:
+31
@@ -225,3 +225,34 @@ func tcpBackoff(consecutiveBackoffs uint) time.Duration {
|
||||
wait := min(shifted, maxWait)
|
||||
return time.Duration(wait)
|
||||
}
|
||||
|
||||
// resolveHostToIP checks if host is already an IP literal, or queries the DNS server.
|
||||
func resolveHostToIP(stack *NetworkStack, host string, timeout time.Duration) (netip.Addr, error) {
|
||||
// 1. If it's already an IP string (e.g. "162.159.200.1"), parse and return directly
|
||||
if ip, err := netip.ParseAddr(host); err == nil {
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
// 2. Start asynchronous DNS A-record query
|
||||
if err := stack.stack.StartLookupIP(host); err != nil {
|
||||
return netip.Addr{}, fmt.Errorf("start DNS lookup: %w", err)
|
||||
}
|
||||
|
||||
// 3. Poll ResultLookupIP until done or timed out
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
addrs, done, err := stack.stack.ResultLookupIP(host)
|
||||
if done {
|
||||
if err != nil {
|
||||
return netip.Addr{}, fmt.Errorf("DNS lookup error: %w", err)
|
||||
}
|
||||
if len(addrs) == 0 {
|
||||
return netip.Addr{}, fmt.Errorf("no addresses returned for %s", host)
|
||||
}
|
||||
return addrs[0], nil
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
|
||||
return netip.Addr{}, fmt.Errorf("DNS lookup timed out for %s", host)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user