Skip to content

Commit a64bea5

Browse files
committed
net: make UnixAddr implement sockaddr interface
This is in preparation for runtime-integrated network pollster for BSD variants. Update #5199 R=golang-dev, dave CC=golang-dev https://golang.org/cl/11932044
1 parent e257cd8 commit a64bea5

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/pkg/net/sock_posix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"time"
1212
)
1313

14-
// A sockaddr represents a TCP, UDP, IP network endpoint address that
15-
// can be converted into a syscall.Sockaddr.
14+
// A sockaddr represents a TCP, UDP, IP or Unix network endpoint
15+
// address that can be converted into a syscall.Sockaddr.
1616
type sockaddr interface {
1717
Addr
1818
family() int

src/pkg/net/unixsock.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ func (a *UnixAddr) String() string {
2323
return a.Name
2424
}
2525

26-
func (a *UnixAddr) toAddr() Addr {
27-
if a == nil { // nil *UnixAddr
28-
return nil // nil interface
29-
}
30-
return a
31-
}
32-
3326
// ResolveUnixAddr parses addr as a Unix domain socket address.
3427
// The string net gives the network name, "unix", "unixgram" or
3528
// "unixpacket".

src/pkg/net/unixsock_posix.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ import (
1313
"time"
1414
)
1515

16-
func (a *UnixAddr) isUnnamed() bool {
17-
if a == nil || a.Name == "" {
18-
return true
19-
}
20-
return false
21-
}
22-
2316
func unixSocket(net string, laddr, raddr *UnixAddr, mode string, deadline time.Time) (*netFD, error) {
2417
var sotype int
2518
switch net {
@@ -36,12 +29,12 @@ func unixSocket(net string, laddr, raddr *UnixAddr, mode string, deadline time.T
3629
var la, ra syscall.Sockaddr
3730
switch mode {
3831
case "dial":
39-
if !laddr.isUnnamed() {
32+
if !laddr.isWildcard() {
4033
la = &syscall.SockaddrUnix{Name: laddr.Name}
4134
}
4235
if raddr != nil {
4336
ra = &syscall.SockaddrUnix{Name: raddr.Name}
44-
} else if sotype != syscall.SOCK_DGRAM || laddr.isUnnamed() {
37+
} else if sotype != syscall.SOCK_DGRAM || laddr.isWildcard() {
4538
return nil, &OpError{Op: mode, Net: net, Err: errMissingAddress}
4639
}
4740
case "listen":
@@ -106,6 +99,29 @@ func sotypeToNet(sotype int) string {
10699
}
107100
}
108101

102+
func (a *UnixAddr) family() int {
103+
return syscall.AF_UNIX
104+
}
105+
106+
// isWildcard reports whether a is a wildcard address.
107+
func (a *UnixAddr) isWildcard() bool {
108+
return a == nil || a.Name == ""
109+
}
110+
111+
func (a *UnixAddr) sockaddr(family int) (syscall.Sockaddr, error) {
112+
if a == nil {
113+
return nil, nil
114+
}
115+
return &syscall.SockaddrUnix{Name: a.Name}, nil
116+
}
117+
118+
func (a *UnixAddr) toAddr() sockaddr {
119+
if a == nil {
120+
return nil
121+
}
122+
return a
123+
}
124+
109125
// UnixConn is an implementation of the Conn interface for connections
110126
// to Unix domain sockets.
111127
type UnixConn struct {

0 commit comments

Comments
 (0)