util.h 4.8 KB

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