util.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef __UTIL_H__
  2. #define __UTIL_H__
  3. #include <curl/curl.h>
  4. #include <jansson.h>
  5. #if defined(unix) || defined(__APPLE__)
  6. #include <errno.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #define SOCKETTYPE long
  11. #define SOCKETFAIL(a) ((a) < 0)
  12. #define INVSOCK -1
  13. #define INVINETADDR -1
  14. #define CLOSESOCKET close
  15. #define SOCKERRMSG strerror(errno)
  16. #elif defined WIN32
  17. #include <ws2tcpip.h>
  18. #include <winsock2.h>
  19. #define SOCKETTYPE SOCKET
  20. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  21. #define INVSOCK INVALID_SOCKET
  22. #define INVINETADDR INADDR_NONE
  23. #define CLOSESOCKET closesocket
  24. extern char *WSAErrorMsg(void);
  25. #define SOCKERRMSG WSAErrorMsg()
  26. #ifndef SHUT_RDWR
  27. #define SHUT_RDWR SD_BOTH
  28. #endif
  29. #ifndef in_addr_t
  30. #define in_addr_t uint32_t
  31. #endif
  32. #endif
  33. #if JANSSON_MAJOR_VERSION >= 2
  34. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  35. #else
  36. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  37. #endif
  38. struct pool;
  39. enum dev_reason;
  40. struct cgpu_info;
  41. 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);
  42. extern json_t *json_rpc_call_completed(CURL *, int rc, bool probe, int *rolltime, void *out_priv);
  43. extern void gen_hash(unsigned char *data, unsigned char *hash, int len);
  44. extern void hash_data(unsigned char *out_hash, const unsigned char *data);
  45. extern void real_block_target(unsigned char *target, const unsigned char *data);
  46. extern bool hash_target_check(const unsigned char *hash, const unsigned char *target);
  47. extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
  48. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  49. bool sock_full(struct pool *pool);
  50. char *recv_line(struct pool *pool);
  51. bool parse_method(struct pool *pool, char *s);
  52. bool extract_sockaddr(struct pool *pool, char *url);
  53. bool auth_stratum(struct pool *pool);
  54. bool initiate_stratum(struct pool *pool);
  55. void suspend_stratum(struct pool *pool);
  56. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  57. void *realloc_strcat(char *ptr, char *s);
  58. void RenameThread(const char* name);
  59. extern void notifier_init(int pipefd[2]);
  60. extern void notifier_wake(int fd[2]);
  61. extern void notifier_read(int fd[2]);
  62. /* Align a size_t to 4 byte boundaries for fussy arches */
  63. static inline void align_len(size_t *len)
  64. {
  65. if (*len % 4)
  66. *len += 4 - (*len % 4);
  67. }
  68. #define timer_set_delay(tvp_timer, tvp_now, usecs) do { \
  69. struct timeval tv_add = { \
  70. .tv_sec = usecs / 1000000, \
  71. .tv_usec = usecs % 1000000, \
  72. }; \
  73. timeradd(&tv_add, tvp_now, tvp_timer); \
  74. } while(0)
  75. #define timer_set_delay_from_now(tvp_timer, usecs) do { \
  76. struct timeval tv_now; \
  77. gettimeofday(&tv_now, NULL); \
  78. timer_set_delay(tvp_timer, &tv_now, usecs); \
  79. } while(0)
  80. static inline
  81. void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
  82. {
  83. if (tvp_timeout->tv_sec == -1 /* no timeout */ || timercmp(tvp_time, tvp_timeout, <))
  84. *tvp_timeout = *tvp_time;
  85. }
  86. #endif /* __UTIL_H__ */