2020-05-30 14:29:47 +00:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2019-12-03 18:49:52 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ipv4
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"golang.org/x/net/internal/iana"
|
|
|
|
"golang.org/x/net/internal/socket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ctlOpts = [ctlMax]ctlOpt{
|
2020-05-30 14:29:47 +00:00
|
|
|
ctlTTL: {sysIP_RECVTTL, 1, marshalTTL, parseTTL},
|
|
|
|
ctlDst: {sysIP_RECVDSTADDR, net.IPv4len, marshalDst, parseDst},
|
|
|
|
ctlInterface: {sysIP_RECVIF, syscall.SizeofSockaddrDatalink, marshalInterface, parseInterface},
|
2019-12-03 18:49:52 +00:00
|
|
|
ctlPacketInfo: {sysIP_PKTINFO, sizeofInetPktinfo, marshalPacketInfo, parsePacketInfo},
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:29:47 +00:00
|
|
|
sockOpts = map[int]*sockOpt{
|
2019-12-03 18:49:52 +00:00
|
|
|
ssoTOS: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_TOS, Len: 4}},
|
|
|
|
ssoTTL: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_TTL, Len: 4}},
|
|
|
|
ssoMulticastTTL: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_TTL, Len: 1}},
|
2020-05-30 14:29:47 +00:00
|
|
|
ssoMulticastInterface: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_IF, Len: sizeofIPMreqn}, typ: ssoTypeIPMreqn},
|
|
|
|
ssoMulticastLoopback: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_LOOP, Len: 4}},
|
2019-12-03 18:49:52 +00:00
|
|
|
ssoReceiveTTL: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVTTL, Len: 4}},
|
2020-05-30 14:29:47 +00:00
|
|
|
ssoReceiveDst: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVDSTADDR, Len: 4}},
|
|
|
|
ssoReceiveInterface: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVIF, Len: 4}},
|
2019-12-03 18:49:52 +00:00
|
|
|
ssoHeaderPrepend: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_HDRINCL, Len: 4}},
|
2020-05-30 14:29:47 +00:00
|
|
|
ssoStripHeader: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_STRIPHDR, Len: 4}},
|
2019-12-03 18:49:52 +00:00
|
|
|
ssoJoinGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_JOIN_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
|
|
|
|
ssoLeaveGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_LEAVE_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
|
|
|
|
ssoJoinSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_JOIN_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
|
|
|
|
ssoLeaveSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_LEAVE_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
|
|
|
|
ssoBlockSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_BLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
|
|
|
|
ssoUnblockSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_UNBLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
|
2020-05-30 14:29:47 +00:00
|
|
|
ssoPacketInfo: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVPKTINFO, Len: 4}},
|
2019-12-03 18:49:52 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (pi *inetPktinfo) setIfindex(i int) {
|
|
|
|
pi.Ifindex = uint32(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gr *groupReq) setGroup(grp net.IP) {
|
|
|
|
sa := (*sockaddrInet)(unsafe.Pointer(uintptr(unsafe.Pointer(gr)) + 4))
|
2020-05-30 14:29:47 +00:00
|
|
|
sa.Len = sizeofSockaddrInet
|
2019-12-03 18:49:52 +00:00
|
|
|
sa.Family = syscall.AF_INET
|
|
|
|
copy(sa.Addr[:], grp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gsr *groupSourceReq) setSourceGroup(grp, src net.IP) {
|
|
|
|
sa := (*sockaddrInet)(unsafe.Pointer(uintptr(unsafe.Pointer(gsr)) + 4))
|
2020-05-30 14:29:47 +00:00
|
|
|
sa.Len = sizeofSockaddrInet
|
2019-12-03 18:49:52 +00:00
|
|
|
sa.Family = syscall.AF_INET
|
|
|
|
copy(sa.Addr[:], grp)
|
2020-05-30 14:29:47 +00:00
|
|
|
sa = (*sockaddrInet)(unsafe.Pointer(uintptr(unsafe.Pointer(gsr)) + 132))
|
|
|
|
sa.Len = sizeofSockaddrInet
|
2019-12-03 18:49:52 +00:00
|
|
|
sa.Family = syscall.AF_INET
|
|
|
|
copy(sa.Addr[:], src)
|
|
|
|
}
|