api.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412
  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 15sec", port);
  1870. sleep(15);
  1871. }
  1872. }
  1873. else
  1874. bound = 1;
  1875. }
  1876. if (bound == 0) {
  1877. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1878. return;
  1879. }
  1880. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1881. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1882. CLOSESOCKET(sock);
  1883. return;
  1884. }
  1885. if (opt_api_allow)
  1886. applog(LOG_WARNING, "API running in IP access mode");
  1887. else {
  1888. if (opt_api_network)
  1889. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1890. else
  1891. applog(LOG_WARNING, "API running in local access mode");
  1892. }
  1893. io_buffer = malloc(MYBUFSIZ+1);
  1894. msg_buffer = malloc(MYBUFSIZ+1);
  1895. while (bye == 0) {
  1896. clisiz = sizeof(cli);
  1897. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1898. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1899. goto die;
  1900. }
  1901. connectaddr = inet_ntoa(cli.sin_addr);
  1902. addrok = false;
  1903. writemode = false;
  1904. if (opt_api_allow) {
  1905. for (i = 0; i < ips; i++) {
  1906. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1907. addrok = true;
  1908. writemode = ipaccess[i].writemode;
  1909. break;
  1910. }
  1911. }
  1912. } else {
  1913. if (opt_api_network)
  1914. addrok = true;
  1915. else
  1916. addrok = (strcmp(connectaddr, localaddr) == 0);
  1917. }
  1918. if (opt_debug)
  1919. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1920. if (addrok) {
  1921. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1922. if (SOCKETFAIL(n))
  1923. buf[0] = '\0';
  1924. else
  1925. buf[n] = '\0';
  1926. if (opt_debug) {
  1927. if (SOCKETFAIL(n))
  1928. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1929. else
  1930. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1931. }
  1932. if (!SOCKETFAIL(n)) {
  1933. // the time of the request in now
  1934. when = time(NULL);
  1935. did = false;
  1936. if (*buf != ISJSON) {
  1937. isjson = false;
  1938. param = strchr(buf, SEPARATOR);
  1939. if (param != NULL)
  1940. *(param++) = '\0';
  1941. cmd = buf;
  1942. }
  1943. else {
  1944. isjson = true;
  1945. param = NULL;
  1946. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1947. json_config = json_loadb(buf, n, 0, &json_err);
  1948. #elif JANSSON_MAJOR_VERSION > 1
  1949. json_config = json_loads(buf, 0, &json_err);
  1950. #else
  1951. json_config = json_loads(buf, &json_err);
  1952. #endif
  1953. if (!json_is_object(json_config)) {
  1954. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1955. send_result(c, isjson);
  1956. did = true;
  1957. }
  1958. else {
  1959. json_val = json_object_get(json_config, JSON_COMMAND);
  1960. if (json_val == NULL) {
  1961. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1962. send_result(c, isjson);
  1963. did = true;
  1964. }
  1965. else {
  1966. if (!json_is_string(json_val)) {
  1967. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1968. send_result(c, isjson);
  1969. did = true;
  1970. }
  1971. else {
  1972. cmd = (char *)json_string_value(json_val);
  1973. json_val = json_object_get(json_config, JSON_PARAMETER);
  1974. if (json_is_string(json_val))
  1975. param = (char *)json_string_value(json_val);
  1976. else if (json_is_integer(json_val)) {
  1977. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1978. param = param_buf;
  1979. } else if (json_is_real(json_val)) {
  1980. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1981. param = param_buf;
  1982. }
  1983. }
  1984. }
  1985. }
  1986. }
  1987. if (!did)
  1988. for (i = 0; cmds[i].name != NULL; i++) {
  1989. if (strcmp(cmd, cmds[i].name) == 0) {
  1990. if (cmds[i].requires_writemode && !writemode) {
  1991. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1992. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1993. }
  1994. else
  1995. (cmds[i].func)(c, param, isjson);
  1996. send_result(c, isjson);
  1997. did = true;
  1998. break;
  1999. }
  2000. }
  2001. if (!did) {
  2002. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  2003. send_result(c, isjson);
  2004. }
  2005. }
  2006. }
  2007. CLOSESOCKET(c);
  2008. }
  2009. die:
  2010. pthread_cleanup_pop(true);
  2011. }