-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1414 from libp2p/merge-swarm
move the go-libp2p-swarm here
- Loading branch information
Showing
46 changed files
with
5,666 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package swarm | ||
|
||
import ( | ||
ma "github.com/multiformats/go-multiaddr" | ||
mamask "github.com/whyrusleeping/multiaddr-filter" | ||
) | ||
|
||
// http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml | ||
var lowTimeoutFilters = ma.NewFilters() | ||
|
||
func init() { | ||
for _, p := range []string{ | ||
"/ip4/10.0.0.0/ipcidr/8", | ||
"/ip4/100.64.0.0/ipcidr/10", | ||
"/ip4/169.254.0.0/ipcidr/16", | ||
"/ip4/172.16.0.0/ipcidr/12", | ||
"/ip4/192.0.0.0/ipcidr/24", | ||
"/ip4/192.0.0.0/ipcidr/29", | ||
"/ip4/192.0.0.8/ipcidr/32", | ||
"/ip4/192.0.0.170/ipcidr/32", | ||
"/ip4/192.0.0.171/ipcidr/32", | ||
"/ip4/192.0.2.0/ipcidr/24", | ||
"/ip4/192.168.0.0/ipcidr/16", | ||
"/ip4/198.18.0.0/ipcidr/15", | ||
"/ip4/198.51.100.0/ipcidr/24", | ||
"/ip4/203.0.113.0/ipcidr/24", | ||
"/ip4/240.0.0.0/ipcidr/4", | ||
} { | ||
f, err := mamask.NewMask(p) | ||
if err != nil { | ||
panic("error in lowTimeoutFilters init: " + err.Error()) | ||
} | ||
lowTimeoutFilters.AddFilter(*f, ma.ActionDeny) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package swarm | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/libp2p/go-libp2p-core/peer" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
// maxDialDialErrors is the maximum number of dial errors we record | ||
const maxDialDialErrors = 16 | ||
|
||
// DialError is the error type returned when dialing. | ||
type DialError struct { | ||
Peer peer.ID | ||
DialErrors []TransportError | ||
Cause error | ||
Skipped int | ||
} | ||
|
||
func (e *DialError) Timeout() bool { | ||
return os.IsTimeout(e.Cause) | ||
} | ||
|
||
func (e *DialError) recordErr(addr ma.Multiaddr, err error) { | ||
if len(e.DialErrors) >= maxDialDialErrors { | ||
e.Skipped++ | ||
return | ||
} | ||
e.DialErrors = append(e.DialErrors, TransportError{ | ||
Address: addr, | ||
Cause: err, | ||
}) | ||
} | ||
|
||
func (e *DialError) Error() string { | ||
var builder strings.Builder | ||
fmt.Fprintf(&builder, "failed to dial %s:", e.Peer) | ||
if e.Cause != nil { | ||
fmt.Fprintf(&builder, " %s", e.Cause) | ||
} | ||
for _, te := range e.DialErrors { | ||
fmt.Fprintf(&builder, "\n * [%s] %s", te.Address, te.Cause) | ||
} | ||
if e.Skipped > 0 { | ||
fmt.Fprintf(&builder, "\n ... skipping %d errors ...", e.Skipped) | ||
} | ||
return builder.String() | ||
} | ||
|
||
// Unwrap implements https://godoc.org/golang.org/x/xerrors#Wrapper. | ||
func (e *DialError) Unwrap() error { | ||
return e.Cause | ||
} | ||
|
||
var _ error = (*DialError)(nil) | ||
|
||
// TransportError is the error returned when dialing a specific address. | ||
type TransportError struct { | ||
Address ma.Multiaddr | ||
Cause error | ||
} | ||
|
||
func (e *TransportError) Error() string { | ||
return fmt.Sprintf("failed to dial %s: %s", e.Address, e.Cause) | ||
} | ||
|
||
var _ error = (*TransportError)(nil) |
Oops, something went wrong.