Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Last active December 19, 2015 11:59
Show Gist options
  • Select an option

  • Save TechplexEngineer/5951594 to your computer and use it in GitHub Desktop.

Select an option

Save TechplexEngineer/5951594 to your computer and use it in GitHub Desktop.

IP Addresses are displayed in a human readable format known as Dotted Decimal notation with four octets separated by a dot ie. 192.168.1.1

In the process of trying to have a nice display and one that is sortable in asp.net I devised this solution.

Synposis: The model stores the octets in the database for easy querying form other interfaces. The public accessors can return a stringified form or the IP's Decimal representation.

Using the decimal representation allows for sorting but not noce display. So a bit of work needs to be done in the view to form the IP nicely.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace PcnWeb.Models
{
public class IPAddress
{
public IPAddress()
{
this.UPSs = new List<UPS>();
}
public virtual ICollection<UPS> UPSs { get; set; }
[Key]
public int ipAddressRecId { get; set; }
[Required]
[NotMapped]
[RegularExpression(@"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|DHCP", ErrorMessage = "IP Address must be in the form of four octets 0-255: 255.255.255.255 or DHCP")]
public string IP_Address
{
get
{
if (ipOctet1 == null | ipOctet2 == null | ipOctet3 == null | ipOctet4 == null)
return "DHCP";
return ipOctet1 + "." + ipOctet2 + "." + ipOctet3 + "." + ipOctet4;
}
set
{
if (value == "DHCP")
{
ipOctet1 = null;
ipOctet2 = null;
ipOctet3 = null;
ipOctet4 = null;
return;
}
//@todo Parseing could throw a number of errors: ArgumentNullException, FormatException, Exception
byte[] temp = System.Net.IPAddress.Parse(value).GetAddressBytes();
ipOctet1 = temp[0];
ipOctet2 = temp[1];
ipOctet3 = temp[2];
ipOctet4 = temp[3];
}
}
[NotMapped] //@todo check if this is needed. Don't want an extra database entry.
public long ipTotal
{
get
{
if (ipOctet1 == null || ipOctet2 == null || ipOctet3 == null || ipOctet4 == null)
return 0;
//(first octet * 2^24) + (second octet * 2^16) + (third octet * 2^8) + (fourth octet)
return ((long)ipOctet1 * 16777216) + ((long)ipOctet2 * 65536) + ((long)ipOctet3 * 256) + (long)ipOctet4;
}
}
public Nullable<int> ipOctet1 { get; set; }
public Nullable<int> ipOctet2 { get; set; }
public Nullable<int> ipOctet3 { get; set; }
public Nullable<int> ipOctet4 { get; set; }
[Required]
[NotMapped]
[RegularExpression(@"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|DHCP", ErrorMessage = "Subnet Mask must be in the form of four octets 0-255: 255.255.255.255 or DHCP")]
public string Subnet_Mask
{
get
{
if (smOctet1 == null || smOctet2 == null || smOctet3 == null || smOctet4 == null)
return "DHCP";
return smOctet1 + "." + smOctet2 + "." + smOctet3 + "." + smOctet4;
}
set
{
if (value == "DHCP")
{
smOctet1 = null;
smOctet2 = null;
smOctet3 = null;
smOctet4 = null;
return;
}
//@todo Parseing could throw a number of errors: ArgumentNullException, FormatException, Exception
byte[] temp = System.Net.IPAddress.Parse(value).GetAddressBytes();
smOctet1 = temp[0];
smOctet2 = temp[1];
smOctet3 = temp[2];
smOctet4 = temp[3];
}
}
[NotMapped] //@todo check if this is needed. Don't want an extra database entry.
public long smTotal
{
get
{
if (smOctet1 == null || smOctet2 == null || smOctet3 == null || smOctet4 == null)
return 0;
//(first octet * 2^24) + (second octet * 2^16) + (third octet * 2^8) + (fourth octet)
return ((long)smOctet1 * 16777216) + ((long)smOctet2 * 65536) + ((long)smOctet3 * 256) + (long)smOctet4;
}
}
public Nullable<int> smOctet1 { get; set; }
public Nullable<int> smOctet2 { get; set; }
public Nullable<int> smOctet3 { get; set; }
public Nullable<int> smOctet4 { get; set; }
}
}
@model IEnumerable<PcnWeb.Models.IPAddress>
@using PcnWeb.Helpers
@{
ViewBag.Title = "Index of IP Address".pluralize();
}
<h2>@ViewBag.Title</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@{
var grid = new WebGrid(Model, defaultSort: "ipAddressRecId", canPage: true, rowsPerPage:25);
}
@grid.GetHtml(
columns: grid.Columns(
grid.Column(columnName: "ipTotal", header: "IP Address", format: (item) => {
long ip = item.ipTotal;
if (ip == 0)
return "DHCP";
long ipOct1 = ip / 16777216;
ip = ip % 16777216;
long ipOct2 = ip / 65536;
ip = ip % 65536;
long ipOct3 = ip / 256;
ip = ip % 256;
long ipOct4 = ip;
return ipOct1 + "." + ipOct2 + "." + ipOct3 + "." + ipOct4;
}),
grid.Column(columnName: "smTotal", header: "Subnet Mask", format: (item) => {
long sm = item.smTotal;
if (sm == 0)
return "DHCP";
long smOct1 = sm / 16777216;
sm = sm % 16777216;
long smOct2 = sm / 65536;
sm = sm % 65536;
long smOct3 = sm / 256;
sm = sm % 256;
long smOct4 = sm;
return smOct1 + "." + smOct2 + "." + smOct3 + "." + smOct4;
}),
grid.Column(
format: (item) =>
{
var links = Html.ActionLink("Edit", "Edit", new { id=item.ipAddressRecId }) + " | " +
Html.ActionLink("Details", "Details", new { id=item.ipAddressRecId }) + " | " +
Html.ActionLink("Delete", "Delete", new { id=item.ipAddressRecId });
return Html.Raw(links);
})
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment