Repository :
http://git.fedorahosted.org/cgit/firewalld-recode.git
On branch : master
---------------------------------------------------------------
commit 2912a7b44773a2165e0e49bdb7066b0d7d9742a3
Author: Daniel Kopecek <dkopecek(a)redhat.com>
Date: Wed Oct 22 13:05:38 2014 +0200
Added IPAddress type implementation
---------------------------------------------------------------
src/Daemon/Firewall/IPAddress.cpp | 83 +++++++++++++++++++++++++++++++++++++
src/Daemon/Firewall/IPAddress.hpp | 32 ++++++++++++++
2 files changed, 115 insertions(+), 0 deletions(-)
diff --git a/src/Daemon/Firewall/IPAddress.cpp b/src/Daemon/Firewall/IPAddress.cpp
new file mode 100644
index 0000000..ed64bf6
--- /dev/null
+++ b/src/Daemon/Firewall/IPAddress.cpp
@@ -0,0 +1,83 @@
+#include "Common/Types.hpp"
+#include "Common/Utility.hpp"
+#include "IPAddress.hpp"
+#include <arpa/inet.h>
+#include <stdexcept>
+#include <glibmm/regex.h>
+
+namespace firewalld
+{
+
+ IPAddress::IPAddress(const UString& addr_spec)
+ {
+ UString ip;
+ UString net;
+ Protocol ip_protocol = Protocol::Unknown;
+ Protocol net_protocol = Protocol::Unknown;
+
+ UString::size_type slash_pos = addr_spec.find_first_of('/');
+
+ if (slash_pos != UString::npos) {
+ _is_network = true;
+ ip = addr_spec.substr(0, slash_pos);
+ net = addr_spec.substr(slash_pos + 1);
+
+ //
+ // Net has to be either /<N> (IPv4/6) or
/<n>.<n>.<n>.<n> (IPv4)
+ //
+ auto regex_cidr = Glib::Regex::create("^[0-9]{1,2}$");
+ auto regex_ipv4 = Glib::Regex::create("^([0-9]{1,2}\\.){3}[0-9]{1,2}$");
+
+ if (regex_ipv4->match(net)) {
+ struct ::sockaddr_storage sa;
+ if (::inet_pton(AF_INET, net.c_str(), &sa) != 1) {
+
+ }
+ net_protocol = Protocol::IPv4;
+ } else if (regex_cidr->match(net)) {
+ int cidr = stringToNumber<int>(net);
+ if (cidr < 0 || cidr > 32) {
+ throw std::runtime_error("Invalid IP address cidr subnet specification ");
+ }
+ } else {
+ throw std::runtime_error("Invalid IP address subnet specification");
+ }
+ } else {
+ _is_network = false;
+ ip = addr_spec;
+ }
+
+ struct ::sockaddr_storage sa;
+
+ if (::inet_pton(AF_INET, ip.c_str(), &sa) != 1) {
+ if (::inet_pton(AF_INET6, ip.c_str(), &sa) != 1) {
+ throw std::runtime_error("Invalid IP address");
+ }
+ }
+ //
+ // If we got here, it's a correct IP address spec
+ //
+ _addr_spec = addr_spec;
+ }
+
+ bool IPAddress::isNetwork() const
+ {
+ return _is_network;
+ }
+
+ IPAddress::Protocol IPAddress::protocol() const
+ {
+ return _protocol;
+ }
+
+ const UString& IPAddress::toUString() const
+ {
+ return _addr_spec;
+ }
+
+ const String& IPAddress::toString() const
+ {
+ return _addr_spec.raw();
+ }
+
+} /* namespace firewalld */
diff --git a/src/Daemon/Firewall/IPAddress.hpp b/src/Daemon/Firewall/IPAddress.hpp
new file mode 100644
index 0000000..7b6e5f2
--- /dev/null
+++ b/src/Daemon/Firewall/IPAddress.hpp
@@ -0,0 +1,32 @@
+#ifndef FIREWALLD_FIREWALL_IPADDRESS_HPP
+#define FIREWALLD_FIREWALL_IPADDRESS_HPP
+
+namespace firewalld
+{
+
+ class IPAddress
+ {
+ public:
+ enum Protocol {
+ IPv4,
+ IPv6,
+ Unknown
+ };
+
+ IPAddress(const UString& addr_spec);
+
+ bool isNetwork() const;
+ Protocol protocol() const;
+
+ const UString& toUString() const;
+ const String& toString() const;
+
+ private:
+ UString _addr_spec;
+ Protocol _protocol;
+ bool _is_network;
+ };
+
+} /* namespace firewalld */
+
+#endif /* FIREWALLD_FIREWALL_IPADDRESS_HPP */