util.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 SOCKERRMSG strerror(errno)
  28. static inline bool sock_blocks(void)
  29. {
  30. return (errno == EAGAIN || errno == EWOULDBLOCK);
  31. }
  32. #elif defined WIN32
  33. #include <ws2tcpip.h>
  34. #include <winsock2.h>
  35. #define SOCKETTYPE SOCKET
  36. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  37. #define INVSOCK INVALID_SOCKET
  38. #define INVINETADDR INADDR_NONE
  39. #define CLOSESOCKET closesocket
  40. extern char *WSAErrorMsg(void);
  41. #define SOCKERRMSG WSAErrorMsg()
  42. static inline bool sock_blocks(void)
  43. {
  44. return (WSAGetLastError() == WSAEWOULDBLOCK);
  45. }
  46. #ifndef SHUT_RDWR
  47. #define SHUT_RDWR SD_BOTH
  48. #endif
  49. #ifndef in_addr_t
  50. #define in_addr_t uint32_t
  51. #endif
  52. #endif
  53. #if JANSSON_MAJOR_VERSION >= 2
  54. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  55. #else
  56. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  57. #endif
  58. extern char *json_dumps_ANY(json_t *, size_t flags);
  59. struct thr_info;
  60. struct pool;
  61. enum dev_reason;
  62. struct cgpu_info;
  63. 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);
  64. extern json_t *json_rpc_call_completed(CURL *, int rc, bool probe, int *rolltime, void *out_priv);
  65. extern char *absolute_uri(char *uri, const char *ref); // ref must be a root URI
  66. extern void gen_hash(unsigned char *data, unsigned char *hash, int len);
  67. extern void hash_data(unsigned char *out_hash, const unsigned char *data);
  68. extern void real_block_target(unsigned char *target, const unsigned char *data);
  69. extern bool hash_target_check(const unsigned char *hash, const unsigned char *target);
  70. extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
  71. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  72. void thr_info_freeze(struct thr_info *thr);
  73. void thr_info_cancel(struct thr_info *thr);
  74. void nmsleep(unsigned int msecs);
  75. void nusleep(unsigned int usecs);
  76. void cgtime(struct timeval *tv);
  77. void subtime(struct timeval *a, struct timeval *b);
  78. void addtime(struct timeval *a, struct timeval *b);
  79. bool time_more(struct timeval *a, struct timeval *b);
  80. bool time_less(struct timeval *a, struct timeval *b);
  81. void copy_time(struct timeval *dest, const struct timeval *src);
  82. double us_tdiff(struct timeval *end, struct timeval *start);
  83. double tdiff(struct timeval *end, struct timeval *start);
  84. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  85. bool sock_full(struct pool *pool);
  86. char *recv_line(struct pool *pool);
  87. bool parse_method(struct pool *pool, char *s);
  88. bool extract_sockaddr(struct pool *pool, char *url);
  89. bool auth_stratum(struct pool *pool);
  90. bool initiate_stratum(struct pool *pool);
  91. bool restart_stratum(struct pool *pool);
  92. void suspend_stratum(struct pool *pool);
  93. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  94. void *realloc_strcat(char *ptr, char *s);
  95. extern char *sanestr(char *o, char *s);
  96. void RenameThread(const char* name);
  97. enum bfg_strerror_type {
  98. BST_ERRNO,
  99. BST_SOCKET,
  100. BST_LIBUSB,
  101. };
  102. extern const char *bfg_strerror(int, enum bfg_strerror_type);
  103. typedef SOCKETTYPE notifier_t[2];
  104. extern void notifier_init(notifier_t);
  105. extern void notifier_wake(notifier_t);
  106. extern void notifier_read(notifier_t);
  107. extern void notifier_destroy(notifier_t);
  108. /* Align a size_t to 4 byte boundaries for fussy arches */
  109. static inline void align_len(size_t *len)
  110. {
  111. if (*len % 4)
  112. *len += 4 - (*len % 4);
  113. }
  114. static inline
  115. void set_maxfd(int *p_maxfd, int fd)
  116. {
  117. if (fd > *p_maxfd)
  118. *p_maxfd = fd;
  119. }
  120. #define TIMEVAL_USECS(usecs) ( \
  121. (struct timeval){ \
  122. .tv_sec = (usecs) / 1000000, \
  123. .tv_usec = (usecs) % 1000000, \
  124. } \
  125. )
  126. #define timer_set_delay(tvp_timer, tvp_now, usecs) do { \
  127. struct timeval tv_add = TIMEVAL_USECS(usecs); \
  128. timeradd(&tv_add, tvp_now, tvp_timer); \
  129. } while(0)
  130. #define timer_set_delay_from_now(tvp_timer, usecs) do { \
  131. struct timeval tv_now; \
  132. gettimeofday(&tv_now, NULL); \
  133. timer_set_delay(tvp_timer, &tv_now, usecs); \
  134. } while(0)
  135. static inline
  136. bool timer_passed(struct timeval *tvp_timer, struct timeval *tvp_now)
  137. {
  138. return (tvp_timer->tv_sec != -1 && timercmp(tvp_timer, tvp_now, <));
  139. }
  140. static inline
  141. void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
  142. {
  143. if (tvp_time->tv_sec == -1)
  144. return;
  145. if (tvp_timeout->tv_sec == -1 /* no timeout */ || timercmp(tvp_time, tvp_timeout, <))
  146. *tvp_timeout = *tvp_time;
  147. }
  148. static inline
  149. struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_now)
  150. {
  151. if (tvp_timeout->tv_sec == -1)
  152. return NULL;
  153. if (timercmp(tvp_timeout, tvp_now, <))
  154. timerclear(tvp_timeout);
  155. else
  156. timersub(tvp_timeout, tvp_now, tvp_timeout);
  157. return tvp_timeout;
  158. }
  159. #endif /* __UTIL_H__ */