How to use ipBlockFromCIDR method of types Package

Best K6 code snippet using types.ipBlockFromCIDR


Source:ipblock.go Github


Full Screen

...44 switch {45 case strings.Contains(s, "-"):46 return ipBlockFromRange(s)47 case strings.Contains(s, "/"):48 return ipBlockFromCIDR(s)49 default:50 if net.ParseIP(s) == nil {51 return nil, fmt.Errorf("%s is not a valid IP, IP range or CIDR", s)52 }53 return ipBlockFromRange(s + "-" + s)54 }55}56func ipBlockFromRange(s string) (*ipBlock, error) {57 ss := strings.SplitN(s, "-", 2)58 ip0, ip1 := net.ParseIP(ss[0]), net.ParseIP(ss[1])59 if ip0 == nil || ip1 == nil {60 return nil, errors.New("wrong IP range format: " + s)61 }62 if (ip0.To4() == nil) != (ip1.To4() == nil) { // XOR63 return nil, errors.New("mixed IP range format: " + s)64 }65 block := ipBlockFromTwoIPs(ip0, ip1)66 if block.count.Sign() <= 0 {67 return nil, errors.New("negative IP range: " + s)68 }69 return block, nil70}71func ipBlockFromTwoIPs(ip0, ip1 net.IP) *ipBlock {72 // This code doesn't do any checks on the validity of the arguments, that should be73 // done before and/or after it is called74 var block ipBlock75 block.firstIP = new(big.Int)76 block.count = new(big.Int)77 block.ipv6 = ip0.To4() == nil78 if block.ipv6 {79 block.firstIP.SetBytes(ip0.To16())80 block.count.SetBytes(ip1.To16())81 } else {82 block.firstIP.SetBytes(ip0.To4())83 block.count.SetBytes(ip1.To4())84 }85 block.count.Sub(block.count, block.firstIP)86 block.count.Add(block.count, big.NewInt(1))87 return &block88}89func ipBlockFromCIDR(s string) (*ipBlock, error) {90 _, pnet, err := net.ParseCIDR(s)91 if err != nil {92 return nil, fmt.Errorf("parseCIDR() failed parsing %s: %w", s, err)93 }94 ip0 := pnet.IP95 // TODO: this is just to copy it, it will probably be better to copy the bytes ...96 ip1 := net.ParseIP(ip0.String())97 if ip1.To4() == nil {98 ip1 = ip1.To16()99 } else {100 ip1 = ip1.To4()101 }102 for i := range ip1 {103 ip1[i] |= (255 ^ pnet.Mask[i])...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?