util.h 8.0 KB

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