util.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #elif defined WIN32
  18. #include <ws2tcpip.h>
  19. #include <winsock2.h>
  20. #define SOCKETTYPE SOCKET
  21. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  22. #define INVSOCK INVALID_SOCKET
  23. #define INVINETADDR INADDR_NONE
  24. #define CLOSESOCKET closesocket
  25. extern char *WSAErrorMsg(void);
  26. #define SOCKERRMSG WSAErrorMsg()
  27. #ifndef SHUT_RDWR
  28. #define SHUT_RDWR SD_BOTH
  29. #endif
  30. #ifndef in_addr_t
  31. #define in_addr_t uint32_t
  32. #endif
  33. #endif
  34. #if JANSSON_MAJOR_VERSION >= 2
  35. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  36. #else
  37. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  38. #endif
  39. struct pool;
  40. enum dev_reason;
  41. struct cgpu_info;
  42. 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);
  43. extern json_t *json_rpc_call_completed(CURL *, int rc, bool probe, int *rolltime, void *out_priv);
  44. extern void gen_hash(unsigned char *data, unsigned char *hash, int len);
  45. extern void hash_data(unsigned char *out_hash, const unsigned char *data);
  46. extern void real_block_target(unsigned char *target, const unsigned char *data);
  47. extern bool hash_target_check(const unsigned char *hash, const unsigned char *target);
  48. extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
  49. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  50. bool sock_full(struct pool *pool);
  51. char *recv_line(struct pool *pool);
  52. bool parse_method(struct pool *pool, char *s);
  53. bool extract_sockaddr(struct pool *pool, char *url);
  54. bool auth_stratum(struct pool *pool);
  55. bool initiate_stratum(struct pool *pool);
  56. void suspend_stratum(struct pool *pool);
  57. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  58. void *realloc_strcat(char *ptr, char *s);
  59. void RenameThread(const char* name);
  60. typedef SOCKETTYPE notifier_t[2];
  61. extern void notifier_init(notifier_t);
  62. extern void notifier_wake(notifier_t);
  63. extern void notifier_read(notifier_t);
  64. extern void notifier_destroy(notifier_t);
  65. /* Align a size_t to 4 byte boundaries for fussy arches */
  66. static inline void align_len(size_t *len)
  67. {
  68. if (*len % 4)
  69. *len += 4 - (*len % 4);
  70. }
  71. static inline
  72. void set_maxfd(int *p_maxfd, int fd)
  73. {
  74. if (fd > *p_maxfd)
  75. *p_maxfd = fd;
  76. }
  77. #define timer_set_delay(tvp_timer, tvp_now, usecs) do { \
  78. struct timeval tv_add = { \
  79. .tv_sec = usecs / 1000000, \
  80. .tv_usec = usecs % 1000000, \
  81. }; \
  82. timeradd(&tv_add, tvp_now, tvp_timer); \
  83. } while(0)
  84. #define timer_set_delay_from_now(tvp_timer, usecs) do { \
  85. struct timeval tv_now; \
  86. gettimeofday(&tv_now, NULL); \
  87. timer_set_delay(tvp_timer, &tv_now, usecs); \
  88. } while(0)
  89. static inline
  90. bool timer_passed(struct timeval *tvp_timer, struct timeval *tvp_now)
  91. {
  92. return (tvp_timer->tv_sec != -1 && timercmp(tvp_timer, tvp_now, <));
  93. }
  94. static inline
  95. void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
  96. {
  97. if (tvp_time->tv_sec == -1)
  98. return;
  99. if (tvp_timeout->tv_sec == -1 /* no timeout */ || timercmp(tvp_time, tvp_timeout, <))
  100. *tvp_timeout = *tvp_time;
  101. }
  102. static inline
  103. struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_now)
  104. {
  105. if (tvp_timeout->tv_sec == -1)
  106. return NULL;
  107. if (timercmp(tvp_timeout, tvp_now, <))
  108. timerclear(tvp_timeout);
  109. else
  110. timersub(tvp_timeout, tvp_now, tvp_timeout);
  111. return tvp_timeout;
  112. }
  113. #endif /* __UTIL_H__ */