From 2e0bc5e13318272743fc5486abb813f2b8d20b5e Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Wed, 12 Nov 2014 17:28:34 +0100 Subject: [PATCH] api: add api-bind param -b 0.0.0.0:4068 possible values : 5000 or :5000 to use port 5000 (local only) 0.0.0.0:5000 to allow connections from the network 127.0.0.1:4068 to only allow local connections (default) Use -b 0 to disable the API system. Signed-off-by: Tanguy Pruvot --- README.txt | 7 ++++++- api-example.php | 3 ++- api.c | 30 ++++++++++++------------------ cpu-miner.c | 28 ++++++++++++++++++++++------ 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/README.txt b/README.txt index 6f5b0ee..ac695fe 100644 --- a/README.txt +++ b/README.txt @@ -1,5 +1,5 @@ -ccMiner release 1.4.7-tpruvot (Nov 2014) - "Blake Intensity" +ccMiner release 1.4.8-tpruvot (12 Nov 2014) - "API Stats" --------------------------------------------------------------- *************************************************************** @@ -107,6 +107,7 @@ its command line interface and options. -q, --quiet disable per-thread hashmeter output -D, --debug enable debug output -P, --protocol-dump verbose dump of protocol-level activities + -b, --api-bind IP/Port for the miner API (default: 127.0.0.1:4068) --benchmark run in offline benchmark mode --cputest debug hashes from cpu algorithms -c, --config=FILE load a JSON-format configuration file @@ -154,6 +155,10 @@ features. >>> RELEASE HISTORY <<< + Nov. 12th 2014 v1.4.8 + Add a basic API and sample php json wrapper + Fix displayed hashrate for multi gpus systems + Nov. 11th 2014 v1.4.7 Average hashrate (based on the 20 last scans) Rewrite blake algo diff --git a/api-example.php b/api-example.php index 86e8ac6..8459922 100644 --- a/api-example.php +++ b/api-example.php @@ -2,6 +2,7 @@ /** * Sample Request API to ccminer */ +defined('API_HOST') || define('API_HOST', '127.0.0.1'); defined('API_PORT') || define('API_PORT', 4068); function getsock($port) @@ -15,7 +16,7 @@ function getsock($port) return NULL; } - $res = socket_connect($socket, '127.0.0.1', $port); + $res = socket_connect($socket, API_HOST, $port); if ($res === false) { $error = socket_strerror(socket_last_error()); $msg = "socket connect($port) failed"; diff --git a/api.c b/api.c index 4a7a173..45355fc 100644 --- a/api.c +++ b/api.c @@ -87,8 +87,9 @@ static struct IP4ACCESS *ipaccess = NULL; // Socket is on 127.0.0.1 #define QUEUE 10 -#define LOCAL_ADDR_V4 "127.0.0.1" -static const char *localaddr = LOCAL_ADDR_V4; +#define ALLIP4 "0.0.0.0" + +static const char *localaddr = "127.0.0.1"; static const char *UNAVAILABLE = " - API will not be available"; static char *buffer = NULL; static time_t startup = 0; @@ -96,13 +97,12 @@ static int bye = 0; extern int opt_intensity; extern int opt_n_threads; -extern int opt_api_listen; +extern char *opt_api_allow; +extern int opt_api_listen; /* port */ extern uint64_t global_hashrate; extern uint32_t accepted_count; extern uint32_t rejected_count; -char *opt_api_allow = LOCAL_ADDR_V4; -int opt_api_network = 1; #define gpu_threads opt_n_threads extern void get_currentalgo(char* buf, int sz); @@ -162,8 +162,8 @@ static void gpustatus(int thr_id) static char *getsummary(char *params) { char algo[64] = ""; - int uptime = (time(NULL) - startup); - double accps = (60.0 * accepted_count) / (uptime ? uptime : 1.0); + time_t uptime = (time(NULL) - startup); + double accps = (60.0 * accepted_count) / (uptime ? (uint32_t) uptime : 1.0); get_currentalgo(algo, sizeof(algo)); @@ -206,11 +206,6 @@ static void send_result(SOCKETTYPE c, char *result) n = send(c, result, strlen(result) + 1, 0); } -/* - * Interpret [W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option - * special case of 0/0 allows /0 (means all IP addresses) - */ -#define ALLIP4 "0/0" /* * N.B. IP4 addresses are by Definition 32bit big endian on all platforms */ @@ -285,8 +280,8 @@ static void setup_ipaccess() dot = strchr(ptr, '.'); if (dot) *(dot++) = '\0'; - octet = atoi(ptr); + if (octet < 0 || octet > 0xff) goto popipo; // skip invalid @@ -319,11 +314,11 @@ static bool check_connect(struct sockaddr_in *cli, char **connectaddr, char *gro if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) { addrok = true; *group = ipaccess[i].group; + applog(LOG_BLUE, "ip accepted %x", ipaccess[i].ip); break; } } - } else if (opt_api_network) - addrok = true; + } else addrok = (strcmp(*connectaddr, localaddr) == 0); @@ -332,7 +327,7 @@ static bool check_connect(struct sockaddr_in *cli, char **connectaddr, char *gro static void api() { - const char *addr = localaddr; + const char *addr = opt_api_allow; short int port = opt_api_listen; // 4068 char buf[MYBUFSIZ]; int c, n, bound; @@ -375,7 +370,7 @@ static void api() memset(&serv, 0, sizeof(serv)); serv.sin_family = AF_INET; - serv.sin_addr.s_addr = inet_addr(localaddr); + serv.sin_addr.s_addr = inet_addr(addr); if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) { applog(LOG_ERR, "API initialisation 2 failed (%s)%s", strerror(errno), UNAVAILABLE); return; @@ -414,7 +409,6 @@ static void api() bound = 1; } - if (bound == 0) { applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE); free(apisock); diff --git a/cpu-miner.c b/cpu-miner.c index cf551ce..9cae3e3 100644 --- a/cpu-miner.c +++ b/cpu-miner.c @@ -241,7 +241,8 @@ int opt_statsavg = 20; int opt_intensity = 0; uint32_t opt_work_size = 0; /* default */ -int opt_api_listen = 4068; +char *opt_api_allow = "127.0.0.1"; /* 0.0.0.0 for all ips */ +int opt_api_listen = 4068; /* 0 to disable */ #ifdef HAVE_GETOPT_LONG #include @@ -310,7 +311,9 @@ Options:\n\ -q, --quiet disable per-thread hashmeter output\n\ --no-color disable colored output\n\ -D, --debug enable debug output\n\ - -P, --protocol-dump verbose dump of protocol-level activities\n" + -P, --protocol-dump verbose dump of protocol-level activities\n\ + -b, --api-bind IP/Port for the miner API (default: 127.0.0.1:4068)\n" + #ifdef HAVE_SYSLOG_H "\ -S, --syslog use system log for output messages\n" @@ -334,10 +337,11 @@ static char const short_options[] = #ifdef HAVE_SYSLOG_H "S" #endif - "a:c:i:Dhp:Px:qr:R:s:t:T:o:u:O:Vd:f:mv:N:"; + "a:c:i:Dhp:Px:qr:R:s:t:T:o:u:O:Vd:f:mv:N:b:"; static struct option const options[] = { { "algo", 1, NULL, 'a' }, + { "api-bind", 1, NULL, 'b' }, #ifndef WIN32 { "background", 0, NULL, 'B' }, #endif @@ -1578,7 +1582,6 @@ out: return NULL; } -#define PROGRAM_VERSION "1.4.8" static void show_version_and_exit(void) { printf("%s v%s\n" @@ -1586,7 +1589,7 @@ static void show_version_and_exit(void) "pthreads static %s\n" #endif "%s\n", - PACKAGE_STRING, PROGRAM_VERSION, + PACKAGE_NAME, PACKAGE_VERSION, #ifdef WIN32 PTW32_VERSION_STRING, #endif @@ -1621,6 +1624,19 @@ static void parse_arg(int key, char *arg) if (i == ARRAY_SIZE(algo_names)) show_usage_and_exit(1); break; + case 'b': + p = strstr(arg, ":"); + if (p) { + /* ip:port */ + if (p - arg > 0) { + opt_api_allow = strdup(arg); + opt_api_allow[p - arg] = '\0'; + } + opt_api_listen = atoi(p + 1); + } + else if (arg) + opt_api_listen = atoi(arg); + break; case 'B': opt_background = true; break; @@ -1969,7 +1985,7 @@ int main(int argc, char *argv[]) long flags; int i; - printf("*** ccminer " PROGRAM_VERSION " for nVidia GPUs by tpruvot@github ***\n"); + printf("*** ccminer " PACKAGE_VERSION " for nVidia GPUs by tpruvot@github ***\n"); #ifdef WIN32 printf("\tBuilt with VC++ 2013 and nVidia CUDA SDK 6.5\n\n"); #else