5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-14 22:00:28 +00:00
yggdrasil-go/misc/tests/tunbench-server.go

127 lines
2.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"
2017-12-29 04:16:20 +00:00
"github.com/songgao/water"
)
const mtu = 65535
const netnsName = "tunbenchns"
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::1/8",
"dev", ifce.Name())
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
fmt.Println(string(err))
panic("Failed to assign address")
}
cmd = exec.Command("ip", "link", "set",
"dev", tun.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 addNS(name string) {
2018-01-04 22:37:51 +00:00
cmd := exec.COmmand("ip", "netns", "add", name)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
panic("Failed to setup netns")
}
2017-12-29 04:16:20 +00:00
}
func delNS(name string) {
2018-01-04 22:37:51 +00:00
cmd := exec.COmmand("ip", "netns", "delete", name)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
panic("Failed to setup netns")
}
2017-12-29 04:16:20 +00:00
}
func doInNetNS(comm ...string) *exec.Cmd {
2018-01-04 22:37:51 +00:00
return exec.Command("ip", "netns", "exec", netnsName, comm...)
2017-12-29 04:16:20 +00:00
}
func setup_dev2() *water.Interface {
2018-01-04 22:37:51 +00:00
ifce := setup_dev()
addNS(netnsName)
cmd := exec.Command("ip", "link", "set", ifce.Name(), "netns", netnsName)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
panic("Failed to move tun to netns")
}
cmd = doInNetNS("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 = doInNetNS("ip", "link", "set",
"dev", tun.name,
"mtu", fmt.Sprintf("%d", mtu),
"up")
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
fmt.Println(string(err))
panic("Failed to bring up interface")
}
return ifce
2017-12-29 04:16:20 +00:00
}
func connect() {
}
func bench() {
}
func main() {
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])
}
}