parent
9eb32a3174
commit
7630cd4e7d
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
10
go.mod
10
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
|
||||
)
|
||||
|
|
12
go.sum
12
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=
|
||||
|
|
|
@ -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"],
|
||||
)
|
||||
|
|
|
@ -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"],
|
||||
)
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue