util.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 int
  11. #define SOCKETFAIL(a) ((a) < 0)
  12. #define INVSOCK -1
  13. #define INVINETADDR -1
  14. #define CLOSESOCKET close
  15. #define SOCKERRMSG strerror(errno)
  16. static inline bool sock_blocks(void)
  17. {
  18. return (errno == EAGAIN || errno == EWOULDBLOCK);
  19. }
  20. #elif defined WIN32
  21. #include <ws2tcpip.h>
  22. #include <winsock2.h>
  23. #define SOCKETTYPE SOCKET
  24. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  25. #define INVSOCK INVALID_SOCKET
  26. #define INVINETADDR INADDR_NONE
  27. #define CLOSESOCKET closesocket
  28. extern char *WSAErrorMsg(void);
  29. #define SOCKERRMSG WSAErrorMsg()
  30. static inline bool sock_blocks(void)
  31. {
  32. return (WSAGetLastError() == WSAEWOULDBLOCK);
  33. }
  34. #ifndef SHUT_RDWR
  35. #define SHUT_RDWR SD_BOTH
  36. #endif
  37. #ifndef in_addr_t
  38. #define in_addr_t uint32_t
  39. #endif
  40. #endif
  41. #if JANSSON_MAJOR_VERSION >= 2
  42. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  43. #else
  44. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  45. #endif
  46. struct pool;
  47. enum dev_reason;
  48. struct cgpu_info;
  49. 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);
  50. extern json_t *json_rpc_call_completed(CURL *, int rc, bool probe, int *rolltime, void *out_priv);
  51. extern void gen_hash(unsigned char *data, unsigned char *hash, int len);
  52. extern void hash_data(unsigned char *out_hash, const unsigned char *data);
  53. extern void real_block_target(unsigned char *target, const unsigned char *data);
  54. extern bool hash_target_check(const unsigned char *hash, const unsigned char *target);
  55. extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
  56. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  57. bool sock_full(struct pool *pool);
  58. char *recv_line(struct pool *pool);
  59. bool parse_method(struct pool *pool, char *s);
  60. bool extract_sockaddr(struct pool *pool, char *url);
  61. bool auth_stratum(struct pool *pool);
  62. bool initiate_stratum(struct pool *pool);
  63. void suspend_stratum(struct pool *pool);
  64. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  65. void *realloc_strcat(char *ptr, char *s);
  66. void RenameThread(const char* name);
  67. typedef SOCKETTYPE notifier_t[2];
  68. extern void notifier_init(notifier_t);
  69. extern void notifier_wake(notifier_t);
  70. extern void notifier_read(notifier_t);
  71. /* Align a size_t to 4 byte boundaries for fussy arches */
  72. static inline void align_len(size_t *len)
  73. {
  74. if (*len % 4)
  75. *len += 4 - (*len % 4);
  76. }
  77. #endif /* __UTIL_H__ */