From 92fc20d9c21b578cc1b50d544eefc179076f955d Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Sat, 23 Apr 2022 22:44:37 +0200 Subject: [PATCH] fix: fix IPv6 related multicast problem (#339) * 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 --- lib/server.ts | 6 +++++- package.json | 2 +- test/server.ts | 49 +++++++++++++++++++++++++++++++++++-------------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/lib/server.ts b/lib/server.ts index 51678054..65743e68 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -259,7 +259,7 @@ class CoAPServer extends EventEmitter { multicastAddress, this._multicastInterface ) - } else { + } else if (this._options.type === 'udp4') { allAddresses(this._options.type).forEach(( _interface ) => { @@ -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) { diff --git a/package.json b/package.json index 512c7d07..783e676d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/server.ts b/test/server.ts index 0a422a85..1a9f1f89 100644 --- a/test/server.ts +++ b/test/server.ts @@ -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') @@ -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 = [ + { + 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() + }) }) }) })