api.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411
  1. /*
  2. * Copyright 2011-2012 Andrew Smith
  3. * Copyright 2011-2012 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. *
  10. * Note: the code always includes GPU support even if there are no GPUs
  11. * this simplifies handling multiple other device code being included
  12. * depending on compile options
  13. */
  14. #include "config.h"
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include "compat.h"
  24. #include "miner.h"
  25. #include "driver-cpu.h" /* for algo_names[], TODO: re-factor dependency */
  26. #if defined(unix) || defined(__APPLE__)
  27. #include <errno.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #define SOCKETTYPE int
  32. #define SOCKETFAIL(a) ((a) < 0)
  33. #define INVSOCK -1
  34. #define INVINETADDR -1
  35. #define CLOSESOCKET close
  36. #define SOCKERRMSG strerror(errno)
  37. #endif
  38. #ifdef WIN32
  39. #include <ws2tcpip.h>
  40. #include <winsock2.h>
  41. #define SOCKETTYPE SOCKET
  42. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  43. #define INVSOCK INVALID_SOCKET
  44. #define INVINETADDR INADDR_NONE
  45. #define CLOSESOCKET closesocket
  46. static char WSAbuf[1024];
  47. struct WSAERRORS {
  48. int id;
  49. char *code;
  50. } WSAErrors[] = {
  51. { 0, "No error" },
  52. { WSAEINTR, "Interrupted system call" },
  53. { WSAEBADF, "Bad file number" },
  54. { WSAEACCES, "Permission denied" },
  55. { WSAEFAULT, "Bad address" },
  56. { WSAEINVAL, "Invalid argument" },
  57. { WSAEMFILE, "Too many open sockets" },
  58. { WSAEWOULDBLOCK, "Operation would block" },
  59. { WSAEINPROGRESS, "Operation now in progress" },
  60. { WSAEALREADY, "Operation already in progress" },
  61. { WSAENOTSOCK, "Socket operation on non-socket" },
  62. { WSAEDESTADDRREQ, "Destination address required" },
  63. { WSAEMSGSIZE, "Message too long" },
  64. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  65. { WSAENOPROTOOPT, "Bad protocol option" },
  66. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  67. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  68. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  69. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  70. { WSAEAFNOSUPPORT, "Address family not supported" },
  71. { WSAEADDRINUSE, "Address already in use" },
  72. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  73. { WSAENETDOWN, "Network is down" },
  74. { WSAENETUNREACH, "Network is unreachable" },
  75. { WSAENETRESET, "Net connection reset" },
  76. { WSAECONNABORTED, "Software caused connection abort" },
  77. { WSAECONNRESET, "Connection reset by peer" },
  78. { WSAENOBUFS, "No buffer space available" },
  79. { WSAEISCONN, "Socket is already connected" },
  80. { WSAENOTCONN, "Socket is not connected" },
  81. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  82. { WSAETOOMANYREFS, "Too many references, can't splice" },
  83. { WSAETIMEDOUT, "Connection timed out" },
  84. { WSAECONNREFUSED, "Connection refused" },
  85. { WSAELOOP, "Too many levels of symbolic links" },
  86. { WSAENAMETOOLONG, "File name too long" },
  87. { WSAEHOSTDOWN, "Host is down" },
  88. { WSAEHOSTUNREACH, "No route to host" },
  89. { WSAENOTEMPTY, "Directory not empty" },
  90. { WSAEPROCLIM, "Too many processes" },
  91. { WSAEUSERS, "Too many users" },
  92. { WSAEDQUOT, "Disc quota exceeded" },
  93. { WSAESTALE, "Stale NFS file handle" },
  94. { WSAEREMOTE, "Too many levels of remote in path" },
  95. { WSASYSNOTREADY, "Network system is unavailable" },
  96. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  97. { WSANOTINITIALISED, "WSAStartup not yet called" },
  98. { WSAEDISCON, "Graceful shutdown in progress" },
  99. { WSAHOST_NOT_FOUND, "Host not found" },
  100. { WSANO_DATA, "No host data of that type was found" },
  101. { -1, "Unknown error code" }
  102. };
  103. static char *WSAErrorMsg()
  104. {
  105. int i;
  106. int id = WSAGetLastError();
  107. /* Assume none of them are actually -1 */
  108. for (i = 0; WSAErrors[i].id != -1; i++)
  109. if (WSAErrors[i].id == id)
  110. break;
  111. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  112. return &(WSAbuf[0]);
  113. }
  114. #define SOCKERRMSG WSAErrorMsg()
  115. #ifndef SHUT_RDWR
  116. #define SHUT_RDWR SD_BOTH
  117. #endif
  118. #ifndef in_addr_t
  119. #define in_addr_t uint32_t
  120. #endif
  121. #endif
  122. // Big enough for largest API request
  123. // though a PC with 100s of PGAs/CPUs may exceed the size ...
  124. // Current code assumes it can socket send this size also
  125. #define MYBUFSIZ 65432 // TODO: intercept before it's exceeded
  126. // Number of requests to queue - normally would be small
  127. // However lots of PGA's may mean more
  128. #define QUEUE 100
  129. static char *io_buffer = NULL;
  130. static char *msg_buffer = NULL;
  131. static SOCKETTYPE sock = INVSOCK;
  132. static const char *UNAVAILABLE = " - API will not be available";
  133. static const char *BLANK = "";
  134. static const char *COMMA = ",";
  135. static const char SEPARATOR = '|';
  136. #define SEPSTR "|"
  137. static const char GPUSEP = ',';
  138. static const char *APIVERSION = "1.8";
  139. static const char *DEAD = "Dead";
  140. static const char *SICK = "Sick";
  141. static const char *NOSTART = "NoStart";
  142. static const char *DISABLED = "Disabled";
  143. static const char *ALIVE = "Alive";
  144. #define _DYNAMIC "D"
  145. static const char *DYNAMIC = _DYNAMIC;
  146. static const char *YES = "Y";
  147. static const char *NO = "N";
  148. static const char *DEVICECODE = ""
  149. #ifdef HAVE_OPENCL
  150. "GPU "
  151. #endif
  152. #ifdef USE_BITFORCE
  153. "BFL "
  154. #endif
  155. #ifdef USE_ICARUS
  156. "ICA "
  157. #endif
  158. #ifdef USE_ZTEX
  159. "ZTX "
  160. #endif
  161. #ifdef WANT_CPUMINE
  162. "CPU "
  163. #endif
  164. "";
  165. static const char *OSINFO =
  166. #if defined(__linux)
  167. "Linux";
  168. #else
  169. #if defined(__APPLE__)
  170. "Apple";
  171. #else
  172. #if defined (WIN32)
  173. "Windows";
  174. #else
  175. #if defined(unix)
  176. "Unix";
  177. #else
  178. "Unknown";
  179. #endif
  180. #endif
  181. #endif
  182. #endif
  183. #define _DEVS "DEVS"
  184. #define _POOLS "POOLS"
  185. #define _SUMMARY "SUMMARY"
  186. #define _STATUS "STATUS"
  187. #define _VERSION "VERSION"
  188. #define _MINECON "CONFIG"
  189. #define _GPU "GPU"
  190. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  191. #define _PGA "PGA"
  192. #endif
  193. #ifdef WANT_CPUMINE
  194. #define _CPU "CPU"
  195. #endif
  196. #define _GPUS "GPUS"
  197. #define _PGAS "PGAS"
  198. #define _CPUS "CPUS"
  199. #define _NOTIFY "NOTIFY"
  200. #define _DEVDETAILS "DEVDETAILS"
  201. #define _BYE "BYE"
  202. static const char ISJSON = '{';
  203. #define JSON0 "{"
  204. #define JSON1 "\""
  205. #define JSON2 "\":["
  206. #define JSON3 "]"
  207. #define JSON4 ",\"id\":1}"
  208. #define JSON_START JSON0
  209. #define JSON_DEVS JSON1 _DEVS JSON2
  210. #define JSON_POOLS JSON1 _POOLS JSON2
  211. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  212. #define JSON_STATUS JSON1 _STATUS JSON2
  213. #define JSON_VERSION JSON1 _VERSION JSON2
  214. #define JSON_MINECON JSON1 _MINECON JSON2
  215. #define JSON_GPU JSON1 _GPU JSON2
  216. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  217. #define JSON_PGA JSON1 _PGA JSON2
  218. #endif
  219. #ifdef WANT_CPUMINE
  220. #define JSON_CPU JSON1 _CPU JSON2
  221. #endif
  222. #define JSON_GPUS JSON1 _GPUS JSON2
  223. #define JSON_PGAS JSON1 _PGAS JSON2
  224. #define JSON_CPUS JSON1 _CPUS JSON2
  225. #define JSON_NOTIFY JSON1 _NOTIFY JSON2
  226. #define JSON_DEVDETAILS JSON1 _DEVDETAILS JSON2
  227. #define JSON_BYE JSON1 _BYE JSON1
  228. #define JSON_CLOSE JSON3
  229. #define JSON_END JSON4
  230. static const char *JSON_COMMAND = "command";
  231. static const char *JSON_PARAMETER = "parameter";
  232. #define MSG_INVGPU 1
  233. #define MSG_ALRENA 2
  234. #define MSG_ALRDIS 3
  235. #define MSG_GPUMRE 4
  236. #define MSG_GPUREN 5
  237. #define MSG_GPUNON 6
  238. #define MSG_POOL 7
  239. #define MSG_NOPOOL 8
  240. #define MSG_DEVS 9
  241. #define MSG_NODEVS 10
  242. #define MSG_SUMM 11
  243. #define MSG_GPUDIS 12
  244. #define MSG_GPUREI 13
  245. #define MSG_INVCMD 14
  246. #define MSG_MISID 15
  247. #define MSG_GPUDEV 17
  248. #ifdef WANT_CPUMINE
  249. #define MSG_CPUNON 16
  250. #define MSG_CPUDEV 18
  251. #define MSG_INVCPU 19
  252. #endif
  253. #define MSG_NUMGPU 20
  254. #define MSG_NUMCPU 21
  255. #define MSG_VERSION 22
  256. #define MSG_INVJSON 23
  257. #define MSG_MISCMD 24
  258. #define MSG_MISPID 25
  259. #define MSG_INVPID 26
  260. #define MSG_SWITCHP 27
  261. #define MSG_MISVAL 28
  262. #define MSG_NOADL 29
  263. #define MSG_NOGPUADL 30
  264. #define MSG_INVINT 31
  265. #define MSG_GPUINT 32
  266. #define MSG_MINECON 33
  267. #define MSG_GPUMERR 34
  268. #define MSG_GPUMEM 35
  269. #define MSG_GPUEERR 36
  270. #define MSG_GPUENG 37
  271. #define MSG_GPUVERR 38
  272. #define MSG_GPUVDDC 39
  273. #define MSG_GPUFERR 40
  274. #define MSG_GPUFAN 41
  275. #define MSG_MISFN 42
  276. #define MSG_BADFN 43
  277. #define MSG_SAVED 44
  278. #define MSG_ACCDENY 45
  279. #define MSG_ACCOK 46
  280. #define MSG_ENAPOOL 47
  281. #define MSG_DISPOOL 48
  282. #define MSG_ALRENAP 49
  283. #define MSG_ALRDISP 50
  284. #define MSG_DISLASTP 51
  285. #define MSG_MISPDP 52
  286. #define MSG_INVPDP 53
  287. #define MSG_TOOMANYP 54
  288. #define MSG_ADDPOOL 55
  289. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  290. #define MSG_PGANON 56
  291. #define MSG_PGADEV 57
  292. #define MSG_INVPGA 58
  293. #endif
  294. #define MSG_NUMPGA 59
  295. #define MSG_NOTIFY 60
  296. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  297. #define MSG_PGALRENA 61
  298. #define MSG_PGALRDIS 62
  299. #define MSG_PGAENA 63
  300. #define MSG_PGADIS 64
  301. #define MSG_PGAUNW 65
  302. #endif
  303. #define MSG_REMLASTP 66
  304. #define MSG_ACTPOOL 67
  305. #define MSG_REMPOOL 68
  306. #define MSG_DEVDETAILS 69
  307. enum code_severity {
  308. SEVERITY_ERR,
  309. SEVERITY_WARN,
  310. SEVERITY_INFO,
  311. SEVERITY_SUCC,
  312. SEVERITY_FAIL
  313. };
  314. enum code_parameters {
  315. PARAM_GPU,
  316. PARAM_PGA,
  317. PARAM_CPU,
  318. PARAM_GPUMAX,
  319. PARAM_PGAMAX,
  320. PARAM_CPUMAX,
  321. PARAM_PMAX,
  322. PARAM_POOLMAX,
  323. // Single generic case: have the code resolve it - see below
  324. PARAM_DMAX,
  325. PARAM_CMD,
  326. PARAM_POOL,
  327. PARAM_STR,
  328. PARAM_BOTH,
  329. PARAM_NONE
  330. };
  331. struct CODES {
  332. const enum code_severity severity;
  333. const int code;
  334. const enum code_parameters params;
  335. const char *description;
  336. } codes[] = {
  337. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  338. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  339. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  340. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  341. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  342. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  343. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  344. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  345. { SEVERITY_SUCC, MSG_DEVS, PARAM_DMAX, "%d GPU(s)"
  346. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  347. " - %d PGA(s)"
  348. #endif
  349. #ifdef WANT_CPUMINE
  350. " - %d CPU(s)"
  351. #endif
  352. },
  353. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs"
  354. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  355. "/PGAs"
  356. #endif
  357. #ifdef WANT_CPUMINE
  358. "/CPUs"
  359. #endif
  360. },
  361. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  362. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  363. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  364. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  365. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  366. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  367. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  368. { SEVERITY_ERR, MSG_PGANON, PARAM_NONE, "No PGAs" },
  369. { SEVERITY_SUCC, MSG_PGADEV, PARAM_PGA, "PGA%d" },
  370. { SEVERITY_ERR, MSG_INVPGA, PARAM_PGAMAX, "Invalid PGA id %d - range is 0 - %d" },
  371. { SEVERITY_INFO, MSG_PGALRENA,PARAM_PGA, "PGA %d already enabled" },
  372. { SEVERITY_INFO, MSG_PGALRDIS,PARAM_PGA, "PGA %d already disabled" },
  373. { SEVERITY_INFO, MSG_PGAENA, PARAM_PGA, "PGA %d sent enable message" },
  374. { SEVERITY_INFO, MSG_PGADIS, PARAM_PGA, "PGA %d set disable flag" },
  375. { SEVERITY_ERR, MSG_PGAUNW, PARAM_PGA, "PGA %d is not flagged WELL, cannot enable" },
  376. #endif
  377. #ifdef WANT_CPUMINE
  378. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  379. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  380. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  381. #endif
  382. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  383. { SEVERITY_SUCC, MSG_NUMPGA, PARAM_NONE, "PGA count" },
  384. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  385. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
  386. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  387. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  388. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  389. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  390. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  391. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  392. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  393. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  394. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  395. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  396. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "CGMiner config" },
  397. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  398. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported succeess" },
  399. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  400. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported succeess" },
  401. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  402. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported succeess" },
  403. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  404. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported succeess" },
  405. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  406. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  407. { SEVERITY_ERR, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  408. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  409. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  410. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  411. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  412. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  413. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  414. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  415. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  416. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  417. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  418. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  419. { SEVERITY_ERR, MSG_REMLASTP,PARAM_POOL, "Cannot remove last pool %d:'%s'" },
  420. { SEVERITY_ERR, MSG_ACTPOOL, PARAM_POOL, "Cannot remove active pool %d:'%s'" },
  421. { SEVERITY_SUCC, MSG_REMPOOL, PARAM_BOTH, "Removed pool %d:'%s'" },
  422. { SEVERITY_SUCC, MSG_NOTIFY, PARAM_NONE, "Notify" },
  423. { SEVERITY_SUCC, MSG_DEVDETAILS,PARAM_NONE, "Device Details" },
  424. { SEVERITY_FAIL, 0, 0, NULL }
  425. };
  426. static int my_thr_id = 0;
  427. static int bye = 0;
  428. static bool ping = true;
  429. static time_t when = 0; // when the request occurred
  430. struct IP4ACCESS {
  431. in_addr_t ip;
  432. in_addr_t mask;
  433. bool writemode;
  434. };
  435. static struct IP4ACCESS *ipaccess = NULL;
  436. static int ips = 0;
  437. #ifdef USE_BITFORCE
  438. extern struct device_api bitforce_api;
  439. #endif
  440. #ifdef USE_ICARUS
  441. extern struct device_api icarus_api;
  442. #endif
  443. #ifdef USE_ZTEX
  444. extern struct device_api ztex_api;
  445. #endif
  446. // This is only called when expected to be needed (rarely)
  447. // i.e. strings outside of the codes control (input from the user)
  448. static char *escape_string(char *str, bool isjson)
  449. {
  450. char *buf, *ptr;
  451. int count;
  452. count = 0;
  453. for (ptr = str; *ptr; ptr++) {
  454. switch (*ptr) {
  455. case ',':
  456. case '|':
  457. case '=':
  458. if (!isjson)
  459. count++;
  460. break;
  461. case '"':
  462. if (isjson)
  463. count++;
  464. break;
  465. case '\\':
  466. count++;
  467. break;
  468. }
  469. }
  470. if (count == 0)
  471. return str;
  472. buf = malloc(strlen(str) + count + 1);
  473. if (unlikely(!buf))
  474. quit(1, "Failed to malloc escape buf");
  475. ptr = buf;
  476. while (*str)
  477. switch (*str) {
  478. case ',':
  479. case '|':
  480. case '=':
  481. if (!isjson)
  482. *(ptr++) = '\\';
  483. *(ptr++) = *(str++);
  484. break;
  485. case '"':
  486. if (isjson)
  487. *(ptr++) = '\\';
  488. *(ptr++) = *(str++);
  489. break;
  490. case '\\':
  491. *(ptr++) = '\\';
  492. *(ptr++) = *(str++);
  493. break;
  494. default:
  495. *(ptr++) = *(str++);
  496. break;
  497. }
  498. *ptr = '\0';
  499. return buf;
  500. }
  501. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  502. static int numpgas()
  503. {
  504. int count = 0;
  505. int i;
  506. for (i = 0; i < total_devices; i++) {
  507. #ifdef USE_BITFORCE
  508. if (devices[i]->api == &bitforce_api)
  509. count++;
  510. #endif
  511. #ifdef USE_ICARUS
  512. if (devices[i]->api == &icarus_api)
  513. count++;
  514. #endif
  515. #ifdef USE_ZTEX
  516. if (devices[i]->api == &ztex_api)
  517. count++;
  518. #endif
  519. }
  520. return count;
  521. }
  522. static int pgadevice(int pgaid)
  523. {
  524. int count = 0;
  525. int i;
  526. for (i = 0; i < total_devices; i++) {
  527. #ifdef USE_BITFORCE
  528. if (devices[i]->api == &bitforce_api)
  529. count++;
  530. #endif
  531. #ifdef USE_ICARUS
  532. if (devices[i]->api == &icarus_api)
  533. count++;
  534. #endif
  535. #ifdef USE_ZTEX
  536. if (devices[i]->api == &ztex_api)
  537. count++;
  538. #endif
  539. if (count == (pgaid + 1))
  540. return i;
  541. }
  542. return -1;
  543. }
  544. #endif
  545. // All replies (except BYE) start with a message
  546. // thus for JSON, message() inserts JSON_START at the front
  547. // and send_result() adds JSON_END at the end
  548. static char *message(int messageid, int paramid, char *param2, bool isjson)
  549. {
  550. char severity;
  551. char *ptr;
  552. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  553. int pga;
  554. #endif
  555. #ifdef WANT_CPUMINE
  556. int cpu;
  557. #endif
  558. int i;
  559. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  560. if (codes[i].code == messageid) {
  561. switch (codes[i].severity) {
  562. case SEVERITY_WARN:
  563. severity = 'W';
  564. break;
  565. case SEVERITY_INFO:
  566. severity = 'I';
  567. break;
  568. case SEVERITY_SUCC:
  569. severity = 'S';
  570. break;
  571. case SEVERITY_ERR:
  572. default:
  573. severity = 'E';
  574. break;
  575. }
  576. sprintf(msg_buffer, isjson
  577. ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"When\":%lu,\"Code\":%d,\"Msg\":\""
  578. : _STATUS "=%c,When=%lu,Code=%d,Msg=",
  579. severity, (unsigned long)when, messageid);
  580. ptr = msg_buffer + strlen(msg_buffer);
  581. switch(codes[i].params) {
  582. case PARAM_GPU:
  583. case PARAM_PGA:
  584. case PARAM_CPU:
  585. sprintf(ptr, codes[i].description, paramid);
  586. break;
  587. case PARAM_POOL:
  588. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  589. break;
  590. case PARAM_GPUMAX:
  591. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  592. break;
  593. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  594. case PARAM_PGAMAX:
  595. pga = numpgas();
  596. sprintf(ptr, codes[i].description, paramid, pga - 1);
  597. break;
  598. #endif
  599. #ifdef WANT_CPUMINE
  600. case PARAM_CPUMAX:
  601. if (opt_n_threads > 0)
  602. cpu = num_processors;
  603. else
  604. cpu = 0;
  605. sprintf(ptr, codes[i].description, paramid, cpu - 1);
  606. break;
  607. #endif
  608. case PARAM_PMAX:
  609. sprintf(ptr, codes[i].description, total_pools);
  610. break;
  611. case PARAM_POOLMAX:
  612. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  613. break;
  614. case PARAM_DMAX:
  615. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  616. pga = numpgas();
  617. #endif
  618. #ifdef WANT_CPUMINE
  619. if (opt_n_threads > 0)
  620. cpu = num_processors;
  621. else
  622. cpu = 0;
  623. #endif
  624. sprintf(ptr, codes[i].description, nDevs
  625. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  626. , pga
  627. #endif
  628. #ifdef WANT_CPUMINE
  629. , cpu
  630. #endif
  631. );
  632. break;
  633. case PARAM_CMD:
  634. sprintf(ptr, codes[i].description, JSON_COMMAND);
  635. break;
  636. case PARAM_STR:
  637. sprintf(ptr, codes[i].description, param2);
  638. break;
  639. case PARAM_BOTH:
  640. sprintf(ptr, codes[i].description, paramid, param2);
  641. break;
  642. case PARAM_NONE:
  643. default:
  644. strcpy(ptr, codes[i].description);
  645. }
  646. ptr = msg_buffer + strlen(msg_buffer);
  647. sprintf(ptr, isjson
  648. ? "\",\"Description\":\"%s\"}" JSON_CLOSE
  649. : ",Description=%s" SEPSTR,
  650. opt_api_description);
  651. return msg_buffer;
  652. }
  653. }
  654. sprintf(msg_buffer, isjson
  655. ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"When\":%lu,\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE
  656. : _STATUS "=F,When=%lu,Code=-1,Msg=%d,Description=%s" SEPSTR,
  657. (unsigned long)when, messageid, opt_api_description);
  658. return msg_buffer;
  659. }
  660. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  661. {
  662. sprintf(io_buffer, isjson
  663. ? "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE
  664. : "%s" _VERSION ",CGMiner=%s,API=%s" SEPSTR,
  665. message(MSG_VERSION, 0, NULL, isjson),
  666. VERSION, APIVERSION);
  667. }
  668. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  669. {
  670. char buf[BUFSIZ];
  671. int pgacount = 0;
  672. int cpucount = 0;
  673. char *adlinuse = (char *)NO;
  674. #ifdef HAVE_ADL
  675. const char *adl = YES;
  676. int i;
  677. for (i = 0; i < nDevs; i++) {
  678. if (gpus[i].has_adl) {
  679. adlinuse = (char *)YES;
  680. break;
  681. }
  682. }
  683. #else
  684. const char *adl = NO;
  685. #endif
  686. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  687. pgacount = numpgas();
  688. #endif
  689. #ifdef WANT_CPUMINE
  690. cpucount = opt_n_threads > 0 ? num_processors : 0;
  691. #endif
  692. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  693. sprintf(buf, isjson
  694. ? "," JSON_MINECON "{\"GPU Count\":%d,\"PGA Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\",\"Log Interval\":%d,\"Device Code\":\"%s\",\"OS\":\"%s\"}" JSON_CLOSE
  695. : _MINECON ",GPU Count=%d,PGA Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s,Log Interval=%d,Device Code=%s,OS=%s" SEPSTR,
  696. nDevs, pgacount, cpucount, total_pools, adl, adlinuse,
  697. strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO);
  698. strcat(io_buffer, buf);
  699. }
  700. static void gpustatus(int gpu, bool isjson)
  701. {
  702. char intensity[20];
  703. char buf[BUFSIZ];
  704. char *enabled;
  705. char *status;
  706. float gt, gv;
  707. int ga, gf, gp, gc, gm, pt;
  708. if (gpu >= 0 && gpu < nDevs) {
  709. struct cgpu_info *cgpu = &gpus[gpu];
  710. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  711. #ifdef HAVE_ADL
  712. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  713. #endif
  714. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  715. if (cgpu->deven != DEV_DISABLED)
  716. enabled = (char *)YES;
  717. else
  718. enabled = (char *)NO;
  719. if (cgpu->status == LIFE_DEAD)
  720. status = (char *)DEAD;
  721. else if (cgpu->status == LIFE_SICK)
  722. status = (char *)SICK;
  723. else if (cgpu->status == LIFE_NOSTART)
  724. status = (char *)NOSTART;
  725. else
  726. status = (char *)ALIVE;
  727. if (cgpu->dynamic)
  728. strcpy(intensity, DYNAMIC);
  729. else
  730. sprintf(intensity, "%d", cgpu->intensity);
  731. sprintf(buf, isjson
  732. ? "{\"GPU\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"Fan Speed\":%d,\"Fan Percent\":%d,\"GPU Clock\":%d,\"Memory Clock\":%d,\"GPU Voltage\":%.3f,\"GPU Activity\":%d,\"Powertune\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Intensity\":\"%s\",\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f}"
  733. : "GPU=%d,Enabled=%s,Status=%s,Temperature=%.2f,Fan Speed=%d,Fan Percent=%d,GPU Clock=%d,Memory Clock=%d,GPU Voltage=%.3f,GPU Activity=%d,Powertune=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Intensity=%s,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f" SEPSTR,
  734. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  735. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  736. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  737. cgpu->utility, intensity,
  738. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  739. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  740. strcat(io_buffer, buf);
  741. }
  742. }
  743. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  744. static void pgastatus(int pga, bool isjson)
  745. {
  746. char buf[BUFSIZ];
  747. char *enabled;
  748. char *status;
  749. int numpga = numpgas();
  750. if (numpga > 0 && pga >= 0 && pga < numpga) {
  751. int dev = pgadevice(pga);
  752. if (dev < 0) // Should never happen
  753. return;
  754. struct cgpu_info *cgpu = devices[dev];
  755. double frequency = 0;
  756. #ifdef USE_ZTEX
  757. if (cgpu->api == &ztex_api && cgpu->device_ztex)
  758. frequency = cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1);
  759. #endif
  760. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  761. if (cgpu->deven != DEV_DISABLED)
  762. enabled = (char *)YES;
  763. else
  764. enabled = (char *)NO;
  765. if (cgpu->status == LIFE_DEAD)
  766. status = (char *)DEAD;
  767. else if (cgpu->status == LIFE_SICK)
  768. status = (char *)SICK;
  769. else if (cgpu->status == LIFE_NOSTART)
  770. status = (char *)NOSTART;
  771. else
  772. status = (char *)ALIVE;
  773. sprintf(buf, isjson
  774. ? "{\"PGA\":%d,\"Name\":\"%s\",\"ID\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f,\"Frequency\":%.2f}"
  775. : "PGA=%d,Name=%s,ID=%d,Enabled=%s,Status=%s,Temperature=%.2f,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f,Frequency=%.2f" SEPSTR,
  776. pga, cgpu->api->name, cgpu->device_id,
  777. enabled, status, cgpu->temp,
  778. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  779. cgpu->accepted, cgpu->rejected, cgpu->hw_errors, cgpu->utility,
  780. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  781. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, frequency);
  782. strcat(io_buffer, buf);
  783. }
  784. }
  785. #endif
  786. #ifdef WANT_CPUMINE
  787. static void cpustatus(int cpu, bool isjson)
  788. {
  789. char buf[BUFSIZ];
  790. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  791. struct cgpu_info *cgpu = &cpus[cpu];
  792. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  793. sprintf(buf, isjson
  794. ? "{\"CPU\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f}"
  795. : "CPU=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f" SEPSTR,
  796. cpu, cgpu->total_mhashes / total_secs,
  797. opt_log_interval, cgpu->rolling,
  798. cgpu->accepted, cgpu->rejected,
  799. cgpu->utility,
  800. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  801. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  802. strcat(io_buffer, buf);
  803. }
  804. }
  805. #endif
  806. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  807. {
  808. int devcount = 0;
  809. int numpga = 0;
  810. int i;
  811. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  812. numpga = numpgas();
  813. #endif
  814. if (nDevs == 0 && opt_n_threads == 0 && numpga == 0) {
  815. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  816. return;
  817. }
  818. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  819. if (isjson) {
  820. strcat(io_buffer, COMMA);
  821. strcat(io_buffer, JSON_DEVS);
  822. }
  823. for (i = 0; i < nDevs; i++) {
  824. if (isjson && devcount > 0)
  825. strcat(io_buffer, COMMA);
  826. gpustatus(i, isjson);
  827. devcount++;
  828. }
  829. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  830. if (numpga > 0)
  831. for (i = 0; i < numpga; i++) {
  832. if (isjson && devcount > 0)
  833. strcat(io_buffer, COMMA);
  834. pgastatus(i, isjson);
  835. devcount++;
  836. }
  837. #endif
  838. #ifdef WANT_CPUMINE
  839. if (opt_n_threads > 0)
  840. for (i = 0; i < num_processors; i++) {
  841. if (isjson && devcount > 0)
  842. strcat(io_buffer, COMMA);
  843. cpustatus(i, isjson);
  844. devcount++;
  845. }
  846. #endif
  847. if (isjson)
  848. strcat(io_buffer, JSON_CLOSE);
  849. }
  850. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  851. {
  852. int id;
  853. if (nDevs == 0) {
  854. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  855. return;
  856. }
  857. if (param == NULL || *param == '\0') {
  858. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  859. return;
  860. }
  861. id = atoi(param);
  862. if (id < 0 || id >= nDevs) {
  863. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  864. return;
  865. }
  866. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  867. if (isjson) {
  868. strcat(io_buffer, COMMA);
  869. strcat(io_buffer, JSON_GPU);
  870. }
  871. gpustatus(id, isjson);
  872. if (isjson)
  873. strcat(io_buffer, JSON_CLOSE);
  874. }
  875. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  876. static void pgadev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  877. {
  878. int numpga = numpgas();
  879. int id;
  880. if (numpga == 0) {
  881. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  882. return;
  883. }
  884. if (param == NULL || *param == '\0') {
  885. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  886. return;
  887. }
  888. id = atoi(param);
  889. if (id < 0 || id >= numpga) {
  890. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  891. return;
  892. }
  893. strcpy(io_buffer, message(MSG_PGADEV, id, NULL, isjson));
  894. if (isjson) {
  895. strcat(io_buffer, COMMA);
  896. strcat(io_buffer, JSON_PGA);
  897. }
  898. pgastatus(id, isjson);
  899. if (isjson)
  900. strcat(io_buffer, JSON_CLOSE);
  901. }
  902. static void pgaenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  903. {
  904. int numpga = numpgas();
  905. struct thr_info *thr;
  906. int pga;
  907. int id;
  908. int i;
  909. if (numpga == 0) {
  910. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  911. return;
  912. }
  913. if (param == NULL || *param == '\0') {
  914. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  915. return;
  916. }
  917. id = atoi(param);
  918. if (id < 0 || id >= numpga) {
  919. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  920. return;
  921. }
  922. int dev = pgadevice(id);
  923. if (dev < 0) { // Should never happen
  924. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  925. return;
  926. }
  927. struct cgpu_info *cgpu = devices[dev];
  928. if (cgpu->deven != DEV_DISABLED) {
  929. strcpy(io_buffer, message(MSG_PGALRENA, id, NULL, isjson));
  930. return;
  931. }
  932. if (cgpu->status != LIFE_WELL) {
  933. strcpy(io_buffer, message(MSG_PGAUNW, id, NULL, isjson));
  934. return;
  935. }
  936. for (i = 0; i < mining_threads; i++) {
  937. pga = thr_info[i].cgpu->device_id;
  938. if (pga == dev) {
  939. thr = &thr_info[i];
  940. cgpu->deven = DEV_ENABLED;
  941. tq_push(thr->q, &ping);
  942. }
  943. }
  944. strcpy(io_buffer, message(MSG_PGAENA, id, NULL, isjson));
  945. }
  946. static void pgadisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  947. {
  948. int numpga = numpgas();
  949. int id;
  950. if (numpga == 0) {
  951. strcpy(io_buffer, message(MSG_PGANON, 0, NULL, isjson));
  952. return;
  953. }
  954. if (param == NULL || *param == '\0') {
  955. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  956. return;
  957. }
  958. id = atoi(param);
  959. if (id < 0 || id >= numpga) {
  960. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  961. return;
  962. }
  963. int dev = pgadevice(id);
  964. if (dev < 0) { // Should never happen
  965. strcpy(io_buffer, message(MSG_INVPGA, id, NULL, isjson));
  966. return;
  967. }
  968. struct cgpu_info *cgpu = devices[dev];
  969. if (cgpu->deven == DEV_DISABLED) {
  970. strcpy(io_buffer, message(MSG_PGALRDIS, id, NULL, isjson));
  971. return;
  972. }
  973. cgpu->deven = DEV_DISABLED;
  974. strcpy(io_buffer, message(MSG_PGADIS, id, NULL, isjson));
  975. }
  976. #endif
  977. #ifdef WANT_CPUMINE
  978. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  979. {
  980. int id;
  981. if (opt_n_threads == 0) {
  982. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  983. return;
  984. }
  985. if (param == NULL || *param == '\0') {
  986. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  987. return;
  988. }
  989. id = atoi(param);
  990. if (id < 0 || id >= num_processors) {
  991. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  992. return;
  993. }
  994. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  995. if (isjson) {
  996. strcat(io_buffer, COMMA);
  997. strcat(io_buffer, JSON_CPU);
  998. }
  999. cpustatus(id, isjson);
  1000. if (isjson)
  1001. strcat(io_buffer, JSON_CLOSE);
  1002. }
  1003. #endif
  1004. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1005. {
  1006. char buf[BUFSIZ];
  1007. char *status, *lp;
  1008. char *rpc_url;
  1009. char *rpc_user;
  1010. int i;
  1011. if (total_pools == 0) {
  1012. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1013. return;
  1014. }
  1015. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  1016. if (isjson) {
  1017. strcat(io_buffer, COMMA);
  1018. strcat(io_buffer, JSON_POOLS);
  1019. }
  1020. for (i = 0; i < total_pools; i++) {
  1021. struct pool *pool = pools[i];
  1022. if (!pool->enabled)
  1023. status = (char *)DISABLED;
  1024. else {
  1025. if (pool->idle)
  1026. status = (char *)DEAD;
  1027. else
  1028. status = (char *)ALIVE;
  1029. }
  1030. if (pool->hdr_path)
  1031. lp = (char *)YES;
  1032. else
  1033. lp = (char *)NO;
  1034. rpc_url = escape_string(pool->rpc_url, isjson);
  1035. rpc_user = escape_string(pool->rpc_user, isjson);
  1036. sprintf(buf, isjson
  1037. ? "%s{\"POOL\":%d,\"URL\":\"%s\",\"Status\":\"%s\",\"Priority\":%d,\"Long Poll\":\"%s\",\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Remote Failures\":%d,\"User\":\"%s\"}"
  1038. : "%sPOOL=%d,URL=%s,Status=%s,Priority=%d,Long Poll=%s,Getworks=%d,Accepted=%d,Rejected=%d,Discarded=%d,Stale=%d,Get Failures=%d,Remote Failures=%d,User=%s" SEPSTR,
  1039. (isjson && (i > 0)) ? COMMA : BLANK,
  1040. i, rpc_url, status, pool->prio, lp,
  1041. pool->getwork_requested,
  1042. pool->accepted, pool->rejected,
  1043. pool->discarded_work,
  1044. pool->stale_shares,
  1045. pool->getfail_occasions,
  1046. pool->remotefail_occasions,
  1047. rpc_user);
  1048. strcat(io_buffer, buf);
  1049. if (rpc_url != pool->rpc_url)
  1050. free(rpc_url);
  1051. rpc_url = NULL;
  1052. if (rpc_user != pool->rpc_user)
  1053. free(rpc_user);
  1054. rpc_user = NULL;
  1055. }
  1056. if (isjson)
  1057. strcat(io_buffer, JSON_CLOSE);
  1058. }
  1059. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1060. {
  1061. double utility, mhs;
  1062. #ifdef WANT_CPUMINE
  1063. char *algo = (char *)(algo_names[opt_algo]);
  1064. if (algo == NULL)
  1065. algo = "(null)";
  1066. #endif
  1067. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  1068. mhs = total_mhashes_done / total_secs;
  1069. #ifdef WANT_CPUMINE
  1070. sprintf(io_buffer, isjson
  1071. ? "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"Algorithm\":\"%s\",\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE
  1072. : "%s" _SUMMARY ",Elapsed=%.0f,Algorithm=%s,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f" SEPSTR,
  1073. message(MSG_SUMM, 0, NULL, isjson),
  1074. total_secs, algo, mhs, found_blocks,
  1075. total_getworks, total_accepted, total_rejected,
  1076. hw_errors, utility, total_discarded, total_stale,
  1077. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1078. #else
  1079. sprintf(io_buffer, isjson
  1080. ? "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE
  1081. : "%s" _SUMMARY ",Elapsed=%.0f,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f" SEPSTR,
  1082. message(MSG_SUMM, 0, NULL, isjson),
  1083. total_secs, mhs, found_blocks,
  1084. total_getworks, total_accepted, total_rejected,
  1085. hw_errors, utility, total_discarded, total_stale,
  1086. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  1087. #endif
  1088. }
  1089. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1090. {
  1091. struct thr_info *thr;
  1092. int gpu;
  1093. int id;
  1094. int i;
  1095. if (gpu_threads == 0) {
  1096. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1097. return;
  1098. }
  1099. if (param == NULL || *param == '\0') {
  1100. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1101. return;
  1102. }
  1103. id = atoi(param);
  1104. if (id < 0 || id >= nDevs) {
  1105. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1106. return;
  1107. }
  1108. if (gpus[id].deven != DEV_DISABLED) {
  1109. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  1110. return;
  1111. }
  1112. for (i = 0; i < gpu_threads; i++) {
  1113. gpu = thr_info[i].cgpu->device_id;
  1114. if (gpu == id) {
  1115. thr = &thr_info[i];
  1116. if (thr->cgpu->status != LIFE_WELL) {
  1117. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  1118. return;
  1119. }
  1120. gpus[id].deven = DEV_ENABLED;
  1121. tq_push(thr->q, &ping);
  1122. }
  1123. }
  1124. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  1125. }
  1126. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1127. {
  1128. int id;
  1129. if (nDevs == 0) {
  1130. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1131. return;
  1132. }
  1133. if (param == NULL || *param == '\0') {
  1134. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1135. return;
  1136. }
  1137. id = atoi(param);
  1138. if (id < 0 || id >= nDevs) {
  1139. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1140. return;
  1141. }
  1142. if (gpus[id].deven == DEV_DISABLED) {
  1143. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  1144. return;
  1145. }
  1146. gpus[id].deven = DEV_DISABLED;
  1147. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  1148. }
  1149. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1150. {
  1151. int id;
  1152. if (nDevs == 0) {
  1153. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1154. return;
  1155. }
  1156. if (param == NULL || *param == '\0') {
  1157. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1158. return;
  1159. }
  1160. id = atoi(param);
  1161. if (id < 0 || id >= nDevs) {
  1162. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1163. return;
  1164. }
  1165. reinit_device(&gpus[id]);
  1166. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  1167. }
  1168. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1169. {
  1170. char buf[BUFSIZ];
  1171. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  1172. sprintf(buf, isjson
  1173. ? "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE
  1174. : _GPUS ",Count=%d" SEPSTR,
  1175. nDevs);
  1176. strcat(io_buffer, buf);
  1177. }
  1178. static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1179. {
  1180. char buf[BUFSIZ];
  1181. int count = 0;
  1182. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  1183. count = numpgas();
  1184. #endif
  1185. strcpy(io_buffer, message(MSG_NUMPGA, 0, NULL, isjson));
  1186. sprintf(buf, isjson
  1187. ? "," JSON_PGAS "{\"Count\":%d}" JSON_CLOSE
  1188. : _PGAS ",Count=%d" SEPSTR,
  1189. count);
  1190. strcat(io_buffer, buf);
  1191. }
  1192. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1193. {
  1194. char buf[BUFSIZ];
  1195. int count = 0;
  1196. #ifdef WANT_CPUMINE
  1197. count = opt_n_threads > 0 ? num_processors : 0;
  1198. #endif
  1199. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  1200. sprintf(buf, isjson
  1201. ? "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE
  1202. : _CPUS ",Count=%d" SEPSTR,
  1203. count);
  1204. strcat(io_buffer, buf);
  1205. }
  1206. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1207. {
  1208. struct pool *pool;
  1209. int id;
  1210. if (total_pools == 0) {
  1211. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1212. return;
  1213. }
  1214. if (param == NULL || *param == '\0') {
  1215. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1216. return;
  1217. }
  1218. id = atoi(param);
  1219. if (id < 0 || id >= total_pools) {
  1220. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1221. return;
  1222. }
  1223. pool = pools[id];
  1224. pool->enabled = true;
  1225. switch_pools(pool);
  1226. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  1227. }
  1228. static void copyadvanceafter(char ch, char **param, char **buf)
  1229. {
  1230. #define src_p (*param)
  1231. #define dst_b (*buf)
  1232. while (*src_p && *src_p != ch) {
  1233. if (*src_p == '\\' && *(src_p+1) != '\0')
  1234. src_p++;
  1235. *(dst_b++) = *(src_p++);
  1236. }
  1237. if (*src_p)
  1238. src_p++;
  1239. *(dst_b++) = '\0';
  1240. }
  1241. static bool pooldetails(char *param, char **url, char **user, char **pass)
  1242. {
  1243. char *ptr, *buf;
  1244. ptr = buf = malloc(strlen(param)+1);
  1245. if (unlikely(!buf))
  1246. quit(1, "Failed to malloc pooldetails buf");
  1247. *url = buf;
  1248. // copy url
  1249. copyadvanceafter(',', &param, &buf);
  1250. if (!(*param)) // missing user
  1251. goto exitsama;
  1252. *user = buf;
  1253. // copy user
  1254. copyadvanceafter(',', &param, &buf);
  1255. if (!*param) // missing pass
  1256. goto exitsama;
  1257. *pass = buf;
  1258. // copy pass
  1259. copyadvanceafter(',', &param, &buf);
  1260. return true;
  1261. exitsama:
  1262. free(ptr);
  1263. return false;
  1264. }
  1265. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1266. {
  1267. char *url, *user, *pass;
  1268. char *ptr;
  1269. if (param == NULL || *param == '\0') {
  1270. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  1271. return;
  1272. }
  1273. if (!pooldetails(param, &url, &user, &pass)) {
  1274. ptr = escape_string(param, isjson);
  1275. strcpy(io_buffer, message(MSG_INVPDP, 0, ptr, isjson));
  1276. if (ptr != param)
  1277. free(ptr);
  1278. ptr = NULL;
  1279. return;
  1280. }
  1281. if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
  1282. strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
  1283. return;
  1284. }
  1285. ptr = escape_string(url, isjson);
  1286. strcpy(io_buffer, message(MSG_ADDPOOL, 0, ptr, isjson));
  1287. if (ptr != url)
  1288. free(ptr);
  1289. ptr = NULL;
  1290. }
  1291. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1292. {
  1293. struct pool *pool;
  1294. int id;
  1295. if (total_pools == 0) {
  1296. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1297. return;
  1298. }
  1299. if (param == NULL || *param == '\0') {
  1300. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1301. return;
  1302. }
  1303. id = atoi(param);
  1304. if (id < 0 || id >= total_pools) {
  1305. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1306. return;
  1307. }
  1308. pool = pools[id];
  1309. if (pool->enabled == true) {
  1310. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  1311. return;
  1312. }
  1313. pool->enabled = true;
  1314. if (pool->prio < current_pool()->prio)
  1315. switch_pools(pool);
  1316. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  1317. }
  1318. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1319. {
  1320. struct pool *pool;
  1321. int id;
  1322. if (total_pools == 0) {
  1323. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1324. return;
  1325. }
  1326. if (param == NULL || *param == '\0') {
  1327. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1328. return;
  1329. }
  1330. id = atoi(param);
  1331. if (id < 0 || id >= total_pools) {
  1332. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1333. return;
  1334. }
  1335. pool = pools[id];
  1336. if (pool->enabled == false) {
  1337. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  1338. return;
  1339. }
  1340. if (active_pools() <= 1) {
  1341. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  1342. return;
  1343. }
  1344. pool->enabled = false;
  1345. if (pool == current_pool())
  1346. switch_pools(NULL);
  1347. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  1348. }
  1349. static void removepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1350. {
  1351. struct pool *pool;
  1352. char *rpc_url;
  1353. bool dofree = false;
  1354. int id;
  1355. if (total_pools == 0) {
  1356. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  1357. return;
  1358. }
  1359. if (param == NULL || *param == '\0') {
  1360. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  1361. return;
  1362. }
  1363. id = atoi(param);
  1364. if (id < 0 || id >= total_pools) {
  1365. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  1366. return;
  1367. }
  1368. if (total_pools <= 1) {
  1369. strcpy(io_buffer, message(MSG_REMLASTP, id, NULL, isjson));
  1370. return;
  1371. }
  1372. pool = pools[id];
  1373. if (pool == current_pool())
  1374. switch_pools(NULL);
  1375. if (pool == current_pool()) {
  1376. strcpy(io_buffer, message(MSG_ACTPOOL, id, NULL, isjson));
  1377. return;
  1378. }
  1379. pool->enabled = false;
  1380. rpc_url = escape_string(pool->rpc_url, isjson);
  1381. if (rpc_url != pool->rpc_url)
  1382. dofree = true;
  1383. remove_pool(pool);
  1384. strcpy(io_buffer, message(MSG_REMPOOL, id, rpc_url, isjson));
  1385. if (dofree)
  1386. free(rpc_url);
  1387. rpc_url = NULL;
  1388. }
  1389. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  1390. {
  1391. int id;
  1392. char *gpusep;
  1393. if (nDevs == 0) {
  1394. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  1395. return false;
  1396. }
  1397. if (param == NULL || *param == '\0') {
  1398. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  1399. return false;
  1400. }
  1401. gpusep = strchr(param, GPUSEP);
  1402. if (gpusep == NULL) {
  1403. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  1404. return false;
  1405. }
  1406. *(gpusep++) = '\0';
  1407. id = atoi(param);
  1408. if (id < 0 || id >= nDevs) {
  1409. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  1410. return false;
  1411. }
  1412. *gpu = id;
  1413. *value = gpusep;
  1414. return true;
  1415. }
  1416. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1417. {
  1418. int id;
  1419. char *value;
  1420. int intensity;
  1421. char intensitystr[7];
  1422. if (!splitgpuvalue(param, &id, &value, isjson))
  1423. return;
  1424. if (!strncasecmp(value, DYNAMIC, 1)) {
  1425. gpus[id].dynamic = true;
  1426. strcpy(intensitystr, DYNAMIC);
  1427. }
  1428. else {
  1429. intensity = atoi(value);
  1430. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1431. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1432. return;
  1433. }
  1434. gpus[id].dynamic = false;
  1435. gpus[id].intensity = intensity;
  1436. sprintf(intensitystr, "%d", intensity);
  1437. }
  1438. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1439. }
  1440. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1441. {
  1442. #ifdef HAVE_ADL
  1443. int id;
  1444. char *value;
  1445. int clock;
  1446. if (!splitgpuvalue(param, &id, &value, isjson))
  1447. return;
  1448. clock = atoi(value);
  1449. if (set_memoryclock(id, clock))
  1450. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1451. else
  1452. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1453. #else
  1454. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1455. #endif
  1456. }
  1457. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1458. {
  1459. #ifdef HAVE_ADL
  1460. int id;
  1461. char *value;
  1462. int clock;
  1463. if (!splitgpuvalue(param, &id, &value, isjson))
  1464. return;
  1465. clock = atoi(value);
  1466. if (set_engineclock(id, clock))
  1467. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1468. else
  1469. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1470. #else
  1471. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1472. #endif
  1473. }
  1474. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1475. {
  1476. #ifdef HAVE_ADL
  1477. int id;
  1478. char *value;
  1479. int fan;
  1480. if (!splitgpuvalue(param, &id, &value, isjson))
  1481. return;
  1482. fan = atoi(value);
  1483. if (set_fanspeed(id, fan))
  1484. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1485. else
  1486. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1487. #else
  1488. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1489. #endif
  1490. }
  1491. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1492. {
  1493. #ifdef HAVE_ADL
  1494. int id;
  1495. char *value;
  1496. float vddc;
  1497. if (!splitgpuvalue(param, &id, &value, isjson))
  1498. return;
  1499. vddc = atof(value);
  1500. if (set_vddc(id, vddc))
  1501. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1502. else
  1503. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1504. #else
  1505. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1506. #endif
  1507. }
  1508. static void send_result(SOCKETTYPE c, bool isjson);
  1509. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1510. {
  1511. if (isjson)
  1512. strcpy(io_buffer, JSON_START JSON_BYE);
  1513. else
  1514. strcpy(io_buffer, _BYE);
  1515. send_result(c, isjson);
  1516. *io_buffer = '\0';
  1517. bye = 1;
  1518. PTH(&thr_info[my_thr_id]) = 0L;
  1519. kill_work();
  1520. }
  1521. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1522. {
  1523. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1524. }
  1525. void notifystatus(int device, struct cgpu_info *cgpu, bool isjson)
  1526. {
  1527. char buf[BUFSIZ];
  1528. char *reason;
  1529. if (cgpu->device_last_not_well == 0)
  1530. reason = REASON_NONE;
  1531. else
  1532. switch(cgpu->device_not_well_reason) {
  1533. case REASON_THREAD_FAIL_INIT:
  1534. reason = REASON_THREAD_FAIL_INIT_STR;
  1535. break;
  1536. case REASON_THREAD_ZERO_HASH:
  1537. reason = REASON_THREAD_ZERO_HASH_STR;
  1538. break;
  1539. case REASON_THREAD_FAIL_QUEUE:
  1540. reason = REASON_THREAD_FAIL_QUEUE_STR;
  1541. break;
  1542. case REASON_DEV_SICK_IDLE_60:
  1543. reason = REASON_DEV_SICK_IDLE_60_STR;
  1544. break;
  1545. case REASON_DEV_DEAD_IDLE_600:
  1546. reason = REASON_DEV_DEAD_IDLE_600_STR;
  1547. break;
  1548. case REASON_DEV_NOSTART:
  1549. reason = REASON_DEV_NOSTART_STR;
  1550. break;
  1551. case REASON_DEV_OVER_HEAT:
  1552. reason = REASON_DEV_OVER_HEAT_STR;
  1553. break;
  1554. case REASON_DEV_THERMAL_CUTOFF:
  1555. reason = REASON_DEV_THERMAL_CUTOFF_STR;
  1556. break;
  1557. default:
  1558. reason = REASON_UNKNOWN_STR;
  1559. break;
  1560. }
  1561. // ALL counters (and only counters) must start the name with a '*'
  1562. // Simplifies future external support for adding new counters
  1563. sprintf(buf, isjson
  1564. ? "%s{\"NOTIFY\":%d,\"Name\":\"%s\",\"ID\":%d,\"Last Well\":%lu,\"Last Not Well\":%lu,\"Reason Not Well\":\"%s\",\"*Thread Fail Init\":%d,\"*Thread Zero Hash\":%d,\"*Thread Fail Queue\":%d,\"*Dev Sick Idle 60s\":%d,\"*Dev Dead Idle 600s\":%d,\"*Dev Nostart\":%d,\"*Dev Over Heat\":%d,\"*Dev Thermal Cutoff\":%d}" JSON_CLOSE
  1565. : "%sNOTIFY=%d,Name=%s,ID=%d,Last Well=%lu,Last Not Well=%lu,Reason Not Well=%s,*Thread Fail Init=%d,*Thread Zero Hash=%d,*Thread Fail Queue=%d,*Dev Sick Idle 60s=%d,*Dev Dead Idle 600s=%d,*Dev Nostart=%d,*Dev Over Heat=%d,*Dev Thermal Cutoff=%d" SEPSTR,
  1566. (isjson && (device > 0)) ? COMMA : BLANK,
  1567. device, cgpu->api->name, cgpu->device_id,
  1568. cgpu->device_last_well, cgpu->device_last_not_well, reason,
  1569. cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
  1570. cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
  1571. cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
  1572. cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count);
  1573. strcat(io_buffer, buf);
  1574. }
  1575. static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1576. {
  1577. int i;
  1578. if (total_devices == 0) {
  1579. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1580. return;
  1581. }
  1582. strcpy(io_buffer, message(MSG_NOTIFY, 0, NULL, isjson));
  1583. if (isjson) {
  1584. strcat(io_buffer, COMMA);
  1585. strcat(io_buffer, JSON_NOTIFY);
  1586. }
  1587. for (i = 0; i < total_devices; i++)
  1588. notifystatus(i, devices[i], isjson);
  1589. if (isjson)
  1590. strcat(io_buffer, JSON_CLOSE);
  1591. }
  1592. static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1593. {
  1594. char buf[BUFSIZ];
  1595. struct cgpu_info *cgpu;
  1596. int i;
  1597. if (total_devices == 0) {
  1598. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  1599. return;
  1600. }
  1601. strcpy(io_buffer, message(MSG_DEVDETAILS, 0, NULL, isjson));
  1602. if (isjson) {
  1603. strcat(io_buffer, COMMA);
  1604. strcat(io_buffer, JSON_DEVDETAILS);
  1605. }
  1606. for (i = 0; i < total_devices; i++) {
  1607. cgpu = devices[i];
  1608. sprintf(buf, isjson
  1609. ? "%s{\"DEVDETAILS\":%d,\"Name\":\"%s\",\"ID\":%d,\"Driver\":\"%s\",\"Kernel\":\"%s\",\"Model\":\"%s\",\"Device Path\":\"%s\"}"
  1610. : "%sDEVDETAILS=%d,Name=%s,ID=%d,Driver=%s,Kernel=%s,Model=%s,Device Path=%s" SEPSTR,
  1611. (isjson && (i > 0)) ? COMMA : BLANK,
  1612. i, cgpu->api->name, cgpu->device_id,
  1613. cgpu->api->dname, cgpu->kname ? : BLANK,
  1614. cgpu->name ? : BLANK, cgpu->device_path ? : BLANK);
  1615. strcat(io_buffer, buf);
  1616. }
  1617. if (isjson)
  1618. strcat(io_buffer, JSON_CLOSE);
  1619. }
  1620. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1621. {
  1622. FILE *fcfg;
  1623. char *ptr;
  1624. if (param == NULL || *param == '\0') {
  1625. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  1626. return;
  1627. }
  1628. fcfg = fopen(param, "w");
  1629. if (!fcfg) {
  1630. ptr = escape_string(param, isjson);
  1631. strcpy(io_buffer, message(MSG_BADFN, 0, ptr, isjson));
  1632. if (ptr != param)
  1633. free(ptr);
  1634. ptr = NULL;
  1635. return;
  1636. }
  1637. write_config(fcfg);
  1638. fclose(fcfg);
  1639. ptr = escape_string(param, isjson);
  1640. strcpy(io_buffer, message(MSG_SAVED, 0, ptr, isjson));
  1641. if (ptr != param)
  1642. free(ptr);
  1643. ptr = NULL;
  1644. }
  1645. struct CMDS {
  1646. char *name;
  1647. void (*func)(SOCKETTYPE, char *, bool);
  1648. bool requires_writemode;
  1649. } cmds[] = {
  1650. { "version", apiversion, false },
  1651. { "config", minerconfig, false },
  1652. { "devs", devstatus, false },
  1653. { "pools", poolstatus, false },
  1654. { "summary", summary, false },
  1655. { "gpuenable", gpuenable, true },
  1656. { "gpudisable", gpudisable, true },
  1657. { "gpurestart", gpurestart, true },
  1658. { "gpu", gpudev, false },
  1659. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_ZTEX)
  1660. { "pga", pgadev, false },
  1661. { "pgaenable", pgaenable, true },
  1662. { "pgadisable", pgadisable, true },
  1663. #endif
  1664. #ifdef WANT_CPUMINE
  1665. { "cpu", cpudev, false },
  1666. #endif
  1667. { "gpucount", gpucount, false },
  1668. { "pgacount", pgacount, false },
  1669. { "cpucount", cpucount, false },
  1670. { "switchpool", switchpool, true },
  1671. { "addpool", addpool, true },
  1672. { "enablepool", enablepool, true },
  1673. { "disablepool", disablepool, true },
  1674. { "removepool", removepool, true },
  1675. { "gpuintensity", gpuintensity, true },
  1676. { "gpumem", gpumem, true },
  1677. { "gpuengine", gpuengine, true },
  1678. { "gpufan", gpufan, true },
  1679. { "gpuvddc", gpuvddc, true },
  1680. { "save", dosave, true },
  1681. { "quit", doquit, true },
  1682. { "privileged", privileged, true },
  1683. { "notify", notify, false },
  1684. { "devdetails", devdetails, false },
  1685. { NULL, NULL, false }
  1686. };
  1687. static void send_result(SOCKETTYPE c, bool isjson)
  1688. {
  1689. int n;
  1690. int len;
  1691. if (isjson)
  1692. strcat(io_buffer, JSON_END);
  1693. len = strlen(io_buffer);
  1694. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : BLANK);
  1695. // ignore failure - it's closed immediately anyway
  1696. n = send(c, io_buffer, len+1, 0);
  1697. if (opt_debug) {
  1698. if (SOCKETFAIL(n))
  1699. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1700. else
  1701. applog(LOG_DEBUG, "API: sent %d", n);
  1702. }
  1703. }
  1704. static void tidyup(__maybe_unused void *arg)
  1705. {
  1706. bye = 1;
  1707. if (sock != INVSOCK) {
  1708. shutdown(sock, SHUT_RDWR);
  1709. CLOSESOCKET(sock);
  1710. sock = INVSOCK;
  1711. }
  1712. if (ipaccess != NULL) {
  1713. free(ipaccess);
  1714. ipaccess = NULL;
  1715. }
  1716. if (msg_buffer != NULL) {
  1717. free(msg_buffer);
  1718. msg_buffer = NULL;
  1719. }
  1720. if (io_buffer != NULL) {
  1721. free(io_buffer);
  1722. io_buffer = NULL;
  1723. }
  1724. }
  1725. /*
  1726. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1727. * special case of 0/0 allows /0 (means all IP addresses)
  1728. */
  1729. #define ALLIP4 "0/0"
  1730. /*
  1731. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1732. */
  1733. static void setup_ipaccess()
  1734. {
  1735. char *buf, *ptr, *comma, *slash, *dot;
  1736. int ipcount, mask, octet, i;
  1737. bool writemode;
  1738. buf = malloc(strlen(opt_api_allow) + 1);
  1739. if (unlikely(!buf))
  1740. quit(1, "Failed to malloc ipaccess buf");
  1741. strcpy(buf, opt_api_allow);
  1742. ipcount = 1;
  1743. ptr = buf;
  1744. while (*ptr)
  1745. if (*(ptr++) == ',')
  1746. ipcount++;
  1747. // possibly more than needed, but never less
  1748. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1749. if (unlikely(!ipaccess))
  1750. quit(1, "Failed to calloc ipaccess");
  1751. ips = 0;
  1752. ptr = buf;
  1753. while (ptr && *ptr) {
  1754. while (*ptr == ' ' || *ptr == '\t')
  1755. ptr++;
  1756. if (*ptr == ',') {
  1757. ptr++;
  1758. continue;
  1759. }
  1760. comma = strchr(ptr, ',');
  1761. if (comma)
  1762. *(comma++) = '\0';
  1763. writemode = false;
  1764. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1765. if (tolower(*ptr) == 'w')
  1766. writemode = true;
  1767. ptr += 2;
  1768. }
  1769. ipaccess[ips].writemode = writemode;
  1770. if (strcmp(ptr, ALLIP4) == 0)
  1771. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1772. else {
  1773. slash = strchr(ptr, '/');
  1774. if (!slash)
  1775. ipaccess[ips].mask = 0xffffffff;
  1776. else {
  1777. *(slash++) = '\0';
  1778. mask = atoi(slash);
  1779. if (mask < 1 || mask > 32)
  1780. goto popipo; // skip invalid/zero
  1781. ipaccess[ips].mask = 0;
  1782. while (mask-- >= 0) {
  1783. octet = 1 << (mask % 8);
  1784. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1785. }
  1786. }
  1787. ipaccess[ips].ip = 0; // missing default to '.0'
  1788. for (i = 0; ptr && (i < 4); i++) {
  1789. dot = strchr(ptr, '.');
  1790. if (dot)
  1791. *(dot++) = '\0';
  1792. octet = atoi(ptr);
  1793. if (octet < 0 || octet > 0xff)
  1794. goto popipo; // skip invalid
  1795. ipaccess[ips].ip |= (octet << (i * 8));
  1796. ptr = dot;
  1797. }
  1798. ipaccess[ips].ip &= ipaccess[ips].mask;
  1799. }
  1800. ips++;
  1801. popipo:
  1802. ptr = comma;
  1803. }
  1804. free(buf);
  1805. }
  1806. void api(int api_thr_id)
  1807. {
  1808. char buf[BUFSIZ];
  1809. char param_buf[BUFSIZ];
  1810. const char *localaddr = "127.0.0.1";
  1811. SOCKETTYPE c;
  1812. int n, bound;
  1813. char *connectaddr;
  1814. char *binderror;
  1815. time_t bindstart;
  1816. short int port = opt_api_port;
  1817. struct sockaddr_in serv;
  1818. struct sockaddr_in cli;
  1819. socklen_t clisiz;
  1820. char *cmd;
  1821. char *param;
  1822. bool addrok;
  1823. bool writemode;
  1824. json_error_t json_err;
  1825. json_t *json_config;
  1826. json_t *json_val;
  1827. bool isjson;
  1828. bool did;
  1829. int i;
  1830. pthread_cleanup_push(tidyup, NULL);
  1831. my_thr_id = api_thr_id;
  1832. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1833. sleep(opt_log_interval);
  1834. if (!opt_api_listen) {
  1835. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1836. return;
  1837. }
  1838. if (opt_api_allow) {
  1839. setup_ipaccess();
  1840. if (ips == 0) {
  1841. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1842. return;
  1843. }
  1844. }
  1845. sock = socket(AF_INET, SOCK_STREAM, 0);
  1846. if (sock == INVSOCK) {
  1847. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1848. return;
  1849. }
  1850. memset(&serv, 0, sizeof(serv));
  1851. serv.sin_family = AF_INET;
  1852. if (!opt_api_allow && !opt_api_network) {
  1853. serv.sin_addr.s_addr = inet_addr(localaddr);
  1854. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1855. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1856. return;
  1857. }
  1858. }
  1859. serv.sin_port = htons(port);
  1860. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1861. bound = 0;
  1862. bindstart = time(NULL);
  1863. while (bound == 0) {
  1864. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1865. binderror = SOCKERRMSG;
  1866. if ((time(NULL) - bindstart) > 61)
  1867. break;
  1868. else {
  1869. applog(LOG_WARNING, "API bind to port %d failed - trying again in 30sec", port);
  1870. sleep(30);
  1871. }
  1872. } else
  1873. bound = 1;
  1874. }
  1875. if (bound == 0) {
  1876. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1877. return;
  1878. }
  1879. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1880. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1881. CLOSESOCKET(sock);
  1882. return;
  1883. }
  1884. if (opt_api_allow)
  1885. applog(LOG_WARNING, "API running in IP access mode");
  1886. else {
  1887. if (opt_api_network)
  1888. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1889. else
  1890. applog(LOG_WARNING, "API running in local access mode");
  1891. }
  1892. io_buffer = malloc(MYBUFSIZ+1);
  1893. msg_buffer = malloc(MYBUFSIZ+1);
  1894. while (bye == 0) {
  1895. clisiz = sizeof(cli);
  1896. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1897. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1898. goto die;
  1899. }
  1900. connectaddr = inet_ntoa(cli.sin_addr);
  1901. addrok = false;
  1902. writemode = false;
  1903. if (opt_api_allow) {
  1904. for (i = 0; i < ips; i++) {
  1905. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1906. addrok = true;
  1907. writemode = ipaccess[i].writemode;
  1908. break;
  1909. }
  1910. }
  1911. } else {
  1912. if (opt_api_network)
  1913. addrok = true;
  1914. else
  1915. addrok = (strcmp(connectaddr, localaddr) == 0);
  1916. }
  1917. if (opt_debug)
  1918. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1919. if (addrok) {
  1920. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1921. if (SOCKETFAIL(n))
  1922. buf[0] = '\0';
  1923. else
  1924. buf[n] = '\0';
  1925. if (opt_debug) {
  1926. if (SOCKETFAIL(n))
  1927. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1928. else
  1929. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1930. }
  1931. if (!SOCKETFAIL(n)) {
  1932. // the time of the request in now
  1933. when = time(NULL);
  1934. did = false;
  1935. if (*buf != ISJSON) {
  1936. isjson = false;
  1937. param = strchr(buf, SEPARATOR);
  1938. if (param != NULL)
  1939. *(param++) = '\0';
  1940. cmd = buf;
  1941. }
  1942. else {
  1943. isjson = true;
  1944. param = NULL;
  1945. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1946. json_config = json_loadb(buf, n, 0, &json_err);
  1947. #elif JANSSON_MAJOR_VERSION > 1
  1948. json_config = json_loads(buf, 0, &json_err);
  1949. #else
  1950. json_config = json_loads(buf, &json_err);
  1951. #endif
  1952. if (!json_is_object(json_config)) {
  1953. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1954. send_result(c, isjson);
  1955. did = true;
  1956. }
  1957. else {
  1958. json_val = json_object_get(json_config, JSON_COMMAND);
  1959. if (json_val == NULL) {
  1960. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1961. send_result(c, isjson);
  1962. did = true;
  1963. }
  1964. else {
  1965. if (!json_is_string(json_val)) {
  1966. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1967. send_result(c, isjson);
  1968. did = true;
  1969. }
  1970. else {
  1971. cmd = (char *)json_string_value(json_val);
  1972. json_val = json_object_get(json_config, JSON_PARAMETER);
  1973. if (json_is_string(json_val))
  1974. param = (char *)json_string_value(json_val);
  1975. else if (json_is_integer(json_val)) {
  1976. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1977. param = param_buf;
  1978. } else if (json_is_real(json_val)) {
  1979. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1980. param = param_buf;
  1981. }
  1982. }
  1983. }
  1984. }
  1985. }
  1986. if (!did)
  1987. for (i = 0; cmds[i].name != NULL; i++) {
  1988. if (strcmp(cmd, cmds[i].name) == 0) {
  1989. if (cmds[i].requires_writemode && !writemode) {
  1990. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1991. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1992. }
  1993. else
  1994. (cmds[i].func)(c, param, isjson);
  1995. send_result(c, isjson);
  1996. did = true;
  1997. break;
  1998. }
  1999. }
  2000. if (!did) {
  2001. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2002. send_result(c, isjson);
  2003. }
  2004. }
  2005. }
  2006. CLOSESOCKET(c);
  2007. }
  2008. die:
  2009. pthread_cleanup_pop(true);
  2010. }