Skip to content

Commit

Permalink
stylistic changes
Browse files Browse the repository at this point in the history
Signed-off-by: KonnovKM <[email protected]>
  • Loading branch information
Boneyan committed Jul 31, 2023
1 parent 5d50c4b commit 5771e16
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions dhcpv4/nclient4/ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ import (
)

const (
versIHL = 0
tos = 1
totalLen = 2
id = 4
flagsFO = 6
ttl = 8
protocol = 9
checksumOff = 10
srcAddr = 12
dstAddr = 16
ipVersionShift = 4
versIHL = 0
tos = 1
totalLen = 2
id = 4
flagsFO = 6
ttl = 8
protocol = 9
checksumOff = 10
srcAddr = 12
dstAddr = 16
)

// transportProtocolNumber is the number of a transport protocol.
Expand Down Expand Up @@ -102,7 +101,7 @@ const (
ipv4Version = 4
)

// IPVersion returns the version of IP used in the given packet. It returns -1
// ipVersion returns the version of IP used in the given packet. It returns -1
// if the packet is not large enough to contain the version field.
func ipVersion(b []byte) int {
// Length must be at least offset+length of version field.
Expand All @@ -112,24 +111,9 @@ func ipVersion(b []byte) int {
return int(b[versIHL] >> ipVersionShift)
}

// IsValid performs basic validation on the packet.
func (b ipv4) isValid(pktSize int) bool {
if len(b) < ipv4MinimumSize {
return false
}

hlen := int(b.headerLength())
tlen := int(b.totalLength())
if hlen < ipv4MinimumSize || hlen > tlen || tlen > pktSize {
return false
}

if ipVersion(b) != ipv4Version {
return false
}

return true
}
const (
ipVersionShift = 4
)

// headerLength returns the value of the "header length" field of the ipv4
// header.
Expand Down Expand Up @@ -204,6 +188,25 @@ func (b ipv4) encode(i *ipv4Fields) {
copy(b[dstAddr:dstAddr+ipv4AddressSize], i.DstAddr)
}

// isValid performs basic validation on the packet.
func (b ipv4) isValid(pktSize int) bool {
if len(b) < ipv4MinimumSize {
return false
}

Check warning on line 195 in dhcpv4/nclient4/ipv4.go

View check run for this annotation

Codecov / codecov/patch

dhcpv4/nclient4/ipv4.go#L194-L195

Added lines #L194 - L195 were not covered by tests

hlen := int(b.headerLength())
tlen := int(b.totalLength())
if hlen < ipv4MinimumSize || hlen > tlen || tlen > pktSize {
return false
}

Check warning on line 201 in dhcpv4/nclient4/ipv4.go

View check run for this annotation

Codecov / codecov/patch

dhcpv4/nclient4/ipv4.go#L200-L201

Added lines #L200 - L201 were not covered by tests

if ipVersion(b) != ipv4Version {
return false
}

Check warning on line 205 in dhcpv4/nclient4/ipv4.go

View check run for this annotation

Codecov / codecov/patch

dhcpv4/nclient4/ipv4.go#L204-L205

Added lines #L204 - L205 were not covered by tests

return true
}

const (
udpSrcPort = 0
udpDstPort = 2
Expand Down

0 comments on commit 5771e16

Please sign in to comment.