github-code-scanning[bot] commented on code in PR #7096:
URL: https://github.com/apache/trafficcontrol/pull/7096#discussion_r982922207


##########
tc-health-client/sar/multisar.go:
##########
@@ -0,0 +1,388 @@
+// 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"
+       "net"
+       "os"
+       "sync"
+       "time"
+
+       "github.com/apache/trafficcontrol/lib/go-llog"
+)
+
+type HostPort struct {
+       Host string
+       Port int
+}
+
+type SARResult struct {
+       Host string
+       Port int
+       RTT  time.Duration
+       Err  error
+}
+
+// MultiSAR is like SAR for multiple requests.
+// SAR has to listen on a raw IP port without a TCP socket, which is 
relatively inexpensive for a single request,
+// but expensive for large numbers of requests.
+// MultiSAR uses a single listener on an ephemeral local port for all SAR 
requests, significantly reducing
+// resource costs.
+func MultiSAR(log llog.Log, hosts []HostPort, timeout time.Duration) 
([]SARResult, error) {
+       log = llog.LibInit(log)
+
+       localAddrStr, err := GetLocalAddr()
+       if err != nil {
+               return nil, errors.New("getting local address: " + err.Error())
+       }
+
+       localAddr := net.ParseIP(localAddrStr)
+       if localAddr == nil {
+               return nil, 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 nil, errors.New("failed to listen on ephemeral port: " + 
err.Error())
+       }
+       defer ephemeralPortHolder.Close()
+
+       srcPort := ephemeralPortHolder.Port()
+
+       // pre-construct all the packets, so we listen for as little time as 
possible
+
+       // TODO implement Initial Sequence Number ISN per RFC9293ยง3.4.1? It 
might be faster to use the same seq num for all packets
+       seqNum := uint32(42)
+
+       type HostPortPacket struct {
+               HostPort
+               TCPHdr TCPHdr
+       }
+
+       packets := []HostPortPacket{}
+
+       hostAddr := map[string]string{}    // map[host]addr - note hosts may be 
IPs, in which case host and addr will be the same
+       addrHosts := map[string][]string{} // map[addr][]host - note multiple 
FQDNs may have the same IP
+
+       results := []SARResult{}
+
+       for _, host := range hosts {
+               makeHostErrResult := func(err error) SARResult {
+                       return SARResult{
+                               Host: host.Host,
+                               Port: host.Port,
+                               RTT:  0,
+                               Err:  err,
+                       }
+               }
+
+               remoteAddr := net.ParseIP(host.Host)
+               if remoteAddr != nil {
+                       // host is IP
+                       hostAddr[host.Host] = host.Host
+                       addrHosts[host.Host] = append(addrHosts[host.Host], 
host.Host)
+               } else {
+                       // host isn't an IP, assume FQDN
+                       addrs, err := net.LookupHost(host.Host)
+                       if err != nil {
+                               results = append(results, 
makeHostErrResult(errors.New("lookup up host '"+host.Host+"': "+err.Error())))
+                               continue
+                       }
+                       if len(addrs) == 0 {
+                               results = append(results, 
makeHostErrResult(errors.New("looking up host '"+host.Host+"' succeeded, but no 
addresses were found.")))
+                               continue
+                       }
+                       remoteAddr = net.ParseIP(addrs[0])
+                       if remoteAddr == nil {
+                               results = append(results, 
makeHostErrResult(errors.New("failed to parse addr '"+host.Host+"' ip 
'"+addrs[0]+"' as IP")))
+                               continue
+                       }
+
+                       hostAddr[host.Host] = addrs[0]
+                       addrHosts[addrs[0]] = append(addrHosts[addrs[0]], 
host.Host)
+               }
+
+               if v4 := remoteAddr.To4(); v4 != nil {
+                       remoteAddr = v4
+               }
+
+               // TODO handle IPv6
+
+               window := 256 * 10
+               destPort := host.Port
+               dataOffset := 5 // because we have no options?
+               native := TCPHdrNative{
+                       SrcPort:    uint16(srcPort),

Review Comment:
   ## Incorrect conversion between integer types
   
   Incorrect conversion of an integer with architecture-dependent bit size from 
[strconv.Atoi](1) to a lower bit size type uint16 without an upper bound check.
   
   [Show more 
details](https://github.com/apache/trafficcontrol/security/code-scanning/228)



-- 
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: issues-unsubscr...@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to