util.h 11 KB

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