util.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 {.buf=NULL,}
  136. static inline
  137. void bytes_init(bytes_t *b)
  138. {
  139. *b = (bytes_t)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_reset(bytes_t *b)
  194. {
  195. b->sz = 0;
  196. }
  197. static inline
  198. void bytes_free(bytes_t *b)
  199. {
  200. free(b->buf);
  201. b->sz = b->allocsz = 0;
  202. }
  203. static inline
  204. void set_maxfd(int *p_maxfd, int fd)
  205. {
  206. if (fd > *p_maxfd)
  207. *p_maxfd = fd;
  208. }
  209. static inline
  210. void timer_unset(struct timeval *tvp)
  211. {
  212. tvp->tv_sec = -1;
  213. }
  214. static inline
  215. bool timer_isset(const struct timeval *tvp)
  216. {
  217. return tvp->tv_sec != -1;
  218. }
  219. extern void (*timer_set_now)(struct timeval *);
  220. extern void bfg_init_time();
  221. #define cgtime(tvp) timer_set_now(tvp)
  222. #define TIMEVAL_USECS(usecs) ( \
  223. (struct timeval){ \
  224. .tv_sec = (usecs) / 1000000, \
  225. .tv_usec = (usecs) % 1000000, \
  226. } \
  227. )
  228. #define timer_set_delay(tvp_timer, tvp_now, usecs) do { \
  229. struct timeval tv_add = TIMEVAL_USECS(usecs); \
  230. timeradd(&tv_add, tvp_now, tvp_timer); \
  231. } while(0)
  232. #define timer_set_delay_from_now(tvp_timer, usecs) do { \
  233. struct timeval tv_now; \
  234. timer_set_now(&tv_now); \
  235. timer_set_delay(tvp_timer, &tv_now, usecs); \
  236. } while(0)
  237. static inline
  238. const struct timeval *_bfg_nullisnow(const struct timeval *tvp, struct timeval *tvp_buf)
  239. {
  240. if (tvp)
  241. return tvp;
  242. cgtime(tvp_buf);
  243. return tvp_buf;
  244. }
  245. static inline
  246. int timer_elapsed(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  247. {
  248. struct timeval tv;
  249. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  250. timersub(_tvp_now, tvp_timer, &tv);
  251. return tv.tv_sec;
  252. }
  253. static inline
  254. bool timer_passed(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  255. {
  256. if (!timer_isset(tvp_timer))
  257. return false;
  258. struct timeval tv;
  259. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  260. return timercmp(tvp_timer, _tvp_now, <);
  261. }
  262. #if defined(WIN32) && !defined(HAVE_POOR_GETTIMEOFDAY)
  263. #define HAVE_POOR_GETTIMEOFDAY
  264. #endif
  265. #ifdef HAVE_POOR_GETTIMEOFDAY
  266. extern void bfg_gettimeofday(struct timeval *);
  267. #else
  268. #define bfg_gettimeofday(out) gettimeofday(out, NULL)
  269. #endif
  270. static inline
  271. void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
  272. {
  273. if (!timer_isset(tvp_time))
  274. return;
  275. if ((!timer_isset(tvp_timeout)) || timercmp(tvp_time, tvp_timeout, <))
  276. *tvp_timeout = *tvp_time;
  277. }
  278. static inline
  279. struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_now)
  280. {
  281. if (!timer_isset(tvp_timeout))
  282. return NULL;
  283. if (timercmp(tvp_timeout, tvp_now, <))
  284. timerclear(tvp_timeout);
  285. else
  286. timersub(tvp_timeout, tvp_now, tvp_timeout);
  287. return tvp_timeout;
  288. }
  289. #define RUNONCE(rv) do { \
  290. static bool _runonce = false; \
  291. if (_runonce) \
  292. return rv; \
  293. _runonce = true; \
  294. } while(0)
  295. static inline
  296. char *maybe_strdup(const char *s)
  297. {
  298. return s ? strdup(s) : NULL;
  299. }
  300. static inline
  301. void maybe_strdup_if_null(const char **p, const char *s)
  302. {
  303. if (!*p)
  304. *p = maybe_strdup(s);
  305. }
  306. extern void run_cmd(const char *cmd);
  307. #endif /* __UTIL_H__ */