sources/network/ipaddress.h

changeset 186
9330b93d9946
parent 184
afd63357a03d
child 188
5fc32e4b2a8c
--- a/sources/network/ipaddress.h	Wed Jan 27 14:16:58 2021 +0200
+++ b/sources/network/ipaddress.h	Wed Jan 27 18:55:03 2021 +0200
@@ -29,6 +29,7 @@
 */
 
 #pragma once
+#include <ostream>
 #include "../main.h"
 
 struct sockaddr;
@@ -36,48 +37,26 @@
 
 BEGIN_ZFC_NAMESPACE
 
-struct IPAddress
+namespace net
 {
-	enum
-	{
-		LOCALHOST = 0x7f000001
-	};
-
-	enum WithPort
-	{
-		WITH_PORT,
-		NO_PORT
-	};
-
-	class StringParseError : public std::exception
+	using octet_t = std::uint8_t;
+	struct ip_address
 	{
-		std::string m_message;
-
-	public:
-		StringParseError (std::string message) :
-			m_message (message) {}
-
-		const char* what() const throw()
-		{
-			return m_message.data();
-		}
+		std::uint32_t host;
+		std::uint16_t port;
+		bool operator<(const ip_address& other) const;
+		bool operator==(const ip_address& other) const;
 	};
-
-	unsigned long host;
-	unsigned short port;
-
-	bool compare (const IPAddress& other) const;
-	unsigned char octet (int n) const;
-	void set_octet (int n, unsigned char oct);
-	std::string to_string (WithPort withport = NO_PORT) const;
-	sockaddr_in to_sockaddr_in() const;
-	bool operator< (const IPAddress& other) const;
-	bool operator== (const IPAddress& other) const { return compare (other); }
-	bool operator!= (const IPAddress& other) const { return not compare (other); }
-	unsigned char operator[] (int n) const { return octet (n); }
-
-	static IPAddress from_string (std::string input);
-	static IPAddress resolve (std::string node);
-};
+	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<ip_address> ip_scan_octets(const char* address_string);
+	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