README.md 1.32 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8 9
# go-multiaddr

[multiaddr](https://github.com/jbenet/multiaddr) implementation in Go.

## Example

### Simple

```go
Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
10
import ma "github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11

Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
12 13
// construct from a string (err signals parse failure)
m1, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14

Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
15 16 17 18 19 20 21 22 23
// 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)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25 26 27 28 29
```

### Protocols

```go
// get the multiaddr protocol description objects
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
addr.Protocols()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32 33
// []Protocol{
//   Protocol{ Code: 4, Name: 'ip4', Size: 32},
//   Protocol{ Code: 17, Name: 'udp', Size: 16},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36 37 38 39
// }
```

### En/decapsulate

```go
Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
40
m.Encapsulate(ma.NewMultiaddr("/sctp/5678"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
// <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
42
m.Decapsulate(ma.NewMultiaddr("/udp")) // up to + inc last occurrence of subaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43 44 45 46 47 48 49 50
// <Multiaddr /ip4/127.0.0.1>
```

### Tunneling

Multiaddr allows expressing tunnels very nicely.

```js
Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
51 52
printer, _ := ma.NewMultiaddr("/ip4/192.168.0.13/tcp/80")
proxy, _ := ma.NewMultiaddr("/ip4/10.20.30.40/tcp/443")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
printerOverProxy := proxy.Encapsulate(printer)
Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
54
// /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55

Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
56 57
proxyAgain := printerOverProxy.Decapsulate(printer)
// /ip4/10.20.30.40/tcp/443
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
```