aboutsummaryrefslogtreecommitdiffstats
path: root/src/parsing.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2007-10-16 16:15:19 +0000
committerlloyd <[email protected]>2007-10-16 16:15:19 +0000
commit19a5c9f4d223314dc5eb6c971f332171f3a85623 (patch)
tree8e7ec3fb9225f9f82f9769688e6ecaf8bbc8975c /src/parsing.cpp
parent77afd52c616f50f5213b24f9b2df0d27e61c95f2 (diff)
Add functions that can convert between binary IPv4 addresses and standard
decimal-dotted string notation.
Diffstat (limited to 'src/parsing.cpp')
-rw-r--r--src/parsing.cpp44
1 files changed, 43 insertions, 1 deletions
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;
+ }
+
}