sys_freebsd.go 2.3 KB

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