2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Posix / Mono.Unix / AbstractUnixEndPoint.cs
blob6c1fe21c9f24bd77b7ba3f040d0b99caadb918ad
1 //
2 // Mono.Unix.AbstractUnixEndPoint: EndPoint derived class for AF_UNIX family sockets.
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Alp Toker (alp@atoker.com)
7 //
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // (C) 2006 Alp Toker
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Net;
34 using System.Net.Sockets;
35 using System.Text;
37 namespace Mono.Unix
39 [Serializable]
40 public class AbstractUnixEndPoint : EndPoint
42 string path;
44 public AbstractUnixEndPoint (string path)
46 if (path == null)
47 throw new ArgumentNullException ("path");
49 if (path == "")
50 throw new ArgumentException ("Cannot be empty.", "path");
51 this.path = path;
54 public string Path {
55 get {
56 return(path);
58 set {
59 path=value;
63 public override AddressFamily AddressFamily {
64 get { return AddressFamily.Unix; }
67 public override EndPoint Create (SocketAddress socketAddress)
70 * Should also check this
72 int addr = (int) AddressFamily.Unix;
73 if (socketAddress [0] != (addr & 0xFF))
74 throw new ArgumentException ("socketAddress is not a unix socket address.");
76 if (socketAddress [1] != ((addr & 0xFF00) >> 8))
77 throw new ArgumentException ("socketAddress is not a unix socket address.");
80 byte [] bytes = new byte [socketAddress.Size - 2 - 1];
81 for (int i = 0; i < bytes.Length; i++) {
82 bytes [i] = socketAddress [2 + 1 + i];
85 string name = Encoding.Default.GetString (bytes);
86 return new AbstractUnixEndPoint (name);
89 public override SocketAddress Serialize ()
91 byte [] bytes = Encoding.Default.GetBytes (path);
92 SocketAddress sa = new SocketAddress (AddressFamily, 2 + 1 + bytes.Length);
93 //NULL prefix denotes the abstract namespace, see unix(7)
94 //in this case, there is no NULL suffix
95 sa [2] = 0;
97 // sa [0] -> family low byte, sa [1] -> family high byte
98 for (int i = 0; i < bytes.Length; i++)
99 sa [i + 2 + 1] = bytes [i];
101 return sa;
104 public override string ToString() {
105 return(path);
108 public override int GetHashCode ()
110 return path.GetHashCode ();
113 public override bool Equals (object o)
115 AbstractUnixEndPoint other = o as AbstractUnixEndPoint;
116 if (other == null)
117 return false;
119 return (other.path == path);