Browse Source

allow url based config files

Conflicts:
	cgminer.c
	miner.h
	util.c
Kano 12 years ago
parent
commit
9ac4b3f79b
3 changed files with 116 additions and 1 deletions
  1. 49 1
      miner.c
  2. 7 0
      miner.h
  3. 60 0
      util.c

+ 49 - 1
miner.c

@@ -2,7 +2,7 @@
  * Copyright 2011-2014 Con Kolivas
  * Copyright 2011-2014 Luke Dashjr
  * Copyright 2014 Nate Woolls
- * Copyright 2012-2013 Andrew Smith
+ * Copyright 2012-2014 Andrew Smith
  * Copyright 2010 Jeff Garzik
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -395,8 +395,13 @@ static int include_count;
 #define JSON_LOAD_ERROR_LEN strlen(JSON_LOAD_ERROR)
 #define JSON_MAX_DEPTH 10
 #define JSON_MAX_DEPTH_ERR "Too many levels of JSON includes (limit 10) or a loop"
+<<<<<<<
 
 char *cmd_idle, *cmd_sick, *cmd_dead;
+|||||||
+=======
+#define JSON_WEB_ERROR "WEB config err"
+>>>>>>>
 
 #if defined(unix) || defined(__APPLE__)
 	static char *opt_stderr_cmd = NULL;
@@ -2896,13 +2901,56 @@ static char *parse_config(json_t *config, bool fileconf, int * const fileconf_lo
 
 struct bfg_loaded_configfile *bfg_loaded_configfiles;
 
+char conf_web1[] = "http://";
+char conf_web2[] = "https://";
+
+static char *load_web_config(const char *arg)
+{
+	json_t *val;
+	CURL *curl;
+
+	curl = curl_easy_init();
+	if (unlikely(!curl))
+		quithere(1, "CURL initialisation failed");
+
+	val = json_web_config(curl, arg);
+
+	curl_easy_cleanup(curl);
+
+	if (!val || !json_is_object(val))
+		return JSON_WEB_ERROR;
+
+	if (!cnfbuf)
+		cnfbuf = strdup(arg);
+
+	config_loaded = true;
+
+	return parse_config(val, true);
+}
+
 static char *load_config(const char *arg, void __maybe_unused *unused)
 {
 	json_error_t err;
 	json_t *config;
 	char *json_error;
 	size_t siz;
+<<<<<<<
 	struct bfg_loaded_configfile *cfginfo;
+|||||||
+
+	if (!cnfbuf)
+		cnfbuf = strdup(arg);
+=======
+
+#ifdef HAVE_LIBCURL
+	if (strncasecmp(arg, conf_web1, sizeof(conf_web1)-1) == 0 ||
+	    strncasecmp(arg, conf_web2, sizeof(conf_web2)-1) == 0)
+		return load_web_config(arg);
+#endif
+
+	if (!cnfbuf)
+		cnfbuf = strdup(arg);
+>>>>>>>
 
 	cfginfo = malloc(sizeof(*cfginfo));
 	*cfginfo = (struct bfg_loaded_configfile){

+ 7 - 0
miner.h

@@ -1016,6 +1016,13 @@ extern int swork_id;
 extern pthread_rwlock_t netacc_lock;
 
 extern const uint32_t sha256_init_state[];
+<<<<<<< HEAD
+||||||| parent of c7d6886... allow url based config files
+#ifdef HAVE_LIBCURL
+=======
+#ifdef HAVE_LIBCURL
+extern json_t *json_web_config(CURL *curl, const char *url);
+>>>>>>> c7d6886... allow url based config files
 extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
 			     const char *rpc_req, bool, bool, int *,
 			     struct pool *pool, bool);

+ 60 - 0
util.c

@@ -450,7 +450,67 @@ static int curl_debug_cb(__maybe_unused CURL *handle, curl_infotype type,
 	return 0;
 }
 
+<<<<<<< HEAD
 void json_rpc_call_async(CURL *curl, const char *url,
+||||||| parent of c7d6886... allow url based config files
+json_t *json_rpc_call(CURL *curl, const char *url,
+=======
+json_t *json_web_config(CURL *curl, const char *url)
+{
+	struct data_buffer all_data = {NULL, 0};
+	char curl_err_str[CURL_ERROR_SIZE];
+	json_error_t err;
+	long timeout = 60;
+	json_t *val;
+	int rc;
+
+	memset(&err, 0, sizeof(err));
+
+	/* it is assumed that 'curl' is freshly [re]initialized at this pt */
+
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
+
+	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
+	curl_easy_setopt(curl, CURLOPT_URL, url);
+	curl_easy_setopt(curl, CURLOPT_ENCODING, "");
+	curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
+
+	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
+	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
+	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
+	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
+	curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
+
+	val = NULL;
+	rc = curl_easy_perform(curl);
+	if (rc) {
+		applog(LOG_ERR, "HTTP config request of '%s' failed: %s",
+				url, curl_err_str);
+		goto c_out;
+	}
+
+	if (!all_data.buf) {
+		applog(LOG_ERR, "Empty config data received from '%s'",
+				url);
+		goto c_out;
+	}
+
+	val = JSON_LOADS(all_data.buf, &err);
+	if (!val) {
+		applog(LOG_ERR, "JSON config decode of '%s' failed(%d): %s",
+				url, err.line, err.text);
+		goto c_out;
+	}
+
+c_out:
+	databuf_free(&all_data);
+	curl_easy_reset(curl);
+	curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
+	return val;
+}
+
+json_t *json_rpc_call(CURL *curl, const char *url,
+>>>>>>> c7d6886... allow url based config files
 		      const char *userpass, const char *rpc_req,
 		      bool longpoll,
 		      struct pool *pool, bool share,