mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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
This commit is contained in:
parent
e257cd8aae
commit
a64bea5c99
3 changed files with 27 additions and 18 deletions
|
|
@ -11,8 +11,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A sockaddr represents a TCP, UDP, IP network endpoint address that
|
// A sockaddr represents a TCP, UDP, IP or Unix network endpoint
|
||||||
// can be converted into a syscall.Sockaddr.
|
// address that can be converted into a syscall.Sockaddr.
|
||||||
type sockaddr interface {
|
type sockaddr interface {
|
||||||
Addr
|
Addr
|
||||||
family() int
|
family() int
|
||||||
|
|
|
||||||
|
|
@ -23,13 +23,6 @@ func (a *UnixAddr) String() string {
|
||||||
return a.Name
|
return a.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *UnixAddr) toAddr() Addr {
|
|
||||||
if a == nil { // nil *UnixAddr
|
|
||||||
return nil // nil interface
|
|
||||||
}
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveUnixAddr parses addr as a Unix domain socket address.
|
// ResolveUnixAddr parses addr as a Unix domain socket address.
|
||||||
// The string net gives the network name, "unix", "unixgram" or
|
// The string net gives the network name, "unix", "unixgram" or
|
||||||
// "unixpacket".
|
// "unixpacket".
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *UnixAddr) isUnnamed() bool {
|
|
||||||
if a == nil || a.Name == "" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func unixSocket(net string, laddr, raddr *UnixAddr, mode string, deadline time.Time) (*netFD, error) {
|
func unixSocket(net string, laddr, raddr *UnixAddr, mode string, deadline time.Time) (*netFD, error) {
|
||||||
var sotype int
|
var sotype int
|
||||||
switch net {
|
switch net {
|
||||||
|
|
@ -36,12 +29,12 @@ func unixSocket(net string, laddr, raddr *UnixAddr, mode string, deadline time.T
|
||||||
var la, ra syscall.Sockaddr
|
var la, ra syscall.Sockaddr
|
||||||
switch mode {
|
switch mode {
|
||||||
case "dial":
|
case "dial":
|
||||||
if !laddr.isUnnamed() {
|
if !laddr.isWildcard() {
|
||||||
la = &syscall.SockaddrUnix{Name: laddr.Name}
|
la = &syscall.SockaddrUnix{Name: laddr.Name}
|
||||||
}
|
}
|
||||||
if raddr != nil {
|
if raddr != nil {
|
||||||
ra = &syscall.SockaddrUnix{Name: raddr.Name}
|
ra = &syscall.SockaddrUnix{Name: raddr.Name}
|
||||||
} else if sotype != syscall.SOCK_DGRAM || laddr.isUnnamed() {
|
} else if sotype != syscall.SOCK_DGRAM || laddr.isWildcard() {
|
||||||
return nil, &OpError{Op: mode, Net: net, Err: errMissingAddress}
|
return nil, &OpError{Op: mode, Net: net, Err: errMissingAddress}
|
||||||
}
|
}
|
||||||
case "listen":
|
case "listen":
|
||||||
|
|
@ -106,6 +99,29 @@ func sotypeToNet(sotype int) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *UnixAddr) family() int {
|
||||||
|
return syscall.AF_UNIX
|
||||||
|
}
|
||||||
|
|
||||||
|
// isWildcard reports whether a is a wildcard address.
|
||||||
|
func (a *UnixAddr) isWildcard() bool {
|
||||||
|
return a == nil || a.Name == ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *UnixAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
||||||
|
if a == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return &syscall.SockaddrUnix{Name: a.Name}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *UnixAddr) toAddr() sockaddr {
|
||||||
|
if a == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
// UnixConn is an implementation of the Conn interface for connections
|
// UnixConn is an implementation of the Conn interface for connections
|
||||||
// to Unix domain sockets.
|
// to Unix domain sockets.
|
||||||
type UnixConn struct {
|
type UnixConn struct {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue