Browse Source

Add API options for port and listen mode

nfactor-troky
Kano 13 years ago
parent
commit
df3fad3dbc
  1. 25
      api-example.php
  2. 12
      api.c
  3. 14
      main.c
  4. 2
      miner.h

25
api-example.php

@ -2,23 +2,23 @@ @@ -2,23 +2,23 @@
#
# Sample Socket I/O to CGMiner API
#
function getsock($port)
function getsock($addr, $port)
{
$socket = null;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null)
{
$error = socket_strerror(socket_last_error());
$msg = "socket create($port) failed";
$msg = "socket create(TCP) failed";
echo "ERR: $msg '$error'\n";
return NULL;
}
$res = socket_connect($socket, '127.0.0.1', $port);
$res = socket_connect($socket, $addr, $port);
if ($res === false)
{
$error = socket_strerror(socket_last_error());
$msg = "socket connect($port) failed";
$msg = "socket connect($addr,$port) failed";
echo "ERR: $msg '$error'\n";
socket_close($socket);
return NULL;
@ -44,7 +44,7 @@ function readsockline($socket) @@ -44,7 +44,7 @@ function readsockline($socket)
#
function request($cmd)
{
$socket = getsock(4028);
$socket = getsock('127.0.0.1', 4028);
if ($socket != null)
{
socket_write($socket, $cmd, strlen($cmd));
@ -105,13 +105,16 @@ function request($cmd) @@ -105,13 +105,16 @@ function request($cmd)
return null;
}
#
$ver = request('apiversion');
echo print_r($ver, true)."\n";
$r = request('apiversion');
echo print_r($r, true)."\n";
#
$dev = request('dev');
echo print_r($dev, true)."\n";
$r = request('dev');
echo print_r($r, true)."\n";
#
$pool = request('pool');
echo print_r($pool, true)."\n";
$r = request('pool');
echo print_r($r, true)."\n";
#
$r = request('summary');
echo print_r($r, true)."\n";
#
?>

12
api.c

@ -239,12 +239,12 @@ void send_result(int c, char *result) @@ -239,12 +239,12 @@ void send_result(int c, char *result)
void api(void)
{
char buf[BUFSIZ];
const char *addr;
const char *addr = "127.0.0.1";
int c, sock, n, bound;
char tmpaddr[32];
char *binderror;
time_t bindstart;
short int port = 4028;
short int port = opt_api_port;
struct sockaddr_in serv;
struct sockaddr_in cli;
socklen_t clisiz;
@ -253,8 +253,6 @@ void api(void) @@ -253,8 +253,6 @@ void api(void)
char *params;
int i;
addr = "127.0.0.1";
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
applog(LOG_ERR, "API1 initialisation failed (%s)%s", strerror(errno), UNAVAILABLE);
@ -264,10 +262,12 @@ void api(void) @@ -264,10 +262,12 @@ void api(void)
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
if (!opt_api_listen) {
if (inet_pton(AF_INET, addr, &(serv.sin_addr)) == 0) {
applog(LOG_ERR, "API2 initialisation failed (%s)%s", strerror(errno), UNAVAILABLE);
return;
}
}
serv.sin_port = htons(port);
// try for 1 minute ... in case the old one hasn't completely gone yet
@ -312,8 +312,10 @@ void api(void) @@ -312,8 +312,10 @@ void api(void)
return;
}
if (!opt_api_listen)
inet_ntop(AF_INET, &(cli.sin_addr), &(tmpaddr[0]), sizeof(tmpaddr)-1);
if (strcmp(tmpaddr, addr) == 0) {
if (opt_api_listen || strcmp(tmpaddr, addr) == 0) {
n = read(c, &buf[0], BUFSIZ-1);
if (n >= 0) {
buf[n] = '\0';

14
main.c

@ -222,6 +222,8 @@ static bool opt_fail_only; @@ -222,6 +222,8 @@ static bool opt_fail_only;
bool opt_autofan;
bool opt_autoengine;
bool opt_noadl;
int opt_api_port = 4028;
bool opt_api_listen;
char *opt_kernel_path;
char *cgminer_path;
@ -955,6 +957,11 @@ static char *set_int_0_to_9999(const char *arg, int *i) @@ -955,6 +957,11 @@ static char *set_int_0_to_9999(const char *arg, int *i)
return set_int_range(arg, i, 0, 9999);
}
static char *set_int_1_to_65535(const char *arg, int *i)
{
return set_int_range(arg, i, 1, 65535);
}
static char *force_nthreads_int(const char *arg, int *i)
{
forced_n_threads = true;
@ -1490,6 +1497,12 @@ static struct opt_table opt_config_table[] = { @@ -1490,6 +1497,12 @@ static struct opt_table opt_config_table[] = {
"\n\taltivec_4way\tAltivec implementation for PowerPC G4 and G5 machines"
#endif
),
OPT_WITHOUT_ARG("--api-listen",
opt_set_bool, &opt_api_listen,
"Enable API to listen on/for any address, default: only 127.0.0.1"),
OPT_WITH_ARG("--api-port",
set_int_1_to_65535, opt_show_intval, &opt_api_port,
"Port number of miner API, default: 4028"),
#ifdef HAVE_ADL
OPT_WITHOUT_ARG("--auto-fan",
opt_set_bool, &opt_autofan,
@ -3110,6 +3123,7 @@ static void write_config(FILE *fcfg) @@ -3110,6 +3123,7 @@ static void write_config(FILE *fcfg)
if (opt->type & OPT_HASARG &&
((void *)opt->cb_arg == (void *)set_int_0_to_9999 ||
(void *)opt->cb_arg == (void *)set_int_1_to_65535 ||
(void *)opt->cb_arg == (void *)set_int_0_to_10 ||
(void *)opt->cb_arg == (void *)set_int_1_to_10) && opt->desc != opt_hidden)
fprintf(fcfg, ",\n\"%s\" : \"%d\"", p+2, *(int *)opt->u.arg);

2
miner.h

@ -334,6 +334,8 @@ extern char *cgminer_path; @@ -334,6 +334,8 @@ extern char *cgminer_path;
extern bool opt_autofan;
extern bool opt_autoengine;
extern bool use_curses;
extern int opt_api_port;
extern bool opt_api_listen;
extern const uint32_t sha256_init_state[];
extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,

Loading…
Cancel
Save