sys_freebsd.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2013 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 ipv6
  5. import (
  6. "net"
  7. "runtime"
  8. "strings"
  9. "syscall"
  10. "unsafe"
  11. "golang.org/x/net/internal/iana"
  12. )
  13. type sysSockoptLen int32
  14. var (
  15. ctlOpts = [ctlMax]ctlOpt{
  16. ctlTrafficClass: {sysIPV6_TCLASS, 4, marshalTrafficClass, parseTrafficClass},
  17. ctlHopLimit: {sysIPV6_HOPLIMIT, 4, marshalHopLimit, parseHopLimit},
  18. ctlPacketInfo: {sysIPV6_PKTINFO, sysSizeofInet6Pktinfo, marshalPacketInfo, parsePacketInfo},
  19. ctlNextHop: {sysIPV6_NEXTHOP, sysSizeofSockaddrInet6, marshalNextHop, parseNextHop},
  20. ctlPathMTU: {sysIPV6_PATHMTU, sysSizeofIPv6Mtuinfo, marshalPathMTU, parsePathMTU},
  21. }
  22. sockOpts = [ssoMax]sockOpt{
  23. ssoTrafficClass: {iana.ProtocolIPv6, sysIPV6_TCLASS, ssoTypeInt},
  24. ssoHopLimit: {iana.ProtocolIPv6, sysIPV6_UNICAST_HOPS, ssoTypeInt},
  25. ssoMulticastInterface: {iana.ProtocolIPv6, sysIPV6_MULTICAST_IF, ssoTypeInterface},
  26. ssoMulticastHopLimit: {iana.ProtocolIPv6, sysIPV6_MULTICAST_HOPS, ssoTypeInt},
  27. ssoMulticastLoopback: {iana.ProtocolIPv6, sysIPV6_MULTICAST_LOOP, ssoTypeInt},
  28. ssoReceiveTrafficClass: {iana.ProtocolIPv6, sysIPV6_RECVTCLASS, ssoTypeInt},
  29. ssoReceiveHopLimit: {iana.ProtocolIPv6, sysIPV6_RECVHOPLIMIT, ssoTypeInt},
  30. ssoReceivePacketInfo: {iana.ProtocolIPv6, sysIPV6_RECVPKTINFO, ssoTypeInt},
  31. ssoReceivePathMTU: {iana.ProtocolIPv6, sysIPV6_RECVPATHMTU, ssoTypeInt},
  32. ssoPathMTU: {iana.ProtocolIPv6, sysIPV6_PATHMTU, ssoTypeMTUInfo},
  33. ssoChecksum: {iana.ProtocolIPv6, sysIPV6_CHECKSUM, ssoTypeInt},
  34. ssoICMPFilter: {iana.ProtocolIPv6ICMP, sysICMP6_FILTER, ssoTypeICMPFilter},
  35. ssoJoinGroup: {iana.ProtocolIPv6, sysMCAST_JOIN_GROUP, ssoTypeGroupReq},
  36. ssoLeaveGroup: {iana.ProtocolIPv6, sysMCAST_LEAVE_GROUP, ssoTypeGroupReq},
  37. ssoJoinSourceGroup: {iana.ProtocolIPv6, sysMCAST_JOIN_SOURCE_GROUP, ssoTypeGroupSourceReq},
  38. ssoLeaveSourceGroup: {iana.ProtocolIPv6, sysMCAST_LEAVE_SOURCE_GROUP, ssoTypeGroupSourceReq},
  39. ssoBlockSourceGroup: {iana.ProtocolIPv6, sysMCAST_BLOCK_SOURCE, ssoTypeGroupSourceReq},
  40. ssoUnblockSourceGroup: {iana.ProtocolIPv6, sysMCAST_UNBLOCK_SOURCE, ssoTypeGroupSourceReq},
  41. }
  42. )
  43. func init() {
  44. if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" {
  45. archs, _ := syscall.Sysctl("kern.supported_archs")
  46. for _, s := range strings.Fields(archs) {
  47. if s == "amd64" {
  48. freebsd32o64 = true
  49. break
  50. }
  51. }
  52. }
  53. }
  54. func (sa *sysSockaddrInet6) setSockaddr(ip net.IP, i int) {
  55. sa.Len = sysSizeofSockaddrInet6
  56. sa.Family = syscall.AF_INET6
  57. copy(sa.Addr[:], ip)
  58. sa.Scope_id = uint32(i)
  59. }
  60. func (pi *sysInet6Pktinfo) setIfindex(i int) {
  61. pi.Ifindex = uint32(i)
  62. }
  63. func (mreq *sysIPv6Mreq) setIfindex(i int) {
  64. mreq.Interface = uint32(i)
  65. }
  66. func (gr *sysGroupReq) setGroup(grp net.IP) {
  67. sa := (*sysSockaddrInet6)(unsafe.Pointer(&gr.Group))
  68. sa.Len = sysSizeofSockaddrInet6
  69. sa.Family = syscall.AF_INET6
  70. copy(sa.Addr[:], grp)
  71. }
  72. func (gsr *sysGroupSourceReq) setSourceGroup(grp, src net.IP) {
  73. sa := (*sysSockaddrInet6)(unsafe.Pointer(&gsr.Group))
  74. sa.Len = sysSizeofSockaddrInet6
  75. sa.Family = syscall.AF_INET6
  76. copy(sa.Addr[:], grp)
  77. sa = (*sysSockaddrInet6)(unsafe.Pointer(&gsr.Source))
  78. sa.Len = sysSizeofSockaddrInet6
  79. sa.Family = syscall.AF_INET6
  80. copy(sa.Addr[:], src)
  81. }