util.h 3.3 KB

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