namesys.go 2.58 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package namesys

import (
4
	"strings"
5
	"time"
6

7
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8 9
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
	ci "github.com/ipfs/go-ipfs/p2p/crypto"
10
	path "github.com/ipfs/go-ipfs/path"
11
	routing "github.com/ipfs/go-ipfs/routing"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
)

14
// mpns (a multi-protocol NameSystem) implements generic IPFS naming.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
//
16
// Uses several Resolvers:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18 19 20 21 22
// (a) ipfs routing naming: SFS-like PKI names.
// (b) dns domains: resolves using links in DNS TXT records
// (c) proquints: interprets string as the raw byte data.
//
// It can only publish to: (a) ipfs routing naming.
//
23 24 25
type mpns struct {
	resolvers  map[string]resolver
	publishers map[string]Publisher
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26 27 28
}

// NewNameSystem will construct the IPFS naming system based on Routing
29
func NewNameSystem(r routing.IpfsRouting, ds ds.Datastore) NameSystem {
30 31
	return &mpns{
		resolvers: map[string]resolver{
32
			"dns":      newDNSResolver(),
33 34 35 36
			"proquint": new(ProquintResolver),
			"dht":      newRoutingResolver(r),
		},
		publishers: map[string]Publisher{
37
			"/ipns/": NewRoutingPublisher(r, ds),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40 41
		},
	}
}

42 43 44 45 46 47 48 49 50
// Resolve implements Resolver.
func (ns *mpns) Resolve(ctx context.Context, name string) (path.Path, error) {
	return ns.ResolveN(ctx, name, DefaultDepthLimit)
}

// ResolveN implements Resolver.
func (ns *mpns) ResolveN(ctx context.Context, name string, depth int) (path.Path, error) {
	if strings.HasPrefix(name, "/ipfs/") {
		return path.ParsePath(name)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
	}
52 53 54 55 56 57

	if !strings.HasPrefix(name, "/") {
		return path.ParsePath("/ipfs/" + name)
	}

	return resolve(ctx, ns, name, depth, "/ipns/")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58 59
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
// resolveOnce implements resolver.
func (ns *mpns) resolveOnce(ctx context.Context, name string) (path.Path, error) {
	if !strings.HasPrefix(name, "/ipns/") {
		name = "/ipns/" + name
	}
	segments := strings.SplitN(name, "/", 3)
	if len(segments) < 3 || segments[0] != "" {
		log.Warningf("Invalid name syntax for %s", name)
		return "", ErrResolveFailed
	}

	for protocol, resolver := range ns.resolvers {
		log.Debugf("Attempting to resolve %s with %s", name, protocol)
		p, err := resolver.resolveOnce(ctx, segments[2])
		if err == nil {
			return p, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76 77
		}
	}
78 79
	log.Warningf("No resolver found for %s", name)
	return "", ErrResolveFailed
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82
}

// Publish implements Publisher
83 84
func (ns *mpns) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
	return ns.publishers["/ipns/"].Publish(ctx, name, value)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85
}
86 87 88 89

func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, val path.Path, eol time.Time) error {
	return ns.publishers["/ipns/"].PublishWithEOL(ctx, name, val, eol)
}