From 7630cd4e7deb81becda4fac00711ff9d085c17d3 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 1 May 2023 18:50:45 +0000 Subject: [PATCH] checkpoint going to rewrite this in C++ I think --- cmd/kdhcpd/main.go | 2 ++ go.mod | 10 ++++++-- go.sum | 12 +++++++++ log/BUILD.bazel | 2 +- server/BUILD.bazel | 6 +---- server/addr.go | 61 ---------------------------------------------- server/packet.go | 8 +++++- server/server.go | 13 +++++++--- 8 files changed, 40 insertions(+), 74 deletions(-) delete mode 100644 server/addr.go diff --git a/cmd/kdhcpd/main.go b/cmd/kdhcpd/main.go index 80b1ed6..84c32cc 100644 --- a/cmd/kdhcpd/main.go +++ b/cmd/kdhcpd/main.go @@ -5,6 +5,7 @@ import ( "git.wntrmute.dev/kyle/kdhcp/log" "git.wntrmute.dev/kyle/kdhcp/server" + "github.com/davecgh/go-spew/spew" ) func main() { @@ -30,5 +31,6 @@ func main() { } log.Debugf("receive %d byte packet from %s", len(packet.Data), packet.Addr) + spew.Dump(packet.Data) } } diff --git a/go.mod b/go.mod index 9f9d3d1..cef7529 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,13 @@ module git.wntrmute.dev/kyle/kdhcp go 1.20 +require github.com/hashicorp/go-syslog v1.0.0 + require ( - github.com/davecgh/go-spew v1.1.1 - github.com/hashicorp/go-syslog v1.0.0 + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/insomniacslk/dhcp v0.0.0-20230407062729-974c6f05fe16 // indirect + github.com/josharian/native v1.1.0 // indirect + github.com/pierrec/lz4/v4 v4.1.14 // indirect + github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 // indirect + golang.org/x/sys v0.5.0 // indirect ) diff --git a/go.sum b/go.sum index 1332d1a..1e30595 100644 --- a/go.sum +++ b/go.sum @@ -2,3 +2,15 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/hashicorp/go-syslog v1.0.0 h1:KaodqZuhUoZereWVIYmpUgZysurB1kBLX2j0MwMrUAE= github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= +github.com/insomniacslk/dhcp v0.0.0-20230407062729-974c6f05fe16 h1:+aAGyK41KRn8jbF2Q7PLL0Sxwg6dShGcQSeCC7nZQ8E= +github.com/insomniacslk/dhcp v0.0.0-20230407062729-974c6f05fe16/go.mod h1:IKrnDWs3/Mqq5n0lI+RxA2sB7MvN/vbMBP3ehXg65UI= +github.com/josharian/native v1.0.1-0.20221213033349-c1e37c09b531/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= +github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA= +github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= +github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE= +github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= +github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 h1:tHNk7XK9GkmKUR6Gh8gVBKXc2MVSZ4G/NnWLtzw4gNA= +github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923/go.mod h1:eLL9Nub3yfAho7qB0MzZizFhTU2QkLeoVsWdHtDW264= +golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/log/BUILD.bazel b/log/BUILD.bazel index c9fceed..a73a852 100644 --- a/log/BUILD.bazel +++ b/log/BUILD.bazel @@ -5,5 +5,5 @@ go_library( srcs = ["logger.go"], importpath = "git.wntrmute.dev/kyle/kdhcp/log", visibility = ["//visibility:public"], - deps = ["//bazel-kdhcp/external/com_github_hashicorp_go_syslog:go-syslog"], + deps = ["@com_github_hashicorp_go_syslog//:go-syslog"], ) diff --git a/server/BUILD.bazel b/server/BUILD.bazel index f69a764..7cf6ab0 100644 --- a/server/BUILD.bazel +++ b/server/BUILD.bazel @@ -3,15 +3,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "server", srcs = [ - "addr.go", "config.go", "packet.go", "server.go", ], importpath = "git.wntrmute.dev/kyle/kdhcp/server", visibility = ["//visibility:public"], - deps = [ - "//bazel-kdhcp/external/com_github_davecgh_go_spew/spew", - "//log", - ], + deps = ["//log"], ) diff --git a/server/addr.go b/server/addr.go deleted file mode 100644 index c623f4c..0000000 --- a/server/addr.go +++ /dev/null @@ -1,61 +0,0 @@ -package server - -import ( - "fmt" - "net" - - "git.wntrmute.dev/kyle/kdhcp/bazel-kdhcp/external/com_github_davecgh_go_spew/spew" - "git.wntrmute.dev/kyle/kdhcp/log" -) - -type addr struct { - ifi *net.Interface - IP net.IP - ipn *net.IPNet -} - -func addrsForDevice(dev string) ([]addr, error) { - netInterface, err := net.InterfaceByName(dev) - if err != nil { - return nil, fmt.Errorf("while selecting interface %s: %w", dev, err) - } - - spew.Dump(netInterface) - - var addrs []addr - devAddrs, err := netInterface.Addrs() - if err != nil { - return nil, err - } - - for _, devAddr := range devAddrs { - log.Debugf("consider %s", devAddr.String()) - ip, ipn, err := net.ParseCIDR(devAddr.String()) - if err != nil { - continue - } - - if ip == nil { - continue // address isn't an IP address - } - - log.Debugf("found IP: %s", ip) - ip = ip.To4() - - // DHCP should only listen on private addresses. - if !ip.IsPrivate() { - log.Debugln("skipping non-private") - continue - } - - // only support IPv4 for now - if len(ip) != 4 { - log.Debugf("%d IP, only supporting v4 right now", len(ip)) - continue - } - - addrs = append(addrs, addr{netInterface, ip, ipn}) - } - - return addrs, nil -} diff --git a/server/packet.go b/server/packet.go index 20f2604..ad5451b 100644 --- a/server/packet.go +++ b/server/packet.go @@ -1,8 +1,14 @@ package server -import "net" +import ( + "net" + + "github.com/insomniacslk/dhcp/dhcpv4" +) type Packet struct { Data []byte Addr net.Addr + + Raw *dhcpv4.DHCPv4 } diff --git a/server/server.go b/server/server.go index 2e259ea..dcd2503 100644 --- a/server/server.go +++ b/server/server.go @@ -5,10 +5,9 @@ import ( "net" "git.wntrmute.dev/kyle/kdhcp/log" + "github.com/insomniacslk/dhcp/dhcpv4" ) -// github.com/insomniacslk/dhcp - type Server struct { cfg *Config conn net.PacketConn @@ -42,10 +41,16 @@ func (srv *Server) ReadFrom() (*Packet, error) { return nil, err } - return &Packet{ + packet := &Packet{ Data: b[:n], Addr: addr, - }, nil + } + packet.Raw, err = dhcpv4.FromBytes(b[:n]) + if err != nil { + return packet, err + } + + return packet, nil } func NewServer(cfg *Config) (*Server, error) {