switch_posix.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build !plan9
  5. package socktest
  6. import (
  7. "fmt"
  8. "syscall"
  9. )
  10. func familyString(family int) string {
  11. switch family {
  12. case syscall.AF_INET:
  13. return "inet4"
  14. case syscall.AF_INET6:
  15. return "inet6"
  16. case syscall.AF_UNIX:
  17. return "local"
  18. default:
  19. return fmt.Sprintf("%d", family)
  20. }
  21. }
  22. func typeString(sotype int) string {
  23. var s string
  24. switch sotype & 0xff {
  25. case syscall.SOCK_STREAM:
  26. s = "stream"
  27. case syscall.SOCK_DGRAM:
  28. s = "datagram"
  29. case syscall.SOCK_RAW:
  30. s = "raw"
  31. case syscall.SOCK_SEQPACKET:
  32. s = "seqpacket"
  33. default:
  34. s = fmt.Sprintf("%d", sotype&0xff)
  35. }
  36. if flags := uint(sotype) & ^uint(0xff); flags != 0 {
  37. s += fmt.Sprintf("|%#x", flags)
  38. }
  39. return s
  40. }
  41. func protocolString(proto int) string {
  42. switch proto {
  43. case 0:
  44. return "default"
  45. case syscall.IPPROTO_TCP:
  46. return "tcp"
  47. case syscall.IPPROTO_UDP:
  48. return "udp"
  49. default:
  50. return fmt.Sprintf("%d", proto)
  51. }
  52. }