Skip to content

Commit

Permalink
Initial networking
Browse files Browse the repository at this point in the history
  • Loading branch information
Antidote committed Oct 27, 2015
1 parent 8124d21 commit 95c23d3
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
44 changes: 44 additions & 0 deletions include/Athena/IPAddress.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef IPADDRESS_HPP
#define IPADDRESS_HPP

#include "Athena/Global.hpp"
#include <string.h>

#include <cstring>
#include <utility>

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
17 changes: 17 additions & 0 deletions include/Athena/OAuth.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef OAUTH_HPP
#define OAUTH_HPP

#include <Athena/Global.hpp>

namespace Athena
{
namespace net
{
class OAuth
{

};
}
}

#endif
16 changes: 16 additions & 0 deletions include/Athena/Socket.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef SOCKET_HPP
#define SOCKET_HPP

namespace Athena
{
namespace net
{
class Socket
{
public:
private:
};
}
}

#endif
97 changes: 97 additions & 0 deletions src/Athena/IPAddress.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "Athena/IPAddress.hpp"

#ifdef __unix__
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#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 <winsock2.h>
#include <ws2tcpip.h>
#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<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr;
freeaddrinfo(result);
m_address = ip;
m_valid = true;
}
}
}
else
{
m_address = ip;
m_valid = true;
}
}
}
}
}
1 change: 1 addition & 0 deletions src/Athena/OAuth.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Athena/OAuth.hpp"
1 change: 1 addition & 0 deletions src/Athena/Socket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Athena/Socket.hpp"

0 comments on commit 95c23d3

Please sign in to comment.