Skip to content

Commit

Permalink
fix newstrategy test in geneva_test.go, fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
garmr-ulfr committed Oct 13, 2023
1 parent 9257f73 commit e5dd902
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 80 deletions.
8 changes: 6 additions & 2 deletions actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
package actions

import (
"errors"
"fmt"

"github.com/google/gopacket"

Check failure on line 10 in actions/actions.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/google/gopacket' is not allowed from list 'Main' (depguard)

"github.com/getlantern/geneva/internal"
"github.com/getlantern/geneva/internal/scanner"
"github.com/getlantern/geneva/triggers"
"github.com/google/gopacket"

// gopacket best practice says import this, too.
_ "github.com/google/gopacket/layers"
)

var ErrInvalidAction = errors.New("invalid action")

// ActionTree represents a Geneva (trigger, action) pair.
//
// Technically, Geneva uses the term "action tree" to refer to the tree of actions in the tuple
Expand Down Expand Up @@ -122,5 +126,5 @@ func ParseAction(s *scanner.Scanner) (Action, error) {
return DefaultSendAction, nil
}

return nil, fmt.Errorf("invalid action at %d", s.Pos())
return nil, fmt.Errorf("%w at %d", ErrInvalidAction, s.Pos())
}
7 changes: 6 additions & 1 deletion actions/duplicate_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"errors"
"fmt"

"github.com/google/gopacket"

Check failure on line 7 in actions/duplicate_action.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/google/gopacket' is not allowed from list 'Main' (depguard)

"github.com/getlantern/geneva/internal"
"github.com/getlantern/geneva/internal/scanner"
"github.com/google/gopacket"
)

// DuplicateAction is a Geneva action that duplicates a packet and applies separate action trees to
Expand Down Expand Up @@ -114,6 +115,10 @@ func ParseDuplicateAction(s *scanner.Scanner) (Action, error) {
}

if action.Left, err = ParseAction(s); err != nil {
if !errors.Is(err, ErrInvalidAction) {
return nil, err
}

if c, err2 := s.Peek(); err2 == nil && c == ',' {
action.Left = &SendAction{}
} else {
Expand Down
22 changes: 16 additions & 6 deletions actions/fragment_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func fragmentTCPSegment(packet gopacket.Packet, fragSize int) ([]gopacket.Packet
ipHdrLen := uint16(ipv4Buf[0]&0x0f) * 4

first := gopacket.NewPacket(buf, packet.Layers()[0].LayerType(), gopacket.NoCopy)
updateChecksums(first)
if err := updateChecksums(first); err != nil {
return nil, err
}

// create the second fragment.
f2Len := headersLen + tcpPayloadLen - fragSize
Expand All @@ -166,7 +168,9 @@ func fragmentTCPSegment(packet gopacket.Packet, fragSize int) ([]gopacket.Packet
binary.BigEndian.PutUint32(tcp[4:], seqNum)

second := gopacket.NewPacket(buf, packet.Layers()[0].LayerType(), gopacket.NoCopy)
updateChecksums(second)
if err := updateChecksums(second); err != nil {
return nil, err
}

return []gopacket.Packet{first, second}, nil
}
Expand Down Expand Up @@ -260,14 +264,16 @@ func FragmentIPPacket(packet gopacket.Packet, fragSize int) ([]gopacket.Packet,
return []gopacket.Packet{first, second}, nil
}

func updateChecksums(packet gopacket.Packet) {
if ipv4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4); ipv4 != nil {
func updateChecksums(packet gopacket.Packet) error {
if ipv4, _ := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4); ipv4 != nil {
common.UpdateIPv4Checksum(ipv4)
}

if tcp := packet.Layer(layers.LayerTypeTCP).(*layers.TCP); tcp != nil {
common.UpdateTCPChecksum(tcp)
if tcp, _ := packet.Layer(layers.LayerTypeTCP).(*layers.TCP); tcp != nil {
return common.UpdateTCPChecksum(tcp)
}

return nil
}

// VerifyIPv4Checksum verifies whether an IPv4 header's checksum field is correct.
Expand Down Expand Up @@ -375,6 +381,10 @@ func ParseFragmentAction(s *scanner.Scanner) (Action, error) {
}

if action.FirstFragmentAction, err = ParseAction(s); err != nil {
if !errors.Is(err, ErrInvalidAction) {
return nil, err
}

if c, err2 := s.Peek(); err2 == nil && c == ',' {
action.FirstFragmentAction = &SendAction{}
} else {
Expand Down
Loading

0 comments on commit e5dd902

Please sign in to comment.