Exemplo n.º 1
0
func (link *Link) GetType() (intf_type byte, err error) {
	l := *link

	if !l.HasLink() {
		if err = l.SetLinkStatus(LINK_UP); err != nil {
			err = errors.New("SetLinkStatus failed: " + err.Error())
			return
		}
	}

	stdout, _, err := sys.Run(l.CmdIfconfig, l.Interface.Name)
	if err != nil {
		return
	}

	for _, line := range stdout {
		if strings.Contains(line, IFCONFIG_MEDIA_ETHERNET) {
			intf_type = INTF_TYPE_ETHERNET
			return
		} else if strings.Contains(line, IFCONFIG_MEDIA_WIRELESS) {
			intf_type = INTF_TYPE_WIRELESS
			return
		}
	}

	err = errors.New("Unknown interface type")

	return
}
Exemplo n.º 2
0
/*
 * Stop dhcpcd on intf. Print an error if anything goes wrong
 */
func (d Dhcpcd) Stop() (err error) {
	_, _, err = sys.Run(d.CmdDhcpcd, "-k", d.Interface)
	if err != nil {
		err = errors.New("Failed to stop dhcpcd: " + err.Error())
	}
	return
}
Exemplo n.º 3
0
func (i Ip) AddAddress(ip string, af byte) (err error) {
	use_af, err := mkUseAf(af)
	if err != nil {
		return
	}

	sys.Run("/sbin/ip", use_af, "addr", "add", ip, "dev", i.Interface)

	return
}
Exemplo n.º 4
0
func (l Link) GetMTU() (mtu int, err error) {
	stdout, _, err := sys.Run(l.CmdIfconfig, l.Interface.Name)
	if err != nil {
		return
	}

	raw_mtu := strings.Fields(stdout[0])[5]
	mtu, err = strconv.Atoi(raw_mtu)
	return
}
Exemplo n.º 5
0
func (i Ip) AddAddress(ip string, af byte) (err error) {
	use_af, err := mkUseAf(af)
	if err != nil {
		return
	}

	sys.Run("/sbin/ifconfig", i.Interface, use_af, "alias", ip)

	return
}
Exemplo n.º 6
0
func (r RIB) RemoveRoute(network net.IPNet) (err error) {
	_, use_af, err := getAfDetails(network)
	if err != nil {
		return
	}

	use_af = "-" + use_af

	_, _, err = sys.Run("/sbin/route", "delete", use_af, network.String())

	return
}
Exemplo n.º 7
0
func (r RIB) AddRoute(network net.IPNet, gateway net.IP) (err error) {
	_, use_af, err := getAfDetails(network)
	if err != nil {
		return
	}

	use_af = "-" + use_af

	_, _, err = sys.Run("/sbin/route", "add", use_af, network.String(), gateway.String())

	return
}
Exemplo n.º 8
0
func (r RA) DisableRA() (err error) {
	enabled, err := r.AcceptsRA()
	if err != nil {
		return
	}

	if enabled {
		_, _, err = sys.Run(r.CmdIfconfig, r.Interface, IFCONFIG_USE_AF_INET6, "-"+IFCONFIG_ENABLE_RTADV)
	}

	return
}
Exemplo n.º 9
0
func (i Ip) FlushAddresses(af byte) (err error) {
	use_af, err := mkUseAf(af)
	if err != nil {
		return
	}

	if len(use_af) == 0 {
		return
	}

	sys.Run("/sbin/ip", use_af, "addr", "flush", "dev", i.Interface, "scope", "global")

	return
}
Exemplo n.º 10
0
func (l Link) SetLinkStatus(link_status byte) (err error) {
	var status string
	if link_status == LINK_UP {
		status = "up"
	} else if link_status == LINK_DOWN {
		status = "down"
	} else {
		err = errors.New("Unknown link status: " + strconv.Itoa(int(link_status)))
		return
	}

	_, _, err = sys.Run(l.CmdIfconfig, l.Interface.Name, status)
	return
}
Exemplo n.º 11
0
func (r RA) AcceptsRA() (result bool, err error) {
	stdout, _, err := sys.Run(r.CmdIfconfig, r.Interface, IFCONFIG_USE_AF_INET6)
	if err != nil {
		return
	}

	for _, line := range stdout {
		if strings.Contains(line, IFCONFIG_ND6_OPTIONS) {
			result = strings.Contains(line, IFCONFIG_ACCEPT_RTADV)
			return
		}
	}

	return
}
Exemplo n.º 12
0
func (rc *ResolvConf) RemoveConfig() {
	r := *rc

	_, _, err := sys.Run(r.CmdResolvconf, "-d", r.Interface)
	if err != nil {
		err = errors.New("Failed to remove config for " + r.Interface + ": " + err.Error())
	}

	if err = r.UpdateResolvConf(); err != nil {
		err = errors.New("Failed to update /etc/resolv.conf: " + err.Error())
		return
	}

	*rc = r
}
Exemplo n.º 13
0
func (ovpn *OpenVPN) Start() (err error) {
	o := *ovpn

	_, _, err = sys.Run(o.CmdOpenvpn, "--daemon", "--config", o.ConfigFile, "--writepid", o.PidFile)
	if err != nil {
		err = errors.New("Failed to start OpenVPN: " + err.Error())
		return
	}

	var timeout_counter = 0
	for {
		if timeout_counter >= OVPN_STARTUP_MAXWAIT {
			err = errors.New("Connection establishment took too long")
			o.Stop()
			break
		}

		is_connected, e := o.IsConnected()
		if e != nil {
			err = e
			return
		}

		if is_connected {
			break
		}

		time.Sleep(1 * time.Second)
		timeout_counter += 1
	}

	raw_intf, err := net.InterfaceByName(o.InterfaceName)
	intf, err := network.InterfaceFactory(*raw_intf)
	if err != nil {
		return
	}

	o.Interface = intf

	*ovpn = o

	return
}
Exemplo n.º 14
0
func (s Service) IsRunning(name string) (result bool, err error) {
	stdout, stderr, err := sys.Run(CmdService, name, "status")
	log.Print(CmdService)
	log.Print(stdout)
	log.Print(stderr)
	if err != nil {
		return
	}

	if strings.Contains(stdout[0], SERVICE_SERVICE_RUNNING) {
		result = true
	} else if strings.Contains(stdout[0], SERVICE_SERVICE_NOT_RUNNING) {
		result = false
	} else if strings.Contains(stdout[0], SERVICE_SERVICE_NOT_ENABLED) {
		err = errors.New("Service " + name + " is not enabled")
	} else {
		err = errors.New("Unknown error trying to determine service status of " + name)
	}
	return
}
Exemplo n.º 15
0
func (r RIB) GetRoute(network net.IPNet) (result Route, err error) {
	af, use_af, err := getAfDetails(network)
	if err != nil {
		return
	}

	stdout, _, err := sys.Run("/usr/bin/netstat", "-rn", "-f", use_af)
	if err != nil {
		return
	}

	for _, line := range stdout {
		if strings.HasPrefix(line, "Routing") {
			continue
		} else if line == "" {
			continue
		} else if strings.HasPrefix(line, "Internet") {
			continue
		} else if strings.HasPrefix(line, "Destination") {
			continue
		}

		route, e := parseRoutingLine(af, line)
		if e != nil {
			continue
		}

		if (network.IP.Equal(route.Destination.IP)) &&
			(network.Mask.String() == route.Destination.Mask.String()) {
			result = route
			return
		}
	}

	err = errors.New("Failed to find any routes")

	return
}
Exemplo n.º 16
0
func (d Dhcpcd) GetOffer() (ip net.IP, network net.IPNet, err error) {

	if err = d.RemoveLeaseFile(); err != nil {
		err = errors.New("Failed to remove lease file: " + err.Error())
		return
	}

	stdout, _, err := sys.Run(d.CmdDhcpcd, "-4T", d.Interface)
	if err != nil {
		return
	}

	var raw_ip, cidr_mask string
	for _, line := range stdout {
		t := strings.Split(line, "=")
		if t[0] == "new_ip_address" {
			raw_ip = t[1]
		} else if t[0] == "new_subnet_cidr" {
			cidr_mask = t[1]
		}
	}

	if len(raw_ip) == 0 {
		err = errors.New("Failed to obtain ip address")
		return
	} else if len(cidr_mask) == 0 {
		err = errors.New("Failed to obtain cidr mask")
		return
	}

	i, n, err := net.ParseCIDR(raw_ip + "/" + cidr_mask)
	if err == nil {
		ip = i
		network = *n
	}

	return
}
Exemplo n.º 17
0
func (s Systemd) IsRunning(name string) (result bool, err error) {
	stdout, _, err := sys.Run(CmdSystemd, "status", name)
	if err != nil {
		result = false
		err = nil
		return
	}

	for _, line := range stdout {
		if strings.Contains(line, SYSTEMD_SERVICE_RUNNING) {
			result = true
			break
		} else if strings.Contains(line, SYSTEMD_SERVICE_NOT_RUNNING) {
			result = false
			break
		} else if strings.Contains(line, SYSTEMD_SERVICE_NOT_ENABLED) {
			err = errors.New("Service " + name + " is not enabled")
		} else {
			err = errors.New("Unknown error trying to determine service status of " + name)
		}
	}
	return
}
Exemplo n.º 18
0
/*
 * Start dhcpcd on intf. Print an error if anything goes wrong
 */
