Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace PlatformPlatform.SharedKernel.StronglyTypedIds;
Expand Down Expand Up @@ -33,12 +33,28 @@ public static long NewId()
/// </summary>
private static int GetUniqueGeneratorIdFromIpAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
const string noNetworkAdapters = "No network adapters with an IPv4 address in the system. IdGenerator is meant to create unique IDs across multiple machines, and requires an IP address to do so.";
var ipAddress = Array.Find(host.AddressList, ip => ip.AddressFamily == AddressFamily.InterNetwork)
?? throw new InvalidOperationException(noNetworkAdapters);

var lastSegment = ipAddress.ToString().Split('.')[3];
return int.Parse(lastSegment);
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
.Where(ni => ni.OperationalStatus == OperationalStatus.Up &&
ni.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel
)
.ToArray();

foreach (var networkInterface in networkInterfaces)
{
var properties = networkInterface.GetIPProperties();
var ipv4Address = properties.UnicastAddresses
.FirstOrDefault(addr => addr.Address.AddressFamily == AddressFamily.InterNetwork);

if (ipv4Address is not null)
{
var lastSegment = ipv4Address.Address.ToString().Split('.')[3];
return int.Parse(lastSegment);
}
}

throw new InvalidOperationException(noNetworkAdapters);
}
}