util.h 8.1 KB

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