func (d Dhcpcd) Start() (err error) {
	var args []string
	args = append(args, "-q")

	if !d.UseResolvconf {
		args = append(args, "-C")
		args = append(args, "resolv.conf")
	}

	if !d.SetMtu {
		args = append(args, "-C")
		args = append(args, "mtu")
	}

	args = append(args, d.Interface)

	_, _, err = sys.Run(d.CmdDhcpcd, args...)
	if err != nil {
		err = errors.New("Failed to start dhcpcd: " + err.Error())
	}

	return
}
Exemplo n.º 19
0
func (l Link) HasCarrier() (result bool, err error) {
	stdout, _, err := sys.Run(l.CmdIfconfig, l.Interface.Name)
	if err != nil {
		return
	}

	for _, line := range stdout {
		if strings.Contains(line, IFCONFIG_STATUS) {
			status := strings.Split(line, " ")[1]
			if status == IFCONFIG_CARRIER_ACTIVE {
				result = true
				return
			} else if status == IFCONFIG_CARRIER_ASSOCIATED {
				result = true
				return
			}
		}
	}

	result = false

	return
}
Exemplo n.º 20
0
/*
 * Send count ARP Request packet(s) to ipaddr using arping. Return true if
 * the return code of arping is zero, false otherwise.
 */
func Arping(ipaddr net.IP, intf net.Interface, count int) (up bool, latency float64, err error) {
	if ipaddr == nil {
		return
	}

	arping, err := sys.BinaryPrefix("arping")
	if err != nil {
		return
	}

	stdout, _, err := sys.Run(arping, "-I", intf.Name, "-c", strconv.Itoa(count), ipaddr.String())

	var tot_latency float64 = 0

	if err == nil {
		up = true

		for _, line := range stdout {
			if !strings.Contains(line, "bytes from") {
				continue
			}

			raw_latency := strings.Fields(line)[6]
			raw_latency = strings.Split(raw_latency, "=")[1]
			l, err := strconv.ParseFloat(raw_latency, 64)
			if err != nil {
				continue
			}

			tot_latency += (l / 1000)
		}
	}

	latency = (tot_latency / float64(count))

	return
}
Exemplo n.º 21
0
Arquivo: fping.go Projeto: r3boot/rlib
/*
 * Send count icmp/ipv6-icmp packet(s) to ipaddr using fping. Return true if
 * the return code of fping is zero, false otherwise.
 */
