1
2
3
4
5 package ipv4
6
7 import (
8 "net"
9 "runtime"
10 "strings"
11 "syscall"
12 "unsafe"
13
14 "golang.org/x/net/internal/iana"
15 "golang.org/x/net/internal/socket"
16
17 "golang.org/x/sys/unix"
18 )
19
20 const sockoptReceiveInterface = unix.IP_RECVIF
21
22 var (
23 ctlOpts = [ctlMax]ctlOpt{
24 ctlTTL: {unix.IP_RECVTTL, 1, marshalTTL, parseTTL},
25 ctlDst: {unix.IP_RECVDSTADDR, net.IPv4len, marshalDst, parseDst},
26 ctlInterface: {unix.IP_RECVIF, syscall.SizeofSockaddrDatalink, marshalInterface, parseInterface},
27 }
28
29 sockOpts = map[int]*sockOpt{
30 ssoTOS: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_TOS, Len: 4}},
31 ssoTTL: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_TTL, Len: 4}},
32 ssoMulticastTTL: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_MULTICAST_TTL, Len: 1}},
33 ssoMulticastInterface: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_MULTICAST_IF, Len: 4}},
34 ssoMulticastLoopback: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_MULTICAST_LOOP, Len: 4}},
35 ssoReceiveTTL: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_RECVTTL, Len: 4}},
36 ssoReceiveDst: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_RECVDSTADDR, Len: 4}},
37 ssoReceiveInterface: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_RECVIF, Len: 4}},
38 ssoHeaderPrepend: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_HDRINCL, Len: 4}},
39 ssoJoinGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.MCAST_JOIN_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
40 ssoLeaveGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.MCAST_LEAVE_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
41 ssoJoinSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.MCAST_JOIN_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
42 ssoLeaveSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.MCAST_LEAVE_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
43 ssoBlockSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.MCAST_BLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
44 ssoUnblockSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: unix.MCAST_UNBLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
45 }
46 )
47
48 func init() {
49 freebsdVersion, _ = syscall.SysctlUint32("kern.osreldate")
50 if freebsdVersion >= 1000000 {
51 sockOpts[ssoMulticastInterface] = &sockOpt{Option: socket.Option{Level: iana.ProtocolIP, Name: unix.IP_MULTICAST_IF, Len: unix.SizeofIPMreqn}, typ: ssoTypeIPMreqn}
52 }
53 if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" {
54 archs, _ := syscall.Sysctl("kern.supported_archs")
55 for _, s := range strings.Fields(archs) {
56 if s == "amd64" {
57 compatFreeBSD32 = true
58 break
59 }
60 }
61 }
62 }
63
64 func (gr *groupReq) setGroup(grp net.IP) {
65 sa := (*sockaddrInet)(unsafe.Pointer(&gr.Group))
66 sa.Len = sizeofSockaddrInet
67 sa.Family = syscall.AF_INET
68 copy(sa.Addr[:], grp)
69 }
70
71 func (gsr *groupSourceReq) setSourceGroup(grp, src net.IP) {
72 sa := (*sockaddrInet)(unsafe.Pointer(&gsr.Group))
73 sa.Len = sizeofSockaddrInet
74 sa.Family = syscall.AF_INET
75 copy(sa.Addr[:], grp)
76 sa = (*sockaddrInet)(unsafe.Pointer(&gsr.Source))
77 sa.Len = sizeofSockaddrInet
78 sa.Family = syscall.AF_INET
79 copy(sa.Addr[:], src)
80 }
81
View as plain text