Skip to content

Commit

Permalink
fix: fix IPv6 related multicast problem (#339)
Browse files Browse the repository at this point in the history
* test(server): add IPv6 to multicast test

* fix: add workaround for IPv6 multicast addresses

* test: skip IPv6 multicast test on macos

* chore: bump version to 1.0.7
  • Loading branch information
JKRhb committed Apr 23, 2022
1 parent a1e25a0 commit 92fc20d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class CoAPServer extends EventEmitter {
multicastAddress,
this._multicastInterface
)
} else {
} else if (this._options.type === 'udp4') {
allAddresses(this._options.type).forEach((
_interface
) => {
Expand All @@ -268,6 +268,10 @@ class CoAPServer extends EventEmitter {
_interface
)
})
} else {
// FIXME: Iterating over all network interfaces does not
// work for IPv6 at the moment
sock.addMembership(multicastAddress)
}
}
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coap",
"version": "1.0.6",
"version": "1.0.7",
"description": "A CoAP library for node modelled after 'http'",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
49 changes: 35 additions & 14 deletions test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { parse, generate } from 'coap-packet'
import { nextPort } from './common'
import { expect } from 'chai'
import { CoapPacket, Option } from '../models/models'
import { CoapPacket, CoapServerOptions, Option } from '../models/models'
import { request, createServer } from '../index'
import { createSocket } from 'dgram'
import BufferListStream = require('bl')
Expand Down Expand Up @@ -1000,22 +1000,43 @@ describe('server', function () {
describe('multicast', function () {
const port = nextPort()

it('receive CoAP message', function (done) {
const server = createServer({
multicastAddress: '224.0.1.2'
})
const testVector: Array<CoapServerOptions & {addressType: string}> = [
{
addressType: 'IPv4',
multicastAddress: '224.0.1.2',
type: 'udp4'
},
{
addressType: 'IPv6',
multicastAddress: 'ff02::fd',
type: 'udp6'
}]

server.listen(port)
testVector.forEach(({ addressType, multicastAddress, type }) => {
it(`receive ${addressType} CoAP message`, function (done) {
if (addressType === 'IPv6' && process.platform === 'darwin') {
// FIXME: IPv6 multicast seems to have problems on macos
// at the moment
this.skip()
}

server.once('request', (req, res) => {
done()
})
const server = createServer({
multicastAddress,
type
})

request({
host: '224.0.1.2',
port: port,
multicast: true
}).end()
server.listen(port)

server.once('request', (req, res) => {
done()
})

request({
host: multicastAddress,
port: port,
multicast: true
}).end()
})
})
})
})
Expand Down

0 comments on commit 92fc20d

Please sign in to comment.