miner.php implement API Multicast handling to automatically find your local net miners
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
diff --git a/miner.php b/miner.php
index f0f72d9..3714ea1 100644
--- a/miner.php
+++ b/miner.php
@@ -2,6 +2,8 @@
session_start();
#
global $title, $miner, $port, $readonly, $notify, $rigs;
+global $mcast, $mcastaddr, $mcastport, $mcastcode;
+global $mcastlistport, $mcasttimeout;
global $rigipsecurity, $rigtotals, $forcerigtotals;
global $socksndtimeoutsec, $sockrcvtimeoutsec;
global $checklastshare, $poolinputs, $hidefields;
@@ -43,6 +45,25 @@ $poolinputs = false;
# format: 'IP:Port' or 'Host:Port' or 'Host:Port:Name'
$rigs = array('127.0.0.1:4028');
#
+# Set $mcast to true to look for your rigs and ignore $rigs
+$mcast = false;
+#
+# API Multicast address all cgminers are listening on
+$mcastaddr = '224.0.0.75';
+#
+# API Multicast UDP port all cgminers are listening on
+$mcastport = 4028;
+#
+# The code all cgminers expect in the Multicast message sent
+$mcastcode = 'FTW';
+#
+# UDP port cgminers are to reply on (by request)
+$mcastlistport = 4027;
+#
+# Set $mcasttimeout to the number of seconds (floating point)
+# to wait for replies to the Multicast message
+$mcasttimeout = 1.5;
+#
# Set $rigipsecurity to false to show the IP/Port of the rig
# in the socket error messages and also show the full socket message
$rigipsecurity = true;
@@ -324,6 +345,88 @@ global $haderror, $error;
$haderror = false;
$error = null;
#
+function getrigs()
+{
+ global $rigs, $mcastaddr, $mcastport, $mcastcode, $mcasttimeout, $error;
+
+ $listname = "0.0.0.0";
+
+ $rigs = array();
+
+ $rep_soc = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
+ if ($rep_soc === false || $rep_soc == null)
+ {
+ $msg = "ERR: mcast listen socket create(UDP) failed";
+ if ($rigipsecurity === false)
+ {
+ $error = socket_strerror(socket_last_error());
+ $error = "$msg '$error'\n";
+ }
+ else
+ $error = "$msg\n";
+
+ return;
+ }
+
+ $res = socket_bind($rep_soc, $listname, $mcastlistport);
+ if ($res === false)
+ {
+ $msg1 = "ERR: mcast listen socket bind(";
+ $msg2 = ") failed";
+ if ($rigipsecurity === false)
+ {
+ $error = socket_strerror(socket_last_error());
+ $error = "$msg1$listname,$mcastlistport$msg2 '$error'\n";
+ }
+ else
+ $error = "$msg1$msg2\n";
+
+ socket_close($rep_soc);
+ return;
+ }
+
+ $mcast_soc = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
+ if ($mcast_soc === false || $mcast_soc == null)
+ {
+ $msg = "ERR: mcast send socket create(UDP) failed";
+ if ($rigipsecurity === false)
+ {
+ $error = socket_strerror(socket_last_error());
+ $error = "$msg '$error'\n";
+ }
+ else
+ $error = "$msg\n";
+
+ socket_close($rep_soc);
+ return;
+ }
+
+ $buf = "cgminer-$mcastcode-$mcastlistport";
+ socket_sendto($mcast_soc, $buf, strlen($buf), 0, $mcastaddr, $mcastport);
+ socket_close($mcast_soc);
+
+ $stt = microtime(true);
+ while (true)
+ {
+ $got = @socket_recvfrom($rep_soc, $buf, 32, MSG_DONTWAIT, $ip, $p);
+ if ($got !== false && $got > 0)
+ {
+ $ans = explode('-', $buf);
+ if (count($ans) == 3 && $ans[0] == 'cgm' && $ans[1] == 'FTW')
+ {
+ $rp = intval($ans[2]);
+ $rigs[] = "$ip:$rp";
+ }
+ }
+ if ((microtime(true) - $stt) >= $mcasttimeout)
+ break;
+
+ usleep(100000);
+ }
+
+ socket_close($rep_soc);
+}
+#
function getsock($rig, $addr, $port)
{
global $rigipsecurity;
@@ -2768,6 +2871,8 @@ function display()
pagebuttons(null, null);
}
#
+if (isset($mcast) && $mcast === true)
+ getrigs();
display();
#
?>