Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: go
go:
- 1.13.x
before_install:
- sudo apt-get -y install python3-pip python3-setuptools
- pip3 install scapy
- go get -u github.com/mattn/goveralls
- go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
script:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export GOPRIVATE := code.cfops.it
IMPORT_PATH := github.com/majek/slirpnetstack
GOFLAGS=-ldflags=-compressdwarf=false
GOFLAGS=-ldflags=-compressdwarf=false -gcflags=all="-N -l"

bin/slirpnetstack: *.go go.mod
go build \
Expand Down
119 changes: 119 additions & 0 deletions dhcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package main

import (
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"

"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv4/server4"
"github.com/sirupsen/logrus"

"github.com/coredhcp/coredhcp"
"github.com/coredhcp/coredhcp/config"
"github.com/coredhcp/coredhcp/handler"
"github.com/coredhcp/coredhcp/logger"

"github.com/coredhcp/coredhcp/plugins"
"github.com/coredhcp/coredhcp/plugins/dns"
"github.com/coredhcp/coredhcp/plugins/nbp"
rangepl "github.com/coredhcp/coredhcp/plugins/range"
"github.com/coredhcp/coredhcp/plugins/router"
"github.com/coredhcp/coredhcp/plugins/serverid"
)

var bootfilePlugin = plugins.Plugin{
Name: "slirp.bootfile",
Setup4: bootfile_setup4,
}

var desiredPlugins = []*plugins.Plugin{
&dns.Plugin,
&rangepl.Plugin,
&router.Plugin,
&serverid.Plugin,
&nbp.Plugin,
&bootfilePlugin,
}

func bootfile_setup4(args ...string) (handler.Handler4, error) {
h := func(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
resp.BootFileName = args[0]
return resp, false
}
return h, nil
}

func setupDHCP(s *stack.Stack, state *State) error {
log := logger.GetLogger("plugins")
log.Logger.SetLevel(logrus.WarnLevel)
for _, plugin := range desiredPlugins {
if e := plugins.RegisterPlugin(plugin); e != nil {
return e
}
}

conf := config.New()
plugins := make([]*config.PluginConfig, 0)
plugins = append(plugins,
&config.PluginConfig{
Name: "range",
Args: []string{"/dev/null",
state.DHCPStart.String(), state.DHCPEnd.String(),
"24h"},
},
&config.PluginConfig{
Name: "dns",
Args: state.DHCPDns.servers,
},
&config.PluginConfig{
Name: "server_id",
Args: []string{state.Host.String()},
})
if !state.Restricted {
plugins = append(plugins,
&config.PluginConfig{
Name: "router",
Args: []string{state.Host.String()},
},
)
}
if state.DHCPNbp != "" {
plugins = append(plugins,
&config.PluginConfig{
Name: "nbp",
Args: []string{state.DHCPNbp},
},
)
}
if state.DHCPBootfile != "" {
plugins = append(plugins,
&config.PluginConfig{
Name: "slirp.bootfile",
Args: []string{state.DHCPBootfile},
},
)
}
conf.Server4 = &config.ServerConfig{
Plugins: plugins,
}

server := coredhcp.NewServer(conf)
if _, _, e := server.LoadPlugins(server.Config); e != nil {
return e
}

// no IP, this will catch broadcasted packets
addr := tcpip.FullAddress{1, "", dhcpv4.ServerPort}
if conn, e := gonet.DialUDP(s, &addr, nil, ipv4.ProtocolNumber); e != nil {
return e
} else if server4, e := server4.NewServer("", nil, server.MainHandler4, server4.WithConn(conn)); e != nil {
return e
} else {
server.Server4 = server4
}
go server.Server4.Serve()

return nil
}
50 changes: 50 additions & 0 deletions dnsconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"bufio"
"net"
"os"
"strings"
)

type dnsConfig struct {
servers []string // server addresses
err error // any error that occurs during open of resolv.conf
unknownOpt bool // anything unknown was encountered
}

// See resolv.conf(5) on a Linux machine.
func dnsReadConfig(filename string) *dnsConfig {
conf := &dnsConfig{}

file, err := os.Open(filename)
if err != nil {
conf.err = err
return conf
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if len(line) > 0 && (line[0] == ';' || line[0] == '#') {
// comment.
continue
}
f := strings.Fields(line)
if len(f) < 1 {
continue
}
switch f[0] {
case "nameserver":
if len(f) > 1 && len(conf.servers) < 3 { // small, but the standard limit
if net.ParseIP(f[1]) != nil {
conf.servers = append(conf.servers, f[1])
}
}
default:
conf.unknownOpt = true
}
}
return conf
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ go 1.13

require (
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/coredhcp/coredhcp v0.0.0-20200209180252-349f5927f59c
github.com/golang/protobuf v1.3.3 // indirect
github.com/insomniacslk/dhcp v0.0.0-20200210095418-45e5f320b2f0
github.com/opencontainers/runtime-spec v0.1.2-0.20171211145439-b2d941ef6a78
github.com/pin/tftp v2.1.0+incompatible
github.com/sirupsen/logrus v1.4.2
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad // indirect
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
Expand Down
Loading