#define __RFB_HOSTNAME_H__
#include <assert.h>
+#include <ctype.h>
#include <stdlib.h>
#include <rdr/Exception.h>
#include <rfb/util.h>
namespace rfb {
+ static bool isAllSpace(const char *string) {
+ if (string == NULL)
+ return false;
+ while(*string != '\0') {
+ if (! isspace(*string))
+ return false;
+ string++;
+ }
+ return true;
+ }
+
static void getHostAndPort(const char* hi, char** host, int* port, int basePort=5900) {
const char* hostStart;
const char* hostEnd;
if (hi == NULL)
throw rdr::Exception("NULL host specified");
+ // Trim leading whitespace
+ while(isspace(*hi))
+ hi++;
+
assert(host);
assert(port);
throw rdr::Exception("unmatched [ in host");
portStart = hostEnd + 1;
- if (*portStart == '\0')
+ if (isAllSpace(portStart))
portStart = NULL;
} else {
hostStart = &hi[0];
}
}
+ // Back up past trailing space
+ while(isspace(*(hostEnd - 1)) && hostEnd > hostStart)
+ hostEnd--;
+
if (hostStart == hostEnd)
*host = strDup("localhost");
else {
*port = strtol(portStart + 1, &end, 10);
else
*port = strtol(portStart + 2, &end, 10);
- if (*end != '\0')
+ if (*end != '\0' && ! isAllSpace(end))
throw rdr::Exception("invalid port specified");
if ((portStart[1] != ':') && (*port < 100))
doTest("[2001:1234::20:1]:100", "2001:1234::20:1", 100);
doTest("[2001:1234::20:1]:5901", "2001:1234::20:1", 5901);
+ doTest(" 1.2.3.4 ", "1.2.3.4", 5900);
+ doTest(" 1.2.3.4:5901 ", "1.2.3.4", 5901);
+ doTest(" 1.2.3.4: 5901 ", "1.2.3.4", 5901);
+ doTest(" 1.2.3.4 :5901 ", "1.2.3.4", 5901);
+ doTest(" [1.2.3.4]:5902 ", "1.2.3.4", 5902);
+ doTest(" :5903 ", "localhost", 5903);
+ doTest(" ::4 ", "localhost", 4);
+ doTest(" [::1] ", "::1", 5900);
+ doTest(" 2001:1234::20:1 ", "2001:1234::20:1", 5900);
+ doTest(" [2001:1234::20:1] ", "2001:1234::20:1", 5900);
+ doTest(" [2001:1234::20:1]:5905 ", "2001:1234::20:1", 5905);
+
return 0;
}