private static void TcpSend(Program.AppMessage msg, int lParam) { PwUuid puID = new PwUuid(true); // One message ID for all processes string strPrefix = TcpGetPrefix(); List <string> lLocks = UrlUtil.GetFilePaths(UrlUtil.GetTempPath(), strPrefix + "*" + IpcTcpSuffix, SearchOption.TopDirectoryOnly); foreach (string strLock in lLocks) { try { string strName = UrlUtil.GetFileName(strLock); ushort uPort = ushort.Parse(strName.Substring(strPrefix.Length, strName.Length - strPrefix.Length - IpcTcpSuffix.Length), NumberFormatInfo.InvariantInfo); if ((uPort < (uint)IpcTcpPortMin) || (uPort >= (uint)IpcTcpPortMaxExcl)) { Debug.Assert(false); continue; } using (TcpClient tc = new TcpClient()) { tc.Connect(IPAddress.Loopback, (int)uPort); using (NetworkStream s = tc.GetStream()) { byte[] pb = TcpCreateMessage(puID, msg, lParam); // Timestamped s.Write(pb, 0, pb.Length); } } } catch (Exception) { Debug.Assert(false); } } }
private static void FswSend(Program.AppMessage msg, int lParam) { FswEnsurePaths(); IpcMessage ipcMsg = new IpcMessage(); byte[] pbID = CryptoRandom.Instance.GetRandomBytes(8); ipcMsg.ID = MemUtil.BytesToUInt64(pbID); ipcMsg.Time = DateTime.UtcNow.ToBinary(); ipcMsg.Message = msg; ipcMsg.LParam = lParam; // Send just to others, not to own m_vProcessedMsgs.Add(ipcMsg); for (int r = 0; r < IpcComRetryCount; ++r) { try { List <IpcMessage> l = ReadMessagesPriv(); CleanOldMessages(l); l.Add(ipcMsg); MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); bw.Write(IpcFileSig); bw.Write((uint)l.Count); for (int j = 0; j < l.Count; ++j) { IpcMessage.Serialize(bw, l[j]); } byte[] pbPlain = ms.ToArray(); bw.Close(); ms.Close(); byte[] pbFile = ProtectedData.Protect(pbPlain, IpcOptEnt, DataProtectionScope.CurrentUser); FileStream fsWrite = new FileStream(m_strMsgFilePath, FileMode.Create, FileAccess.Write, FileShare.None); fsWrite.Write(pbFile, 0, pbFile.Length); fsWrite.Close(); break; } catch (Exception) { } Thread.Sleep(IpcComRetryDelay); } CleanOldMessages(m_vProcessedMsgs); }
private static byte[] TcpCreateMessage(PwUuid puID, Program.AppMessage msg, int lParam) { const int cbID = (int)PwUuid.UuidSize; byte[] pb = new byte[cbID + 8 + 4 + 4]; Array.Copy(puID.UuidBytes, pb, cbID); MemUtil.Int64ToBytesEx(DateTime.UtcNow.ToBinary(), pb, cbID); MemUtil.Int32ToBytesEx((int)msg, pb, cbID + 8); MemUtil.Int32ToBytesEx(lParam, pb, cbID + 8 + 4); byte[] pbEnc = CryptoUtil.ProtectData(pb, IpcTcpOptEnt, DataProtectionScope.CurrentUser); byte[] pbSend = new byte[1 + 4 + pbEnc.Length]; pbSend[0] = 1; // Message type MemUtil.Int32ToBytesEx(pbEnc.Length, pbSend, 1); Array.Copy(pbEnc, 0, pbSend, 1 + 4, pbEnc.Length); Debug.Assert(pbSend.Length <= IpcTcpMsgSizeMax); return(pbSend); }
public static void Send(Program.AppMessage msg, int lParam, bool bWaitWithTimeout) { if (!KeePassLib.Native.NativeLib.IsUnix()) // Windows { if (bWaitWithTimeout) { IntPtr pResult = new IntPtr(0); NativeMethods.SendMessageTimeout((IntPtr)NativeMethods.HWND_BROADCAST, Program.ApplicationMessage, (IntPtr)msg, (IntPtr)lParam, NativeMethods.SMTO_ABORTIFHUNG, 5000, ref pResult); } else { NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, Program.ApplicationMessage, (IntPtr)msg, (IntPtr)lParam); } } else // Unix { if (m_chClient == null) { m_chClient = new IpcClientChannel(); ChannelServices.RegisterChannel(m_chClient, false); } try { IpcBroadcastSingleton ipc = (Activator.GetObject(typeof( IpcBroadcastSingleton), "ipc://" + IpcServerPortName + "/" + IpcObjectName) as IpcBroadcastSingleton); if (ipc != null) { ipc.Call((int)msg, lParam); } } catch (Exception) { } // Server might not exist } }
// private static IpcServerChannel m_chServer = null; // private static IpcClientChannel m_chClient = null; // private const string IpcServerPortName = "KeePassBroadcastPort"; // private const string IpcObjectName = "KeePassBroadcastSingleton"; public static void Send(Program.AppMessage msg, int lParam, bool bWaitWithTimeout) { if (!NativeLib.IsUnix()) // Windows { if (bWaitWithTimeout) { IntPtr pResult = IntPtr.Zero; NativeMethods.SendMessageTimeout((IntPtr)NativeMethods.HWND_BROADCAST, Program.ApplicationMessage, (IntPtr)msg, (IntPtr)lParam, NativeMethods.SMTO_ABORTIFHUNG, 5000, ref pResult); } else { NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, Program.ApplicationMessage, (IntPtr)msg, (IntPtr)lParam); } } else // Unix { // if(m_chClient == null) // { // m_chClient = new IpcClientChannel(); // ChannelServices.RegisterChannel(m_chClient, false); // } // try // { // IpcBroadcastSingleton ipc = (Activator.GetObject(typeof( // IpcBroadcastSingleton), "ipc://" + GetPortName() + "/" + // IpcObjectName) as IpcBroadcastSingleton); // if(ipc != null) ipc.Call((int)msg, lParam); // } // catch(Exception) { } // Server might not exist // FswSend(msg, lParam); TcpSend(msg, lParam); } }