README.md 3.72 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
# go-multiaddr

Richard Littauer's avatar
Richard Littauer committed
3
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
Richard Littauer's avatar
Richard Littauer committed
4 5 6
[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs)
[![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
Matt Joiner's avatar
Matt Joiner committed
7
[![GoDoc](https://godoc.org/github.com/multiformats/go-multiaddr?status.svg)](https://godoc.org/github.com/multiformats/go-multiaddr)
Richard Littauer's avatar
Richard Littauer committed
8 9
[![Travis CI](https://img.shields.io/travis/multiformats/go-multiaddr.svg?style=flat-square&branch=master)](https://travis-ci.org/multiformats/go-multiaddr)
[![codecov.io](https://img.shields.io/codecov/c/github/multiformats/go-multiaddr.svg?style=flat-square&branch=master)](https://codecov.io/github/multiformats/go-multiaddr?branch=master)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10

11
> [multiaddr](https://github.com/multiformats/multiaddr) implementation in go
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12

13
Multiaddr is a standard way to represent addresses that:
14

15 16 17 18 19
- Support any standard network protocols.
- Self-describe (include protocols).
- Have a binary packed format.
- Have a nice string representation.
- Encapsulate well.
Richard Littauer's avatar
Richard Littauer committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

## Table of Contents

- [Install](#install)
- [Usage](#usage)
  - [Example](#example)
    - [Simple](#simple)
    - [Protocols](#protocols)
    - [En/decapsulate](#endecapsulate)
    - [Tunneling](#tunneling)
- [Maintainers](#maintainers)
- [Contribute](#contribute)
- [License](#license)

## Install

```sh
Richard Littauer's avatar
Richard Littauer committed
37
go get github.com/multiformats/go-multiaddr
Richard Littauer's avatar
Richard Littauer committed
38 39 40 41 42 43 44
```

## Usage

### Example

#### Simple
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46

```go
Lars Gierth's avatar
Lars Gierth committed
47
import ma "github.com/multiformats/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48

Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
49 50
// 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
51

Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
52 53 54 55 56 57 58 59 60
// 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
61 62
```

Richard Littauer's avatar
Richard Littauer committed
63
#### Protocols
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64 65 66

```go
// get the multiaddr protocol description objects
Richard Littauer's avatar
Richard Littauer committed
67
m1.Protocols()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68 69 70
// []Protocol{
//   Protocol{ Code: 4, Name: 'ip4', Size: 32},
//   Protocol{ Code: 17, Name: 'udp', Size: 16},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71 72 73
// }
```

Richard Littauer's avatar
Richard Littauer committed
74
#### En/decapsulate
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75 76

```go
Overbool's avatar
Overbool committed
77 78 79 80 81 82 83 84
import ma "github.com/multiformats/go-multiaddr"

m, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
// <Multiaddr /ip4/127.0.0.1/udp/1234>

sctpMA, err := ma.NewMultiaddr("/sctp/5678")

m.Encapsulate(sctpMA)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85
// <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
Overbool's avatar
Overbool committed
86 87 88 89

udpMA, err := ma.NewMultiaddr("/udp/1234")

m.Decapsulate(udpMA) // up to + inc last occurrence of subaddr
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91 92
// <Multiaddr /ip4/127.0.0.1>
```

Richard Littauer's avatar
Richard Littauer committed
93
#### Tunneling
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94 95 96 97

Multiaddr allows expressing tunnels very nicely.

```js
Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
98 99
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
100
printerOverProxy := proxy.Encapsulate(printer)
Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
101
// /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102

Juan Batiz-Benet's avatar
docs  
Juan Batiz-Benet committed
103 104
proxyAgain := printerOverProxy.Decapsulate(printer)
// /ip4/10.20.30.40/tcp/443
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105
```
Richard Littauer's avatar
Richard Littauer committed
106 107 108 109 110 111 112

## Contribute

Contributions welcome. Please check out [the issues](https://github.com/multiformats/go-multiaddr/issues).

Check out our [contributing document](https://github.com/multiformats/multiformats/blob/master/contributing.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).

Richard Littauer's avatar
Richard Littauer committed
113
Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
Richard Littauer's avatar
Richard Littauer committed
114 115 116

## License

Richard Littauer's avatar
Richard Littauer committed
117
[MIT](LICENSE) © 2014 Juan Batiz-Benet