1 //
2 // Mono.Posix.UnixEndPoint: EndPoint derived class for AF_UNIX family sockets.
3 //
4 // Authors:
5 //	Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
9 
10 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
29 //
30 using System;
31 using System.Net;
32 using System.Net.Sockets;
33 using System.Text;
34 
35 namespace Mono.Posix
36 {
37 	[Serializable]
38 	[Obsolete ("Use Mono.Unix.UnixEndPoint")]
39 	public class UnixEndPoint : EndPoint
40 	{
41 		string filename;
42 
UnixEndPoint(string filename)43 		public UnixEndPoint (string filename)
44 		{
45 			if (filename == null)
46 				throw new ArgumentNullException ("filename");
47 
48 			if (filename == "")
49 				throw new ArgumentException ("Cannot be empty.", "filename");
50 			this.filename = filename;
51 		}
52 
53 		public string Filename {
54 			get {
55 				return(filename);
56 			}
57 			set {
58 				filename=value;
59 			}
60 		}
61 
62 		public override AddressFamily AddressFamily {
63 			get { return AddressFamily.Unix; }
64 		}
65 
Create(SocketAddress socketAddress)66 		public override EndPoint Create (SocketAddress socketAddress)
67 		{
68 			/*
69 			 * Should also check this
70 			 *
71 			int addr = (int) AddressFamily.Unix;
72 			if (socketAddress [0] != (addr & 0xFF))
73 				throw new ArgumentException ("socketAddress is not a unix socket address.");
74 
75 			if (socketAddress [1] != ((addr & 0xFF00) >> 8))
76 				throw new ArgumentException ("socketAddress is not a unix socket address.");
77 			 */
78 
79 			byte [] bytes = new byte [socketAddress.Size - 2];
80 			for (int i = 0; i < bytes.Length; i++) {
81 				bytes [i] = socketAddress [i + 2];
82 			}
83 
84 			string name = Encoding.Default.GetString (bytes);
85 			return new UnixEndPoint (name);
86 		}
87 
Serialize()88 		public override SocketAddress Serialize ()
89 		{
90 			byte [] bytes = Encoding.Default.GetBytes (filename);
91 			SocketAddress sa = new SocketAddress (AddressFamily, bytes.Length + 2);
92 			// sa [0] -> family low byte, sa [1] -> family high byte
93 			for (int i = 0; i < bytes.Length; i++)
94 				sa [i + 2] = bytes [i];
95 
96 			return sa;
97 		}
98 
ToString()99 		public override string ToString() {
100 			return(filename);
101 		}
102 
GetHashCode()103 		public override int GetHashCode ()
104 		{
105 			return filename.GetHashCode ();
106 		}
107 
Equals(object o)108 		public override bool Equals (object o)
109 		{
110 			UnixEndPoint other = o as UnixEndPoint;
111 			if (other == null)
112 				return false;
113 
114 			return (other.filename == filename);
115 		}
116 	}
117 }
118 
119