sys_freebsd.go 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "golang.org/x/net/internal/socket"
  13. )
  14. var (
  15. ctlOpts = [ctlMax]ctlOpt{
  16. ctlTrafficClass: {sysIPV6_TCLASS, 4, marshalTrafficClass, parseTrafficClass},
  17. ctlHopLimit: {sysIPV6_HOPLIMIT, 4, marshalHopLimit, parseHopLimit},
  18. ctlPacketInfo: {sysIPV6_PKTINFO, sizeofInet6Pktinfo, marshalPacketInfo, parsePacketInfo},
  19. ctlNextHop: {sysIPV6_NEXTHOP, sizeofSockaddrInet6, marshalNextHop, parseNextHop},
  20. ctlPathMTU: {sysIPV6_PATHMTU, sizeofIPv6Mtuinfo, marshalPathMTU, parsePathMTU},
  21. }
  22. sockOpts = map[int]sockOpt{
  23. ssoTrafficClass: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_TCLASS, Len: 4}},
  24. ssoHopLimit: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_UNICAST_HOPS, Len: 4}},
  25. ssoMulticastInterface: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_MULTICAST_IF, Len: 4}},
  26. ssoMulticastHopLimit: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_MULTICAST_HOPS, Len: 4}},
  27. ssoMulticastLoopback: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_MULTICAST_LOOP, Len: 4}},
  28. ssoReceiveTrafficClass: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_RECVTCLASS, Len: 4}},
  29. ssoReceiveHopLimit: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_RECVHOPLIMIT, Len: 4}},
  30. ssoReceivePacketInfo: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_RECVPKTINFO, Len: 4}},
  31. ssoReceivePathMTU: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_RECVPATHMTU, Len: 4}},
  32. ssoPathMTU: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_PATHMTU, Len: sizeofIPv6Mtuinfo}},
  33. ssoChecksum: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysIPV6_CHECKSUM, Len: 4}},
  34. ssoICMPFilter: {Option: socket.Option{Level: iana.ProtocolIPv6ICMP, Name: sysICMP6_FILTER, Len: sizeofICMPv6Filter}},
  35. ssoJoinGroup: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysMCAST_JOIN_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
  36. ssoLeaveGroup: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysMCAST_LEAVE_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
  37. ssoJoinSourceGroup: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysMCAST_JOIN_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
  38. ssoLeaveSourceGroup: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysMCAST_LEAVE_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
  39. ssoBlockSourceGroup: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysMCAST_BLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
  40. ssoUnblockSourceGroup: {Option: socket.Option{Level: iana.ProtocolIPv6, Name: sysMCAST_UNBLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: 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. compatFreeBSD32 = true
  49. break
  50. }
  51. }
  52. }
  53. }
  54. func (sa *sockaddrInet6) setSockaddr(ip net.IP, i int) {
  55. sa.Len = sizeofSockaddrInet6
  56. sa.Family = syscall.AF_INET6
  57. copy(sa.Addr[:], ip)
  58. sa.Scope_id = uint32(i)
  59. }
  60. func (pi *inet6Pktinfo) setIfindex(i int) {
  61. pi.Ifindex = uint32(i)
  62. }
  63. func (mreq *ipv6Mreq) setIfindex(i int) {
  64. mreq.Interface = uint32(i)
  65. }
  66. func (gr *groupReq) setGroup(grp net.IP) {
  67. sa := (*sockaddrInet6)(unsafe.Pointer(&gr.Group))
  68. sa.Len = sizeofSockaddrInet6
  69. sa.Family = syscall.AF_INET6
  70. copy(sa.Addr[:], grp)
  71. }
  72. func (gsr *groupSourceReq) setSourceGroup(grp, src net.IP) {
  73. sa := (*sockaddrInet6)(unsafe.Pointer(&gsr.Group))
  74. sa.Len = sizeofSockaddrInet6
  75. sa.Family = syscall.AF_INET6
  76. copy(sa.Addr[:], grp)
  77. sa = (*sockaddrInet6)(unsafe.Pointer(&gsr.Source))
  78. sa.Len = sizeofSockaddrInet6
  79. sa.Family = syscall.AF_INET6
  80. copy(sa.Addr[:], src)
  81. }