Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:brainboxdotcc/DPP
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 16, 2024
2 parents 8030123 + 84141aa commit 0887b4b
Show file tree
Hide file tree
Showing 45 changed files with 1,620 additions and 1,296 deletions.
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@
"nullopt",
"chrono",
"ciphersuite",
"rmap"
"rmap",
"WSAPOLLFD"
],
"flagWords": [
"hte"
Expand Down
7 changes: 7 additions & 0 deletions include/dpp/discordvoiceclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,13 @@ class DPP_EXPORT discord_voice_client : public websocket_client
*/
dave_version_t dave_version;

/**
* @brief Destination address for where packets go
* on the UDP socket
*/
address_t destination{};


/**
* @brief Send data to UDP socket immediately.
*
Expand Down
37 changes: 29 additions & 8 deletions include/dpp/dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <sys/types.h>
#include <string>
#include <unordered_map>
#include <cstring>
#include <dpp/socket.h>

namespace dpp {

Expand All @@ -40,23 +42,42 @@ namespace dpp {
*/
struct dns_cache_entry {
/**
* @brief Resolved address information
* @brief Resolved address metadata
*/
addrinfo addr;

/**
* @brief Socket address.
* Discord only supports ipv4, but sockaddr_in6 is larger
* than sockaddr_in, sockaddr_storage will hold either. This
* means that if discord ever do support ipv6 we just flip
* one value in dns.cpp and that should be all that is needed.
* @brief Resolved address as string.
* The metadata is needed to know what type of address it is.
* Do not do silly stuff like just looking to see if '.' is in it!
*/
sockaddr_storage ai_addr;
std::string resolved_addr;

/**
* @brief Time at which this cache entry is invalidated
*/
time_t expire_timestamp;

/**
* @brief Get address length
* @return address length
*/
[[nodiscard]] int size() const;

/**
* @brief Get the address_t that corresponds to this cache entry
* for use when connecting with ::connect()
* @param port Port number to connect to
* @return address_t prefilled with the IP and port number
*/
[[nodiscard]] const address_t get_connecting_address(uint16_t port) const;

/**
* @brief Allocate a socket file descriptor for the given dns address
* @return File descriptor ready for calling connect(), or INVALID_SOCKET
* on failure.
*/
[[nodiscard]] socket make_connecting_socket() const;
};

/**
Expand All @@ -73,4 +94,4 @@ namespace dpp {
* @throw dpp::connection_exception On failure to resolve hostname
*/
const dns_cache_entry* resolve_hostname(const std::string& hostname, const std::string& port);
} // namespace dpp
}
135 changes: 130 additions & 5 deletions include/dpp/socket.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,53 @@
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#pragma once

#include <dpp/export.h>
#ifdef _WIN32
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <io.h>
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
#define pollfd WSAPOLLFD
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include <string_view>
#include <cstdint>


namespace dpp
{
/**
* @brief Represents a socket file descriptor.
* This is used to ensure parity between windows and unix-like systems.
*/
/**
* @brief Represents a socket file descriptor.
* This is used to ensure parity between windows and unix-like systems.
*/
#ifndef _WIN32
using socket = int;
#else
using socket = SOCKET;
#endif
} // namespace dpp

#ifndef SOCKET_ERROR
/**
Expand All @@ -26,3 +62,92 @@ namespace dpp
*/
#define INVALID_SOCKET ~0
#endif

/**
* @brief Represents an IPv4 address for use with socket functions such as
* bind().
*
* Avoids type punning with C style casts from sockaddr_in to sockaddr pointers.
*/
class DPP_EXPORT address_t {
/**
* @brief Internal sockaddr struct
*/
sockaddr socket_addr{};

public:

/**
* @brief Create a new address_t
* @param ip IPv4 address
* @param port Port number
* @note Leave both as defaults to create a default bind-to-any setting
*/
address_t(const std::string_view ip = "0.0.0.0", uint16_t port = 0);

/**
* @brief Get sockaddr
* @return sockaddr pointer
*/
[[nodiscard]] sockaddr *get_socket_address();

/**
* @brief Returns size of sockaddr_in
* @return sockaddr_in size
* @note It is important the size this returns is sizeof(sockaddr_in) not
* sizeof(sockaddr), this is NOT a bug but requirement of C socket functions.
*/
[[nodiscard]] size_t size();

/**
* @brief Get the port bound to a file descriptor
* @param fd File descriptor
* @return Port number, or 0 if no port bound
*/
[[nodiscard]] uint16_t get_port(socket fd);
};

