From 73ea58378a8640937195e936d65d834dfb79b1b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20B=C3=B6hm?= Date: Tue, 15 Jul 2014 23:26:50 +0200 Subject: [PATCH] Refactored Backend --- backend.go | 73 ++++++++++++++++++++++++++++++------------------------ ddns.go | 2 +- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/backend.go b/backend.go index dcc9f0b..8e91a2b 100644 --- a/backend.go +++ b/backend.go @@ -8,47 +8,54 @@ import ( "strings" ) +// This function implements the PowerDNS-Pipe-Backend protocol and generates +// the response data it possible func RunBackend(conn *connection.RedisConnection) { + // handshake with PowerDNS fmt.Printf("OK\tDDNS Go Backend\n") bio := bufio.NewReader(os.Stdin) for { line, _, err := bio.ReadLine() + HandleErr(err) - - parts := strings.Split(string(line), "\t") - if len(parts) == 6 { - query_name := parts[1] - - // get the host part of the fqdn - // pi.d.example.org -> pi - hostname := "" - if strings.HasSuffix(query_name, DdnsDomain) { - hostname = query_name[:len(query_name)-len(DdnsDomain)] - } - - query_class := parts[2] - // query_type := parts[3] - query_id := parts[4] - - if hostname != "" { - // check for existance and create response - if conn.HostExist(hostname) { - host := conn.GetHost(hostname) - - record := "A" - if !host.IsIPv4() { - record = "AAAA" - } - - fmt.Printf("DATA\t%s\t%s\t%s\t10\t%s\t%s\n", - query_name, query_class, record, query_id, host.Ip) - } - } - } - - fmt.Printf("END\n") + HandleRequest(string(line), conn) } } + +func HandleRequest(line string, conn *connection.RedisConnection) { + defer fmt.Printf("END\n") + + parts := strings.Split(line, "\t") + if len(parts) != 6 { + return + } + + query_name := parts[1] + query_class := parts[2] + // query_type := parts[3] // TODO Handle SOA Requests + query_id := parts[4] + + // get the host part of the fqdn + // pi.d.example.org -> pi + hostname := "" + if strings.HasSuffix(query_name, DdnsDomain) { + hostname = query_name[:len(query_name)-len(DdnsDomain)] + } + + if hostname == "" || ! conn.HostExist(hostname) { + return + } + + host := conn.GetHost(hostname) + + record := "A" + if !host.IsIPv4() { + record = "AAAA" + } + + fmt.Printf("DATA\t%s\t%s\t%s\t10\t%s\t%s\n", + query_name, query_class, record, query_id, host.Ip) +} diff --git a/ddns.go b/ddns.go index 57246f9..ec0173c 100644 --- a/ddns.go +++ b/ddns.go @@ -44,7 +44,7 @@ func PrepareForExecution() string { } func main() { - cmd = PrepareForExecution() + cmd := PrepareForExecution() conn := connection.OpenConnection() defer conn.Close()