Skip to content

Commit

Permalink
added tcp server
Browse files Browse the repository at this point in the history
  • Loading branch information
kwitsch committed Jan 9, 2022
1 parent d945f79 commit 6e4a28f
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type Server struct {
udp *dns.Server
tcp *dns.Server
cache *cache.Cache
ttl uint32
verbose bool
Expand All @@ -20,14 +21,14 @@ type Server struct {
func New(cache *cache.Cache, ttl int, verbose bool) *Server {
res := &Server{
udp: createUDPServer(),
tcp: createTCPServer(),
cache: cache,
ttl: uint32(ttl),
verbose: verbose,
Error: make(chan error, 1),
Error: make(chan error, 2),
}

handler := res.udp.Handler.(*dns.ServeMux)
handler.HandleFunc(".", res.OnRequest)
res.setupHandlers()

return res
}
Expand All @@ -36,12 +37,23 @@ func (s *Server) Start() {
go func() {
s.Error <- s.udp.ListenAndServe()
}()
go func() {
s.Error <- s.tcp.ListenAndServe()
}()
}

func (s *Server) Stop() {
s.udp.Shutdown()
}

func (s *Server) setupHandlers() {
uh := s.udp.Handler.(*dns.ServeMux)
uh.HandleFunc(".", s.OnRequest)

th := s.tcp.Handler.(*dns.ServeMux)
th.HandleFunc(".", s.OnRequest)
}

func createUDPServer() *dns.Server {
return &dns.Server{
Addr: ":53",
Expand All @@ -54,6 +66,17 @@ func createUDPServer() *dns.Server {
}
}

func createTCPServer() *dns.Server {
return &dns.Server{
Addr: ":53",
Net: "tcp",
Handler: dns.NewServeMux(),
NotifyStartedFunc: func() {
fmt.Println("TCP server is up and running")
},
}
}

const rdnsSuf string = ".in-addr.arpa"

func (s *Server) OnRequest(w dns.ResponseWriter, request *dns.Msg) {
Expand Down

0 comments on commit 6e4a28f

Please sign in to comment.