| 1 | /************************************************************************************ |
| 2 | * |
| 3 | * D++, A Lightweight C++ library for Discord |
| 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * Copyright 2021 Craig Edwards and D++ contributors |
| 7 | * (https://github.com/brainboxdotcc/DPP/graphs/contributors) |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | ************************************************************************************/ |
| 22 | #pragma once |
| 23 | |
| 24 | #include <dpp/export.h> |
| 25 | #include <dpp/compat.h> |
| 26 | |
| 27 | #include <string_view> |
| 28 | #include <cstdint> |
| 29 | |
| 30 | |
| 31 | namespace dpp |
| 32 | { |
| 33 | /** |
| 34 | * @brief Represents a socket file descriptor. |
| 35 | * This is used to ensure parity between windows and unix-like systems. |
| 36 | */ |
| 37 | #ifndef _WIN32 |
| 38 | using socket = int; |
| 39 | #else |
| 40 | using socket = SOCKET; |
| 41 | #endif |
| 42 | |
| 43 | #ifndef SOCKET_ERROR |
| 44 | /** |
| 45 | * @brief Represents a socket in error state |
| 46 | */ |
| 47 | #define SOCKET_ERROR -1 |
| 48 | #endif |
| 49 | |
| 50 | #ifndef INVALID_SOCKET |
| 51 | /** |
| 52 | * @brief Represents a socket which is not yet assigned |
| 53 | */ |
| 54 | #define INVALID_SOCKET ~0 |
| 55 | #endif |
| 56 | |
| 57 | /** |
| 58 | * @brief Represents an IPv4 address for use with socket functions such as |
| 59 | * bind(). |
| 60 | * |
| 61 | * Avoids type punning with C style casts from sockaddr_in to sockaddr pointers. |
| 62 | */ |
| 63 | class DPP_EXPORT address_t { |
| 64 | /** |
| 65 | * @brief Internal sockaddr struct |
| 66 | */ |
| 67 | sockaddr socket_addr{}; |
| 68 | |
| 69 | public: |
| 70 | |
| 71 | /** |
| 72 | * @brief Create a new address_t |
| 73 | * @param ip IPv4 address |
| 74 | * @param port Port number |
| 75 | * @note Leave both as defaults to create a default bind-to-any setting |
| 76 | */ |
| 77 | address_t(const std::string_view ip = "0.0.0.0", uint16_t port = 0); |
| 78 | |
| 79 | /** |
| 80 | * @brief Get sockaddr |
| 81 | * @return sockaddr pointer |
| 82 | */ |
| 83 | [[nodiscard]] sockaddr *get_socket_address(); |
| 84 | |
| 85 | /** |
| 86 | * @brief Returns size of sockaddr_in |
| 87 | * @return sockaddr_in size |
| 88 | * @note It is important the size this returns is sizeof(sockaddr_in) not |
| 89 | * sizeof(sockaddr), this is NOT a bug but requirement of C socket functions. |
| 90 | */ |
| 91 | [[nodiscard]] size_t size(); |
| 92 | |
| 93 | /** |
| 94 | * @brief Get the port bound to a file descriptor |
| 95 | * @param fd File descriptor |
| 96 | * @return Port number, or 0 if no port bound |
| 97 | */ |
| 98 | [[nodiscard]] uint16_t get_port(socket fd); |
| 99 | }; |
| 100 | |
| 101 | enum raii_socket_type { |
| 102 | rst_udp, |
| 103 | rst_tcp, |
| 104 | |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * @brief Allocates a dpp::socket, closing it on destruction |
| 109 | */ |
| 110 | struct DPP_EXPORT raii_socket { |
| 111 | /** |
| 112 | * @brief File descriptor |
| 113 | */ |
| 114 | socket fd; |
| 115 | |
| 116 | /** |
| 117 | * @brief Construct a socket. |
| 118 | * Calls socket() and returns a new file descriptor |
| 119 | */ |
| 120 | raii_socket(raii_socket_type type = rst_udp); |
| 121 | |
| 122 | /** |
| 123 | * @brief Convert an established fd to an raii_socket |
| 124 | * @param plain_fd |
| 125 | */ |
| 126 | raii_socket(socket plain_fd); |
| 127 | |
| 128 | /** |
| 129 | * @brief Non-copyable |
| 130 | */ |
| 131 | raii_socket(raii_socket&) = delete; |
| 132 | |
| 133 | /** |
| 134 | * @brief Non-movable |
| 135 | */ |
| 136 | raii_socket(raii_socket&&) = delete; |
| 137 | |
| 138 | /** |
| 139 | * @brief Sets the value of a socket option. |
| 140 | * @tparam T type to set option for |
| 141 | * @param level The level at which to change the socket options |
| 142 | * @param name The option to change the value of |
| 143 | * @param value The value to set |
| 144 | * @return True if set successfully |
| 145 | */ |
| 146 | template <typename T> bool set_option(int level, int name, T value); |
| 147 | |
| 148 | /** |
| 149 | * @brief Bind socket to IP/port |
| 150 | * @param address address to bind to |
| 151 | * @return true on success |
| 152 | */ |
| 153 | bool bind(address_t address); |
| 154 | |
| 155 | /** |
| 156 | * @brief Listen on previously bound port |
| 157 | * @return true on success |
| 158 | */ |
| 159 | bool listen(); |
| 160 | |
| 161 | /** |
| 162 | * @brief Accept a pending connection on listening socket |
| 163 | * @return new connection file descriptor |
| 164 | */ |
| 165 | socket accept(); |
| 166 | |
| 167 | /** |
| 168 | * @brief Non-copyable |
| 169 | */ |
| 170 | raii_socket operator=(raii_socket&) = delete; |
| 171 | |
| 172 | /** |
| 173 | * @brief Non-movable |
| 174 | */ |
| 175 | raii_socket operator=(raii_socket&&) = delete; |
| 176 | |
| 177 | /** |
| 178 | * @brief Destructor |
| 179 | * Frees the socket by closing it |
| 180 | */ |
| 181 | ~raii_socket(); |
| 182 | }; |
| 183 | |
| 184 | extern template DPP_EXPORT bool raii_socket::set_option<int>(int level, int name, int value); |
| 185 | |
| 186 | |
| 187 | } |