/**
* @brief Allocates a dpp::socket, closing it on destruction
*/
struct DPP_EXPORT raii_socket {
/**
* @brief File descriptor
*/
socket fd;

/**
* @brief Construct a socket.
* Calls socket() and returns a new file descriptor
*/
raii_socket();

/**
* @brief Non-copyable
*/
raii_socket(raii_socket&) = delete;

/**
* @brief Non-movable
*/
raii_socket(raii_socket&&) = delete;

/**
* @brief Non-copyable
*/
raii_socket operator=(raii_socket&) = delete;

/**
* @brief Non-movable
*/
raii_socket operator=(raii_socket&&) = delete;

/**
* @brief Destructor
* Frees the socket by closing it
*/
~raii_socket();
};


}
28 changes: 16 additions & 12 deletions src/dpp/dave/array_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,50 @@ template <typename T> class array_view {
* @param data data pointer to array
* @param size size of array
*/
array_view(T* data, size_t size)
: data_(data)
, size_(size)
{
array_view(T* data, size_t size) : array(data), array_size(size) {
}

/**
* @brief Get size of view
* @return size
*/
size_t size() const { return size_; }
size_t size() const {
return array_size;
}

/**
* @brief Get data of view from first element
* @return data
*/
T* data() const { return data_; }
T* data() const {
return array;
}

/**
* @brief Get start of view, first element
* @return first element
*/
T* begin() const { return data_; }
T* begin() const {
return array;
}

/**
* @brief Get ending iterator of view, 1+last element
* @return end of view
*/
T* end() const { return data_ + size_; }
T* end() const {
return array + array_size;
}

private:
/**
* @brief array data
*/
T* data_ = nullptr;
T* array = nullptr;
/**
* @brief Array size
*/
size_t size_ = 0;
size_t array_size = 0;
};

/**
Expand Down Expand Up @@ -111,5 +116,4 @@ inline array_view<T> make_array_view(std::vector<T>& data)
return array_view<T>(data.data(), data.size());
}

} // namespace dpp::dave

}
7 changes: 3 additions & 4 deletions src/dpp/dave/cipher_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@

namespace dpp::dave {

std::unique_ptr<cipher_interface> create_cipher(dpp::cluster& cl, const encryption_key& encryptionKey)
std::unique_ptr<cipher_interface> create_cipher(dpp::cluster& cl, const encryption_key& key)
{
auto cipher = std::make_unique<openssl_aead_cipher>(cl, encryptionKey);
auto cipher = std::make_unique<openssl_aead_cipher>(cl, key);
return cipher->is_valid() ? std::move(cipher) : nullptr;
}

} // namespace dpp::dave

}
31 changes: 15 additions & 16 deletions src/dpp/dave/cipher_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,25 @@ class cipher_interface { // NOLINT

/**
* @brief Encrypt audio or video
* @param ciphertextBufferOut Output buffer of ciphertext
* @param plaintextBuffer Input buffer for plaintext
* @param nonceBuffer Input nonce/IV
* @param additionalData Additional data for GCM AEAD encryption
* @param tagBufferOut AEAD Tag for verification
* @param ciphertext_buffer_out Output buffer of ciphertext
* @param plaintext_buffer Input buffer for plaintext
* @param nonce_buffer Input nonce/IV
* @param additional_data Additional data for GCM AEAD encryption
* @param tag_buffer_out AEAD Tag for verification
* @return true if encryption succeeded, false if it failed
*/
virtual bool encrypt(byte_view ciphertextBufferOut, const_byte_view plaintextBuffer, const_byte_view nonceBuffer, const_byte_view additionalData, byte_view tagBufferOut) = 0;
virtual bool encrypt(byte_view ciphertext_buffer_out, const_byte_view plaintext_buffer, const_byte_view nonce_buffer, const_byte_view additional_data, byte_view tag_buffer_out) = 0;

/**
* @brief Decrypt audio or video
* @param plaintextBufferOut Output buffer for plaintext
* @param ciphertextBuffer Input buffer for ciphetext
* @param tagBuffer AEAD Tag for verification
* @param nonceBuffer Nonce/IV
* @param additionalData Additional data for GCM AEAD encryption
* @param plaintext_buffer_out Output buffer for plaintext
* @param ciphertext_buffer Input buffer for ciphetext
* @param tag_buffer AEAD Tag for verification
* @param nonce_buffer Nonce/IV
* @param additional_data Additional data for GCM AEAD encryption
* @return true if decryption succeeded, false if it failed
*/
virtual bool decrypt(byte_view plaintextBufferOut, const_byte_view ciphertextBuffer, const_byte_view tagBuffer, const_byte_view nonceBuffer, const_byte_view additionalData) = 0;
virtual bool decrypt(byte_view plaintext_buffer_out, const_byte_view ciphertext_buffer, const_byte_view tag_buffer, const_byte_view nonce_buffer, const_byte_view additional_data) = 0;

protected:

Expand All @@ -94,10 +94,9 @@ class cipher_interface { // NOLINT

/**
* @brief Factory function to create new cipher interface of the best supported type for DAVE
* @param encryptionKey encryption key
* @param key encryption key
* @return an instance of a class derived from cipher_interface
*/
std::unique_ptr<cipher_interface> create_cipher(dpp::cluster& cl, const encryption_key& encryptionKey);

} // namespace dpp::dave
std::unique_ptr<cipher_interface> create_cipher(dpp::cluster& cl, const encryption_key& key);

}
3 changes: 1 addition & 2 deletions src/dpp/dave/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,4 @@ class clock : public clock_interface {
}
};

} // namespace dpp::dave

}
Loading

0 comments on commit 0887b4b

Please sign in to comment.