Commit a64bea5c authored by Mikio Hara's avatar Mikio Hara

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
parent e257cd8a
...@@ -11,8 +11,8 @@ import ( ...@@ -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 { ...@@ -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 ( ...@@ -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 ...@@ -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 { ...@@ -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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment