multiaddr

package
v0.0.0-...-bebc87d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2014 License: MIT Imports: 6 Imported by: 0

README

go-multiaddr

multiaddr implementation in Go.

Example

Simple
import "github.com/jbenet/go-multiaddr"

m := multiaddr.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
// <Multiaddr /ip4/127.0.0.1/udp/1234>
m.buffer
// <Buffer >
m.String()
// /ip4/127.0.0.1/udp/1234

// construct with Buffer
m = multiaddr.Multiaddr{ Bytes: m.Bytes }
// <Multiaddr /ip4/127.0.0.1/udp/1234>
Protocols
// get the multiaddr protocol description objects
addr.Protocols()
// []*Protocol{
//   &Protocol{ Code: 4, Name: 'ip4', Size: 32},
//   &Protocol{ Code: 17, Name: 'udp', Size: 16},
// }
Other formats
// handles the stupid url version too
m = multiaddr.NewUrl("udp4://127.0.0.1:1234")
// <Multiaddr /ip4/127.0.0.1/udp/1234>
m.Url(buf)
// udp4://127.0.0.1:1234
En/decapsulate
m.Encapsulate(m.NewMultiaddr("/sctp/5678"))
// <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
m.Decapsulate(m.NewMultiaddr("/udp")) // up to + inc last occurrence of subaddr
// <Multiaddr /ip4/127.0.0.1>
Tunneling

Multiaddr allows expressing tunnels very nicely.

printer := multiaddr.NewMultiaddr("/ip4/192.168.0.13/tcp/80")
proxy := multiaddr.NewMultiaddr("/ip4/10.20.30.40/tcp/443")
printerOverProxy := proxy.Encapsulate(printer)
// <Multiaddr /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80>

proxyAgain := printerOverProxy.Decapsulate(multiaddr.NewMultiaddr("/ip4"))
// <Multiaddr /ip4/10.20.30.40/tcp/443>

Documentation

Index

Constants

View Source
const (
	P_IP4  = 4
	P_TCP  = 6
	P_UDP  = 17
	P_DCCP = 33
	P_IP6  = 41
	P_SCTP = 132
)

replicating table here to: 1. avoid parsing the csv 2. ensuring errors in the csv don't screw up code. 3. changing a number has to happen in two places.

Variables

View Source
var Protocols = []*Protocol{
	&Protocol{P_IP4, 32, "ip4"},
	&Protocol{P_TCP, 16, "tcp"},
	&Protocol{P_UDP, 16, "udp"},
	&Protocol{P_DCCP, 16, "dccp"},
	&Protocol{P_IP6, 128, "ip6"},

	&Protocol{P_SCTP, 16, "sctp"},
}

Protocols is the list of multiaddr protocols supported by this module.

Functions

This section is empty.

Types

type Multiaddr

type Multiaddr struct {
	Bytes []byte
}

Multiaddr is the data structure representing a multiaddr

func FromIP

func FromIP(ip net.IP) (*Multiaddr, error)

FromIP converts a net.IP type to a Multiaddr.

func FromNetAddr

func FromNetAddr(a net.Addr) (*Multiaddr, error)

FromNetAddr converts a net.Addr type to a Multiaddr.

func NewMultiaddr

func NewMultiaddr(s string) (*Multiaddr, error)

NewMultiaddr parses and validates an input string, returning a *Multiaddr

func (*Multiaddr) Decapsulate

func (m *Multiaddr) Decapsulate(o *Multiaddr) (*Multiaddr, error)

Decapsulate unwraps Multiaddr up until the given Multiaddr is found.

func (*Multiaddr) DialArgs

func (m *Multiaddr) DialArgs() (string, string, error)

DialArgs is a convenience function returning arguments for use in net.Dial

func (*Multiaddr) Encapsulate

func (m *Multiaddr) Encapsulate(o *Multiaddr) *Multiaddr

Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr

func (*Multiaddr) Equal

func (m *Multiaddr) Equal(m2 *Multiaddr) bool

Equal tests whether two multiaddrs are equal

func (*Multiaddr) IsThinWaist

func (m *Multiaddr) IsThinWaist() bool

IsThinWaist returns whether this multiaddr includes "Thin Waist" Protocols. This means: /{IP4, IP6}/{TCP, UDP}

func (*Multiaddr) Protocols

func (m *Multiaddr) Protocols() (ret []*Protocol, err error)

Protocols returns the list of protocols this Multiaddr has.

func (*Multiaddr) String

func (m *Multiaddr) String() (string, error)

String returns the string representation of a Multiaddr

type Protocol

type Protocol struct {
	Code int
	Size int
	Name string
}

Protocol is a Multiaddr protocol description structure.

func ProtocolWithCode

func ProtocolWithCode(c int) *Protocol

ProtocolWithCode returns the Protocol description with given protocol code.

func ProtocolWithName

func ProtocolWithName(s string) *Protocol

ProtocolWithName returns the Protocol description with given string name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL