From 0325eb8536c8deecb3375e2464556434d1b6e283 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Sat, 29 Apr 2023 23:46:44 -0700 Subject: [PATCH] bazel stuff --- BUILD.bazel | 2 +- WORKSPACE.bazel => WORKSPACE | 3 +++ cmd/kdhcpd/BUILD.bazel | 15 +++++++++++++++ deps.bzl | 9 +++++++++ gazelle.sh | 2 +- go.mod | 5 +++++ go.sum | 4 ++++ server/BUILD.bazel | 9 +++++++++ server/server.go | 30 +++++++++++++++++++++++++----- 9 files changed, 72 insertions(+), 7 deletions(-) rename WORKSPACE.bazel => WORKSPACE (99%) create mode 100644 cmd/kdhcpd/BUILD.bazel create mode 100644 deps.bzl mode change 100644 => 100755 gazelle.sh create mode 100644 go.sum create mode 100644 server/BUILD.bazel diff --git a/BUILD.bazel b/BUILD.bazel index b369316..cf529bf 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,4 +1,4 @@ load("@bazel_gazelle//:def.bzl", "gazelle") -# gazelle:prefix git.wntrmute.dev/kyle/bladerunner +# gazelle:prefix git.wntrmute.dev/kyle/kdhcp gazelle(name = "gazelle") diff --git a/WORKSPACE.bazel b/WORKSPACE similarity index 99% rename from WORKSPACE.bazel rename to WORKSPACE index fc54605..1d1a8bf 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE @@ -31,6 +31,9 @@ load("//:deps.bzl", "go_dependencies") # gazelle:repository_macro deps.bzl%go_dependencies go_dependencies() + go_rules_dependencies() + go_register_toolchains(version = "1.20.3") + gazelle_dependencies() diff --git a/cmd/kdhcpd/BUILD.bazel b/cmd/kdhcpd/BUILD.bazel new file mode 100644 index 0000000..f300b9a --- /dev/null +++ b/cmd/kdhcpd/BUILD.bazel @@ -0,0 +1,15 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") + +go_library( + name = "kdhcpd_lib", + srcs = ["main.go"], + importpath = "git.wntrmute.dev/kyle/kdhcp/cmd/kdhcpd", + visibility = ["//visibility:private"], + deps = ["//server"], +) + +go_binary( + name = "kdhcpd", + embed = [":kdhcpd_lib"], + visibility = ["//visibility:public"], +) diff --git a/deps.bzl b/deps.bzl new file mode 100644 index 0000000..7163a95 --- /dev/null +++ b/deps.bzl @@ -0,0 +1,9 @@ +load("@bazel_gazelle//:deps.bzl", "go_repository") + +def go_dependencies(): + go_repository( + name = "com_github_davecgh_go_spew", + importpath = "github.com/davecgh/go-spew", + sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=", + version = "v1.1.1", + ) diff --git a/gazelle.sh b/gazelle.sh old mode 100644 new mode 100755 index 1ee87ce..5b14453 --- a/gazelle.sh +++ b/gazelle.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -euxo pipefail diff --git a/go.mod b/go.mod index 65692ca..9f9d3d1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module git.wntrmute.dev/kyle/kdhcp go 1.20 + +require ( + github.com/davecgh/go-spew v1.1.1 + github.com/hashicorp/go-syslog v1.0.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1332d1a --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +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= diff --git a/server/BUILD.bazel b/server/BUILD.bazel new file mode 100644 index 0000000..78cc2a4 --- /dev/null +++ b/server/BUILD.bazel @@ -0,0 +1,9 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "server", + srcs = ["server.go"], + importpath = "git.wntrmute.dev/kyle/kdhcp/server", + visibility = ["//visibility:public"], + deps = ["@com_github_davecgh_go_spew//spew"], +) diff --git a/server/server.go b/server/server.go index 2c7246a..87bddf8 100644 --- a/server/server.go +++ b/server/server.go @@ -1,13 +1,21 @@ package server + import ( "errors" "log" "net" + + "github.com/davecgh/go-spew/spew" ) // github.com/insomniacslk/dhcp +type addr struct { + IP net.IP + ipn *net.IPNet +} + type Config struct { Device string `yaml:"device"` } @@ -17,35 +25,47 @@ type Server struct { l net.Listener } -func addrsForDevice(dev string) ([]net.IP, error) { +func addrsForDevice(dev string) ([]addr, error) { netInterface, err := net.InterfaceByName(dev) if err != nil { return nil, err } - var addrs []net.IP + spew.Dump(netInterface) + + var addrs []addr devAddrs, err := netInterface.Addrs() if err != nil { return nil, err } - for _, addr := range devAddrs { - ip := net.ParseIP(addr.String()) + for _, devAddr := range devAddrs { + log.Printf("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.Printf("found IP: %s", ip) + ip = ip.To4() + // DHCP should only listen on private addresses. if !ip.IsPrivate() { + log.Println("skipping non-private") continue } // only support IPv4 for now if len(ip) != 4 { + log.Printf("%d IP, only supporting v4 right now", len(ip)) continue } - addrs = append(addrs, ip) + addrs = append(addrs, addr{ip, ipn}) } return addrs, nil