Hostname lables are not allowed to be empty

Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
diff --git a/src/transports/utils/dns.c b/src/transports/utils/dns.c
index f266586..1916e77 100644
--- a/src/transports/utils/dns.c
+++ b/src/transports/utils/dns.c
@@ -42,12 +42,25 @@
     labelsz = 0;
     while (1) {
 
-        /*  Hostname is parsed without an error. */
-        if (namelen == 0)
-            return 0;
+        /*  End of the hostname. */
+        if (namelen == 0) {
 
-        /*  New label. */
+            /*  The last label cannot be empty. */
+            if (labelsz == 0)
+                return -EINVAL;
+
+            /*  Success! */
+            return 0;
+        }
+
+        /*  End of a label. */
         if (*name == '.') {
+
+            /*  The old label cannot be empty. */
+            if (labelsz == 0)
+                return -EINVAL;
+
+            /*  Start new label. */
             labelsz = 0;
             ++name;
             --namelen;
diff --git a/tests/tcp.c b/tests/tcp.c
index abba9a1..9b5e18e 100644
--- a/tests/tcp.c
+++ b/tests/tcp.c
@@ -117,6 +117,15 @@
     rc = nn_connect (sc, "tcp://[::1]:5555");
     nn_assert (rc < 0);
     errno_assert (nn_errno () == EINVAL);
+    rc = nn_connect (sc, "tcp://abc.123.:5555");
+    nn_assert (rc < 0);
+    errno_assert (nn_errno () == EINVAL);
+    rc = nn_connect (sc, "tcp://abc...123:5555");
+    nn_assert (rc < 0);
+    errno_assert (nn_errno () == EINVAL);
+    rc = nn_connect (sc, "tcp://.123:5555");
+    nn_assert (rc < 0);
+    errno_assert (nn_errno () == EINVAL);
 
     /*  Connect correctly. Do so before binding the peer socket. */
     rc = nn_connect (sc, SOCKET_ADDRESS);