Skip to content

Commit

Permalink
new icmp without rfc validation
Browse files Browse the repository at this point in the history
  • Loading branch information
YairSlobodin1 committed Oct 7, 2024
1 parent fdc2963 commit 871f836
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
37 changes: 31 additions & 6 deletions pkg/netp/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (

// general non-strict ICMP type, code ranges
const (
MinICMPType int64 = 0
MaxICMPType int64 = 254
MinICMPCode int64 = 0
MaxICMPCode int64 = 255
MinICMPType int = 0
MaxICMPType int = 254
MinICMPCode int = 0
MaxICMPCode int = 255
)

type ICMPTypeCode struct {
Expand Down Expand Up @@ -49,14 +49,35 @@ func NewICMP(typeCode *ICMPTypeCode) (ICMP, error) {
return ICMP{TypeCode: res}, nil
}

func NewICMPWithoutRFCValidation(typeCode *ICMPTypeCode) (ICMP, error) {
if typeCode == nil {
return ICMP{TypeCode: nil}, nil
}
if typeCode.Type < MinICMPType || typeCode.Type > MaxICMPType {
return ICMP{}, fmt.Errorf("icmp type must be in the range [%d-%d]; got %d", MinICMPType, MaxICMPType, typeCode.Type)
}
if typeCode.Code != nil && (*typeCode.Code < MinICMPCode || *typeCode.Code > MaxICMPCode) {
return ICMP{}, fmt.Errorf("icmp code must be in the range [%d-%d]; got %d", MinICMPCode, MaxICMPCode, typeCode.Code)
}
return ICMP{TypeCode: &ICMPTypeCode{Type: typeCode.Type, Code: typeCode.Code}}, nil
}

func ICMPFromTypeAndCode(icmpType, icmpCode *int) (ICMP, error) {
return newICMPFromTypeAndCode(icmpType, icmpCode, NewICMP)
}

func ICMPFromTypeAndCodeWithoutRFCValidation(icmpType, icmpCode *int) (ICMP, error) {
return newICMPFromTypeAndCode(icmpType, icmpCode, NewICMPWithoutRFCValidation)
}

func newICMPFromTypeAndCode(icmpType, icmpCode *int, newIcmp func(*ICMPTypeCode) (ICMP, error)) (ICMP, error) {
if icmpType == nil && icmpCode != nil {
return ICMP{}, fmt.Errorf("cannot specify ICMP code without ICMP type")
}
if icmpType != nil {
return NewICMP(&ICMPTypeCode{Type: *icmpType, Code: icmpCode})
return newIcmp(&ICMPTypeCode{Type: *icmpType, Code: icmpCode})
}
return NewICMP(nil)
return newIcmp(nil)
}

func int64ToInt(i *int64) *int {
Expand All @@ -71,6 +92,10 @@ func ICMPFromTypeAndCode64(icmpType, icmpCode *int64) (ICMP, error) {
return ICMPFromTypeAndCode(int64ToInt(icmpType), int64ToInt(icmpCode))
}

func ICMPFromTypeAndCode64WithoutValidation(icmpType, icmpCode *int64) (ICMP, error) {
return ICMPFromTypeAndCodeWithoutRFCValidation(int64ToInt(icmpType), int64ToInt(icmpCode))
}

func (t ICMP) ICMPTypeCode() *ICMPTypeCode {
if t.TypeCode == nil {
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/netset/icmpset.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func AllICMPSet() *ICMPSet {
}

func AllICMPCodes() *CodeSet {
return interval.New(netp.MinICMPCode, netp.MaxICMPCode).ToSet()
return interval.New(int64(netp.MinICMPCode), int64(netp.MaxICMPCode)).ToSet()
}

func AllICMPTypes() *TypeSet {
return interval.New(netp.MinICMPType, netp.MaxICMPType).ToSet()
return interval.New(int64(netp.MinICMPType), int64(netp.MaxICMPType)).ToSet()
}

var allICMP = AllICMPSet()
Expand Down

0 comments on commit 871f836

Please sign in to comment.