Skip to content

Make libp2p-utp an happy transport and part of the libp2p blessed family #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ js-libp2p-utp
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)

[![](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-utp.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-utp)
[![](https://img.shields.io/travis/libp2p/js-libp2p-utp.svg?style=flat-square)](https://travis-ci.com/libp2p/js-libp2p-utp)
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-utp.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-utp)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"ip-address": "^5.9.0",
"lodash.includes": "^4.3.0",
"lodash.isfunction": "^3.0.9",
"mafmt": "^6.0.6",
"multiaddr": "^6.0.4",
"mafmt": "^6.0.7",
"multiaddr": "^6.0.6",
"once": "^1.4.0",
"pull-stream": "^3.6.9",
"stream-to-pull-stream": "^1.7.2",
"pull-stream": "^3.6.11",
"stream-to-pull-stream": "^1.7.3",
"utp-native": "^2.1.3"
},
"contributors": [
Expand Down
45 changes: 12 additions & 33 deletions src/create-listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,52 +79,31 @@ module.exports = (handler) => {

const lOpts = listeningAddr.toOptions()
log('Listening on %s %s', lOpts.port, lOpts.host)
console.log(Number(lOpts.port), lOpts.port)
server.listen(Number(lOpts.port), lOpts.host, callback)
}

listener.getAddrs = (callback) => {
const multiaddrs = []
const address = server.address()
const addr = server.address()

if (!address) {
if (!addr) {
return callback(new Error('Listener is not ready yet'))
}

// Because TCP will only return the IPv6 version
// we need to capture from the passed multiaddr
/*
if (listeningAddr.toString().indexOf('ip4') !== -1) {
let m = listeningAddr.decapsulate('utp')
m = m.encapsulate('/tcp/' + address.port)
if (ipfsId) {
m = m.encapsulate('/ipfs/' + ipfsId)
}

if (m.toString().indexOf('0.0.0.0') !== -1) {
const netInterfaces = os.networkInterfaces()
Object.keys(netInterfaces).forEach((niKey) => {
netInterfaces[niKey].forEach((ni) => {
if (ni.family === 'IPv4') {
multiaddrs.push(multiaddr(m.toString().replace('0.0.0.0', ni.address)))
}
})
})
} else {
multiaddrs.push(m)
}
let ma
if (addr.family === 'IPv6') {
ma = multiaddr(`/ip6/${addr.address}/udp/${addr.port}/utp`)
} else if (addr.family === 'IPv4') {
console.log(`/ip4/${addr.address}/udp/${addr.port}/utp`)
ma = multiaddr(`/ip4/${addr.address}/udp/${addr.port}/utp`)
}
*/

if (address.family === 'IPv6') {
let ma = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
if (ipfsId) {
ma = ma.encapsulate('/ipfs/' + ipfsId)
}

multiaddrs.push(ma)
if (ipfsId) {
ma = ma.encapsulate('/ipfs/' + ipfsId)
}

multiaddrs.push(ma)

callback(null, multiaddrs)
}

Expand Down
13 changes: 10 additions & 3 deletions test/connection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ const expect = chai.expect
chai.use(dirtyChai)
const UTP = require('../src')

describe('Connection', () => {
it('create an instance', () => {
const utp = new UTP()
describe('Valid libp2p Connection', () => {
let utp

beforeEach(() => {
utp = new UTP()
})

it.skip('.getObservedAddrs', (done) => {
expect(utp).to.exist()
})
it.skip('.getPeerInfo', (done) => {})
it.skip('.setPeerInfo', (done) => {})
})
63 changes: 28 additions & 35 deletions test/listen-dial.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const multiaddr = require('multiaddr')
const UTP = require('../src')
const isCI = process.env.CI

describe('listen', () => {
describe('Listener (.createListener => listener)', () => {
let utp

function ma (port) {
Expand All @@ -24,17 +24,10 @@ describe('listen', () => {
utp = new UTP()
})

it('close listener with connections, through timeout', function (done) {
it('.close with connections, through timeout', function (done) {
this.timeout(20 * 1000)

const listener = utp.createListener((conn) => {
pull(conn, conn)
})

listener.on('connection', () => {
// Testing
console.log('incomming connection')
})
const listener = utp.createListener((conn) => pull(conn, conn))

const addr = ma(6000)
const connectOptions = addr.toOptions()
Expand All @@ -46,30 +39,22 @@ describe('listen', () => {
socket1.write('Some data that is never handled')
socket1.end()

// TODO Unfortunately utp has no notion of gracious socket closing
// This feature needs to be shimmed on top to make it a proper libp2p
// transport
socket1.on('error', (err) => {
expect(err).to.not.exist()
})
socket2.on('error', (err) => {
expect(err).to.not.exist()
})
socket1.on('connect', () => {
listener.close(done)
})
socket1.on('error', (err) => expect(err).to.not.exist())
socket2.on('error', (err) => expect(err).to.not.exist())
socket1.on('connect', () => listener.close(done))
})
})

it.skip('listen on port 0', (done) => {
it('.listen on port 0', (done) => {
const listener = utp.createListener((conn) => {})

listener.listen(ma(0), () => {
listener.close(done)
})
})

it.skip('listen on IPv6 addr', function (done) {
// TODO: Get utp to work with IPv6 Addresses
it.skip('.listen on IPv6 addr', function (done) {
if (isCI) { return this.skip() }

const ma = multiaddr('/ip6/::/udp/12000/utp')
Expand All @@ -80,7 +65,7 @@ describe('listen', () => {
})
})

it.skip('listen on any Interface', (done) => {
it('.listen on any Interface', (done) => {
const ma = multiaddr('/ip4/0.0.0.0/udp/12000/utp')

const listener = utp.createListener((conn) => {})
Expand All @@ -90,7 +75,7 @@ describe('listen', () => {
})
})

it.skip('getAddrs', (done) => {
it('.getAddrs', (done) => {
const listener = utp.createListener((conn) => {})
const addr = ma(12000)

Expand All @@ -104,7 +89,7 @@ describe('listen', () => {
})
})

it.skip('getAddrs on port 0 listen', (done) => {
it('.getAddrs on port 0 listen', (done) => {
const addr = ma(0)

const listener = utp.createListener((conn) => {})
Expand All @@ -117,7 +102,8 @@ describe('listen', () => {
})
})

it.skip('getAddrs from listening on 0.0.0.0', (done) => {
// TODO: Get utp to understand the meaning of 0.0.0.0
it.skip('.getAddrs from listening on 0.0.0.0', (done) => {
const addr = multiaddr('/ip4/0.0.0.0/udp/12000/utp')

const listener = utp.createListener((conn) => {})
Expand All @@ -132,7 +118,8 @@ describe('listen', () => {
})
})

it.skip('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
// TODO: Get utp to understand the meaning of 0.0.0.0
it.skip('.getAddrs from listening on 0.0.0.0 and port 0', (done) => {
const addr = multiaddr('/ip4/0.0.0.0/udp/0/utp')
const listener = utp.createListener((conn) => {})

Expand All @@ -146,7 +133,7 @@ describe('listen', () => {
})
})

it.skip('getAddrs preserves IPFS Id', (done) => {
it('.getAddrs preserves IPFS Id', (done) => {
const ipfsId = '/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw'
const addr = ma(9090).encapsulate(ipfsId)

Expand All @@ -156,16 +143,22 @@ describe('listen', () => {
listener.getAddrs((err, multiaddrs) => {
expect(err).to.not.exist()
expect(multiaddrs.length).to.equal(1)
expect(multiaddrs[0]).to.eql(ma)
expect(multiaddrs[0]).to.eql(addr)
listener.close(done)
})
})
})
})

describe('dial', () => {
it.skip('create an instance', () => {
const utp = new UTP()
expect(utp).to.exist()
/*
describe('Dialer (.dial)', () => {
let utp

beforeEach(() => {
utp = new UTP()
})

it.skip('things', () => {
})
})
*/