sources/network/ipaddress.h

changeset 5
146825d63b9a
child 7
01e4e9ae323a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sources/network/ipaddress.h	Thu Dec 11 05:58:55 2014 +0200
@@ -0,0 +1,50 @@
+#pragma once
+#include "../main.h"
+
+struct sockaddr;
+
+enum WithPort { IP_WITH_PORT, IP_NO_PORT };
+
+struct IPAddress
+{
+	unsigned long host;
+	unsigned short port;
+	static bool sink;
+
+	IPAddress();
+	IPAddress (unsigned long host, unsigned short port);
+	IPAddress (const IPAddress& other);
+
+	       METHOD compare (const IPAddress& other) const -> bool;
+	       METHOD octet (int n) const -> unsigned char;
+	       METHOD set_octet (int n, unsigned char oct) -> void;
+	       METHOD to_string (WithPort withport = IP_NO_PORT) const -> String;
+	       METHOD to_sockaddr_in() const -> sockaddr_in;
+	       METHOD operator< (const IPAddress& other) const -> bool;
+	inline METHOD operator== (const IPAddress& other) const -> bool;
+	inline METHOD operator!= (const IPAddress& other) const -> bool;
+	inline METHOD operator[] (int n) const -> unsigned char;
+
+	static METHOD from_string (String input, bool* ok = &sink) -> IPAddress;
+	static METHOD resolve (String node, bool* ok = &sink) -> IPAddress;
+};
+
+extern const IPAddress localhost;
+
+inline METHOD
+IPAddress::operator[] (int n) const -> unsigned char
+{
+	return octet (n);
+}
+
+inline METHOD
+IPAddress::operator== (const IPAddress& other) const -> bool
+{
+	return compare (other);
+}
+
+inline METHOD
+IPAddress::operator!= (const IPAddress& other) const -> bool
+{
+	return not operator== (other);
+}

mercurial