Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: support JoinSourceSpecificGroup and LeaveSourceSpecificGroup for… #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ipv4/sockopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
const (
ssoTypeIPMreq = iota + 1
ssoTypeIPMreqn
ssoTypeIPMreqSource
ssoTypeGroupReq
ssoTypeGroupSourceReq
)
Expand Down
7 changes: 6 additions & 1 deletion ipv4/sockopt_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ func (so *sockOpt) setGroup(c *socket.Conn, ifi *net.Interface, grp net.IP) erro
}

func (so *sockOpt) setSourceGroup(c *socket.Conn, ifi *net.Interface, grp, src net.IP) error {
return so.setGroupSourceReq(c, ifi, grp, src)
switch so.typ {
case ssoTypeIPMreqSource:
return so.setIPMreqSource(c, ifi, grp, src)
default:
return so.setGroupSourceReq(c, ifi, grp, src)
}
}

func (so *sockOpt) setBPF(c *socket.Conn, f []bpf.RawInstruction) error {
Expand Down
37 changes: 37 additions & 0 deletions ipv4/sys_asmreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func (so *sockOpt) setIPMreq(c *socket.Conn, ifi *net.Interface, grp net.IP) err
return so.Set(c, b)
}

func (so *sockOpt) setIPMreqSource(c *socket.Conn, ifi *net.Interface, grp, src net.IP) error {
mreqSource := ipMreqSource{
Multiaddr: [4]byte{grp[0], grp[1], grp[2], grp[3]},
Sourceaddr: [4]byte{src[0], src[1], src[2], src[3]},
}
if err := setIPMreqSourceInterface(&mreqSource, ifi); err != nil {
return err
}
b := (*[sizeofIPMreqSource]byte)(unsafe.Pointer(&mreqSource))[:sizeofIPMreqSource]
return so.Set(c, b)
}

func (so *sockOpt) getMulticastIf(c *socket.Conn) (*net.Interface, error) {
var b [4]byte
if _, err := so.Get(c, b[:]); err != nil {
Expand Down Expand Up @@ -72,6 +84,31 @@ func setIPMreqInterface(mreq *ipMreq, ifi *net.Interface) error {
return errNoSuchInterface
}

func setIPMreqSourceInterface(mreqSource *ipMreqSource, ifi *net.Interface) error {
if ifi == nil {
return nil
}
ifat, err := ifi.Addrs()
if err != nil {
return err
}
for _, ifa := range ifat {
switch ifa := ifa.(type) {
case *net.IPAddr:
if ip := ifa.IP.To4(); ip != nil {
copy(mreqSource.Interface[:], ip)
return nil
}
case *net.IPNet:
if ip := ifa.IP.To4(); ip != nil {
copy(mreqSource.Interface[:], ip)
return nil
}
}
}
return errNoSuchInterface
}

func netIP4ToInterface(ip net.IP) (*net.Interface, error) {
ift, err := net.Interfaces()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions ipv4/sys_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ var (
ssoHeaderPrepend: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_HDRINCL, Len: 4}},
ssoJoinGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_ADD_MEMBERSHIP, Len: sizeofIPMreq}, typ: ssoTypeIPMreq},
ssoLeaveGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_DROP_MEMBERSHIP, Len: sizeofIPMreq}, typ: ssoTypeIPMreq},
ssoJoinSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_ADD_SOURCE_MEMBERSHIP, Len: sizeofIPMreqSource}, typ: ssoTypeIPMreqSource},
ssoLeaveSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_DROP_SOURCE_MEMBERSHIP, Len: sizeofIPMreqSource}, typ: ssoTypeIPMreqSource},
}
)

Expand Down