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

Last change on this file since 1458 was 1458, checked in by Paul Lelgemann, 11 years ago
  • Removed unused references and using statement in CrypP2P, PeerToPeer, PeerToPeerBaseProxy

+ Skeleton for P2PEditor

File size: 11.4 KB
Line 
1using System;
2using System.Text;
3using Cryptool.PluginBase.IO;
4
5namespace Cryptool.Plugins.PeerToPeer.Internal
6{
7    public static class DHT_CommonManagement
8    {
9        private static Encoding enc = Encoding.UTF8;
10        private static string PublishersTimeStamp = "PubTimeStamp";
11        private static string AliveMsg = "AliveMsg";
12        private static string EncryptedData = "EncryptedText";
13        private static string InitializationVector = "InitializationVector";
14
15        /// <summary>
16        /// Store the ID of the Publisher/Manager, so all Subscribers/Workers can retrieve the
17        /// Publishers/Managers ID from the DHT to register themselves.
18        /// </summary>
19        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
20        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
21        /// <param name="publishersId">PeerId of the Publisher/Manager</param>
22        /// <returns></returns>
23        public static bool SetTopicsPublisherId(ref IP2PControl p2pControl, string sTopicName, PeerId publishersId)
24        {
25            return p2pControl.DHTstore(sTopicName, publishersId.ToByteArray());
26        }
27
28        /// <summary>
29        /// Loads the actual Publishers-/Managers-ID out of the DHT.
30        /// </summary>
31        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
32        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
33        /// <returns>PeerId, if already in the DHT, otherwise null</returns>
34        public static PeerId GetTopicsPublisherId(ref IP2PControl p2pControl, string sTopicName)
35        {
36            PeerId pid = null;
37            byte[] byteLoad = p2pControl.DHTload(sTopicName);
38            if (byteLoad != null)
39                pid = p2pControl.GetPeerID(byteLoad);
40            return pid;
41        }
42
43        /// <summary>
44        /// Store alive message interval in DHT - necessary for maintanance all Subscriber/Worker-Lists.
45        /// </summary>
46        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
47        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
48        /// <param name="aliveMessageInterval">AliveMessageInterval in milliseconds</param>
49        /// <returns>true, if storing value in the DHT was successfull.</returns>
50        public static bool SetAliveMessageInterval(ref IP2PControl p2pControl, string sTopicName,
51                                                   long aliveMessageInterval)
52        {
53            return p2pControl.DHTstore(sTopicName + AliveMsg, BitConverter.GetBytes(aliveMessageInterval));
54        }
55
56        /// <summary>
57        /// Alive message interval, prescribed by the Publisher/Manager.
58        /// In this interval - in milliseconds - the Subscribers/Workers have to send a alive message,
59        /// otherwise they will be removed from the Subscriber-/Worker-List.
60        /// </summary>
61        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
62        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
63        /// <returns></returns>
64        public static long GetAliveMessageInterval(ref IP2PControl p2pControl, string sTopicName)
65        {
66            long ret = 0;
67            byte[] byteLoad = p2pControl.DHTload(sTopicName + AliveMsg);
68            if (byteLoad != null)
69                ret = BitConverter.ToInt64(byteLoad, 0);
70            return ret;
71        }
72
73        /// <summary>
74        /// Storing encrypted Data in the DHT. This is necessary for all workers, who want to bruteforce
75        /// the encryption.
76        /// </summary>
77        /// <param name="p2pControl">necessary P2PControl to use DHT operation(s)</param>
78        /// <param name="sTopicName">the topic name on which all Peers are registered</param>
79        /// <param name="cStream">CryptoolStream from the Encryption-PlugIn</param>
80        /// <returns></returns>
81        public static bool SetEncryptedData(ref IP2PControl p2pControl, string sTopicName, CryptoolStream cStream)
82        {
83            bool encryptedTextStored = false;
84            if (cStream != null && sTopicName != null && sTopicName != String.Empty && p2pControl != null)
85            {
86                var newEncryptedData = new CryptoolStream();
87                newEncryptedData.OpenRead(cStream.FileName);
88                if (newEncryptedData.CanRead)
89                {
90                    // Convert CryptoolStream to an byte Array and store it in the DHT
91                    if (newEncryptedData.Length > Int32.MaxValue)
92                        throw (new Exception(
93                            "Encrypted Data are too long for this PlugIn. The maximum size of Data is " + Int32.MaxValue +
94                            "!"));
95                    var 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}
Note: See TracBrowser for help on using the repository browser.