]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/cypherpunks.ru/govpn/tap.go
Various stylistic and grammar fixes
[govpn.git] / src / cypherpunks.ru / govpn / tap.go
index 6e545b4decfeae9262a73f82e00348e60c89c609..f24630a1e59067acacbd305b711f77f19f677ea4 100644 (file)
@@ -1,6 +1,6 @@
 /*
 GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -22,19 +22,18 @@ import (
        "io"
 )
 
+// TAP is a TUN or a TAP interface.
 type TAP struct {
        Name string
        Sink chan []byte
        dev  io.ReadWriter
-       buf0 []byte
-       buf1 []byte
-       bufZ bool
 }
 
 var (
        taps = make(map[string]*TAP)
 )
 
+// NewTAP creates a new TUN/TAP virtual interface
 func NewTAP(ifaceName string, mtu int) (*TAP, error) {
        tapRaw, err := newTAPer(ifaceName)
        if err != nil {
@@ -43,24 +42,25 @@ func NewTAP(ifaceName string, mtu int) (*TAP, error) {
        tap := TAP{
                Name: ifaceName,
                dev:  tapRaw,
-               buf0: make([]byte, mtu),
-               buf1: make([]byte, mtu),
                Sink: make(chan []byte),
        }
        go func() {
                var n int
                var err error
                var buf []byte
+               buf0 := make([]byte, mtu)
+               buf1 := make([]byte, mtu)
+               bufZ := false
                for {
-                       if tap.bufZ {
-                               buf = tap.buf0
+                       if bufZ {
+                               buf = buf0
                        } else {
-                               buf = tap.buf1
+                               buf = buf1
                        }
-                       tap.bufZ = !tap.bufZ
+                       bufZ = !bufZ
                        n, err = tap.dev.Read(buf)
                        if err != nil {
-                               panic("Reading TAP:" + err.Error())
+                               panic("Reading TUN/TAP:" + err.Error())
                        }
                        tap.Sink <- buf[:n]
                }
@@ -72,6 +72,7 @@ func (t *TAP) Write(data []byte) (n int, err error) {
        return t.dev.Write(data)
 }
 
+// TAPListen opens an existing TAP (creates if none exists)
 func TAPListen(ifaceName string, mtu int) (*TAP, error) {
        tap, exists := taps[ifaceName]
        if exists {