aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/parsing.h6
-rw-r--r--src/parsing.cpp44
2 files changed, 49 insertions, 1 deletions
diff --git a/include/parsing.h b/include/parsing.h
index ce36365fe..dcfde9568 100644
--- a/include/parsing.h
+++ b/include/parsing.h
@@ -27,6 +27,12 @@ u32bit parse_expr(const std::string&);
std::string to_string(u64bit, u32bit = 0);
u32bit to_u32bit(const std::string&);
+/*************************************************
+* String/Network Address Conversions *
+*************************************************/
+u32bit string_to_ipv4(const std::string&);
+std::string ipv4_to_string(u32bit);
+
}
#endif
diff --git a/src/parsing.cpp b/src/parsing.cpp
index f4d17318d..cfe7a5563 100644
--- a/src/parsing.cpp
+++ b/src/parsing.cpp
@@ -6,6 +6,7 @@
#include <botan/parsing.h>
#include <botan/exceptn.h>
#include <botan/charset.h>
+#include <botan/bit_ops.h>
namespace Botan {
@@ -30,7 +31,6 @@ u32bit to_u32bit(const std::string& number)
return n;
}
-
/*************************************************
* Convert an integer into a string *
*************************************************/
@@ -238,4 +238,46 @@ u32bit parse_expr(const std::string& expr)
return to_u32bit(expr);
}
+/*************************************************
+* Convert a decimal-dotted string to binary IP *
+*************************************************/
+u32bit string_to_ipv4(const std::string& str)
+ {
+ std::vector<std::string> parts = split_on(str, '.');
+
+ if(parts.size() != 4)
+ throw Decoding_Error("Invalid IP string " + str);
+
+ u32bit ip = 0;
+
+ for(size_t j = 0; j != parts.size(); j++)
+ {
+ u32bit octet = to_u32bit(parts[j]);
+
+ if(octet > 255)
+ throw Decoding_Error("Invalid IP string " + str);
+
+ ip = (ip << 8) | (octet & 0xFF);
+ }
+
+ return ip;
+ }
+
+/*************************************************
+* Convert an IP address to decimal-dotted string *
+*************************************************/
+std::string ipv4_to_string(u32bit ip)
+ {
+ std::string str;
+
+ for(size_t j = 0; j != sizeof(ip); j++)
+ {
+ if(j)
+ str += ".";
+ str += to_string(get_byte(j, ip));
+ }
+
+ return str;
+ }
+
}