source: trunk/CrypPlugins/PeerToPeer/DHT_CommonManagement.cs @ 1433

Last change on this file since 1433 was 1433, checked in by Paul Lelgemann, 12 years ago

o Extracted common classes from PeerToPeerBase plugin into new PeerToPeer plugin as a preparation for the new P2P proxy
o Modified directory properties to ignore the CrypBuild directory

File size: 11.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Cryptool.PluginBase.Control;
6using Cryptool.PluginBase.IO;
7
8namespace Cryptool.Plugins.PeerToPeer.Internal
9{
10    public static class DHT_CommonManagement
11    {
12        private static Encoding enc = Encoding.UTF8;
13        private static string PublishersTimeStamp = "PubTimeStamp";
14        private static string AliveMsg = "AliveMsg";
15        private static string EncryptedData = "EncryptedText";
16        private static string InitializationVector = "InitializationVector";
17
18        /// <summary>
19        /// Store the ID of the Publisher/Manager, so all Subscribers/Workers can retrieve the
20        /// Publishers/Managers ID from the DHT to register themselves.
21        /// </summary>
22        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
23        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
24        /// <param name="publishersId">PeerId of the Publisher/Manager</param>
25        /// <returns></returns>
26        public static bool SetTopicsPublisherId(ref IP2PControl p2pControl, string sTopicName, PeerId publishersId)
27        {
28            return p2pControl.DHTstore(sTopicName, publishersId.ToByteArray());
29        }
30
31        /// <summary>
32        /// Loads the actual Publishers-/Managers-ID out of the DHT.
33        /// </summary>
34        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
35        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
36        /// <returns>PeerId, if already in the DHT, otherwise null</returns>
37        public static PeerId GetTopicsPublisherId(ref IP2PControl p2pControl, string sTopicName)
38        {
39            PeerId pid = null;
40            byte[] byteLoad = p2pControl.DHTload(sTopicName);
41            if(byteLoad != null)
42                pid = p2pControl.GetPeerID(byteLoad);
43            return pid;
44        }
45
46        /// <summary>
47        /// Store alive message interval in DHT - necessary for maintanance all Subscriber/Worker-Lists.
48        /// </summary>
49        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
50        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
51        /// <param name="aliveMessageInterval">AliveMessageInterval in milliseconds</param>
52        /// <returns>true, if storing value in the DHT was successfull.</returns>
53        public static bool SetAliveMessageInterval(ref IP2PControl p2pControl, string sTopicName, long aliveMessageInterval)
54        {
55            return p2pControl.DHTstore(sTopicName + AliveMsg, System.BitConverter.GetBytes(aliveMessageInterval));
56        }
57
58        /// <summary>
59        /// Alive message interval, prescribed by the Publisher/Manager.
60        /// In this interval - in milliseconds - the Subscribers/Workers have to send a alive message,
61        /// otherwise they will be removed from the Subscriber-/Worker-List.
62        /// </summary>
63        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
64        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
65        /// <returns></returns>
66        public static long GetAliveMessageInterval(ref IP2PControl p2pControl, string sTopicName)
67        {
68            long ret = 0;
69            byte[] byteLoad = p2pControl.DHTload(sTopicName + AliveMsg);
70            if (byteLoad != null)
71                ret = System.BitConverter.ToInt64(byteLoad, 0);
72            return ret;
73        }
74
75        /// <summary>
76        /// Storing encrypted Data in the DHT. This is necessary for all workers, who want to bruteforce
77        /// the encryption.
78        /// </summary>
79        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
80        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
81        /// <param name="cStream">CryptoolStream from the Encryption-PlugIn</param>
82        /// <returns></returns>
83        public static bool SetEncryptedData(ref IP2PControl p2pControl, string sTopicName, CryptoolStream cStream)
84        {
85            bool encryptedTextStored = false;
86            if (cStream != null && sTopicName != null && sTopicName != String.Empty && p2pControl != null)
87            {
88                CryptoolStream newEncryptedData = new CryptoolStream();
89                newEncryptedData.OpenRead(cStream.FileName);
90                if (newEncryptedData.CanRead)
91                {
92                    // Convert CryptoolStream to an byte Array and store it in the DHT
93                    if (newEncryptedData.Length > Int32.MaxValue)
94                        throw (new Exception("Encrypted Data are too long for this PlugIn. The maximum size of Data is " + Int32.MaxValue + "!"));
95                    byte[] byteEncryptedData = new byte[newEncryptedData.Length];
96                    int k = newEncryptedData.Read(byteEncryptedData, 0, byteEncryptedData.Length);
97                    if (k < byteEncryptedData.Length)
98                        throw (new Exception("Read Data are shorter than byteArrayLen"));
99                    encryptedTextStored = p2pControl.DHTstore(sTopicName + EncryptedData, byteEncryptedData);
100                }
101                else
102                {
103                    throw (new Exception("Reading the CryptoolStream wasn't possible"));
104                }
105            }
106            return encryptedTextStored;
107        }
108
109        /// <summary>
110        /// Functions necessary for P2PWorkers. You will get the EncryptedData, necessary
111        /// for bruteforcing.
112        /// </summary>
113        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
114        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
115        /// <returns></returns>
116        public static byte[] GetEncryptedData(ref IP2PControl p2pControl, string sTopicName)
117        {
118            byte[] ret = null;
119            if (p2pControl != null && sTopicName != null && sTopicName != String.Empty)
120            {
121                return p2pControl.DHTload(sTopicName + EncryptedData);
122            }
123            return ret;
124        }
125
126        /// <summary>
127        /// Storing initialization vector if neccessary (some cipher-block-mode (e.g. CBC, OFB))
128        /// </summary>
129        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
130        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
131        /// <param name="initVector">the init vector</param>
132        /// <returns>true, if storing value in the DHT was successfull.</returns>
133        public static bool SetInitializationVector(ref IP2PControl p2pControl, string sTopicName, byte[] initVector)
134        {
135            return p2pControl.DHTstore(sTopicName + InitializationVector, initVector);
136        }
137
138        /// <summary>
139        /// Functions necessary for P2PWorkers. You will get the Initialization Vector, necessary
140        /// for some cipher-block-mode (e.g. CBC, OFB) decryption.
141        /// </summary>
142        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
143        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
144        /// <returns>the initialization vector if available, otherwise null</returns>
145        public static byte[] GetInitializationVector(ref IP2PControl p2pControl, string sTopicName)
146        {
147            return p2pControl.DHTload(sTopicName + InitializationVector);
148        }
149
150        /// <summary>
151        /// Removes all entries out of the DHT, which maintain the Publish/Subscriber-Network
152        /// </summary>
153        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
154        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
155        /// <returns></returns>
156        public static bool DeleteAllPublishersEntries(ref IP2PControl p2pControl, string sTopicName)
157        {
158            bool id = p2pControl.DHTremove(sTopicName);
159            bool alive = p2pControl.DHTremove(sTopicName + AliveMsg);
160            bool timestamp = p2pControl.DHTremove(sTopicName + PublishersTimeStamp);
161            //if timestamp wasn't removed successfully, that's no problem!
162            return (id && alive);
163        }
164
165        /// <summary>
166        /// Removes all entries out of the DHT, which maintain the Manager/Workers-Network
167        /// </summary>
168        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
169        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
170        /// <returns></returns>
171        public static bool DeleteAllManagersEntries(ref IP2PControl p2pControl, string sTopicName)
172        {
173            bool encrypt = p2pControl.DHTremove(sTopicName + EncryptedData);
174            bool init = p2pControl.DHTremove(sTopicName + InitializationVector);
175            return (encrypt && init);
176        }
177
178        // new stuff - Arnie 2010.02.02
179       
180        ///// <summary>
181        ///// Sets or updates the TimeStamp for the actual Publisher
182        ///// </summary>
183        ///// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
184        ///// <param name="sTopicName">the topic name on which all Peers are registered</param>
185        ///// <returns>true, if storing succeeds, otherwise false</returns>
186        //public static bool SetPublishersTimeStamp(ref IP2PControl p2pControl, string sTopicName)
187        //{
188        //    byte[] timeStamp = enc.GetBytes(DateTime.Now.ToString());
189        //    bool store = p2pControl.DHTstore(sTopicName + PublishersTimeStamp, timeStamp);
190        //}
191
192        ///// <summary>
193        /////  Gets the TimeStamp for the actual Publisher, if it's stored in the DHT (when a Publisher
194        /////  is active, this value have to be occupied). Otherwise, when no Publisher is active, returns null.
195        /////  If the DHT value is occupied, but parsing to DateTime wasn't possible, an Exception will be thrown
196        ///// </summary>
197        ///// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
198        ///// <param name="sTopicName">the topic name on which all Peers are registered</param>
199        ///// <returns>DateTime, if DHT entry is occupied, otherwise false.</returns>
200        //public static DateTime GetPublishersTimeStamp(ref IP2PControl p2pControl, string sTopicName)
201        //{
202        //    DateTime retTimeStamp = null;
203        //    byte[] timeStamp = p2pControl.DHTload(sTopicName + PublishersTimeStamp);
204        //    //when timeStamp is null, DHT entry isn't occupied --> No Publisher is active at present
205        //    if (timeStamp != null)
206        //    {
207        //        string sTimeStamp = enc.GetString(timeStamp);
208        //        // if parsing isn't possible though DHT entry is occupied, throw an exception
209        //        if (!DateTime.TryParse(sTimeStamp, out retTimeStamp))
210        //        {
211        //            throw (new Exception("Parsing the DHT entry '" + sTopicName + PublishersTimeStamp
212        //                + "' to DateTime wasn't possible. Value: '" + sTimeStamp + "'"));
213        //        }
214        //    }
215        //    return retTimeStamp;
216        //}
217    }
218}
219
Note: See TracBrowser for help on using the repository browser.