sys_freebsd.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2014 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. package ipv4
  5. import (
  6. "net"
  7. "runtime"
  8. "strings"
  9. "syscall"
  10. "unsafe"
  11. "golang.org/x/net/internal/iana"
  12. "golang.org/x/net/internal/socket"
  13. )
  14. var (
  15. ctlOpts = [ctlMax]ctlOpt{
  16. ctlTTL: {sysIP_RECVTTL, 1, marshalTTL, parseTTL},
  17. ctlDst: {sysIP_RECVDSTADDR, net.IPv4len, marshalDst, parseDst},
  18. ctlInterface: {sysIP_RECVIF, syscall.SizeofSockaddrDatalink, marshalInterface, parseInterface},
  19. }
  20. sockOpts = map[int]*sockOpt{
  21. ssoTOS: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_TOS, Len: 4}},
  22. ssoTTL: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_TTL, Len: 4}},
  23. ssoMulticastTTL: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_TTL, Len: 1}},
  24. ssoMulticastInterface: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_IF, Len: 4}},
  25. ssoMulticastLoopback: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_LOOP, Len: 4}},
  26. ssoReceiveTTL: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVTTL, Len: 4}},
  27. ssoReceiveDst: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVDSTADDR, Len: 4}},
  28. ssoReceiveInterface: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVIF, Len: 4}},
  29. ssoHeaderPrepend: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_HDRINCL, Len: 4}},
  30. ssoJoinGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_JOIN_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
  31. ssoLeaveGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_LEAVE_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
  32. ssoJoinSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_JOIN_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
  33. ssoLeaveSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_LEAVE_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
  34. ssoBlockSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_BLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
  35. ssoUnblockSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_UNBLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
  36. }
  37. )
  38. func init() {
  39. freebsdVersion, _ = syscall.SysctlUint32("kern.osreldate")
  40. if freebsdVersion >= 1000000 {
  41. sockOpts[ssoMulticastInterface] = &sockOpt{Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_IF, Len: sizeofIPMreqn}, typ: ssoTypeIPMreqn}
  42. }
  43. if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" {
  44. archs, _ := syscall.Sysctl("kern.supported_archs")
  45. for _, s := range strings.Fields(archs) {
  46. if s == "amd64" {
  47. freebsd32o64 = true
  48. break
  49. }
  50. }
  51. }
  52. }
  53. func (gr *groupReq) setGroup(grp net.IP) {
  54. sa := (*sockaddrInet)(unsafe.Pointer(&gr.Group))
  55. sa.Len = sizeofSockaddrInet
  56. sa.Family = syscall.AF_INET
  57. copy(sa.Addr[:], grp)
  58. }
  59. func (gsr *groupSourceReq) setSourceGroup(grp, src net.IP) {
  60. sa := (*sockaddrInet)(unsafe.Pointer(&gsr.Group))
  61. sa.Len = sizeofSockaddrInet
  62. sa.Family = syscall.AF_INET
  63. copy(sa.Addr[:], grp)
  64. sa = (*sockaddrInet)(unsafe.Pointer(&gsr.Source))
  65. sa.Len = sizeofSockaddrInet
  66. sa.Family = syscall.AF_INET
  67. copy(sa.Addr[:], src)
  68. }