registry.go 1.51 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
package manet

import (
	"fmt"
	"net"
	"sync"

	ma "github.com/jbenet/go-multiaddr"
)

type AddrParser func(a net.Addr) (ma.Multiaddr, error)
type MaddrParser func(ma ma.Multiaddr) (net.Addr, error)

var maddrParsers map[string]MaddrParser
var addrParsers map[string]AddrParser
var addrParsersLock sync.Mutex

func RegisterAddressType(netname, maname string, ap AddrParser, mp MaddrParser) {
	addrParsersLock.Lock()
	defer addrParsersLock.Unlock()
	addrParsers[netname] = ap
	maddrParsers[maname] = mp
}

func init() {
	addrParsers = make(map[string]AddrParser)
	maddrParsers = make(map[string]MaddrParser)

	funcs := map[string]AddrParser{
		"tcp": parseTcpNetAddr,
		"udp": parseUdpNetAddr,
		"utp": parseUtpNetAddr,
	}

	for k, v := range funcs {
		RegisterAddressType(k, k, v, parseBasicNetMaddr)
		RegisterAddressType(k+"4", k, v, parseBasicNetMaddr)
		RegisterAddressType(k+"6", k, v, parseBasicNetMaddr)
	}

	for _, i := range []string{"ip", "ip4", "ip6"} {
		RegisterAddressType(i, i, parseIpNetAddr, parseBasicNetMaddr)
	}

	addrParsers["ip+net"] = parseIpPlusNetAddr
}

func getAddrParser(net string) (AddrParser, error) {
	addrParsersLock.Lock()
	defer addrParsersLock.Unlock()

	parser, ok := addrParsers[net]
	if !ok {
		return nil, fmt.Errorf("unknown network %v", net)
	}
	return parser, nil
}

func getMaddrParser(name string) (MaddrParser, error) {
	addrParsersLock.Lock()
	defer addrParsersLock.Unlock()
	p, ok := maddrParsers[name]
	if !ok {
		return nil, fmt.Errorf("network not supported: %s", name)
	}

	return p, nil
}