Commit b3982904 authored by Mikio Hara's avatar Mikio Hara

ipv4: deduplicate socket option code

Change-Id: If22f722cf87f7e5833b9c9aea0a078db83f37c26
Reviewed-on: https://go-review.googlesource.com/27734Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 7722dc0a
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd netbsd openbsd
// +build darwin dragonfly freebsd netbsd openbsd windows
package ipv4
......
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ipv4
import (
"net"
"os"
"syscall"
"unsafe"
"golang.org/x/net/internal/iana"
)
func setsockoptIPMreq(s uintptr, name int, ifi *net.Interface, grp net.IP) error {
mreq := sysIPMreq{Multiaddr: [4]byte{grp[0], grp[1], grp[2], grp[3]}}
if err := setIPMreqInterface(&mreq, ifi); err != nil {
return err
}
return os.NewSyscallError("setsockopt", syscall.Setsockopt(syscall.Handle(s), iana.ProtocolIP, int32(name), (*byte)(unsafe.Pointer(&mreq)), int32(sysSizeofIPMreq)))
}
func getsockoptInterface(s uintptr, name int) (*net.Interface, error) {
var b [4]byte
l := int32(4)
if err := syscall.Getsockopt(syscall.Handle(s), iana.ProtocolIP, int32(name), (*byte)(unsafe.Pointer(&b[0])), &l); err != nil {
return nil, os.NewSyscallError("getsockopt", err)
}
ifi, err := netIP4ToInterface(net.IPv4(b[0], b[1], b[2], b[3]))
if err != nil {
return nil, err
}
return ifi, nil
}
func setsockoptInterface(s uintptr, name int, ifi *net.Interface) error {
ip, err := netInterfaceToIP4(ifi)
if err != nil {
return err
}
var b [4]byte
copy(b[:], ip)
return os.NewSyscallError("setsockopt", syscall.Setsockopt(syscall.Handle(s), iana.ProtocolIP, int32(name), (*byte)(unsafe.Pointer(&b[0])), 4))
}
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux netbsd openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd windows
package ipv4
......
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ipv4
import (
"net"
"os"
"syscall"
"unsafe"
"golang.org/x/net/internal/iana"
)
func getInt(s uintptr, opt *sockOpt) (int, error) {
if opt.name < 1 || opt.typ != ssoTypeInt {
return 0, errOpNoSupport
}
var i int32
l := int32(4)
if err := syscall.Getsockopt(syscall.Handle(s), iana.ProtocolIP, int32(opt.name), (*byte)(unsafe.Pointer(&i)), &l); err != nil {
return 0, os.NewSyscallError("getsockopt", err)
}
return int(i), nil
}
func setInt(s uintptr, opt *sockOpt, v int) error {
if opt.name < 1 || opt.typ != ssoTypeInt {
return errOpNoSupport
}
i := int32(v)
return os.NewSyscallError("setsockopt", syscall.Setsockopt(syscall.Handle(s), iana.ProtocolIP, int32(opt.name), (*byte)(unsafe.Pointer(&i)), 4))
}
func getInterface(s uintptr, opt *sockOpt) (*net.Interface, error) {
if opt.name < 1 || opt.typ != ssoTypeInterface {
return nil, errOpNoSupport
}
return getsockoptInterface(s, opt.name)
}
func setInterface(s uintptr, opt *sockOpt, ifi *net.Interface) error {
if opt.name < 1 || opt.typ != ssoTypeInterface {
return errOpNoSupport
}
return setsockoptInterface(s, opt.name, ifi)
}
func getICMPFilter(s uintptr, opt *sockOpt) (*ICMPFilter, error) {
return nil, errOpNoSupport
}
func setICMPFilter(s uintptr, opt *sockOpt, f *ICMPFilter) error {
return errOpNoSupport
}
func setGroup(s uintptr, opt *sockOpt, ifi *net.Interface, grp net.IP) error {
if opt.name < 1 || opt.typ != ssoTypeIPMreq {
return errOpNoSupport
}
return setsockoptIPMreq(s, opt.name, ifi, grp)
}
func setSourceGroup(s uintptr, opt *sockOpt, ifi *net.Interface, grp, src net.IP) error {
// TODO(mikio): implement this
return errOpNoSupport
}
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ipv4
import (
"syscall"
"unsafe"
)
func getsockopt(s uintptr, level, name int, v unsafe.Pointer, l *uint32) error {
return syscall.Getsockopt(syscall.Handle(s), int32(level), int32(name), (*byte)(v), (*int32)(unsafe.Pointer(l)))
}
func setsockopt(s uintptr, level, name int, v unsafe.Pointer, l uint32) error {
return syscall.Setsockopt(syscall.Handle(s), int32(level), int32(name), (*byte)(v), int32(l))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment