Skip to content

Commit

Permalink
separate types for ICMP with 8-bit type/code, and with RFC-only combi…
Browse files Browse the repository at this point in the history
…nations (#70)

* icmp followup: separate types for ICMP with 8-bit type/code, and with RFC-only combinations

Signed-off-by: adisos <[email protected]>

* rename file

Signed-off-by: adisos <[email protected]>

* small updates

Signed-off-by: adisos <[email protected]>

* small fixes

Signed-off-by: adisos <[email protected]>

---------

Signed-off-by: adisos <[email protected]>
  • Loading branch information
adisos authored Oct 2, 2024
1 parent c4bc6d3 commit dbe18f8
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 247 deletions.
14 changes: 8 additions & 6 deletions pkg/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ func NewUDPSet() *Set {
}

// ICMPConnection returns a set of connections containing the ICMP protocol with specified type,code values
func ICMPConnection(icmpType, icmpCode *int64) (*Set, error) {
icmp, err := netp.ICMPFromTypeAndCode64(icmpType, icmpCode)
if err != nil {
return nil, err
}
return netset.NewICMPTransport(icmp), nil
func ICMPConnection(icmpType, icmpCode int64) *Set {
return netset.NewICMPTransport(icmpType, icmpType, icmpCode, icmpCode)
}

// ICMPConnectionTypeCodeRanges returns a set of connections containing the ICMP
// protocol with specified type,code ranges values
func ICMPConnectionTypeCodeRanges(minIcmpType, maxICMPType, minCode, maxCode int64) *Set {
return netset.NewICMPTransport(minIcmpType, maxICMPType, minCode, maxCode)
}

// All returns a set of all protocols (TCP,UPD,ICMP) in the set (with all possible properties values)
Expand Down
50 changes: 38 additions & 12 deletions pkg/connection/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package connection

import (
"github.com/np-guard/models/pkg/interval"
"github.com/np-guard/models/pkg/netp"
"github.com/np-guard/models/pkg/netset"
"github.com/np-guard/models/pkg/spec"
Expand Down Expand Up @@ -52,6 +53,40 @@ func getCubeAsTCPItems(srcPorts, dstPorts *netset.PortSet, p int64) []spec.TcpUd

type Details spec.ProtocolList

func getCubeAsICMPItems(typesSet, codesSet *interval.CanonicalSet) []spec.Icmp {
allTypes := typesSet.Equal(netset.AllICMPTypes())
allCodes := codesSet.Equal(netset.AllICMPCodes())
switch {
case allTypes && allCodes:
return []spec.Icmp{{Protocol: spec.IcmpProtocolICMP}}
case allTypes:
res := []spec.Icmp{}
for _, code64 := range codesSet.Elements() {
code := int(code64)
res = append(res, spec.Icmp{Protocol: spec.IcmpProtocolICMP, Code: &code})
}
return res
case allCodes:
res := []spec.Icmp{}
for _, type64 := range typesSet.Elements() {
t := int(type64)
res = append(res, spec.Icmp{Protocol: spec.IcmpProtocolICMP, Type: &t})
}
return res
default:
res := []spec.Icmp{}
// iterate both codes and types
for _, type64 := range typesSet.Elements() {
t := int(type64)
for _, code64 := range codesSet.Elements() {
code := int(code64)
res = append(res, spec.Icmp{Protocol: spec.IcmpProtocolICMP, Type: &t, Code: &code})
}
}
return res
}
}

// ToJSON returns a `Details` object for JSON representation of the input connection Set.
func ToJSON(c *Set) Details {
if c == nil {
Expand All @@ -72,19 +107,10 @@ func ToJSON(c *Set) Details {
}
}
for _, item := range c.ICMPSet().Partitions() {
if item.TypeCode != nil {
t := item.TypeCode.Type
res = append(res, spec.Icmp{
Protocol: spec.IcmpProtocolICMP,
Type: &t,
Code: item.TypeCode.Code,
})
} else {
res = append(res, spec.Icmp{
Protocol: spec.IcmpProtocolICMP,
})
icmpItems := getCubeAsICMPItems(item.Left, item.Right)
for _, item := range icmpItems {
res = append(res, item)
}
}

return Details(res)
}
8 changes: 8 additions & 0 deletions pkg/netp/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import (
"slices"
)

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

type ICMPTypeCode struct {
// ICMP type allowed.
Type int
Expand Down
Loading

0 comments on commit dbe18f8

Please sign in to comment.