From 871f836aa9381a0c7c85a0f2b3edefec2c054741 Mon Sep 17 00:00:00 2001 From: Yair Slobodin Date: Mon, 7 Oct 2024 15:47:25 +0300 Subject: [PATCH 1/2] new icmp without rfc validation --- pkg/netp/icmp.go | 37 +++++++++++++++++++++++++++++++------ pkg/netset/icmpset.go | 4 ++-- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/pkg/netp/icmp.go b/pkg/netp/icmp.go index 174a2ae..65bac02 100644 --- a/pkg/netp/icmp.go +++ b/pkg/netp/icmp.go @@ -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 { @@ -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 { @@ -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 diff --git a/pkg/netset/icmpset.go b/pkg/netset/icmpset.go index 96b6995..22136bc 100644 --- a/pkg/netset/icmpset.go +++ b/pkg/netset/icmpset.go @@ -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() From 57cc0ad01d9e4bdce9e914dd1086c004a8e38b3f Mon Sep 17 00:00:00 2001 From: Yair Slobodin Date: Tue, 8 Oct 2024 12:10:20 +0300 Subject: [PATCH 2/2] rename function --- pkg/netp/icmp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/netp/icmp.go b/pkg/netp/icmp.go index 65bac02..e69201c 100644 --- a/pkg/netp/icmp.go +++ b/pkg/netp/icmp.go @@ -92,7 +92,7 @@ func ICMPFromTypeAndCode64(icmpType, icmpCode *int64) (ICMP, error) { return ICMPFromTypeAndCode(int64ToInt(icmpType), int64ToInt(icmpCode)) } -func ICMPFromTypeAndCode64WithoutValidation(icmpType, icmpCode *int64) (ICMP, error) { +func ICMPFromTypeAndCode64WithoutRFCValidation(icmpType, icmpCode *int64) (ICMP, error) { return ICMPFromTypeAndCodeWithoutRFCValidation(int64ToInt(icmpType), int64ToInt(icmpCode)) }