Blob Blame History Raw
From e061cba1b91650ab08ae8fa50e8cadb13ac3d97d Mon Sep 17 00:00:00 2001
From: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Date: Sun, 16 Jun 2013 11:35:14 -0700
Subject: [PATCH] URL encoded Targetnames

Assume target names are URL encoded with '%' as the special character.

Any sequence of '%' followed by two bytes in the target name will be replaced
with the byte that the second two bytes represent in hexadecimal.

Example
iqn.ronnie.test%3A1234
will be translated to iqn.ronnie.test:1234
---
 include/iscsi.h |  12 ++++--
 lib/init.c      | 114 +++++++++++++++++++++++++++++++++++++++-----------------
 lib/login.c     |   1 +
 3 files changed, 88 insertions(+), 39 deletions(-)

diff --git a/include/iscsi.h b/include/iscsi.h
index f14d404..a4ed932 100644
--- a/include/iscsi.h
+++ b/include/iscsi.h
@@ -125,6 +125,10 @@ iscsi_set_initial_r2t(struct iscsi_context *iscsi, enum iscsi_initial_r2t initia
  * iSCSI URL format :
  * iscsi://[<username>[%<password>]@]<host>[:<port>]/<target-iqn>/<lun>
  *
+ * Target names are url encoded with '%' as a special character.
+ * Example:
+ * "iqn.ronnie.test%3A1234" will be translated to "iqn.ronnie.test:1234"
+ *
  * Function will return a pointer to an iscsi url structure if successful,
  * or it will return NULL and set iscsi_get_error() accrodingly if there was a problem
  * with the URL.
diff --git a/lib/init.c b/lib/init.c
index 18f3fb2..60a1b6d 100644
--- a/lib/init.c
+++ b/lib/init.c
@@ -358,6 +358,45 @@ iscsi_is_logged_in(struct iscsi_context *iscsi)
 	return iscsi->is_loggedin;
 }
 
+static int
+h2i(int h)
+{
+	if (h >= 'a' && h <= 'f') {
+		return h - 'a' + 10;
+	}
+	if (h >= 'A' && h <= 'F') {
+		return h - 'A' + 10;
+	}
+	return h - '0';
+}
+
+static void
+iscsi_decode_url_string(char *str)
+{
+	while (*str) {
+		char *tmp = str;
+		char c;
+
+		if (*str++ != '%') {
+			continue;
+		}
+
+		if (*str == 0) {
+			return;
+		}
+		c = h2i(*str++) << 4;
+
+		if (*str == 0) {
+			return;
+		}
+		c |= h2i(*str++);
+
+		*tmp++ = c;
+		memmove(tmp, str, strlen(str));
+		tmp[strlen(str)] = 0;
+	}
+}
+
 struct iscsi_url *
 iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
 {
@@ -464,6 +506,8 @@ iscsi_parse_url(struct iscsi_context *iscsi, const char *url, int full)
 		iscsi_url->lun = l;
 	}
 
+	iscsi_decode_url_string(&iscsi_url->target[0]);
+
 	return iscsi_url;
 }
 
-- 
1.8.1.4