Program Listing for File socket_address.h
↰ Return to documentation for file (liberate/net/socket_address.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_SOCKET_ADDRESS_H
#define LIBERATE_NET_SOCKET_ADDRESS_H
// *** Config
#include <liberate.h>
// *** C++ includes
#include <iostream>
#include <string>
#include <stdexcept>
#include <cstddef>
#include <memory>
// *** Own includes
#include <liberate/cpp/operators/comparison.h>
#include <liberate/net/address_type.h>
#include <liberate/types/byte.h>
namespace liberate::net {
/*****************************************************************************
* Forward declarations
**/
class network;
/*****************************************************************************
* Socket address
**/
class LIBERATE_API socket_address
: public ::liberate::cpp::comparison_operators<socket_address>
{
public:
socket_address();
inline socket_address(socket_address && other) = default;
socket_address(socket_address const & other);
socket_address & operator=(socket_address const & other);
virtual ~socket_address() = default;
socket_address(void const * buf, size_t len);
socket_address(address_type type, void const * buf, size_t len,
uint16_t port = 0);
explicit socket_address(std::string const & address, uint16_t port = 0);
explicit socket_address(char const * address, uint16_t port = 0, size_t size = 0);
static bool verify_cidr(std::string const & address);
bool verify_netmask(size_t const & netmask) const;
size_t max_netmask() const;
std::string cidr_str() const;
uint16_t port() const;
address_type type() const;
std::string full_str() const;
size_t bufsize() const;
inline static constexpr size_t bufsize_available()
{
// See https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms740504(v=vs.85)
// Other socket address types, e.g. sa_un are defined to be slightly
// smaller; this should always be the maximum size in actual use, plus
// some optional padding.
return 128;
}
void const * buffer() const;
void * buffer();
size_t min_bufsize(bool with_type = true, bool with_port = true) const;
size_t serialize(void * buf, size_t len, bool with_type = true, bool with_port = true) const;
static std::tuple<size_t, socket_address>
deserialize(void const * buf, size_t len, bool with_port = true);
static std::tuple<size_t, socket_address>
deserialize(address_type type, void const * buf, size_t len,
bool with_port = true);
bool set_port(uint16_t port);
bool is_any() const;
bool is_loopback() const;
void swap(socket_address & other);
size_t hash() const;
void operator++();
bool is_equal_to(socket_address const & other) const;
bool is_less_than(socket_address const & other) const;
private:
struct socket_address_impl;
std::shared_ptr<socket_address_impl> m_impl;
friend LIBERATE_API_FRIEND std::ostream & operator<<(std::ostream & os, socket_address const & addr);
friend class LIBERATE_API_FRIEND network;
};
LIBERATE_API std::ostream & operator<<(std::ostream & os, socket_address const & addr);
inline void
swap(socket_address & first, socket_address & second)
{
return first.swap(second);
}
} // namespace liberate::net
/*******************************************************************************
* std namespace specializations
**/
namespace std {
template <> struct LIBERATE_API hash<liberate::net::socket_address>
{
size_t operator()(liberate::net::socket_address const & x) const
{
return x.hash();
}
};
} // namespace std
#endif // guard