sources/network/ipaddress.h

Wed, 27 Jan 2021 19:01:37 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 19:01:37 +0200
changeset 188
5fc32e4b2a8c
parent 186
9330b93d9946
child 189
248d0b85cbda
permissions
-rw-r--r--

validate the ip address octets better

/*
	Copyright 2014 - 2016 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. Neither the name of the copyright holder nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once
#include <ostream>
#include "../main.h"

struct sockaddr;
struct sockaddr_in;

BEGIN_ZFC_NAMESPACE

namespace net
{
	using octet_t = std::uint8_t;
	struct ip_address
	{
		std::uint32_t host;
		std::uint16_t port;
		bool operator<(const ip_address& other) const;
		bool operator==(const ip_address& other) const;
	};
	constexpr ip_address localhost = {0x7f000001, 0};
	int ip_compare(const ip_address& one, const ip_address& other);
	net::octet_t ip_octet(const ip_address& address, unsigned char n);
	void ip_set_octet(ip_address* address, unsigned char n, net::octet_t octet);
	std::optional<unsigned short> ip_parse_port(const char* port_string, std::ostream& errorStream);
	std::optional<ip_address> ip_resolve_hostname(const std::string& node, std::ostream& errorStream);
	std::optional<ip_address> ip_resolve(const std::string& input_string, std::ostream &errorStream);
	sockaddr_in ip_address_to_sockaddr_in(const ip_address& address);
	std::string ip_address_to_string(const ip_address& address);
}

END_ZFC_NAMESPACE

mercurial