<commit_before>var debug = require('debug')('mdns:lib:networking');
var debuginbound = require('debug')('mdns:inbound');
var debugoutbound = require('debug')('mdns:outbound');

var util = require('util');
var EventEmitter = require('events').EventEmitter;
var os = require('os');
var dgram = require('dgram');

var dns = require('mdns-js-packet');
var DNSPacket = dns.DNSPacket;
//var DNSRecord = dns.DNSRecord;

var MDNS_MULTICAST = '192.168.3.11';


var Networking = module.exports = function (options) {
  options = options || {};
  this.created = 0;
  this.connections = [];
  this.started = false;
  this.users = [];
  this.INADDR_ANY = typeof options.INADDR_ANY === 'undefined' ?
    false : options.INADDR_ANY;
};

util.inherits(Networking, EventEmitter);


Networking.prototype.start = function () {
  var interfaces = os.networkInterfaces();
  var index = 0;
  for (var key in interfaces) {
    if (interfaces.hasOwnProperty(key)) {
      for (var i = 0; i < interfaces[key].length; i++) {
        var iface = interfaces[key][i];
        //no localhost
        if (iface.internal) {
          continue;
        }
        //no IPv6 addresses
        if (iface.address.indexOf(':') !== -1) {
          continue;
        }
        debug('interface', key, iface.address);
        this.createSocket(index++, key,
          iface.address, 0, this.bindToAddress.bind(this));
      }
    }
  }

  if (this.INADDR_ANY) {
    this.createSocket(index++, 'pseudo multicast',
      '0.0.0.0', 5353, this.bindToAddress.bind(this));
  }
};


Networking.prototype.stop = function () {
  debug('stopping');

  this.connections.forEach(closeEach);
  this.connections = [];

  function closeEach(connection) {
    var socket = connection.socket;
    socket.close();
    socket.unref();
  }
};


Networking.prototype.createSocket = function (
  interfaceIndex, networkInterface, address, port, next) {
  var sock = dgram.createSocket('udp4');
  debug('creating socket for', networkInterface);
  this.created++;

  sock.bind(port, address, function () {
    if (port === 5353) {
      sock.addMembership(MDNS_MULTICAST);
      sock.setMulticastTTL(255);
      sock.setMulticastLoopback(true);
    }
    next(null, interfaceIndex, networkInterface, sock);
  });

};


Networking.prototype.bindToAddress = function (
  err, interfaceIndex, networkInterface, sock) {
  if (err) {
    debug('there was an error binding %s', err);
    return;
  }
  debug('bindToAddress', networkInterface);
  var info = sock.address();

  var connection = {
    socket:sock,
    interfaceIndex: interfaceIndex,
    networkInterface: networkInterface,<commit_msg>add reuseAddr for createSocket with dgram<commit_after>var debug = require('debug')('mdns:lib:networking');
var debuginbound = require('debug')('mdns:inbound');
var debugoutbound = require('debug')('mdns:outbound');

var util = require('util');
var EventEmitter = require('events').EventEmitter;
var os = require('os');
var dgram = require('dgram');

var dns = require('mdns-js-packet');
var DNSPacket = dns.DNSPacket;
//var DNSRecord = dns.DNSRecord;

var MDNS_MULTICAST = '192.168.3.11';


var Networking = module.exports = function (options) {
  options = options || {};
  this.created = 0;
  this.connections = [];
  this.started = false;
  this.users = [];
  this.INADDR_ANY = typeof options.INADDR_ANY === 'undefined' ?
    false : options.INADDR_ANY;
};

util.inherits(Networking, EventEmitter);


Networking.prototype.start = function () {
  var interfaces = os.networkInterfaces();
  var index = 0;
  for (var key in interfaces) {
    if (interfaces.hasOwnProperty(key)) {
      for (var i = 0; i < interfaces[key].length; i++) {
        var iface = interfaces[key][i];
        //no localhost
        if (iface.internal) {
          continue;
        }
        //no IPv6 addresses
        if (iface.address.indexOf(':') !== -1) {
          continue;
        }
        debug('interface', key, iface.address);
        this.createSocket(index++, key,
          iface.address, 0, this.bindToAddress.bind(this));
      }
    }
  }

  if (this.INADDR_ANY) {
    this.createSocket(index++, 'pseudo multicast',
      '0.0.0.0', 5353, this.bindToAddress.bind(this));
  }
};


Networking.prototype.stop = function () {
  debug('stopping');

  this.connections.forEach(closeEach);
  this.connections = [];

  function closeEach(connection) {
    var socket = connection.socket;
    socket.close();
    socket.unref();
  }
};


Networking.prototype.createSocket = function (
  interfaceIndex, networkInterface, address, port, next) {
  var sock = dgram.createSocket({ type: 'udp4', reuseAddr: true });
  debug('creating socket for', networkInterface);
  this.created++;

  sock.bind(port, address, function () {
    if (port === 5353) {
      sock.addMembership(MDNS_MULTICAST);
      sock.setMulticastTTL(255);
      sock.setMulticastLoopback(true);
    }
    next(null, interfaceIndex, networkInterface, sock);
  });

};


Networking.prototype.bindToAddress = function (
  err, interfaceIndex, networkInterface, sock) {
  if (err) {
    debug('there was an error binding %s', err);
    return;
  }
  debug('bindToAddress', networkInterface);
  var info = sock.address();

  var connection = {
    socket:sock,
    interfaceIndex: interfaceIndex,
    networkInterface: networkInterface,
{'fraction_non_alphanumeric': 0.14925654056088838, 'fraction_numerical': 0.011669489930359496, 'mean_word_length': 3.454316848281643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5477711', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1674, 'n_words': 358}