Exemple #1
0
func setupVEth(stn *SandboxNetwork) error {
	ifc, err := tenus.NewLinkFrom(stn.VethGuest)

	if err != nil {
		return fmt.Errorf("Unable to fetch inteface %s, %s.", stn.VethGuest, err)
	}

	// Bring the link down to prepare for renaming
	if err = ifc.SetLinkDown(); err != nil {
		return fmt.Errorf("Unable to bring interface %s down, %s.", stn.VethGuest, err)
	}

	// Rename the interface to a standard eth0 (not really a necessity)
	if err = tenus.RenameInterfaceByName(stn.VethGuest, ozDefaultInterfaceInternal); err != nil {
		return fmt.Errorf("Unable to rename interface %s, %s.", stn.VethGuest, err)
	}

	// Refetch the interface again as it has changed
	ifc, err = tenus.NewLinkFrom(ozDefaultInterfaceInternal)
	if err != nil {
		return fmt.Errorf("Unable to fetch interface %s, %s.", ozDefaultInterfaceInternal, err)
	}

	// Bring the link back up
	if err = ifc.SetLinkUp(); err != nil {
		return fmt.Errorf("Unable to bring interface %s up, %s.", ozDefaultInterfaceInternal, err)
	}

	// Set the link's default gateway
	if err = ifc.SetLinkDefaultGw(&stn.Gateway); err != nil {
		return fmt.Errorf("Unable to set default route %s.", err)
	}

	stn.Interface = ifc

	return nil
}
Exemple #2
0
func setupLoopback(stn *SandboxNetwork) error {
	// Bring loopback interface up
	lo, err := tenus.NewLinkFrom("lo")
	if err != nil {
		return fmt.Errorf("Unable to fetch loopback interface, %s.", err)
	}

	// Bring the link up
	err = lo.SetLinkUp()
	if err != nil {
		return fmt.Errorf("Unable to bring loopback interface up, %s.", err)
	}

	return nil
}
Exemple #3
0
func main() {

	version := flag.Bool("version", false, "print lcvpn version")
	flag.Parse()

	if *version {
		fmt.Println(AppVersion)
		os.Exit(0)
	}

	routeReload := make(chan bool, 1)

	initConfig(routeReload)

	conf := config.Load().(VPNState)

	lIP, lNet, err := net.ParseCIDR(conf.Main.local)
	if nil != err {
		flag.Usage()
		log.Fatalln("\nlocal ip is not in ip/cidr format")
	}

	iface, err := water.NewTUN("")

	if nil != err {
		log.Fatalln("Unable to allocate TUN interface:", err)
	}

	log.Println("Interface allocated:", iface.Name())

	link, err := tenus.NewLinkFrom(iface.Name())
	if nil != err {
		log.Fatalln("Unable to get interface info", err)
	}

	err = link.SetLinkMTU(MTU)
	if nil != err {
		log.Fatalln("Unable to set MTU to 1300 on interface")
	}

	err = link.SetLinkIp(lIP, lNet)
	if nil != err {
		log.Fatalln("Unable to set IP to ", lIP, "/", lNet, " on interface")
	}

	err = link.SetLinkUp()
	if nil != err {
		log.Fatalln("Unable to UP interface")
	}

	// start routes changes in config monitoring
	go routesThread(iface.Name(), routeReload)

	log.Println("Interface parameters configured")

	// Start listen threads
	for i := 0; i < conf.Main.RecvThreads; i++ {
		go rcvrThread("udp4", conf.Main.Port, iface)
	}

	// init udp socket for write

	writeAddr, err := net.ResolveUDPAddr("udp", ":")
	if nil != err {
		log.Fatalln("Unable to get UDP socket:", err)
	}

	writeConn, err := net.ListenUDP("udp", writeAddr)
	if nil != err {
		log.Fatalln("Unable to create UDP socket:", err)
	}
	defer writeConn.Close()

	// Start sender threads

	for i := 0; i < conf.Main.SendThreads; i++ {
		go sndrThread(writeConn, iface)
	}

	exitChan := make(chan os.Signal, 1)
	signal.Notify(exitChan, syscall.SIGTERM)

	<-exitChan
}