util.h 7.1 KB

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