Ver código fonte

Improved SID (search handle) allocation mechanism

Tal Aloni 8 anos atrás
pai
commit
b12a340d70

+ 13 - 6
SMBLibrary/Server/ConnectionState/SMB1ConnectionState.cs

@@ -270,15 +270,22 @@ namespace SMBLibrary.Server
             }
         }
 
-        public ushort AllocateSearchHandle()
+        public ushort? AllocateSearchHandle()
         {
-            while (OpenSearches.ContainsKey(m_nextSearchHandle) || m_nextSearchHandle == 0 || m_nextSearchHandle == 0xFFFF)
+            for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
             {
-                m_nextSearchHandle++;
+                ushort searchHandle = (ushort)(m_nextSearchHandle + offset);
+                if (searchHandle == 0 || searchHandle == 0xFFFF)
+                {
+                    continue;
+                }
+                if (!OpenSearches.ContainsKey(searchHandle))
+                {
+                    m_nextSearchHandle = (ushort)(searchHandle + 1);
+                    return searchHandle;
+                }
             }
-            ushort searchHandle = m_nextSearchHandle;
-            m_nextSearchHandle++;
-            return searchHandle;
+            return null;
         }
 
         public void ReleaseSearchHandle(ushort searchHandle)

+ 7 - 7
SMBLibrary/Server/SMB1/Transaction2SubcommandHelper.cs

@@ -40,12 +40,6 @@ namespace SMBLibrary.Server.SMB1
             }
             bool exactNameWithoutExtension = searchPattern.Contains("\"");
 
-            if (state.OpenSearches.Count > SMB1ConnectionState.MaxSearches)
-            {
-                header.Status = NTStatus.STATUS_OS2_NO_MORE_SIDS;
-                return null;
-            }
-
             FileSystemEntry entry = fileSystem.GetEntry(path);
             if (entry == null)
             {
@@ -128,7 +122,13 @@ namespace SMBLibrary.Server.SMB1
             }
             else
             {
-                response.SID = state.AllocateSearchHandle();
+                ushort? searchHandle = state.AllocateSearchHandle();
+                if (!searchHandle.HasValue)
+                {
+                    header.Status = NTStatus.STATUS_OS2_NO_MORE_SIDS;
+                    return null;
+                }
+                response.SID = searchHandle.Value;
                 entries.RemoveRange(0, returnCount);
                 state.OpenSearches.Add(response.SID, entries);
             }