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 | |
| 5 | // Unix domain sockets |
| 6 | |
| 7 | package net |
| 8 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 9 | // UnixAddr represents the address of a Unix domain socket end point. |
| 10 | type UnixAddr struct { |
Albert Strasheim | 01fad6a | 2011-01-19 14:21:58 -0500 | [diff] [blame] | 11 | Name string |
| 12 | Net string |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 13 | } |
| 14 | |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 15 | // Network returns the address's network name, "unix" or "unixgram". |
| 16 | func (a *UnixAddr) Network() string { |
Albert Strasheim | 01fad6a | 2011-01-19 14:21:58 -0500 | [diff] [blame] | 17 | return a.Net |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | func (a *UnixAddr) String() string { |
| 21 | if a == nil { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 22 | return "<nil>" |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 23 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 24 | return a.Name |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | func (a *UnixAddr) toAddr() Addr { |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 28 | if a == nil { // nil *UnixAddr |
| 29 | return nil // nil interface |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 30 | } |
Robert Griesemer | a3d1045 | 2009-12-15 15:35:38 -0800 | [diff] [blame] | 31 | return a |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // ResolveUnixAddr parses addr as a Unix domain socket address. |
Albert Strasheim | 01fad6a | 2011-01-19 14:21:58 -0500 | [diff] [blame] | 35 | // The string net gives the network name, "unix", "unixgram" or |
| 36 | // "unixpacket". |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 37 | func ResolveUnixAddr(net, addr string) (*UnixAddr, error) { |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 38 | switch net { |
| 39 | case "unix": |
Albert Strasheim | 01fad6a | 2011-01-19 14:21:58 -0500 | [diff] [blame] | 40 | case "unixpacket": |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 41 | case "unixgram": |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 42 | default: |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 43 | return nil, UnknownNetworkError(net) |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 44 | } |
Albert Strasheim | 01fad6a | 2011-01-19 14:21:58 -0500 | [diff] [blame] | 45 | return &UnixAddr{addr, net}, nil |
Russ Cox | c83b838 | 2009-11-02 18:37:30 -0800 | [diff] [blame] | 46 | } |