diff --git a/src/core/packetgen/connection.go b/src/core/packetgen/connection.go index 707c07b6..e74b36c2 100644 --- a/src/core/packetgen/connection.go +++ b/src/core/packetgen/connection.go @@ -27,7 +27,6 @@ import ( "crypto/tls" "fmt" "net" - "syscall" "time" "github.com/google/gopacket" @@ -65,51 +64,6 @@ type Connection interface { Target() string } -type rawConn struct { - fd int - buf gopacket.SerializeBuffer -} - -// openRawConn opens a raw ip network connection based on the provided config -// use ipv6 as it also supports ipv4 -func openRawConn() (*rawConn, error) { - fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW) - if err != nil { - return nil, err - } - - err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1) - if err != nil { - return nil, err - } - - return &rawConn{ - fd: fd, - buf: gopacket.NewSerializeBuffer(), - }, nil -} - -func (conn *rawConn) Write(packet Packet) (n int, err error) { - if err := packet.Serialize(conn.buf); err != nil { - return 0, fmt.Errorf("error serializing packet: %w", err) - } - - addr := &syscall.SockaddrInet4{} - - // ipv6 is not supported for now - copy(addr.Addr[:], packet.IP().To4()) - - return 0, syscall.Sendto(conn.fd, conn.buf.Bytes(), 0, addr) -} - -func (conn *rawConn) Close() error { - return syscall.Close(conn.fd) -} - -func (conn *rawConn) Target() string { return "raw://" } - -func (conn *rawConn) Read(_ []byte) (int, error) { return 0, nil } - type netConnConfig struct { Protocol string Address string diff --git a/src/core/packetgen/raw_conn_unix.go b/src/core/packetgen/raw_conn_unix.go new file mode 100644 index 00000000..3229b235 --- /dev/null +++ b/src/core/packetgen/raw_conn_unix.go @@ -0,0 +1,56 @@ +//go:build !windows +// +build !windows + +package packetgen + +import ( + "fmt" + "syscall" + + "github.com/google/gopacket" +) + +type rawConn struct { + fd int + buf gopacket.SerializeBuffer +} + +// openRawConn opens a raw ip network connection based on the provided config +// use ipv6 as it also supports ipv4 +func openRawConn() (*rawConn, error) { + fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW) + if err != nil { + return nil, err + } + + err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1) + if err != nil { + return nil, err + } + + return &rawConn{ + fd: fd, + buf: gopacket.NewSerializeBuffer(), + }, nil +} + +func (conn *rawConn) Write(packet Packet) (n int, err error) { + if err := packet.Serialize(conn.buf); err != nil { + return 0, fmt.Errorf("error serializing packet: %w", err) + } + + addr := &syscall.SockaddrInet4{} + + // ipv6 is not supported for now + copy(addr.Addr[:], packet.IP().To4()) + + return 0, syscall.Sendto(conn.fd, conn.buf.Bytes(), 0, addr) +} + +func (conn *rawConn) Close() error { + return syscall.Close(conn.fd) +} + +func (conn *rawConn) Target() string { return "raw://" } + +func (conn *rawConn) Read(_ []byte) (int, error) { return 0, nil } diff --git a/src/core/packetgen/raw_conn_windows.go b/src/core/packetgen/raw_conn_windows.go new file mode 100644 index 00000000..354b36fb --- /dev/null +++ b/src/core/packetgen/raw_conn_windows.go @@ -0,0 +1,25 @@ +//go:build windows +// +build windows + +package packetgen + +import "errors" + +// unsupported on windows +type rawConn struct{} + +func openRawConn() (*rawConn, error) { + return nil, errors.New("raw connections not supported on windows") +} + +func (conn *rawConn) Write(packet Packet) (n int, err error) { + return 0, errors.New("raw connections not supported on windows") +} + +func (conn *rawConn) Close() error { + return nil +} + +func (conn *rawConn) Target() string { return "raw://" } + +func (conn *rawConn) Read(_ []byte) (int, error) { return 0, nil }