Changeset View
Standalone View
source/network/IPTools.cpp
- This file was added.
/* Copyright (C) 2019 Wildfire Games. | |||||
* This file is part of 0 A.D. | |||||
* | |||||
* 0 A.D. is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 2 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* 0 A.D. is distributed in the hope that 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 0 A.D. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "precompiled.h" | |||||
#include "IPTools.h" | |||||
#include <arpa/inet.h> | |||||
vladislavbelov: Windows doesn't have this include. | |||||
LefoUnsubmitted Done Inline ActionsLet's drop windows support entirely and bundle 0ad for windows with cygwin. Lefo: Let's drop windows support entirely and bundle 0ad for windows with cygwin. | |||||
vladislavbelovUnsubmitted Not Done Inline ActionsI'm not sure that it's possible currently. We use Win API directly in some cases. We need someone who knows cygwin well. vladislavbelov: I'm not sure that it's possible currently. We use Win API directly in some cases. We need… | |||||
elexisAuthorUnsubmitted Done Inline ActionsWasn't the conclusion to use getaddrinfo, making this header obsolete?
That escalated quickly :P elexis: Wasn't the conclusion to use `getaddrinfo`, making this header obsolete?
> Let's drop windows… | |||||
#include <string> | |||||
// TODO: implement banmasks | |||||
/** | |||||
* Parses an IPv4 address, such as "223.252.161.128" to an u32 representation in hostbyte order (useful for bitmask matching). | |||||
*/ | |||||
bool IPTools::ParseIPv4Address(const std::string& ipAddress, u32& ipAddressNum) | |||||
{ | |||||
// Returns in network byte order, big endianness | |||||
// TODO: This doesn't exist on all platforms | |||||
if (inet_pton(AF_INET, ipAddress.c_str(), &ipAddressNum) != 1) | |||||
elexisAuthorUnsubmitted Done Inline Actions@Lefo I wonder if the cheapest platform-independent implementation would be to reinvent the parsing, admittedly I didn't check yet. elexis: @Lefo I wonder if the cheapest platform-independent implementation would be to reinvent the… | |||||
LefoUnsubmitted Not Done Inline ActionsDefinitely not. You can simply use getaddrinfo to convert ip address to binary representation. See CreateStunRequest in source/network/StunClient.cpp for reference. It might be a good idea to pass AI_NUMERICHOST to hints.ai_flags. If you want to enforce parsing IPv4 only, pass AF_INET to hints.ai_family. Lefo: Definitely not. You can simply use getaddrinfo to convert ip address to binary representation. | |||||
elexisAuthorUnsubmitted Done Inline ActionsThanks! elexis: Thanks! | |||||
return false; | |||||
// Convert to host byte order (little endianness) | |||||
ipAddressNum = ntohl(ipAddressNum); | |||||
return true; | |||||
} | |||||
/** | |||||
* Parses CIDR notation, for example "223.252.161.128/25". | |||||
*/ | |||||
bool IPTools::ParseSubnet(const std::string& subnet, u32& subnetAddress, u8& subnetMaskBits) | |||||
{ | |||||
std::istringstream subnetStream(subnet); | |||||
std::string subnetAddressString; | |||||
std::getline(subnetStream, subnetAddressString, '/'); | |||||
if (!ParseIPv4Address(subnetAddressString, subnetAddress)) | |||||
return false; | |||||
std::string subnetMaskBitsString; | |||||
std::getline(subnetStream, subnetMaskBitsString); | |||||
subnetMaskBits = static_cast<u8>(std::stoi(subnetMaskBitsString)); | |||||
return true; | |||||
} | |||||
/** | |||||
* All values in hostbyte order (little endianness) | |||||
*/ | |||||
bool IPTools::IsIpV4PartOfSubnet(u32 ipAddress, u32 subnetAddress, int subnetMaskBits) | |||||
{ | |||||
return ((0xFFFFFFFF << (32 - subnetMaskBits)) & ipAddress) == subnetAddress; | |||||
elexisAuthorUnsubmitted Done Inline Actions@ffffffff hardcoded, is that good? elexis: @ffffffff hardcoded, is that good? | |||||
} |
Windows doesn't have this include.