2 // Mono.Unix.UnixEndPoint: EndPoint derived class for AF_UNIX family sockets.
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Net.Sockets;
38 public class UnixEndPoint : EndPoint
42 public UnixEndPoint (string filename)
45 throw new ArgumentNullException ("filename");
48 throw new ArgumentException ("Cannot be empty.", "filename");
49 this.filename = filename;
52 public string Filename {
61 public override AddressFamily AddressFamily {
62 get { return AddressFamily.Unix; }
65 public override EndPoint Create (SocketAddress socketAddress)
68 * Should also check this
70 int addr = (int) AddressFamily.Unix;
71 if (socketAddress [0] != (addr & 0xFF))
72 throw new ArgumentException ("socketAddress is not a unix socket address.");
74 if (socketAddress [1] != ((addr & 0xFF00) >> 8))
75 throw new ArgumentException ("socketAddress is not a unix socket address.");
78 if (socketAddress.Size == 2) {
80 // Probably from RemoteEndPoint which on linux does not return the file name.
81 UnixEndPoint uep = new UnixEndPoint ("a");
85 byte [] bytes = new byte [socketAddress.Size - 2 - 1];
86 for (int i = 0; i < bytes.Length; i++) {
87 bytes [i] = socketAddress [i + 2];
90 string name = Encoding.Default.GetString (bytes);
91 return new UnixEndPoint (name);
94 public override SocketAddress Serialize ()
96 byte [] bytes = Encoding.Default.GetBytes (filename);
97 SocketAddress sa = new SocketAddress (AddressFamily, 2 + bytes.Length + 1);
98 // sa [0] -> family low byte, sa [1] -> family high byte
99 for (int i = 0; i < bytes.Length; i++)
100 sa [2 + i] = bytes [i];
102 //NULL suffix for non-abstract path
103 sa[2 + bytes.Length] = 0;
108 public override string ToString() {
112 public override int GetHashCode ()
114 return filename.GetHashCode ();
117 public override bool Equals (object o)
119 UnixEndPoint other = o as UnixEndPoint;
123 return (other.filename == filename);