5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-14 04:30:32 +00:00
yggdrasil-go/misc/tests/tunbench-client.go

84 lines
1.4 KiB
Go
Raw Normal View History

2017-12-29 04:16:20 +00:00
package main
import (
2018-01-04 22:37:51 +00:00
"fmt"
2017-12-29 04:16:20 +00:00
"log"
2018-01-04 22:37:51 +00:00
"net"
"os/exec"
"time"
2017-12-29 04:16:20 +00:00
"github.com/songgao/water"
)
const mtu = 65535
func setup_dev() *water.Interface {
ifce, err := water.New(water.Config{
DeviceType: water.TUN,
})
if err != nil {
2018-01-04 22:37:51 +00:00
panic(err)
2017-12-29 04:16:20 +00:00
}
2018-01-04 22:37:51 +00:00
return ifce
2017-12-29 04:16:20 +00:00
}
func setup_dev1() *water.Interface {
2018-01-04 22:37:51 +00:00
ifce := setup_dev()
cmd := exec.Command("ip", "-f", "inet6",
"addr", "add", "fc00::2/8",
"dev", ifce.Name())
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
panic("Failed to assign address")
}
cmd = exec.Command("ip", "link", "set",
"dev", ifce.Name(),
"mtu", fmt.Sprintf("%d", mtu),
"up")
out, err = cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
panic("Failed to bring up interface")
}
return ifce
2017-12-29 04:16:20 +00:00
}
func connect(ifce *water.Interface) {
2018-01-04 22:37:51 +00:00
conn, err := net.DialTimeout("tcp", "192.168.2.2:9001", time.Second)
if err != nil {
panic(err)
}
sock := conn.(*net.TCPConn)
// TODO go a worker to move packets to/from the tun
2017-12-29 04:16:20 +00:00
}
func bench() {
}
func main() {
2018-01-04 22:37:51 +00:00
ifce := setup_dev1()
connect(ifce)
bench()
fmt.Println("Done?")
return
2017-12-29 04:16:20 +00:00
ifce, err := water.New(water.Config{
DeviceType: water.TUN,
})
if err != nil {
2018-01-04 22:37:51 +00:00
panic(err)
2017-12-29 04:16:20 +00:00
}
log.Printf("Interface Name: %s\n", ifce.Name())
packet := make([]byte, 2000)
for {
n, err := ifce.Read(packet)
if err != nil {
2018-01-04 22:37:51 +00:00
panic(err)
2017-12-29 04:16:20 +00:00
log.Fatal(err)
}
log.Printf("Packet Received: % x\n", packet[:n])
}
}