Determines whether an IP range (or IP address) is included by another IP address range.

Source: Internet
Author: User
Tags ranges

The following method is used to determine whether an IP address is included in an IP Range:

Some static methods may reference custom classes with the same name space,

For merging two adjacent IP segments, you can add the maximum or minimum IP addresses to the CIDR Block

Using system;
Using system. Text. regularexpressions;

Namespace hkh. Common
{
/// <Summary>
/// Summary of clsmacandip.
/// </Summary>
Public class clsipaddress
{
Private clsipaddress ()
{
}

# Region determine whether an IP address is valid

/// <Summary>
/// Determine whether the IP address is valid
/// </Summary>
/// <Param name = "Strip"> </param>
/// <Returns> </returns>
Public static bool validateipaddress (string strip)
{
If (null = strip | "" = strip. Trim () | convert. isdbnull (strip ))
Return false;

Return RegEx. ismatch (strip, @ "^ (/d {1, 2} | 1/D/d | 2 [0-4]/d | 25 [0-5])/.) {3} (/d {1, 2} | 1/D/d | 2 [0-4]/d | 25 [0-5]) $ ");
}

# Endregion

# Region converted to a value

/// <Summary>
/// Convert to a value
/// </Summary>
/// <Param name = "ipaddr"> </param>
/// <Returns> </returns>
Public static uint transnum (string ipaddr)
{
If (! Validateipaddress (ipaddr ))
Throw new exception ("ip address isn't well format! ");

String [] ipstrarray = new string [4];
Ipstrarray = ipaddr. Split ('.');
Return makelong (makeword (byte. parse (ipstrarray [3]), byte. parse (ipstrarray [2]), makeword (byte. parse (ipstrarray [1]), byte. parse (ipstrarray [0]);
}

# Endregion

# Region formatting

/// <Summary>
/// Format
/// </Summary>
/// <Param name = "ipaddr"> </param>
/// <Returns> </returns>
Public static string format (string ipaddr)
{
If (ipaddr [0] = '.')
Ipaddr = "0" + ipaddr;

If (ipaddr [ipaddr. Length-1] = '.')
Ipaddr + = "0.0.0 .";
Else
Ipaddr + = ". 0.0.0 .";

Ipaddr = ipaddr. substring (0, clsstring. indexof (ipaddr, ".", 4 ));

/// Remove useless 0
// Ipaddr = RegEx. Replace (ipaddr, "^ 0 {1, 2 }","");
// Ipaddr = RegEx. Replace (ipaddr, @ "/. + 0 {1, 2 }",".");

If (validateipaddress (ipaddr ))
Return ipaddr;

Return "0.0.0.0 ";
}

# Endregion

# Region compare the size of two IP addresses

/// <Summary>
/// Compare the two IP addresses
/// </Summary>
/// <Param name = "IP1"> </param>
/// <Param name = "ip2"> </param>
/// <Returns> </returns>
Public static int compare (string IP1, string ip2)
{
If (! (Validateipaddress (IP1) & validateipaddress (ip2 )))
Throw new exception ("ip address isn't well format! ");

Uint ipnum1 = transnum (IP1 );
Uint ipnum2 = transnum (ip2 );

If (ipnum1 = ipnum2)
Return 0;

Return ipnum1> ipnum2? 1:-1;
}

# Endregion

# Region determine whether an IP address is included in the specified IP address range

/// <Summary>
/// Determine whether an IP address is included in the specified IP address range
/// </Summary>
/// <Param name = "ipregion"> </param>
/// <Param name = "ip"> </param>
/// <Returns> </returns>
Public static bool isinclude (string [] ipregion, string IP)
{
// Verify
If (null = ipregion | null = IP | 0 = ipregion. length)
Return false;

If (! Validateipaddress (IP ))
Return false;

If (1 = ipregion. length)
{
If (! Validateipaddress (ipregion [0])
Return false;

If (0 = compare (ipregion [0], ip ))
Return true;
}

If (! (Validateipaddress (ipregion [0]) & validateipaddress (ipregion [1])
Return false;

Uint ipnum = transnum (IP );
Uint ipnum1 = transnum (ipregion [0]);
Uint ipnum2 = transnum (ipregion [1]);

// Compare
If (math. Min (ipnum1, ipnum2) <= ipnum & math. Max (ipnum1, ipnum2)> = ipnum)
Return true;

Return false;
}

# Endregion

# Region checks whether two IP ranges overlap. If the two IP ranges overlap, the merge interval is returned. Otherwise, null is returned.

/// <Summary>
/// Determine whether two IP ranges overlap. If the two IP ranges overlap, return the merge range; otherwise, return null.
/// </Summary>
/// <Param name = "ipregion1"> </param>
/// <Param name = "ipregion2"> </param>
/// <Returns> </returns>
Public static string [] checkoverlapandmerge (string [] ipregion1, string [] ipregion2)
{
// Verify
If (null = ipregion1 | null = ipregion2 | 0 = ipregion1.length | 0 = ipregion2.length)
Return NULL;

// Process the length of the score group = 1 and> 1
If (1 = ipregion1.length)
{
If (! Validateipaddress (ipregion1 [0])
Throw new exception ("ip address isn't well format! ");

If (1 = ipregion2.length)
{
If (! Validateipaddress (ipregion2 [0])
Throw new exception ("ip address isn't well format! ");

// Equal
If (equals (ipregion1 [0], ipregion2 [0])
Return ipregion1;
}
//> 1
Else
{
If (! (Validateipaddress (ipregion2 [0]) & validateipaddress (ipregion2 [1])
Throw new exception ("ip address isn't well format! ");

If (isinclude (ipregion2, ipregion1 [0])
Return ipregion2;
}
}
//> 1
Else
{
If (! (Validateipaddress (ipregion1 [0]) & validateipaddress (ipregion1 [1])
Throw new exception ("ip address isn't well format! ");

If (1 = ipregion2.length)
{
If (! Validateipaddress (ipregion2 [0])
Throw new exception ("ip address isn't well format! ");

If (isinclude (ipregion1, ipregion2 [0])
Return ipregion1;
}
//> 1
Else
{
If (! (Validateipaddress (ipregion2 [0]) & validateipaddress (ipregion2 [1])
Throw new exception ("ip address isn't well format! ");

Uint ipnum1_1 = transnum (ipregion1 [0]);
Uint ipnum1_2 = transnum (ipregion1 [1]);
Uint ipnum2_1 = transnum (ipregion2 [0]);
Uint ipnum2_2 = transnum (ipregion2 [1]);

# IP address in region size format [0] <= IP Address [1]

If (ipnum1_1> ipnum1_2)
{
String stemp = ipregion1 [0];
Ipregion1 [0] = ipregion1 [1];
Ipregion1 [1] = stemp;

Uint utemp = ipnum1_1;
Ipnum1_1 = ipnum1_2;
Ipnum1_2 = utemp;
}

If (ipnum2_1> ipnum2_2)
{
String stemp = ipregion2 [0];
Ipregion2 [0] = ipregion2 [1];
Ipregion2 [1] = stemp;

Uint utemp = ipnum2_1;
Ipnum2_1 = ipnum2_2;
Ipnum2_2 = utemp;
}

# Endregion

# Region judgment and merging

// Mutually exclusive
If (ipnum1_1> ipnum2_2 | ipnum1_2 <ipnum2_1)
Return NULL;

// Merge
String [] result = new string [2];

Result [0] = ipnum1_1 <ipnum2_1? Ipregion1 [0]: ipregion2 [0];
Result [1] = ipnum1_2> ipnum2_2? Ipregion1 [1]: ipregion2 [1];

Return result;

# Endregion
}

}

Return NULL;
}

# Endregion

# Region private Method

/// <Summary>
/// Shift toggle _ 8
/// </Summary>
/// <Param name = "blow"> </param>
/// <Param name = "bhigh"> </param>
/// <Returns> </returns>
Private Static ushort makeword (byte blow, byte bhigh)
{
Return (ushort) (byte) (blow) | (ushort) (byte) (bhigh) <8 ));
}

/// <Summary>
/// Shift toggle _ 16
/// </Summary>
/// <Param name = "blow"> </param>
/// <Param name = "bhigh"> </param>
/// <Returns> </returns>
Private Static uint makelong (ushort blow, ushort bhigh)
{
Return (uint) (ushort) (blow) | (uint) (ushort) (bhigh) <16 ));
}

# Endregion
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.