api-example.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2011 Kano
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <unistd.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <sys/types.h>
  18. #include "compat.h"
  19. #include "miner.h"
  20. #if defined(unix)
  21. #include <errno.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #include <netdb.h>
  26. #define SOCKETTYPE int
  27. #define SOCKETFAIL(a) ((a) < 0)
  28. #define INVSOCK -1
  29. #define CLOSESOCKET close
  30. #define SOCKETINIT {}
  31. #define SOCKERRMSG strerror(errno)
  32. #endif
  33. #ifdef WIN32
  34. #include <winsock2.h>
  35. #include "inet_ntop.h"
  36. #include "inet_pton.h"
  37. #define SOCKETTYPE SOCKET
  38. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  39. #define INVSOCK INVALID_SOCKET
  40. #define CLOSESOCKET closesocket
  41. static char WSAbuf[1024];
  42. struct WSAERRORS {
  43. int id;
  44. char *code;
  45. } WSAErrors[] = {
  46. { 0, "No error" },
  47. { WSAEINTR, "Interrupted system call" },
  48. { WSAEBADF, "Bad file number" },
  49. { WSAEACCES, "Permission denied" },
  50. { WSAEFAULT, "Bad address" },
  51. { WSAEINVAL, "Invalid argument" },
  52. { WSAEMFILE, "Too many open sockets" },
  53. { WSAEWOULDBLOCK, "Operation would block" },
  54. { WSAEINPROGRESS, "Operation now in progress" },
  55. { WSAEALREADY, "Operation already in progress" },
  56. { WSAENOTSOCK, "Socket operation on non-socket" },
  57. { WSAEDESTADDRREQ, "Destination address required" },
  58. { WSAEMSGSIZE, "Message too long" },
  59. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  60. { WSAENOPROTOOPT, "Bad protocol option" },
  61. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  62. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  63. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  64. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  65. { WSAEAFNOSUPPORT, "Address family not supported" },
  66. { WSAEADDRINUSE, "Address already in use" },
  67. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  68. { WSAENETDOWN, "Network is down" },
  69. { WSAENETUNREACH, "Network is unreachable" },
  70. { WSAENETRESET, "Net connection reset" },
  71. { WSAECONNABORTED, "Software caused connection abort" },
  72. { WSAECONNRESET, "Connection reset by peer" },
  73. { WSAENOBUFS, "No buffer space available" },
  74. { WSAEISCONN, "Socket is already connected" },
  75. { WSAENOTCONN, "Socket is not connected" },
  76. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  77. { WSAETOOMANYREFS, "Too many references, can't splice" },
  78. { WSAETIMEDOUT, "Connection timed out" },
  79. { WSAECONNREFUSED, "Connection refused" },
  80. { WSAELOOP, "Too many levels of symbolic links" },
  81. { WSAENAMETOOLONG, "File name too long" },
  82. { WSAEHOSTDOWN, "Host is down" },
  83. { WSAEHOSTUNREACH, "No route to host" },
  84. { WSAENOTEMPTY, "Directory not empty" },
  85. { WSAEPROCLIM, "Too many processes" },
  86. { WSAEUSERS, "Too many users" },
  87. { WSAEDQUOT, "Disc quota exceeded" },
  88. { WSAESTALE, "Stale NFS file handle" },
  89. { WSAEREMOTE, "Too many levels of remote in path" },
  90. { WSASYSNOTREADY, "Network system is unavailable" },
  91. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  92. { WSANOTINITIALISED, "WSAStartup not yet called" },
  93. { WSAEDISCON, "Graceful shutdown in progress" },
  94. { WSAHOST_NOT_FOUND, "Host not found" },
  95. { WSANO_DATA, "No host data of that type was found" },
  96. { -1, "Unknown error code" }
  97. };
  98. static char *WSAErrorMsg()
  99. {
  100. char *msg;
  101. int i;
  102. int id = WSAGetLastError();
  103. /* Assume none of them are actually -1 */
  104. for (i = 0; WSAErrors[i].id != -1; i++)
  105. if (WSAErrors[i].id == id)
  106. break;
  107. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  108. return &(WSAbuf[0]);
  109. }
  110. #define SOCKERRMSG WSAErrorMsg()
  111. static WSADATA WSA_Data;
  112. #define SOCKETINIT int wsa; \
  113. if (wsa = WSAStartup(0x0202, &WSA_Data)) { \
  114. printf("Socket startup failed: %d\n", wsa); \
  115. return 1; \
  116. }
  117. #ifndef SHUT_RDWR
  118. #define SHUT_RDWR SD_BOTH
  119. #endif
  120. #endif
  121. static const char SEPARATOR = '|';
  122. static const char COMMA = ',';
  123. static const char EQ = '=';
  124. void display(char *buf)
  125. {
  126. char *nextobj, *item, *nextitem, *eq;
  127. int itemcount;
  128. while (buf != NULL) {
  129. nextobj = strchr(buf, SEPARATOR);
  130. if (nextobj != NULL)
  131. *(nextobj++) = '\0';
  132. if (*buf) {
  133. item = buf;
  134. itemcount = 0;
  135. while (item != NULL) {
  136. nextitem = strchr(item, COMMA);
  137. if (nextitem != NULL)
  138. *(nextitem++) = '\0';
  139. if (*item) {
  140. eq = strchr(item, EQ);
  141. if (eq != NULL)
  142. *(eq++) = '\0';
  143. if (itemcount == 0)
  144. printf("[%s%s] =>\n(\n", item, (eq != NULL && isdigit(*eq)) ? eq : "");
  145. if (eq != NULL)
  146. printf(" [%s] => %s\n", item, eq);
  147. else
  148. printf(" [%d] => %s\n", itemcount, item);
  149. }
  150. item = nextitem;
  151. itemcount++;
  152. }
  153. if (itemcount > 0)
  154. puts(")");
  155. }
  156. buf = nextobj;
  157. }
  158. }
  159. int callapi(char *command, char *host, short int port)
  160. {
  161. char buf[BUFSIZ];
  162. struct hostent *ip;
  163. struct sockaddr_in serv;
  164. SOCKETTYPE sock;
  165. int ret = 0;
  166. int n;
  167. SOCKETINIT;
  168. ip = gethostbyname(host);
  169. sock = socket(AF_INET, SOCK_STREAM, 0);
  170. if (sock == INVSOCK) {
  171. printf("Socket initialisation failed: %s\n", SOCKERRMSG);
  172. return 1;
  173. }
  174. memset(&serv, 0, sizeof(serv));
  175. serv.sin_family = AF_INET;
  176. serv.sin_addr = *((struct in_addr *)ip->h_addr);
  177. serv.sin_port = htons(port);
  178. if (SOCKETFAIL(connect(sock, (struct sockaddr *)&serv, sizeof(struct sockaddr)))) {
  179. printf("Socket connect failed: %s\n", SOCKERRMSG);
  180. return 1;
  181. }
  182. n = send(sock, command, strlen(command)+1, 0);
  183. if (SOCKETFAIL(n)) {
  184. printf("Send failed: %s\n", SOCKERRMSG);
  185. ret = 1;
  186. }
  187. else {
  188. n = recv(sock, buf, BUFSIZ, 0);
  189. buf[n] = '\0';
  190. printf("Reply was '%s'\n", buf);
  191. display(buf);
  192. }
  193. CLOSESOCKET(sock);
  194. return ret;
  195. }
  196. static char *trim(char *str)
  197. {
  198. char *ptr;
  199. while (isspace(*str))
  200. str++;
  201. ptr = strchr(str, '\0');
  202. while (ptr-- > str) {
  203. if (isspace(*ptr))
  204. *ptr = '\0';
  205. }
  206. return str;
  207. }
  208. int main(int argc, char *argv[])
  209. {
  210. char *command = "summary";
  211. char *host = "127.0.0.1";
  212. short int port = 4028;
  213. char *ptr;
  214. if (argc > 1) {
  215. ptr = trim(argv[1]);
  216. if (strlen(ptr) > 0)
  217. command = ptr;
  218. }
  219. if (argc > 2) {
  220. ptr = trim(argv[2]);
  221. if (strlen(ptr) > 0)
  222. host = ptr;
  223. }
  224. if (argc > 3) {
  225. ptr = trim(argv[3]);
  226. if (strlen(ptr) > 0)
  227. port = atoi(ptr);
  228. }
  229. return callapi(command, host, port);
  230. }