Skip to content

Instantly share code, notes, and snippets.

@maxogden

maxogden/index.js

Created May 4, 2018
Embed
What would you like to do?
proxy multicast from one interface to another (node.js)
// proxies 239.255.42.42:5004 from eth0 239.1.2.3:6666 wlan0
var dgram = require('dgram')
var os = require('os')
var socket = dgram.createSocket({ type: 'udp4', reuseAddr: true })
socket.bind(5004, function () {
socket.addMembership('239.255.42.42', getIpForInterface('eth0'))
socket.setMulticastTTL(255)
})
var proxy = dgram.createSocket({type: 'udp4', reuseAddr: true})
proxy.bind(6666, '192.168.1.35', function () {
proxy.addMembership('239.1.2.3', '192.168.1.35')
})
socket.on('message', function (m) {
if (!m.length) return
proxy.send(m, 0, m.length, '6666', '239.1.2.3', function (err) {
if (err) throw err
})
})
function getIpForInterface (ifname) {
return os.networkInterfaces()[ifname].reduce(function (i, j) {
if (i.family === 'IPv4') return i
return j
}).address
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.