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
10 //
11 
12 //
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.
31 //
32 using System;
33 using System.Net;
34 using System.Net.Sockets;
35 using System.Text;
36 
37 namespace Mono.Unix
38 {
39 	[Serializable]
40 	public class AbstractUnixEndPoint : EndPoint
41 	{
42 		string path;
43 
AbstractUnixEndPoint(string path)44 		public AbstractUnixEndPoint (string path)
45 		{
46 			if (path == null)
47 				throw new ArgumentNullException ("path");
48 
49 			if (path == "")
50 				throw new ArgumentException ("Cannot be empty.", "path");
51 			this.path = path;
52 		}
53 
54 		public string Path {
55 			get {
56 				return(path);
57 			}
58 			set {
59 				path=value;
60 			}
61 		}
62 
63 		public override AddressFamily AddressFamily {
64 			get { return AddressFamily.Unix; }
65 		}
66 
Create(SocketAddress socketAddress)67 		public override EndPoint Create (SocketAddress socketAddress)
68 		{
69 			/*
70 			 * Should also check this
71 			 *
72 			int addr = (int) AddressFamily.Unix;
73 			if (socketAddress [0] != (addr & 0xFF))
74 				throw new ArgumentException ("socketAddress is not a unix socket address.");
75 
76 			if (socketAddress [1] != ((addr & 0xFF00) >> 8))
77 				throw new ArgumentException ("socketAddress is not a unix socket address.");
78 			 */
79 
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];
83 			}
84 
85 			string name = Encoding.Default.GetString (bytes);
86 			return new AbstractUnixEndPoint (name);
87 		}
88 
Serialize()89 		public override SocketAddress Serialize ()
90 		{
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;
96 
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];
100 
101 			return sa;
102 		}
103 
ToString()104 		public override string ToString() {
105 			return(path);
106 		}
107 
GetHashCode()108 		public override int GetHashCode ()
109 		{
110 			return path.GetHashCode ();
111 		}
112 
Equals(object o)113 		public override bool Equals (object o)
114 		{
115 			AbstractUnixEndPoint other = o as AbstractUnixEndPoint;
116 			if (other == null)
117 				return false;
118 
119 			return (other.path == path);
120 		}
121 	}
122 }
123 
124