util.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef __UTIL_H__
  2. #define __UTIL_H__
  3. #if defined(unix) || defined(__APPLE__)
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #define SOCKETTYPE int
  9. #define SOCKETFAIL(a) ((a) < 0)
  10. #define INVSOCK -1
  11. #define INVINETADDR -1
  12. #define CLOSESOCKET close
  13. #define SOCKERRMSG strerror(errno)
  14. #elif defined WIN32
  15. #include <ws2tcpip.h>
  16. #include <winsock2.h>
  17. #define SOCKETTYPE SOCKET
  18. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  19. #define INVSOCK INVALID_SOCKET
  20. #define INVINETADDR INADDR_NONE
  21. #define CLOSESOCKET closesocket
  22. static char WSAbuf[1024];
  23. struct WSAERRORS {
  24. int id;
  25. char *code;
  26. } WSAErrors[] = {
  27. { 0, "No error" },
  28. { WSAEINTR, "Interrupted system call" },
  29. { WSAEBADF, "Bad file number" },
  30. { WSAEACCES, "Permission denied" },
  31. { WSAEFAULT, "Bad address" },
  32. { WSAEINVAL, "Invalid argument" },
  33. { WSAEMFILE, "Too many open sockets" },
  34. { WSAEWOULDBLOCK, "Operation would block" },
  35. { WSAEINPROGRESS, "Operation now in progress" },
  36. { WSAEALREADY, "Operation already in progress" },
  37. { WSAENOTSOCK, "Socket operation on non-socket" },
  38. { WSAEDESTADDRREQ, "Destination address required" },
  39. { WSAEMSGSIZE, "Message too long" },
  40. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  41. { WSAENOPROTOOPT, "Bad protocol option" },
  42. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  43. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  44. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  45. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  46. { WSAEAFNOSUPPORT, "Address family not supported" },
  47. { WSAEADDRINUSE, "Address already in use" },
  48. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  49. { WSAENETDOWN, "Network is down" },
  50. { WSAENETUNREACH, "Network is unreachable" },
  51. { WSAENETRESET, "Net connection reset" },
  52. { WSAECONNABORTED, "Software caused connection abort" },
  53. { WSAECONNRESET, "Connection reset by peer" },
  54. { WSAENOBUFS, "No buffer space available" },
  55. { WSAEISCONN, "Socket is already connected" },
  56. { WSAENOTCONN, "Socket is not connected" },
  57. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  58. { WSAETOOMANYREFS, "Too many references, can't splice" },
  59. { WSAETIMEDOUT, "Connection timed out" },
  60. { WSAECONNREFUSED, "Connection refused" },
  61. { WSAELOOP, "Too many levels of symbolic links" },
  62. { WSAENAMETOOLONG, "File name too long" },
  63. { WSAEHOSTDOWN, "Host is down" },
  64. { WSAEHOSTUNREACH, "No route to host" },
  65. { WSAENOTEMPTY, "Directory not empty" },
  66. { WSAEPROCLIM, "Too many processes" },
  67. { WSAEUSERS, "Too many users" },
  68. { WSAEDQUOT, "Disc quota exceeded" },
  69. { WSAESTALE, "Stale NFS file handle" },
  70. { WSAEREMOTE, "Too many levels of remote in path" },
  71. { WSASYSNOTREADY, "Network system is unavailable" },
  72. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  73. { WSANOTINITIALISED, "WSAStartup not yet called" },
  74. { WSAEDISCON, "Graceful shutdown in progress" },
  75. { WSAHOST_NOT_FOUND, "Host not found" },
  76. { WSANO_DATA, "No host data of that type was found" },
  77. { -1, "Unknown error code" }
  78. };
  79. static char *WSAErrorMsg()
  80. {
  81. int i;
  82. int id = WSAGetLastError();
  83. /* Assume none of them are actually -1 */
  84. for (i = 0; WSAErrors[i].id != -1; i++)
  85. if (WSAErrors[i].id == id)
  86. break;
  87. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  88. return &(WSAbuf[0]);
  89. }
  90. #define SOCKERRMSG WSAErrorMsg()
  91. #ifndef SHUT_RDWR
  92. #define SHUT_RDWR SD_BOTH
  93. #endif
  94. #ifndef in_addr_t
  95. #define in_addr_t uint32_t
  96. #endif
  97. #endif
  98. #if JANSSON_MAJOR_VERSION >= 2
  99. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  100. #else
  101. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  102. #endif
  103. struct pool;
  104. bool sock_send(int sock, char *s, ssize_t len);
  105. char *recv_line(SOCKETTYPE sock);
  106. bool parse_method(struct pool *pool, char *s);
  107. bool extract_sockaddr(struct pool *pool, char *url);
  108. bool auth_stratum(struct pool *pool);
  109. bool initiate_stratum(struct pool *pool);
  110. #endif /* __UTIL_H__ */