@@ -3,7 +3,6 @@ package sockets
3
3
import (
4
4
"fmt"
5
5
"net"
6
- "os"
7
6
"strings"
8
7
"syscall"
9
8
@@ -27,7 +26,6 @@ type NetworkPacket interface {
27
26
// RawSocket represents a raw socket and stores info about its file descriptor,
28
27
// Ethernet protocol type and Link Layer info
29
28
type RawSocket struct {
30
- shutdownChan chan os.Signal
31
29
fd int
32
30
ethType uint16
33
31
layer4Filter string
@@ -45,7 +43,6 @@ func NewRawSocket(protocol string, ethType uint16) (*RawSocket, error) {
45
43
}
46
44
47
45
rawSocket := & RawSocket {
48
- shutdownChan : make (chan os.Signal , 1 ),
49
46
ethType : ethType ,
50
47
layer4Filter : filter ,
51
48
}
@@ -83,19 +80,23 @@ func (rs *RawSocket) ReadToChan(dataChan chan<- NetworkPacket, errChan chan<- er
83
80
}
84
81
85
82
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 ])
90
85
if err != nil {
91
- errChan <- fmt .Errorf ("error reading IP packet : %v" , err )
86
+ errChan <- fmt .Errorf ("failed to read ETH frame : %v" , err )
92
87
continue
93
88
}
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 )
98
95
}
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 )
99
100
}
100
101
}
101
102
}
@@ -105,6 +106,19 @@ func (rs *RawSocket) Close() error {
105
106
return syscall .Close (rs .fd )
106
107
}
107
108
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
+
108
122
func handleLayer4Protocol (protocol string , packet protocols.IPPacket , dataChan chan <- NetworkPacket , errChan chan <- error ) {
109
123
var np NetworkPacket
110
124
var err error
0 commit comments