api.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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.3";
  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. struct IP4ACCESS {
  335. in_addr_t ip;
  336. in_addr_t mask;
  337. bool writemode;
  338. };
  339. static struct IP4ACCESS *ipaccess = NULL;
  340. static int ips = 0;
  341. // All replies (except BYE) start with a message
  342. // thus for JSON, message() inserts JSON_START at the front
  343. // and send_result() adds JSON_END at the end
  344. static char *message(int messageid, int paramid, char *param2, bool isjson)
  345. {
  346. char severity;
  347. char *ptr;
  348. #ifdef WANT_CPUMINE
  349. int cpu;
  350. #endif
  351. int i;
  352. for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
  353. if (codes[i].code == messageid) {
  354. switch (codes[i].severity) {
  355. case SEVERITY_WARN:
  356. severity = 'W';
  357. break;
  358. case SEVERITY_INFO:
  359. severity = 'I';
  360. break;
  361. case SEVERITY_SUCC:
  362. severity = 'S';
  363. break;
  364. case SEVERITY_ERR:
  365. default:
  366. severity = 'E';
  367. break;
  368. }
  369. if (isjson)
  370. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"Code\":%d,\"Msg\":\"", severity, messageid);
  371. else
  372. sprintf(msg_buffer, _STATUS "=%c,Code=%d,Msg=", severity, messageid);
  373. ptr = msg_buffer + strlen(msg_buffer);
  374. switch(codes[i].params) {
  375. case PARAM_GPU:
  376. case PARAM_CPU:
  377. sprintf(ptr, codes[i].description, paramid);
  378. break;
  379. case PARAM_POOL:
  380. sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
  381. break;
  382. case PARAM_GPUMAX:
  383. sprintf(ptr, codes[i].description, paramid, nDevs - 1);
  384. break;
  385. case PARAM_PMAX:
  386. sprintf(ptr, codes[i].description, total_pools);
  387. break;
  388. case PARAM_POOLMAX:
  389. sprintf(ptr, codes[i].description, paramid, total_pools - 1);
  390. break;
  391. #ifdef WANT_CPUMINE
  392. case PARAM_GCMAX:
  393. if (opt_n_threads > 0)
  394. cpu = num_processors;
  395. else
  396. cpu = 0;
  397. sprintf(ptr, codes[i].description, nDevs, cpu);
  398. break;
  399. #else
  400. case PARAM_GMAX:
  401. sprintf(ptr, codes[i].description, nDevs);
  402. break;
  403. #endif
  404. case PARAM_CMD:
  405. sprintf(ptr, codes[i].description, JSON_COMMAND);
  406. break;
  407. case PARAM_STR:
  408. sprintf(ptr, codes[i].description, param2);
  409. break;
  410. case PARAM_BOTH:
  411. sprintf(ptr, codes[i].description, paramid, param2);
  412. break;
  413. case PARAM_NONE:
  414. default:
  415. strcpy(ptr, codes[i].description);
  416. }
  417. ptr = msg_buffer + strlen(msg_buffer);
  418. if (isjson)
  419. sprintf(ptr, "\",\"Description\":\"%s\"}" JSON_CLOSE, opt_api_description);
  420. else
  421. sprintf(ptr, ",Description=%s%c", opt_api_description, SEPARATOR);
  422. return msg_buffer;
  423. }
  424. }
  425. if (isjson)
  426. sprintf(msg_buffer, JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE,
  427. messageid, opt_api_description);
  428. else
  429. sprintf(msg_buffer, _STATUS "=F,Code=-1,Msg=%d,Description=%s%c",
  430. messageid, opt_api_description, SEPARATOR);
  431. return msg_buffer;
  432. }
  433. static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  434. {
  435. if (isjson)
  436. sprintf(io_buffer, "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE,
  437. message(MSG_VERSION, 0, NULL, isjson),
  438. VERSION, APIVERSION);
  439. else
  440. sprintf(io_buffer, "%s" _VERSION ",CGMiner=%s,API=%s%c",
  441. message(MSG_VERSION, 0, NULL, isjson),
  442. VERSION, APIVERSION, SEPARATOR);
  443. }
  444. static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  445. {
  446. char buf[BUFSIZ];
  447. int cpucount = 0;
  448. char *adlinuse = (char *)NO;
  449. #ifdef HAVE_ADL
  450. const char *adl = YES;
  451. int i;
  452. for (i = 0; i < nDevs; i++) {
  453. if (gpus[i].has_adl) {
  454. adlinuse = (char *)YES;
  455. break;
  456. }
  457. }
  458. #else
  459. const char *adl = NO;
  460. #endif
  461. #ifdef WANT_CPUMINE
  462. cpucount = opt_n_threads > 0 ? num_processors : 0;
  463. #endif
  464. strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
  465. if (isjson)
  466. 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);
  467. else
  468. 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);
  469. strcat(io_buffer, buf);
  470. }
  471. static void gpustatus(int gpu, bool isjson)
  472. {
  473. char intensity[20];
  474. char buf[BUFSIZ];
  475. char *enabled;
  476. char *status;
  477. float gt, gv;
  478. int ga, gf, gp, gc, gm, pt;
  479. if (gpu >= 0 && gpu < nDevs) {
  480. struct cgpu_info *cgpu = &gpus[gpu];
  481. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  482. #ifdef HAVE_ADL
  483. if (!gpu_stats(gpu, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  484. #endif
  485. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  486. if (cgpu->deven != DEV_DISABLED)
  487. enabled = (char *)YES;
  488. else
  489. enabled = (char *)NO;
  490. if (cgpu->status == LIFE_DEAD)
  491. status = (char *)DEAD;
  492. else if (cgpu->status == LIFE_SICK)
  493. status = (char *)SICK;
  494. else if (cgpu->status == LIFE_NOSTART)
  495. status = (char *)NOSTART;
  496. else
  497. status = (char *)ALIVE;
  498. if (cgpu->dynamic)
  499. strcpy(intensity, DYNAMIC);
  500. else
  501. sprintf(intensity, "%d", cgpu->intensity);
  502. if (isjson)
  503. 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}",
  504. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  505. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  506. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  507. cgpu->utility, intensity,
  508. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  509. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  510. else
  511. 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",
  512. gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
  513. cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
  514. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  515. cgpu->utility, intensity,
  516. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  517. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  518. strcat(io_buffer, buf);
  519. }
  520. }
  521. #ifdef WANT_CPUMINE
  522. static void cpustatus(int cpu, bool isjson)
  523. {
  524. char buf[BUFSIZ];
  525. if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
  526. struct cgpu_info *cgpu = &cpus[cpu];
  527. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  528. if (isjson)
  529. 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}",
  530. cpu, cgpu->total_mhashes / total_secs,
  531. opt_log_interval, cgpu->rolling,
  532. cgpu->accepted, cgpu->rejected,
  533. cgpu->utility,
  534. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  535. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
  536. else
  537. 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",
  538. cpu, cgpu->total_mhashes / total_secs,
  539. opt_log_interval, cgpu->rolling,
  540. cgpu->accepted, cgpu->rejected,
  541. cgpu->utility,
  542. ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
  543. (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, SEPARATOR);
  544. strcat(io_buffer, buf);
  545. }
  546. }
  547. #endif
  548. static void devstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  549. {
  550. int i;
  551. if (nDevs == 0 && opt_n_threads == 0) {
  552. strcpy(io_buffer, message(MSG_NODEVS, 0, NULL, isjson));
  553. return;
  554. }
  555. strcpy(io_buffer, message(MSG_DEVS, 0, NULL, isjson));
  556. if (isjson) {
  557. strcat(io_buffer, COMMA);
  558. strcat(io_buffer, JSON_DEVS);
  559. }
  560. for (i = 0; i < nDevs; i++) {
  561. if (isjson && i > 0)
  562. strcat(io_buffer, COMMA);
  563. gpustatus(i, isjson);
  564. }
  565. #ifdef WANT_CPUMINE
  566. if (opt_n_threads > 0)
  567. for (i = 0; i < num_processors; i++) {
  568. if (isjson && (i > 0 || nDevs > 0))
  569. strcat(io_buffer, COMMA);
  570. cpustatus(i, isjson);
  571. }
  572. #endif
  573. if (isjson)
  574. strcat(io_buffer, JSON_CLOSE);
  575. }
  576. static void gpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  577. {
  578. int id;
  579. if (nDevs == 0) {
  580. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  581. return;
  582. }
  583. if (param == NULL || *param == '\0') {
  584. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  585. return;
  586. }
  587. id = atoi(param);
  588. if (id < 0 || id >= nDevs) {
  589. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  590. return;
  591. }
  592. strcpy(io_buffer, message(MSG_GPUDEV, id, NULL, isjson));
  593. if (isjson) {
  594. strcat(io_buffer, COMMA);
  595. strcat(io_buffer, JSON_GPU);
  596. }
  597. gpustatus(id, isjson);
  598. if (isjson)
  599. strcat(io_buffer, JSON_CLOSE);
  600. }
  601. #ifdef WANT_CPUMINE
  602. static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  603. {
  604. int id;
  605. if (opt_n_threads == 0) {
  606. strcpy(io_buffer, message(MSG_CPUNON, 0, NULL, isjson));
  607. return;
  608. }
  609. if (param == NULL || *param == '\0') {
  610. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  611. return;
  612. }
  613. id = atoi(param);
  614. if (id < 0 || id >= num_processors) {
  615. strcpy(io_buffer, message(MSG_INVCPU, id, NULL, isjson));
  616. return;
  617. }
  618. strcpy(io_buffer, message(MSG_CPUDEV, id, NULL, isjson));
  619. if (isjson) {
  620. strcat(io_buffer, COMMA);
  621. strcat(io_buffer, JSON_CPU);
  622. }
  623. cpustatus(id, isjson);
  624. if (isjson)
  625. strcat(io_buffer, JSON_CLOSE);
  626. }
  627. #endif
  628. static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  629. {
  630. char buf[BUFSIZ];
  631. char *status, *lp;
  632. int i;
  633. if (total_pools == 0) {
  634. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  635. return;
  636. }
  637. strcpy(io_buffer, message(MSG_POOL, 0, NULL, isjson));
  638. if (isjson) {
  639. strcat(io_buffer, COMMA);
  640. strcat(io_buffer, JSON_POOLS);
  641. }
  642. for (i = 0; i < total_pools; i++) {
  643. struct pool *pool = pools[i];
  644. if (!pool->enabled)
  645. status = (char *)DISABLED;
  646. else {
  647. if (pool->idle)
  648. status = (char *)DEAD;
  649. else
  650. status = (char *)ALIVE;
  651. }
  652. if (pool->hdr_path)
  653. lp = (char *)YES;
  654. else
  655. lp = (char *)NO;
  656. if (isjson)
  657. 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}",
  658. (i > 0) ? COMMA : "",
  659. i, pool->rpc_url, status, pool->prio, lp,
  660. pool->getwork_requested,
  661. pool->accepted, pool->rejected,
  662. pool->discarded_work,
  663. pool->stale_shares,
  664. pool->getfail_occasions,
  665. pool->remotefail_occasions);
  666. else
  667. 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",
  668. i, pool->rpc_url, status, pool->prio, lp,
  669. pool->getwork_requested,
  670. pool->accepted, pool->rejected,
  671. pool->discarded_work,
  672. pool->stale_shares,
  673. pool->getfail_occasions,
  674. pool->remotefail_occasions, SEPARATOR);
  675. strcat(io_buffer, buf);
  676. }
  677. if (isjson)
  678. strcat(io_buffer, JSON_CLOSE);
  679. }
  680. static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  681. {
  682. double utility, mhs;
  683. #ifdef WANT_CPUMINE
  684. char *algo = (char *)(algo_names[opt_algo]);
  685. if (algo == NULL)
  686. algo = "(null)";
  687. #endif
  688. utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
  689. mhs = total_mhashes_done / total_secs;
  690. #ifdef WANT_CPUMINE
  691. if (isjson)
  692. 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,
  693. message(MSG_SUMM, 0, NULL, isjson),
  694. total_secs, algo, mhs, found_blocks,
  695. total_getworks, total_accepted, total_rejected,
  696. hw_errors, utility, total_discarded, total_stale,
  697. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  698. else
  699. 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",
  700. message(MSG_SUMM, 0, NULL, isjson),
  701. total_secs, algo, mhs, found_blocks,
  702. total_getworks, total_accepted, total_rejected,
  703. hw_errors, utility, total_discarded, total_stale,
  704. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  705. #else
  706. if (isjson)
  707. 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,
  708. message(MSG_SUMM, 0, NULL, isjson),
  709. total_secs, mhs, found_blocks,
  710. total_getworks, total_accepted, total_rejected,
  711. hw_errors, utility, total_discarded, total_stale,
  712. total_go, local_work, total_ro, new_blocks, total_mhashes_done);
  713. else
  714. 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",
  715. message(MSG_SUMM, 0, NULL, isjson),
  716. total_secs, mhs, found_blocks,
  717. total_getworks, total_accepted, total_rejected,
  718. hw_errors, utility, total_discarded, total_stale,
  719. total_go, local_work, total_ro, new_blocks, total_mhashes_done, SEPARATOR);
  720. #endif
  721. }
  722. static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  723. {
  724. struct thr_info *thr;
  725. int gpu;
  726. int id;
  727. int i;
  728. if (gpu_threads == 0) {
  729. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  730. return;
  731. }
  732. if (param == NULL || *param == '\0') {
  733. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  734. return;
  735. }
  736. id = atoi(param);
  737. if (id < 0 || id >= nDevs) {
  738. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  739. return;
  740. }
  741. if (gpus[id].deven != DEV_DISABLED) {
  742. strcpy(io_buffer, message(MSG_ALRENA, id, NULL, isjson));
  743. return;
  744. }
  745. for (i = 0; i < gpu_threads; i++) {
  746. gpu = thr_info[i].cgpu->device_id;
  747. if (gpu == id) {
  748. thr = &thr_info[i];
  749. if (thr->cgpu->status != LIFE_WELL) {
  750. strcpy(io_buffer, message(MSG_GPUMRE, id, NULL, isjson));
  751. return;
  752. }
  753. gpus[id].deven = DEV_ENABLED;
  754. tq_push(thr->q, &ping);
  755. }
  756. }
  757. strcpy(io_buffer, message(MSG_GPUREN, id, NULL, isjson));
  758. }
  759. static void gpudisable(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  760. {
  761. int id;
  762. if (nDevs == 0) {
  763. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  764. return;
  765. }
  766. if (param == NULL || *param == '\0') {
  767. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  768. return;
  769. }
  770. id = atoi(param);
  771. if (id < 0 || id >= nDevs) {
  772. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  773. return;
  774. }
  775. if (gpus[id].deven == DEV_DISABLED) {
  776. strcpy(io_buffer, message(MSG_ALRDIS, id, NULL, isjson));
  777. return;
  778. }
  779. gpus[id].deven = DEV_DISABLED;
  780. strcpy(io_buffer, message(MSG_GPUDIS, id, NULL, isjson));
  781. }
  782. static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  783. {
  784. int id;
  785. if (nDevs == 0) {
  786. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  787. return;
  788. }
  789. if (param == NULL || *param == '\0') {
  790. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  791. return;
  792. }
  793. id = atoi(param);
  794. if (id < 0 || id >= nDevs) {
  795. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  796. return;
  797. }
  798. reinit_device(&gpus[id]);
  799. strcpy(io_buffer, message(MSG_GPUREI, id, NULL, isjson));
  800. }
  801. static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  802. {
  803. char buf[BUFSIZ];
  804. strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
  805. if (isjson)
  806. sprintf(buf, "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE, nDevs);
  807. else
  808. sprintf(buf, _GPUS ",Count=%d%c", nDevs, SEPARATOR);
  809. strcat(io_buffer, buf);
  810. }
  811. static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  812. {
  813. char buf[BUFSIZ];
  814. int count = 0;
  815. #ifdef WANT_CPUMINE
  816. count = opt_n_threads > 0 ? num_processors : 0;
  817. #endif
  818. strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
  819. if (isjson)
  820. sprintf(buf, "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE, count);
  821. else
  822. sprintf(buf, _CPUS ",Count=%d%c", count, SEPARATOR);
  823. strcat(io_buffer, buf);
  824. }
  825. static void switchpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  826. {
  827. struct pool *pool;
  828. int id;
  829. if (total_pools == 0) {
  830. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  831. return;
  832. }
  833. if (param == NULL || *param == '\0') {
  834. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  835. return;
  836. }
  837. id = atoi(param);
  838. if (id < 0 || id >= total_pools) {
  839. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  840. return;
  841. }
  842. pool = pools[id];
  843. pool->enabled = true;
  844. switch_pools(pool);
  845. strcpy(io_buffer, message(MSG_SWITCHP, id, NULL, isjson));
  846. }
  847. static void copyadvanceafter(char ch, char **param, char **buf)
  848. {
  849. #define src_p (*param)
  850. #define dst_b (*buf)
  851. while (*src_p && *src_p != ch) {
  852. if (*src_p == '\\' && *(src_p+1) != '\0')
  853. src_p++;
  854. *(dst_b++) = *(src_p++);
  855. }
  856. if (*src_p)
  857. src_p++;
  858. *(dst_b++) = '\0';
  859. }
  860. static bool pooldetails(char *param, char **url, char **user, char **pass)
  861. {
  862. char *ptr, *buf;
  863. ptr = buf = malloc(strlen(param)+1);
  864. if (unlikely(!buf))
  865. quit(1, "Failed to malloc pooldetails buf");
  866. *url = buf;
  867. // copy url
  868. copyadvanceafter(',', &param, &buf);
  869. if (!(*param)) // missing user
  870. goto exitsama;
  871. *user = buf;
  872. // copy user
  873. copyadvanceafter(',', &param, &buf);
  874. if (!*param) // missing pass
  875. goto exitsama;
  876. *pass = buf;
  877. // copy pass
  878. copyadvanceafter(',', &param, &buf);
  879. return true;
  880. exitsama:
  881. free(ptr);
  882. return false;
  883. }
  884. static void addpool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  885. {
  886. char *url, *user, *pass;
  887. if (param == NULL || *param == '\0') {
  888. strcpy(io_buffer, message(MSG_MISPDP, 0, NULL, isjson));
  889. return;
  890. }
  891. if (!pooldetails(param, &url, &user, &pass)) {
  892. strcpy(io_buffer, message(MSG_INVPDP, 0, param, isjson));
  893. return;
  894. }
  895. if (add_pool_details(true, url, user, pass) == ADD_POOL_MAXIMUM) {
  896. strcpy(io_buffer, message(MSG_TOOMANYP, MAX_POOLS, NULL, isjson));
  897. return;
  898. }
  899. strcpy(io_buffer, message(MSG_ADDPOOL, 0, url, isjson));
  900. }
  901. static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  902. {
  903. struct pool *pool;
  904. int id;
  905. if (total_pools == 0) {
  906. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  907. return;
  908. }
  909. if (param == NULL || *param == '\0') {
  910. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  911. return;
  912. }
  913. id = atoi(param);
  914. if (id < 0 || id >= total_pools) {
  915. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  916. return;
  917. }
  918. pool = pools[id];
  919. if (pool->enabled == true) {
  920. strcpy(io_buffer, message(MSG_ALRENAP, id, NULL, isjson));
  921. return;
  922. }
  923. pool->enabled = true;
  924. if (pool->prio < current_pool()->prio)
  925. switch_pools(pool);
  926. strcpy(io_buffer, message(MSG_ENAPOOL, id, NULL, isjson));
  927. }
  928. static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  929. {
  930. struct pool *pool;
  931. int id;
  932. if (total_pools == 0) {
  933. strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
  934. return;
  935. }
  936. if (param == NULL || *param == '\0') {
  937. strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
  938. return;
  939. }
  940. id = atoi(param);
  941. if (id < 0 || id >= total_pools) {
  942. strcpy(io_buffer, message(MSG_INVPID, id, NULL, isjson));
  943. return;
  944. }
  945. pool = pools[id];
  946. if (pool->enabled == false) {
  947. strcpy(io_buffer, message(MSG_ALRDISP, id, NULL, isjson));
  948. return;
  949. }
  950. if (active_pools() <= 1) {
  951. strcpy(io_buffer, message(MSG_DISLASTP, id, NULL, isjson));
  952. return;
  953. }
  954. pool->enabled = false;
  955. if (pool == current_pool())
  956. switch_pools(NULL);
  957. strcpy(io_buffer, message(MSG_DISPOOL, id, NULL, isjson));
  958. }
  959. static bool splitgpuvalue(char *param, int *gpu, char **value, bool isjson)
  960. {
  961. int id;
  962. char *gpusep;
  963. if (nDevs == 0) {
  964. strcpy(io_buffer, message(MSG_GPUNON, 0, NULL, isjson));
  965. return false;
  966. }
  967. if (param == NULL || *param == '\0') {
  968. strcpy(io_buffer, message(MSG_MISID, 0, NULL, isjson));
  969. return false;
  970. }
  971. gpusep = strchr(param, GPUSEP);
  972. if (gpusep == NULL) {
  973. strcpy(io_buffer, message(MSG_MISVAL, 0, NULL, isjson));
  974. return false;
  975. }
  976. *(gpusep++) = '\0';
  977. id = atoi(param);
  978. if (id < 0 || id >= nDevs) {
  979. strcpy(io_buffer, message(MSG_INVGPU, id, NULL, isjson));
  980. return false;
  981. }
  982. *gpu = id;
  983. *value = gpusep;
  984. return true;
  985. }
  986. static void gpuintensity(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  987. {
  988. int id;
  989. char *value;
  990. int intensity;
  991. char intensitystr[7];
  992. if (!splitgpuvalue(param, &id, &value, isjson))
  993. return;
  994. if (!strncasecmp(value, DYNAMIC, 1)) {
  995. gpus[id].dynamic = true;
  996. strcpy(intensitystr, DYNAMIC);
  997. }
  998. else {
  999. intensity = atoi(value);
  1000. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  1001. strcpy(io_buffer, message(MSG_INVINT, 0, value, isjson));
  1002. return;
  1003. }
  1004. gpus[id].dynamic = false;
  1005. gpus[id].intensity = intensity;
  1006. sprintf(intensitystr, "%d", intensity);
  1007. }
  1008. strcpy(io_buffer, message(MSG_GPUINT, id, intensitystr, isjson));
  1009. }
  1010. static void gpumem(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1011. {
  1012. #ifdef HAVE_ADL
  1013. int id;
  1014. char *value;
  1015. int clock;
  1016. if (!splitgpuvalue(param, &id, &value, isjson))
  1017. return;
  1018. clock = atoi(value);
  1019. if (set_memoryclock(id, clock))
  1020. strcpy(io_buffer, message(MSG_GPUMERR, id, value, isjson));
  1021. else
  1022. strcpy(io_buffer, message(MSG_GPUMEM, id, value, isjson));
  1023. #else
  1024. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1025. #endif
  1026. }
  1027. static void gpuengine(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1028. {
  1029. #ifdef HAVE_ADL
  1030. int id;
  1031. char *value;
  1032. int clock;
  1033. if (!splitgpuvalue(param, &id, &value, isjson))
  1034. return;
  1035. clock = atoi(value);
  1036. if (set_engineclock(id, clock))
  1037. strcpy(io_buffer, message(MSG_GPUEERR, id, value, isjson));
  1038. else
  1039. strcpy(io_buffer, message(MSG_GPUENG, id, value, isjson));
  1040. #else
  1041. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1042. #endif
  1043. }
  1044. static void gpufan(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1045. {
  1046. #ifdef HAVE_ADL
  1047. int id;
  1048. char *value;
  1049. int fan;
  1050. if (!splitgpuvalue(param, &id, &value, isjson))
  1051. return;
  1052. fan = atoi(value);
  1053. if (set_fanspeed(id, fan))
  1054. strcpy(io_buffer, message(MSG_GPUFERR, id, value, isjson));
  1055. else
  1056. strcpy(io_buffer, message(MSG_GPUFAN, id, value, isjson));
  1057. #else
  1058. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1059. #endif
  1060. }
  1061. static void gpuvddc(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1062. {
  1063. #ifdef HAVE_ADL
  1064. int id;
  1065. char *value;
  1066. float vddc;
  1067. if (!splitgpuvalue(param, &id, &value, isjson))
  1068. return;
  1069. vddc = atof(value);
  1070. if (set_vddc(id, vddc))
  1071. strcpy(io_buffer, message(MSG_GPUVERR, id, value, isjson));
  1072. else
  1073. strcpy(io_buffer, message(MSG_GPUVDDC, id, value, isjson));
  1074. #else
  1075. strcpy(io_buffer, message(MSG_NOADL, 0, NULL, isjson));
  1076. #endif
  1077. }
  1078. static void send_result(SOCKETTYPE c, bool isjson);
  1079. void doquit(SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1080. {
  1081. if (isjson)
  1082. strcpy(io_buffer, JSON_START JSON_BYE);
  1083. else
  1084. strcpy(io_buffer, _BYE);
  1085. send_result(c, isjson);
  1086. *io_buffer = '\0';
  1087. bye = 1;
  1088. PTH(&thr_info[my_thr_id]) = 0L;
  1089. kill_work();
  1090. }
  1091. void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson)
  1092. {
  1093. strcpy(io_buffer, message(MSG_ACCOK, 0, NULL, isjson));
  1094. }
  1095. void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
  1096. {
  1097. FILE *fcfg;
  1098. if (param == NULL || *param == '\0') {
  1099. strcpy(io_buffer, message(MSG_MISFN, 0, NULL, isjson));
  1100. return;
  1101. }
  1102. fcfg = fopen(param, "w");
  1103. if (!fcfg) {
  1104. strcpy(io_buffer, message(MSG_BADFN, 0, param, isjson));
  1105. return;
  1106. }
  1107. write_config(fcfg);
  1108. fclose(fcfg);
  1109. strcpy(io_buffer, message(MSG_SAVED, 0, param, isjson));
  1110. }
  1111. struct CMDS {
  1112. char *name;
  1113. void (*func)(SOCKETTYPE, char *, bool);
  1114. bool requires_writemode;
  1115. } cmds[] = {
  1116. { "version", apiversion, false },
  1117. { "config", minerconfig, false },
  1118. { "devs", devstatus, false },
  1119. { "pools", poolstatus, false },
  1120. { "summary", summary, false },
  1121. { "gpuenable", gpuenable, true },
  1122. { "gpudisable", gpudisable, true },
  1123. { "gpurestart", gpurestart, true },
  1124. { "gpu", gpudev, false },
  1125. #ifdef WANT_CPUMINE
  1126. { "cpu", cpudev, false },
  1127. #endif
  1128. { "gpucount", gpucount, false },
  1129. { "cpucount", cpucount, false },
  1130. { "switchpool", switchpool, true },
  1131. { "addpool", addpool, true },
  1132. { "enablepool", enablepool, true },
  1133. { "disablepool", disablepool, true },
  1134. { "gpuintensity", gpuintensity, true },
  1135. { "gpumem", gpumem, true },
  1136. { "gpuengine", gpuengine, true },
  1137. { "gpufan", gpufan, true },
  1138. { "gpuvddc", gpuvddc, true },
  1139. { "save", dosave, true },
  1140. { "quit", doquit, true },
  1141. { "privileged", privileged, true },
  1142. { NULL, NULL, false }
  1143. };
  1144. static void send_result(SOCKETTYPE c, bool isjson)
  1145. {
  1146. int n;
  1147. int len;
  1148. if (isjson)
  1149. strcat(io_buffer, JSON_END);
  1150. len = strlen(io_buffer);
  1151. applog(LOG_DEBUG, "API: send reply: (%d) '%.10s%s'", len+1, io_buffer, len > 10 ? "..." : "");
  1152. // ignore failure - it's closed immediately anyway
  1153. n = send(c, io_buffer, len+1, 0);
  1154. if (opt_debug) {
  1155. if (SOCKETFAIL(n))
  1156. applog(LOG_DEBUG, "API: send failed: %s", SOCKERRMSG);
  1157. else
  1158. applog(LOG_DEBUG, "API: sent %d", n);
  1159. }
  1160. }
  1161. static void tidyup()
  1162. {
  1163. bye = 1;
  1164. if (sock != INVSOCK) {
  1165. shutdown(sock, SHUT_RDWR);
  1166. CLOSESOCKET(sock);
  1167. sock = INVSOCK;
  1168. }
  1169. if (ipaccess != NULL) {
  1170. free(ipaccess);
  1171. ipaccess = NULL;
  1172. }
  1173. if (msg_buffer != NULL) {
  1174. free(msg_buffer);
  1175. msg_buffer = NULL;
  1176. }
  1177. if (io_buffer != NULL) {
  1178. free(io_buffer);
  1179. io_buffer = NULL;
  1180. }
  1181. }
  1182. /*
  1183. * Interpret [R|W:]IP[/Prefix][,[R|W:]IP2[/Prefix2][,...]] --api-allow option
  1184. * special case of 0/0 allows /0 (means all IP addresses)
  1185. */
  1186. #define ALLIP4 "0/0"
  1187. /*
  1188. * N.B. IP4 addresses are by Definition 32bit big endian on all platforms
  1189. */
  1190. static void setup_ipaccess()
  1191. {
  1192. char *buf, *ptr, *comma, *slash, *dot;
  1193. int ipcount, mask, octet, i;
  1194. bool writemode;
  1195. buf = malloc(strlen(opt_api_allow) + 1);
  1196. if (unlikely(!buf))
  1197. quit(1, "Failed to malloc ipaccess buf");
  1198. strcpy(buf, opt_api_allow);
  1199. ipcount = 1;
  1200. ptr = buf;
  1201. while (*ptr)
  1202. if (*(ptr++) == ',')
  1203. ipcount++;
  1204. // possibly more than needed, but never less
  1205. ipaccess = calloc(ipcount, sizeof(struct IP4ACCESS));
  1206. if (unlikely(!ipaccess))
  1207. quit(1, "Failed to calloc ipaccess");
  1208. ips = 0;
  1209. ptr = buf;
  1210. while (ptr && *ptr) {
  1211. while (*ptr == ' ' || *ptr == '\t')
  1212. ptr++;
  1213. if (*ptr == ',') {
  1214. ptr++;
  1215. continue;
  1216. }
  1217. comma = strchr(ptr, ',');
  1218. if (comma)
  1219. *(comma++) = '\0';
  1220. writemode = false;
  1221. if (isalpha(*ptr) && *(ptr+1) == ':') {
  1222. if (tolower(*ptr) == 'w')
  1223. writemode = true;
  1224. ptr += 2;
  1225. }
  1226. ipaccess[ips].writemode = writemode;
  1227. if (strcmp(ptr, ALLIP4) == 0)
  1228. ipaccess[ips].ip = ipaccess[ips].mask = 0;
  1229. else {
  1230. slash = strchr(ptr, '/');
  1231. if (!slash)
  1232. ipaccess[ips].mask = 0xffffffff;
  1233. else {
  1234. *(slash++) = '\0';
  1235. mask = atoi(slash);
  1236. if (mask < 1 || mask > 32)
  1237. goto popipo; // skip invalid/zero
  1238. ipaccess[ips].mask = 0;
  1239. while (mask-- >= 0) {
  1240. octet = 1 << (mask % 8);
  1241. ipaccess[ips].mask |= (octet << (8 * (mask >> 3)));
  1242. }
  1243. }
  1244. ipaccess[ips].ip = 0; // missing default to '.0'
  1245. for (i = 0; ptr && (i < 4); i++) {
  1246. dot = strchr(ptr, '.');
  1247. if (dot)
  1248. *(dot++) = '\0';
  1249. octet = atoi(ptr);
  1250. if (octet < 0 || octet > 0xff)
  1251. goto popipo; // skip invalid
  1252. ipaccess[ips].ip |= (octet << (i * 8));
  1253. ptr = dot;
  1254. }
  1255. ipaccess[ips].ip &= ipaccess[ips].mask;
  1256. }
  1257. ips++;
  1258. popipo:
  1259. ptr = comma;
  1260. }
  1261. free(buf);
  1262. }
  1263. void api(int api_thr_id)
  1264. {
  1265. char buf[BUFSIZ];
  1266. char param_buf[BUFSIZ];
  1267. const char *localaddr = "127.0.0.1";
  1268. SOCKETTYPE c;
  1269. int n, bound;
  1270. char *connectaddr;
  1271. char *binderror;
  1272. time_t bindstart;
  1273. short int port = opt_api_port;
  1274. struct sockaddr_in serv;
  1275. struct sockaddr_in cli;
  1276. socklen_t clisiz;
  1277. char *cmd;
  1278. char *param;
  1279. bool addrok;
  1280. bool writemode;
  1281. json_error_t json_err;
  1282. json_t *json_config;
  1283. json_t *json_val;
  1284. bool isjson;
  1285. bool did;
  1286. int i;
  1287. my_thr_id = api_thr_id;
  1288. /* This should be done first to ensure curl has already called WSAStartup() in windows */
  1289. sleep(opt_log_interval);
  1290. if (!opt_api_listen) {
  1291. applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
  1292. return;
  1293. }
  1294. if (opt_api_allow) {
  1295. setup_ipaccess();
  1296. if (ips == 0) {
  1297. applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
  1298. return;
  1299. }
  1300. }
  1301. sock = socket(AF_INET, SOCK_STREAM, 0);
  1302. if (sock == INVSOCK) {
  1303. applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1304. return;
  1305. }
  1306. memset(&serv, 0, sizeof(serv));
  1307. serv.sin_family = AF_INET;
  1308. if (!opt_api_allow && !opt_api_network) {
  1309. serv.sin_addr.s_addr = inet_addr(localaddr);
  1310. if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
  1311. applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1312. return;
  1313. }
  1314. }
  1315. serv.sin_port = htons(port);
  1316. // try for more than 1 minute ... in case the old one hasn't completely gone yet
  1317. bound = 0;
  1318. bindstart = time(NULL);
  1319. while (bound == 0) {
  1320. if (SOCKETFAIL(bind(sock, (struct sockaddr *)(&serv), sizeof(serv)))) {
  1321. binderror = SOCKERRMSG;
  1322. if ((time(NULL) - bindstart) > 61)
  1323. break;
  1324. else {
  1325. applog(LOG_WARNING, "API bind to port %d failed - trying again in 15sec", port);
  1326. sleep(15);
  1327. }
  1328. }
  1329. else
  1330. bound = 1;
  1331. }
  1332. if (bound == 0) {
  1333. applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
  1334. return;
  1335. }
  1336. if (SOCKETFAIL(listen(sock, QUEUE))) {
  1337. applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1338. CLOSESOCKET(sock);
  1339. return;
  1340. }
  1341. if (opt_api_allow)
  1342. applog(LOG_WARNING, "API running in IP access mode");
  1343. else {
  1344. if (opt_api_network)
  1345. applog(LOG_WARNING, "API running in UNRESTRICTED access mode");
  1346. else
  1347. applog(LOG_WARNING, "API running in local access mode");
  1348. }
  1349. io_buffer = malloc(MYBUFSIZ+1);
  1350. msg_buffer = malloc(MYBUFSIZ+1);
  1351. while (bye == 0) {
  1352. clisiz = sizeof(cli);
  1353. if (SOCKETFAIL(c = accept(sock, (struct sockaddr *)(&cli), &clisiz))) {
  1354. applog(LOG_ERR, "API failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
  1355. goto die;
  1356. }
  1357. connectaddr = inet_ntoa(cli.sin_addr);
  1358. addrok = false;
  1359. writemode = false;
  1360. if (opt_api_allow) {
  1361. for (i = 0; i < ips; i++) {
  1362. if ((cli.sin_addr.s_addr & ipaccess[i].mask) == ipaccess[i].ip) {
  1363. addrok = true;
  1364. writemode = ipaccess[i].writemode;
  1365. break;
  1366. }
  1367. }
  1368. } else {
  1369. if (opt_api_network)
  1370. addrok = true;
  1371. else
  1372. addrok = (strcmp(connectaddr, localaddr) == 0);
  1373. }
  1374. if (opt_debug)
  1375. applog(LOG_DEBUG, "API: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
  1376. if (addrok) {
  1377. n = recv(c, &buf[0], BUFSIZ-1, 0);
  1378. if (SOCKETFAIL(n))
  1379. buf[0] = '\0';
  1380. else
  1381. buf[n] = '\0';
  1382. if (opt_debug) {
  1383. if (SOCKETFAIL(n))
  1384. applog(LOG_DEBUG, "API: recv failed: %s", SOCKERRMSG);
  1385. else
  1386. applog(LOG_DEBUG, "API: recv command: (%d) '%s'", n, buf);
  1387. }
  1388. if (!SOCKETFAIL(n)) {
  1389. did = false;
  1390. if (*buf != ISJSON) {
  1391. isjson = false;
  1392. param = strchr(buf, SEPARATOR);
  1393. if (param != NULL)
  1394. *(param++) = '\0';
  1395. cmd = buf;
  1396. }
  1397. else {
  1398. isjson = true;
  1399. param = NULL;
  1400. #if JANSSON_MAJOR_VERSION > 2 || (JANSSON_MAJOR_VERSION == 2 && JANSSON_MINOR_VERSION > 0)
  1401. json_config = json_loadb(buf, n, 0, &json_err);
  1402. #elif JANSSON_MAJOR_VERSION > 1
  1403. json_config = json_loads(buf, 0, &json_err);
  1404. #else
  1405. json_config = json_loads(buf, &json_err);
  1406. #endif
  1407. if (!json_is_object(json_config)) {
  1408. strcpy(io_buffer, message(MSG_INVJSON, 0, NULL, isjson));
  1409. send_result(c, isjson);
  1410. did = true;
  1411. }
  1412. else {
  1413. json_val = json_object_get(json_config, JSON_COMMAND);
  1414. if (json_val == NULL) {
  1415. strcpy(io_buffer, message(MSG_MISCMD, 0, NULL, isjson));
  1416. send_result(c, isjson);
  1417. did = true;
  1418. }
  1419. else {
  1420. if (!json_is_string(json_val)) {
  1421. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1422. send_result(c, isjson);
  1423. did = true;
  1424. }
  1425. else {
  1426. cmd = (char *)json_string_value(json_val);
  1427. json_val = json_object_get(json_config, JSON_PARAMETER);
  1428. if (json_is_string(json_val))
  1429. param = (char *)json_string_value(json_val);
  1430. else if (json_is_integer(json_val)) {
  1431. sprintf(param_buf, "%d", (int)json_integer_value(json_val));
  1432. param = param_buf;
  1433. } else if (json_is_real(json_val)) {
  1434. sprintf(param_buf, "%f", (double)json_real_value(json_val));
  1435. param = param_buf;
  1436. }
  1437. }
  1438. }
  1439. }
  1440. }
  1441. if (!did)
  1442. for (i = 0; cmds[i].name != NULL; i++) {
  1443. if (strcmp(cmd, cmds[i].name) == 0) {
  1444. if (cmds[i].requires_writemode && !writemode) {
  1445. strcpy(io_buffer, message(MSG_ACCDENY, 0, cmds[i].name, isjson));
  1446. applog(LOG_DEBUG, "API: access denied to '%s' for '%s' command", connectaddr, cmds[i].name);
  1447. }
  1448. else
  1449. (cmds[i].func)(c, param, isjson);
  1450. send_result(c, isjson);
  1451. did = true;
  1452. break;
  1453. }
  1454. }
  1455. if (!did) {
  1456. strcpy(io_buffer, message(MSG_INVCMD, 0, NULL, isjson));
  1457. send_result(c, isjson);
  1458. }
  1459. }
  1460. }
  1461. CLOSESOCKET(c);
  1462. }
  1463. die:
  1464. tidyup();
  1465. }