Example #1
0
func HostTun() {
	route_id, err := uuid.FromString(*route_id)
	if err != nil {
		panic(err)
	}

	cmd_stdoutQueue := make(chan interface{})
	cmd_stdinQueue := make(chan interface{})
	SpawnNodeSubprocess(*config_path, cmd_stdoutQueue, cmd_stdinQueue)

	// Wait for route establishment before setting up TUN
	for {
		msg_recvd := <-cmd_stdoutQueue
		if reflect.TypeOf(msg_recvd) == reflect.TypeOf(Stdout_EstablishedRoute{}) {
			break
		}
	}

	ifce, err1 := water.NewTUN("")
	if err1 != nil {
		fmt.Fprintf(os.Stderr, "Error creating tun interface: %v\n", err1)
		return
	}

	go func() {
		for {
			msg_recvd := <-cmd_stdoutQueue

			if reflect.TypeOf(msg_recvd) == reflect.TypeOf(Stdout_RecvMessage{}) {
				_, err := ifce.Write(msg_recvd.(Stdout_RecvMessage).Contents)
				if err == nil {
					fmt.Fprintf(os.Stderr, "Error writing to tun interface: %v\n", err)
					break
				}
			}
		}
	}()

	for {
		buffer := make([]byte, DATAGRAMSIZE)
		_, err2 := ifce.Read(buffer)
		if err2 != nil {
			fmt.Fprintf(os.Stderr, "Error reading from tun interface: %v\n", err2)
			break
		}
		cmd_stdinQueue <- Stdin_SendMessage{route_id, buffer}
	}

}
Example #2
0
func newTun(name string) (iface *water.Interface, err error) {

	iface, err = water.NewTUN(name)
	if err != nil {
		return nil, err
	}
	logger.Info("interface %v created", iface.Name())

	sargs := fmt.Sprintf("link set dev %s up mtu %d qlen 100", iface.Name(), MTU)
	args := strings.Split(sargs, " ")
	cmd := exec.Command("ip", args...)
	logger.Info("ip %s", sargs)
	err = cmd.Run()
	if err != nil {
		return nil, err
	}

	return iface, nil
}
Example #3
0
File: main.go Project: kanocz/lcvpn
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
}