<commit_before>/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * SLPServer.cpp
 * Copyright (C) 2012 <NAME>
 */

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <ola/BaseTypes.h>
#include <ola/Callback.h>
#include <ola/Logging.h>
#include <ola/StringUtils.h>
#include <ola/io/BigEndianStream.h>
#include <ola/io/MemoryBuffer.h>
#include <ola/io/SelectServer.h>
#include <ola/math/Random.h>
#include <ola/network/IPV4Address.h>
#include <ola/network/NetworkUtils.h>
#include <ola/network/Socket.h>
#include <ola/network/SocketAddress.h>
#include <ola/network/TCPSocketFactory.h>
#include <ola/stl/STLUtils.h>

#include <algorithm>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#include "tools/slp/ServerCommon.h"
#include "tools/slp/SLPPacketBuilder.h"
#include "tools/slp/SLPPacketParser.h"
#include "tools/slp/SLPServer.h"
#include "tools/slp/SLPStore.h"
#include "tools/slp/SLPStrings.h"
#include "tools/slp/SLPUtil.h"

namespace ola {
namespace slp {

using ola::NewCallback;
using ola::NewSingleCallback;
using ola::ToUpper;
using ola::io::BigEndianInputStream;
using ola::io::BigEndianOutputStream;
using ola::io::MemoryBuffer;
using ola::math::Random;
using ola::network::HostToNetwork;
using ola::network::IPV4Address;
using ola::network::IPV4SocketAddress;
using ola::network::NetworkToHost;
using ola::network::TCPAcceptingSocket;
using ola::network::TCPSocket;
using ola::network::UDPSocket;
using std::auto_ptr;
using std::ostringstream;
using std::string;
using std::vector;


const char SLPServer::DAADVERT[] = "DAAdvert";
const char SLPServer::DEREGSRVS_ERROR_COUNT_VAR[] = "slp-dereg-srv-errors";
const char SLPServer::FINDSRVS_EMPTY_COUNT_VAR[] =
  "slp-find-srvs-empty-response";
const char SLPServer::METHOD_CALLS_VAR[] = "slp-server-methods";
const char SLPServer::METHOD_DEREG_SERVICE[] = "DeRegisterService";
const char SLPServer::METHOD_FIND_SERVICE[] = "FindService";
const char SLPServer::METHOD_REG_SERVICE[] = "RegisterService";
const char SLPServer::REGSRVS_ERROR_COUNT_VAR[] = "slp-reg-srv-errors";
const char SLPServer::SLP_PORT_VAR[] = "slp-port";
const char SLPServer::SRVACK[] = "SrvAck";
const char SLPServer::SRVREG[] = "SrvReg";
const char SLPServer::SRVRPLY[] = "SrvRply";
const char SLPServer::SRVRQST[] = "SrvRqst";
// This counter tracks the number of packets received by type.
// This is incremented prior to packet checks.
const char SLPServer::UDP_RX_PACKET_BY_TYPE_VAR[] = "slp-udp-rx-packets";
// The total number of recieved SLP UDP packets
const char SLPServer::UDP_RX_TOTAL_VAR[] = "slp-udp-rx";
const char SLPServer::UDP_TX_PACKET_BY_TYPE_VAR[] = "slp-udp-tx-packets";


/**
 * Setup a new SLP server.
 * @param ss the SelectServer to use
 * @param udp_socket the socket to use for UDP SLP traffic
 * @param tcp_socket the TCP socket to listen for incoming TCP SLP connections.
 * @param export_map the ExportMap to use for exporting variables, may be NULL
 * @param options the SLP Server options.
 */
SLPServer::SLPServer(ola::io::SelectServerInterface *ss,
                     ola::network::UDPSocket *udp_socket,
                     ola::network::TCPAcceptingSocket *tcp_socket,
                     ola::ExportMap *export_map,
                     const SLPServerOptions &options)
    : m_enable_da(options.enable_da),
      m_slp_port(options.slp_port),
      m_config_da_beat(options.config_da_beat * ONE_THOUSAND),
      m_config_da_find(options.config_da_find * ONE_THOUSAND),
      m_config_mc_max(options.config_mc_max * ONE_THOUSAND),
      m_config_retry(options.config_retry * ONE_THOUSAND),
      m_config_retry_max(options.config_retry_max * ONE_THOUSAND),
      m_config_start_wait(options.config_start_wait * ONE_THOUSAND),
      m_en_lang(reinterpret_cast<const char*>(EN_LANGUAGE_TAG),
                sizeof(EN_LANGUAGE_TAG)),
      m_iface_address(options.ip_address),
      m_multicast_endpoint(IPV4SocketAddress(
            IPV4Address(HostToNetwork(SLP_MULTICAST_ADDRESS)), m_slp_port)),
      m_ss(ss),
      m_da_beat_timer(ola::thread::INVALID_TIMEOUT),
      m_store_cleaner_timer(ola::thread::INVALID_TIMEOUT),
      m_active_da_discovery_timer(ola::thread::INVALID_TIMEOUT),
      m_udp_socket(udp_socket),
      m_slp_accept_socket(tcp_socket),
      m_udp_sender(m_udp_socket),
      m_configured_scopes(options.scopes),
      m_xid_allocator(Random(0, UINT16_MAX)),
      m_export_map(export_map) {
  ToLower(&m_en_lang);

  if (m_configured_scopes.empty())
    m_configured_scopes = ScopeSet(DEFAULT_SLP_SCOPE);

  export_map->GetBoolVar("slp-da-enabled")->Set(options.enable_da);
  export_map->GetIntegerVar("slp-config-da-beat")->Set(options.config_da_beat);
  export_map->GetIntegerVar("slp-config-da-find")->Set(options.config_da_find);
  export_map->GetIntegerVar("slp-config-mc-max")->Set(options.config_mc_max);
  export_map->GetIntegerVar("slp-port")->Set(options.slp_port);
  export_map->GetIntegerVar(FINDSRVS_EMPTY_COUNT_VAR);
  export_map->GetIntegerVar(UDP_RX_TOTAL_VAR);
  export_map->GetStringVar("slp-scope-list")->Set(
      m_configured_scopes.ToString());
  export_map->GetUIntMapVar(UDP_RX_PACKET_BY_TYPE_VAR, "type");
  export_map->GetUIntMapVar(METHOD_CALLS_VAR, "method");
}


SLPServer::~SLPServer() {
  m_ss->RemoveTimeout(m_da_beat_timer);
  m_ss->RemoveTimeout(m_store_cleaner_timer);
  m_ss->RemoveTimeout(m_active_da_discovery_timer);

  for (PendingOperationsByURL::iterator iter = m_pending_ops.begin();
       iter != m_pending_ops.end(); ++iter) {
    m_ss->RemoveTimeout(iter->second->timer_id);
    delete iter->second;
  }

  m_udp_socket->Close();
  STLDeleteValues(m_pending_acks);<commit_msg> * export some of the other configs vars<commit_after>/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * SLPServer.cpp
 * Copyright (C) 2012 <NAME>
 */

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <ola/BaseTypes.h>
#include <ola/Callback.h>
#include <ola/Logging.h>
#include <ola/StringUtils.h>
#include <ola/io/BigEndianStream.h>
#include <ola/io/MemoryBuffer.h>
#include <ola/io/SelectServer.h>
#include <ola/math/Random.h>
#include <ola/network/IPV4Address.h>
#include <ola/network/NetworkUtils.h>
#include <ola/network/Socket.h>
#include <ola/network/SocketAddress.h>
#include <ola/network/TCPSocketFactory.h>
#include <ola/stl/STLUtils.h>

#include <algorithm>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#include "tools/slp/ServerCommon.h"
#include "tools/slp/SLPPacketBuilder.h"
#include "tools/slp/SLPPacketParser.h"
#include "tools/slp/SLPServer.h"
#include "tools/slp/SLPStore.h"
#include "tools/slp/SLPStrings.h"
#include "tools/slp/SLPUtil.h"

namespace ola {
namespace slp {

using ola::NewCallback;
using ola::NewSingleCallback;
using ola::ToUpper;
using ola::io::BigEndianInputStream;
using ola::io::BigEndianOutputStream;
using ola::io::MemoryBuffer;
using ola::math::Random;
using ola::network::HostToNetwork;
using ola::network::IPV4Address;
using ola::network::IPV4SocketAddress;
using ola::network::NetworkToHost;
using ola::network::TCPAcceptingSocket;
using ola::network::TCPSocket;
using ola::network::UDPSocket;
using std::auto_ptr;
using std::ostringstream;
using std::string;
using std::vector;


const char SLPServer::DAADVERT[] = "DAAdvert";
const char SLPServer::DEREGSRVS_ERROR_COUNT_VAR[] = "slp-dereg-srv-errors";
const char SLPServer::FINDSRVS_EMPTY_COUNT_VAR[] =
  "slp-find-srvs-empty-response";
const char SLPServer::METHOD_CALLS_VAR[] = "slp-server-methods";
const char SLPServer::METHOD_DEREG_SERVICE[] = "DeRegisterService";
const char SLPServer::METHOD_FIND_SERVICE[] = "FindService";
const char SLPServer::METHOD_REG_SERVICE[] = "RegisterService";
const char SLPServer::REGSRVS_ERROR_COUNT_VAR[] = "slp-reg-srv-errors";
const char SLPServer::SLP_PORT_VAR[] = "slp-port";
const char SLPServer::SRVACK[] = "SrvAck";
const char SLPServer::SRVREG[] = "SrvReg";
const char SLPServer::SRVRPLY[] = "SrvRply";
const char SLPServer::SRVRQST[] = "SrvRqst";
// This counter tracks the number of packets received by type.
// This is incremented prior to packet checks.
const char SLPServer::UDP_RX_PACKET_BY_TYPE_VAR[] = "slp-udp-rx-packets";
// The total number of recieved SLP UDP packets
const char SLPServer::UDP_RX_TOTAL_VAR[] = "slp-udp-rx";
const char SLPServer::UDP_TX_PACKET_BY_TYPE_VAR[] = "slp-udp-tx-packets";


/**
 * Setup a new SLP server.
 * @param ss the SelectServer to use
 * @param udp_socket the socket to use for UDP SLP traffic
 * @param tcp_socket the TCP socket to listen for incoming TCP SLP connections.
 * @param export_map the ExportMap to use for exporting variables, may be NULL
 * @param options the SLP Server options.
 */
SLPServer::SLPServer(ola::io::SelectServerInterface *ss,
                     ola::network::UDPSocket *udp_socket,
                     ola::network::TCPAcceptingSocket *tcp_socket,
                     ola::ExportMap *export_map,
                     const SLPServerOptions &options)
    : m_enable_da(options.enable_da),
      m_slp_port(options.slp_port),
      m_config_da_beat(options.config_da_beat * ONE_THOUSAND),
      m_config_da_find(options.config_da_find * ONE_THOUSAND),
      m_config_mc_max(options.config_mc_max * ONE_THOUSAND),
      m_config_retry(options.config_retry * ONE_THOUSAND),
      m_config_retry_max(options.config_retry_max * ONE_THOUSAND),
      m_config_start_wait(options.config_start_wait * ONE_THOUSAND),
      m_en_lang(reinterpret_cast<const char*>(EN_LANGUAGE_TAG),
                sizeof(EN_LANGUAGE_TAG)),
      m_iface_address(options.ip_address),
      m_multicast_endpoint(IPV4SocketAddress(
            IPV4Address(HostToNetwork(SLP_MULTICAST_ADDRESS)), m_slp_port)),
      m_ss(ss),
      m_da_beat_timer(ola::thread::INVALID_TIMEOUT),
      m_store_cleaner_timer(ola::thread::INVALID_TIMEOUT),
      m_active_da_discovery_timer(ola::thread::INVALID_TIMEOUT),
      m_udp_socket(udp_socket),
      m_slp_accept_socket(tcp_socket),
      m_udp_sender(m_udp_socket),
      m_configured_scopes(options.scopes),
      m_xid_allocator(Random(0, UINT16_MAX)),
      m_export_map(export_map) {
  ToLower(&m_en_lang);

  if (m_configured_scopes.empty())
    m_configured_scopes = ScopeSet(DEFAULT_SLP_SCOPE);

  export_map->GetBoolVar("slp-da-enabled")->Set(options.enable_da);
  export_map->GetIntegerVar("slp-config-da-beat")->Set(options.config_da_beat);
  export_map->GetIntegerVar("slp-config-da-find")->Set(options.config_da_find);
  export_map->GetIntegerVar("slp-config-mc-max")->Set(options.config_mc_max);
  export_map->GetIntegerVar("slp-config-retry")->Set(options.config_retry);
  export_map->GetIntegerVar("slp-config-retry-max")->Set(
    options.config_retry_max);
  export_map->GetIntegerVar("slp-config-start_wait")->Set(
    options.config_start_wait);
  export_map->GetIntegerVar("slp-port")->Set(options.slp_port);
  export_map->GetIntegerVar(FINDSRVS_EMPTY_COUNT_VAR);
  export_map->GetIntegerVar(UDP_RX_TOTAL_VAR);
  export_map->GetStringVar("slp-scope-list")->Set(
      m_configured_scopes.ToString());
  export_map->GetUIntMapVar(UDP_RX_PACKET_BY_TYPE_VAR, "type");
  export_map->GetUIntMapVar(METHOD_CALLS_VAR, "method");
}


SLPServer::~SLPServer() {
  m_ss->RemoveTimeout(m_da_beat_timer);
  m_ss->RemoveTimeout(m_store_cleaner_timer);
  m_ss->RemoveTimeout(m_active_da_discovery_timer);

  for (PendingOperationsByURL::iterator iter = m_pending_ops.begin();
       iter != m_pending_ops.end(); ++iter) {
    m_ss->RemoveTimeout(iter->second->timer_id);
    delete iter->second;
  }

  m_udp_socket->Close();
  STLDeleteValues(m_pending_acks);
{'fraction_non_alphanumeric': 0.12277867528271405, 'fraction_numerical': 0.004154165705054235, 'mean_word_length': 5.274131274131274, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4405249', 'n_tokens_mistral': 4605, 'n_tokens_neox': 4554, 'n_words': 883}