1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:14:45 -07: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:
Mikio Hara 2013-07-28 16:15:07 +09:00
parent e257cd8aae
commit a64bea5c99
3 changed files with 27 additions and 18 deletions

View File

@ -11,8 +11,8 @@ import (
"time"
)
// A sockaddr represents a TCP, UDP, IP network endpoint address that
// can be converted into a syscall.Sockaddr.
// A sockaddr represents a TCP, UDP, IP or Unix network endpoint
// address that can be converted into a syscall.Sockaddr.
type sockaddr interface {
Addr
family() int

View File

@ -23,13 +23,6 @@ func (a *UnixAddr) String() string {
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.
// The string net gives the network name, "unix", "unixgram" or
// "unixpacket".

View File

@ -13,13 +13,6 @@ import (
"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) {
var sotype int
switch net {
@ -36,12 +29,12 @@ func unixSocket(net string, laddr, raddr *UnixAddr, mode string, deadline time.T
var la, ra syscall.Sockaddr
switch mode {
case "dial":
if !laddr.isUnnamed() {
if !laddr.isWildcard() {
la = &syscall.SockaddrUnix{Name: laddr.Name}
}
if raddr != nil {
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}
}
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
// to Unix domain sockets.
type UnixConn struct {