rob05c commented on code in PR #7096: URL: https://github.com/apache/trafficcontrol/pull/7096#discussion_r982915522
########## tc-health-client/sar/sar.go: ########## @@ -0,0 +1,348 @@ +// package sar implements a syn-ack-rst health ping. +// It sends a TCP SYN, waits for an ACK, then immediately sends an RST to kill the connection. +// The primary purpose of this is as a health check, to verify the remote host is reachable, and able and willing to respond. +package sar + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "errors" + "fmt" + "net" + "strings" + "sync" + "time" + + "github.com/apache/trafficcontrol/lib/go-llog" +) + +// SAR calls SARAddr if host is an IP address, or SARHost if host is an FQDN. +func SAR(log llog.Log, host string, port int, timeout time.Duration) (time.Duration, error) { + log = llog.LibInit(log) + if ip := net.ParseIP(host); ip != nil { + return SARAddr(log, host, port, timeout) + } + return SARHost(log, host, port, timeout) +} + +// SARAddr sends a syn-ack-reset to the given addr. +// The addr must be an IP. +// On success, the round-trip time to receive the Ack is returned, with a nil error. +// The addr is a string representation of an IPv4 or IPv6 address. +// TODO add optional local addr param +func SARAddr(log llog.Log, addr string, port int, timeout time.Duration) (time.Duration, error) { + log = llog.LibInit(log) + remoteAddr := net.ParseIP(addr) + if remoteAddr == nil { + return 0, errors.New("failed to parse addr '" + addr + "' as IP") + } + if v4 := remoteAddr.To4(); v4 != nil { + remoteAddr = v4 + } + + localAddrStr, err := GetLocalAddr() + if err != nil { + return 0, errors.New("getting local address: " + err.Error()) + } + + localAddr := net.ParseIP(localAddrStr) + if localAddr == nil { + return 0, errors.New("failed to parse local addr '" + localAddrStr + "' as IP") + } + if v4 := localAddr.To4(); v4 != nil { + localAddr = v4 + } + + ephemeralPortHolder, err := GetAndHoldEphemeralPort(localAddrStr) + if err != nil { + return 0, errors.New("failed to listen on ephemeral port: " + err.Error()) + } + defer ephemeralPortHolder.Close() + + srcPort := ephemeralPortHolder.Port() + + // TODO implement Initial Sequence Number ISN per RFC9293ยง3.4.1 + seqNum := uint32(42) + window := 256 * 10 + destPort := port + dataOffset := 5 // because we have no options? + + native := TCPHdrNative{ + SrcPort: uint16(srcPort), Review Comment: False positive, bounds are checked in GetAndHoldEphemeralPort. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
