Fix early port binding

©! I, Hugo Landau <hlandau@devever.net>, hereby licence these changes under the
©! licence with SHA256 hash
©! fd80a26fbb3f644af1fa994134446702932968519797227e07a1368dea80f0bc.
pull/18/head
Hugo Landau 8 years ago
parent a1612ebdb5
commit 5b35abfbe1

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

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

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

Loading…
Cancel
Save