github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/p2p/netutil/netstat.go (about)

     1  // +build linux windows
     2  
     3  package netutil
     4  
     5  import (
     6  	"github.com/cakturk/go-netstat/netstat"
     7  )
     8  
     9  func UdpPortListeners(port int) (processes map[int]string, err error) {
    10  	var binds []netstat.SockTabEntry
    11  	processes = make(map[int]string)
    12  
    13  	for _, socks := range []func(netstat.AcceptFn) ([]netstat.SockTabEntry, error){
    14  		netstat.UDPSocks,
    15  		netstat.UDP6Socks,
    16  	} {
    17  		binds, err = socks(func(se *netstat.SockTabEntry) bool {
    18  			return int(se.LocalAddr.Port) == port
    19  		})
    20  		if err != nil {
    21  			continue
    22  		}
    23  
    24  		for _, listener := range binds {
    25  			processes[listener.Process.Pid] = listener.Process.Name
    26  		}
    27  	}
    28  	return
    29  }