util.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. extern char *json_dumps_ANY(json_t *, size_t flags);
  47. struct pool;
  48. enum dev_reason;
  49. struct cgpu_info;
  50. 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);
  51. extern json_t *json_rpc_call_completed(CURL *, int rc, bool probe, int *rolltime, void *out_priv);
  52. extern void gen_hash(unsigned char *data, unsigned char *hash, int len);
  53. extern void hash_data(unsigned char *out_hash, const unsigned char *data);
  54. extern void real_block_target(unsigned char *target, const unsigned char *data);
  55. extern bool hash_target_check(const unsigned char *hash, const unsigned char *target);
  56. extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
  57. bool stratum_send(struct pool *pool, char *s, ssize_t len);
  58. bool sock_full(struct pool *pool);
  59. char *recv_line(struct pool *pool);
  60. bool parse_method(struct pool *pool, char *s);
  61. bool extract_sockaddr(struct pool *pool, char *url);
  62. bool auth_stratum(struct pool *pool);
  63. bool initiate_stratum(struct pool *pool);
  64. void suspend_stratum(struct pool *pool);
  65. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  66. void *realloc_strcat(char *ptr, char *s);
  67. extern char *sanestr(char *o, char *s);
  68. void RenameThread(const char* name);
  69. typedef SOCKETTYPE notifier_t[2];
  70. extern void notifier_init(notifier_t);
  71. extern void notifier_wake(notifier_t);
  72. extern void notifier_read(notifier_t);
  73. /* Align a size_t to 4 byte boundaries for fussy arches */
  74. static inline void align_len(size_t *len)
  75. {
  76. if (*len % 4)
  77. *len += 4 - (*len % 4);
  78. }
  79. #endif /* __UTIL_H__ */