From 0e35d1261517b380a4f576943aafd7fc079b89a5 Mon Sep 17 00:00:00 2001 From: David Vultur Date: Wed, 17 Jun 2020 17:18:33 +0300 Subject: [PATCH] Fixed upd4/udp6 socket type --- lib/server.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/server.js b/lib/server.js index fd59f52..987e608 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,5 +1,6 @@ var assert = require('assert'); var dgram = require('dgram'); +var net = require('net'); var EventEmitter = require('events').EventEmitter; var util = require('util'); @@ -47,10 +48,17 @@ Server.prototype.listen = function listen(port, address, callback) { callback = address; address = '0.0.0.0'; } + + var address_type = net.isIP(address); + + if(!address_type) + throw new TypeError('ip address is invalid'); + + var socket_type = address_type == 4 ? 'udp4' : 'udp6' var self = this; - this._socket = dgram.createSocket('udp6'); + this._socket = dgram.createSocket(socket_type); this._socket.once('listening', function () { self.emit('listening'); if (typeof (callback) === 'function') @@ -71,7 +79,7 @@ Server.prototype.listen = function listen(port, address, callback) { }; var src = { - family: 'udp6', + family: socket_type, address: rinfo.address, port: rinfo.port };