Skip to content

Commit c0ac1a5

Browse files
committed
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>
1 parent a46fb68 commit c0ac1a5

9 files changed

+43
-55
lines changed

ipv4/batch.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package ipv4
99
import (
1010
"net"
1111
"runtime"
12-
"syscall"
1312

1413
"golang.org/x/net/internal/socket"
1514
)
@@ -76,7 +75,7 @@ type Message = socket.Message
7675
// headers.
7776
func (c *payloadHandler) ReadBatch(ms []Message, flags int) (int, error) {
7877
if !c.ok() {
79-
return 0, syscall.EINVAL
78+
return 0, errInvalidConn
8079
}
8180
switch runtime.GOOS {
8281
case "linux":
@@ -107,7 +106,7 @@ func (c *payloadHandler) ReadBatch(ms []Message, flags int) (int, error) {
107106
// On other platforms, this method will write only a single message.
108107
func (c *payloadHandler) WriteBatch(ms []Message, flags int) (int, error) {
109108
if !c.ok() {
110-
return 0, syscall.EINVAL
109+
return 0, errInvalidConn
111110
}
112111
switch runtime.GOOS {
113112
case "linux":
@@ -139,7 +138,7 @@ func (c *payloadHandler) WriteBatch(ms []Message, flags int) (int, error) {
139138
// On other platforms, this method will read only a single message.
140139
func (c *packetHandler) ReadBatch(ms []Message, flags int) (int, error) {
141140
if !c.ok() {
142-
return 0, syscall.EINVAL
141+
return 0, errInvalidConn
143142
}
144143
switch runtime.GOOS {
145144
case "linux":
@@ -170,7 +169,7 @@ func (c *packetHandler) ReadBatch(ms []Message, flags int) (int, error) {
170169
// On other platforms, this method will write only a single message.
171170
func (c *packetHandler) WriteBatch(ms []Message, flags int) (int, error) {
172171
if !c.ok() {
173-
return 0, syscall.EINVAL
172+
return 0, errInvalidConn
174173
}
175174
switch runtime.GOOS {
176175
case "linux":

ipv4/dgramopt.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package ipv4
66

77
import (
88
"net"
9-
"syscall"
109

1110
"golang.org/x/net/bpf"
1211
)
@@ -15,7 +14,7 @@ import (
1514
// multicast packets.
1615
func (c *dgramOpt) MulticastTTL() (int, error) {
1716
if !c.ok() {
18-
return 0, syscall.EINVAL
17+
return 0, errInvalidConn
1918
}
2019
so, ok := sockOpts[ssoMulticastTTL]
2120
if !ok {
@@ -28,7 +27,7 @@ func (c *dgramOpt) MulticastTTL() (int, error) {
2827
// outgoing multicast packets.
2928
func (c *dgramOpt) SetMulticastTTL(ttl int) error {
3029
if !c.ok() {
31-
return syscall.EINVAL
30+
return errInvalidConn
3231
}
3332
so, ok := sockOpts[ssoMulticastTTL]
3433
if !ok {
@@ -41,7 +40,7 @@ func (c *dgramOpt) SetMulticastTTL(ttl int) error {
4140
// packet transmissions.
4241
func (c *dgramOpt) MulticastInterface() (*net.Interface, error) {
4342
if !c.ok() {
44-
return nil, syscall.EINVAL
43+
return nil, errInvalidConn
4544
}
4645
so, ok := sockOpts[ssoMulticastInterface]
4746
if !ok {
@@ -54,7 +53,7 @@ func (c *dgramOpt) MulticastInterface() (*net.Interface, error) {
5453
// multicast packet transmissions.
5554
func (c *dgramOpt) SetMulticastInterface(ifi *net.Interface) error {
5655
if !c.ok() {
57-
return syscall.EINVAL
56+
return errInvalidConn
5857
}
5958
so, ok := sockOpts[ssoMulticastInterface]
6059
if !ok {
@@ -67,7 +66,7 @@ func (c *dgramOpt) SetMulticastInterface(ifi *net.Interface) error {
6766
// should be copied and send back to the originator.
6867
func (c *dgramOpt) MulticastLoopback() (bool, error) {
6968
if !c.ok() {
70-
return false, syscall.EINVAL
69+
return false, errInvalidConn
7170
}
7271
so, ok := sockOpts[ssoMulticastLoopback]
7372
if !ok {
@@ -84,7 +83,7 @@ func (c *dgramOpt) MulticastLoopback() (bool, error) {
8483
// should be copied and send back to the originator.
8584
func (c *dgramOpt) SetMulticastLoopback(on bool) error {
8685
if !c.ok() {
87-
return syscall.EINVAL
86+
return errInvalidConn
8887
}
8988
so, ok := sockOpts[ssoMulticastLoopback]
9089
if !ok {
@@ -104,7 +103,7 @@ func (c *dgramOpt) SetMulticastLoopback(on bool) error {
104103
// configuration.
105104
func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
106105
if !c.ok() {
107-
return syscall.EINVAL
106+
return errInvalidConn
108107
}
109108
so, ok := sockOpts[ssoJoinGroup]
110109
if !ok {
@@ -122,7 +121,7 @@ func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
122121
// source-specific group.
123122
func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
124123
if !c.ok() {
125-
return syscall.EINVAL
124+
return errInvalidConn
126125
}
127126
so, ok := sockOpts[ssoLeaveGroup]
128127
if !ok {
@@ -143,7 +142,7 @@ func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
143142
// routing configuration.
144143
func (c *dgramOpt) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
145144
if !c.ok() {
146-
return syscall.EINVAL
145+
return errInvalidConn
147146
}
148147
so, ok := sockOpts[ssoJoinSourceGroup]
149148
if !ok {
@@ -164,7 +163,7 @@ func (c *dgramOpt) JoinSourceSpecificGroup(ifi *net.Interface, group, source net
164163
// interface ifi.
165164
func (c *dgramOpt) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
166165
if !c.ok() {
167-
return syscall.EINVAL
166+
return errInvalidConn
168167
}
169168
so, ok := sockOpts[ssoLeaveSourceGroup]
170169
if !ok {
@@ -186,7 +185,7 @@ func (c *dgramOpt) LeaveSourceSpecificGroup(ifi *net.Interface, group, source ne
186185
// ifi.
187186
func (c *dgramOpt) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
188187
if !c.ok() {
189-
return syscall.EINVAL
188+
return errInvalidConn
190189
}
191190
so, ok := sockOpts[ssoBlockSourceGroup]
192191
if !ok {
@@ -207,7 +206,7 @@ func (c *dgramOpt) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source
207206
// group by ExcludeSourceSpecificGroup again on the interface ifi.
208207
func (c *dgramOpt) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
209208
if !c.ok() {
210-
return syscall.EINVAL
209+
return errInvalidConn
211210
}
212211
so, ok := sockOpts[ssoUnblockSourceGroup]
213212
if !ok {
@@ -228,7 +227,7 @@ func (c *dgramOpt) IncludeSourceSpecificGroup(ifi *net.Interface, group, source
228227
// Currently only Linux supports this.
229228
func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
230229
if !c.ok() {
231-
return nil, syscall.EINVAL
230+
return nil, errInvalidConn
232231
}
233232
so, ok := sockOpts[ssoICMPFilter]
234233
if !ok {
@@ -241,7 +240,7 @@ func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
241240
// Currently only Linux supports this.
242241
func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
243242
if !c.ok() {
244-
return syscall.EINVAL
243+
return errInvalidConn
245244
}
246245
so, ok := sockOpts[ssoICMPFilter]
247246
if !ok {
@@ -255,7 +254,7 @@ func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
255254
// Only supported on Linux.
256255
func (c *dgramOpt) SetBPF(filter []bpf.RawInstruction) error {
257256
if !c.ok() {
258-
return syscall.EINVAL
257+
return errInvalidConn
259258
}
260259
so, ok := sockOpts[ssoAttachFilter]
261260
if !ok {

ipv4/endpoint.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package ipv4
66

77
import (
88
"net"
9-
"syscall"
109
"time"
1110

1211
"golang.org/x/net/internal/socket"
@@ -58,7 +57,7 @@ func (c *dgramOpt) ok() bool { return c != nil && c.Conn != nil }
5857
// SetControlMessage sets the per packet IP-level socket options.
5958
func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
6059
if !c.payloadHandler.ok() {
61-
return syscall.EINVAL
60+
return errInvalidConn
6261
}
6362
return setControlMessage(c.dgramOpt.Conn, &c.payloadHandler.rawOpt, cf, on)
6463
}
@@ -67,7 +66,7 @@ func (c *PacketConn) SetControlMessage(cf ControlFlags, on bool) error {
6766
// endpoint.
6867
func (c *PacketConn) SetDeadline(t time.Time) error {
6968
if !c.payloadHandler.ok() {
70-
return syscall.EINVAL
69+
return errInvalidConn
7170
}
7271
return c.payloadHandler.PacketConn.SetDeadline(t)
7372
}
@@ -76,7 +75,7 @@ func (c *PacketConn) SetDeadline(t time.Time) error {
7675
// endpoint.
7776
func (c *PacketConn) SetReadDeadline(t time.Time) error {
7877
if !c.payloadHandler.ok() {
79-
return syscall.EINVAL
78+
return errInvalidConn
8079
}
8180
return c.payloadHandler.PacketConn.SetReadDeadline(t)
8281
}
@@ -85,15 +84,15 @@ func (c *PacketConn) SetReadDeadline(t time.Time) error {
8584
// endpoint.
8685
func (c *PacketConn) SetWriteDeadline(t time.Time) error {
8786
if !c.payloadHandler.ok() {
88-
return syscall.EINVAL
87+
return errInvalidConn
8988
}
9089
return c.payloadHandler.PacketConn.SetWriteDeadline(t)
9190
}
9291

9392
// Close closes the endpoint.
9493
func (c *PacketConn) Close() error {
9594
if !c.payloadHandler.ok() {
96-
return syscall.EINVAL
95+
return errInvalidConn
9796
}
9897
return c.payloadHandler.PacketConn.Close()
9998
}
@@ -124,7 +123,7 @@ type RawConn struct {
124123
// SetControlMessage sets the per packet IP-level socket options.
125124
func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
126125
if !c.packetHandler.ok() {
127-
return syscall.EINVAL
126+
return errInvalidConn
128127
}
129128
return setControlMessage(c.dgramOpt.Conn, &c.packetHandler.rawOpt, cf, on)
130129
}
@@ -133,7 +132,7 @@ func (c *RawConn) SetControlMessage(cf ControlFlags, on bool) error {
133132
// endpoint.
134133
func (c *RawConn) SetDeadline(t time.Time) error {
135134
if !c.packetHandler.ok() {
136-
return syscall.EINVAL
135+
return errInvalidConn
137136
}
138137
return c.packetHandler.IPConn.SetDeadline(t)
139138
}
@@ -142,7 +141,7 @@ func (c *RawConn) SetDeadline(t time.Time) error {
142141
// endpoint.
143142
func (c *RawConn) SetReadDeadline(t time.Time) error {
144143
if !c.packetHandler.ok() {
145-
return syscall.EINVAL
144+
return errInvalidConn
146145
}
147146
return c.packetHandler.IPConn.SetReadDeadline(t)
148147
}
@@ -151,15 +150,15 @@ func (c *RawConn) SetReadDeadline(t time.Time) error {
151150
// endpoint.
152151
func (c *RawConn) SetWriteDeadline(t time.Time) error {
153152
if !c.packetHandler.ok() {
154-
return syscall.EINVAL
153+
return errInvalidConn
155154
}
156155
return c.packetHandler.IPConn.SetWriteDeadline(t)
157156
}
158157

159158
// Close closes the endpoint.
160159
func (c *RawConn) Close() error {
161160
if !c.packetHandler.ok() {
162-
return syscall.EINVAL
161+
return errInvalidConn
163162
}
164163
return c.packetHandler.IPConn.Close()
165164
}

ipv4/genericopt.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
package ipv4
66

7-
import "syscall"
8-
97
// TOS returns the type-of-service field value for outgoing packets.
108
func (c *genericOpt) TOS() (int, error) {
119
if !c.ok() {
12-
return 0, syscall.EINVAL
10+
return 0, errInvalidConn
1311
}
1412
so, ok := sockOpts[ssoTOS]
1513
if !ok {
@@ -22,7 +20,7 @@ func (c *genericOpt) TOS() (int, error) {
2220
// packets.
2321
func (c *genericOpt) SetTOS(tos int) error {
2422
if !c.ok() {
25-
return syscall.EINVAL
23+
return errInvalidConn
2624
}
2725
so, ok := sockOpts[ssoTOS]
2826
if !ok {
@@ -34,7 +32,7 @@ func (c *genericOpt) SetTOS(tos int) error {
3432
// TTL returns the time-to-live field value for outgoing packets.
3533
func (c *genericOpt) TTL() (int, error) {
3634
if !c.ok() {
37-
return 0, syscall.EINVAL
35+
return 0, errInvalidConn
3836
}
3937
so, ok := sockOpts[ssoTTL]
4038
if !ok {
@@ -47,7 +45,7 @@ func (c *genericOpt) TTL() (int, error) {
4745
// packets.
4846
func (c *genericOpt) SetTTL(ttl int) error {
4947
if !c.ok() {
50-
return syscall.EINVAL
48+
return errInvalidConn
5149
}
5250
so, ok := sockOpts[ssoTTL]
5351
if !ok {

ipv4/header.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"net"
1111
"runtime"
12-
"syscall"
1312

1413
"golang.org/x/net/internal/socket"
1514
)
@@ -54,7 +53,7 @@ func (h *Header) String() string {
5453
// Marshal returns the binary encoding of h.
5554
func (h *Header) Marshal() ([]byte, error) {
5655
if h == nil {
57-
return nil, syscall.EINVAL
56+
return nil, errInvalidConn
5857
}
5958
if h.Len < HeaderLen {
6059
return nil, errHeaderTooShort

ipv4/helper.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
var (
13+
errInvalidConn = errors.New("invalid connection")
1314
errMissingAddress = errors.New("missing address")
1415
errMissingHeader = errors.New("missing header")
1516
errHeaderTooShort = errors.New("header too short")

ipv4/packet.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package ipv4
66

77
import (
88
"net"
9-
"syscall"
109

1110
"golang.org/x/net/internal/socket"
1211
)
@@ -28,7 +27,7 @@ func (c *packetHandler) ok() bool { return c != nil && c.IPConn != nil && c.Conn
2827
// header h, the payload p and the control message cm.
2928
func (c *packetHandler) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error) {
3029
if !c.ok() {
31-
return nil, nil, nil, syscall.EINVAL
30+
return nil, nil, nil, errInvalidConn
3231
}
3332
return c.readFrom(b)
3433
}
@@ -63,7 +62,7 @@ func slicePacket(b []byte) (h, p []byte, err error) {
6362
// Options = optional
6463
func (c *packetHandler) WriteTo(h *Header, p []byte, cm *ControlMessage) error {
6564
if !c.ok() {
66-
return syscall.EINVAL
65+
return errInvalidConn
6766
}
6867
return c.writeTo(h, p, cm)
6968
}

0 commit comments

Comments
 (0)