Added check and failover of hash algorithms: OpenSSL > Cng > Default
authorpkanavos <pkanavos@gmail.com>
Tue, 13 Nov 2012 16:36:43 +0000 (18:36 +0200)
committerpkanavos <pkanavos@gmail.com>
Tue, 13 Nov 2012 16:36:43 +0000 (18:36 +0200)
Added larger sleep when Block commits fail due to open files

trunk/Pithos.Client.WPF/App.xaml.cs
trunk/Pithos.Client.WPF/Properties/Settings.Designer.cs
trunk/Pithos.Client.WPF/Properties/Settings.settings
trunk/Pithos.Client.WPF/app.config
trunk/Pithos.Core/Agents/BlockUpdater.cs
trunk/Pithos.Network/BlockHashAlgorithms.cs
trunk/Pithos.Network/SHA1OpenSSL.cs

index 3568ea8..89dd975 100644 (file)
@@ -45,6 +45,7 @@ using System.IO;
 using System.Linq;\r
 using System.Net;\r
 using System.Reflection;\r
+using System.Security.Cryptography;\r
 using System.Text;\r
 using System.Threading;\r
 using System.Threading.Tasks;\r
@@ -53,6 +54,7 @@ using Caliburn.Micro;
 using Pithos.Client.WPF.Configuration;\r
 using Pithos.Client.WPF.Properties;\r
 using Pithos.Interfaces;\r
+using Pithos.Network;\r
 using log4net.Appender;\r
 using log4net.Core;\r
 using log4net.Repository.Hierarchy;\r
@@ -78,7 +80,8 @@ namespace Pithos.Client.WPF
             //var instanceMutex=new Mutex()\r
             InitializeLogging();\r
 \r
-            \r
+            RegisterOpenSSL();\r
+\r
 \r
             //Detect OS, if Major Version is 6+ use RestartManager\r
             OperatingSystem os = Environment.OSVersion;\r
@@ -110,6 +113,37 @@ namespace Pithos.Client.WPF
             InitializeComponent();            \r
         }\r
 \r
