Add API options for port and listen mode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
diff --git a/api-example.php b/api-example.php
index 12486c0..916a546 100644
--- a/api-example.php
+++ b/api-example.php
@@ -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)
#
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)
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";
#
?>
diff --git a/api.c b/api.c
index 791029d..4bd27d8 100644
--- a/api.c
+++ b/api.c
@@ -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)
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,9 +262,11 @@ void api(void)
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
- if (inet_pton(AF_INET, addr, &(serv.sin_addr)) == 0) {
- applog(LOG_ERR, "API2 initialisation failed (%s)%s", strerror(errno), UNAVAILABLE);
- return;
+ 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);
@@ -312,8 +312,10 @@ void api(void)
return;
}
- inet_ntop(AF_INET, &(cli.sin_addr), &(tmpaddr[0]), sizeof(tmpaddr)-1);
- if (strcmp(tmpaddr, addr) == 0) {
+ if (!opt_api_listen)
+ inet_ntop(AF_INET, &(cli.sin_addr), &(tmpaddr[0]), sizeof(tmpaddr)-1);
+
+ if (opt_api_listen || strcmp(tmpaddr, addr) == 0) {
n = read(c, &buf[0], BUFSIZ-1);
if (n >= 0) {
buf[n] = '\0';
diff --git a/main.c b/main.c
index fd0d8a3..d703892 100644
--- a/main.c
+++ b/main.c
@@ -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)
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[] = {
"\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)
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);
diff --git a/miner.h b/miner.h
index ea81a2a..9ad14b3 100644
--- a/miner.h
+++ b/miner.h
@@ -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,