From 5b35abfbe1b5873ded05a230166f1e2648bd6ed0 Mon Sep 17 00:00:00 2001 From: Hugo Landau Date: Sat, 12 Nov 2016 11:44:14 +0000 Subject: [PATCH] Fix early port binding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ©! I, Hugo Landau , hereby licence these changes under the ©! licence with SHA256 hash ©! fd80a26fbb3f644af1fa994134446702932968519797227e07a1368dea80f0bc. --- Makefile | 21 ++++++------ main.go | 14 ++++---- server/server.go | 83 ++++++++++++++++++++++++++++++++++-------------- 3 files changed, 78 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 135e156..b197a4e 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,15 @@ PROJNAME=github.com/hlandau/ncdns BINARIES=$(PROJNAME) $(PROJNAME)/ncdt $(PROJNAME)/ncdumpzone ############################################################################### -# v1.9 NNSC:github.com/hlandau/degoutils/_stdenv/Makefile.ref +# v1.13 NNSC:github.com/hlandau/degoutils/_stdenv/Makefile.ref # This is a standard Makefile for building Go code designed to be copied into # other projects. Code below this line is not intended to be modified. +# +# NOTE: Use of this Makefile is not mandatory. People familiar with the use +# of the "go" command who have a GOPATH setup can use go get/go install. # XXX: prebuild-checks needs bash, fix this at some point -SHELL := /bin/bash +SHELL := $(shell which bash) -include Makefile.extra -include Makefile.assets @@ -35,13 +38,8 @@ ifeq ($(V),1) endif ## Buildinfo -BUILDNAME?=$(shell date -u "+%Y%m%d%H%M%S") on $(shell hostname -f) -BUILDINFO=$(shell (echo built $(BUILDNAME); GOPATH="$(GOPATH)"; go list -f '{{range $$imp := .Deps}}{{printf "%s\n" $$imp}}{{end}}' $(1) | sort -u | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | awk "{print \"$$GOPATH/src/\" \$$0}" | (while read line; do x="$$line"; while [ ! -e "$$x/.git" -a ! -e "$$x/.hg" ]; do x=$${x%/*}; if [ "$$x" = "" ]; then break; fi; done; echo "$$x"; done) | sort -u | (while read line; do echo git $${line\#$$GOPATH/src/} $$(git -C "$$line" rev-parse HEAD) $$(git -C "$$line" describe --all --dirty=+ --abbrev=99 --always); done)) | base64 -w 0 | tr -d '=') -VPFX=$(shell if [ -e "$(GOPATH)/src/$(PROJNAME)/vendor" ]; then echo "$(PROJNAME)/vendor/"; fi) -BUILDINFO_FLAG= - ifeq ($(USE_BUILDINFO),1) - BUILDINFO_FLAG= -ldflags "-X $(VPFX)github.com/hlandau/degoutils/buildinfo.RawBuildInfo=$(call BUILDINFO,$(1))" + BUILDINFO_FLAG=-ldflags "$$($$GOPATH/src/github.com/hlandau/buildinfo/gen $(1))" endif ## Standard Rules @@ -67,7 +65,7 @@ prebuild-checks: $(DIRS): | .gotten $(call QI,DIRS)mkdir -p $(GOPATH)/src $(GOBIN); \ - if [ ! -e "src" ]; then \ + if [ ! -e "src" ]; then \ ln -s $(GOPATH)/src src; \ fi; \ if [ ! -e "bin" ]; then \ @@ -75,7 +73,7 @@ $(DIRS): | .gotten fi .gotten: - $(call QI,GO-GET,$(PROJNAME))go get $(BINARIES) + $(call QI,GO-GET,$(PROJNAME))go get $(PROJNAME)/... $(Q)touch .gotten .NOTPARALLEL: prebuild-checks $(DIRS) @@ -88,3 +86,6 @@ install: all $(call QI,INSTALL,$(BINARIES))for x in $(BINARIES); do \ install -Dp $(GOBIN)/`basename "$$x"` $(DESTDIR)$(PREFIX)/bin; \ done + +update: | .gotten + $(call QI,GO-GET,$(PROJNAME))go get -u $(PROJNAME)/... diff --git a/main.go b/main.go index b05f6a2..0386e43 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main -import "gopkg.in/hlandau/service.v2" -import "gopkg.in/hlandau/easyconfig.v1" -import "github.com/hlandau/ncdns/server" -import "github.com/hlandau/degoutils/xlogconfig" -import "path/filepath" +import ( + "github.com/hlandau/dexlogconfig" + "github.com/hlandau/ncdns/server" + "gopkg.in/hlandau/easyconfig.v1" + "gopkg.in/hlandau/service.v2" + "path/filepath" +) func main() { cfg := server.Config{} @@ -13,7 +15,7 @@ func main() { ProgramName: "ncdns", } config.ParseFatal(&cfg) - xlogconfig.Init() + dexlogconfig.Init() // We use the configPath to resolve paths relative to the config file. cfg.ConfigDir = filepath.Dir(config.ConfigFilePath()) diff --git a/server/server.go b/server/server.go index f9b2d96..5e5d684 100644 --- a/server/server.go +++ b/server/server.go @@ -1,19 +1,20 @@ package server -import "gopkg.in/hlandau/madns.v1" -import "github.com/hlandau/ncdns/backend" -import "github.com/hlandau/ncdns/namecoin" -import "github.com/miekg/dns" -import "os" -import "net" -import "fmt" -import "sync" -import "strings" -import "path/filepath" -import "crypto" -import "github.com/hlandau/xlog" - -const version = "1.0" +import ( + "crypto" + "fmt" + "github.com/hlandau/buildinfo" + "github.com/hlandau/ncdns/backend" + "github.com/hlandau/ncdns/namecoin" + "github.com/hlandau/xlog" + "github.com/miekg/dns" + "gopkg.in/hlandau/madns.v1" + "net" + "os" + "path/filepath" + "strings" + "sync" +) var log, Log = xlog.New("ncdns.server") @@ -24,8 +25,10 @@ type Server struct { namecoinConn namecoin.Conn mux *dns.ServeMux - udpListener *dns.Server - tcpListener *dns.Server + udpServer *dns.Server + udpConn *net.UDPConn + tcpServer *dns.Server + tcpListener net.Listener wgStart sync.WaitGroup } @@ -61,7 +64,11 @@ func (cfg *Config) cpath(s string) string { return filepath.Join(cfg.ConfigDir, s) } +var ncdnsVersion string + func New(cfg *Config) (s *Server, err error) { + ncdnsVersion = buildinfo.VersionSummary("github.com/hlandau/ncdns", "ncdns") + s = &Server{ cfg: *cfg, namecoinConn: namecoin.Conn{ @@ -101,7 +108,7 @@ func New(cfg *Config) (s *Server, err error) { ecfg := &madns.EngineConfig{ Backend: b, - VersionString: "ncdns/" + version, + VersionString: ncdnsVersion, } // key setup @@ -128,6 +135,29 @@ func New(cfg *Config) (s *Server, err error) { return } + s.mux = dns.NewServeMux() + s.mux.Handle(".", s.engine) + + tcpAddr, err := net.ResolveTCPAddr("tcp", s.cfg.Bind) + if err != nil { + return + } + + s.tcpListener, err = net.ListenTCP("tcp", tcpAddr) + if err != nil { + return + } + + udpAddr, err := net.ResolveUDPAddr("udp", s.cfg.Bind) + if err != nil { + return + } + + s.udpConn, err = net.ListenUDP("udp", udpAddr) + if err != nil { + return + } + if cfg.HTTPListenAddr != "" { err = webStart(cfg.HTTPListenAddr, s) if err != nil { @@ -170,20 +200,16 @@ func (s *Server) loadKey(fn, privateFn string) (k *dns.DNSKEY, privatek crypto.P } func (s *Server) Start() error { - s.mux = dns.NewServeMux() - s.mux.Handle(".", s.engine) - s.wgStart.Add(2) - s.udpListener = s.runListener("udp") - s.tcpListener = s.runListener("tcp") + s.udpServer = s.runListener("udp") + s.tcpServer = s.runListener("tcp") s.wgStart.Wait() - log.Info("Listeners started") return nil } func (s *Server) doRunListener(ds *dns.Server) { - err := ds.ListenAndServe() + err := ds.ActivateAndServe() log.Fatale(err) } @@ -196,6 +222,15 @@ func (s *Server) runListener(net string) *dns.Server { s.wgStart.Done() }, } + switch net { + case "tcp": + ds.Listener = s.tcpListener + case "udp": + ds.PacketConn = s.udpConn + default: + panic("unreachable") + } + go s.doRunListener(ds) return ds }