From f274433c461e7187f4d153944fbf2ef35ccdd8d2 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sat, 15 Jan 2011 12:24:09 +0100 Subject: [PATCH] Put string.go inside DNS package --- Makefile | 3 +-- keygen.go | 2 +- string.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 string.go diff --git a/Makefile b/Makefile index d8d15669..a61017dd 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ GOFILES=\ tsig.go\ dnssec.go\ keygen.go\ + string.go\ include $(GOROOT)/src/Make.pkg @@ -21,13 +22,11 @@ include $(GOROOT)/src/Make.pkg all: package gomake -C resolver package gomake -C responder package - gomake -C strconv package dnstest: gotest gomake -C resolver test gomake -C responder test -# gomake -C strconv test _examples: gomake -C _examples diff --git a/keygen.go b/keygen.go index f3fa2c77..779ad9a9 100644 --- a/keygen.go +++ b/keygen.go @@ -67,7 +67,7 @@ func (r *RR_DNSKEY) Generate(bits int) (PrivateKey, os.Error) { // of BIND9 (Private-key-format: v1.3). It needs some // info from the key (hashing, keytag), so its a method // of the RR_DNSKEY. -func (r *RR_DNSKEY) PrivateKeyToString(p PrivateKey) (s string) { +func (r *RR_DNSKEY) PrivateKeyString(p PrivateKey) (s string) { switch t := p.(type) { case *rsa.PrivateKey: algorithm := strconv.Itoa(int(r.Algorithm)) + " (" + alg_str[r.Algorithm] + ")" diff --git a/string.go b/string.go new file mode 100644 index 00000000..325c12b6 --- /dev/null +++ b/string.go @@ -0,0 +1,81 @@ +package dns + +import ( + "unicode" + "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, _ := strconv.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, _ := strconv.Atoi(num) + sec += uint32(i) + return +} + +func SecondsToString(val uint32) (str string) { + mod := val / w + if mod > 0 { + str += strconv.Itoa(int(mod)) + "w" + } + val -= mod * w + + mod = val / d + if mod > 0 { + str += strconv.Itoa(int(mod)) + "d" + } + val -= mod * d + + mod = val / h + if mod > 0 { + str += strconv.Itoa(int(mod)) + "h" + } + val -= mod * h + + mod = val / m + if mod > 0 { + str += strconv.Itoa(int(mod)) + "m" + } + val -= mod * m + + if val > 0 { + str += strconv.Itoa(int(val)) + } + return +} + +// Read a string and convert it to the correct +// Resource Record. +func SetString(s string) RR { + + k := new(RR_DNSKEY) + return k +}