ipv4: drop the use of syscall package in platform-independent code

Change-Id: I5a8eac6e80fd2c9f4604231d51cb91d3b8514fea
Reviewed-on: https://go-review.googlesource.com/121882
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/ipv4/batch.go b/ipv4/batch.go
index b445499..5ce9b35 100644
--- a/ipv4/batch.go
+++ b/ipv4/batch.go
@@ -9,7 +9,6 @@
 import (
 	"net"
 	"runtime"
-	"syscall"
 
 	"golang.org/x/net/internal/socket"
 )
@@ -76,7 +75,7 @@
 // headers.
 func (c *payloadHandler) ReadBatch(ms []Message, flags int) (int, error) {
 	if !c.ok() {
-		return 0, syscall.EINVAL
+		return 0, errInvalidConn
 	}
 	switch runtime.GOOS {
 	case "linux":
@@ -107,7 +106,7 @@
 // On other platforms, this method will write only a single message.
 func (c *payloadHandler) WriteBatch(ms []Message, flags int) (int, error) {
 	if !c.ok() {
-		return 0, syscall.EINVAL
+		return 0, errInvalidConn
 	}
 	switch runtime.GOOS {
 	case "linux":
@@ -139,7 +138,7 @@
 // On other platforms, this method will read only a single message.
 func (c *packetHandler) ReadBatch(ms []Message, flags int) (int, error) {
 	if !c.ok() {
-		return 0, syscall.EINVAL
+		return 0, errInvalidConn
 	}
 	switch runtime.GOOS {
 	case "linux":
@@ -170,7 +169,7 @@
 // On other platforms, this method will write only a single message.
 func (c *packetHandler) WriteBatch(ms []Message, flags int) (int, error) {
 	if !c.ok() {
-		return 0, syscall.EINVAL
+		return 0, errInvalidConn
 	}
 	switch runtime.GOOS {
 	case "linux":
diff --git a/ipv4/dgramopt.go b/ipv4/dgramopt.go
index 54d77d5..3676449 100644
--- a/ipv4/dgramopt.go
+++ b/ipv4/dgramopt.go
@@ -6,7 +6,6 @@
 
 import (
 	"net"
-	"syscall"
 
 	"golang.org/x/net/bpf"
 )
@@ -15,7 +14,7 @@
 // multicast packets.
 func (c *dgramOpt) MulticastTTL() (int, error) {
 	if !c.ok() {
-		return 0, syscall.EINVAL
+		return 0, errInvalidConn
 	}
 	so, ok := sockOpts[ssoMulticastTTL]
 	if !ok {
@@ -28,7 +27,7 @@
 // outgoing multicast packets.
 func (c *dgramOpt) SetMulticastTTL(ttl int) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoMulticastTTL]
 	if !ok {
@@ -41,7 +40,7 @@
 // packet transmissions.
 func (c *dgramOpt) MulticastInterface() (*net.Interface, error) {
 	if !c.ok() {
-		return nil, syscall.EINVAL
+		return nil, errInvalidConn
 	}
 	so, ok := sockOpts[ssoMulticastInterface]
 	if !ok {
@@ -54,7 +53,7 @@
 // multicast packet transmissions.
 func (c *dgramOpt) SetMulticastInterface(ifi *net.Interface) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoMulticastInterface]
 	if !ok {
@@ -67,7 +66,7 @@
 // should be copied and send back to the originator.
 func (c *dgramOpt) MulticastLoopback() (bool, error) {
 	if !c.ok() {
-		return false, syscall.EINVAL
+		return false, errInvalidConn
 	}
 	so, ok := sockOpts[ssoMulticastLoopback]
 	if !ok {
@@ -84,7 +83,7 @@
 // should be copied and send back to the originator.
 func (c *dgramOpt) SetMulticastLoopback(on bool) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoMulticastLoopback]
 	if !ok {
@@ -104,7 +103,7 @@
 // configuration.
 func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoJoinGroup]
 	if !ok {
@@ -122,7 +121,7 @@
 // source-specific group.
 func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoLeaveGroup]
 	if !ok {
@@ -143,7 +142,7 @@
 // routing configuration.
 func (c *dgramOpt) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoJoinSourceGroup]
 	if !ok {
@@ -164,7 +163,7 @@
 // interface ifi.
 func (c *dgramOpt) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoLeaveSourceGroup]
 	if !ok {
@@ -186,7 +185,7 @@
 // ifi.
 func (c *dgramOpt) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoBlockSourceGroup]
 	if !ok {
@@ -207,7 +206,7 @@
 // group by ExcludeSourceSpecificGroup again on the interface ifi.
 func (c *dgramOpt) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoUnblockSourceGroup]
 	if !ok {
@@ -228,7 +227,7 @@
 // Currently only Linux supports this.
 func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
 	if !c.ok() {
-		return nil, syscall.EINVAL
+		return nil, errInvalidConn
 	}
 	so, ok := sockOpts[ssoICMPFilter]
 	if !ok {
@@ -241,7 +240,7 @@
 // Currently only Linux supports this.
 func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoICMPFilter]
 	if !ok {
@@ -255,7 +254,7 @@
 // Only supported on Linux.
 func (c *dgramOpt) SetBPF(filter []bpf.RawInstruction) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoAttachFilter]
 	if !ok {
diff --git a/ipv4/endpoint.go b/ipv4/endpoint.go
index 2ab8773..5009463 100644
--- a/ipv4/endpoint.go
+++ b/ipv4/endpoint.go
@@ -6,7 +6,6 @@
 
 import (
 	"net"
-	"syscall"
 	"time"
 
 	"golang.org/x/net/internal/socket"
@@ -58,7 +57,7 @@
 // SetControlMessage sets the per packet IP-level socket options.
 func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
 	if !c.payloadHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return setControlMessage(c.dgramOpt.Conn, &c.payloadHandler.rawOpt, cf, on)
 }
@@ -67,7 +66,7 @@
 // endpoint.
 func (c *PacketConn) SetDeadline(t time.Time) error {
 	if !c.payloadHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return c.payloadHandler.PacketConn.SetDeadline(t)
 }
@@ -76,7 +75,7 @@
 // endpoint.
 func (c *PacketConn) SetReadDeadline(t time.Time) error {
 	if !c.payloadHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return c.payloadHandler.PacketConn.SetReadDeadline(t)
 }
@@ -85,7 +84,7 @@
 // endpoint.
 func (c *PacketConn) SetWriteDeadline(t time.Time) error {
 	if !c.payloadHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return c.payloadHandler.PacketConn.SetWriteDeadline(t)
 }
@@ -93,7 +92,7 @@
 // Close closes the endpoint.
 func (c *PacketConn) Close() error {
 	if !c.payloadHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return c.payloadHandler.PacketConn.Close()
 }
@@ -124,7 +123,7 @@
 // SetControlMessage sets the per packet IP-level socket options.
 func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
 	if !c.packetHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return setControlMessage(c.dgramOpt.Conn, &c.packetHandler.rawOpt, cf, on)
 }
@@ -133,7 +132,7 @@
 // endpoint.
 func (c *RawConn) SetDeadline(t time.Time) error {
 	if !c.packetHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return c.packetHandler.IPConn.SetDeadline(t)
 }
@@ -142,7 +141,7 @@
 // endpoint.
 func (c *RawConn) SetReadDeadline(t time.Time) error {
 	if !c.packetHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return c.packetHandler.IPConn.SetReadDeadline(t)
 }
@@ -151,7 +150,7 @@
 // endpoint.
 func (c *RawConn) SetWriteDeadline(t time.Time) error {
 	if !c.packetHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return c.packetHandler.IPConn.SetWriteDeadline(t)
 }
@@ -159,7 +158,7 @@
 // Close closes the endpoint.
 func (c *RawConn) Close() error {
 	if !c.packetHandler.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return c.packetHandler.IPConn.Close()
 }
diff --git a/ipv4/genericopt.go b/ipv4/genericopt.go
index 119bf84..587ae4a 100644
--- a/ipv4/genericopt.go
+++ b/ipv4/genericopt.go
@@ -4,12 +4,10 @@
 
 package ipv4
 
-import "syscall"
-
 // TOS returns the type-of-service field value for outgoing packets.
 func (c *genericOpt) TOS() (int, error) {
 	if !c.ok() {
-		return 0, syscall.EINVAL
+		return 0, errInvalidConn
 	}
 	so, ok := sockOpts[ssoTOS]
 	if !ok {
@@ -22,7 +20,7 @@
 // packets.
 func (c *genericOpt) SetTOS(tos int) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoTOS]
 	if !ok {
@@ -34,7 +32,7 @@
 // TTL returns the time-to-live field value for outgoing packets.
 func (c *genericOpt) TTL() (int, error) {
 	if !c.ok() {
-		return 0, syscall.EINVAL
+		return 0, errInvalidConn
 	}
 	so, ok := sockOpts[ssoTTL]
 	if !ok {
@@ -47,7 +45,7 @@
 // packets.
 func (c *genericOpt) SetTTL(ttl int) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	so, ok := sockOpts[ssoTTL]
 	if !ok {
diff --git a/ipv4/header.go b/ipv4/header.go
index c8822a6..358afe2 100644
--- a/ipv4/header.go
+++ b/ipv4/header.go
@@ -9,7 +9,6 @@
 	"fmt"
 	"net"
 	"runtime"
-	"syscall"
 
 	"golang.org/x/net/internal/socket"
 )
@@ -54,7 +53,7 @@
 // Marshal returns the binary encoding of h.
 func (h *Header) Marshal() ([]byte, error) {
 	if h == nil {
-		return nil, syscall.EINVAL
+		return nil, errInvalidConn
 	}
 	if h.Len < HeaderLen {
 		return nil, errHeaderTooShort
diff --git a/ipv4/helper.go b/ipv4/helper.go
index a5052e3..8d8ff98 100644
--- a/ipv4/helper.go
+++ b/ipv4/helper.go
@@ -10,6 +10,7 @@
 )
 
 var (
+	errInvalidConn              = errors.New("invalid connection")
 	errMissingAddress           = errors.New("missing address")
 	errMissingHeader            = errors.New("missing header")
 	errHeaderTooShort           = errors.New("header too short")
diff --git a/ipv4/packet.go b/ipv4/packet.go
index f00f5b0..966bb77 100644
--- a/ipv4/packet.go
+++ b/ipv4/packet.go
@@ -6,7 +6,6 @@
 
 import (
 	"net"
-	"syscall"
 
 	"golang.org/x/net/internal/socket"
 )
@@ -28,7 +27,7 @@
 // header h, the payload p and the control message cm.
 func (c *packetHandler) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error) {
 	if !c.ok() {
-		return nil, nil, nil, syscall.EINVAL
+		return nil, nil, nil, errInvalidConn
 	}
 	return c.readFrom(b)
 }
@@ -63,7 +62,7 @@
 //	Options       = optional
 func (c *packetHandler) WriteTo(h *Header, p []byte, cm *ControlMessage) error {
 	if !c.ok() {
-		return syscall.EINVAL
+		return errInvalidConn
 	}
 	return c.writeTo(h, p, cm)
 }
diff --git a/ipv4/payload_cmsg.go b/ipv4/payload_cmsg.go
index 3f06d76..eec520c 100644
--- a/ipv4/payload_cmsg.go
+++ b/ipv4/payload_cmsg.go
@@ -6,10 +6,7 @@
 
 package ipv4
 
-import (
-	"net"
-	"syscall"
-)
+import "net"
 
 // ReadFrom reads a payload of the received IPv4 datagram, from the
 // endpoint c, copying the payload into b. It returns the number of
@@ -17,7 +14,7 @@
 // src of the received datagram.
 func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error) {
 	if !c.ok() {
-		return 0, nil, nil, syscall.EINVAL
+		return 0, nil, nil, errInvalidConn
 	}
 	return c.readFrom(b)
 }
@@ -30,7 +27,7 @@
 // control of the outgoing datagram is not required.
 func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
 	if !c.ok() {
-		return 0, syscall.EINVAL
+		return 0, errInvalidConn
 	}
 	return c.writeTo(b, cm, dst)
 }
diff --git a/ipv4/payload_nocmsg.go b/ipv4/payload_nocmsg.go
index 3926de7..8cb21b3 100644
--- a/ipv4/payload_nocmsg.go
+++ b/ipv4/payload_nocmsg.go
@@ -6,10 +6,7 @@
 
 package ipv4
 
-import (
-	"net"
-	"syscall"
-)
+import "net"
 
 // ReadFrom reads a payload of the received IPv4 datagram, from the
 // endpoint c, copying the payload into b. It returns the number of
@@ -17,7 +14,7 @@
 // src of the received datagram.
 func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error) {
 	if !c.ok() {
-		return 0, nil, nil, syscall.EINVAL
+		return 0, nil, nil, errInvalidConn
 	}
 	if n, src, err = c.PacketConn.ReadFrom(b); err != nil {
 		return 0, nil, nil, err
@@ -33,7 +30,7 @@
 // control of the outgoing datagram is not required.
 func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
 	if !c.ok() {
-		return 0, syscall.EINVAL
+		return 0, errInvalidConn
 	}
 	if dst == nil {
 		return 0, errMissingAddress