ftpiiu_plugin/include/sockAddr.h

171 lines
4.6 KiB
C
Raw Permalink Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2024 Michael Theall
2020-04-05 21:16:16 +02:00
//
// 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 <https://www.gnu.org/licenses/>.
#pragma once
#include <netinet/in.h>
#include <sys/socket.h>
#include <compare>
2020-04-05 21:16:16 +02:00
#include <cstdint>
2020-04-06 07:36:03 +02:00
/// \brief Socket address
2020-04-05 21:16:16 +02:00
class SockAddr
{
public:
enum class Domain
{
IPv4 = AF_INET,
#ifndef NO_IPV6
IPv6 = AF_INET6,
#endif
};
/// \brief 0.0.0.0
static SockAddr const AnyIPv4;
#ifndef NO_IPV6
/// \brief ::
static SockAddr const AnyIPv6;
#endif
2020-04-05 21:16:16 +02:00
~SockAddr ();
SockAddr ();
/// \brief Parameterized constructor
/// \param domain_ Socket domain
/// \note Initial address is INADDR_ANY/in6addr_any
SockAddr (Domain domain_);
/// \brief Parameterized constructor
/// \param addr_ Socket address (network byte order)
/// \param port_ Socket port (host byte order)
SockAddr (in_addr_t addr_, std::uint16_t port_ = 0);
/// \brief Parameterized constructor
/// \param addr_ Socket address (network byte order)
/// \param port_ Socket port (host byte order)
SockAddr (in_addr const &addr_, std::uint16_t port_ = 0);
#ifndef NO_IPV6
/// \brief Parameterized constructor
/// \param addr_ Socket address
/// \param port_ Socket port (host byte order)
SockAddr (in6_addr const &addr_, std::uint16_t port_ = 0);
#endif
2020-04-06 07:36:03 +02:00
/// \brief Copy constructor
/// \param that_ Object to copy
2020-04-05 21:16:16 +02:00
SockAddr (SockAddr const &that_);
2020-04-06 07:36:03 +02:00
/// \brief Move constructor
/// \param that_ Object to move from
2020-04-05 21:16:16 +02:00
SockAddr (SockAddr &&that_);
2020-04-06 07:36:03 +02:00
/// \brief Copy assignment
/// \param that_ Object to copy
2020-04-05 21:16:16 +02:00
SockAddr &operator= (SockAddr const &that_);
2020-04-06 07:36:03 +02:00
/// \brief Move assignment
/// \param that_ Object to move from
2020-04-05 21:16:16 +02:00
SockAddr &operator= (SockAddr &&that_);
/// \brief Parameterized constructor
/// \param addr_ Address (network byte order)
SockAddr (sockaddr_in const &addr_);
2020-04-05 21:16:16 +02:00
#ifndef NO_IPV6
/// \brief Parameterized constructor
/// \param addr_ Address (network byte order)
SockAddr (sockaddr_in6 const &addr_);
2020-04-05 21:16:16 +02:00
#endif
/// \brief Parameterized constructor
/// \param addr_ Address (network byte order)
SockAddr (sockaddr_storage const &addr_);
2020-04-05 21:16:16 +02:00
/// \brief sockaddr_in cast operator (network byte order)
operator sockaddr_in const & () const;
2020-04-05 21:16:16 +02:00
#ifndef NO_IPV6
/// \brief sockaddr_in6 cast operator (network byte order)
operator sockaddr_in6 const & () const;
2020-04-05 21:16:16 +02:00
#endif
/// \brief sockaddr_storage cast operator (network byte order)
operator sockaddr_storage const & () const;
/// \brief sockaddr* cast operator (network byte order)
operator sockaddr * ();
/// \brief sockaddr const* cast operator (network byte order)
operator sockaddr const * () const;
/// \brief Equality operator
bool operator== (SockAddr const &that_) const;
/// \brief Comparison operator
std::strong_ordering operator<=> (SockAddr const &that_) const;
2020-04-05 21:16:16 +02:00
/// \brief sockaddr domain
Domain domain () const;
/// \brief sockaddr size
socklen_t size () const;
/// \brief Set address
/// \param addr_ Address to set (network byte order)
void setAddr (in_addr_t addr_);
/// \brief Set address
/// \param addr_ Address to set (network byte order)
void setAddr (in_addr const &addr_);
#ifndef NO_IPV6
/// \brief Set address
/// \param addr_ Address to set (network byte order)
void setAddr (in6_addr const &addr_);
#endif
2020-04-05 21:16:16 +02:00
/// \brief Address port (host byte order)
2020-04-05 21:16:16 +02:00
std::uint16_t port () const;
2020-04-06 07:36:03 +02:00
2020-12-12 02:15:33 +01:00
/// \brief Set address port
/// \param port_ Port to set (host byte order)
void setPort (std::uint16_t port_);
2020-12-12 02:15:33 +01:00
2020-04-06 07:36:03 +02:00
/// \brief Address name
2020-12-12 02:15:33 +01:00
/// \param buffer_ Buffer to hold name
2020-04-06 07:36:03 +02:00
/// \param size_ Size of buffer_
/// \retval buffer_ success
/// \retval nullptr failure
2020-04-05 21:16:16 +02:00
char const *name (char *buffer_, std::size_t size_) const;
2020-04-06 07:36:03 +02:00
/// \brief Address name
/// \retval nullptr failure
/// \note This function is not reentrant
2020-04-05 21:16:16 +02:00
char const *name () const;
private:
/// \brief Address storage (network byte order)
sockaddr_storage m_addr = {};
2020-04-05 21:16:16 +02:00
};