diff --git a/Makefile b/Makefile index 638c3b29..c1a8298f 100644 --- a/Makefile +++ b/Makefile @@ -21,11 +21,13 @@ all: package install: $(INSTALLFILES) gomake -C dnssec install gomake -C resolver install +# gomake -C strconv install dnstest: gotest gomake -C dnssec test gomake -C resolver test +# gomake -C strconv test _examples: gomake -C _examples diff --git a/TODO b/TODO index 9d5b7209..f2766fd2 100644 --- a/TODO +++ b/TODO @@ -6,6 +6,8 @@ Todo: * IDN? * Unknown RRs? * query-time, server in string ouput of dns.Msg +* Parsing from strings +* Server support (signing, receiving queries) Issues: * Better sized buffers diff --git a/_examples/q/q.go b/_examples/q/q.go index fb3b3a81..fc650e88 100644 --- a/_examples/q/q.go +++ b/_examples/q/q.go @@ -11,7 +11,7 @@ import ( ) func main() { - var dnssec *bool = flag.Bool("dnssec", false, "Set DO flag; set bufsize to 4096") + var dnssec *bool = flag.Bool("dnssec", false, "Request DNSSEC records") var port *string = flag.String("port", "53", "Set the query port") var aa *bool = flag.Bool("aa", false, "Set AA flag in query") var ad *bool = flag.Bool("ad", false, "Set AD flag in query") diff --git a/strconv/6.out b/strconv/6.out new file mode 100755 index 00000000..b5b28792 Binary files /dev/null and b/strconv/6.out differ diff --git a/strconv/Makefile b/strconv/Makefile new file mode 100644 index 00000000..c897d521 --- /dev/null +++ b/strconv/Makefile @@ -0,0 +1,11 @@ +# Copyright 2009 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +include $(GOROOT)/src/Make.inc + +TARG=dns/strconv +GOFILES=\ + strconv.go\ + +include $(GOROOT)/src/Make.pkg diff --git a/strconv/_go_.6 b/strconv/_go_.6 new file mode 100644 index 00000000..04f16510 Binary files /dev/null and b/strconv/_go_.6 differ diff --git a/strconv/_gotest_.6 b/strconv/_gotest_.6 new file mode 100644 index 00000000..d47f86ce Binary files /dev/null and b/strconv/_gotest_.6 differ diff --git a/strconv/_obj/dns/strconv.a b/strconv/_obj/dns/strconv.a new file mode 100644 index 00000000..9d09c3de Binary files /dev/null and b/strconv/_obj/dns/strconv.a differ diff --git a/strconv/_test/dns/strconv.a b/strconv/_test/dns/strconv.a new file mode 100644 index 00000000..34dfab1f Binary files /dev/null and b/strconv/_test/dns/strconv.a differ diff --git a/strconv/_testmain.6 b/strconv/_testmain.6 new file mode 100644 index 00000000..658d6c9d Binary files /dev/null and b/strconv/_testmain.6 differ diff --git a/strconv/_testmain.go b/strconv/_testmain.go new file mode 100644 index 00000000..c7c6fa4c --- /dev/null +++ b/strconv/_testmain.go @@ -0,0 +1,15 @@ +package main + +import "dns/strconv" +import "testing" +import __regexp__ "regexp" + +var tests = []testing.InternalTest{ + {"strconv.TestConversion", strconv.TestConversion}, +} +var benchmarks = []testing.InternalBenchmark{} + +func main() { + testing.Main(__regexp__.MatchString, tests) + testing.RunBenchmarks(__regexp__.MatchString, benchmarks) +} diff --git a/strconv/strconv.go b/strconv/strconv.go new file mode 100644 index 00000000..630d67ac --- /dev/null +++ b/strconv/strconv.go @@ -0,0 +1,73 @@ +package strconv + +import ( + "unicode" + conv "strconv" +) + +const ( + m = 60 + h = m * m + d = 24 * h + w = 7 * d +) + +// Convert a Ttl to a value. Supported value 'm' for minutes, 'h' for hours +// 'w' for week and 'd' for days. Stuff like '1d1d' is legal and return the value of '2d' +func StringToSeconds(ttl string) (sec uint32, ok bool) { + num := "" + for _, k := range ttl { + if unicode.IsDigit(k) { + num += string(k) + } else { + i, _ := conv.Atoi(num) + switch k { + case 'm': + sec += uint32(i) * m + case 'h': + sec += uint32(i) * h + case 'd': + sec += uint32(i) * d + case 'w': + sec += uint32(i) * w + default: + return + } + num = "" + } + } + i, _ := conv.Atoi(num) + sec += uint32(i) + return +} + +func SecondsToString(val uint32) (str string) { + mod := val / w + if mod > 0 { + str += conv.Itoa(int(mod)) + "w" + } + val -= mod * w + + mod = val / d + if mod > 0 { + str += conv.Itoa(int(mod)) + "d" + } + val -= mod * d + + mod = val / h + if mod > 0 { + str += conv.Itoa(int(mod)) + "h" + } + val -= mod * h + + mod = val / m + if mod > 0 { + str += conv.Itoa(int(mod)) + "m" + } + val -= mod * m + + if val > 0 { + str += conv.Itoa(int(val)) + } + return +} diff --git a/strconv/ttl_test.go b/strconv/ttl_test.go new file mode 100644 index 00000000..821c8731 --- /dev/null +++ b/strconv/ttl_test.go @@ -0,0 +1,24 @@ +package strconv + +import ( "testing") + +func TestConversion(t *testing.T) { +/* + println(StringToSeconds("6w8d50")) + println(StringToSeconds("50")) + println(StringToSeconds("1m1m")) + println(StringToSeconds("1w")) + println(StringToSeconds("1d")) + println(StringToSeconds("2d")) + println(StringToSeconds("1d1d")) +*/ + + println(SecondsToString(604800)) // 1w + println(SecondsToString(604799)) // 1w-1 + println(SecondsToString(86400)) // 1d + println(SecondsToString(86401)) // 1d+1 + println(SecondsToString(86399)) // 1d-1 + println(SecondsToString(86)) // 1m26 + println(SecondsToString(60)) // 1m + println(SecondsToString(59)) // 59 +}