Skip to content
quarry Artifact [f69c38f3d2]
Login

Artifact [f69c38f3d2]

Artifact f69c38f3d2ee650cc0c113985eb9e049c25fefb0387a607e81ecc70f38141f14:


package pickupclient

import (
	"fmt"
	"net"
	"strings"
)

// inodeOfConn reads the text of /proc/net/tcp and returns the socket
// inode of the connection whose local end is src and remote end is dst
// (both IPv4). It is here, and not in peer_linux.go, so that it is tested
// wherever the tests run.
func inodeOfConn(procNetTCP string, src, dst *net.TCPAddr) (string, bool) {
	hexAddr := func(a *net.TCPAddr) string {
		ip := a.IP.To4()
		if ip == nil {
			return ""
		}
		return fmt.Sprintf("%02X%02X%02X%02X:%04X", ip[3], ip[2], ip[1], ip[0], a.Port)
	}
	l, r := hexAddr(src), hexAddr(dst)
	if l == "" || r == "" {
		return "", false
	}
	for _, line := range strings.Split(procNetTCP, "\n") {
		f := strings.Fields(line)
		// sl local rem st tx:rx tr:when retrnsmt uid timeout inode
		if len(f) >= 10 && strings.EqualFold(f[1], l) && strings.EqualFold(f[2], r) {
			return f[9], true
		}
	}
	return "", false
}