Skip to content

Commit 4da0f3b

Browse files
committed
Listen over IPv6 for UDP
Fixes webtorrent#73
1 parent eada21b commit 4da0f3b

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

server.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function Server (opts) {
5252
self.torrents = {}
5353

5454
self.http = null
55-
self.udp = null
55+
self.udp4 = null
56+
self.udp6 = null
5657
self.ws = null
5758

5859
// start an http tracker unless the user explictly says no
@@ -65,10 +66,15 @@ function Server (opts) {
6566

6667
// start a udp tracker unless the user explicitly says no
6768
if (opts.udp !== false) {
68-
self.udp = dgram.createSocket('udp4')
69-
self.udp.on('message', self.onUdpRequest.bind(self))
70-
self.udp.on('error', self._onError.bind(self))
71-
self.udp.on('listening', onListening)
69+
self.udp4 = self.udp = dgram.createSocket('udp4')
70+
self.udp4.on('message', self.onUdpRequest.bind(self))
71+
self.udp4.on('error', self._onError.bind(self))
72+
self.udp4.on('listening', onListening)
73+
74+
self.udp6 = dgram.createSocket('udp6')
75+
self.udp6.on('message', self.onUdpRequest.bind(self))
76+
self.udp6.on('error', self._onError.bind(self))
77+
self.udp6.on('listening', onListening)
7278
}
7379

7480
// start a websocket tracker (for WebTorrent) unless the user explicitly says no
@@ -83,7 +89,7 @@ function Server (opts) {
8389
self.ws.on('connection', self.onWebSocketConnection.bind(self))
8490
}
8591

86-
var num = !!(self.http || self.ws) + !!self.udp
92+
var num = !!self.http + !!self.udp4 + !!self.udp6
8793
function onListening () {
8894
num -= 1
8995
if (num === 0) {
@@ -115,8 +121,9 @@ Server.prototype.listen = function (/* port, hostname, onlistening */) {
115121
// ATTENTION:
116122
// binding to :: only receives IPv4 connections if the bindv6only
117123
// sysctl is set 0, which is the default on many operating systems.
118-
self.http && self.http.listen(port.http || port, hostname || '::')
119-
self.udp && self.udp.bind(port.udp || port, hostname)
124+
if (self.http) self.http.listen(port.http || port, hostname || '::')
125+
if (self.udp4) self.udp4.bind(port.udp || port, hostname)
126+
if (self.udp6) self.udp6.bind(port.udp || port, hostname)
120127
}
121128

122129
Server.prototype.close = function (cb) {
@@ -126,9 +133,15 @@ Server.prototype.close = function (cb) {
126133

127134
self.listening = false
128135

129-
if (self.udp) {
136+
if (self.udp4) {
130137
try {
131-
self.udp.close()
138+
self.udp4.close()
139+
} catch (err) {}
140+
}
141+
142+
if (self.udp6) {
143+
try {
144+
self.udp6.close()
132145
} catch (err) {}
133146
}
134147

@@ -222,7 +235,8 @@ Server.prototype.onUdpRequest = function (msg, rinfo) {
222235
var buf = makeUdpPacket(response)
223236

224237
try {
225-
self.udp.send(buf, 0, buf.length, rinfo.port, rinfo.address)
238+
var udp = (rinfo.family === 'IPv4') ? self.udp4 : self.udp6
239+
udp.send(buf, 0, buf.length, rinfo.port, rinfo.address)
226240
} catch (err) {
227241
self.emit('warning', err)
228242
}

0 commit comments

Comments
 (0)