[PATCH 2/7] tun_linux: add PIHeaderSize constant instead of magic value

Simon Ruderich simon at ruderich.org
Sun Dec 31 17:16:49 CET 2017


---
 src/tun_linux.go | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/tun_linux.go b/src/tun_linux.go
index daa2462..7fcbe00 100644
--- a/src/tun_linux.go
+++ b/src/tun_linux.go
@@ -48,6 +48,7 @@ import "C"
 const (
 	CloneDevicePath = "/dev/net/tun"
 	IFReqSize       = unix.IFNAMSIZ + 64
+	PIHeaderSize    = 4 // packet information of each frame (if IFF_NO_PI unset)
 )
 
 type NativeTun struct {
@@ -254,7 +255,7 @@ func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
 
 	// reserve space for header
 
-	buff = buff[offset-4:]
+	buff = buff[offset-PIHeaderSize:]
 
 	// add packet information header
 
@@ -279,12 +280,12 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
 	case err := <-tun.errors:
 		return 0, err
 	default:
-		buff := buff[offset-4:]
+		buff := buff[offset-PIHeaderSize:]
 		n, err := tun.fd.Read(buff[:])
-		if n < 4 {
+		if n < PIHeaderSize {
 			return 0, err
 		}
-		return n - 4, err
+		return n - PIHeaderSize, err
 	}
 }
 
-- 
2.15.1



More information about the WireGuard mailing list