-
Notifications
You must be signed in to change notification settings - Fork 21
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
3 changed files
with
89 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package tap | ||
|
||
import ( | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
const ifNameSize = 0x10 | ||
|
||
type Tap struct { | ||
fd int | ||
} | ||
|
||
type ifReq struct { | ||
Name [ifNameSize]byte | ||
Flags uint16 | ||
_ [0x28 - ifNameSize - 2]byte | ||
} | ||
|
||
func ioctl(fd, op, arg uintptr) (uintptr, error) { | ||
res, _, errno := syscall.Syscall( | ||
syscall.SYS_IOCTL, fd, op, arg) | ||
|
||
var err error = nil | ||
if errno != 0 { | ||
err = errno | ||
} | ||
|
||
return res, err | ||
} | ||
|
||
func New(name string) (*Tap, error) { | ||
var err error | ||
|
||
t := &Tap{} | ||
|
||
if t.fd, err = syscall.Open("/dev/net/tun", syscall.O_RDWR, 0); err != nil { | ||
return t, err | ||
} | ||
|
||
ifr := ifReq{ | ||
Name: [ifNameSize]byte{}, | ||
Flags: syscall.IFF_TAP | syscall.IFF_NO_PI, | ||
} | ||
copy(ifr.Name[:ifNameSize-1], name) | ||
|
||
ifrPtr := uintptr(unsafe.Pointer(&ifr)) | ||
if _, err = ioctl(uintptr(t.fd), syscall.TUNSETIFF, ifrPtr); err != nil { | ||
return t, err | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
func (t *Tap) Close() error { | ||
return syscall.Close(t.fd) | ||
} | ||
|
||
func (t Tap) Tx(bytes []byte) error { | ||
if _, err := syscall.Write(t.fd, bytes); err != nil { | ||
return err | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package tap_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bobuhiro11/gokvm/tap" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
t.Parallel() | ||
|
||
tap, err := tap.New("test_tap") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = tap.Close() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |