Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
NetworkAddress.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "NetworkAddress.h"
7
8#if PLATFORM == PLATFORM_WINDOWS
9#include <winsock2.h>
10#else
11#include <arpa/inet.h>
12#endif
13
14#include <boost/asio/ip/address_v4.hpp>
15#include <boost/system/error_code.hpp>
16
17namespace
18{
19 bool ParseIPv4Address(std::string const& host, boost::asio::ip::address_v4& address)
20 {
21 boost::system::error_code error;
22 address = boost::asio::ip::make_address_v4(host, error);
23 if (error)
24 return false;
25
26 if (address.to_string() != host)
27 return false;
28
29 return address.to_uint() != boost::asio::ip::address_v4::uint_type(-1);
30 }
31}
32
33namespace Skyfire::Net
34{
35 Address::Address() : _host("0.0.0.0"), _port(0) { }
36
37 Address::Address(std::string const& host, uint16 port) : _host(host), _port(port) { }
38
39 bool IsIPv4Address(std::string const& host)
40 {
41 boost::asio::ip::address_v4 address;
42 return ParseIPv4Address(host, address);
43 }
44
45 uint32 ToIPv4NetworkOrder(std::string const& host)
46 {
47 boost::asio::ip::address_v4 address;
48 if (!ParseIPv4Address(host, address))
49 return 0;
50
51 return htonl(static_cast<uint32>(address.to_uint()));
52 }
53
55 {
56 uint32 const ip = ntohl(ToIPv4NetworkOrder());
57 return (ip & 0xFF000000) == 0x7F000000;
58 }
59
64}
std::uint32_t uint32
Definition Define.h:77
std::uint16_t uint16
Definition Define.h:78
uint32 ToIPv4NetworkOrder() const
bool IsIPv4Address(std::string const &host)
uint32 ToIPv4NetworkOrder(std::string const &host)