Skip to content

Commit e4bb139

Browse files
committed
net: make protocol-specific Dial and Listen return consistent error value
Update #4856 R=golang-dev, bradfitz, dave CC=golang-dev https://golang.org/cl/12916046
1 parent a8b4a1e commit e4bb139

5 files changed

+33
-52
lines changed

src/pkg/net/iprawsock_posix.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,19 @@ func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) {
189189
func dialIP(netProto string, laddr, raddr *IPAddr, deadline time.Time) (*IPConn, error) {
190190
net, proto, err := parseNetwork(netProto)
191191
if err != nil {
192-
return nil, err
192+
return nil, &OpError{Op: "dial", Net: netProto, Addr: raddr, Err: err}
193193
}
194194
switch net {
195195
case "ip", "ip4", "ip6":
196196
default:
197-
return nil, UnknownNetworkError(netProto)
197+
return nil, &OpError{Op: "dial", Net: netProto, Addr: raddr, Err: UnknownNetworkError(netProto)}
198198
}
199199
if raddr == nil {
200-
return nil, &OpError{"dial", netProto, nil, errMissingAddress}
200+
return nil, &OpError{Op: "dial", Net: netProto, Addr: nil, Err: errMissingAddress}
201201
}
202202
fd, err := internetSocket(net, laddr, raddr, deadline, syscall.SOCK_RAW, proto, "dial", sockaddrToIP)
203203
if err != nil {
204-
return nil, err
204+
return nil, &OpError{Op: "dial", Net: netProto, Addr: raddr, Err: err}
205205
}
206206
return newIPConn(fd), nil
207207
}
@@ -213,16 +213,16 @@ func dialIP(netProto string, laddr, raddr *IPAddr, deadline time.Time) (*IPConn,
213213
func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) {
214214
net, proto, err := parseNetwork(netProto)
215215
if err != nil {
216-
return nil, err
216+
return nil, &OpError{Op: "dial", Net: netProto, Addr: laddr, Err: err}
217217
}
218218
switch net {
219219
case "ip", "ip4", "ip6":
220220
default:
221-
return nil, UnknownNetworkError(netProto)
221+
return nil, &OpError{Op: "listen", Net: netProto, Addr: laddr, Err: UnknownNetworkError(netProto)}
222222
}
223223
fd, err := internetSocket(net, laddr, nil, noDeadline, syscall.SOCK_RAW, proto, "listen", sockaddrToIP)
224224
if err != nil {
225-
return nil, err
225+
return nil, &OpError{Op: "listen", Net: netProto, Addr: laddr, Err: err}
226226
}
227227
return newIPConn(fd), nil
228228
}

src/pkg/net/ipsock_posix.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,7 @@ func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family
133133

134134
func internetSocket(net string, laddr, raddr sockaddr, deadline time.Time, sotype, proto int, mode string, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err error) {
135135
family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
136-
fd, err = socket(net, family, sotype, proto, ipv6only, laddr, raddr, deadline, toAddr)
137-
if err != nil {
138-
goto Error
139-
}
140-
return fd, nil
141-
142-
Error:
143-
addr := raddr
144-
if mode == "listen" {
145-
addr = laddr
146-
}
147-
return nil, &OpError{mode, net, addr, err}
136+
return socket(net, family, sotype, proto, ipv6only, laddr, raddr, deadline, toAddr)
148137
}
149138

150139
func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {

src/pkg/net/tcpsock_posix.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
150150
switch net {
151151
case "tcp", "tcp4", "tcp6":
152152
default:
153-
return nil, UnknownNetworkError(net)
153+
return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: UnknownNetworkError(net)}
154154
}
155155
if raddr == nil {
156-
return nil, &OpError{"dial", net, nil, errMissingAddress}
156+
return nil, &OpError{Op: "dial", Net: net, Addr: nil, Err: errMissingAddress}
157157
}
158158
return dialTCP(net, laddr, raddr, noDeadline)
159159
}
@@ -193,7 +193,7 @@ func dialTCP(net string, laddr, raddr *TCPAddr, deadline time.Time) (*TCPConn, e
193193
}
194194

195195
if err != nil {
196-
return nil, err
196+
return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: err}
197197
}
198198
return newTCPConn(fd), nil
199199
}
@@ -292,14 +292,14 @@ func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
292292
switch net {
293293
case "tcp", "tcp4", "tcp6":
294294
default:
295-
return nil, UnknownNetworkError(net)
295+
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: UnknownNetworkError(net)}
296296
}
297297
if laddr == nil {
298298
laddr = &TCPAddr{}
299299
}
300300
fd, err := internetSocket(net, laddr, nil, noDeadline, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP)
301301
if err != nil {
302-
return nil, err
302+
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
303303
}
304304
return &TCPListener{fd}, nil
305305
}

src/pkg/net/udpsock_posix.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,18 @@ func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
173173
switch net {
174174
case "udp", "udp4", "udp6":
175175
default:
176-
return nil, UnknownNetworkError(net)
176+
return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: UnknownNetworkError(net)}
177177
}
178178
if raddr == nil {
179-
return nil, &OpError{"dial", net, nil, errMissingAddress}
179+
return nil, &OpError{Op: "dial", Net: net, Addr: nil, Err: errMissingAddress}
180180
}
181181
return dialUDP(net, laddr, raddr, noDeadline)
182182
}
183183

