OSDN Git Service

52b964593708f9cdd2e21f43c882226e47624383
[bytom/vapor.git] / vendor / github.com / multiformats / go-multiaddr-dns / dns.go
1 package madns
2
3 import (
4         "bytes"
5         "fmt"
6
7         ma "github.com/multiformats/go-multiaddr"
8 )
9
10 var Dns4Protocol = ma.Protocol{
11         Code:       54,
12         Size:       ma.LengthPrefixedVarSize,
13         Name:       "dns4",
14         VCode:      ma.CodeToVarint(54),
15         Transcoder: DnsTranscoder,
16 }
17 var Dns6Protocol = ma.Protocol{
18         Code:       55,
19         Size:       ma.LengthPrefixedVarSize,
20         Name:       "dns6",
21         VCode:      ma.CodeToVarint(55),
22         Transcoder: DnsTranscoder,
23 }
24 var DnsaddrProtocol = ma.Protocol{
25         Code:       56,
26         Size:       ma.LengthPrefixedVarSize,
27         Name:       "dnsaddr",
28         VCode:      ma.CodeToVarint(56),
29         Transcoder: DnsTranscoder,
30 }
31
32 func init() {
33         err := ma.AddProtocol(Dns4Protocol)
34         if err != nil {
35                 panic(fmt.Errorf("error registering dns4 protocol: %s", err))
36         }
37         err = ma.AddProtocol(Dns6Protocol)
38         if err != nil {
39                 panic(fmt.Errorf("error registering dns6 protocol: %s", err))
40         }
41         err = ma.AddProtocol(DnsaddrProtocol)
42         if err != nil {
43                 panic(fmt.Errorf("error registering dnsaddr protocol: %s", err))
44         }
45 }
46
47 var DnsTranscoder = ma.NewTranscoderFromFunctions(dnsStB, dnsBtS, dnsVal)
48
49 func dnsVal(b []byte) error {
50         if bytes.IndexByte(b, '/') >= 0 {
51                 return fmt.Errorf("domain name %q contains a slash", string(b))
52         }
53         return nil
54 }
55
56 func dnsStB(s string) ([]byte, error) {
57         return []byte(s), nil
58 }
59
60 func dnsBtS(b []byte) (string, error) {
61         return string(b), nil
62 }