<commit_before>/*    Copyright 2016-2020 Firewalla Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
'use strict';
const _ = require('lodash');

const log = require('../net2/logger.js')(__filename, 'info');

const Hook = require('./Hook.js');

const sem = require('../sensor/SensorEventManager.js').getInstance();

const HostTool = require('../net2/HostTool.js');
const hostTool = new HostTool();

const Promise = require('bluebird');

const extend = require('../util/util.js').extend;
const util = require('util');
const bone = require("../lib/Bone.js");

const flowUtil = require("../net2/FlowUtil.js");

const fc = require('../net2/config.js')

const Samba = require('../extension/samba/samba.js');
const samba = new Samba();
const DNSMASQ = require('../extension/dnsmasq/dnsmasq.js');
const dnsmasq = new DNSMASQ();


const HostManager = require('../net2/HostManager.js');

const sysManager = require('../net2/SysManager.js');

const l2 = require('../util/Layer2.js');

const MAX_IPV6_ADDRESSES = 10
const MAX_LINKLOCAL_IPV6_ADDRESSES = 3
const MessageBus = require('../net2/MessageBus.js');

class DeviceHook extends Hook {
  constructor() {
    super();
    this.messageBus = new MessageBus('info');
  }

  async processDeviceUpdate(event) {
    let host = event.host
    let mac = host.mac
    let ipv4Addr = host.ipv4Addr
    let ipv6Addr = host.ipv6Addr

    if (!mac) { // ignore if no mac
      log.warn("Invalid MAC address for process device update:", event);
      return;
    }

    let monInterfaces = await sysManager.getMonitoringInterfaces();
    let foundInterface = Object.keys(monInterfaces).find(e => e.bcast_address === ipv4Addr)
    if (foundInterface) {
      log.warn(`Ignore IP address ${ipv4Addr} as broadcast address of interface ${foundInterface}:`, event);
      return;
    }

    mac = mac.toUpperCase()
    host.mac = mac // make sure the MAC is upper case

    try {

      // 0. update a special name key for source
      if (host.from) {
        let skey = `${host.from}Name`
        host[skey] = host.bname
        host.lastFrom = host.from
        delete host.from
      }

      // 1. if this is a brand new mac address => NewDeviceFound
      let found = await hostTool.macExists(mac)<commit_msg>filter out IPv4 broadcast address for any monitoring interface<commit_after>/*    Copyright 2016-2020 Firewalla Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
'use strict';
const _ = require('lodash');

const log = require('../net2/logger.js')(__filename, 'info');

const Hook = require('./Hook.js');

const sem = require('../sensor/SensorEventManager.js').getInstance();

const HostTool = require('../net2/HostTool.js');
const hostTool = new HostTool();

const Promise = require('bluebird');

const extend = require('../util/util.js').extend;
const util = require('util');
const bone = require("../lib/Bone.js");

const flowUtil = require("../net2/FlowUtil.js");

const fc = require('../net2/config.js')

const Samba = require('../extension/samba/samba.js');
const samba = new Samba();
const DNSMASQ = require('../extension/dnsmasq/dnsmasq.js');
const dnsmasq = new DNSMASQ();


const HostManager = require('../net2/HostManager.js');

const sysManager = require('../net2/SysManager.js');

const l2 = require('../util/Layer2.js');

const MAX_IPV6_ADDRESSES = 10
const MAX_LINKLOCAL_IPV6_ADDRESSES = 3
const MessageBus = require('../net2/MessageBus.js');

class DeviceHook extends Hook {
  constructor() {
    super();
    this.messageBus = new MessageBus('info');
  }

  async processDeviceUpdate(event) {
    let host = event.host
    let mac = host.mac
    let ipv4Addr = host.ipv4Addr
    let ipv6Addr = host.ipv6Addr

    if (!mac) { // ignore if no mac
      log.warn("Invalid MAC address for process device update:", event);
      return;
    }

    /*
     * Filter out IPv4 broadcast address for any monitoring interface
     */
    if (ipv4Addr) {
      let monInterfaces = sysManager.getMonitoringInterfaces();
      let foundInterface = monInterfaces.find(e => e.bcast_address === ipv4Addr)
      if (foundInterface) {
        log.warn(`Ignore IP address ${ipv4Addr} as broadcast address of interface ${foundInterface.name}:`, event);
        return;
      }
    }

    mac = mac.toUpperCase()
    host.mac = mac // make sure the MAC is upper case

    try {

      // 0. update a special name key for source
      if (host.from) {
        let skey = `${host.from}Name`
        host[skey] = host.bname
        host.lastFrom = host.from
        delete host.from
      }

      // 1. if this is a brand new mac address => NewDeviceFound
      let found = await hostTool.macExists(mac)
{'fraction_non_alphanumeric': 0.12452765372724149, 'fraction_numerical': 0.011164548265200962, 'mean_word_length': 3.118104667609618, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '3696949', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1721, 'n_words': 563}