Skip to content

Commit

Permalink
all: imp dhcp host normalization, validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Apr 20, 2021
1 parent cd9eb3b commit 20748f2
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 53 deletions.
75 changes: 75 additions & 0 deletions internal/aghstrings/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package aghstrings

// unit is a convenient alias for struct{}
type unit = struct{}

// Set is a set of strings.
type Set struct {
m map[string]unit
}

// NewSet returns a new string set containing strs.
func NewSet(strs ...string) (set *Set) {
set = &Set{
m: make(map[string]unit, len(strs)),
}

for _, s := range strs {
set.Add(s)
}

return set
}

// Add adds s to the set. Add panics if the set is a nil set, just like a nil
// map does.
func (set *Set) Add(s string) {
set.m[s] = unit{}
}

// Del deletes s from the set. Calling Del on a nil set has no effect, just
// like delete on an empty map doesn't.
func (set *Set) Del(s string) {
if set == nil {
return
}

delete(set.m, s)
}

// Has returns true if s is in the set. Calling Has on a nil set returns false,
// just like indexing on an empty map does.
func (set *Set) Has(s string) (ok bool) {
if set == nil {
return false
}

_, ok = set.m[s]

return ok
}

// Len returns the length of the set. A nil set has a length of zero, just like
// an empty map.
func (set *Set) Len() (n int) {
if set == nil {
return 0
}

return len(set.m)
}

// Values returns all values in the set. The order of the values is undefined.
// Values returns nil if the set is nil.
func (set *Set) Values() (strs []string) {
if set == nil {
return nil
}

strs = make([]string, 0, len(set.m))
for s := range set.m {
strs = append(strs, s)
}

return strs
}
56 changes: 56 additions & 0 deletions internal/aghstrings/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package aghstrings

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSet(t *testing.T) {
const s = "a"

t.Run("nil", func(t *testing.T) {
var set *Set

assert.NotPanics(t, func() {
set.Del(s)
})

assert.NotPanics(t, func() {
assert.False(t, set.Has(s))
})

assert.NotPanics(t, func() {
assert.Equal(t, 0, set.Len())
})

assert.NotPanics(t, func() {
assert.Nil(t, set.Values())
})

assert.Panics(t, func() {
set.Add(s)
})
})

t.Run("non_nil", func(t *testing.T) {
set := NewSet()
assert.Equal(t, 0, set.Len())

ok := set.Has(s)
assert.False(t, ok)

set.Add(s)
ok = set.Has(s)
assert.True(t, ok)

assert.Equal(t, []string{s}, set.Values())

set.Del(s)
ok = set.Has(s)
assert.False(t, ok)

set = NewSet(s)
assert.Equal(t, 1, set.Len())
})
}
5 changes: 5 additions & 0 deletions internal/dhcpd/iprange.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,8 @@ func (r *ipRange) offset(ip net.IP) (offset uint64, ok bool) {
// construction.
return offsetInt.Uint64(), true
}

// String implements the fmt.Stringer interface for *ipRange.
func (r *ipRange) String() (s string) {
return fmt.Sprintf("%s-%s", r.start, r.end)
}
9 changes: 6 additions & 3 deletions internal/dhcpd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ type V4ServerConf struct {

leaseTime time.Duration // the time during which a dynamic lease is considered valid
dnsIPAddrs []net.IP // IPv4 addresses to return to DHCP clients as DNS server addresses
routerIP net.IP // value for Option Router
subnetMask net.IPMask // value for Option SubnetMask
options []dhcpOption

// subnet contains the DHCP server's subnet. The IP is the IP of the
// gateway.
subnet *net.IPNet

options []dhcpOption

// notify is a way to signal to other components that leases have
// change. notify must be called outside of locked sections, since the
Expand Down
Loading

0 comments on commit 20748f2

Please sign in to comment.