util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2012-2013 Con Kolivas
  4. * Copyright 2011 Andrew Smith
  5. * Copyright 2011 Jeff Garzik
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #ifndef BFG_UTIL_H
  13. #define BFG_UTIL_H
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <sys/time.h>
  18. #include <curl/curl.h>
  19. #include <jansson.h>
  20. #include "compat.h"
  21. #define INVALID_TIMESTAMP ((time_t)-1)
  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 SOCKERR (errno)
  33. #define SOCKERRMSG bfg_strerror(errno, BST_SOCKET)
  34. static inline bool sock_blocks(void)
  35. {
  36. return (errno == EAGAIN || errno == EWOULDBLOCK);
  37. }
  38. static inline bool interrupted(void)
  39. {
  40. return (errno == EINTR);
  41. }
  42. #elif defined WIN32
  43. #include <ws2tcpip.h>
  44. #include <winsock2.h>
  45. #define SOCKETTYPE SOCKET
  46. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  47. #define INVSOCK INVALID_SOCKET
  48. #define INVINETADDR INADDR_NONE
  49. #define CLOSESOCKET closesocket
  50. #define SOCKERR (WSAGetLastError())
  51. #define SOCKERRMSG bfg_strerror(WSAGetLastError(), BST_SOCKET)
  52. static inline bool sock_blocks(void)
  53. {
  54. return (WSAGetLastError() == WSAEWOULDBLOCK);
  55. }
  56. static inline bool interrupted(void)
  57. {
  58. return (WSAGetLastError() == WSAEINTR);
  59. }
  60. #ifndef SHUT_RDWR
  61. #define SHUT_RDWR SD_BOTH
  62. #endif
  63. #ifndef in_addr_t
  64. #define in_addr_t uint32_t
  65. #endif
  66. #endif
  67. #define IGNORE_RETURN_VALUE(expr) {if(expr);}(void)0
  68. #if JANSSON_MAJOR_VERSION >= 2
  69. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  70. #else
  71. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  72. #endif
  73. extern char *json_dumps_ANY(json_t *, size_t flags);
  74. static inline
  75. const char *bfg_json_obj_string(json_t *json, const char *key, const char *fail)
  76. {
  77. json = json_object_get(json, key);
  78. if (!json)
  79. return fail;
  80. return json_string_value(json) ?: fail;
  81. }
  82. extern const char *__json_array_string(json_t *, unsigned int entry);
  83. static inline
  84. bool isCspace(int c)
  85. {
  86. switch (c)
  87. {
  88. case ' ': case '\f': case '\n': case '\r': case '\t': case '\v':
  89. return true;
  90. default:
  91. return false;
  92. }
  93. }
  94. typedef struct timeval cgtimer_t;
  95. struct thr_info;
  96. struct pool;
  97. enum dev_reason;
  98. struct cgpu_info;
  99. extern void json_rpc_call_async(CURL *, const char *url, const char *userpass, const char *rpc_req, bool longpoll, struct pool *pool, bool share, void *priv);
  100. extern json_t *json_rpc_call_completed(CURL *, int rc, bool probe, int *rolltime, void *out_priv);
  101. extern char *absolute_uri(char *uri, const char *ref); // ref must be a root URI
  102. extern void ucs2tochar(char *out, const uint16_t *in, size_t sz);
  103. extern char *ucs2tochar_dup(uint16_t *in, size_t sz);
  104. #define BFGINIT(var, val) do{ \
  105. if (!(var)) \
  106. (var) = val; \
  107. }while(0)
  108. extern void gen_hash(unsigned char *data, unsigned char *hash, int len);
  109. extern void hash_data(unsigned char *out_hash, const unsigned char *data);
  110. extern void real_block_target(unsigned char *target, const unsigned char *data);
  111. extern bool hash_target_check(const unsigned char *hash, const unsigned char *target);
  112. extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
  113. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  114. void thr_info_freeze(struct thr_info *thr);
  115. void thr_info_cancel(struct thr_info *thr);
  116. void subtime(struct timeval *a, struct timeval *b);
  117. void addtime(struct timeval *a, struct timeval *b);
  118. bool time_more(struct timeval *a, struct timeval *b);
  119. bool time_less(struct timeval *a, struct timeval *b);
  120. void copy_time(struct timeval *dest, const struct timeval *src);
  121. void timespec_to_val(struct timeval *val, const struct timespec *spec);
  122. void timeval_to_spec(struct timespec *spec, const struct timeval *val);
  123. void us_to_timeval(struct timeval *val, int64_t us);
  124. void us_to_timespec(struct timespec *spec, int64_t us);
  125. void ms_to_timespec(struct timespec *spec, int64_t ms);
  126. void timeraddspec(struct timespec *a, const struct timespec *b);
  127. void cgsleep_ms(int ms);
  128. void cgsleep_us(int64_t us);
  129. #define cgtimer_time(ts_start) timer_set_now(ts_start)
  130. #define cgsleep_prepare_r(ts_start) cgtimer_time(ts_start)
  131. void cgsleep_ms_r(cgtimer_t *ts_start, int ms);
  132. void (*cgsleep_us_r)(cgtimer_t *ts_start, int64_t us);
  133. static inline
  134. int cgtimer_to_ms(cgtimer_t *cgt)
  135. {
  136. return (cgt->tv_sec * 1000) + (cgt->tv_usec / 1000);
  137. }
  138. #define cgtimer_sub(a, b, res) timersub(a, b, res)
  139. double us_tdiff(struct timeval *end, struct timeval *start);
  140. double tdiff(struct timeval *end, struct timeval *start);
  141. bool _stratum_send(struct pool *pool, char *s, ssize_t len, bool force);
  142. #define stratum_send(pool, s, len) _stratum_send(pool, s, len, false)
  143. bool sock_full(struct pool *pool);
  144. char *recv_line(struct pool *pool);
  145. bool parse_method(struct pool *pool, char *s);
  146. bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);
  147. bool auth_stratum(struct pool *pool);
  148. bool initiate_stratum(struct pool *pool);
  149. bool restart_stratum(struct pool *pool);
  150. void suspend_stratum(struct pool *pool);
  151. extern void dev_error_update(struct cgpu_info *, enum dev_reason);
  152. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  153. void *realloc_strcat(char *ptr, char *s);
  154. extern char *sanestr(char *o, char *s);
  155. void RenameThread(const char* name);
  156. enum bfg_strerror_type {
  157. BST_ERRNO,
  158. BST_SOCKET,
  159. BST_LIBUSB,
  160. BST_SYSTEM,
  161. };
  162. extern const char *bfg_strerror(int, enum bfg_strerror_type);
  163. typedef SOCKETTYPE notifier_t[2];
  164. extern void notifier_init(notifier_t);
  165. extern void notifier_wake(notifier_t);
  166. extern void notifier_read(notifier_t);
  167. extern void notifier_init_invalid(notifier_t);
  168. extern void notifier_destroy(notifier_t);
  169. /* Align a size_t to 4 byte boundaries for fussy arches */
  170. static inline void align_len(size_t *len)
  171. {
  172. if (*len % 4)
  173. *len += 4 - (*len % 4);
  174. }
  175. typedef struct bytes_t {
  176. uint8_t *buf;
  177. size_t sz;
  178. size_t allocsz;
  179. } bytes_t;
  180. #define BYTES_INIT ((bytes_t){.buf=NULL,})
  181. static inline
  182. void bytes_init(bytes_t *b)
  183. {
  184. *b = BYTES_INIT;
  185. }
  186. // This can't be inline without ugly const/non-const issues
  187. #define bytes_buf(b) ((b)->buf)
  188. static inline
  189. size_t bytes_len(const bytes_t *b)
  190. {
  191. return b->sz;
  192. }
  193. extern void _bytes_alloc_failure(size_t);
  194. static inline
  195. void bytes_resize(bytes_t *b, size_t newsz)
  196. {
  197. b->sz = newsz;
  198. if (newsz <= b->allocsz)
  199. return;
  200. if (!b->allocsz)
  201. b->allocsz = 0x10;
  202. do {
  203. b->allocsz *= 2;
  204. } while (newsz > b->allocsz);
  205. b->buf = realloc(b->buf, b->allocsz);
  206. if (!b->buf)
  207. _bytes_alloc_failure(b->allocsz);
  208. }
  209. static inline
  210. void bytes_append(bytes_t *b, const void *add, size_t addsz)
  211. {
  212. size_t origsz = bytes_len(b);
  213. bytes_resize(b, origsz + addsz);
  214. memcpy(&bytes_buf(b)[origsz], add, addsz);
  215. }
  216. static inline
  217. void bytes_cat(bytes_t *b, const bytes_t *cat)
  218. {
  219. bytes_append(b, bytes_buf(cat), bytes_len(cat));
  220. }
  221. static inline
  222. void bytes_cpy(bytes_t *dst, const bytes_t *src)
  223. {
  224. dst->sz = src->sz;
  225. if (!dst->sz) {
  226. dst->allocsz = 0;
  227. dst->buf = NULL;
  228. return;
  229. }
  230. dst->allocsz = src->allocsz;
  231. size_t half;
  232. while (dst->sz <= (half = dst->allocsz / 2))
  233. dst->allocsz = half;
  234. dst->buf = malloc(dst->allocsz);
  235. memcpy(dst->buf, src->buf, dst->sz);
  236. }
  237. static inline
  238. void bytes_shift(bytes_t *b, size_t shift)
  239. {
  240. b->sz -= shift;
  241. memmove(bytes_buf(b), &bytes_buf(b)[shift], bytes_len(b));
  242. }
  243. static inline
  244. void bytes_reset(bytes_t *b)
  245. {
  246. b->sz = 0;
  247. }
  248. static inline
  249. void bytes_nullterminate(bytes_t *b)
  250. {
  251. bytes_append(b, "", 1);
  252. --b->sz;
  253. }
  254. static inline
  255. void bytes_free(bytes_t *b)
  256. {
  257. free(b->buf);
  258. b->sz = b->allocsz = 0;
  259. }
  260. static inline
  261. void set_maxfd(int *p_maxfd, int fd)
  262. {
  263. if (fd > *p_maxfd)
  264. *p_maxfd = fd;
  265. }
  266. static inline
  267. void timer_unset(struct timeval *tvp)
  268. {
  269. tvp->tv_sec = -1;
  270. }
  271. static inline
  272. bool timer_isset(const struct timeval *tvp)
  273. {
  274. return tvp->tv_sec != -1;
  275. }
  276. extern void (*timer_set_now)(struct timeval *);
  277. #define cgtime(tvp) timer_set_now(tvp)
  278. #define TIMEVAL_USECS(usecs) ( \
  279. (struct timeval){ \
  280. .tv_sec = (usecs) / 1000000, \
  281. .tv_usec = (usecs) % 1000000, \
  282. } \
  283. )
  284. static inline
  285. long timeval_to_us(const struct timeval *tvp)
  286. {
  287. return ((long)tvp->tv_sec * 1000000) + tvp->tv_usec;
  288. }
  289. #define timer_set_delay(tvp_timer, tvp_now, usecs) do { \
  290. struct timeval tv_add = TIMEVAL_USECS(usecs); \
  291. timeradd(&tv_add, tvp_now, tvp_timer); \
  292. } while(0)
  293. #define timer_set_delay_from_now(tvp_timer, usecs) do { \
  294. struct timeval tv_now; \
  295. timer_set_now(&tv_now); \
  296. timer_set_delay(tvp_timer, &tv_now, usecs); \
  297. } while(0)
  298. static inline
  299. const struct timeval *_bfg_nullisnow(const struct timeval *tvp, struct timeval *tvp_buf)
  300. {
  301. if (tvp)
  302. return tvp;
  303. cgtime(tvp_buf);
  304. return tvp_buf;
  305. }
  306. static inline
  307. long timer_elapsed_us(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  308. {
  309. struct timeval tv;
  310. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  311. timersub(_tvp_now, tvp_timer, &tv);
  312. return timeval_to_us(&tv);
  313. }
  314. static inline
  315. int timer_elapsed(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  316. {
  317. struct timeval tv;
  318. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  319. timersub(_tvp_now, tvp_timer, &tv);
  320. return tv.tv_sec;
  321. }
  322. static inline
  323. bool timer_passed(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  324. {
  325. if (!timer_isset(tvp_timer))
  326. return false;
  327. struct timeval tv;
  328. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  329. return timercmp(tvp_timer, _tvp_now, <);
  330. }
  331. #if defined(WIN32) && !defined(HAVE_POOR_GETTIMEOFDAY)
  332. #define HAVE_POOR_GETTIMEOFDAY
  333. #endif
  334. #ifdef HAVE_POOR_GETTIMEOFDAY
  335. extern void bfg_gettimeofday(struct timeval *);
  336. #else
  337. #define bfg_gettimeofday(out) gettimeofday(out, NULL)
  338. #endif
  339. static inline
  340. void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
  341. {
  342. if (!timer_isset(tvp_time))
  343. return;
  344. if ((!timer_isset(tvp_timeout)) || timercmp(tvp_time, tvp_timeout, <))
  345. *tvp_timeout = *tvp_time;
  346. }
  347. static inline
  348. struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_now)
  349. {
  350. if (!timer_isset(tvp_timeout))
  351. return NULL;
  352. if (timercmp(tvp_timeout, tvp_now, <))
  353. timerclear(tvp_timeout);
  354. else
  355. timersub(tvp_timeout, tvp_now, tvp_timeout);
  356. return tvp_timeout;
  357. }
  358. #define _SNP2(fn, ...) do{ \
  359. int __n42 = fn(s, sz, __VA_ARGS__); \
  360. s += __n42; \
  361. sz = (sz <= __n42) ? 0 : (sz - __n42); \
  362. rv += __n42; \
  363. }while(0)
  364. #define _SNP(...) _SNP2(snprintf, __VA_ARGS__)
  365. #define RUNONCE(rv) do { \
  366. static bool _runonce = false; \
  367. if (_runonce) \
  368. return rv; \
  369. _runonce = true; \
  370. } while(0)
  371. static inline
  372. char *maybe_strdup(const char *s)
  373. {
  374. return s ? strdup(s) : NULL;
  375. }
  376. static inline
  377. void maybe_strdup_if_null(const char **p, const char *s)
  378. {
  379. if (!*p)
  380. *p = maybe_strdup(s);
  381. }
  382. extern void run_cmd(const char *cmd);
  383. #endif /* __UTIL_H__ */