util.h 8.9 KB

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