util.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2012-2013 Con Kolivas
  4. * Copyright 2011 Andrew Smith
  5. * Copyright 2011 Jeff Garzik
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #ifndef __UTIL_H__
  13. #define __UTIL_H__
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <sys/time.h>
  17. #include <curl/curl.h>
  18. #include <jansson.h>
  19. #include "compat.h"
  20. #if defined(unix) || defined(__APPLE__)
  21. #include <errno.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #define SOCKETTYPE int
  26. #define SOCKETFAIL(a) ((a) < 0)
  27. #define INVSOCK -1
  28. #define INVINETADDR -1
  29. #define CLOSESOCKET close
  30. #define SOCKERRMSG strerror(errno)
  31. static inline bool sock_blocks(void)
  32. {
  33. return (errno == EAGAIN || errno == EWOULDBLOCK);
  34. }
  35. #elif defined WIN32
  36. #include <ws2tcpip.h>
  37. #include <winsock2.h>
  38. #define SOCKETTYPE SOCKET
  39. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  40. #define INVSOCK INVALID_SOCKET
  41. #define INVINETADDR INADDR_NONE
  42. #define CLOSESOCKET closesocket
  43. extern char *WSAErrorMsg(void);
  44. #define SOCKERRMSG WSAErrorMsg()
  45. static inline bool sock_blocks(void)
  46. {
  47. return (WSAGetLastError() == WSAEWOULDBLOCK);
  48. }
  49. #ifndef SHUT_RDWR
  50. #define SHUT_RDWR SD_BOTH
  51. #endif
  52. #ifndef in_addr_t
  53. #define in_addr_t uint32_t
  54. #endif
  55. #endif
  56. #define IGNORE_RETURN_VALUE(expr) {if(expr);}(void)0
  57. #if JANSSON_MAJOR_VERSION >= 2
  58. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  59. #else
  60. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  61. #endif
  62. extern char *json_dumps_ANY(json_t *, size_t flags);
  63. struct pool;
  64. enum dev_reason;
  65. struct cgpu_info;
  66. 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);
  67. extern json_t *json_rpc_call_completed(CURL *, int rc, bool probe, int *rolltime, void *out_priv);
  68. extern char *absolute_uri(char *uri, const char *ref); // ref must be a root URI
  69. extern void gen_hash(unsigned char *data, unsigned char *hash, int len);
  70. extern void hash_data(unsigned char *out_hash, const unsigned char *data);
  71. extern void real_block_target(unsigned char *target, const unsigned char *data);
  72. extern bool hash_target_check(const unsigned char *hash, const unsigned char *target);
  73. extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
  74. bool _stratum_send(struct pool *pool, char *s, ssize_t len, bool force);
  75. #define stratum_send(pool, s, len) _stratum_send(pool, s, len, false)
  76. bool sock_full(struct pool *pool);
  77. char *recv_line(struct pool *pool);
  78. bool parse_method(struct pool *pool, char *s);
  79. bool extract_sockaddr(struct pool *pool, char *url);
  80. bool auth_stratum(struct pool *pool);
  81. bool initiate_stratum(struct pool *pool);
  82. bool restart_stratum(struct pool *pool);
  83. void suspend_stratum(struct pool *pool);
  84. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  85. void *realloc_strcat(char *ptr, char *s);
  86. extern char *sanestr(char *o, char *s);
  87. void RenameThread(const char* name);
  88. typedef SOCKETTYPE notifier_t[2];
  89. extern void notifier_init(notifier_t);
  90. extern void notifier_wake(notifier_t);
  91. extern void notifier_read(notifier_t);
  92. extern void notifier_destroy(notifier_t);
  93. /* Align a size_t to 4 byte boundaries for fussy arches */
  94. static inline void align_len(size_t *len)
  95. {
  96. if (*len % 4)
  97. *len += 4 - (*len % 4);
  98. }
  99. static inline
  100. void set_maxfd(int *p_maxfd, int fd)
  101. {
  102. if (fd > *p_maxfd)
  103. *p_maxfd = fd;
  104. }
  105. #define TIMEVAL_USECS(usecs) ( \
  106. (struct timeval){ \
  107. .tv_sec = (usecs) / 1000000, \
  108. .tv_usec = (usecs) % 1000000, \
  109. } \
  110. )
  111. #define timer_set_delay(tvp_timer, tvp_now, usecs) do { \
  112. struct timeval tv_add = TIMEVAL_USECS(usecs); \
  113. timeradd(&tv_add, tvp_now, tvp_timer); \
  114. } while(0)
  115. #define timer_set_delay_from_now(tvp_timer, usecs) do { \
  116. struct timeval tv_now; \
  117. gettimeofday(&tv_now, NULL); \
  118. timer_set_delay(tvp_timer, &tv_now, usecs); \
  119. } while(0)
  120. static inline
  121. bool timer_passed(struct timeval *tvp_timer, struct timeval *tvp_now)
  122. {
  123. return (tvp_timer->tv_sec != -1 && timercmp(tvp_timer, tvp_now, <));
  124. }
  125. static inline
  126. void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
  127. {
  128. if (tvp_time->tv_sec == -1)
  129. return;
  130. if (tvp_timeout->tv_sec == -1 /* no timeout */ || timercmp(tvp_time, tvp_timeout, <))
  131. *tvp_timeout = *tvp_time;
  132. }
  133. static inline
  134. struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_now)
  135. {
  136. if (tvp_timeout->tv_sec == -1)
  137. return NULL;
  138. if (timercmp(tvp_timeout, tvp_now, <))
  139. timerclear(tvp_timeout);
  140. else
  141. timersub(tvp_timeout, tvp_now, tvp_timeout);
  142. return tvp_timeout;
  143. }
  144. #endif /* __UTIL_H__ */