Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Russ Cox | 35ace1d | 2009-11-01 11:15:34 -0800 | [diff] [blame] | 5 | package net |
| 6 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 7 | // UnixAddr represents the address of a Unix domain socket end point. |
| 8 | type UnixAddr struct { |
Albert Strasheim | 01fad6a | 2011-01-19 14:21:58 -0500 | [diff] [blame] | 9 | Name string |
| 10 | Net string |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 11 | } |
| 12 | |
Mikio Hara | 8b6d501 | 2013-03-23 07:39:43 +0900 | [diff] [blame] | 13 | // Network returns the address's network name, "unix", "unixgram" or |
| 14 | // "unixpacket". |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 15 | func (a *UnixAddr) Network() string { |
Albert Strasheim | 01fad6a | 2011-01-19 14:21:58 -0500 | [diff] [blame] | 16 | return a.Net |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | func (a *UnixAddr) String() string { |
| 20 | if a == nil { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 21 | return "<nil>" |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 22 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 23 | return a.Name |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 24 | } |
| 25 | |
Mikio Hara | 22829bd | 2015-05-30 07:33:16 +0900 | [diff] [blame] | 26 | func (a *UnixAddr) isWildcard() bool { |
| 27 | return a == nil || a.Name == "" |
| 28 | } |
| 29 | |
| 30 | func (a *UnixAddr) opAddr() Addr { |
| 31 | if a == nil { |
| 32 | return nil |
| 33 | } |
| 34 | return a |
| 35 | } |
| 36 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 37 | // ResolveUnixAddr parses addr as a Unix domain socket address. |
Albert Strasheim | 01fad6a | 2011-01-19 14:21:58 -0500 | [diff] [blame] | 38 | // The string net gives the network name, "unix", "unixgram" or |
| 39 | // "unixpacket". |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 40 | func ResolveUnixAddr(net, addr string) (*UnixAddr, error) { |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 41 | switch net { |
Mikio Hara | 8b6d501 | 2013-03-23 07:39:43 +0900 | [diff] [blame] | 42 | case "unix", "unixgram", "unixpacket": |
| 43 | return &UnixAddr{Name: addr, Net: net}, nil |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 44 | default: |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 45 | return nil, UnknownNetworkError(net) |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 46 | } |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 47 | } |