public void GetNetworkAddress_HostAddress() { // arrange var inputAdr = new Ipv4Address(192, 168, 0, 1); ushort networkPortion = 26; var expected = new Ipv4Address(192, 168, 0, 0); // act var result = _manager.GetNetworkAddress(inputAdr, networkPortion); // assert Assert.NotNull(result); Assert.True(expected.ToDecimal().SequenceEqual(result.ToDecimal())); }
public string GetFullNetworkData() { var(address, networkPortion) = GetAddressAndValidNetworkPortion(); var isValid = Utils.IsLegalHostAddress(address, networkPortion); var addressType = Utils.GetAddressRange(address); var networkAddress = NetworkManager.GetNetworkAddress(address, networkPortion); var broadcastAddress = NetworkManager.GetBroadcastAddress(address, networkPortion); var firstHost = NetworkManager.GetFirstHostAddress(address, networkPortion); var lastHost = NetworkManager.GetLastHostAddress(address, networkPortion); var classOfAddress = ClassManager.GetClass(address); var possibleSubnets = ClassManager.PossibleSubnetCount(classOfAddress, networkPortion); var possibleHosts = ClassManager.PossibleHostsCount(classOfAddress, networkPortion); var sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine("=================== Full Network Data ==================="); sb.AppendLine(isValid ? "Host Address," : "Special Address,"); sb.AppendLine("Address type: " + addressType.Type()); sb.AppendLine("Network address: " + networkAddress.ToDecimalString()); sb.AppendLine("Broadcast address: " + broadcastAddress.ToDecimalString()); sb.AppendLine("First Host address: " + firstHost.ToDecimalString()); sb.AppendLine("Last Host address: " + lastHost.ToDecimalString()); sb.AppendLine("Possible Subnets Count: " + possibleSubnets); sb.AppendLine("Possible Hosts Count: " + possibleHosts); sb.AppendLine("========================================================="); return(sb.ToString()); }
public bool IsLegalHostAddress(Ipv4Address address, ushort networkPortion) { var broadcast = NetworkManager.GetBroadcastAddress(address, networkPortion); var networkAddress = NetworkManager.GetNetworkAddress(address, networkPortion); if (address == broadcast || networkAddress == address) { return(false); } var networkClass = ClassManager.GetClass(address); var networkClassName = networkClass.GetName(); if (networkClassName == "A" || networkClassName == "B" || networkClassName == "C") { return(ClassManager.IsNetworkPortionValid(networkClass, networkPortion)); } return(false); }