Cleanup in signal.c and related tests
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixEndPoint.cs
1 //
2 // Mono.Unix.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.Unix
36 {
37         [Serializable]
38         public class UnixEndPoint : EndPoint
39         {
40                 string filename;
41                 
42                 public UnixEndPoint (string filename)
43                 {
44                         if (filename == null)
45                                 throw new ArgumentNullException ("filename");
46
47                         if (filename == "")
48                                 throw new ArgumentException ("Cannot be empty.", "filename");
49                         this.filename = filename;
50                 }
51                 
52                 public string Filename {
53                         get {
54                                 return(filename);
55                         }
56                         set {
57                                 filename=value;
58                         }
59                 }
60
61                 public override AddressFamily AddressFamily {
62                         get { return AddressFamily.Unix; }
63                 }
64
65                 public override EndPoint Create (SocketAddress socketAddress)
66                 {
67                         /*
68                          * Should also check this
69                          *
70                         int addr = (int) AddressFamily.Unix;
71                         if (socketAddress [0] != (addr & 0xFF))
72                                 throw new ArgumentException ("socketAddress is not a unix socket address.");
73
74                         if (socketAddress [1] != ((addr & 0xFF00) >> 8))
75                                 throw new ArgumentException ("socketAddress is not a unix socket address.");
76                          */
77
78                         if (socketAddress.Size == 2) {
79                                 // Empty filename.
80                                 // Probably from RemoteEndPoint which on linux does not return the file name.
81                                 UnixEndPoint uep = new UnixEndPoint ("a");
82                                 uep.filename = "";
83                                 return uep;
84                         }
85                         byte [] bytes = new byte [socketAddress.Size - 2 - 1];
86                         for (int i = 0; i < bytes.Length; i++) {
87                                 bytes [i] = socketAddress [i + 2];
88                         }
89
90                         string name = Encoding.Default.GetString (bytes);
91                         return new UnixEndPoint (name);
92                 }
93
94                 public override SocketAddress Serialize ()
95                 {
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];
101
102                         //NULL suffix for non-abstract path
103                         sa[2 + bytes.Length] = 0;
104
105                         return sa;
106                 }
107
108                 public override string ToString() {
109                         return(filename);
110                 }
111
112                 public override int GetHashCode ()
113                 {
114                         return filename.GetHashCode ();
115                 }
116
117                 public override bool Equals (object o)
118                 {
119                         UnixEndPoint other = o as UnixEndPoint;
120                         if (other == null)
121                                 return false;
122
123                         return (other.filename == filename);
124                 }
125         }
126 }
127