From d4870ba73fc790601359c4ac2ed00024c54d152e Mon Sep 17 00:00:00 2001 From: Zafer Balkan Date: Thu, 29 Jan 2026 19:49:07 +0200 Subject: [PATCH 1/7] Added guard clause for NetworkMap.TryGetValue and others for graceful error handling --- TechnitiumLibrary.Net/NetworkMap.cs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/TechnitiumLibrary.Net/NetworkMap.cs b/TechnitiumLibrary.Net/NetworkMap.cs index 8c12473d..a5f46cae 100644 --- a/TechnitiumLibrary.Net/NetworkMap.cs +++ b/TechnitiumLibrary.Net/NetworkMap.cs @@ -20,6 +20,7 @@ You should have received a copy of the GNU General Public License using System; using System.Collections.Generic; using System.Net; +using System.Net.Sockets; namespace TechnitiumLibrary.Net { @@ -28,6 +29,7 @@ public class NetworkMap #region variables readonly List _ipLookupList; + AddressFamily _networkFamily = AddressFamily.Unspecified; bool _sorted; #endregion @@ -105,6 +107,16 @@ public void Add(NetworkAddress networkAddress, T value) { lock (_ipLookupList) { + // The first entry decides what kind of network map + // we are creating + if (_networkFamily == AddressFamily.Unspecified) + { + _networkFamily = networkAddress.AddressFamily; + } + else if (_networkFamily != networkAddress.AddressFamily) + { + throw new InvalidOperationException("Cannot add network address of different address family to the map."); + } _ipLookupList.Add(new IpEntry(networkAddress.Address, value)); _ipLookupList.Add(new IpEntry(networkAddress.GetLastAddress(), value)); @@ -119,12 +131,19 @@ public bool Remove(string networkAddress) public bool Remove(NetworkAddress networkAddress) { + if (networkAddress.AddressFamily != _networkFamily) + { + return false; + } + lock (_ipLookupList) { bool v1 = _ipLookupList.Remove(new IpEntry(networkAddress.Address)); bool v2 = _ipLookupList.Remove(new IpEntry(networkAddress.GetLastAddress())); _sorted = false; + + if (_ipLookupList.Count == 0) _networkFamily = AddressFamily.Unspecified; // reset internal address family, class may be reused. return v1 & v2; } } @@ -136,6 +155,12 @@ public bool TryGetValue(string address, out T value) public bool TryGetValue(IPAddress address, out T value) { + if (address.AddressFamily != _networkFamily) + { + value = default; + return false; + } + if (!_sorted) { lock (_ipLookupList) @@ -149,7 +174,6 @@ public bool TryGetValue(IPAddress address, out T value) } IpEntry findEntry = new IpEntry(address); - IpEntry floorEntry = GetFloorEntry(findEntry); IpEntry ceilingEntry = GetCeilingEntry(findEntry); @@ -244,4 +268,4 @@ public T Value #endregion } } -} +} \ No newline at end of file From 3a3abf51731f81b8565a223d643997caa479f683 Mon Sep 17 00:00:00 2001 From: Zafer Balkan Date: Mon, 2 Feb 2026 14:07:02 +0200 Subject: [PATCH 2/7] Moved guard clause in the lock block --- TechnitiumLibrary.Net/NetworkMap.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/TechnitiumLibrary.Net/NetworkMap.cs b/TechnitiumLibrary.Net/NetworkMap.cs index a5f46cae..48069ae3 100644 --- a/TechnitiumLibrary.Net/NetworkMap.cs +++ b/TechnitiumLibrary.Net/NetworkMap.cs @@ -131,13 +131,13 @@ public bool Remove(string networkAddress) public bool Remove(NetworkAddress networkAddress) { - if (networkAddress.AddressFamily != _networkFamily) - { - return false; - } - lock (_ipLookupList) { + if (networkAddress.AddressFamily != _networkFamily) + { + return false; + } + bool v1 = _ipLookupList.Remove(new IpEntry(networkAddress.Address)); bool v2 = _ipLookupList.Remove(new IpEntry(networkAddress.GetLastAddress())); From 0489632d0ddb08c755b8d0c7209c5ae149c1d76b Mon Sep 17 00:00:00 2001 From: Zafer Balkan Date: Mon, 2 Feb 2026 14:45:17 +0200 Subject: [PATCH 3/7] Newline --- TechnitiumLibrary.Net/NetworkMap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TechnitiumLibrary.Net/NetworkMap.cs b/TechnitiumLibrary.Net/NetworkMap.cs index 48069ae3..da6da125 100644 --- a/TechnitiumLibrary.Net/NetworkMap.cs +++ b/TechnitiumLibrary.Net/NetworkMap.cs @@ -268,4 +268,4 @@ public T Value #endregion } } -} \ No newline at end of file +} From 698b3e671cf13ce58db6e43b44cd816b47e5f3a1 Mon Sep 17 00:00:00 2001 From: Zafer Balkan Date: Mon, 2 Feb 2026 14:46:41 +0200 Subject: [PATCH 4/7] Newline --- TechnitiumLibrary.Net/NetworkMap.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TechnitiumLibrary.Net/NetworkMap.cs b/TechnitiumLibrary.Net/NetworkMap.cs index da6da125..791d8c81 100644 --- a/TechnitiumLibrary.Net/NetworkMap.cs +++ b/TechnitiumLibrary.Net/NetworkMap.cs @@ -174,6 +174,7 @@ public bool TryGetValue(IPAddress address, out T value) } IpEntry findEntry = new IpEntry(address); + IpEntry floorEntry = GetFloorEntry(findEntry); IpEntry ceilingEntry = GetCeilingEntry(findEntry); From 76644b5c8fa6db38e94a3ea81c1daa3ef94c82fc Mon Sep 17 00:00:00 2001 From: Zafer Balkan Date: Mon, 2 Feb 2026 15:28:32 +0200 Subject: [PATCH 5/7] Marked _networkFamily as volatile as multiple threads can access --- TechnitiumLibrary.Net/NetworkMap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TechnitiumLibrary.Net/NetworkMap.cs b/TechnitiumLibrary.Net/NetworkMap.cs index 791d8c81..32569e77 100644 --- a/TechnitiumLibrary.Net/NetworkMap.cs +++ b/TechnitiumLibrary.Net/NetworkMap.cs @@ -29,7 +29,7 @@ public class NetworkMap #region variables readonly List _ipLookupList; - AddressFamily _networkFamily = AddressFamily.Unspecified; + volatile AddressFamily _networkFamily = AddressFamily.Unspecified; bool _sorted; #endregion From 42a12d97f23765ce5cd5e08ab49c024c7bffd7db Mon Sep 17 00:00:00 2001 From: Zafer Balkan Date: Mon, 2 Feb 2026 17:16:20 +0200 Subject: [PATCH 6/7] Removed AddressFamily reset on clear --- TechnitiumLibrary.Net/NetworkMap.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/TechnitiumLibrary.Net/NetworkMap.cs b/TechnitiumLibrary.Net/NetworkMap.cs index 32569e77..5ebfe7ff 100644 --- a/TechnitiumLibrary.Net/NetworkMap.cs +++ b/TechnitiumLibrary.Net/NetworkMap.cs @@ -143,7 +143,6 @@ public bool Remove(NetworkAddress networkAddress) _sorted = false; - if (_ipLookupList.Count == 0) _networkFamily = AddressFamily.Unspecified; // reset internal address family, class may be reused. return v1 & v2; } } From 2d9748db746403a7d3a120c9f787c8340fbb41d9 Mon Sep 17 00:00:00 2001 From: Zafer Balkan Date: Tue, 3 Feb 2026 17:28:00 +0200 Subject: [PATCH 7/7] Added copyright --- TechnitiumLibrary.Net/NetworkMap.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TechnitiumLibrary.Net/NetworkMap.cs b/TechnitiumLibrary.Net/NetworkMap.cs index 5ebfe7ff..26f50f34 100644 --- a/TechnitiumLibrary.Net/NetworkMap.cs +++ b/TechnitiumLibrary.Net/NetworkMap.cs @@ -1,6 +1,7 @@ /* Technitium Library Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com) +Copyright (C) 2026 Zafer Balkan (zafer@zaferbalkan.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by