Use the Go language to calculate CIDR for network IP addresses

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Using many languages such as Java, C #, and so on for many years, has not been how to use the displacement operators in these languages, recently in order to achieve a small function, only to realize the purpose of the several operators. Before describing how to calculate CIDR address segments in the Go language, first describe what is the CIDR of the network address.

CIDR full name Classless Inter-Domain Routing (Non-class inter-domain routing), compared to CIDR, the global IPV4 address (represented by 32 bit bits) was divided into a, B, C, D, E Five-class address as early as the 80 's. Where a Class A address is preceded by 0, class B addresses begin with 10, Class C addresses begin with 110, Class D addresses begin with 1110, and class E addresses begin with 1111. Where the class D address is mainly used for routing multicast, E-class address is temporarily reserved. The remaining bits of each type of address are divided into two parts: Network and host.

Each class of IP address specific range as shown, where a, B, c three address each reserved an intranet reserved address, these pieces of reserved address is not look familiar:


Although there are five types of addresses (actually available only three classes) division, but the allocation of IPV4 address resources is much smaller than this, after all, the world so many organizations and enterprises in the hand, in order to subdivide these large IP addresses, the relevant organizations introduced through the mask to refine the IP segment, so that more granular distribution. The IPV4 mask is represented by an IP address as well as a 32 bit bit, and we can add a "/xx" to the IP address to indicate the mask for CIDR, where XX is a number not greater than 32, for example, if you specify Xx=22, it indicates that you want to use the 11111111.11111111.11111100.00000000 "(22 bits 1+10 bits 0) as the mask of IP subdivision, this practice is equivalent to no longer only use the original ABCDE five types of address to divide the IP address, So the word classless is used in the name. There's a ipcalc under Linux. Third-party commands can be used to calculate CIDR, and you can install it yourself.


Although there are similar ipcalc tools available, but the development of network programs, still have to have a direct call to the LIB, in go search under the circumstances, I decided to implement, the code idea is very simple, the main is to calculate the number of bits of the mask, the following code for the students to reference (in order to demonstrate , only the low 16-bit IP is calculated):

Package Mainimport ("FMT" "Strings" "StrConv") func main () {minip, Maxip: = Getcidriprange ("100.111.111.111/22") fmt. Println ("CIDR min IP:", Minip, "CIDR Maximum IP:", MAXIP) fmt. Println ("Mask:", Getcidripmask) fmt. PRINTLN ("host number", Getcidrhostnum)}func getcidriprange (CIDR string) (string, string) {IP: = strings. Split (CIDR, "/") [0]ipsegs: = Strings. Split (IP, ".") Masklen, _: = StrConv. Atoi (Strings. Split (CIDR, "/") [1]) Seg3minip, Seg3maxip: = Getipseg3range (Ipsegs, Masklen) Seg4minip, Seg4maxip: = Getipseg4range ( Ipsegs, Masklen) Ipprefix: = Ipsegs[0] + "." + ipsegs[1] + "." return Ipprefix + StrConv. Itoa (Seg3minip) + "." + StrConv. Itoa (Seg4minip), Ipprefix + StrConv. Itoa (SEG3MAXIP) + "." + StrConv. Itoa (SEG4MAXIP)}//calculates the number of hosts that can be owned in a CIDR address range func getcidrhostnum (masklen int) UINT {cidripnum: = UINT (0) var i uint = UINT (32-m askLen-1) for; I >= 1; i--{cidripnum = 1 << i}return cidripnum}//get CIDR mask func getcidripmask (masklen int) string {//^uint32 (0) binary is 32 bits 1, the binary cidrmask of the CIDR mask is obtained by shifting to the Left: = ^uint32(0) << uint (32-masklen) fmt. Println (FMT. Sprintf ("%b \ n", cidrmask))//calculates the four fragments of the CIDR mask, moves the desired fragment to the lowest 8 bits of memory, and turns it into a 8-bit integer, resulting in CIDRMASKSEG1: = Uint8 (Cidrmask >> CIDRMASKSEG2: = uint8 (Cidrmask >>) CidrMaskSeg3: = Uint8 (cidrmask >> 8) CidrMaskSeg4: = Uint8 (Cidrmask &A mp UInt32 (255)) return to FMT. Sprint (CIDRMASKSEG1) + "." + FMT. Sprint (CIDRMASKSEG2) + "." + FMT. Sprint (CIDRMASKSEG3) + "." + FMT. Sprint (CIDRMASKSEG4)}//Gets the interval of the third IP (the first fragment. The second fragment. The third fragment. Fragment IV) Func Getipseg3range (Ipsegs []string, Masklen int) (int, int) { If Masklen > {SEGIP, _: = StrConv. Atoi (ipsegs[2]) return SEGIP, segip}ipseg, _: = StrConv. Atoi (ipsegs[2]) return Getipsegrange (Uint8 (ipseg), Uint8 (24-masklen))}//get the fourth segment of the IP range (the first fragment. The second fragment. The third fragment. Fragment IV) Func Getipseg4range (Ipsegs []string, Masklen int) (int, int) {ipseg, _: = StrConv. Atoi (Ipsegs[3]) Segminip, Segmaxip: = Getipsegrange (Uint8 (ipseg), Uint8 (32-masklen)) return Segminip + 1, segmaxip}// Calculates the interval func getipsegrange (USERSEGIP, offset uint8) of an IP fragment based on the user-entered base IP address and CIDR mask (int, int) {var Ipsegmax uint8 = 255netSegIp: = Ipsegmax << Offsetsegminip: = NETSEGIP & USERSEGIPSEGMAXIP: = Use RSEGIP & (255 << offset) | ^ (255 << offset) return int (SEGMINIP), int (SEGMAXIP)}




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.