Example #1
0
func addrip() {
	var err error
	var addr net.IP
	var network *net.IPNet
	if len(arg) == 1 {
		showips()
		return
	}
	cursor++
	whatIWant = "add|del"
	cmd := arg[cursor]

	switch cmd {
	case "add", "del":
		cursor++
		whatIWant = "CIDR format address"
		addr, network, err = net.ParseCIDR(arg[cursor])
		if err != nil {
			usage()
		}
	default:
		usage()
	}
	iface := dev()
	switch cmd {
	case "add":
		if err := netlink.NetworkLinkAddIp(iface, addr, network); err != nil {
			log.Fatalf("Adding %v to %v failed: %v", arg[1], arg[2], err)
		}
	case "del":
		if err := netlink.NetworkLinkDelIp(iface, addr, network); err != nil {
			log.Fatalf("Deleting %v from %v failed: %v", arg[1], arg[2], err)
		}
	default:
		log.Fatalf("devip: arg[0] changed: can't happen")
	}
	return

}
Example #2
0
func c2(re *regexp.Regexp) {
	fails := 0
	r := make(chan *dhcpInfo)
	ifaces, err := net.Interfaces()
	if err != nil {
		log.Printf("client: Can't enumerate interfaces? %v", err)
		return
	}
	for _, v := range ifaces {
		log.Printf("Let's check %v", v)
		if !re.Match([]byte(v.Name)) {
			continue
		}
		log.Printf("Let's USE iface  %v", v)
		go one(v, r)
	}
	for p := range r {
		if p == nil {
			fails++
			if fails > 10 {
				os.Exit(1)
			}
			continue
		}
		//fmt.Printf("GOT ONE! %v\n", len(p))
		if p.OpCode() != dhcp.BootReply {
			fmt.Printf("not a reply?\n")
			continue
		}
		options := p.ParseOptions()
		switch dhcp.MessageType(options[dhcp.OptionDHCPMessageType][0]) {
		case dhcp.Offer:
			fmt.Printf("reply. flags %v HLen %v XId %v CI %v YI %v SI %v GI %v CH %v\n",
				p.Flags(), p.HLen(), p.XId(),
				p.CIAddr(),
				p.YIAddr(),
				p.SIAddr(),
				p.GIAddr(),
				p.CHAddr())
			addr := p.YIAddr()

			netmask := options[dhcp.OptionSubnetMask]
			if netmask != nil {
				fmt.Printf("OptionSubnetMask is %v\n", netmask)
			} else {
				// what do to?
				netmask = addr
			}
			gwData := options[dhcp.OptionRouter]
			fmt.Printf("gwData: %v", gwData)
			dst := &net.IPNet{IP: p.YIAddr(), Mask: netmask}
			// Add the address to the iface.
			if err := netlink.NetworkLinkAddIp(p.i, p.YIAddr(), dst); err != nil {
				fmt.Printf("Add %v to %v: %v", dst, p.i, err)
				continue
			}
			if gwData != nil {
				fmt.Printf("router %v\n", gwData)
				routerName := net.IP(gwData).String()
				fmt.Printf("%v, %v, %v, %v\n", "", "", "", p.YIAddr, routerName, p.i.Name)
				if err := netlink.AddRoute("", p.YIAddr().String(), routerName, p.i.Name); err != nil {
					fmt.Printf("Can't add route: %v\n", err)
				}
			}

		default:
			fmt.Printf("not what we hoped: %v\n", dhcp.MessageType(p.HType()))
		}
	}
}