Documentation ¶
Overview ¶
Package multiaddr provides an implementation of the Multiaddr network address format. Multiaddr emphasizes explicitness, self-description, and portability. It allows applications to treat addresses as opaque tokens, and to avoid making assumptions about the address representation (e.g. length). Learn more at https://github.com/multiformats/multiaddr
Basic Use:
import ( "bytes" "strings" ma "github.com/multiformats/go-multiaddr" ) // construct from a string (err signals parse failure) m1, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234") // construct from bytes (err signals parse failure) m2, err := ma.NewMultiaddrBytes(m1.Bytes()) // true strings.Equal(m1.String(), "/ip4/127.0.0.1/udp/1234") strings.Equal(m1.String(), m2.String()) bytes.Equal(m1.Bytes(), m2.Bytes()) m1.Equal(m2) m2.Equal(m1) // tunneling (en/decap) printer, _ := ma.NewMultiaddr("/ip4/192.168.0.13/tcp/80") proxy, _ := ma.NewMultiaddr("/ip4/10.20.30.40/tcp/443") printerOverProxy := proxy.Encapsulate(printer) proxyAgain := printerOverProxy.Decapsulate(printer)
Index ¶
Constants ¶
const ( P_IP4 = 0x0004 P_TCP = 0x0006 P_UDP = 0x0111 P_DCCP = 0x0021 P_IP6 = 0x0029 P_QUIC = 0x01CC P_SCTP = 0x0084 P_UDT = 0x012D P_UTP = 0x012E P_UNIX = 0x0190 P_P2P = 0x01A5 P_IPFS = 0x01A5 // alias for backwards compatability P_HTTP = 0x01E0 P_HTTPS = 0x01BB P_ONION = 0x01BC )
You **MUST** register your multicodecs with https://github.com/multiformats/multicodec before adding them here.
TODO: Use a single source of truth for all multicodecs instead of distributing them like this...
const (
LengthPrefixedVarSize = -1
)
These are special sizes
Variables ¶
var ErrProtocolNotFound = fmt.Errorf("protocol not found in multiaddr")
var Protocols = []Protocol{
protoIP4,
protoTCP,
protoUDP,
protoDCCP,
protoIP6,
protoSCTP,
protoONION,
protoUTP,
protoUDT,
protoQUIC,
protoHTTP,
protoHTTPS,
protoP2P,
protoUNIX,
}
Protocols is the list of multiaddr protocols supported by this module.
var ProtocolsByName = map[string]Protocol{}
var TranscoderIP4 = NewTranscoderFromFunctions(ip4StB, ipBtS, nil)
var TranscoderIP6 = NewTranscoderFromFunctions(ip6StB, ipBtS, nil)
var TranscoderOnion = NewTranscoderFromFunctions(onionStB, onionBtS, nil)
var TranscoderP2P = NewTranscoderFromFunctions(p2pStB, p2pBtS, p2pVal)
var TranscoderPort = NewTranscoderFromFunctions(portStB, portBtS, nil)
var TranscoderUnix = NewTranscoderFromFunctions(unixStB, unixBtS, nil)
Functions ¶
func AddProtocol ¶
func CodeToVarint ¶
CodeToVarint converts an integer to a varint-encoded []byte
func ReadVarintCode ¶
ReadVarintCode reads a varint code from the beginning of buf. returns the code, and the number of bytes read.
func SwapToP2pMultiaddrs ¶ added in v1.3.0
func SwapToP2pMultiaddrs()
SwapToP2pMultiaddrs is a function to make the transition from /ipfs/... multiaddrs to /p2p/... multiaddrs easier The first stage of the rollout is to ship this package to all users so that all users of multiaddr can parse both /ipfs/ and /p2p/ multiaddrs as the same code (P_P2P). During this stage of the rollout, all addresses with P_P2P will continue printing as /ipfs/, so that older clients without the new parsing code won't break. Once the network has adopted the new parsing code broadly enough, users of multiaddr can add a call to this method to an init function in their codebase. This will cause any P_P2P multiaddr to print out as /p2p/ instead of /ipfs/. Note that the binary serialization of this multiaddr does not change at any point. This means that this code is not a breaking network change at any point
func VarintToCode ¶
VarintToCode converts a varint-encoded []byte to an integer protocol code
Types ¶
type Multiaddr ¶
type Multiaddr interface { // Equal returns whether two Multiaddrs are exactly equal Equal(Multiaddr) bool // Bytes returns the []byte representation of this Multiaddr Bytes() []byte // String returns the string representation of this Multiaddr // (may panic if internal state is corrupted) String() string // Protocols returns the list of Protocols this Multiaddr includes // will panic if protocol code incorrect (and bytes accessed incorrectly) Protocols() []Protocol // Encapsulate wraps this Multiaddr around another. For example: // // /ip4/1.2.3.4 encapsulate /tcp/80 = /ip4/1.2.3.4/tcp/80 // Encapsulate(Multiaddr) Multiaddr // Decapsultate removes a Multiaddr wrapping. For example: // // /ip4/1.2.3.4/tcp/80 decapsulate /ip4/1.2.3.4 = /tcp/80 // Decapsulate(Multiaddr) Multiaddr // ValueForProtocol returns the value (if any) following the specified protocol ValueForProtocol(code int) (string, error) }
Multiaddr is a cross-protocol, cross-platform format for representing internet addresses. It emphasizes explicitness and self-description. Learn more here: https://github.com/multiformats/multiaddr
Multiaddrs have both a binary and string representation.
import ma "github.com/multiformats/go-multiaddr" addr, err := ma.NewMultiaddr("/ip4/1.2.3.4/tcp/80") // err non-nil when parsing failed.
func NewMultiaddr ¶
NewMultiaddr parses and validates an input string, returning a *Multiaddr
func NewMultiaddrBytes ¶
NewMultiaddrBytes initializes a Multiaddr from a byte representation. It validates it as an input string.
func StringCast ¶
StringCast like Cast, but parses a string. Will also panic if it fails to parse.
type Protocol ¶
type Protocol struct { Code int Size int // a size of -1 indicates a length-prefixed variable size Name string VCode []byte Path bool // indicates a path protocol (eg unix, http) Transcoder Transcoder }
Protocol is a Multiaddr protocol description structure.
func ProtocolWithCode ¶
ProtocolWithCode returns the Protocol description with given protocol code.
func ProtocolWithName ¶
ProtocolWithName returns the Protocol description with given string name.
func ProtocolsWithString ¶
ProtocolsWithString returns a slice of protocols matching given string.