mirror of https://github.com/namecoin/ncdns
refactoring
parent
13d64f59a7
commit
3f88db348f
@ -0,0 +1,16 @@
|
||||
package abstract
|
||||
import "github.com/miekg/dns"
|
||||
|
||||
type Backend interface {
|
||||
// Lookup all resource records having a given fully-qualified owner name,
|
||||
// regardless of type or class. Returns a slice of all those resource records
|
||||
// or an error.
|
||||
//
|
||||
// The returned slice may contain both authoritative and non-authoritative records
|
||||
// (for example, NS records for delegations and glue records.)
|
||||
//
|
||||
// The existence of wildcard records will be determined by doing a lookup for a name
|
||||
// like "*.example.com", so there is no need to process the wildcard logic other than
|
||||
// to make sure such a lookup functions correctly.
|
||||
Lookup(qname string) (rrs []dns.RR, err error)
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
// This package contains extensions to btcjson used by the namecoin package.
|
||||
// It is not intended for public use.
|
||||
package extratypes
|
||||
|
||||
import "github.com/hlandauf/btcjson"
|
||||
import "encoding/json"
|
||||
|
||||
type NameShowCmd struct {
|
||||
id interface{}
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func NewNameShowCmd(id interface{}, name string) (*NameShowCmd, error) {
|
||||
return &NameShowCmd {
|
||||
id: id,
|
||||
Name: name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *NameShowCmd) Id() interface{} {
|
||||
return c.id
|
||||
}
|
||||
|
||||
func (c *NameShowCmd) Method() string {
|
||||
return "name_show"
|
||||
}
|
||||
|
||||
func (c *NameShowCmd) MarshalJSON() ([]byte, error) {
|
||||
params := []interface{}{
|
||||
c.Name,
|
||||
}
|
||||
|
||||
raw, err := btcjson.NewRawCmd(c.id, c.Method(), params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json.Marshal(raw)
|
||||
}
|
||||
|
||||
func (c *NameShowCmd) UnmarshalJSON(b []byte) error {
|
||||
// We don't need to implement this as we are only ever the client.
|
||||
panic("not implemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
type NameShowReply struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
func replyParser(m json.RawMessage) (interface{}, error) {
|
||||
nsr := &NameShowReply{}
|
||||
err := json.Unmarshal(m, nsr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nsr, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
btcjson.RegisterCustomCmd("name_show", nil, replyParser, "name_show <name>")
|
||||
}
|
@ -1,35 +1,57 @@
|
||||
// Error types for processing DNS requests.
|
||||
package ncerr
|
||||
|
||||
import "github.com/miekg/dns"
|
||||
import "fmt"
|
||||
|
||||
// An Error interface which allows an associated rcode to be queried.
|
||||
type Error interface {
|
||||
error
|
||||
|
||||
// Returns the rcode which this error should be represented as in the DNS protocol.
|
||||
Rcode() int
|
||||
}
|
||||
|
||||
type Rerr struct {
|
||||
type rerr struct {
|
||||
error
|
||||
e error
|
||||
rcode int
|
||||
}
|
||||
|
||||
func (re *Rerr) Error() string {
|
||||
func (re *rerr) Error() string {
|
||||
return re.e.Error()
|
||||
}
|
||||
|
||||
func (re *Rerr) Rcode() int {
|
||||
func (re *rerr) Rcode() int {
|
||||
return re.rcode
|
||||
}
|
||||
|
||||
func rerrorf(rcode int, fmts string, args ...interface{}) Error {
|
||||
re := &Rerr{}
|
||||
// Used to generate an Error which has a particular rcode. Otherwise like fmt.Errorf.
|
||||
func Rerrorf(rcode int, fmts string, args ...interface{}) Error {
|
||||
re := &rerr{}
|
||||
re.e = fmt.Errorf(fmts, args...)
|
||||
re.rcode = rcode
|
||||
return re
|
||||
}
|
||||
|
||||
// Standard errors.
|
||||
var ErrNoSuchDomain = rerrorf(dns.RcodeNameError, "no such domain")
|
||||
var ErrNotInZone = rerrorf(dns.RcodeRefused, "domain not in zone")
|
||||
var ErrNoResults = rerrorf(0, "no results")
|
||||
|
||||
// Represents NXDOMAIN. Used when the name requested lies within a zone for
|
||||
// which this server is authoritative, but does not exist.
|
||||
//
|
||||
// Note that a name is considered to exist if there exist any records of any
|
||||
// type at a name, even if those records were not requested or sent. A name is
|
||||
// also considered to exist if there are any names under it.
|
||||
//
|
||||
// In other words, b.c should return NOERROR even if it has no records of any
|
||||
// type if there is a record at a.b.c, or so on.
|
||||
var ErrNoSuchDomain = Rerrorf(dns.RcodeNameError, "no such domain")
|
||||
|
||||
// Represents REFUSED, which we use when a request is received for a zone for
|
||||
// which the server is not authoritative.
|
||||
var ErrNotInZone = Rerrorf(dns.RcodeRefused, "domain not in zone")
|
||||
|
||||
// Represents NOERROR. This error is used when NXDOMAIN is not an appropriate
|
||||
// response code, but no results were returned. (DNS also uses NOERROR when results
|
||||
// are returned, but we return nil in that case.)
|
||||
var ErrNoResults = Rerrorf(0, "no results")
|
||||
|
@ -0,0 +1,26 @@
|
||||
package util
|
||||
import "strings"
|
||||
|
||||
// miekg/dns demands a superflous trailing dot, this makes sure it is correctly appended.
|
||||
func Absname(n string) string {
|
||||
if n == "" {
|
||||
return "."
|
||||
}
|
||||
if n[len(n)-1] != '.' {
|
||||
return n + "."
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// Split a domain name a.b.c.d.e into parts a (the head) and b.c.d.e (the rest).
|
||||
func SplitDomainHead(name string) (head string, rest string, err error) {
|
||||
parts := strings.Split(name, ".")
|
||||
|
||||
head = parts[len(parts)-1]
|
||||
|
||||
if len(parts) >= 2 {
|
||||
rest = strings.Join(parts[0:len(parts)-1], ".")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue