Skip to content

Commit 26a332b

Browse files
committed
vendor 6-to-7 gx dependencies
1 parent 4d0c113 commit 26a332b

File tree

1,144 files changed

+348527
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,144 files changed

+348527
-0
lines changed

ipfs-6-to-7/vendor/gx/ipfs/QmNRz7BDWfdFNVLt7AVvmRefkrURD25EeoipcXqo6yoXU1/go-ipld-cbor/node.go

Lines changed: 565 additions & 0 deletions
Large diffs are not rendered by default.

ipfs-6-to-7/vendor/gx/ipfs/QmNRz7BDWfdFNVLt7AVvmRefkrURD25EeoipcXqo6yoXU1/go-ipld-cbor/node_test.go

Lines changed: 433 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
package addrutil
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
manet "gx/ipfs/QmRK2LxanhK2gZq6k6R7vk5ZoYZk8ULSSTB7FzDsMUX6CB/go-multiaddr-net"
8+
logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
9+
ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr"
10+
11+
_ "gx/ipfs/QmepzWZwZK23YHYVjhKBEvJnNTgsg71bWetZU9bEsP4qqf/go-ws-transport"
12+
)
13+
14+
var log = logging.Logger("addrutil")
15+
16+
// SupportedTransportStrings is the list of supported transports for the swarm.
17+
// These are strings of encapsulated multiaddr protocols. E.g.:
18+
// /ip4/tcp
19+
var SupportedTransportStrings = []string{
20+
"/ip4/tcp",
21+
"/ip6/tcp",
22+
"/ip4/udp/utp",
23+
"/ip6/udp/utp",
24+
"/ip4/tcp/ws",
25+
"/ip6/tcp/ws",
26+
// "/ip4/udp/udt", disabled because the lib doesnt work on arm
27+
// "/ip6/udp/udt", disabled because the lib doesnt work on arm
28+
}
29+
30+
// SupportedTransportProtocols is the list of supported transports for the swarm.
31+
// These are []ma.Protocol lists. Populated at runtime from SupportedTransportStrings
32+
var SupportedTransportProtocols = [][]ma.Protocol{}
33+
34+
func init() {
35+
// initialize SupportedTransportProtocols
36+
transports := make([][]ma.Protocol, len(SupportedTransportStrings))
37+
for _, s := range SupportedTransportStrings {
38+
t, err := ma.ProtocolsWithString(s)
39+
if err != nil {
40+
panic(err) // important to fix this in the codebase
41+
}
42+
transports = append(transports, t)
43+
}
44+
SupportedTransportProtocols = transports
45+
}
46+
47+
// AddTransport adds a transport protocol combination to the list of supported transports
48+
func AddTransport(s string) error {
49+
t, err := ma.ProtocolsWithString(s)
50+
if err != nil {
51+
return err
52+
}
53+
54+
SupportedTransportStrings = append(SupportedTransportStrings, s)
55+
SupportedTransportProtocols = append(SupportedTransportProtocols, t)
56+
return nil
57+
}
58+
59+
// FilterAddrs is a filter that removes certain addresses, according the given filters.
60+
// if all filters return true, the address is kept.
61+
func FilterAddrs(a []ma.Multiaddr, filters ...func(ma.Multiaddr) bool) []ma.Multiaddr {
62+
b := make([]ma.Multiaddr, 0, len(a))
63+
for _, addr := range a {
64+
good := true
65+
for _, filter := range filters {
66+
good = good && filter(addr)
67+
}
68+
if good {
69+
b = append(b, addr)
70+
}
71+
}
72+
return b
73+
}
74+
75+
// FilterUsableAddrs removes certain addresses
76+
// from a list. the addresses removed are those known NOT
77+
// to work with our network. Namely, addresses with UTP.
78+
func FilterUsableAddrs(a []ma.Multiaddr) []ma.Multiaddr {
79+
return FilterAddrs(a, AddrUsableFunc)
80+
}
81+
82+
func AddrUsableFunc(m ma.Multiaddr) bool {
83+
return AddrUsable(m, false)
84+
}
85+
86+
// AddrOverNonLocalIP returns whether the addr uses a non-local ip link
87+
func AddrOverNonLocalIP(a ma.Multiaddr) bool {
88+
split := ma.Split(a)
89+
if len(split) < 1 {
90+
return false
91+
}
92+
if manet.IsIP6LinkLocal(split[0]) {
93+
return false
94+
}
95+
return true
96+
}
97+
98+
// AddrUsable returns whether our network can use this addr.
99+
// We only use the transports in SupportedTransportStrings,
100+
// and we do not link local addresses. Loopback is ok
101+
// as we need to be able to connect to multiple ipfs nodes
102+
// in the same machine.
103+
func AddrUsable(a ma.Multiaddr, partial bool) bool {
104+
if a == nil {
105+
return false
106+
}
107+
108+
if !AddrOverNonLocalIP(a) {
109+
return false
110+
}
111+
112+
// test the address protocol list is in SupportedTransportProtocols
113+
matches := func(supported, test []ma.Protocol) bool {
114+
if len(test) > len(supported) {
115+
return false
116+
}
117+
118+
// when partial, it's ok if test < supported.
119+
if !partial && len(supported) != len(test) {
120+
return false
121+
}
122+
123+
for i := range test {
124+
if supported[i].Code != test[i].Code {
125+
return false
126+
}
127+
}
128+
return true
129+
}
130+
131+
transport := a.Protocols()
132+
for _, supported := range SupportedTransportProtocols {
133+
if matches(supported, transport) {
134+
return true
135+
}
136+
}
137+
138+
return false
139+
}
140+
141+
// ResolveUnspecifiedAddress expands an unspecified ip addresses (/ip4/0.0.0.0, /ip6/::) to
142+
// use the known local interfaces. If ifaceAddr is nil, we request interface addresses
143+
// from the network stack. (this is so you can provide a cached value if resolving many addrs)
144+
func ResolveUnspecifiedAddress(resolve ma.Multiaddr, ifaceAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
145+
// split address into its components
146+
split := ma.Split(resolve)
147+
148+
// if first component (ip) is not unspecified, use it as is.
149+
if !manet.IsIPUnspecified(split[0]) {
150+
return []ma.Multiaddr{resolve}, nil
151+
}
152+
153+
out := make([]ma.Multiaddr, 0, len(ifaceAddrs))
154+
for _, ia := range ifaceAddrs {
155+
// must match the first protocol to be resolve.
156+
if ia.Protocols()[0].Code != resolve.Protocols()[0].Code {
157+
continue
158+
}
159+
160+
split[0] = ia
161+
joined := ma.Join(split...)
162+
out = append(out, joined)
163+
log.Debug("adding resolved addr:", resolve, joined, out)
164+
}
165+
if len(out) < 1 {
166+
return nil, fmt.Errorf("failed to resolve: %s", resolve)
167+
}
168+
return out, nil
169+
}
170+
171+
// ResolveUnspecifiedAddresses expands unspecified ip addresses (/ip4/0.0.0.0, /ip6/::) to
172+
// use the known local interfaces.
173+
func ResolveUnspecifiedAddresses(unspecAddrs, ifaceAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
174+
175+
// todo optimize: only fetch these if we have a "any" addr.
176+
if len(ifaceAddrs) < 1 {
177+
var err error
178+
ifaceAddrs, err = InterfaceAddresses()
179+
if err != nil {
180+
return nil, err
181+
}
182+
// log.Debug("InterfaceAddresses:", ifaceAddrs)
183+
}
184+
185+
var outputAddrs []ma.Multiaddr
186+
for _, a := range unspecAddrs {
187+
// unspecified?
188+
resolved, err := ResolveUnspecifiedAddress(a, ifaceAddrs)
189+
if err != nil {
190+
continue // optimistic. if we cant resolve anything, we'll know at the bottom.
191+
}
192+
// log.Debug("resolved:", a, resolved)
193+
outputAddrs = append(outputAddrs, resolved...)
194+
}
195+
196+
if len(outputAddrs) < 1 {
197+
return nil, fmt.Errorf("failed to specify addrs: %s", unspecAddrs)
198+
}
199+
200+
log.Event(context.TODO(), "interfaceListenAddresses", func() logging.Loggable {
201+
var addrs []string
202+
for _, addr := range outputAddrs {
203+
addrs = append(addrs, addr.String())
204+
}
205+
return logging.Metadata{"addresses": addrs}
206+
}())
207+
208+
log.Debug("ResolveUnspecifiedAddresses:", unspecAddrs, ifaceAddrs, outputAddrs)
209+
return outputAddrs, nil
210+
}
211+
212+
// InterfaceAddresses returns a list of addresses associated with local machine
213+
// Note: we do not return link local addresses. IP loopback is ok, because we
214+
// may be connecting to other nodes in the same machine.
215+
func InterfaceAddresses() ([]ma.Multiaddr, error) {
216+
maddrs, err := manet.InterfaceMultiaddrs()
217+
if err != nil {
218+
return nil, err
219+
}
220+
log.Debug("InterfaceAddresses: from manet:", maddrs)
221+
222+
var out []ma.Multiaddr
223+
for _, a := range maddrs {
224+
if !AddrUsable(a, true) { // partial
225+
// log.Debug("InterfaceAddresses: skipping unusable:", a)
226+
continue
227+
}
228+
229+
out = append(out, a)
230+
}
231+
232+
log.Debug("InterfaceAddresses: usable:", out)
233+
return out, nil
234+
}
235+
236+
// AddrInList returns whether or not an address is part of a list.
237+
// this is useful to check if NAT is happening (or other bugs?)
238+
func AddrInList(addr ma.Multiaddr, list []ma.Multiaddr) bool {
239+
for _, addr2 := range list {
240+
if addr.Equal(addr2) {
241+
return true
242+
}
243+
}
244+
return false
245+
}
246+
247+
// AddrIsShareableOnWAN returns whether the given address should be shareable on the
248+
// wide area network (wide internet).
249+
func AddrIsShareableOnWAN(addr ma.Multiaddr) bool {
250+
s := ma.Split(addr)
251+
if len(s) < 1 {
252+
return false
253+
}
254+
a := s[0]
255+
if manet.IsIPLoopback(a) || manet.IsIP6LinkLocal(a) || manet.IsIPUnspecified(a) {
256+
return false
257+
}
258+
return manet.IsThinWaist(a)
259+
}
260+
261+
// WANShareableAddrs filters addresses based on whether they're shareable on WAN
262+
func WANShareableAddrs(inp []ma.Multiaddr) []ma.Multiaddr {
263+
return FilterAddrs(inp, AddrIsShareableOnWAN)
264+
}
265+
266+
// Subtract filters out all addrs in b from a
267+
func Subtract(a, b []ma.Multiaddr) []ma.Multiaddr {
268+
return FilterAddrs(a, func(m ma.Multiaddr) bool {
269+
for _, bb := range b {
270+
if m.Equal(bb) {
271+
return false
272+
}
273+
}
274+
return true
275+
})
276+
}
277+
278+
// CheckNATWarning checks if our observed addresses differ. if so,
279+
// informs the user that certain things might not work yet
280+
func CheckNATWarning(observed, expected ma.Multiaddr, listen []ma.Multiaddr) {
281+
if observed.Equal(expected) {
282+
return
283+
}
284+
285+
if !AddrInList(observed, listen) { // probably a nat
286+
log.Warningf(natWarning, observed, listen)
287+
}
288+
}
289+
290+
const natWarning = `Remote peer observed our address to be: %s
291+
The local addresses are: %s
292+
Thus, connection is going through NAT, and other connections may fail.
293+
294+
IPFS NAT traversal is still under development. Please bug us on github or irc to fix this.
295+
Baby steps: http://jbenet.static.s3.amazonaws.com/271dfcf/baby-steps.gif
296+
`

0 commit comments

Comments
 (0)