Skip to content

Commit

Permalink
socket: Expose parameter conversion functions for consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
amyspark committed Jan 4, 2024
1 parent 641bb7f commit d807942
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 59 deletions.
109 changes: 58 additions & 51 deletions impl/socket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@
#endif

#include <cstring>
#ifndef _WIN32

#ifdef _WIN32
# include <winsock2.h>
#else
# include <sys/socket.h>
# include <netdb.h>
# include <arpa/inet.h>
# include <netinet/in.h>
# include <netinet/udp.h>
# include <unistd.h>
#endif

#include <substrate/utility>
#include <substrate/socket>

#ifndef _WIN32
# include <unistd.h>
using substrate::INVALID_SOCKET;
inline int closesocket(const int s) { return close(s); }
#else
# include <winsock2.h>
#endif

using namespace substrate;

size_t sockaddrLen(const sockaddr_storage &addr) noexcept
inline size_t sockaddrLen(const sockaddr_storage &addr) noexcept
{
switch (addr.ss_family)
{
Expand Down Expand Up @@ -90,52 +91,6 @@ char socket_t::peek() const noexcept
return buffer;
}

inline int typeToFamily(const socketType_t type) noexcept
{
if (type == socketType_t::ipv4)
return AF_INET;
else if (type == socketType_t::ipv6)
return AF_INET6;
return AF_UNSPEC;
}

inline int protocolToHints(const socketProtocol_t protocol) noexcept
{
switch (protocol)
{
case socketProtocol_t::udp:
return IPPROTO_UDP;
case socketProtocol_t::raw:
return IPPROTO_RAW;
case socketProtocol_t::tcp:
default:
return IPPROTO_TCP;
}
}

inline int protocolToType(const socketProtocol_t protocol) noexcept
{
switch (protocol)
{
case socketProtocol_t::udp:
return SOCK_DGRAM;
case socketProtocol_t::raw:
return SOCK_RAW;
case socketProtocol_t::tcp:
default:
return SOCK_STREAM;
}
}

inline size_t familyToSize(const sa_family_t family) noexcept
{
if (family == AF_INET)
return sizeof(sockaddr_in);
else if (family == AF_INET6)
return sizeof(sockaddr_in6);
return sizeof(sockaddr_storage);
}

inline uint16_t toBE(const uint16_t value) noexcept
{
const std::array<uint8_t, 2> resultBytes
Expand Down Expand Up @@ -202,4 +157,56 @@ sockaddr_storage substrate::socket::prepare(const socketType_t family, const cha
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

int substrate::socket::typeToFamily(const socketType_t type) noexcept
{
switch(type)
{
case socketType_t::ipv4:
return AF_INET;
case socketType_t::ipv6:
return AF_INET6;
case socketType_t::unknown:
// fallback
case socketType_t::dontCare:
return AF_UNSPEC;
}

Check warning on line 173 in impl/socket.cxx

View check run for this annotation

Codecov / codecov/patch

impl/socket.cxx#L173

Added line #L173 was not covered by tests
}

int substrate::socket::protocolToHints(const socketProtocol_t protocol) noexcept
{
switch (protocol)
{
case socketProtocol_t::udp:
return IPPROTO_UDP;
case socketProtocol_t::raw:
return IPPROTO_RAW;
case socketProtocol_t::tcp:
return IPPROTO_TCP;
}

Check warning on line 186 in impl/socket.cxx

View check run for this annotation

Codecov / codecov/patch

impl/socket.cxx#L186

Added line #L186 was not covered by tests
}

int substrate::socket::protocolToType(const socketProtocol_t protocol) noexcept
{
switch (protocol)
{
case socketProtocol_t::udp:
return SOCK_DGRAM;
case socketProtocol_t::raw:
return SOCK_RAW;
case socketProtocol_t::tcp:
return SOCK_STREAM;
}

Check warning on line 199 in impl/socket.cxx

View check run for this annotation

Codecov / codecov/patch

impl/socket.cxx#L199

Added line #L199 was not covered by tests
}

size_t substrate::socket::familyToSize(sa_family_t family) noexcept
{
if (family == AF_INET)
return sizeof(sockaddr_in);
else if (family == AF_INET6)
return sizeof(sockaddr_in6);
else
return sizeof(sockaddr_storage);

Check warning on line 209 in impl/socket.cxx

View check run for this annotation

Codecov / codecov/patch

impl/socket.cxx#L209

Added line #L209 was not covered by tests
}

/* vim: set ft=cpp ts=4 sw=4 noexpandtab: */
23 changes: 15 additions & 8 deletions substrate/socket
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@

#include <cstdint>
#include <cstdlib>
#include <utility>

#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <ws2tcpip.h>
# include <ws2tcpip.h>
#undef WIN32_LEAN_AND_MEAN
#include <type_traits>

using ssize_t = typename std::make_signed<size_t>::type;
#else
# include <sys/socket.h>
#endif
#include <utility>

#include <substrate/internal/defs>

struct sockaddr;
struct sockaddr_storage;
#include <substrate/internal/types>

namespace substrate
{
Expand All @@ -31,6 +29,7 @@ namespace substrate
using sockType_t = int32_t;
using bufferlen_t = size_t;
constexpr static sockType_t INVALID_SOCKET{-1};
using sa_family_t = sa_family_t;
#else
using sa_family_t = ADDRESS_FAMILY;
using sockType_t = SOCKET;
Expand Down Expand Up @@ -98,6 +97,14 @@ namespace substrate
{
SUBSTRATE_API sockaddr_storage prepare(const socketType_t family, const char *const where,
const uint16_t port, const socketProtocol_t protocol = socketProtocol_t::tcp) noexcept;

SUBSTRATE_API int typeToFamily(socketType_t type) noexcept;

SUBSTRATE_API int protocolToHints(socketProtocol_t protocol) noexcept;

SUBSTRATE_API int protocolToType(socketProtocol_t protocol) noexcept;

SUBSTRATE_API size_t familyToSize(sa_family_t family) noexcept;
}
}

Expand Down

0 comments on commit d807942

Please sign in to comment.