本文整理汇总了Golang中net.ResolveUnixAddr函数的典型用法代码示例。如果您正苦于以下问题:Golang ResolveUnixAddr函数的具体用法?Golang ResolveUnixAddr怎么用?Golang ResolveUnixAddr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ResolveUnixAddr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: UnixAudienceListener
func UnixAudienceListener(sockaddr string) {
fi, err := os.Stat(sockaddr)
if err == nil {
fmode := fi.Mode()
if fmode&os.ModeType == os.ModeSocket {
o.Warn("Removing stale socket at %s", sockaddr)
os.Remove(sockaddr)
} else {
o.Fail("%s exists and is not a socket", sockaddr)
}
}
laddr, err := net.ResolveUnixAddr("unix", sockaddr)
o.MightFail(err, "Couldn't resolve audience socket address")
l, err := net.ListenUnix("unix", laddr)
o.MightFail(err, "Couldn't start audience unixsock listener")
// Fudge the permissions on the unixsock!
fi, err = os.Stat(sockaddr)
if err == nil {
os.Chmod(sockaddr, fi.Mode()|0777)
} else {
o.Warn("Couldn't fudge permission on audience socket: %s", err)
}
// make sure we clean up the unix socket when we die.
defer l.Close()
defer os.Remove(sockaddr)
AudienceListener(l)
}
示例2: Init
func (self *UdpOutput) Init(pcf *plugins.PluginCommonConfig, conf toml.Primitive) error {
var err error
log.Println("UdpOutput Init.")
self.config = &UdpOutputConfig{
Net: "udp",
// Defines maximum size of udp data for IPv4.
MaxMessageSize: 65507,
}
if err := toml.PrimitiveDecode(conf, self.config); err != nil {
return fmt.Errorf("Can't unmarshal UdpOutput config: %s", err)
}
if self.config.MaxMessageSize < 512 {
return fmt.Errorf("Maximum message size can't be smaller than 512 bytes.")
}
if self.config.Net == "unixgram" {
if runtime.GOOS == "windows" {
return errors.New("Can't use Unix datagram sockets on Windows.")
}
var unixAddr, lAddr *net.UnixAddr
unixAddr, err = net.ResolveUnixAddr(self.config.Net, self.config.Address)
if err != nil {
return fmt.Errorf("Error resolving unixgram address '%s': %s", self.config.Address,
err.Error())
}
if self.config.LocalAddress != "" {
lAddr, err = net.ResolveUnixAddr(self.config.Net, self.config.LocalAddress)
if err != nil {
return fmt.Errorf("Error resolving local unixgram address '%s': %s",
self.config.LocalAddress, err.Error())
}
}
if self.conn, err = net.DialUnix(self.config.Net, lAddr, unixAddr); err != nil {
return fmt.Errorf("Can't connect to '%s': %s", self.config.Address,
err.Error())
}
} else {
var udpAddr, lAddr *net.UDPAddr
if udpAddr, err = net.ResolveUDPAddr(self.config.Net, self.config.Address); err != nil {
return fmt.Errorf("Error resolving UDP address '%s': %s", self.config.Address,
err.Error())
}
if self.config.LocalAddress != "" {
lAddr, err = net.ResolveUDPAddr(self.config.Net, self.config.LocalAddress)
if err != nil {
return fmt.Errorf("Error resolving local UDP address '%s': %s",
self.config.Address, err.Error())
}
}
if self.conn, err = net.DialUDP(self.config.Net, lAddr, udpAddr); err != nil {
return fmt.Errorf("Can't connect to '%s': %s", self.config.Address,
err.Error())
}
}
return err
}
示例3: Init
// Initialize UDP connection
func (o *UdpOutput) Init(config interface{}) (err error) {
o.UdpOutputConfig = config.(*UdpOutputConfig) // assert we have the right config type
if o.UdpOutputConfig.MaxMessageSize < 512 {
return fmt.Errorf("Maximum message size can't be smaller than 512 bytes.")
}
if o.Net == "unixgram" {
if runtime.GOOS == "windows" {
return errors.New("Can't use Unix datagram sockets on Windows.")
}
var unixAddr, lAddr *net.UnixAddr
unixAddr, err = net.ResolveUnixAddr(o.Net, o.Address)
if err != nil {
return fmt.Errorf("Error resolving unixgram address '%s': %s", o.Address,
err.Error())
}
if o.LocalAddress != "" {
lAddr, err = net.ResolveUnixAddr(o.Net, o.LocalAddress)
if err != nil {
return fmt.Errorf("Error resolving local unixgram address '%s': %s",
o.LocalAddress, err.Error())
}
}
if o.conn, err = net.DialUnix(o.Net, lAddr, unixAddr); err != nil {
return fmt.Errorf("Can't connect to '%s': %s", o.Address,
err.Error())
}
} else {
var udpAddr, lAddr *net.UDPAddr
if udpAddr, err = net.ResolveUDPAddr(o.Net, o.Address); err != nil {
return fmt.Errorf("Error resolving UDP address '%s': %s", o.Address,
err.Error())
}
if o.LocalAddress != "" {
lAddr, err = net.ResolveUDPAddr(o.Net, o.LocalAddress)
if err != nil {
return fmt.Errorf("Error resolving local UDP address '%s': %s",
o.Address, err.Error())
}
}
if o.conn, err = net.DialUDP(o.Net, lAddr, udpAddr); err != nil {
return fmt.Errorf("Can't connect to '%s': %s", o.Address,
err.Error())
}
}
return
}
示例4: StartClientHandler
func StartClientHandler(tree *ProcessTree, quit chan bool) {
path, _ := filepath.Abs(zeusSockName)
addr, err := net.ResolveUnixAddr("unix", path)
if err != nil {
panic("Can't open socket.")
}
listener, err := net.ListenUnix("unix", addr)
if err != nil {
ErrorCantCreateListener()
}
defer listener.Close()
connections := make(chan *unixsocket.Usock)
go func() {
for {
if conn, err := listener.AcceptUnix(); err != nil {
errorUnableToAcceptSocketConnection()
time.Sleep(500 * time.Millisecond)
} else {
connections <- unixsocket.NewUsock(conn)
}
}
}()
for {
select {
case <-quit:
quit <- true
return
case conn := <-connections:
go handleClientConnection(tree, conn)
}
}
}
示例5: SetServers
// Can have dup server address for higher weight
func (this *StandardServerSelector) SetServers(servers ...string) error {
if len(servers) == 0 {
return ErrNoServers
}
naddr := make([]net.Addr, len(servers))
for i, server := range servers {
if strings.Contains(server, "/") {
addr, err := net.ResolveUnixAddr("unix", server)
if err != nil {
return err
}
naddr[i] = addr
} else {
tcpaddr, err := net.ResolveTCPAddr("tcp", server)
if err != nil {
return err
}
naddr[i] = tcpaddr
}
}
this.lk.Lock()
defer this.lk.Unlock()
this.addrs = naddr
return nil
}
示例6: unixDial
func unixDial(networ, addr string) (net.Conn, error) {
var raddr *net.UnixAddr
var err error
if addr == "unix.socket:80" {
raddr, err = net.ResolveUnixAddr("unix", shared.VarPath("unix.socket"))
if err != nil {
return nil, fmt.Errorf(gettext.Gettext("cannot resolve unix socket address: %v"), err)
}
} else { // TODO - I think this is dead code
raddr, err = net.ResolveUnixAddr("unix", addr)
if err != nil {
return nil, fmt.Errorf(gettext.Gettext("cannot resolve unix socket address: %v"), err)
}
}
return net.DialUnix("unix", nil, raddr)
}
示例7: Open
// Open opens the transport.
func (c Config) Open() (transports.Transport, error) {
if c.Name == "" {
c.Name = path.Join(os.TempDir(), "telehash-"+randomString(8)+".sock")
}
if c.Mode == 0 {
c.Mode = 0700
}
c.Mode &= os.ModePerm
c.Mode |= os.ModeSocket
laddr, err := net.ResolveUnixAddr("unix", c.Name)
if err != nil {
return nil, err
}
listener, err := net.ListenUnix("unix", laddr)
if err != nil {
return nil, err
}
err = os.Chmod(laddr.Name, c.Mode)
if err != nil {
listener.Close()
os.Remove(laddr.Name)
return nil, err
}
return &transport{(*unixAddr)(laddr), listener}, nil
}
示例8: TestListenUnix
func (_ *ListenerSuite) TestListenUnix(c *C) {
addr, _ := net.ResolveUnixAddr("unix", "/tmp/go_test_socket")
cfg := &conf.ListenerConf{
Name: "local",
Ports: []*conf.PortConf{
&conf.PortConf{
Type: "unix",
Addr: addr,
},
},
}
ll := listener.NewListener(cfg)
ll.SetHandler(echoHandler)
ll.Start()
time.Sleep(50 * time.Millisecond)
cc, err := net.DialUnix("unix", nil, addr)
c.Assert(err, IsNil)
testEchoConnection(c, cc)
cc.Close()
ll.Stop()
}
示例9: main
func main() {
path := "/tmp/example.sock"
addr, err := net.ResolveUnixAddr("unixgram", path)
if err != nil {
fmt.Printf("ResolveUnixAddr err: %s\n", err)
return
}
conn, err := net.ListenUnixgram("unixgram", addr)
if err != nil {
fmt.Printf("ListenUnixgram err: %s\n", err)
return
}
// Ensure world writable access
os.Chmod(path, 0666)
data := make([]byte, 4096)
length, _, err := conn.ReadFrom(data)
if err != nil {
fmt.Printf("conn.ReadFrom error: %s\n", err)
return
}
fmt.Printf("Got: %d bytes\n", length)
}
示例10: NewTServerUnixDomainTimeout
func NewTServerUnixDomainTimeout(listenAddr string, clientTimeout time.Duration) (*TServerUnixDomain, error) {
addr, err := net.ResolveUnixAddr("unix", listenAddr)
if err != nil {
return nil, err
}
return &TServerUnixDomain{addr: addr, clientTimeout: clientTimeout}, nil
}
示例11: serveFpr
func serveFpr(addr string, s *siegfried.Siegfried) {
// remove the socket file if it exists
if _, err := os.Stat(addr); err == nil {
os.Remove(addr)
}
uaddr, err := net.ResolveUnixAddr("unix", addr)
if err != nil {
log.Fatalf("FPR error: failed to get address: %v", err)
}
lis, err := net.ListenUnix("unix", uaddr)
if err != nil {
log.Fatalf("FPR error: failed to listen: %v", err)
}
buf := make([]byte, 4024)
for {
conn, err := lis.Accept()
if err != nil {
log.Fatalf("FPR error: bad connection: %v", err)
}
l, err := conn.Read(buf)
if err != nil {
conn.Write([]byte("error reading from connection: " + err.Error()))
}
conn.Write(fpridentify(s, string(buf[:l])))
conn.Close()
}
}
示例12: connectViaUnix
func connectViaUnix(c *Client, remote *RemoteConfig) error {
c.BaseURL = "http://unix.socket"
c.BaseWSURL = "ws://unix.socket"
c.Transport = "unix"
uDial := func(network, addr string) (net.Conn, error) {
// The arguments 'network' and 'addr' are ignored because
// they are the wrong information.
// addr is generated from BaseURL which becomes
// 'unix.socket:80' which is certainly not what we want.
// handle:
// unix:///path/to/socket
// unix:/path/to/socket
// unix:path/to/socket
path := strings.TrimPrefix(remote.Addr, "unix:")
if strings.HasPrefix(path, "///") {
// translate unix:///path/to, to just "/path/to"
path = path[2:]
}
raddr, err := net.ResolveUnixAddr("unix", path)
if err != nil {
return nil, err
}
return net.DialUnix("unix", nil, raddr)
}
c.Http.Transport = &http.Transport{Dial: uDial}
c.websocketDialer.NetDial = uDial
c.Remote = remote
st, err := c.ServerStatus()
if err != nil {
return err
}
c.Certificate = st.Environment.Certificate
return nil
}
示例13: StartUnixListener
// StartUnixListener starts up a unix domain socket listener for reconnects to
// be sent through
func (irc *Bot) StartUnixListener() {
unaddr, err := net.ResolveUnixAddr("unix", irc.unixastr)
if err != nil {
panic(err)
}
list, err := net.ListenUnix("unix", unaddr)
if err != nil {
panic(err)
}
irc.unixlist = list
con, err := list.AcceptUnix()
if err != nil {
fmt.Println("unix listener error: ", err)
return
}
list.Close()
fi, err := irc.con.(*net.TCPConn).File()
if err != nil {
panic(err)
}
err = sendfd.SendFD(con, fi)
if err != nil {
panic(err)
}
select {
case <-irc.Incoming:
default:
close(irc.Incoming)
}
close(irc.outgoing)
}
示例14: ListenAndServer
func (s *Server) ListenAndServer() error {
db, _ := OpenFile("/var/level/testdb", nil)
hashDb, _ := OpenFile("/var/level/testhashdb", nil)
var network string
var (
addr net.Addr
err error
)
if strings.Contains(s.Addr, "/") {
network = "unix"
addr, err = net.ResolveUnixAddr("unix", s.Addr)
} else {
network = "tcp"
addr, err = net.ResolveTCPAddr("tcp", s.Addr)
}
if err != nil {
return err
}
l, e := net.Listen(network, addr.String())
if e != nil {
return e
}
defer l.Close()
for {
rw, e := l.Accept()
if e != nil {
return e
}
c := s.newConn(rw)
go c.serve(db, hashDb)
}
}
示例15: TestSocketPathIsUnixAddr
func (s *suite) TestSocketPathIsUnixAddr(c *gc.C) {
path := socketpath()
addr, err := net.ResolveUnixAddr("unix", path)
c.Assert(err, gc.IsNil)
c.Assert(addr.Name, gc.Equals, path)
c.Assert(addr.Net, gc.Equals, "unix")
}