forked from songgao/water
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
131 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// +build go1.11 | ||
|
||
package water | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCloseUnblockPendingRead(t *testing.T) { | ||
ifce, err := New(Config{DeviceType: TUN}) | ||
if err != nil { | ||
t.Fatalf("creating TUN error: %v\n", err) | ||
} | ||
|
||
c := make(chan struct{}) | ||
go func() { | ||
ifce.Read(make([]byte, 1<<16)) | ||
close(c) | ||
}() | ||
|
||
// make sure ifce.Close() happens after ifce.Read() blocks | ||
time.Sleep(1 * time.Second) | ||
|
||
ifce.Close() | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
|
||
select { | ||
case <-c: | ||
t.Log("Pending Read unblocked") | ||
case <-ctx.Done(): | ||
t.Fatal("Timeouted, pending read blocked") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// +build darwin,go1.11 | ||
|
||
package water | ||
|
||
import "syscall" | ||
|
||
func setNonBlock(fd int) error { | ||
return syscall.SetNonblock(fd, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// +build darwin,!go1.11 | ||
|
||
package water | ||
|
||
func setNonBlock(fd int) error { | ||
// There's a but pre-go1.11 that causes 'resource temporarily unavailable' | ||
// error in non-blocking mode. So just skip it here. Close() won't be able | ||
// to unblock a pending read, but that's better than being broken. | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// +build linux,go1.11 | ||
|
||
package water | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func openDev(config Config) (ifce *Interface, err error) { | ||
var fdInt int | ||
if fdInt, err = syscall.Open( | ||
"/dev/net/tun", os.O_RDWR|syscall.O_NONBLOCK, 0); err != nil { | ||
return nil, err | ||
} | ||
|
||
name, err := setupFd(config, uintptr(fdInt)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Interface{ | ||
isTAP: config.DeviceType == TAP, | ||
ReadWriteCloser: os.NewFile(uintptr(fdInt), "tun"), | ||
name: name, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// +build linux,!go1.11 | ||
|
||
package water | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
func openDev(config Config) (ifce *Interface, err error) { | ||
var file *os.File | ||
if file, err = os.OpenFile( | ||
"/dev/net/tun", os.O_RDWR, 0); err != nil { | ||
return nil, err | ||
} | ||
|
||
name, err := setupFd(config, file.Fd()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Interface{ | ||
isTAP: config.DeviceType == TAP, | ||
ReadWriteCloser: file, | ||
name: name, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters