add go fd test

This commit is contained in:
KusakabeSi 2021-10-06 09:22:20 +00:00
parent 776df87879
commit bfd76ff76a
4 changed files with 83 additions and 3 deletions

1
example_config/super_mode/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
n1_test_fd_mode2

View File

@ -47,8 +47,10 @@ os.environ["EG_FD_TX"] = str(ew)
print(str(er), str(ew)) print(str(er), str(ew))
#p = subprocess.Popen('./etherguard-go -config example_config/super_mode/n1.yaml -mode edge'.split(" "),pass_fds=[er,ew]) #p = subprocess.Popen('./etherguard-go -config example_config/super_mode/n1_fd.yaml -mode edge'.split(" "),pass_fds=[er,ew])
p = subprocess.Popen('python3 example_config/super_mode/n1_test_fd_mode2.py'.split(" "),pass_fds=[er,ew]) #p = subprocess.Popen('python3 example_config/super_mode/n1_test_fd_mode2.py'.split(" "),pass_fds=[er,ew])
p = subprocess.Popen('example_config/super_mode/n1_test_fd_mode2'.split(" "),pass_fds=[er,ew])
os.close(er) os.close(er)
os.close(ew) os.close(ew)

View File

@ -0,0 +1,77 @@
package main
import (
"fmt"
"os"
"errors"
"time"
"syscall"
"os/signal"
"strconv"
)
func ReadLoop(fileRX *os.File) {
buf := make([]byte,65535)
for {
size, err := fileRX.Read(buf)
if err != nil{
fmt.Println(err)
return
}
fmt.Println("Go read fd" , fileRX.Fd() , ":" ,buf[:size])
}
}
func WriteLoop(fileTX *os.File) {
t := 0
packet := ":gogogogogogogogogogogogo\n"
for {
fmt.Println("Go write fd:" , fileTX.Fd())
_, err := fileTX.Write([]byte( strconv.Itoa(t) + packet))
if err != nil {
fmt.Println(err)
return
}
t += 1
time.Sleep(1 * time.Second)
}
}
func cleanup(ftx *os.File,frx *os.File) {
fmt.Println("Ctrl + C, exit")
ftx.Close()
frx.Close()
}
func main() {
fdRXstr, has := os.LookupEnv("EG_FD_RX")
if !has {
fmt.Println(errors.New("Need Environment Variable EG_FD_RX"))
return
}
fdRX, err := strconv.Atoi(fdRXstr)
if err != nil {
fmt.Println(err)
return
}
fdTxstr, has := os.LookupEnv("EG_FD_TX")
if !has {
fmt.Println(errors.New("Need Environment Variable EG_FD_TX"))
return
}
fdTX, err := strconv.Atoi(fdTxstr)
if err != nil {
fmt.Println(err)
return
}
fileRX := os.NewFile(uintptr(fdRX), "pipeRX")
fileTX := os.NewFile(uintptr(fdTX), "pipeTX")
go ReadLoop(fileRX)
go WriteLoop(fileTX)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
cleanup(fileRX,fileTX)
}

View File

@ -59,7 +59,7 @@ func (tap *FdTap) Read(buf []byte, offset int) (int, error) {
} // read a packet from the device (without any additional headers) } // read a packet from the device (without any additional headers)
func (tap *FdTap) Write(buf []byte, offset int) (size int, err error) { func (tap *FdTap) Write(buf []byte, offset int) (size int, err error) {
packet := buf[offset:] packet := buf[offset:]
size, err = tap.fileRX.Write(packet) size, err = tap.fileTX.Write(packet)
if err != nil { if err != nil {
return 0, err return 0, err
} }