184184
func dialUDP(net string, laddr, raddr *UDPAddr, deadline time.Time) (*UDPConn, error) {
185185
fd, err := internetSocket(net, laddr, raddr, deadline, syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP)
186186
if err != nil {
187-
return nil, err
187+
return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: err}
188188
}
189189
return newUDPConn(fd), nil
190190
}
@@ -200,14 +200,14 @@ func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
200200
switch net {
201201
case "udp", "udp4", "udp6":
202202
default:
203-
return nil, UnknownNetworkError(net)
203+
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: UnknownNetworkError(net)}
204204
}
205205
if laddr == nil {
206206
laddr = &UDPAddr{}
207207
}
208208
fd, err := internetSocket(net, laddr, nil, noDeadline, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
209209
if err != nil {
210-
return nil, err
210+
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
211211
}
212212
return newUDPConn(fd), nil
213213
}
@@ -220,25 +220,25 @@ func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, e
220220
switch net {
221221
case "udp", "udp4", "udp6":
222222
default:
223-
return nil, UnknownNetworkError(net)
223+
return nil, &OpError{Op: "listen", Net: net, Addr: gaddr, Err: UnknownNetworkError(net)}
224224
}
225225
if gaddr == nil || gaddr.IP == nil {
226-
return nil, &OpError{"listen", net, nil, errMissingAddress}
226+
return nil, &OpError{Op: "listen", Net: net, Addr: nil, Err: errMissingAddress}
227227
}
228228
fd, err := internetSocket(net, gaddr, nil, noDeadline, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
229229
if err != nil {
230-
return nil, err
230+
return nil, &OpError{Op: "listen", Net: net, Addr: gaddr, Err: err}
231231
}
232232
c := newUDPConn(fd)
233233
if ip4 := gaddr.IP.To4(); ip4 != nil {
234234
if err := listenIPv4MulticastUDP(c, ifi, ip4); err != nil {
235235
c.Close()
236-
return nil, &OpError{"listen", net, &IPAddr{IP: ip4}, err}
236+
return nil, &OpError{Op: "listen", Net: net, Addr: &IPAddr{IP: ip4}, Err: err}
237237
}
238238
} else {
239239
if err := listenIPv6MulticastUDP(c, ifi, gaddr.IP); err != nil {
240240
c.Close()
241-
return nil, &OpError{"listen", net, &IPAddr{IP: gaddr.IP}, err}
241+
return nil, &OpError{Op: "listen", Net: net, Addr: &IPAddr{IP: gaddr.IP}, Err: err}
242242
}
243243
}
244244
return c, nil

src/pkg/net/unixsock_posix.go

+10-18
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func unixSocket(net string, laddr, raddr sockaddr, mode string, deadline time.Ti
3535
raddr = nil
3636
}
3737
if raddr == nil && (sotype != syscall.SOCK_DGRAM || laddr == nil) {
38-
return nil, &OpError{Op: mode, Net: net, Err: errMissingAddress}
38+
return nil, errMissingAddress
3939
}
4040
case "listen":
4141
default:
@@ -51,17 +51,9 @@ func unixSocket(net string, laddr, raddr sockaddr, mode string, deadline time.Ti
5151

5252
fd, err := socket(net, syscall.AF_UNIX, sotype, 0, false, laddr, raddr, deadline, f)
5353
if err != nil {
54-
goto error
54+
return nil, err
5555
}
5656
return fd, nil
57-
58-
error:
59-
addr := raddr
60-
switch mode {
61-
case "listen":
62-
addr = laddr
63-
}
64-
return nil, &OpError{Op: mode, Net: net, Addr: addr, Err: err}
6557
}
6658

6759
func sockaddrToUnix(sa syscall.Sockaddr) Addr {
@@ -250,15 +242,15 @@ func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error) {
250242
switch net {
251243
case "unix", "unixgram", "unixpacket":
252244
default:
253-
return nil, UnknownNetworkError(net)
245+
return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: UnknownNetworkError(net)}
254246
}
255247
return dialUnix(net, laddr, raddr, noDeadline)
256248
}
257249

258250
func dialUnix(net string, laddr, raddr *UnixAddr, deadline time.Time) (*UnixConn, error) {
259251
fd, err := unixSocket(net, laddr, raddr, "dial", deadline)
260252
if err != nil {
261-
return nil, err
253+
return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: err}
262254
}
263255
return newUnixConn(fd), nil
264256
}
@@ -277,14 +269,14 @@ func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) {
277269
switch net {
278270
case "unix", "unixpacket":
279271
default:
280-
return nil, UnknownNetworkError(net)
272+
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: UnknownNetworkError(net)}
281273
}
282274
if laddr == nil {
283-
return nil, &OpError{"listen", net, nil, errMissingAddress}
275+
return nil, &OpError{Op: "listen", Net: net, Addr: nil, Err: errMissingAddress}
284276
}
285277
fd, err := unixSocket(net, laddr, nil, "listen", noDeadline)
286278
if err != nil {
287-
return nil, err
279+
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
288280
}
289281
return &UnixListener{fd, laddr.Name}, nil
290282
}
@@ -365,14 +357,14 @@ func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error) {
365357
switch net {
366358
case "unixgram":
367359
default:
368-
return nil, UnknownNetworkError(net)
360+
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: UnknownNetworkError(net)}
369361
}
370362
if laddr == nil {
371-
return nil, &OpError{"listen", net, nil, errMissingAddress}
363+
return nil, &OpError{Op: "listen", Net: net, Addr: nil, Err: errMissingAddress}
372364
}
373365
fd, err := unixSocket(net, laddr, nil, "listen", noDeadline)
374366
if err != nil {
375-
return nil, err
367+
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
376368
}
377369
return newUnixConn(fd), nil
378370
}

0 commit comments

Comments
 (0)