1
0
mirror of https://github.com/fumiama/water.git synced 2026-06-07 19:40:38 +08:00

Merge pull request #24 from ArroyoNetworks/master

darwin: Detect IP Version for NULL Header on Write
This commit is contained in:
Song Gao
2017-05-22 19:00:23 -07:00
committed by GitHub

View File

@@ -151,6 +151,11 @@ func (t *tunReadCloser) Read(to []byte) (int, error) {
}
func (t *tunReadCloser) Write(from []byte) (int, error) {
if len(from) == 0 {
return 0, syscall.EIO
}
t.wMu.Lock()
defer t.wMu.Unlock()
@@ -159,7 +164,16 @@ func (t *tunReadCloser) Write(from []byte) (int, error) {
}
t.wBuf = t.wBuf[:len(from)+4]
t.wBuf[3] = 2 // Family: IP (2)
// Determine the IP Family for the NULL L2 Header
ipVer := from[0] >> 4
if ipVer == 4 {
t.wBuf[3] = syscall.AF_INET
} else if ipVer == 6 {
t.wBuf[3] = syscall.AF_INET6
} else {
return 0, errors.New("Unable to determine IP version from packet.")
}
copy(t.wBuf[4:], from)
n, err := t.f.Write(t.wBuf)