diff --git a/CMakeLists.txt b/CMakeLists.txt index edc0254..0f10297 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,6 +153,15 @@ add_library(AthenaZelda EXCLUDE_FROM_ALL include/Athena/SkywardSwordQuest.hpp ) +add_library(AthenaNet + src/Athena/Socket.cpp + src/Athena/OAuth.cpp + src/Athena/IPAddress.cpp + + include/Athena/Socket.hpp + include/Athena/OAuth.hpp + include/Athena/IPAddress.hpp) + # Icon set(ATHENA_ICO ${CMAKE_CURRENT_SOURCE_DIR}/Athena.ico) diff --git a/include/Athena/IPAddress.hpp b/include/Athena/IPAddress.hpp new file mode 100644 index 0000000..a56e031 --- /dev/null +++ b/include/Athena/IPAddress.hpp @@ -0,0 +1,44 @@ +#ifndef IPADDRESS_HPP +#define IPADDRESS_HPP + +#include "Athena/Global.hpp" +#include + +#include +#include + +namespace Athena +{ +namespace net +{ +class IPAddress +{ +public: + + static const IPAddress None; + static const IPAddress Any; + static const IPAddress Localhost; + static const IPAddress Broadcast; + + IPAddress() : m_address(~0u), m_valid(false) {} + + IPAddress(const std::string& address); + + IPAddress(atUint8 a, atUint8 b, atUint8 c, atUint8 d); + + IPAddress(atUint32 address); + + const std::string toString() const; + const atUint32 toInt() const; + + static IPAddress localAddress(); +private: + + atUint32 m_address; + bool m_valid; + void resolve(const std::string& address); +}; +} +} + +#endif diff --git a/include/Athena/OAuth.hpp b/include/Athena/OAuth.hpp new file mode 100644 index 0000000..22fee29 --- /dev/null +++ b/include/Athena/OAuth.hpp @@ -0,0 +1,17 @@ +#ifndef OAUTH_HPP +#define OAUTH_HPP + +#include + +namespace Athena +{ +namespace net +{ +class OAuth +{ + +}; +} +} + +#endif diff --git a/include/Athena/Socket.hpp b/include/Athena/Socket.hpp new file mode 100644 index 0000000..1f7bf89 --- /dev/null +++ b/include/Athena/Socket.hpp @@ -0,0 +1,16 @@ +#ifndef SOCKET_HPP +#define SOCKET_HPP + +namespace Athena +{ +namespace net +{ +class Socket +{ +public: +private: +}; +} +} + +#endif diff --git a/src/Athena/IPAddress.cpp b/src/Athena/IPAddress.cpp new file mode 100644 index 0000000..a75f932 --- /dev/null +++ b/src/Athena/IPAddress.cpp @@ -0,0 +1,97 @@ +#include "Athena/IPAddress.hpp" + +#ifdef __unix__ +#include +#include +#include +#include +#include +#include +#include +#elif defined(_WIN32) +#ifdef _WIN32_WINDOWS +#undef _WIN32_WINDOWS +#endif +#ifdef _WIN32_WINNT +#undef _WIN32_WINNT +#endif +#define _WIN32_WINDOWS 0x0501 +#define _WIN32_WINNT 0x0501 +#include +#include +#endif + +namespace Athena +{ +namespace net +{ +const IPAddress IPAddress::Any; +const IPAddress IPAddress::None = IPAddress( 0, 0, 0, 0); +const IPAddress IPAddress::Localhost = IPAddress(127, 0, 0, 1); +const IPAddress IPAddress::Broadcast = IPAddress(255, 255, 255, 255); + +IPAddress::IPAddress(const std::string& address) + : m_valid(false) +{ + resolve(address); +} + +IPAddress::IPAddress(atUint8 a, atUint8 b, atUint8 c, atUint8 d) + : m_address(htonl((a << 24)| (b << 16) | (c << 8) | d)), + m_valid(true) +{ +} + +IPAddress::IPAddress(atUint32 address) + : m_address(htonl(address)), + m_valid(true) +{ +} + +const std::string IPAddress::toString() const +{ + in_addr address; + address.s_addr = m_address; + return inet_ntoa(address); +} + +void IPAddress::resolve(const std::string& address) +{ + if (address == "0.0.0.0") + { + m_address = 0; + m_valid = true; + } + else if(address == "255.255.255.255") + { + m_address = ~0u; + m_valid = true; + } + else + { + atUint32 ip = inet_addr(address.c_str()); + if (ip == INADDR_NONE) + { + addrinfo hints = {0}; + hints.ai_family = AF_INET; + addrinfo* result = nullptr; + if (getaddrinfo(address.c_str(), NULL, &hints, &result)) + { + if (result) + { + ip = reinterpret_cast(result->ai_addr)->sin_addr.s_addr; + freeaddrinfo(result); + m_address = ip; + m_valid = true; + } + } + } + else + { + m_address = ip; + m_valid = true; + } + } +} +} +} diff --git a/src/Athena/OAuth.cpp b/src/Athena/OAuth.cpp new file mode 100644 index 0000000..67d3cd8 --- /dev/null +++ b/src/Athena/OAuth.cpp @@ -0,0 +1 @@ +#include "Athena/OAuth.hpp" diff --git a/src/Athena/Socket.cpp b/src/Athena/Socket.cpp new file mode 100644 index 0000000..1bd5f24 --- /dev/null +++ b/src/Athena/Socket.cpp @@ -0,0 +1 @@ +#include "Athena/Socket.hpp"