bazel stuff

This commit is contained in:
Kyle Isom 2023-04-29 23:46:44 -07:00
parent f3fc03392a
commit 0325eb8536
9 changed files with 72 additions and 7 deletions

View File

@ -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")

View File

@ -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()

15
cmd/kdhcpd/BUILD.bazel Normal file
View File

@ -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"],
)

9
deps.bzl Normal file
View File

@ -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",
)

2
gazelle.sh Normal file → Executable file
View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
set -euxo pipefail

5
go.mod
View File

@ -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
)

4
go.sum Normal file
View File

@ -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=

9
server/BUILD.bazel Normal file
View File

@ -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"],
)

View File

@ -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