Browse Source

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 <tanguy.pruvot@gmail.com>
2upstream
Tanguy Pruvot 10 years ago
parent
commit
2e0bc5e133
  1. 7
      README.txt
  2. 3
      api-example.php
  3. 30
      api.c
  4. 28
      cpu-miner.c

7
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 -q, --quiet disable per-thread hashmeter output
-D, --debug enable debug output -D, --debug enable debug output
-P, --protocol-dump verbose dump of protocol-level activities -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 --benchmark run in offline benchmark mode
--cputest debug hashes from cpu algorithms --cputest debug hashes from cpu algorithms
-c, --config=FILE load a JSON-format configuration file -c, --config=FILE load a JSON-format configuration file
@ -154,6 +155,10 @@ features.
>>> RELEASE HISTORY <<< >>> 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 Nov. 11th 2014 v1.4.7
Average hashrate (based on the 20 last scans) Average hashrate (based on the 20 last scans)
Rewrite blake algo Rewrite blake algo

3
api-example.php

@ -2,6 +2,7 @@
/** /**
* Sample Request API to ccminer * Sample Request API to ccminer
*/ */
defined('API_HOST') || define('API_HOST', '127.0.0.1');
defined('API_PORT') || define('API_PORT', 4068); defined('API_PORT') || define('API_PORT', 4068);
function getsock($port) function getsock($port)
@ -15,7 +16,7 @@ function getsock($port)
return NULL; return NULL;
} }
$res = socket_connect($socket, '127.0.0.1', $port); $res = socket_connect($socket, API_HOST, $port);
if ($res === false) { if ($res === false) {
$error = socket_strerror(socket_last_error()); $error = socket_strerror(socket_last_error());
$msg = "socket connect($port) failed"; $msg = "socket connect($port) failed";

30
api.c

@ -87,8 +87,9 @@ static struct IP4ACCESS *ipaccess = NULL;
// Socket is on 127.0.0.1 // Socket is on 127.0.0.1
#define QUEUE 10 #define QUEUE 10
#define LOCAL_ADDR_V4 "127.0.0.1" #define ALLIP4 "0.0.0.0"
static const char *localaddr = LOCAL_ADDR_V4;
static const char *localaddr = "127.0.0.1";
static const char *UNAVAILABLE = " - API will not be available"; static const char *UNAVAILABLE = " - API will not be available";
static char *buffer = NULL; static char *buffer = NULL;
static time_t startup = 0; static time_t startup = 0;
@ -96,13 +97,12 @@ static int bye = 0;
extern int opt_intensity; extern int opt_intensity;
extern int opt_n_threads; 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 uint64_t global_hashrate;
extern uint32_t accepted_count; extern uint32_t accepted_count;
extern uint32_t rejected_count; extern uint32_t rejected_count;
char *opt_api_allow = LOCAL_ADDR_V4;
int opt_api_network = 1;
#define gpu_threads opt_n_threads #define gpu_threads opt_n_threads
extern void get_currentalgo(char* buf, int sz); extern void get_currentalgo(char* buf, int sz);
@ -162,8 +162,8 @@ static void gpustatus(int thr_id)
static char *getsummary(char *params) static char *getsummary(char *params)
{ {
char algo[64] = ""; char algo[64] = "";
int uptime = (time(NULL) - startup); time_t uptime = (time(NULL) - startup);
double accps = (60.0 * accepted_count) / (uptime ? uptime : 1.0); double accps = (60.0 * accepted_count) / (uptime ? (uint32_t) uptime : 1.0);
get_currentalgo(algo, sizeof(algo)); 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); 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 * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
*/ */
@ -285,8 +280,8 @@ static void setup_ipaccess()
dot = strchr(ptr, '.'); dot = strchr(ptr, '.');
if (dot) if (dot)
*(dot++) = '\0'; *(dot++) = '\0';
octet = atoi(ptr); octet = atoi(ptr);
if (octet < 0 || octet > 0xff) if (octet < 0 || octet > 0xff)
goto popipo; // skip invalid 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) { if ((client_ip & ipaccess[i].mask) == ipaccess[i].ip) {
addrok = true; addrok = true;
*group = ipaccess[i].group; *group = ipaccess[i].group;
applog(LOG_BLUE, "ip accepted %x", ipaccess[i].ip);
break; break;
} }
} }
} else if (opt_api_network) }
addrok = true;
else else
addrok = (strcmp(*connectaddr, localaddr) == 0); addrok = (strcmp(*connectaddr, localaddr) == 0);
@ -332,7 +327,7 @@ static bool check_connect(struct sockaddr_in *cli, char **connectaddr, char *gro
static void api() static void api()
{ {
const char *addr = localaddr; const char *addr = opt_api_allow;
short int port = opt_api_listen; // 4068 short int port = opt_api_listen; // 4068
char buf[MYBUFSIZ]; char buf[MYBUFSIZ];
int c, n, bound; int c, n, bound;
@ -375,7 +370,7 @@ static void api()
memset(&serv, 0, sizeof(serv)); memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET; 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) { if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
applog(LOG_ERR, "API initialisation 2 failed (%s)%s", strerror(errno), UNAVAILABLE); applog(LOG_ERR, "API initialisation 2 failed (%s)%s", strerror(errno), UNAVAILABLE);
return; return;
@ -414,7 +409,6 @@ static void api()
bound = 1; bound = 1;
} }
if (bound == 0) { if (bound == 0) {
applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE); applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
free(apisock); free(apisock);

28
cpu-miner.c

@ -241,7 +241,8 @@ int opt_statsavg = 20;
int opt_intensity = 0; int opt_intensity = 0;
uint32_t opt_work_size = 0; /* default */ 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 #ifdef HAVE_GETOPT_LONG
#include <getopt.h> #include <getopt.h>
@ -310,7 +311,9 @@ Options:\n\
-q, --quiet disable per-thread hashmeter output\n\ -q, --quiet disable per-thread hashmeter output\n\
--no-color disable colored output\n\ --no-color disable colored output\n\
-D, --debug enable debug 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 #ifdef HAVE_SYSLOG_H
"\ "\
-S, --syslog use system log for output messages\n" -S, --syslog use system log for output messages\n"
@ -334,10 +337,11 @@ static char const short_options[] =
#ifdef HAVE_SYSLOG_H #ifdef HAVE_SYSLOG_H
"S" "S"
#endif #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[] = { static struct option const options[] = {
{ "algo", 1, NULL, 'a' }, { "algo", 1, NULL, 'a' },
{ "api-bind", 1, NULL, 'b' },
#ifndef WIN32 #ifndef WIN32
{ "background", 0, NULL, 'B' }, { "background", 0, NULL, 'B' },
#endif #endif
@ -1578,7 +1582,6 @@ out:
return NULL; return NULL;
} }
#define PROGRAM_VERSION "1.4.8"
static void show_version_and_exit(void) static void show_version_and_exit(void)
{ {
printf("%s v%s\n" printf("%s v%s\n"
@ -1586,7 +1589,7 @@ static void show_version_and_exit(void)
"pthreads static %s\n" "pthreads static %s\n"
#endif #endif
"%s\n", "%s\n",
PACKAGE_STRING, PROGRAM_VERSION, PACKAGE_NAME, PACKAGE_VERSION,
#ifdef WIN32 #ifdef WIN32
PTW32_VERSION_STRING, PTW32_VERSION_STRING,
#endif #endif
@ -1621,6 +1624,19 @@ static void parse_arg(int key, char *arg)
if (i == ARRAY_SIZE(algo_names)) if (i == ARRAY_SIZE(algo_names))
show_usage_and_exit(1); show_usage_and_exit(1);
break; 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': case 'B':
opt_background = true; opt_background = true;
break; break;
@ -1969,7 +1985,7 @@ int main(int argc, char *argv[])
long flags; long flags;
int i; 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 #ifdef WIN32
printf("\tBuilt with VC++ 2013 and nVidia CUDA SDK 6.5\n\n"); printf("\tBuilt with VC++ 2013 and nVidia CUDA SDK 6.5\n\n");
#else #else

Loading…
Cancel
Save