Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from diasdavid/feature/observed-addrs
Browse files Browse the repository at this point in the history
add getObservedAddrs feature to every returned conn
  • Loading branch information
daviddias committed Mar 10, 2016
2 parents 3d17d40 + 77600d3 commit 4d34757
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"tape": "^4.2.0"
},
"dependencies": {
"ip-address": "^5.8.0",
"multiaddr": "^1.0.0"
}
}
31 changes: 29 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// const log = debug('libp2p:tcp')
const tcp = require('net')
const multiaddr = require('multiaddr')
const Address6 = require('ip-address').Address6

exports = module.exports = TCP

Expand All @@ -17,7 +18,11 @@ function TCP () {
options = {}
}
options.ready = options.ready || function noop () {}
return tcp.connect(multiaddr.toOptions(), options.ready)
const conn = tcp.connect(multiaddr.toOptions(), options.ready)
conn.getObservedAddrs = () => {
return [multiaddr]
}
return conn
}

this.createListener = (multiaddrs, options, handler, callback) => {
Expand All @@ -35,7 +40,12 @@ function TCP () {
const freshMultiaddrs = []

multiaddrs.forEach((m) => {
const listener = tcp.createServer(handler)
const listener = tcp.createServer((conn) => {
conn.getObservedAddrs = () => {
return [getMultiaddr(conn)]
}
handler(conn)
})
listener.listen(m.toOptions(), () => {
// Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g)
const address = listener.address()
Expand Down Expand Up @@ -71,3 +81,20 @@ function TCP () {
}
}

function getMultiaddr (conn) {
var mh

if (conn.remoteFamily === 'IPv6') {
var addr = new Address6(conn.remoteAddress)
if (addr.v4) {
var ip4 = addr.to4().correctForm()
mh = multiaddr('/ip4/' + ip4 + '/tcp/' + conn.remotePort)
} else {
mh = multiaddr('/ip6/' + conn.remoteAddress + '/tcp/' + conn.remotePort)
}
} else {
mh = multiaddr('/ip4/' + conn.remoteAddress + '/tcp/' + conn.remotePort)
}

return mh
}
24 changes: 24 additions & 0 deletions tests/libp2p-tcp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,29 @@ describe('libp2p-tcp', function () {
})
})

it('get observed addrs', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
var dialerObsAddrs
var listenerObsAddrs

tcp.createListener(mh, (conn) => {
expect(conn).to.exist
dialerObsAddrs = conn.getObservedAddrs()
conn.end()
}, () => {
const conn = tcp.dial(mh)
conn.on('end', () => {
listenerObsAddrs = conn.getObservedAddrs()
conn.end()

tcp.close(() => {
expect(listenerObsAddrs[0]).to.deep.equal(mh)
expect(dialerObsAddrs.length).to.equal(1)
done()
})
})
})
})

it.skip('listen on IPv6', (done) => {})
})

0 comments on commit 4d34757

Please sign in to comment.