api.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  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. #include "config.h"
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include "compat.h"
  20. #include "miner.h"
  21. #include "device-cpu.h" /* for algo_names[], TODO: re-factor dependency */
  22. #if defined(unix) || defined(__APPLE__)
  23. #include <errno.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #define SOCKETTYPE int
  28. #define SOCKETFAIL(a) ((a) < 0)
  29. #define INVSOCK -1
  30. #define INVINETADDR -1
  31. #define CLOSESOCKET close
  32. #define SOCKERRMSG strerror(errno)
  33. #endif
  34. #ifdef WIN32
  35. #include <ws2tcpip.h>
  36. #include <winsock2.h>
  37. #define SOCKETTYPE SOCKET
  38. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  39. #define INVSOCK INVALID_SOCKET
  40. #define INVINETADDR INADDR_NONE
  41. #define CLOSESOCKET closesocket
  42. static char WSAbuf[1024];
  43. struct WSAERRORS {
  44. int id;
  45. char *code;
  46. } WSAErrors[] = {
  47. { 0, "No error" },
  48. { WSAEINTR, "Interrupted system call" },
  49. { WSAEBADF, "Bad file number" },
  50. { WSAEACCES, "Permission denied" },
  51. { WSAEFAULT, "Bad address" },
  52. { WSAEINVAL, "Invalid argument" },
  53. { WSAEMFILE, "Too many open sockets" },
  54. { WSAEWOULDBLOCK, "Operation would block" },
  55. { WSAEINPROGRESS, "Operation now in progress" },
  56. { WSAEALREADY, "Operation already in progress" },
  57. { WSAENOTSOCK, "Socket operation on non-socket" },
  58. { WSAEDESTADDRREQ, "Destination address required" },
  59. { WSAEMSGSIZE, "Message too long" },
  60. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  61. { WSAENOPROTOOPT, "Bad protocol option" },
  62. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  63. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  64. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  65. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  66. { WSAEAFNOSUPPORT, "Address family not supported" },
  67. { WSAEADDRINUSE, "Address already in use" },
  68. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  69. { WSAENETDOWN, "Network is down" },
  70. { WSAENETUNREACH, "Network is unreachable" },
  71. { WSAENETRESET, "Net connection reset" },
  72. { WSAECONNABORTED, "Software caused connection abort" },
  73. { WSAECONNRESET, "Connection reset by peer" },
  74. { WSAENOBUFS, "No buffer space available" },
  75. { WSAEISCONN, "Socket is already connected" },
  76. { WSAENOTCONN, "Socket is not connected" },
  77. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  78. { WSAETOOMANYREFS, "Too many references, can't splice" },
  79. { WSAETIMEDOUT, "Connection timed out" },
  80. { WSAECONNREFUSED, "Connection refused" },
  81. { WSAELOOP, "Too many levels of symbolic links" },
  82. { WSAENAMETOOLONG, "File name too long" },
  83. { WSAEHOSTDOWN, "Host is down" },
  84. { WSAEHOSTUNREACH, "No route to host" },
  85. { WSAENOTEMPTY, "Directory not empty" },
  86. { WSAEPROCLIM, "Too many processes" },
  87. { WSAEUSERS, "Too many users" },
  88. { WSAEDQUOT, "Disc quota exceeded" },
  89. { WSAESTALE, "Stale NFS file handle" },
  90. { WSAEREMOTE, "Too many levels of remote in path" },
  91. { WSASYSNOTREADY, "Network system is unavailable" },
  92. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  93. { WSANOTINITIALISED, "WSAStartup not yet called" },
  94. { WSAEDISCON, "Graceful shutdown in progress" },
  95. { WSAHOST_NOT_FOUND, "Host not found" },
  96. { WSANO_DATA, "No host data of that type was found" },
  97. { -1, "Unknown error code" }
  98. };
  99. static char *WSAErrorMsg()
  100. {
  101. int i;
  102. int id = WSAGetLastError();
  103. /* Assume none of them are actually -1 */
  104. for (i = 0; WSAErrors[i].id != -1; i++)
  105. if (WSAErrors[i].id == id)
  106. break;
  107. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  108. return &(WSAbuf[0]);
  109. }
  110. #define SOCKERRMSG WSAErrorMsg()
  111. #ifndef SHUT_RDWR
  112. #define SHUT_RDWR SD_BOTH
  113. #endif
  114. #ifndef in_addr_t
  115. #define in_addr_t uint32_t
  116. #endif
  117. #endif
  118. // Big enough for largest API request
  119. // though a PC with 100s of CPUs may exceed the size ...
  120. // Current code assumes it can socket send this size also
  121. #define MYBUFSIZ 32768
  122. // Number of requests to queue - normally would be small
  123. #define QUEUE 10
  124. static char *io_buffer = NULL;
  125. static char *msg_buffer = NULL;
  126. static SOCKETTYPE sock = INVSOCK;
  127. static const char *UNAVAILABLE = " - API will not be available";
  128. //static const char *BLANK = "";
  129. static const char *COMMA = ",";
  130. static const char SEPARATOR = '|';
  131. static const char GPUSEP = ',';
  132. static const char *APIVERSION = "1.4";
  133. static const char *DEAD = "Dead";
  134. static const char *SICK = "Sick";
  135. static const char *NOSTART = "NoStart";
  136. static const char *DISABLED = "Disabled";
  137. static const char *ALIVE = "Alive";
  138. #define _DYNAMIC "D"
  139. static const char *DYNAMIC = _DYNAMIC;
  140. static const char *YES = "Y";
  141. static const char *NO = "N";
  142. #define _DEVS "DEVS"
  143. #define _POOLS "POOLS"
  144. #define _SUMMARY "SUMMARY"
  145. #define _STATUS "STATUS"
  146. #define _VERSION "VERSION"
  147. #define _MINECON "CONFIG"
  148. #ifdef WANT_CPUMINE
  149. #define _CPU "CPU"
  150. #endif
  151. #define _GPU "GPU"
  152. #define _CPUS "CPUS"
  153. #define _GPUS "GPUS"
  154. #define _BYE "BYE"
  155. static const char ISJSON = '{';
  156. #define JSON0 "{"
  157. #define JSON1 "\""
  158. #define JSON2 "\":["
  159. #define JSON3 "]"
  160. #define JSON4 ",\"id\":1}"
  161. #define JSON_START JSON0
  162. #define JSON_DEVS JSON1 _DEVS JSON2
  163. #define JSON_POOLS JSON1 _POOLS JSON2
  164. #define JSON_SUMMARY JSON1 _SUMMARY JSON2
  165. #define JSON_STATUS JSON1 _STATUS JSON2
  166. #define JSON_VERSION JSON1 _VERSION JSON2
  167. #define JSON_MINECON JSON1 _MINECON JSON2
  168. #define JSON_GPU JSON1 _GPU JSON2
  169. #ifdef WANT_CPUMINE
  170. #define JSON_CPU JSON1 _CPU JSON2
  171. #endif
  172. #define JSON_GPUS JSON1 _GPUS JSON2
  173. #define JSON_CPUS JSON1 _CPUS JSON2
  174. #define JSON_BYE JSON1 _BYE JSON1
  175. #define JSON_CLOSE JSON3
  176. #define JSON_END JSON4
  177. static const char *JSON_COMMAND = "command";
  178. static const char *JSON_PARAMETER = "parameter";
  179. #define MSG_INVGPU 1
  180. #define MSG_ALRENA 2
  181. #define MSG_ALRDIS 3
  182. #define MSG_GPUMRE 4
  183. #define MSG_GPUREN 5
  184. #define MSG_GPUNON 6
  185. #define MSG_POOL 7
  186. #define MSG_NOPOOL 8
  187. #define MSG_DEVS 9
  188. #define MSG_NODEVS 10
  189. #define MSG_SUMM 11
  190. #define MSG_GPUDIS 12
  191. #define MSG_GPUREI 13
  192. #define MSG_INVCMD 14
  193. #define MSG_MISID 15
  194. #define MSG_GPUDEV 17
  195. #ifdef WANT_CPUMINE
  196. #define MSG_CPUNON 16
  197. #define MSG_CPUDEV 18
  198. #define MSG_INVCPU 19
  199. #endif
  200. #define MSG_NUMGPU 20
  201. #define MSG_NUMCPU 21
  202. #define MSG_VERSION 22
  203. #define MSG_INVJSON 23
  204. #define MSG_MISCMD 24
  205. #define MSG_MISPID 25
  206. #define MSG_INVPID 26
  207. #define MSG_SWITCHP 27
  208. #define MSG_MISVAL 28
  209. #define MSG_NOADL 29
  210. #define MSG_NOGPUADL 30
  211. #define MSG_INVINT 31
  212. #define MSG_GPUINT 32
  213. #define MSG_MINECON 33
  214. #define MSG_GPUMERR 34
  215. #define MSG_GPUMEM 35
  216. #define MSG_GPUEERR 36
  217. #define MSG_GPUENG 37
  218. #define MSG_GPUVERR 38
  219. #define MSG_GPUVDDC 39
  220. #define MSG_GPUFERR 40
  221. #define MSG_GPUFAN 41
  222. #define MSG_MISFN 42
  223. #define MSG_BADFN 43
  224. #define MSG_SAVED 44
  225. #define MSG_ACCDENY 45
  226. #define MSG_ACCOK 46
  227. #define MSG_ENAPOOL 47
  228. #define MSG_DISPOOL 48
  229. #define MSG_ALRENAP 49
  230. #define MSG_ALRDISP 50
  231. #define MSG_DISLASTP 51
  232. #define MSG_MISPDP 52
  233. #define MSG_INVPDP 53
  234. #define MSG_TOOMANYP 54
  235. #define MSG_ADDPOOL 55
  236. enum code_severity {
  237. SEVERITY_ERR,
  238. SEVERITY_WARN,
  239. SEVERITY_INFO,
  240. SEVERITY_SUCC,
  241. SEVERITY_FAIL
  242. };
  243. enum code_parameters {
  244. PARAM_GPU,
  245. PARAM_CPU,
  246. PARAM_GPUMAX,
  247. PARAM_CPUMAX,
  248. PARAM_PMAX,
  249. PARAM_POOLMAX,
  250. #ifdef WANT_CPUMINE
  251. PARAM_GCMAX,
  252. #else
  253. PARAM_GMAX,
  254. #endif
  255. PARAM_CMD,
  256. PARAM_POOL,
  257. PARAM_STR,
  258. PARAM_BOTH,
  259. PARAM_NONE
  260. };
  261. struct CODES {
  262. const enum code_severity severity;
  263. const int code;
  264. const enum code_parameters params;
  265. const char *description;
  266. } codes[] = {
  267. { SEVERITY_ERR, MSG_INVGPU, PARAM_GPUMAX, "Invalid GPU id %d - range is 0 - %d" },
  268. { SEVERITY_INFO, MSG_ALRENA, PARAM_GPU, "GPU %d already enabled" },
  269. { SEVERITY_INFO, MSG_ALRDIS, PARAM_GPU, "GPU %d already disabled" },
  270. { SEVERITY_WARN, MSG_GPUMRE, PARAM_GPU, "GPU %d must be restarted first" },
  271. { SEVERITY_INFO, MSG_GPUREN, PARAM_GPU, "GPU %d sent enable message" },
  272. { SEVERITY_ERR, MSG_GPUNON, PARAM_NONE, "No GPUs" },
  273. { SEVERITY_SUCC, MSG_POOL, PARAM_PMAX, "%d Pool(s)" },
  274. { SEVERITY_ERR, MSG_NOPOOL, PARAM_NONE, "No pools" },
  275. #ifdef WANT_CPUMINE
  276. { SEVERITY_SUCC, MSG_DEVS, PARAM_GCMAX, "%d GPU(s) - %d CPU(s)" },
  277. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs/CPUs" },
  278. #else
  279. { SEVERITY_SUCC, MSG_DEVS, PARAM_GMAX, "%d GPU(s)" },
  280. { SEVERITY_ERR, MSG_NODEVS, PARAM_NONE, "No GPUs" },
  281. #endif
  282. { SEVERITY_SUCC, MSG_SUMM, PARAM_NONE, "Summary" },
  283. { SEVERITY_INFO, MSG_GPUDIS, PARAM_GPU, "GPU %d set disable flag" },
  284. { SEVERITY_INFO, MSG_GPUREI, PARAM_GPU, "GPU %d restart attempted" },
  285. { SEVERITY_ERR, MSG_INVCMD, PARAM_NONE, "Invalid command" },
  286. { SEVERITY_ERR, MSG_MISID, PARAM_NONE, "Missing device id parameter" },
  287. { SEVERITY_SUCC, MSG_GPUDEV, PARAM_GPU, "GPU%d" },
  288. #ifdef WANT_CPUMINE
  289. { SEVERITY_ERR, MSG_CPUNON, PARAM_NONE, "No CPUs" },
  290. { SEVERITY_SUCC, MSG_CPUDEV, PARAM_CPU, "CPU%d" },
  291. { SEVERITY_ERR, MSG_INVCPU, PARAM_CPUMAX, "Invalid CPU id %d - range is 0 - %d" },
  292. #endif
  293. { SEVERITY_SUCC, MSG_NUMGPU, PARAM_NONE, "GPU count" },
  294. { SEVERITY_SUCC, MSG_NUMCPU, PARAM_NONE, "CPU count" },
  295. { SEVERITY_SUCC, MSG_VERSION, PARAM_NONE, "CGMiner versions" },
  296. { SEVERITY_ERR, MSG_INVJSON, PARAM_NONE, "Invalid JSON" },
  297. { SEVERITY_ERR, MSG_MISCMD, PARAM_CMD, "Missing JSON '%s'" },
  298. { SEVERITY_ERR, MSG_MISPID, PARAM_NONE, "Missing pool id parameter" },
  299. { SEVERITY_ERR, MSG_INVPID, PARAM_POOLMAX, "Invalid pool id %d - range is 0 - %d" },
  300. { SEVERITY_SUCC, MSG_SWITCHP, PARAM_POOL, "Switching to pool %d:'%s'" },
  301. { SEVERITY_ERR, MSG_MISVAL, PARAM_NONE, "Missing comma after GPU number" },
  302. { SEVERITY_ERR, MSG_NOADL, PARAM_NONE, "ADL is not available" },
  303. { SEVERITY_ERR, MSG_NOGPUADL,PARAM_GPU, "GPU %d does not have ADL" },
  304. { SEVERITY_ERR, MSG_INVINT, PARAM_STR, "Invalid intensity (%s) - must be '" _DYNAMIC "' or range " _MIN_INTENSITY_STR " - " _MAX_INTENSITY_STR },
  305. { SEVERITY_INFO, MSG_GPUINT, PARAM_BOTH, "GPU %d set new intensity to %s" },
  306. { SEVERITY_SUCC, MSG_MINECON, PARAM_NONE, "CGMiner config" },
  307. { SEVERITY_ERR, MSG_GPUMERR, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported failure" },
  308. { SEVERITY_SUCC, MSG_GPUMEM, PARAM_BOTH, "Setting GPU %d memoryclock to (%s) reported succeess" },
  309. { SEVERITY_ERR, MSG_GPUEERR, PARAM_BOTH, "Setting GPU %d clock to (%s) reported failure" },
  310. { SEVERITY_SUCC, MSG_GPUENG, PARAM_BOTH, "Setting GPU %d clock to (%s) reported succeess" },
  311. { SEVERITY_ERR, MSG_GPUVERR, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported failure" },
  312. { SEVERITY_SUCC, MSG_GPUVDDC, PARAM_BOTH, "Setting GPU %d vddc to (%s) reported succeess" },
  313. { SEVERITY_ERR, MSG_GPUFERR, PARAM_BOTH, "Setting GPU %d fan to (%s) reported failure" },
  314. { SEVERITY_SUCC, MSG_GPUFAN, PARAM_BOTH, "Setting GPU %d fan to (%s) reported succeess" },
  315. { SEVERITY_ERR, MSG_MISFN, PARAM_NONE, "Missing save filename parameter" },
  316. { SEVERITY_ERR, MSG_BADFN, PARAM_STR, "Can't open or create save file '%s'" },
  317. { SEVERITY_ERR, MSG_SAVED, PARAM_STR, "Configuration saved to file '%s'" },
  318. { SEVERITY_ERR, MSG_ACCDENY, PARAM_STR, "Access denied to '%s' command" },
  319. { SEVERITY_SUCC, MSG_ACCOK, PARAM_NONE, "Privileged access OK" },
  320. { SEVERITY_SUCC, MSG_ENAPOOL, PARAM_POOL, "Enabling pool %d:'%s'" },
  321. { SEVERITY_SUCC, MSG_DISPOOL, PARAM_POOL, "Disabling pool %d:'%s'" },
  322. { SEVERITY_INFO, MSG_ALRENAP, PARAM_POOL, "Pool %d:'%s' already enabled" },
  323. { SEVERITY_INFO, MSG_ALRDISP, PARAM_POOL, "Pool %d:'%s' already disabled" },
  324. { SEVERITY_ERR, MSG_DISLASTP,PARAM_POOL, "Cannot disable last active pool %d:'%s'" },
  325. { SEVERITY_ERR, MSG_MISPDP, PARAM_NONE, "Missing addpool details" },
  326. { SEVERITY_ERR, MSG_INVPDP, PARAM_STR, "Invalid addpool details '%s'" },
  327. { SEVERITY_ERR, MSG_TOOMANYP,PARAM_NONE, "Reached maximum number of pools (%d)" },
  328. { SEVERITY_SUCC, MSG_ADDPOOL, PARAM_STR, "Added pool '%s'" },
  329. { SEVERITY_FAIL, 0, 0, NULL }
  330. };
  331. static int my_thr_id = 0;
  332. static int bye = 0;
  333. static bool ping = true;
  334. static time_t when = 0; // when the request occurred
  335. struct IP4ACCESS {
  336. in_addr_t ip;
  337. in_addr_t mask;
  338. bool writemode;
  339. };
  340. static struct IP4ACCESS *ipaccess = NULL;
  341. static int ips = 0;
  342. // All replies (except BYE) start with a message
  343. // thus for JSON, message() inserts JSON_START at the front
  344. // and send_result() adds JSON_END at the end
  345. static char *message(int messageid, int paramid, char *param2, bool isjson)
  346. {
  347. char severity;
  348. char *ptr;
  349. #ifdef WANT_CPUMINE
  350. int cpu;
  351. #endif
  352. int i;
  353. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  354. if (codes[i].code == messageid) {
  355. switch (codes[i].severity) {
  356. case SEVERITY_WARN:
  357. severity = 'W';
  358. break;
  359. case SEVERITY_INFO:
  360. severity = 'I';
  361. break;
  362. case SEVERITY_SUCC:
  363. severity = 'S';
  364. break;
  365. case SEVERITY_ERR:
  366. default:
  367. severity = 'E';
  368. break;
  369. }
  370. if (isjson)
  371. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"When\":%lu,\"Code\":%d,\"Msg\":\"", severity, (unsigned long)when, messageid);
  372. else
  373. sprintf(msg_buffer, _STATUS "=%c,When=%lu,Code=%d,Msg=", severity, (unsigned long)when, messageid);
  374. ptr = msg_buffer + strlen(msg_buffer);
  375. switch(codes[i].params) {
  376. case PARAM_GPU:
  377. case PARAM_CPU:
  378. sprintf(ptr, codes[i].description, paramid);
  379. break;
  380. case PARAM_POOL:
  381. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  382. break;
  383. case PARAM_GPUMAX:
  384. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  385. break;
  386. case PARAM_PMAX:
  387. sprintf(ptr, codes[i].description, total_pools);
  388. break;
  389. case PARAM_POOLMAX:
  390. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  391. break;
  392. #ifdef WANT_CPUMINE
  393. case PARAM_GCMAX:
  394. if (opt_n_threads > 0)
  395. cpu = num_processors;
  396. else
  397. cpu = 0;
  398. sprintf(ptr, codes[i].description, nDevs, cpu);
  399. break;
  400. #else
  401. case PARAM_GMAX:
  402. sprintf(ptr, codes[i].description, nDevs);
  403. break;
  404. #endif
  405. case PARAM_CMD:
  406. sprintf(ptr, codes[i].description, JSON_COMMAND);
  407. break;
  408. case PARAM_STR:
  409. sprintf(ptr, codes[i].description, param2);
  410. break;
  411. case PARAM_BOTH:
  412. sprintf(ptr, codes[i].description, paramid, param2);
  413. break;
  414. case PARAM_NONE:
  415. default:
  416. strcpy(ptr, codes[i].description);
  417. }
  418. ptr = msg_buffer + strlen(msg_buffer);
  419. if (isjson)
  420. sprintf(ptr, "\",\"Description\":\"%s\"}" JSON_CLOSE, opt_api_description);
  421. else
  422. sprintf(ptr, ",Description=%s%c", opt_api_description, SEPARATOR);
  423. return msg_buffer;
  424. }
  425. }
  426. if (isjson)
  427. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"When\":%lu,\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE,
  428. (unsigned long)when, messageid, opt_api_description);
  429. else
  430. sprintf(msg_buffer, _STATUS "=F,When=%lu,Code=-1,Msg=%d,Description=%s%c",
  431. (unsigned long)when, messageid, opt_api_description, SEPARATOR);
  432. return msg_buffer;
  433. }
  434. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  435. {
  436. if (isjson)
  437. sprintf(io_buffer, "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE,
  438. message(MSG_VERSION, 0, NULL, isjson),
  439. VERSION, APIVERSION);
  440. else
  441. sprintf(io_buffer, "%s" _VERSION ",CGMiner=%s,API=%s%c",
  442. message(MSG_VERSION, 0, NULL, isjson),
  443. VERSION, APIVERSION, SEPARATOR);
  444. }
  445. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  446. {
  447. char buf[BUFSIZ];
  448. int cpucount = 0;
  449. char *adlinuse = (char *)NO;
  450. #ifdef HAVE_ADL
  451. const char *adl = YES;
  452. int i;
  453. for (i = 0; i < nDevs; i++) {
  454. if (gpus[i].has_adl) {
  455. adlinuse = (char *)YES;
  456. break;
  457. }
  458. }
  459. #else
  460. const char *adl = NO;
  461. #endif
  462. #ifdef WANT_CPUMINE
  463. cpucount = opt_n_threads > 0 ? num_processors : 0;
  464. #endif
  465. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  466. if (isjson)
  467. sprintf(buf, "," JSON_MINECON "{\"GPU Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\",\"Log Interval\":\"%d\"}" JSON_CLOSE, nDevs, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval);
  468. else
  469. sprintf(buf, _MINECON ",GPU Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s,Log Interval=%d%c", nDevs, cpucount, total_pools, adl, adlinuse, strategies[pool_strategy].s, opt_log_interval, SEPARATOR);
  470. strcat(io_buffer, buf);
  471. }
  472. static void gpustatus(int gpu, bool isjson)
  473. {
  474. char intensity[20];
  475. char buf[BUFSIZ];
  476. char *enabled;
  477. char *status;
  478. float gt, gv;
  479. int ga, gf, gp, gc, gm, pt;
  480. if (gpu >= 0 && gpu < nDevs) {
  481. struct cgpu_info *cgpu = &gpus[gpu];
  482. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  483. #ifdef HAVE_ADL
  484. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  485. #endif
  486. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  487. if (cgpu->deven != DEV_DISABLED)
  488. enabled = (char *)YES;
  489. else
  490. enabled = (char *)NO;
  491. if (cgpu->status == LIFE_DEAD)
  492. status = (char *)DEAD;
  493. else if (cgpu->status == LIFE_SICK)
  494. status = (char *)SICK;
  495. else if (cgpu->status == LIFE_NOSTART)
  496. status = (char *)NOSTART;
  497. else
  498. status = (char *)ALIVE;
  499. if (cgpu->dynamic)
  500. strcpy(intensity, DYNAMIC);
  501. else
  502. sprintf(intensity, "%d", cgpu->intensity);
  503. if (isjson)
  504. sprintf(buf, "{\"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}",
  505. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  506. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  507. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  508. cgpu->utility, intensity,
  509. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  510. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  511. else
  512. sprintf(buf, "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%c",
  513. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  514. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  515. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  516. cgpu->utility, intensity,
  517. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  518. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  519. strcat(io_buffer, buf);
  520. }
  521. }
  522. #ifdef WANT_CPUMINE
  523. static void cpustatus(int cpu, bool isjson)
  524. {
  525. char buf[BUFSIZ];
  526. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  527. struct cgpu_info *cgpu = &cpus[cpu];
  528. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  529. if (isjson)
  530. sprintf(buf, "{\"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}",
  531. cpu, cgpu->total_mhashes / total_secs,
  532. opt_log_interval, cgpu->rolling,
  533. cgpu->accepted, cgpu->rejected,
  534. cgpu->utility,
  535. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  536. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  537. else
  538. sprintf(buf, "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%c",
  539. cpu, cgpu->total_mhashes / total_secs,
  540. opt_log_interval, cgpu->rolling,
  541. cgpu->accepted, cgpu->rejected,
  542. cgpu->utility,
  543. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  544. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  545. strcat(io_buffer, buf);
  546. }
  547. }
  548. #endif
  549. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  550. {
  551. int i;
  552. if (nDevs == 0 && opt_n_threads == 0) {
  553. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  554. return;
  555. }
  556. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  557. if (isjson) {
  558. strcat(io_buffer, COMMA);
  559. strcat(io_buffer, JSON_DEVS);
  560. }
  561. for (i = 0; i < nDevs; i++) {
  562. if (isjson && i > 0)
  563. strcat(io_buffer, COMMA);
  564. gpustatus(i, isjson);
  565. }
  566. #ifdef WANT_CPUMINE
  567. if (opt_n_threads > 0)
  568. for (i = 0; i < num_processors; i++) {
  569. if (isjson && (i > 0 || nDevs > 0))
  570. strcat(io_buffer, COMMA);
  571. cpustatus(i, isjson);
  572. }
  573. #endif
  574. if (isjson)
  575. strcat(io_buffer, JSON_CLOSE);
  576. }
  577. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  578. {
  579. int id;
  580. if (nDevs == 0) {
  581. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  582. return;
  583. }
  584. if (param == NULL || *param == '\0') {
  585. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  586. return;
  587. }
  588. id = atoi(param);
  589. if (id < 0 || id >= nDevs) {
  590. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  591. return;
  592. }
  593. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  594. if (isjson) {
  595. strcat(io_buffer, COMMA);
  596. strcat(io_buffer, JSON_GPU);
  597. }
  598. gpustatus(id, isjson);
  599. if (isjson)
  600. strcat(io_buffer, JSON_CLOSE);
  601. }
  602. #ifdef WANT_CPUMINE
  603. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  604. {
  605. int id;
  606. if (opt_n_threads == 0) {
  607. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  608. return;
  609. }
  610. if (param == NULL || *param == '\0') {
  611. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  612. return;
  613. }
  614. id = atoi(param);
  615. if (id < 0 || id >= num_processors) {
  616. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  617. return;
  618. }
  619. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  620. if (isjson) {
  621. strcat(io_buffer, COMMA);
  622. strcat(io_buffer, JSON_CPU);
  623. }
  624. cpustatus(id, isjson);
  625. if (isjson)
  626. strcat(io_buffer, JSON_CLOSE);
  627. }
  628. #endif
  629. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  630. {
  631. char buf[BUFSIZ];
  632. char *status, *lp;
  633. int i;
  634. if (total_pools == 0) {
  635. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  636. return;
  637. }
  638. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  639. if (isjson) {
  640. strcat(io_buffer, COMMA);
  641. strcat(io_buffer, JSON_POOLS);
  642. }
  643. for (i = 0; i < total_pools; i++) {
  644. struct pool *pool = pools[i];
  645. if (!pool->enabled)
  646. status = (char *)DISABLED;
  647. else {
  648. if (pool->idle)
  649. status = (char *)DEAD;
  650. else
  651. status = (char *)ALIVE;
  652. }
  653. if (pool->hdr_path)
  654. lp = (char *)YES;
  655. else
  656. lp = (char *)NO;
  657. if (isjson)
  658. sprintf(buf, "%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}",
  659. (i > 0) ? COMMA : "",
  660. i, pool->rpc_url, status, pool->prio, lp,
  661. pool->getwork_requested,
  662. pool->accepted, pool->rejected,
  663. pool->discarded_work,
  664. pool->stale_shares,
  665. pool->getfail_occasions,
  666. pool->remotefail_occasions);
  667. else
  668. sprintf(buf, "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%c",
  669. i, pool->rpc_url, status, pool->prio, lp,
  670. pool->getwork_requested,
  671. pool->accepted, pool->rejected,
  672. pool->discarded_work,
  673. pool->stale_shares,
  674. pool->getfail_occasions,
  675. pool->remotefail_occasions, SEPARATOR);
  676. strcat(io_buffer, buf);
  677. }
  678. if (isjson)
  679. strcat(io_buffer, JSON_CLOSE);
  680. }
  681. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  682. {
  683. double utility, mhs;
  684. #ifdef WANT_CPUMINE
  685. char *algo = (char *)(algo_names[opt_algo]);
  686. if (algo == NULL)
  687. algo = "(null)";
  688. #endif
  689. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  690. mhs = total_mhashes_done / total_secs;
  691. #ifdef WANT_CPUMINE
  692. if (isjson)
  693. sprintf(io_buffer, "%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,
  694. message(MSG_SUMM, 0, NULL, isjson),
  695. total_secs, algo, mhs, found_blocks,
  696. total_getworks, total_accepted, total_rejected,
  697. hw_errors, utility, total_discarded, total_stale,
  698. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  699. else
  700. sprintf(io_buffer, "%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%c",
  701. message(MSG_SUMM, 0, NULL, isjson),
  702. total_secs, algo, mhs, found_blocks,
  703. total_getworks, total_accepted, total_rejected,
  704. hw_errors, utility, total_discarded, total_stale,
  705. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  706. #else
  707. if (isjson)
  708. sprintf(io_buffer, "%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,
  709. message(MSG_SUMM, 0, NULL, isjson),
  710. total_secs, mhs, found_blocks,
  711. total_getworks, total_accepted, total_rejected,
  712. hw_errors, utility, total_discarded, total_stale,
  713. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  714. else
  715. sprintf(io_buffer, "%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%c",
  716. message(MSG_SUMM, 0, NULL, isjson),
  717. total_secs, mhs, found_blocks,
  718. total_getworks, total_accepted, total_rejected,
  719. hw_errors, utility, total_discarded, total_stale,
  720. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  721. #endif
  722. }
  723. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  724. {
  725. struct thr_info *thr;
  726. int gpu;
  727. int id;
  728. int i;
  729. if (gpu_threads == 0) {
  730. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  731. return;
  732. }
  733. if (param == NULL || *param == '\0') {
  734. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  735. return;
  736. }
  737. id = atoi(param);
  738. if (id < 0 || id >= nDevs) {
  739. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  740. return;
  741. }
  742. if (gpus[id].deven != DEV_DISABLED) {
  743. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  744. return;
  745. }
  746. for (i = 0; i < gpu_threads; i++) {
  747. gpu = thr_info[i].cgpu->device_id;
  748. if (gpu == id) {
  749. thr = &thr_info[i];
  750. if (thr->cgpu->status != LIFE_WELL) {
  751. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  752. return;
  753. }
  754. gpus[id].deven = DEV_ENABLED;
  755. tq_push(thr->q, &ping);
  756. }
  757. }
  758. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  759. }
  760. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  761. {
  762. int id;
  763. if (nDevs == 0) {
  764. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  765. return;
  766. }
  767. if (param == NULL || *param == '\0') {
  768. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  769. return;
  770. }
  771. id = atoi(param);
  772. if (id < 0 || id >= nDevs) {
  773. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  774. return;
  775. }
  776. if (gpus[id].deven == DEV_DISABLED) {
  777. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  778. return;
  779. }
  780. gpus[id].deven = DEV_DISABLED;
  781. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  782. }
  783. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  784. {
  785. int id;
  786. if (nDevs == 0) {
  787. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  788. return;
  789. }
  790. if (param == NULL || *param == '\0') {
  791. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  792. return;
  793. }
  794. id = atoi(param);
  795. if (id < 0 || id >= nDevs) {
  796. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  797. return;
  798. }
  799. reinit_device(&gpus[id]);
  800. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  801. }
  802. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  803. {
  804. char buf[BUFSIZ];
  805. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  806. if (isjson)
  807. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  808. else
  809. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  810. strcat(io_buffer, buf);
  811. }
  812. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  813. {
  814. char buf[BUFSIZ];
  815. int count = 0;
  816. #ifdef WANT_CPUMINE
  817. count = opt_n_threads > 0 ? num_processors : 0;
  818. #endif
  819. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  820. if (isjson)
  821. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  822. else
  823. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  824. strcat(io_buffer, buf);
  825. }
  826. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  827. {
  828. struct pool *pool;
  829. int id;
  830. if (total_pools == 0) {
  831. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  832. return;
  833. }
  834. if (param == NULL || *param == '\0') {
  835. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  836. return;
  837. }
  838. id = atoi(param);
  839. if (id < 0 || id >= total_pools) {
  840. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  841. return;
  842. }
  843. pool = pools[id];
  844. pool->enabled = true;
  845. switch_pools(pool);
  846. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  847. }
  848. static void copyadvanceafter(char ch, char **param, char **buf)
  849. {
  850. #define src_p (*param)
  851. #define dst_b (*buf)
  852. while (*src_p && *src_p != ch) {
  853. if (*src_p == '\\' && *(src_p+1) != '\0')
  854. src_p++;
  855. *(dst_b++) = *(src_p++);
  856. }
  857. if (*src_p)
  858. src_p++;
  859. *(dst_b++) = '\0';
  860. }
  861. static bool pooldetails(char *param, char **url, char **user, char **pass)
  862. {
  863. char *ptr, *buf;
  864. ptr = buf = malloc(strlen(param)+1);
  865. if (unlikely(!buf))
  866. quit(1, "Failed to malloc pooldetails buf");
  867. *url = buf;
  868. // copy url
  869. copyadvanceafter(',', &param, &buf);
  870. if (!(*param)) // missing user
  871. goto exitsama;
  872. *user = buf;
  873. // copy user
  874. copyadvanceafter(',', &param, &buf);
  875. if (!*param) // missing pass
  876. goto exitsama;
  877. *pass = buf;
  878. // copy pass
  879. copyadvanceafter(',', &param, &buf);
  880. return true;
  881. exitsama:
  882. free(ptr);
  883. return false;
  884. }
  885. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  886. {
  887. char *url, *user, *pass;
  888. if (param == NULL || *param == '\0') {
  889. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  890. return;
  891. }
  892. if (!pooldetails(param, &url, &user, &pass)) {
  893. strcpy(io_buffer, message(MSG_INVPDP, 0, param, isjson));
  894. return;
  895. }
  896. if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
  897. strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
  898. return;
  899. }
  900. strcpy(io_buffer, message(MSG_ADDPOOL, 0, url, isjson));
  901. }
  902. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  903. {
  904. struct pool *pool;
  905. int id;
  906. if (total_pools == 0) {
  907. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  908. return;
  909. }
  910. if (param == NULL || *param == '\0') {
  911. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  912. return;
  913. }
  914. id = atoi(param);
  915. if (id < 0 || id >= total_pools) {
  916. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  917. return;
  918. }
  919. pool = pools[id];
  920. if (pool->enabled == true) {
  921. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  922. return;
  923. }
  924. pool->enabled = true;
  925. if (pool->prio < current_pool()->prio)
  926. switch_pools(pool);
  927. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  928. }
  929. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  930. {
  931. struct pool *pool;
  932. int id;
  933. if (total_pools == 0) {
  934. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  935. return;
  936. }
  937. if (param == NULL || *param == '\0') {
  938. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  939. return;
  940. }
  941. id = atoi(param);
  942. if (id < 0 || id >= total_pools) {
  943. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  944. return;
  945. }
  946. pool = pools[id];
  947. if (pool->enabled == false) {
  948. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  949. return;
  950. }
  951. if (active_pools() <= 1) {
  952. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  953. return;
  954. }
  955. pool->enabled = false;
  956. if (pool == current_pool())
  957. switch_pools(NULL);
  958. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  959. }
  960. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  961. {
  962. int id;
  963. char *gpusep;
  964. if (nDevs == 0) {
  965. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  966. return false;
  967. }
  968. if (param == NULL || *param == '\0') {
  969. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  970. return false;
  971. }
  972. gpusep = strchr(param, GPUSEP);
  973. if (gpusep == NULL) {
  974. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  975. return false;
  976. }
  977. *(gpusep++) = '\0';
  978. id = atoi(param);
  979. if (id < 0 || id >= nDevs) {
  980. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  981. return false;
  982. }
  983. *gpu = id;
  984. *value = gpusep;
  985. return true;
  986. }
  987. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  988. {
  989. int id;
  990. char *value;
  991. int intensity;
  992. char intensitystr[7];
  993. if (!splitgpuvalue(param, &id, &value, isjson))
  994. return;
  995. if (!strncasecmp(value, DYNAMIC, 1)) {
  996. gpus[id].dynamic = true;
  997. strcpy(intensitystr, DYNAMIC);
  998. }
  999. else {
  1000. intensity = atoi(value);
  1001. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1002. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1003. return;
  1004. }
  1005. gpus[id].dynamic = false;
  1006. gpus[id].intensity = intensity;
  1007. sprintf(intensitystr, "%d", intensity);
  1008. }
  1009. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1010. }
  1011. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1012. {
  1013. #ifdef HAVE_ADL
  1014. int id;
  1015. char *value;
  1016. int clock;
  1017. if (!splitgpuvalue(param, &id, &value, isjson))
  1018. return;
  1019. clock = atoi(value);
  1020. if (set_memoryclock(id, clock))
  1021. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1022. else
  1023. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1024. #else
  1025. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1026. #endif
  1027. }
  1028. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1029. {
  1030. #ifdef HAVE_ADL
  1031. int id;
  1032. char *value;
  1033. int clock;
  1034. if (!splitgpuvalue(param, &id, &value, isjson))
  1035. return;
  1036. clock = atoi(value);
  1037. if (set_engineclock(id, clock))
  1038. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1039. else
  1040. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1041. #else
  1042. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1043. #endif
  1044. }
  1045. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1046. {
  1047. #ifdef HAVE_ADL
  1048. int id;
  1049. char *value;
  1050. int fan;
  1051. if (!splitgpuvalue(param, &id, &value, isjson))
  1052. return;
  1053. fan = atoi(value);
  1054. if (set_fanspeed(id, fan))
  1055. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1056. else
  1057. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1058. #else
  1059. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1060. #endif
  1061. }
  1062. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1063. {
  1064. #ifdef HAVE_ADL
  1065. int id;
  1066. char *value;
  1067. float vddc;
  1068. if (!splitgpuvalue(param, &id, &value, isjson))
  1069. return;
  1070. vddc = atof(value);
  1071. if (set_vddc(id, vddc))
  1072. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1073. else
  1074. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1075. #else
  1076. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1077. #endif
  1078. }
  1079. static void send_result(SOCKETTYPE c, bool isjson);
  1080. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1081. {
  1082. if (isjson)
  1083. strcpy(io_buffer, JSON_START JSON_BYE);
  1084. else
  1085. strcpy(io_buffer, _BYE);
  1086. send_result(c, isjson);
  1087. *io_buffer = '\0';
  1088. bye = 1;
  1089. PTH(&thr_info[my_thr_id]) = 0L;
  1090. kill_work();
  1091. }
  1092. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1093. {
  1094. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1095. }
  1096. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1097. {
  1098. FILE *fcfg;
  1099. if (param == NULL || *param == '\0') {
  1100. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  1101. return;
  1102. }
  1103. fcfg = fopen(param, "w");
  1104. if (!fcfg) {
  1105. strcpy(io_buffer, message(MSG_BADFN, 0, param, isjson));
  1106. return;
  1107. }
  1108. write_config(fcfg);
  1109. fclose(fcfg);
  1110. strcpy(io_buffer, message(MSG_SAVED, 0, param, isjson));
  1111. }
  1112. struct CMDS {
  1113. char *name;
  1114. void (*func)(SOCKETTYPE, char *, bool);
  1115. bool requires_writemode;
  1116. } cmds[] = {
  1117. { "version", apiversion, false },
  1118. { "config", minerconfig, false },
  1119. { "devs", devstatus, false },
  1120. { "pools", poolstatus, false },
  1121. { "summary", summary, false },
  1122. { "gpuenable", gpuenable, true },
  1123. { "gpudisable", gpudisable, true },
  1124. { "gpurestart", gpurestart, true },
  1125. { "gpu", gpudev, false },
  1126. #ifdef WANT_CPUMINE
  1127. { "cpu", cpudev, false },
  1128. #endif
  1129. { "gpucount", gpucount, false },
  1130. { "cpucount", cpucount, false },
  1131. { "switchpool", switchpool, true },
  1132. { "addpool", addpool, true },
  1133. { "enablepool", enablepool, true },
  1134. { "disablepool", disablepool, true },
  1135. { "gpuintensity", gpuintensity, true },
  1136. { "gpumem", gpumem, true },
  1137. { "gpuengine", gpuengine, true },
  1138. { "gpufan", gpufan, true },
  1139. { "gpuvddc", gpuvddc, true },
  1140. { "save", dosave, true },
  1141. { "quit", doquit, true },
  1142. { "privileged", privileged, true },
  1143. { NULL, NULL, false }
  1144. };
  1145. static void send_result(SOCKETTYPE c, bool isjson)
  1146. {
  1147. int n;
  1148. int len;
  1149. if (isjson)
  1150. strcat(io_buffer, JSON_END);
  1151. len = strlen(io_buffer);
  1152. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  1153. // ignore failure - it's closed immediately anyway
  1154. n = send(c, io_buffer, len+1, 0);
  1155. if (opt_debug) {
  1156. if (SOCKETFAIL(n))
  1157. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1158. else
  1159. applog(LOG_DEBUG, "API: sent %d", n);
  1160. }
  1161. }
  1162. static void tidyup()
  1163. {
  1164. bye = 1;
  1165. if (sock != INVSOCK) {
  1166. shutdown(sock, SHUT_RDWR);
  1167. CLOSESOCKET(sock);
  1168. sock = INVSOCK;
  1169. }
  1170. if (ipaccess != NULL) {
  1171. free(ipaccess);
  1172. ipaccess = NULL;
  1173. }
  1174. if (msg_buffer != NULL) {
  1175. free(msg_buffer);
  1176. msg_buffer = NULL;
  1177. }
  1178. if (io_buffer != NULL) {
  1179. free(io_buffer);
  1180. io_buffer = NULL;
  1181. }
  1182. }
  1183. /*
  1184. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1185. * special case of 0/0 allows /0 (means all IP addresses)
  1186. */
  1187. #define ALLIP4 "0/0"
  1188. /*
  1189. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1190. */
  1191. static void setup_ipaccess()
  1192. {
  1193. char *buf, *ptr, *comma, *slash, *dot;
  1194. int ipcount, mask, octet, i;
  1195. bool writemode;
  1196. buf = malloc(strlen(opt_api_allow) + 1);
  1197. if (unlikely(!buf))
  1198. quit(1, "Failed to malloc ipaccess buf");
  1199. strcpy(buf, opt_api_allow);
  1200. ipcount = 1;
  1201. ptr = buf;
  1202. while (*ptr)
  1203. if (*(ptr++) == ',')
  1204. ipcount++;
  1205. // possibly more than needed, but never less
  1206. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1207. if (unlikely(!ipaccess))
  1208. quit(1, "Failed to calloc ipaccess");
  1209. ips = 0;
  1210. ptr = buf;
  1211. while (ptr && *ptr) {
  1212. while (*ptr == ' ' || *ptr == '\t')
  1213. ptr++;
  1214. if (*ptr == ',') {
  1215. ptr++;
  1216. continue;
  1217. }
  1218. comma = strchr(ptr, ',');
  1219. if (comma)
  1220. *(comma++) = '\0';
  1221. writemode = false;
  1222. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1223. if (tolower(*ptr) == 'w')
  1224. writemode = true;
  1225. ptr += 2;
  1226. }
  1227. ipaccess[ips].writemode = writemode;
  1228. if (strcmp(ptr, ALLIP4) == 0)
  1229. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1230. else {
  1231. slash = strchr(ptr, '/');
  1232. if (!slash)
  1233. ipaccess[ips].mask = 0xffffffff;
  1234. else {
  1235. *(slash++) = '\0';
  1236. mask = atoi(slash);
  1237. if (mask < 1 || mask > 32)
  1238. goto popipo; // skip invalid/zero
  1239. ipaccess[ips].mask = 0;
  1240. while (mask-- >= 0) {
  1241. octet = 1 << (mask % 8);
  1242. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1243. }
  1244. }
  1245. ipaccess[ips].ip = 0; // missing default to '.0'
  1246. for (i = 0; ptr && (i < 4); i++) {
  1247. dot = strchr(ptr, '.');
  1248. if (dot)
  1249. *(dot++) = '\0';
  1250. octet = atoi(ptr);
  1251. if (octet < 0 || octet > 0xff)
  1252. goto popipo; // skip invalid
  1253. ipaccess[ips].ip |= (octet << (i * 8));
  1254. ptr = dot;
  1255. }
  1256. ipaccess[ips].ip &= ipaccess[ips].mask;
  1257. }
  1258. ips++;
  1259. popipo:
  1260. ptr = comma;
  1261. }
  1262. free(buf);
  1263. }
  1264. void api(int api_thr_id)
  1265. {
  1266. char buf[BUFSIZ];
  1267. char param_buf[BUFSIZ];
  1268. const char *localaddr = "127.0.0.1";
  1269. SOCKETTYPE c;
  1270. int n, bound;
  1271. char *connectaddr;
  1272. char *binderror;
  1273. time_t bindstart;
  1274. short int port = opt_api_port;
  1275. struct sockaddr_in serv;
  1276. struct sockaddr_in cli;
  1277. socklen_t clisiz;
  1278. char *cmd;
  1279. char *param;
  1280. bool addrok;
  1281. bool writemode;
  1282. json_error_t json_err;
  1283. json_t *json_config;
  1284. json_t *json_val;
  1285. bool isjson;
  1286. bool did;
  1287. int i;
  1288. my_thr_id = api_thr_id;
  1289. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1290. sleep(opt_log_interval);
  1291. if (!opt_api_listen) {
  1292. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1293. return;
  1294. }
  1295. if (opt_api_allow) {
  1296. setup_ipaccess();
  1297. if (ips == 0) {
  1298. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1299. return;
  1300. }
  1301. }
  1302. sock = socket(AF_INET, SOCK_STREAM, 0);
  1303. if (sock == INVSOCK) {
  1304. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1305. return;
  1306. }
  1307. memset(&serv, 0, sizeof(serv));
  1308. serv.sin_family = AF_INET;
  1309. if (!opt_api_allow && !opt_api_network) {
  1310. serv.sin_addr.s_addr = inet_addr(localaddr);
  1311. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1312. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1313. return;
  1314. }
  1315. }
  1316. serv.sin_port = htons(port);
  1317. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1318. bound = 0;
  1319. bindstart = time(NULL);
  1320. while (bound == 0) {
  1321. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1322. binderror = SOCKERRMSG;
  1323. if ((time(NULL) - bindstart) > 61)
  1324. break;
  1325. else {
  1326. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1327. sleep(15);
  1328. }
  1329. }
  1330. else
  1331. bound = 1;
  1332. }
  1333. if (bound == 0) {
  1334. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1335. return;
  1336. }
  1337. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1338. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1339. CLOSESOCKET(sock);
  1340. return;
  1341. }
  1342. if (opt_api_allow)
  1343. applog(LOG_WARNING, "API running in IP access mode");
  1344. else {
  1345. if (opt_api_network)
  1346. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1347. else
  1348. applog(LOG_WARNING, "API running in local access mode");
  1349. }
  1350. io_buffer = malloc(MYBUFSIZ+1);
  1351. msg_buffer = malloc(MYBUFSIZ+1);
  1352. while (bye == 0) {
  1353. clisiz = sizeof(cli);
  1354. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1355. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1356. goto die;
  1357. }
  1358. connectaddr = inet_ntoa(cli.sin_addr);
  1359. addrok = false;
  1360. writemode = false;
  1361. if (opt_api_allow) {
  1362. for (i = 0; i < ips; i++) {
  1363. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1364. addrok = true;
  1365. writemode = ipaccess[i].writemode;
  1366. break;
  1367. }
  1368. }
  1369. } else {
  1370. if (opt_api_network)
  1371. addrok = true;
  1372. else
  1373. addrok = (strcmp(connectaddr, localaddr) == 0);
  1374. }
  1375. if (opt_debug)
  1376. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1377. if (addrok) {
  1378. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1379. if (SOCKETFAIL(n))
  1380. buf[0] = '\0';
  1381. else
  1382. buf[n] = '\0';
  1383. if (opt_debug) {
  1384. if (SOCKETFAIL(n))
  1385. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1386. else
  1387. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1388. }
  1389. if (!SOCKETFAIL(n)) {
  1390. // the time of the request in now
  1391. when = time(NULL);
  1392. did = false;
  1393. if (*buf != ISJSON) {
  1394. isjson = false;
  1395. param = strchr(buf, SEPARATOR);
  1396. if (param != NULL)
  1397. *(param++) = '\0';
  1398. cmd = buf;
  1399. }
  1400. else {
  1401. isjson = true;
  1402. param = NULL;
  1403. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1404. json_config = json_loadb(buf, n, 0, &json_err);
  1405. #elif JANSSON_MAJOR_VERSION > 1
  1406. json_config = json_loads(buf, 0, &json_err);
  1407. #else
  1408. json_config = json_loads(buf, &json_err);
  1409. #endif
  1410. if (!json_is_object(json_config)) {
  1411. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1412. send_result(c, isjson);
  1413. did = true;
  1414. }
  1415. else {
  1416. json_val = json_object_get(json_config, JSON_COMMAND);
  1417. if (json_val == NULL) {
  1418. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1419. send_result(c, isjson);
  1420. did = true;
  1421. }
  1422. else {
  1423. if (!json_is_string(json_val)) {
  1424. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1425. send_result(c, isjson);
  1426. did = true;
  1427. }
  1428. else {
  1429. cmd = (char *)json_string_value(json_val);
  1430. json_val = json_object_get(json_config, JSON_PARAMETER);
  1431. if (json_is_string(json_val))
  1432. param = (char *)json_string_value(json_val);
  1433. else if (json_is_integer(json_val)) {
  1434. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1435. param = param_buf;
  1436. } else if (json_is_real(json_val)) {
  1437. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1438. param = param_buf;
  1439. }
  1440. }
  1441. }
  1442. }
  1443. }
  1444. if (!did)
  1445. for (i = 0; cmds[i].name != NULL; i++) {
  1446. if (strcmp(cmd, cmds[i].name) == 0) {
  1447. if (cmds[i].requires_writemode && !writemode) {
  1448. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1449. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1450. }
  1451. else
  1452. (cmds[i].func)(c, param, isjson);
  1453. send_result(c, isjson);
  1454. did = true;
  1455. break;
  1456. }
  1457. }
  1458. if (!did) {
  1459. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1460. send_result(c, isjson);
  1461. }
  1462. }
  1463. }
  1464. CLOSESOCKET(c);
  1465. }
  1466. die:
  1467. tidyup();
  1468. }