Debian Codegraph @

lib/csharp/MonoExample/NDesk-dbus/UnixNativeTransport.cs

raw
1 // Copyright 2006 Alp Toker <[email protected]>
2 // This software is made available under the MIT License
3 // See COPYING for details
4
5 //We send BSD-style credentials on all platforms
6 //Doesn't seem to break Linux (but is redundant there)
7 //This may turn out to be a bad idea
8 #define HAVE_CMSGCRED
9
10 using System;
11 using System.IO;
12 using System.Text;
13
14 using System.Runtime.InteropServices;
15
16 using Mono.Unix;
17 using Mono.Unix.Native;
18
19 namespace NDesk.DBus.Transports
20 {
21 class UnixSocket
22 {
23 public const short AF_UNIX = 1;
24 //TODO: SOCK_STREAM is 2 on Solaris
25 public const short SOCK_STREAM = 1;
26
27 //TODO: some of these are provided by libsocket instead of libc on Solaris
28
29 [DllImport ("libc", SetLastError=true)]
30 protected static extern int socket (int domain, int type, int protocol);
31
32 [DllImport ("libc", SetLastError=true)]
33 protected static extern int connect (int sockfd, byte[] serv_addr, uint addrlen);
34
35 [DllImport ("libc", SetLastError=true)]
36 protected static extern int bind (int sockfd, byte[] my_addr, uint addrlen);
37
38 [DllImport ("libc", SetLastError=true)]
39 protected static extern int listen (int sockfd, int backlog);
40
41 //TODO: this prototype is probably wrong, fix it
42 [DllImport ("libc", SetLastError=true)]
43 protected static extern int accept (int sockfd, byte[] addr, ref uint addrlen);
44
45 //TODO: confirm and make use of these functions
46 [DllImport ("libc", SetLastError=true)]
47 protected static extern int getsockopt (int s, int optname, IntPtr optval, ref uint optlen);
48
49 [DllImport ("libc", SetLastError=true)]
50 protected static extern int setsockopt (int s, int optname, IntPtr optval, uint optlen);
51
52 [DllImport ("libc", SetLastError=true)]
53 public static extern int recvmsg (int s, IntPtr msg, int flags);
54
55 [DllImport ("libc", SetLastError=true)]
56 public static extern int sendmsg (int s, IntPtr msg, int flags);
57
58 public int Handle;
59
60 public UnixSocket (int handle)
61 {
62 this.Handle = handle;
63 }
64
65 public UnixSocket ()
66 {
67 //TODO: don't hard-code PF_UNIX and SOCK_STREAM or SocketType.Stream
68 //AddressFamily family, SocketType type, ProtocolType proto
69
70 int r = socket (AF_UNIX, SOCK_STREAM, 0);
71 //we should get the Exception from UnixMarshal and throw it here for a better stack trace, but the relevant API seems to be private
72 UnixMarshal.ThrowExceptionForLastErrorIf (r);
73 Handle = r;
74 }
75
76 protected bool connected = false;
77
78 //TODO: consider memory management
79 public void Connect (byte[] remote_end)
80 {
81 int r = connect (Handle, remote_end, (uint)remote_end.Length);
82 //we should get the Exception from UnixMarshal and throw it here for a better stack trace, but the relevant API seems to be private
83 UnixMarshal.ThrowExceptionForLastErrorIf (r);
84 connected = true;
85 }
86
87 //assigns a name to the socket
88 public void Bind (byte[] local_end)
89 {
90 int r = bind (Handle, local_end, (uint)local_end.Length);
91 UnixMarshal.ThrowExceptionForLastErrorIf (r);
92 }
93
94 public void Listen (int backlog)
95 {
96 int r = listen (Handle, backlog);
97 UnixMarshal.ThrowExceptionForLastErrorIf (r);
98 }
99
100 public UnixSocket Accept ()
101 {
102 byte[] addr = new byte[110];
103 uint addrlen = (uint)addr.Length;
104
105 int r = accept (Handle, addr, ref addrlen);
106 UnixMarshal.ThrowExceptionForLastErrorIf (r);
107 //TODO: use the returned addr
108 //TODO: fix probable memory leak here
109 //string str = Encoding.Default.GetString (addr, 0, (int)addrlen);
110 return new UnixSocket (r);
111 }
112 }
113
114 struct IOVector
115 {
116 public IntPtr Base;
117 public int Length;
118 }
119
120 class UnixNativeTransport : UnixTransport
121 {
122 protected UnixSocket socket;
123
124 public override void Open (string path, bool @abstract)
125 {
126 if (String.IsNullOrEmpty (path))
127 throw new ArgumentException ("path");
128
129 if (@abstract)
130 socket = OpenAbstractUnix (path);
131 else
132 socket = OpenUnix (path);
133
134 //socket.Blocking = true;
135 SocketHandle = (long)socket.Handle;
136 Stream = new UnixStream ((int)socket.Handle);
137 }
138
139 //send peer credentials null byte
140 //different platforms do this in different ways
141 #if HAVE_CMSGCRED
142 unsafe void WriteBsdCred ()
143 {
144 //null credentials byte
145 byte buf = 0;
146
147 IOVector iov = new IOVector ();
148 iov.Base = (IntPtr)(&buf);
149 iov.Length = 1;
150
151 msghdr msg = new msghdr ();
152 msg.msg_iov = &iov;
153 msg.msg_iovlen = 1;
154
155 cmsg cm = new cmsg ();
156 msg.msg_control = (IntPtr)(&cm);
157 msg.msg_controllen = (uint)sizeof (cmsg);
158 cm.hdr.cmsg_len = (uint)sizeof (cmsg);
159 cm.hdr.cmsg_level = 0xffff; //SOL_SOCKET
160 cm.hdr.cmsg_type = 0x03; //SCM_CREDS
161
162 int written = UnixSocket.sendmsg (socket.Handle, (IntPtr)(&msg), 0);
163 UnixMarshal.ThrowExceptionForLastErrorIf (written);
164 if (written != 1)
165 throw new Exception ("Failed to write credentials");
166 }
167 #endif
168
169 public override void WriteCred ()
170 {
171 #if HAVE_CMSGCRED
172 try {
173 WriteBsdCred ();
174 } catch {
175 if (Protocol.Verbose)
176 Console.Error.WriteLine ("Warning: WriteBsdCred() failed; falling back to ordinary WriteCred()");
177 //null credentials byte
178 byte buf = 0;
179 Stream.WriteByte (buf);
180 }
181 #else
182 //null credentials byte
183 byte buf = 0;
184 Stream.WriteByte (buf);
185 #endif
186 }
187
188 protected UnixSocket OpenAbstractUnix (string path)
189 {
190 byte[] p = Encoding.Default.GetBytes (path);
191
192 byte[] sa = new byte[2 + 1 + p.Length];
193
194 //we use BitConverter to stay endian-safe
195 byte[] afData = BitConverter.GetBytes (UnixSocket.AF_UNIX);
196 sa[0] = afData[0];
197 sa[1] = afData[1];
198
199 sa[2] = 0; //null prefix for abstract domain socket addresses, see unix(7)
200 for (int i = 0 ; i != p.Length ; i++)
201 sa[3 + i] = p[i];
202
203 UnixSocket client = new UnixSocket ();
204 client.Connect (sa);
205
206 return client;
207 }
208
209 public UnixSocket OpenUnix (string path)
210 {
211 byte[] p = Encoding.Default.GetBytes (path);
212
213 byte[] sa = new byte[2 + p.Length + 1];
214
215 //we use BitConverter to stay endian-safe
216 byte[] afData = BitConverter.GetBytes (UnixSocket.AF_UNIX);
217 sa[0] = afData[0];
218 sa[1] = afData[1];
219
220 for (int i = 0 ; i != p.Length ; i++)
221 sa[2 + i] = p[i];
222 sa[2 + p.Length] = 0; //null suffix for domain socket addresses, see unix(7)
223
224 UnixSocket client = new UnixSocket ();
225 client.Connect (sa);
226
227 return client;
228 }
229 }
230
231 #if HAVE_CMSGCRED
232 /*
233 public struct msg
234 {
235 public IntPtr msg_next;
236 public long msg_type;
237 public ushort msg_ts;
238 short msg_spot;
239 IntPtr label;
240 }
241 */
242
243 unsafe struct msghdr
244 {
245 public IntPtr msg_name; //optional address
246 public uint msg_namelen; //size of address
247 public IOVector *msg_iov; //scatter/gather array
248 public int msg_iovlen; //# elements in msg_iov
249 public IntPtr msg_control; //ancillary data, see below
250 public uint msg_controllen; //ancillary data buffer len
251 public int msg_flags; //flags on received message
252 }
253
254 struct cmsghdr
255 {
256 public uint cmsg_len; //data byte count, including header
257 public int cmsg_level; //originating protocol
258 public int cmsg_type; //protocol-specific type
259 }
260
261 unsafe struct cmsgcred
262 {
263 public int cmcred_pid; //PID of sending process
264 public uint cmcred_uid; //real UID of sending process
265 public uint cmcred_euid; //effective UID of sending process
266 public uint cmcred_gid; //real GID of sending process
267 public short cmcred_ngroups; //number or groups
268 public fixed uint cmcred_groups[16]; //groups, CMGROUP_MAX
269 }
270
271 struct cmsg
272 {
273 public cmsghdr hdr;
274 public cmsgcred cred;
275 }
276 #endif
277 }