func Fping(ipaddr net.IP, count int) (up bool, latency float64, err error) {
	var cmd string

	if ipaddr == nil {
		return
	}

	ip_len := len(ipaddr)
	if ip_len == net.IPv4len {
		cmd = "fping"
	} else if ip_len == net.IPv6len {
		cmd = "fping6"
	} else {
		err = errors.New("Unknown address length: " + strconv.Itoa(ip_len))
		return
	}

	fping, err := sys.BinaryPrefix(cmd)
	if err != nil {
		return
	}

	_, stderr, err := sys.Run(fping, "-q", "-c", strconv.Itoa(int(count)), ipaddr.String())
	if err == nil {
		up = true

		latency, err = strconv.ParseFloat(strings.Split(stderr[0], "/")[7], 64)
		if err != nil {
			err = errors.New("Error parsing float: " + strings.Split(stderr[0], "/")[7] + ": " + err.Error())
			up = false
			return
		}
	}

	return
}
Exemplo n.º 22
0
/*
 * Send count ARP Request packet(s) to ipaddr using arping. Return true if
 * the return code of arping is zero, false otherwise.
 */
func Arping(ipaddr net.IP, intf_name string, count int) (up bool, latency float64, err error) {
	if ipaddr == nil {
		return
	}

	arping, err := sys.BinaryPrefix("arping")
	if err != nil {
		return
	}

	stdout, _, err := sys.Run(arping, "-I", intf_name, "-c", strconv.Itoa(count), "-w", "3", ipaddr.String())

	var tot_latency float64 = 0

	if err == nil {
		up = true

		for _, line := range stdout {
			if !strings.HasPrefix(line, "Unicast reply from") {
				continue
			}

			raw_latency := strings.Replace(strings.Split(line, " ")[6], "ms", "", -1)
			l, err := strconv.ParseFloat(raw_latency, 64)
			if err != nil {
				continue
			}

			tot_latency += l
		}
	}

	latency = (tot_latency / float64(count)) / 1000

	return
}
Exemplo n.º 23
0
func (i Ip) FlushAddresses(af byte) (err error) {
	use_af, err := mkUseAf(af)
	if err != nil {
		return
	}

	intf, err := net.InterfaceByName(i.Interface)
	if err != nil {
		err = errors.New("Failed to find interface " + i.Interface + ": " + err.Error())
		return
	}

	addrs, err := intf.Addrs()
	if err != nil {
		err = errors.New("Failed to find addresses for " + i.Interface + ": " + err.Error())
		return
	}

	for _, addr := range addrs {
		sys.Run("/sbin/ifconfig", i.Interface, use_af, addr.String(), "delete")
	}

	return
}
Exemplo n.º 24
0
func (s Systemd) Restart(name string) (err error) {
	_, _, err = sys.Run(CmdSystemd, "restart", name)
	return
}
Exemplo n.º 25
0
func (s Systemd) Disable(name string) (err error) {
	_, _, err = sys.Run(CmdSystemd, "disable", name)
	return
}
Exemplo n.º 26
0
func (l Link) SetMTU(mtu int) (err error) {
	_, _, err = sys.Run(l.CmdIfconfig, l.Interface.Name, "mtu", strconv.Itoa(mtu))
	return
}
Exemplo n.º 27
0
func (s Service) Stop(name string) (err error) {
	_, _, err = sys.Run(CmdService, name, "stop")
	return
}
Exemplo n.º 28
0
func (s Systemd) Stop(name string) (err error) {
	_, _, err = sys.Run(CmdSystemd, "stop", name)
	return
}
Exemplo n.º 29
0
func (s Service) Restart(name string) (err error) {
	_, _, err = sys.Run(CmdService, name, "restart")
	return
}
Exemplo n.º 30
0
func (rc *ResolvConf) UpdateResolvConf() (err error) {
	r := *rc
	_, _, err = sys.Run(r.CmdResolvconf, "-u")
	return
}