示例#1
0
 public void Dispose()
 {
     if (UnixFDArray != null)
     {
         UnixFDArray.Dispose();
     }
 }
        // Send the two buffers and the FDs using sendmsg(), handle short writes
        public unsafe void Sendmsg(byte[] buffer1, long offset1, long length1,
                                   byte[] buffer2, long offset2, long length2,
                                   DBus.Protocol.UnixFDArray fds)
        {
            //SendmsgShort (buffer1, offset1, length1, buffer2, offset2, length2, fds); return;
            long bytes_overall = (long)length1 + length2;
            long written       = 0;

            while (written < bytes_overall)
            {
                if (written >= length1)
                {
                    long written2 = written - length1;
                    written += SendmsgShort(buffer2, offset2 + written2, length2 - written2, null, 0, 0, written == 0 ? fds : null);
                }
                else
                {
                    written += SendmsgShort(buffer1, offset1 + written, length1 - written, buffer2, offset2, length2, written == 0 ? fds : null);
                }
            }
            if (written != bytes_overall)
            {
                throw new Exception("written != bytes_overall");
            }
        }
示例#3
0
        internal UnixFDArray Clone()
        {
            var res = new UnixFDArray();

            foreach (var fd in FDs)
            {
                res.FDs.Add(fd.Clone());
            }
            return(res);
        }
示例#4
0
 public void AttachBodyTo(MessageWriter writer)
 {
     body          = writer.ToArray();
     header.Length = (uint)body.Length;
     if (writer.fdArray.FDs.Count != 0)
     {
         header[FieldCode.UnixFDs] = (uint)writer.fdArray.FDs.Count;
         if (fdArray == null)
         {
             fdArray = new UnixFDArray();
         }
         foreach (var fd in writer.fdArray.FDs)
         {
             fdArray.FDs.Add(fd);
         }
     }
 }
示例#5
0
 public MessageWriter(EndianFlag endianness)
 {
     this.endianness = endianness;
     stream          = new MemoryStream();
     fdArray         = new UnixFDArray();
 }
示例#6
0
        public static Message FromReceivedBytes(Connection connection, byte[] header, byte[] body, UnixFDArray fdArray)
        {
            Message message = new Message();

            message.connection = connection;
            message.body       = body;
            message.fdArray    = fdArray;
            message.SetHeaderData(header);

            return(message);
        }
        // Send the two buffers and the FDs using sendmsg(), don't handle short writes
        // length1 + length2 must not be 0
        public unsafe long SendmsgShort(byte[] buffer1, long offset1, long length1,
                                        byte[] buffer2, long offset2, long length2,
                                        DBus.Protocol.UnixFDArray fds)
        {
            //Console.WriteLine ("SendmsgShort (X, {0}, {1}, {2}, {3}, {4}, {5})", offset1, length1, buffer2 == null ? "-" : "Y", offset2, length2, fds == null ? "-" : "" + fds.FDs.Count);
            AssertValidBuffer(buffer1, offset1, length1);
            if (buffer2 == null)
            {
                if (length2 != 0)
                {
                    throw new ArgumentOutOfRangeException("length2", "!= 0 while buffer2 == null");
                }
                offset2 = 0;
            }
            else
            {
                AssertValidBuffer(buffer2, offset2, length2);
            }

            fixed(byte *ptr1 = buffer1, ptr2 = buffer2)
            {
                var iovecs = new IOVector[] {
                    new IOVector {
                        Base   = (ptr1 + offset1),
                        Length = (int)length1,
                    },
                    new IOVector {
                        Base   = (ptr2 + offset2),
                        Length = (int)length2,
                    },
                };

                /* Simulate short writes
                 * if (iovecs[0].iov_len == 0) {
                 * iovecs[1].iov_len = Math.Min (iovecs[1].iov_len, 5);
                 * } else {
                 * iovecs[0].iov_len = Math.Min (iovecs[0].iov_len, 5);
                 * iovecs[1].iov_len = 0;
                 * }
                 */
                byte[] cmsg = null;

                // Create copy of FDs to prevent the user from Dispose()ing the
                // FDs in another thread between writing the FDs into the cmsg
                // buffer and calling sendmsg()
                fixed(IOVector *iovecPtr = iovecs)
                {
                    using (var fds2 = fds == null ? null : fds.Clone()) {
                        int fdCount = fds2 == null ? 0 : fds2.FDs.Count;
                        if (fdCount != 0)
                        {
                            // Create one SCM_RIGHTS control message
                            cmsg = new byte[CMSG_SPACE((uint)fdCount * sizeof(int))];
                        }

                        fixed(byte *cmsgPtr = cmsg)
                        {
                            var msghdr = new msghdr {
                                msg_iov        = iovecPtr,
                                msg_iovlen     = length2 == 0 ? 1 : 2,
                                msg_control    = (IntPtr)cmsgPtr,
                                msg_controllen = cmsg == null ? 0 : (uint)cmsg.Length,
                            };

                            if (fdCount != 0)
                            {
                                var hdr = new cmsghdr {
                                    cmsg_len   = (UIntPtr)CMSG_LEN((uint)fdCount * sizeof(int)),
                                    cmsg_level = 1,                    // SOL_SOCKET
                                    cmsg_type  = 1,                    // SCM_RIGHTS
                                };
                                *((cmsghdr *)cmsgPtr) = hdr;
                                var data = CMSG_DATA((cmsghdr *)cmsgPtr);
                                fixed(byte *ptr = cmsg)
                                {
                                    for (int i = 0; i < fdCount; i++)
                                    {
                                        ((int *)data)[i] = fds2.FDs[i].Handle;
                                    }
                                }
                            }
                            long r = socket.SendMsg(&msghdr, 0);

                            if (r == 0)
                            {
                                throw new Exception("sendmsg() returned 0");
                            }
                            return(r);
                        }
                    }
                }
            }
        }