Пример #1
0
        void AcceptConnections()
        {
            var buffer = new byte[16];

            while (true)
            {
                try {
                    var clientSocket = socket.Accept();
                    var clientThread = new Thread(() =>
                    {
                        try {
                            using (var stream = new NetworkStream(clientSocket)) {
                                try {
                                    var clientPort           = ((IPEndPoint)clientSocket.RemoteEndPoint).Port;
                                    var clientWaitHandleName = string.Format("{0}.{1}.{2}",
                                                                             waitHandleNamePrefix, (UInt16)IPAddress.HostToNetworkOrder((Int16)clientPort), guidString);
                                    var clientWaitHandle = EventWaitHandle.OpenExisting(clientWaitHandleName);
                                    if (!EventWaitHandle.SignalAndWait(serverWaitHandle, clientWaitHandle, 10000, false))
                                    {
                                        return;
                                    }
                                } catch (Exception ex) {
                                    Debug.Fail(ex.ToString());
                                }
                                Process proc = null;
                                try {
                                    // remote and local are swapped because we are doing reverse lookup
                                    proc = WinInternals.GetProcessForTcpPort(
                                        (IPEndPoint)clientSocket.RemoteEndPoint,
                                        (IPEndPoint)clientSocket.LocalEndPoint);
                                } catch (Exception ex) {
                                    Debug.Fail(ex.ToString());
                                }
                                if (ConnectionHandler != null)
                                {
                                    ConnectionHandler(stream, proc);
                                }
                            }
                        } catch {
                            // can throw if remote closes the connection at a bad time
                        } finally {
                            lock (clientSocketsLock) {
                                clientSockets.Remove(clientSocket);
                            }
                        }
                    });
                    lock (clientSocketsLock) {
                        clientSockets.Add(clientSocket);
                    }
                    clientThread.Name = string.Format("MsysClient{0}", clientCount++);
                    clientThread.Start();
                } catch (Exception ex) {
                    Debug.Assert(disposed, ex.ToString());
                    break;
                }
            }
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private IntPtr CustomWndProc(IntPtr hWnd, uint msg, IntPtr wParam,
                                     IntPtr lParam)
        {
            // we only care about COPYDATA messages
            if (msg != WM_COPYDATA)
            {
                return(DefWindowProcW(hWnd, msg, wParam, lParam));
            }

            IntPtr result = IntPtr.Zero;

            // convert lParam to something usable
            COPYDATASTRUCT copyData = (COPYDATASTRUCT)
                                      Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));

            if (((IntPtr.Size == 4) &&
                 (copyData.dwData.ToInt32() != (unchecked ((int)AGENT_COPYDATA_ID)))) ||
                ((IntPtr.Size == 8) &&
                 (copyData.dwData.ToInt64() != AGENT_COPYDATA_ID)))
            {
                return(result); // failure
            }

            string mapname = Marshal.PtrToStringAnsi(copyData.lpData);

            if (mapname.Length != copyData.cbData - 1)
            {
                return(result); // failure
            }

            try {
                using (MemoryMappedFile fileMap =
                           MemoryMappedFile.OpenExisting(mapname,
                                                         MemoryMappedFileRights.FullControl)) {
                    if (fileMap.SafeMemoryMappedFileHandle.IsInvalid)
                    {
                        return(result); // failure
                    }

                    SecurityIdentifier mapOwner =
                        (SecurityIdentifier)fileMap.GetAccessControl()
                        .GetOwner(typeof(System.Security.Principal.SecurityIdentifier));

                    /* check to see if message sender is same user as this program's
                     * user */

                    var user    = WindowsIdentity.GetCurrent();
                    var userSid = user.User;

                    // see http://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/pageant-backwards-compatibility.html
                    var procOwnerSid = GetProcessOwnerSID(Process.GetCurrentProcess().Id);

                    Process otherProcess = null;
                    try {
                        otherProcess = WinInternals.FindProcessWithMatchingHandle(fileMap);
                    } catch (Exception ex) {
                        Debug.Fail(ex.ToString());
                    }

                    if (userSid == mapOwner || procOwnerSid == mapOwner)
                    {
                        using (MemoryMappedViewStream stream = fileMap.CreateViewStream()) {
                            AnswerMessage(stream, otherProcess);
                        }
                        result = new IntPtr(1);
                        return(result); // success
                    }
                }
            } catch (Exception ex) {
                Debug.Fail(ex.ToString());
            }
            return(result); // failure
        }
Пример #3
0
        void AcceptConnections()
        {
            var buffer = new byte[16];

            while (true)
            {
                try {
                    var clientSocket = socket.Accept();
                    var clientThread = new Thread(() => {
                        try {
                            using (var stream = new NetworkStream(clientSocket)) {
                                stream.Read(buffer, 0, 16);
                                var incomingGuid = new Guid(buffer);
                                if (incomingGuid != guid)
                                {
                                    return;
                                }
                                stream.Write(buffer, 0, 16);
                                stream.Flush();
                                stream.Read(buffer, 0, 12);
                                var pid = BitConverter.ToInt32(buffer, 0);
                                var gid = BitConverter.ToInt32(buffer, 4);
                                var uid = BitConverter.ToInt32(buffer, 8);
                                // FIXME: This should be a cygwin pid, not a windows pid
                                // seems to work fine though
                                pid = Process.GetCurrentProcess().Id;
                                Array.Copy(BitConverter.GetBytes(pid), buffer, 4);
                                stream.Write(buffer, 0, 12);
                                stream.Flush();
                                Process proc = null;
                                try {
                                    // remote and local are swapped because we are doing reverse lookup
                                    proc = WinInternals.GetProcessForTcpPort(
                                        (IPEndPoint)clientSocket.RemoteEndPoint,
                                        (IPEndPoint)clientSocket.LocalEndPoint);
                                } catch (Exception ex) {
                                    Debug.Fail(ex.ToString());
                                }
                                if (ConnectionHandler != null)
                                {
                                    ConnectionHandler(stream, proc);
                                }
                            }
                        } catch {
                            // can throw if remote closes the connection at a bad time
                        } finally {
                            lock (clientSocketsLock) {
                                clientSockets.Remove(clientSocket);
                            }
                        }
                    });
                    lock (clientSocketsLock) {
                        clientSockets.Add(clientSocket);
                    }
                    clientThread.Name = string.Format("CygwinClient{0}", clientCount++);
                    clientThread.Start();
                } catch (Exception ex) {
                    Debug.Assert(disposed, ex.ToString());
                    break;
                }
            }
        }