util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. enum timestamp_format {
  255. BTF_LRTIME = 2, // low resolution time (eg, HH:MM)
  256. BTF_TIME = 1, // eg, HH:MM:SS
  257. BTF_HRTIME = 3, // high resolution time (eg, HH:MM:SS.MICROS)
  258. BTF_DATE = 8,
  259. BTF_BRACKETS = 0x10,
  260. };
  261. extern int format_elapsed2(char * const buf, size_t sz, const int fmt, int elapsed);
  262. #define format_elapsed(buf, fmt, elapsed) \
  263. format_elapsed2(buf, SIZE_MAX, fmt, elapsed)
  264. extern int format_tm2(char * const buf, size_t sz, const int fmt, const struct tm * const, const long usecs);
  265. #define format_tm(buf, fmt, tm, usecs) \
  266. format_tm2(buf, SIZE_MAX, fmt, tm, usecs)
  267. extern int format_timestamp2(char * const buf, size_t sz, const int fmt, const struct timeval * const);
  268. #define format_timestamp(buf, fmt, tv) \
  269. format_timestamp2(buf, SIZE_MAX, fmt, tv)
  270. #define format_time_t(buf, fmt, tt) format_timestamp(buf, fmt, (struct timeval[1]){ { .tv_sec = tt } })
  271. #define format_time_t2(buf, sz, fmt, tt) format_timestamp2(buf, sz, fmt, (struct timeval[1]){ { .tv_sec = tt } })
  272. #define get_datestamp(buf, tt) format_time_t(buf, BTF_DATE | BTF_TIME | BTF_BRACKETS, tt)
  273. #define get_now_datestamp(buf) get_datestamp(buf, time(NULL))
  274. #define get_timestamp(buf, tt) format_time_t(buf, BTF_TIME | BTF_BRACKETS, tt)
  275. static inline
  276. void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
  277. {
  278. if (!timer_isset(tvp_time))
  279. return;
  280. if ((!timer_isset(tvp_timeout)) || timercmp(tvp_time, tvp_timeout, <))
  281. *tvp_timeout = *tvp_time;
  282. }
  283. static inline
  284. struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_now)
  285. {
  286. if (!timer_isset(tvp_timeout))
  287. return NULL;
  288. if (timercmp(tvp_timeout, tvp_now, <))
  289. timerclear(tvp_timeout);
  290. else
  291. timersub(tvp_timeout, tvp_now, tvp_timeout);
  292. return tvp_timeout;
  293. }
  294. enum h2bs_fmt {
  295. H2B_NOUNIT, // "xxx.x"
  296. H2B_SHORT, // "xxx.xMh/s"
  297. H2B_SPACED, // "xxx.x Mh/s"
  298. };
  299. #define REPLACEMENT_CHAR (0xFFFD)
  300. #define U8_DEGREE "\xc2\xb0"
  301. #define U8_HLINE "\xe2\x94\x80"
  302. #define U8_BTEE "\xe2\x94\xb4"
  303. extern int32_t utf8_decode(const void *, int *out_len);
  304. extern void utf8_test();
  305. #define _SNP2(fn, ...) do{ \
  306. int __n42 = fn(s, sz, __VA_ARGS__); \
  307. s += __n42; \
  308. sz = (sz <= __n42) ? 0 : (sz - __n42); \
  309. rv += __n42; \
  310. }while(0)
  311. #define _SNP(...) _SNP2(snprintf, __VA_ARGS__)
  312. extern int format_unit2(char *buf, size_t, bool floatprec, const char *measurement, enum h2bs_fmt fmt, float n, signed char unitin);
  313. extern int percentf3(char * const buf, size_t, double p, const double t);
  314. extern int format_temperature2(char * const buf, size_t, const int pad, const bool highprecision, const bool unicode, const float temp);
  315. #define format_temperature(buf, pad, highprecision, unicode, temp) \
  316. format_temperature2(buf, SIZE_MAX, pad, highprecision, unicode, temp)
  317. extern int format_temperature_sz(const int numsz, const bool unicode);
  318. #define BPRItm "\b\x01%d%p%ld"
  319. #define BPRIts "\b\x02%d%p"
  320. #define BPRItt "\b\x03%d%"PRId64
  321. #define BPRIte "\b\x04%d%d"
  322. #define BPRInd "\b\x05%s%d%f%c"
  323. #define BPRInf "\b\x06%s%d%f%c"
  324. #define BPRItp "\b\x07%f"
  325. #define BPRIpgo "\b\x08%f%f"
  326. #define BPRIpgt "\b\x09%f%f"
  327. #ifdef va_arg
  328. extern int bfg_vsnprintf(char * const buf, size_t, const char *fmt, va_list ap) FORMAT_SYNTAX_CHECK(printf, 3, 0);
  329. #endif
  330. extern int bfg_snprintf(char * const buf, size_t, const char * const fmt, ...) FORMAT_SYNTAX_CHECK(printf, 3, 4);
  331. extern void test_bfg_sprintf();
  332. #define RUNONCE(rv) do { \
  333. static bool _runonce = false; \
  334. if (_runonce) \
  335. return rv; \
  336. _runonce = true; \
  337. } while(0)
  338. static inline
  339. char *maybe_strdup(const char *s)
  340. {
  341. return s ? strdup(s) : NULL;
  342. }
  343. static inline
  344. void maybe_strdup_if_null(const char **p, const char *s)
  345. {
  346. if (!*p)
  347. *p = maybe_strdup(s);
  348. }
  349. #endif /* __UTIL_H__ */