Ejemplo n.º 1
0
Archivo: daemon.go Proyecto: tdr130/oz
// Go through all possible ips between min and max
// and arping each one until a free one is found
func getScanIP(min, max uint64, reservedBytes []uint) string {
	if min > max {
		return ""
	}

	for i := min; i < max; i++ {
		reserved := false
		for _, ipbyte := range reservedBytes {
			if ipbyte == uint(i&0xFF) {
				reserved = true
				break
			}
		}
		if reserved {
			continue
		}
		dstIP := inet_ntoa(i)

		arping.SetTimeout(time.Millisecond * 150)

		_, _, err := arping.PingOverIfaceByName(dstIP, ozDefaultInterfaceBridge)

		if err == arping.ErrTimeout {
			return dstIP.String()
		} else if err != nil {
			return dstIP.String()
		}
	}

	return ""

}
Ejemplo n.º 2
0
Archivo: daemon.go Proyecto: tdr130/oz
// Generate a random ip and arping it to see if it is available
// Returns the ip on success or an ip string is the ip is already taken
func getRandIP(min, max uint64, reservedBytes []uint) string {
	if min > max {
		return ""
	}

	randVal := uint64(0)
	for {
		randVal = uint64(rand.Int63n(int64(max - min)))
		reserved := false
		for _, ipbyte := range reservedBytes {
			if ipbyte == uint(byte(randVal)&0xFF) {
				reserved = true
				break
			}
		}
		if !reserved {
			break
		}
	}

	dstIP := inet_ntoa(randVal + min)

	arping.SetTimeout(time.Millisecond * 150)

	_, _, err := arping.PingOverIfaceByName(dstIP, ozDefaultInterfaceBridge)

	if err == arping.ErrTimeout {
		return dstIP.String()
	} else if err != nil {
		return dstIP.String()
	}

	return ""

}
Ejemplo n.º 3
0
func main() {
	flag.Parse()

	if *helpFlag {
		printHelpAndExit()
	}
	if *verboseFlag {
		arping.EnableVerboseLog()
	}
	arping.SetTimeout(*timeoutFlag)

	if len(flag.Args()) != 1 {
		fmt.Println("Parameter <IP> missing!")
		printHelpAndExit()
	}
	dstIP := net.ParseIP(flag.Arg(0))

	var hwAddr net.HardwareAddr
	var durationNanos time.Duration
	var err error
	if len(*ifaceNameFlag) > 0 {
		hwAddr, durationNanos, err = arping.PingOverIfaceByName(dstIP, *ifaceNameFlag)
	} else {
		hwAddr, durationNanos, err = arping.Ping(dstIP)
	}

	// ping timeout
	if err == arping.ErrTimeout {
		fmt.Println(err)
		os.Exit(1)
	}

	// ping failed
	if err != nil {
		fmt.Println(err)
		os.Exit(2)
	}

	// ping success
	durationMicros := durationNanos / 1000

	var durationString string
	if durationMicros > 1000 {
		durationString = fmt.Sprintf("%d,%03d", durationMicros/1000, durationMicros%1000)
	} else {
		durationString = fmt.Sprintf("%d", durationMicros)
	}

	fmt.Printf("%s (%s) %s usec\n", dstIP, hwAddr, durationString)
	os.Exit(0)
}
Ejemplo n.º 4
0
func AssignLinkLocalIP(link netlink.Link) error {
	ifaceName := link.Attrs().Name
	iface, err := net.InterfaceByName(ifaceName)
	if err != nil {
		log.Error("could not get information about interface")
		return err
	}
	addrs, err := iface.Addrs()
	if err != nil {
		log.Error("Error fetching existing ip on interface")
	}
	for _, addr := range addrs {
		if addr.String()[:7] == "169.254" {
			log.Info("Link Local IP already set on interface")
			return nil
		}
	}
	randSource, err := getPseudoRandomGenerator(link.Attrs().HardwareAddr)
	if err != nil {
		return err
	}
	// try a random address upto 10 times
	for i := 0; i < 10; i++ {
		randGenerator := rand.New(*randSource)
		randomNum := randGenerator.Uint32()
		dstIP := getNewIPV4LLAddr(randomNum)
		if dstIP[2] == 0 || dstIP[2] == 255 {
			i--
			continue
		}
		_, _, err := arping.PingOverIfaceByName(dstIP, ifaceName)
		if err != nil {
			// this ip is not being used
			addr, err := netlink.ParseAddr(dstIP.String() + "/16")
			if err != nil {
				log.Errorf("error while parsing ipv4ll addr, err = %v", err)
				return err
			}
			if err := netlink.AddrAdd(link, addr); err != nil {
				log.Error("ipv4ll addr add failed")
				return err
			}
			log.Infof("Set %s on %s", dstIP.String(), link.Attrs().Name)
			return nil
		}
	}
	log.Error("Could not find a suitable ipv4ll")
	return fmt.Errorf("Could not find a suitable ipv4ll")
}
Ejemplo n.º 5
0
// Generate a random ip and arping it to see if it is available
// Returns the ip on success or an ip string is the ip is already taken
func getRandIP(min, max uint64) string {
	if min > max {
		return ""
	}

	dstIP := inet_ntoa(uint64(rand.Int63n(int64(max-min))) + min)

	arping.SetTimeout(time.Millisecond * 150)

	_, _, err := arping.PingOverIfaceByName(dstIP, ozDefaultInterfaceBridge)

	if err == arping.ErrTimeout {
		return dstIP.String()
	} else if err != nil {
		return dstIP.String()
	}

	return ""

}
Ejemplo n.º 6
0
// Go through all possible ips between min and max
// and arping each one until a free one is found
func getScanIP(min, max uint64) string {
	if min > max {
		return ""
	}

	for i := min; i < max; i++ {
		dstIP := inet_ntoa(i)

		arping.SetTimeout(time.Millisecond * 150)

		_, _, err := arping.PingOverIfaceByName(dstIP, ozDefaultInterfaceBridge)

		if err == arping.ErrTimeout {
			return dstIP.String()
		} else if err != nil {
			return dstIP.String()
		}
	}

	return ""

}