util.h 8.3 KB

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