Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(289)

Delta Between Two Patch Sets: src/pkg/net/unixsock_posix.go

Issue 7058062: code review 7058062: syscall: handle empty address in ReadFrom better (Closed)
Left Patch Set: diff -r 1399878c6731 https://code.google.com/p/go Created 11 years, 2 months ago
Right Patch Set: diff -r f6172d444cc0 http://code.google.com/p/go Created 11 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // +build darwin freebsd linux netbsd openbsd windows 5 // +build darwin freebsd linux netbsd openbsd windows
6 6
7 // Unix domain sockets 7 // Unix domain sockets
8 8
9 package net 9 package net
10 10
(...skipping 18 matching lines...) Expand all
29 } 29 }
30 30
31 var la, ra syscall.Sockaddr 31 var la, ra syscall.Sockaddr
32 switch mode { 32 switch mode {
33 case "dial": 33 case "dial":
34 if laddr != nil { 34 if laddr != nil {
35 la = &syscall.SockaddrUnix{Name: laddr.Name} 35 la = &syscall.SockaddrUnix{Name: laddr.Name}
36 } 36 }
37 if raddr != nil { 37 if raddr != nil {
38 ra = &syscall.SockaddrUnix{Name: raddr.Name} 38 ra = &syscall.SockaddrUnix{Name: raddr.Name}
39 » » } else if sotype != syscall.SOCK_DGRAM { 39 » » } else if sotype != syscall.SOCK_DGRAM || laddr == nil {
40 return nil, &OpError{Op: mode, Net: net, Err: errMissing Address} 40 return nil, &OpError{Op: mode, Net: net, Err: errMissing Address}
41 } 41 }
42 case "listen": 42 case "listen":
43 la = &syscall.SockaddrUnix{Name: laddr.Name} 43 la = &syscall.SockaddrUnix{Name: laddr.Name}
44 default: 44 default:
45 return nil, errors.New("unknown mode: " + mode) 45 return nil, errors.New("unknown mode: " + mode)
46 } 46 }
47 47
48 f := sockaddrToUnix 48 f := sockaddrToUnix
49 if sotype == syscall.SOCK_DGRAM { 49 if sotype == syscall.SOCK_DGRAM {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 // ReadFromUnix can be made to time out and return an error with 117 // ReadFromUnix can be made to time out and return an error with
118 // Timeout() == true after a fixed time limit; see SetDeadline and 118 // Timeout() == true after a fixed time limit; see SetDeadline and
119 // SetReadDeadline. 119 // SetReadDeadline.
120 func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error) { 120 func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error) {
121 if !c.ok() { 121 if !c.ok() {
122 return 0, nil, syscall.EINVAL 122 return 0, nil, syscall.EINVAL
123 } 123 }
124 n, sa, err := c.fd.ReadFrom(b) 124 n, sa, err := c.fd.ReadFrom(b)
125 switch sa := sa.(type) { 125 switch sa := sa.(type) {
126 case *syscall.SockaddrUnix: 126 case *syscall.SockaddrUnix:
127 » » addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)} 127 » » if sa.Name != "" {
128 » » » addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)}
129 » » }
128 } 130 }
129 return 131 return
130 } 132 }
131 133
132 // ReadFrom implements the PacketConn ReadFrom method. 134 // ReadFrom implements the PacketConn ReadFrom method.
133 func (c *UnixConn) ReadFrom(b []byte) (n int, addr Addr, err error) { 135 func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error) {
134 if !c.ok() { 136 if !c.ok() {
135 return 0, nil, syscall.EINVAL 137 return 0, nil, syscall.EINVAL
136 } 138 }
137 » n, uaddr, err := c.ReadFromUnix(b) 139 » n, addr, err := c.ReadFromUnix(b)
138 » return n, uaddr.toAddr(), err 140 » return n, addr.toAddr(), err
139 } 141 }
140 142
141 // ReadMsgUnix reads a packet from c, copying the payload into b and 143 // ReadMsgUnix reads a packet from c, copying the payload into b and
142 // the associated out-of-band data into oob. It returns the number of 144 // the associated out-of-band data into oob. It returns the number of
143 // bytes copied into b, the number of bytes copied into oob, the flags 145 // bytes copied into b, the number of bytes copied into oob, the flags
144 // that were set on the packet, and the source address of the packet. 146 // that were set on the packet, and the source address of the packet.
145 func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAdd r, err error) { 147 func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAdd r, err error) {
146 if !c.ok() { 148 if !c.ok() {
147 return 0, 0, 0, nil, syscall.EINVAL 149 return 0, 0, 0, nil, syscall.EINVAL
148 } 150 }
149 n, oobn, flags, sa, err := c.fd.ReadMsg(b, oob) 151 n, oobn, flags, sa, err := c.fd.ReadMsg(b, oob)
150 switch sa := sa.(type) { 152 switch sa := sa.(type) {
151 case *syscall.SockaddrUnix: 153 case *syscall.SockaddrUnix:
152 » » addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)} 154 » » if sa.Name != "" {
155 » » » addr = &UnixAddr{sa.Name, sotypeToNet(c.fd.sotype)}
156 » » }
153 } 157 }
154 return 158 return
155 } 159 }
156 160
157 // WriteToUnix writes a packet to addr via c, copying the payload from b. 161 // WriteToUnix writes a packet to addr via c, copying the payload from b.
158 // 162 //
159 // WriteToUnix can be made to time out and return an error with 163 // WriteToUnix can be made to time out and return an error with
160 // Timeout() == true after a fixed time limit; see SetDeadline and 164 // Timeout() == true after a fixed time limit; see SetDeadline and
161 // SetWriteDeadline. On packet-oriented connections, write timeouts 165 // SetWriteDeadline. On packet-oriented connections, write timeouts
162 // are rare. 166 // are rare.
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 } 350 }
347 if laddr == nil { 351 if laddr == nil {
348 return nil, &OpError{"listen", net, nil, errMissingAddress} 352 return nil, &OpError{"listen", net, nil, errMissingAddress}
349 } 353 }
350 fd, err := unixSocket(net, laddr, nil, "listen", noDeadline) 354 fd, err := unixSocket(net, laddr, nil, "listen", noDeadline)
351 if err != nil { 355 if err != nil {
352 return nil, err 356 return nil, err
353 } 357 }
354 return newUnixConn(fd), nil 358 return newUnixConn(fd), nil
355 } 359 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b