util.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // This can't be inline without ugly const/non-const issues
  136. #define bytes_buf(b) ((b)->buf)
  137. static inline
  138. size_t bytes_len(const bytes_t *b)
  139. {
  140. return b->sz;
  141. }
  142. extern void _bytes_alloc_failure(size_t);
  143. static inline
  144. void bytes_resize(bytes_t *b, size_t newsz)
  145. {
  146. b->sz = newsz;
  147. if (newsz <= b->allocsz)
  148. return;
  149. if (!b->allocsz)
  150. b->allocsz = 0x10;
  151. do {
  152. b->allocsz *= 2;
  153. } while (newsz > b->allocsz);
  154. b->buf = realloc(b->buf, b->allocsz);
  155. if (!b->buf)
  156. _bytes_alloc_failure(b->allocsz);
  157. }
  158. static inline
  159. void bytes_cat(bytes_t *b, const bytes_t *cat)
  160. {
  161. size_t origsz = bytes_len(b);
  162. size_t addsz = bytes_len(cat);
  163. bytes_resize(b, origsz + addsz);
  164. memcpy(&bytes_buf(b)[origsz], bytes_buf(cat), addsz);
  165. }
  166. static inline
  167. void bytes_cpy(bytes_t *dst, const bytes_t *src)
  168. {
  169. dst->sz = src->sz;
  170. if (!dst->sz) {
  171. dst->allocsz = 0;
  172. dst->buf = NULL;
  173. return;
  174. }
  175. dst->allocsz = src->allocsz;
  176. size_t half;
  177. while (dst->sz <= (half = dst->allocsz / 2))
  178. dst->allocsz = half;
  179. dst->buf = malloc(dst->allocsz);
  180. memcpy(dst->buf, src->buf, dst->sz);
  181. }
  182. static inline
  183. void bytes_free(bytes_t *b)
  184. {
  185. free(b->buf);
  186. b->sz = b->allocsz = 0;
  187. }
  188. static inline
  189. void set_maxfd(int *p_maxfd, int fd)
  190. {
  191. if (fd > *p_maxfd)
  192. *p_maxfd = fd;
  193. }
  194. static inline
  195. void timer_unset(struct timeval *tvp)
  196. {
  197. tvp->tv_sec = -1;
  198. }
  199. static inline
  200. bool timer_isset(const struct timeval *tvp)
  201. {
  202. return tvp->tv_sec != -1;
  203. }
  204. extern void (*timer_set_now)(struct timeval *);
  205. extern void bfg_init_time();
  206. #define cgtime(tvp) timer_set_now(tvp)
  207. #define TIMEVAL_USECS(usecs) ( \
  208. (struct timeval){ \
  209. .tv_sec = (usecs) / 1000000, \
  210. .tv_usec = (usecs) % 1000000, \
  211. } \
  212. )
  213. #define timer_set_delay(tvp_timer, tvp_now, usecs) do { \
  214. struct timeval tv_add = TIMEVAL_USECS(usecs); \
  215. timeradd(&tv_add, tvp_now, tvp_timer); \
  216. } while(0)
  217. #define timer_set_delay_from_now(tvp_timer, usecs) do { \
  218. struct timeval tv_now; \
  219. timer_set_now(&tv_now); \
  220. timer_set_delay(tvp_timer, &tv_now, usecs); \
  221. } while(0)
  222. static inline
  223. const struct timeval *_bfg_nullisnow(const struct timeval *tvp, struct timeval *tvp_buf)
  224. {
  225. if (tvp)
  226. return tvp;
  227. cgtime(tvp_buf);
  228. return tvp_buf;
  229. }
  230. static inline
  231. int timer_elapsed(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  232. {
  233. struct timeval tv;
  234. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  235. timersub(_tvp_now, tvp_timer, &tv);
  236. return tv.tv_sec;
  237. }
  238. static inline
  239. bool timer_passed(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  240. {
  241. if (!timer_isset(tvp_timer))
  242. return false;
  243. struct timeval tv;
  244. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  245. return timercmp(tvp_timer, _tvp_now, <);
  246. }
  247. #if defined(WIN32) && !defined(HAVE_POOR_GETTIMEOFDAY)
  248. #define HAVE_POOR_GETTIMEOFDAY
  249. #endif
  250. #ifdef HAVE_POOR_GETTIMEOFDAY
  251. extern void bfg_gettimeofday(struct timeval *);
  252. #else
  253. #define bfg_gettimeofday(out) gettimeofday(out, NULL)
  254. #endif
  255. static inline
  256. void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
  257. {
  258. if (!timer_isset(tvp_time))
  259. return;
  260. if ((!timer_isset(tvp_timeout)) || timercmp(tvp_time, tvp_timeout, <))
  261. *tvp_timeout = *tvp_time;
  262. }
  263. static inline
  264. struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_now)
  265. {
  266. if (!timer_isset(tvp_timeout))
  267. return NULL;
  268. if (timercmp(tvp_timeout, tvp_now, <))
  269. timerclear(tvp_timeout);
  270. else
  271. timersub(tvp_timeout, tvp_now, tvp_timeout);
  272. return tvp_timeout;
  273. }
  274. #define RUNONCE(rv) do { \
  275. static bool _runonce = false; \
  276. if (_runonce) \
  277. return rv; \
  278. _runonce = true; \
  279. } while(0)
  280. static inline
  281. char *maybe_strdup(const char *s)
  282. {
  283. return s ? strdup(s) : NULL;
  284. }
  285. static inline
  286. void maybe_strdup_if_null(const char **p, const char *s)
  287. {
  288. if (!*p)
  289. *p = maybe_strdup(s);
  290. }
  291. extern void run_cmd(const char *cmd);
  292. #endif /* __UTIL_H__ */