diff --git a/libstratosphere/include/stratosphere.hpp b/libstratosphere/include/stratosphere.hpp index 06444bda..aa648c20 100644 --- a/libstratosphere/include/stratosphere.hpp +++ b/libstratosphere/include/stratosphere.hpp @@ -74,6 +74,7 @@ #include #include #include +#include #include #include #include diff --git a/libstratosphere/include/stratosphere/socket.hpp b/libstratosphere/include/stratosphere/socket.hpp index d43f9238..fc933378 100644 --- a/libstratosphere/include/stratosphere/socket.hpp +++ b/libstratosphere/include/stratosphere/socket.hpp @@ -14,4 +14,8 @@ * along with this program. If not, see . */ #pragma once +#include +#include +#include #include +#include diff --git a/libstratosphere/include/stratosphere/socket/socket_api.hpp b/libstratosphere/include/stratosphere/socket/socket_api.hpp new file mode 100644 index 00000000..4e13549f --- /dev/null +++ b/libstratosphere/include/stratosphere/socket/socket_api.hpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope 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 . + */ +#pragma once +#include +#include + +namespace ams::socket { + + u32 InetHtonl(u32 host); + u16 InetHtons(u16 host); + u32 InetNtohl(u32 net); + u16 InetNtohs(u16 net); + +} diff --git a/libstratosphere/include/stratosphere/socket/socket_types.hpp b/libstratosphere/include/stratosphere/socket/socket_types.hpp index bac58080..4a6f1f9c 100644 --- a/libstratosphere/include/stratosphere/socket/socket_types.hpp +++ b/libstratosphere/include/stratosphere/socket/socket_types.hpp @@ -27,7 +27,7 @@ namespace ams::socket { constexpr inline unsigned int FdSetSize = 0x400; template - constexpr inline InAddrT EncodeInAddr = (A << 24) | (B << 16) | (C << 8) | (D << 0); + constexpr inline InAddrT EncodeInAddr = util::ConvertToBigEndian(InAddrT{(A << 24) | (B << 16) | (C << 8) | (D << 0)}); constexpr inline InAddrT InAddr_Any = EncodeInAddr< 0, 0, 0, 0>; constexpr inline InAddrT InAddr_Broadcast = EncodeInAddr<255, 255, 255, 255>; @@ -62,4 +62,8 @@ namespace ams::socket { char **h_addr_list; }; + struct InAddr { + InAddrT s_addr; + }; + } diff --git a/libstratosphere/source/socket/impl/socket_allocator.hpp b/libstratosphere/source/socket/impl/socket_allocator.hpp new file mode 100644 index 00000000..d6583867 --- /dev/null +++ b/libstratosphere/source/socket/impl/socket_allocator.hpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope 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 . + */ +#pragma once +#include + +namespace ams::socket::impl { + + void *Alloc(size_t size); + void *Calloc(size_t num, size_t size); + void Free(void *ptr); + +} diff --git a/libstratosphere/source/socket/impl/socket_api.hpp b/libstratosphere/source/socket/impl/socket_api.hpp new file mode 100644 index 00000000..f370515e --- /dev/null +++ b/libstratosphere/source/socket/impl/socket_api.hpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope 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 . + */ +#include + +namespace ams::socket::impl { + + u32 InetHtonl(u32 host); + u16 InetHtons(u16 host); + u32 InetNtohl(u32 net); + u16 InetNtohs(u16 net); + +} diff --git a/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp b/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp new file mode 100644 index 00000000..57213085 --- /dev/null +++ b/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope 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 . + */ +#include +#include "socket_api.hpp" +#include "socket_allocator.hpp" + +namespace ams::socket::impl { + + void *Alloc(size_t size) { + /* TODO: expheap, heap generation. */ + return ams::Malloc(size); + } + + void *Calloc(size_t num, size_t size) { + if (void *ptr = Alloc(size * num); ptr != nullptr) { + std::memset(ptr, 0, size * num); + return ptr; + } else { + return nullptr; + } + } + + void Free(void *ptr) { + /* TODO: expheap, heap generation. */ + return ams::Free(ptr); + } + + u32 InetHtonl(u32 host) { + if constexpr (util::IsBigEndian()) { + return host; + } else { + return util::SwapBytes(host); + } + } + + u16 InetHtons(u16 host) { + if constexpr (util::IsBigEndian()) { + return host; + } else { + return util::SwapBytes(host); + } + } + + u32 InetNtohl(u32 net) { + if constexpr (util::IsBigEndian()) { + return net; + } else { + return util::SwapBytes(net); + } + } + + u16 InetNtohs(u16 net) { + if constexpr (util::IsBigEndian()) { + return net; + } else { + return util::SwapBytes(net); + } + } + +} diff --git a/libstratosphere/source/socket/socket_api.cpp b/libstratosphere/source/socket/socket_api.cpp new file mode 100644 index 00000000..28f40550 --- /dev/null +++ b/libstratosphere/source/socket/socket_api.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope 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 . + */ +#include +#include "impl/socket_api.hpp" + +namespace ams::socket { + + u32 InetHtonl(u32 host) { + return impl::InetHtonl(host); + } + + u16 InetHtons(u16 host) { + return impl::InetHtons(host); + } + + u32 InetNtohl(u32 net) { + return impl::InetNtohl(net); + } + + u16 InetNtohs(u16 net) { + return impl::InetNtohs(net); + } + +}