Skip to content

Commit

Permalink
disable raw conn on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Oct 8, 2022
1 parent 435f18e commit acc9c83
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 46 deletions.
46 changes: 0 additions & 46 deletions src/core/packetgen/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"crypto/tls"
"fmt"
"net"
"syscall"
"time"

"github.com/google/gopacket"
Expand Down Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions src/core/packetgen/raw_conn_unix.go
Original file line number Diff line number Diff line change
@@ -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 }
25 changes: 25 additions & 0 deletions src/core/packetgen/raw_conn_windows.go
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit acc9c83

Please sign in to comment.