選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

net_linux.go 761B

12345678910111213141516171819202122232425262728293031
  1. // +build linux
  2. package utils
  3. import (
  4. "fmt"
  5. "net"
  6. "syscall"
  7. )
  8. // Output a description of a connection that can identify it to other systems
  9. // administration tools.
  10. func DescribeConn(c net.Conn) (description string) {
  11. description = "<error>"
  12. switch conn := c.(type) {
  13. case *net.UnixConn:
  14. f, err := conn.File()
  15. if err != nil {
  16. return
  17. }
  18. defer f.Close()
  19. ucred, err := syscall.GetsockoptUcred(int(f.Fd()), syscall.SOL_SOCKET, syscall.SO_PEERCRED)
  20. if err != nil {
  21. return
  22. }
  23. return fmt.Sprintf("%s <-> %s [pid=%d, uid=%d]", conn.LocalAddr().String(), conn.RemoteAddr().String(), ucred.Pid, ucred.Uid)
  24. default:
  25. // *net.TCPConn or *tls.Conn
  26. return fmt.Sprintf("%s <-> %s", conn.LocalAddr().String(), conn.RemoteAddr().String())
  27. }
  28. }