) Determine whether an IP address is an intranet IP address (Memo)

Source: Internet
Author: User
Tags get ip ip number

Java code/* determine whether it is an intranet IP */public static Boolean isinnerip (string IPaddress ){
Boolean isinnerip = false;
Long ipnum = getipnum (IPaddress );
/**
Private IP: Class A 10.0.0.0-10.255.255.255
Class B 172.16.0.0-172.31.255.255
Class C 192.168.0.0-192.168.255.255
Of course, the 127 CIDR block is the loopback address.
**/
Long abegin = getipnum ("10.0.0.0 ");
Long aend = getipnum ("10.20.255 ");
Long bbegin = getipnum ("172.16.0.0 ");
Long bend = getipnum ("172.31.20.255 ");
Long cbegin = getipnum ("192.168.0.0 ");
Long Cend = getipnum ("192.168.255.255 ");
Isinnerip = isinner (ipnum, abegin, aend) | isinner (ipnum, bbegin, bend) | isinner (ipnum, cbegin, Cend) | IPaddress. equals ("127.0.0.1 ");
Return isinnerip;
}

Java code/* Get IP number */
Private Static long getipnum (string IPaddress ){
String [] IP = IPaddress. Split ("//.");
Long A = integer. parseint (IP [0]);
Long B = integer. parseint (IP [1]);
Long c = integer. parseint (IP [2]);
Long d = integer. parseint (IP [3]);

Long ipnum = A * 256*256*256 + B * 256*256 + C * 256 + D;
Return ipnum;
}

Java code
Private Static Boolean isinner (long userip, long begin, long end ){
Return (userip> = begin) & (userip <= END );
}

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.