Skip to content

Commit a11dd11

Browse files
committed
Support for ETH_P_ALL
1 parent f36baf9 commit a11dd11

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

sockets/raw_socket.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sockets
33
import (
44
"fmt"
55
"net"
6-
"os"
76
"strings"
87
"syscall"
98

@@ -27,7 +26,6 @@ type NetworkPacket interface {
2726
// RawSocket represents a raw socket and stores info about its file descriptor,
2827
// Ethernet protocol type and Link Layer info
2928
type RawSocket struct {
30-
shutdownChan chan os.Signal
3129
fd int
3230
ethType uint16
3331
layer4Filter string
@@ -45,7 +43,6 @@ func NewRawSocket(protocol string, ethType uint16) (*RawSocket, error) {
4543
}
4644

4745
rawSocket := &RawSocket{
48-
shutdownChan: make(chan os.Signal, 1),
4946
ethType: ethType,
5047
layer4Filter: filter,
5148
}
@@ -83,19 +80,23 @@ func (rs *RawSocket) ReadToChan(dataChan chan<- NetworkPacket, errChan chan<- er
8380
}
8481

8582
switch rs.ethType {
86-
case syscall.ETH_P_ARP:
87-
// TODO: ARP parsing
88-
case syscall.ETH_P_IP, syscall.ETH_P_IPV6:
89-
packet, err := protocols.IPPacketFromBytes(buf[:n])
83+
case syscall.ETH_P_ALL:
84+
ethFrame, err := protocols.EthFrameFromBytes(buf[:n])
9085
if err != nil {
91-
errChan <- fmt.Errorf("error reading IP packet: %v", err)
86+
errChan <- fmt.Errorf("failed to read ETH frame: %v", err)
9287
continue
9388
}
94-
// IPv4 VS IPv6 packets filtering should be handled by the socket itself
95-
l4Protocol := packet.Header().TransportLayerProtocol()
96-
if rs.layer4Filter == "all" || (l4Protocol == rs.layer4Filter) {
97-
handleLayer4Protocol(l4Protocol, packet, dataChan, errChan)
89+
90+
switch ethFrame.EtherType() {
91+
case "ARP":
92+
// TODO: ARP parsing
93+
case "IPv4", "IPv6":
94+
handleIPPacket(buf[:n], rs.layer4Filter, dataChan, errChan)
9895
}
96+
case syscall.ETH_P_ARP:
97+
// TODO: ARP parsing
98+
case syscall.ETH_P_IP, syscall.ETH_P_IPV6:
99+
handleIPPacket(buf[:n], rs.layer4Filter, dataChan, errChan)
99100
}
100101
}
101102
}
@@ -105,6 +106,19 @@ func (rs *RawSocket) Close() error {
105106
return syscall.Close(rs.fd)
106107
}
107108

109+
func handleIPPacket(raw []byte, filter string, dataChan chan<- NetworkPacket, errChan chan<- error) {
110+
packet, err := protocols.IPPacketFromBytes(raw)
111+
if err != nil {
112+
errChan <- fmt.Errorf("error reading IP packet: %v", err)
113+
return
114+
}
115+
// IPv4 VS IPv6 packets filtering should be handled by the socket itself
116+
l4Protocol := packet.Header().TransportLayerProtocol()
117+
if filter == "all" || (l4Protocol == filter) {
118+
handleLayer4Protocol(l4Protocol, packet, dataChan, errChan)
119+
}
120+
}
121+
108122
func handleLayer4Protocol(protocol string, packet protocols.IPPacket, dataChan chan<- NetworkPacket, errChan chan<- error) {
109123
var np NetworkPacket
110124
var err error

0 commit comments

Comments
 (0)