+        private static void RegisterOpenSSL()\r
+        {\r
+\r
+            //Try to instantiate OpenSSL and hash an empty array\r
+            try\r
+            {\r
+                using(var testHasher=new SHA256OpenSSL())\r
+                {\r
+                    testHasher.ComputeHash(new byte[0]);\r
+                }\r
+                CryptoConfig.AddAlgorithm(typeof(SHA256OpenSSL), "SHA256");\r
+                Log.InfoFormat("Registered OpenSSL hash provider");\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                Log.WarnFormat("OpenSSL instantiation failed. Falling back to CNG provider.\r\n[{0}]",exc);                \r
+            }\r
+            //Running on Vista + ?\r
+            if (Environment.OSVersion.Version.Major >= 6)\r
+            {\r
+                CryptoConfig.AddAlgorithm(typeof(SHA256Cng), "SHA256");\r
+                Log.InfoFormat("Registered CNG hash provider");\r
+            }\r
+            else\r
+            {\r
+                Log.WarnFormat("Running on XP. Falling back to default hash algorithm.");\r
+            }\r
+\r
+            \r
+        }\r
+\r
 \r
 /*        private static string[] platformLibraries = {"libeay32.dll","ssleay32.dll"};\r
         private static Assembly CustomResolve(object sender,ResolveEventArgs args)\r
index 03b0c0b..7f98040 100644 (file)
@@ -389,21 +389,22 @@ namespace Pithos.Client.WPF.Properties {
         \r
         [global::System.Configuration.ApplicationScopedSettingAttribute()]\r
         [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\r
-        [global::System.Configuration.DefaultSettingValueAttribute(@"<?xml version=""1.0"" encoding=""utf-16""?>\r
-<ArrayOfServerSettings xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">\r
-  <ServerSettings>\r
-<ServerName>Pithos Production</ServerName>\r
-    <ServerUri>https://pithos.okeanos.grnet.gr</ServerUri>\r
-    <LoginUri>https://pithos.okeanos.grnet.gr/login</LoginUri>\r
-    <LogoutUri>https://accounts.okeanos.grnet.gr/im/logout</LogoutUri>\r
-  </ServerSettings>\r
-  <ServerSettings>\r
-<ServerName>Pithos Development</ServerName>\r
-    <ServerUri>https://pithos.dev.grnet.gr</ServerUri>\r
-    <LoginUri>https://pithos.dev.grnet.gr/login</LoginUri>\r
-    <LogoutUri>https://pithos.dev.grnet.gr/im/logout</LogoutUri>\r
-  </ServerSettings>\r
-</ArrayOfServerSettings>")]\r
+        [global::System.Configuration.DefaultSettingValueAttribute(@"\r
+          <ArrayOfServerSettings xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">\r
+            <ServerSettings>\r
+              <ServerName>Pithos Production</ServerName>\r
+              <ServerUri>https://pithos.okeanos.grnet.gr</ServerUri>\r
+              <LoginUri>https://pithos.okeanos.grnet.gr/login</LoginUri>\r
+              <LogoutUri>https://accounts.okeanos.grnet.gr/im/logout</LogoutUri>\r
+            </ServerSettings>\r
+            <ServerSettings>\r
+              <ServerName>Pithos Development</ServerName>\r
+              <ServerUri>https://pithos.dev.grnet.gr</ServerUri>\r
+              <LoginUri>https://pithos.dev.grnet.gr/login</LoginUri>\r
+              <LogoutUri>https://pithos.dev.grnet.gr/im/logout</LogoutUri>\r
+            </ServerSettings>\r
+          </ArrayOfServerSettings>\r
+        ")]\r
         public global::Pithos.Interfaces.ServersCollection Servers {\r
             get {\r
                 return ((global::Pithos.Interfaces.ServersCollection)(this["Servers"]));\r
index 093c3b4..4315ad8 100644 (file)
       <Value Profile="(Default)">False</Value>\r
     </Setting>\r
     <Setting Name="Servers" Type="Pithos.Interfaces.ServersCollection" Scope="Application">\r
-      <Value Profile="(Default)">&lt;?xml version="1.0" encoding="utf-16"?&gt;\r
-&lt;ArrayOfServerSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;\r
-  &lt;ServerSettings&gt;\r
-&lt;ServerName&gt;Pithos Production&lt;/ServerName&gt;\r
-    &lt;ServerUri&gt;https://pithos.okeanos.grnet.gr&lt;/ServerUri&gt;\r
-    &lt;LoginUri&gt;https://pithos.okeanos.grnet.gr/login&lt;/LoginUri&gt;\r
-    &lt;LogoutUri&gt;https://accounts.okeanos.grnet.gr/im/logout&lt;/LogoutUri&gt;\r
-  &lt;/ServerSettings&gt;\r
-  &lt;ServerSettings&gt;\r
-&lt;ServerName&gt;Pithos Development&lt;/ServerName&gt;\r
-    &lt;ServerUri&gt;https://pithos.dev.grnet.gr&lt;/ServerUri&gt;\r
-    &lt;LoginUri&gt;https://pithos.dev.grnet.gr/login&lt;/LoginUri&gt;\r
-    &lt;LogoutUri&gt;https://pithos.dev.grnet.gr/im/logout&lt;/LogoutUri&gt;\r
-  &lt;/ServerSettings&gt;\r
-&lt;/ArrayOfServerSettings&gt;</Value>\r
+      <Value Profile="(Default)">\r
+          &lt;ArrayOfServerSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;\r
+            &lt;ServerSettings&gt;\r
+              &lt;ServerName&gt;Pithos Production&lt;/ServerName&gt;\r
+              &lt;ServerUri&gt;https://pithos.okeanos.grnet.gr&lt;/ServerUri&gt;\r
+              &lt;LoginUri&gt;https://pithos.okeanos.grnet.gr/login&lt;/LoginUri&gt;\r
+              &lt;LogoutUri&gt;https://accounts.okeanos.grnet.gr/im/logout&lt;/LogoutUri&gt;\r
+            &lt;/ServerSettings&gt;\r
+            &lt;ServerSettings&gt;\r
+              &lt;ServerName&gt;Pithos Development&lt;/ServerName&gt;\r
+              &lt;ServerUri&gt;https://pithos.dev.grnet.gr&lt;/ServerUri&gt;\r
+              &lt;LoginUri&gt;https://pithos.dev.grnet.gr/login&lt;/LoginUri&gt;\r
+              &lt;LogoutUri&gt;https://pithos.dev.grnet.gr/im/logout&lt;/LogoutUri&gt;\r
+            &lt;/ServerSettings&gt;\r
+          &lt;/ArrayOfServerSettings&gt;\r
+        </Value>\r
     </Setting>\r
   </Settings>\r
 </SettingsFile>
\ No newline at end of file
index 0730c96..3c03440 100644 (file)
       </setting>\r
       <setting name="Servers" serializeAs="Xml">\r
         <value>\r
-          <ArrayOfServerSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
-            xmlns:xsd="http://www.w3.org/2001/XMLSchema">\r
+          <ArrayOfServerSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">\r
             <ServerSettings>\r
               <ServerName>Pithos Production</ServerName>\r
               <ServerUri>https://pithos.okeanos.grnet.gr</ServerUri>\r
       </filter>\r
     </appender>\r
     <appender name="DumpFileAppender" type="log4net.Appender.RollingFileAppender">\r
-      <file type="log4net.Util.PatternString"  value="%env{LOCALAPPDATA}\GRNET\PITHOS\errorlog.xml" />\r
+      <file type="log4net.Util.PatternString" value="%env{LOCALAPPDATA}\GRNET\PITHOS\errorlog.xml" />\r
       <appendToFile value="false" />\r
       <rollingStyle value="Size" />\r
       <maxSizeRollBackups value="10" />\r
       <layout type="log4net.Layout.XMLLayout" />\r
     </appender>\r
     <appender name="DebugFileAppender" type="log4net.Appender.RollingFileAppender">\r
-      <file type="log4net.Util.PatternString"  value="%env{LOCALAPPDATA}\GRNET\PITHOS\debuglog.xml" />\r
+      <file type="log4net.Util.PatternString" value="%env{LOCALAPPDATA}\GRNET\PITHOS\debuglog.xml" />\r
       <appendToFile value="true" />\r
       <rollingStyle value="Size" />\r
       <maxSizeRollBackups value="10" />\r
       <layout type="log4net.Layout.XMLLayout" />\r
     </appender>\r
     <appender name="NetworkFileAppender" type="log4net.Appender.RollingFileAppender">\r
-      <file type="log4net.Util.PatternString"  value="%env{LOCALAPPDATA}\GRNET\PITHOS\network.log" />\r
+      <file type="log4net.Util.PatternString" value="%env{LOCALAPPDATA}\GRNET\PITHOS\network.log" />\r
       <appendToFile value="true" />\r
       <rollingStyle value="Size" />\r
       <maxSizeRollBackups value="10" />\r
index 03df6bd..44b23fb 100644 (file)
@@ -238,9 +238,9 @@ namespace Pithos.Core.Agents
                                 }\r
                                 catch (IOException)\r
                                 {\r
-                                    if (i>=3)\r
+                                    if (i>=2)\r
                                         throw;\r
-                                    Thread.Sleep(100);\r
+                                    Thread.Sleep((i+1)*300);\r
                                 }                                \r
                             }\r
                         }\r
index 38dba4f..c129a34 100644 (file)
@@ -43,11 +43,12 @@ using System.Collections.Concurrent;
 using System.Diagnostics.Contracts;\r
 using System.IO;\r
 using System.Reflection;\r
+using System.Security.Cryptography;\r
 using System.ServiceModel.Channels;\r
 using System.Threading;\r
 using System.Threading.Tasks;\r
 using System.Threading.Tasks.Dataflow;\r
-using OpenSSL.Crypto;\r
+\r
 \r
 namespace Pithos.Network\r
 {\r
@@ -251,10 +252,9 @@ namespace Pithos.Network
             if (size == 0)\r
             {\r
                 var buf = new byte[0];\r
-                using (var hasher = new MessageDigestContext(MessageDigest.CreateByName(algorithm)))\r
-                {                    \r
-                    hasher.Init();\r
-                    hashes[0] = hasher.Digest(buf);\r
+                using (var hasher = HashAlgorithm.Create(algorithm))\r
+                {\r
+                    hashes[0] = hasher.ComputeHash(buf);\r
                     return hashes;\r
                 }\r
             }\r
@@ -264,13 +264,12 @@ namespace Pithos.Network
             var actualHashers = parallelism > blocks ? (byte)blocks : parallelism;\r
 \r
             var buffer = new byte[actualHashers][];\r
-            var hashers = new MessageDigestContext[actualHashers];\r
+            var hashers = new HashAlgorithm[actualHashers];\r
             var bufferManager = GetBufferManager(blockSize, actualHashers);\r
             for (var i = 0; i < actualHashers; i++)\r
             {\r
                 buffer[i] = bufferManager.TakeBuffer(blockSize);// new byte[blockSize];\r
-                hashers[i] = new MessageDigestContext(MessageDigest.CreateByName(algorithm));\r
-                hashers[i].Init();\r
+                hashers[i] = HashAlgorithm.Create(algorithm);                \r
             }\r
             try\r
             {\r
@@ -307,7 +306,8 @@ namespace Pithos.Network
 \r
                                                             var hasher = hashers[idx];\r
 \r
-                                                            byte[] hash;\r
+                                                            byte[] hash=hasher.ComputeHash(buffer[idx],0,lastByteIndex+1);\r
+/*\r
                                                             if (buffer[idx].Length == lastByteIndex || lastByteIndex==-1)\r
                                                                 hash = hasher.Digest(buffer[idx]);\r
                                                             else\r
@@ -316,6 +316,7 @@ namespace Pithos.Network
                                                                 Buffer.BlockCopy(buffer[idx],0,buf,0,lastByteIndex+1);\r
                                                                 hash = hasher.Digest(buf);\r
                                                             }\r
+*/\r
 \r
                                                             \r
                                                             \r
