Browse Source

Merge branch 'stratum_requiretls' into bfgminer

Luke Dashjr 11 years ago
parent
commit
4ded09a45c
2 changed files with 79 additions and 12 deletions
  1. 72 12
      util.c
  2. 7 0
      util.h

+ 72 - 12
util.c

@@ -2132,29 +2132,43 @@ bool bfg_strtobool(const char * const s, char ** const endptr, __maybe_unused co
 	return false;
 	return false;
 }
 }
 
 
-bool uri_get_param_bool(const char * const uri, const char * const param, const bool defval)
+#define URI_FIND_PARAM_FOUND ((const char *)uri_find_param)
+
+const char *uri_find_param(const char * const uri, const char * const param, bool * const invert_p)
 {
 {
 	const char *start = strchr(uri, '#');
 	const char *start = strchr(uri, '#');
-	bool invert = false, foundval = true;
+	if (invert_p)
+		*invert_p = false;
 	if (!start)
 	if (!start)
-		return defval;
+		return NULL;
 	const char *p = start;
 	const char *p = start;
 	++start;
 	++start;
 nextmatch:
 nextmatch:
 	p = strstr(&p[1], param);
 	p = strstr(&p[1], param);
 	if (!p)
 	if (!p)
-		return defval;
+		return NULL;
 	const char *q = &p[strlen(param)];
 	const char *q = &p[strlen(param)];
 	if (isCalpha(q[0]))
 	if (isCalpha(q[0]))
 		goto nextmatch;
 		goto nextmatch;
-	if (p - start >= 2 && (!strncasecmp(&p[-2], "no", 2)) && !isCalpha(p[-3]))
-		invert = true;
+	if (invert_p && p - start >= 2 && (!strncasecmp(&p[-2], "no", 2)) && !isCalpha(p[-3]))
+		*invert_p = true;
 	else
 	else
 	if (isCalpha(p[-1]))
 	if (isCalpha(p[-1]))
 		goto nextmatch;
 		goto nextmatch;
 	if (q[0] == '=')
 	if (q[0] == '=')
+		return &q[1];
+	return URI_FIND_PARAM_FOUND;
+}
+
+enum bfg_tristate uri_get_param_bool2(const char * const uri, const char * const param)
+{
+	bool invert, foundval = true;
+	const char *q = uri_find_param(uri, param, &invert);
+	if (!q)
+		return BTS_UNKNOWN;
+	else
+	if (q != URI_FIND_PARAM_FOUND)
 	{
 	{
-		++q;
 		char *end;
 		char *end;
 		bool v = bfg_strtobool(q, &end, 0);
 		bool v = bfg_strtobool(q, &end, 0);
 		if (end > q && !isCalpha(end[0]))
 		if (end > q && !isCalpha(end[0]))
@@ -2165,6 +2179,35 @@ nextmatch:
 	return foundval;
 	return foundval;
 }
 }
 
 
+bool uri_get_param_bool(const char * const uri, const char * const param, const bool defval)
+{
+	const enum bfg_tristate rv = uri_get_param_bool2(uri, param);
+	if (rv == BTS_UNKNOWN)
+		return defval;
+	return rv;
+}
+
+static
+void _test_uri_find_param(const char * const uri, const char * const param, const ssize_t expect_offset, const int expect_invert)
+{
+	bool invert;
+	const char *actual = uri_find_param(uri, param, (expect_invert >= 0) ? &invert : NULL);
+	ssize_t actual_offset;
+	if (actual == URI_FIND_PARAM_FOUND)
+		actual_offset = -1;
+	else
+	if (!actual)
+		actual_offset = -2;
+	else
+		actual_offset = actual - uri;
+	int actual_invert = (expect_invert >= 0) ? (invert ? 1 : 0) : -1;
+	if (actual_offset != expect_offset || expect_invert != actual_invert)
+		applog(LOG_WARNING, "%s(\"%s\", \"%s\", %s) test failed (offset: expect=%d actual=%d; invert: expect=%d actual=%d)",
+		       "uri_find_param", uri, param, (expect_invert >= 0) ? "(invert)" : "NULL",
+		       expect_offset, actual_offset,
+		       expect_invert, actual_invert);
+}
+
 static
 static
 void _test_uri_get_param(const char * const uri, const char * const param, const bool defval, const bool expect)
 void _test_uri_get_param(const char * const uri, const char * const param, const bool defval, const bool expect)
 {
 {
@@ -2176,6 +2219,13 @@ void _test_uri_get_param(const char * const uri, const char * const param, const
 
 
 void test_uri_get_param()
 void test_uri_get_param()
 {
 {
+	_test_uri_find_param("stratum+tcp://footest/#redirect", "redirect", -1, -1);
+	_test_uri_find_param("stratum+tcp://footest/#redirectme", "redirect", -2, -1);
+	_test_uri_find_param("stratum+tcp://footest/#noredirect", "redirect", -2, -1);
+	_test_uri_find_param("stratum+tcp://footest/#noredirect", "redirect", -1, 1);
+	_test_uri_find_param("stratum+tcp://footest/#redirect", "redirect", -1, 0);
+	_test_uri_find_param("stratum+tcp://footest/#redirect=", "redirect", 32, -1);
+	_test_uri_find_param("stratum+tcp://footest/#noredirect=", "redirect", 34, 1);
 	_test_uri_get_param("stratum+tcp://footest/#redirect", "redirect", false, true);
 	_test_uri_get_param("stratum+tcp://footest/#redirect", "redirect", false, true);
 	_test_uri_get_param("stratum+tcp://footest/#redirectme", "redirect", false, false);
 	_test_uri_get_param("stratum+tcp://footest/#redirectme", "redirect", false, false);
 	_test_uri_get_param("stratum+tcp://footest/#noredirect", "redirect", false, false);
 	_test_uri_get_param("stratum+tcp://footest/#noredirect", "redirect", false, false);
@@ -2583,7 +2633,13 @@ static bool setup_stratum_curl(struct pool *pool)
 	CURL *curl = NULL;
 	CURL *curl = NULL;
 	char s[RBUFSIZE];
 	char s[RBUFSIZE];
 	bool ret = false;
 	bool ret = false;
-	bool try_tls = uri_get_param_bool(pool->rpc_url, "tls", true);
+	bool tls_only = false, try_tls = true;
+	
+	{
+		const enum bfg_tristate tlsparam = uri_get_param_bool2(pool->rpc_url, "tls");
+		if (tlsparam != BTS_UNKNOWN)
+			try_tls = tls_only = tlsparam;
+	}
 
 
 	applog(LOG_DEBUG, "initiate_stratum with sockbuf=%p", pool->sockbuf);
 	applog(LOG_DEBUG, "initiate_stratum with sockbuf=%p", pool->sockbuf);
 	mutex_lock(&pool->stratum_lock);
 	mutex_lock(&pool->stratum_lock);
@@ -2650,11 +2706,15 @@ retry:
 		{
 		{
 			applog(LOG_DEBUG, "Stratum connect failed with TLS to pool %u: %s",
 			applog(LOG_DEBUG, "Stratum connect failed with TLS to pool %u: %s",
 			       pool->pool_no, pool->curl_err_str);
 			       pool->pool_no, pool->curl_err_str);
-			try_tls = false;
-			goto retry;
+			if (!tls_only)
+			{
+				try_tls = false;
+				goto retry;
+			}
 		}
 		}
-		applog(LOG_INFO, "Stratum connect failed to pool %d: %s",
-		       pool->pool_no, pool->curl_err_str);
+		else
+			applog(LOG_INFO, "Stratum connect failed to pool %d: %s",
+			       pool->pool_no, pool->curl_err_str);
 errout:
 errout:
 		curl_easy_cleanup(curl);
 		curl_easy_cleanup(curl);
 		pool->stratum_curl = NULL;
 		pool->stratum_curl = NULL;

+ 7 - 0
util.h

@@ -81,6 +81,12 @@
 
 
 #define IGNORE_RETURN_VALUE(expr)  {if(expr);}(void)0
 #define IGNORE_RETURN_VALUE(expr)  {if(expr);}(void)0
 
 
+enum bfg_tristate {
+	BTS_FALSE = (int)false,
+	BTS_TRUE  = (int)true,
+	BTS_UNKNOWN,
+};
+
 #if JANSSON_MAJOR_VERSION >= 2
 #if JANSSON_MAJOR_VERSION >= 2
 #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
 #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
 #else
 #else
@@ -121,6 +127,7 @@ extern void test_domain_funcs();
 
 
 extern bool bfg_strtobool(const char *, char **endptr, int opts);
 extern bool bfg_strtobool(const char *, char **endptr, int opts);
 
 
+extern enum bfg_tristate uri_get_param_bool2(const char *ri, const char *param);
 extern bool uri_get_param_bool(const char *uri, const char *param, bool defval);
 extern bool uri_get_param_bool(const char *uri, const char *param, bool defval);
 extern void test_uri_get_param();
 extern void test_uri_get_param();