Program Listing for File network.h

Return to documentation for file (liberate/net/network.h)

/*
 * This file is part of liberate.
 *
 * Author(s): Jens Finkhaeuser <jens@finkhaeuser.de>
 *
 * Copyright (c) 2014 Unwesen Ltd.
 * Copyright (c) 2015-2021 Jens Finkhaeuser.
 * Copyright (c) 2022 Interpeer gUG (haftungsbeschränkt)
 *
 * SPDX-License-Identifier: GPL-3.0-only
 *
 * 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 3 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef LIBERATE_NET_NETWORK_H
#define LIBERATE_NET_NETWORK_H

// *** Config
#include <liberate.h>

// *** C++ includes
#include <string>
#include <memory>
#include <stdexcept>

// *** Own includes
#include <liberate/cpp/operators/comparison.h>
#include <liberate/net/address_type.h>
#include <liberate/net/socket_address.h>

namespace liberate::net {

/*****************************************************************************
 * Forward declarations
 **/
class socket_address;


/*****************************************************************************
 * Network
 **/
class LIBERATE_API network
  : public ::liberate::cpp::comparison_operators<network>
{
public:
  explicit network(std::string const & netspec);

  virtual ~network();

  network(network const & other);

  network & operator=(network const & other);


  void reset(std::string const & netspec);


  static bool verify_netspec(std::string const & netspec);


  size_t mask_size() const;


  uint64_t max_size() const;


  address_type family() const;


  bool in_network(socket_address const & address) const;


  socket_address local_address() const;
  socket_address network_address() const;
  socket_address gateway_address() const;
  socket_address broadcast_address() const;

  std::string netspec() const;


  socket_address reserve_address();

  socket_address mapped_address(std::string const & identifier) const;

  socket_address mapped_address(void const * identifier, size_t const & length);

  socket_address reserve_address(std::string const & identifier);

  socket_address reserve_address(void const * identifier, size_t const & length);

  bool reserve_address(socket_address const & addr);

  bool release_address(socket_address const & addr);

  bool is_reserved(std::string const & identifier) const;

  bool is_reserved(void const * identifier, size_t const & length) const;

  bool is_reserved(socket_address const & addr) const;

protected:
  friend struct liberate::cpp::comparison_operators<network>;

  virtual bool is_equal_to(network const & other) const;

  virtual bool is_less_than(network const & other) const;

private:
  // Pointer to implementation
  struct network_impl;
  std::unique_ptr<network_impl> m_impl;

  // Creates a version of the given input address with the netmask applied.
  socket_address make_masked(socket_address const & input) const;

  friend LIBERATE_API_FRIEND std::ostream & operator<<(std::ostream & os, network const & net);
};


LIBERATE_API std::ostream & operator<<(std::ostream & os, network const & net);


} // namespace liberate::net

#endif // guard