index 7b60a3d..5fa968e 100644 (file)
@@ -51,4 +51,49 @@ namespace Pithos.Network
             }\r
         }\r
     }\r
+    \r
+    public class SHA256OpenSSL : SHA256\r
+    {\r
+\r
+        private MessageDigestContext _context;\r
+\r
+        public SHA256OpenSSL()\r
+        {\r
+            _context = new MessageDigestContext(MessageDigest.CreateByName("SHA256"));\r
+            _context.Init();\r
+        }\r
+\r
+        public override void Initialize()\r
+        {\r
+            _context.Init();\r
+        }\r
+\r
+        protected override void HashCore(byte[] array, int ibStart, int cbSize)\r
+        {\r
+            if (array.Length == cbSize)\r
+                _context.Update(array);\r
+            else\r
+            {\r
+                var block = new byte[cbSize];\r
+                Buffer.BlockCopy(array, ibStart, block, 0, cbSize);\r
+                _context.Update(block);\r
+            }\r
+        }\r
+\r
+        protected override byte[] HashFinal()\r
+        {\r
+            return _context.DigestFinal();\r
+        }\r
+\r
+        protected override void Dispose(bool disposing)\r
+        {\r
+            base.Dispose(disposing);\r
+            if (disposing)\r
+            {\r
+                if (_context != null)\r
+                    _context.Dispose();\r
+                _context = null;\r
+            }\r
+        }\r
+    }\r
 }\r