只是一个简单的计网课设
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
eruhs/core/handler.go

80 lines
1.8 KiB

8 months ago
package core
import (
"eruhs/core/parser"
"github.com/google/gopacket"
)
func (c *Capture) handlePacket(packet gopacket.Packet) (*DataPack, error) {
parsedPack := DataPack{}
data := packet.Data()
parsedPack.TotalLength = len(data)
eth, err := parser.DecodeEthernet(data)
if err != nil {
return nil, err
}
parsedPack.AddLayer(eth)
switch eth.Type {
case parser.EthernetTypeARP:
return c.handleARP(eth.Payload, parsedPack)
case parser.EthernetTypeIPv4:
return c.handleIPv4(eth.Payload, parsedPack)
//case custom.EthernetTypeIPv6:
default:
}
return &parsedPack, nil
}
func (c *Capture) handleARP(data []byte, parsedPack DataPack) (*DataPack, error) {
arp, err := parser.DecodeARP(data)
if err != nil {
return &parsedPack, err
}
parsedPack.AddLayer(arp)
return &parsedPack, nil
}
func (c *Capture) handleIPv4(data []byte, parsedPack DataPack) (*DataPack, error) {
ipv4, err := parser.DecodeIPV4(data)
if err != nil {
return &parsedPack, err
}
parsedPack.AddLayer(ipv4)
switch ipv4.Protocol {
case parser.IPv4ProtoTCP:
return c.handleTCP(ipv4.Payload, parsedPack)
case parser.IPv4ProtoUDP:
return c.handleUDP(ipv4.Payload, parsedPack)
default:
// unsupported protocol
}
return &parsedPack, nil
}
func (c *Capture) handleTCP(data []byte, parsedPack DataPack) (*DataPack, error) {
tcp, err := parser.DecodeTCP(data)
if err != nil {
return &parsedPack, err
}
parsedPack.AddLayer(tcp)
return &parsedPack, nil
}
func (c *Capture) handleUDP(data []byte, parsedPack DataPack) (*DataPack, error) {
udp, err := parser.DecodeUDP(data)
if err != nil {
return &parsedPack, err
}
parsedPack.AddLayer(udp)
return &parsedPack, nil
}