api-example.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 3 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. #define SOCKETTYPE SOCKET
  36. #define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
  37. #define INVSOCK INVALID_SOCKET
  38. #define CLOSESOCKET closesocket
  39. static char WSAbuf[1024];
  40. struct WSAERRORS {
  41. int id;
  42. char *code;
  43. } WSAErrors[] = {
  44. { 0, "No error" },
  45. { WSAEINTR, "Interrupted system call" },
  46. { WSAEBADF, "Bad file number" },
  47. { WSAEACCES, "Permission denied" },
  48. { WSAEFAULT, "Bad address" },
  49. { WSAEINVAL, "Invalid argument" },
  50. { WSAEMFILE, "Too many open sockets" },
  51. { WSAEWOULDBLOCK, "Operation would block" },
  52. { WSAEINPROGRESS, "Operation now in progress" },
  53. { WSAEALREADY, "Operation already in progress" },
  54. { WSAENOTSOCK, "Socket operation on non-socket" },
  55. { WSAEDESTADDRREQ, "Destination address required" },
  56. { WSAEMSGSIZE, "Message too long" },
  57. { WSAEPROTOTYPE, "Protocol wrong type for socket" },
  58. { WSAENOPROTOOPT, "Bad protocol option" },
  59. { WSAEPROTONOSUPPORT, "Protocol not supported" },
  60. { WSAESOCKTNOSUPPORT, "Socket type not supported" },
  61. { WSAEOPNOTSUPP, "Operation not supported on socket" },
  62. { WSAEPFNOSUPPORT, "Protocol family not supported" },
  63. { WSAEAFNOSUPPORT, "Address family not supported" },
  64. { WSAEADDRINUSE, "Address already in use" },
  65. { WSAEADDRNOTAVAIL, "Can't assign requested address" },
  66. { WSAENETDOWN, "Network is down" },
  67. { WSAENETUNREACH, "Network is unreachable" },
  68. { WSAENETRESET, "Net connection reset" },
  69. { WSAECONNABORTED, "Software caused connection abort" },
  70. { WSAECONNRESET, "Connection reset by peer" },
  71. { WSAENOBUFS, "No buffer space available" },
  72. { WSAEISCONN, "Socket is already connected" },
  73. { WSAENOTCONN, "Socket is not connected" },
  74. { WSAESHUTDOWN, "Can't send after socket shutdown" },
  75. { WSAETOOMANYREFS, "Too many references, can't splice" },
  76. { WSAETIMEDOUT, "Connection timed out" },
  77. { WSAECONNREFUSED, "Connection refused" },
  78. { WSAELOOP, "Too many levels of symbolic links" },
  79. { WSAENAMETOOLONG, "File name too long" },
  80. { WSAEHOSTDOWN, "Host is down" },
  81. { WSAEHOSTUNREACH, "No route to host" },
  82. { WSAENOTEMPTY, "Directory not empty" },
  83. { WSAEPROCLIM, "Too many processes" },
  84. { WSAEUSERS, "Too many users" },
  85. { WSAEDQUOT, "Disc quota exceeded" },
  86. { WSAESTALE, "Stale NFS file handle" },
  87. { WSAEREMOTE, "Too many levels of remote in path" },
  88. { WSASYSNOTREADY, "Network system is unavailable" },
  89. { WSAVERNOTSUPPORTED, "Winsock version out of range" },
  90. { WSANOTINITIALISED, "WSAStartup not yet called" },
  91. { WSAEDISCON, "Graceful shutdown in progress" },
  92. { WSAHOST_NOT_FOUND, "Host not found" },
  93. { WSANO_DATA, "No host data of that type was found" },
  94. { -1, "Unknown error code" }
  95. };
  96. static char *WSAErrorMsg()
  97. {
  98. char *msg;
  99. int i;
  100. int id = WSAGetLastError();
  101. /* Assume none of them are actually -1 */
  102. for (i = 0; WSAErrors[i].id != -1; i++)
  103. if (WSAErrors[i].id == id)
  104. break;
  105. sprintf(WSAbuf, "Socket Error: (%d) %s", id, WSAErrors[i].code);
  106. return &(WSAbuf[0]);
  107. }
  108. #define SOCKERRMSG WSAErrorMsg()
  109. static WSADATA WSA_Data;
  110. #define SOCKETINIT int wsa; \
  111. if (wsa = WSAStartup(0x0202, &WSA_Data)) { \
  112. printf("Socket startup failed: %d\n", wsa); \
  113. return 1; \
  114. }
  115. #ifndef SHUT_RDWR
  116. #define SHUT_RDWR SD_BOTH
  117. #endif
  118. #endif
  119. #define RECVSIZE 65500
  120. static const char SEPARATOR = '|';
  121. static const char COMMA = ',';
  122. static const char EQ = '=';
  123. void display(char *buf)
  124. {
  125. char *nextobj, *item, *nextitem, *eq;
  126. int itemcount;
  127. while (buf != NULL) {
  128. nextobj = strchr(buf, SEPARATOR);
  129. if (nextobj != NULL)
  130. *(nextobj++) = '\0';
  131. if (*buf) {
  132. item = buf;
  133. itemcount = 0;
  134. while (item != NULL) {
  135. nextitem = strchr(item, COMMA);
  136. if (nextitem != NULL)
  137. *(nextitem++) = '\0';
  138. if (*item) {
  139. eq = strchr(item, EQ);
  140. if (eq != NULL)
  141. *(eq++) = '\0';
  142. if (itemcount == 0)
  143. printf("[%s%s] =>\n(\n", item, (eq != NULL && isdigit(*eq)) ? eq : "");
  144. if (eq != NULL)
  145. printf(" [%s] => %s\n", item, eq);
  146. else
  147. printf(" [%d] => %s\n", itemcount, item);
  148. }
  149. item = nextitem;
  150. itemcount++;
  151. }
  152. if (itemcount > 0)
  153. puts(")");
  154. }
  155. buf = nextobj;
  156. }
  157. }
  158. int callapi(char *command, char *host, short int port)
  159. {
  160. char buf[RECVSIZE+1];
  161. struct hostent *ip;
  162. struct sockaddr_in serv;
  163. SOCKETTYPE sock;
  164. int ret = 0;
  165. int n, p;
  166. SOCKETINIT;
  167. ip = gethostbyname(host);
  168. sock = socket(AF_INET, SOCK_STREAM, 0);
  169. if (sock == INVSOCK) {
  170. printf("Socket initialisation failed: %s\n", SOCKERRMSG);
  171. return 1;
  172. }
  173. memset(&serv, 0, sizeof(serv));
  174. serv.sin_family = AF_INET;
  175. serv.sin_addr = *((struct in_addr *)ip->h_addr);
  176. serv.sin_port = htons(port);
  177. if (SOCKETFAIL(connect(sock, (struct sockaddr *)&serv, sizeof(struct sockaddr)))) {
  178. printf("Socket connect failed: %s\n", SOCKERRMSG);
  179. return 1;
  180. }
  181. n = send(sock, command, strlen(command), 0);
  182. if (SOCKETFAIL(n)) {
  183. printf("Send failed: %s\n", SOCKERRMSG);
  184. ret = 1;
  185. }
  186. else {
  187. p = 0;
  188. buf[0] = '\0';
  189. while (p < RECVSIZE) {
  190. n = recv(sock, &buf[p], RECVSIZE - p , 0);
  191. if (SOCKETFAIL(n)) {
  192. printf("Recv failed: %s\n", SOCKERRMSG);
  193. ret = 1;
  194. break;
  195. }
  196. if (n == 0)
  197. break;
  198. p += n;
  199. buf[p] = '\0';
  200. }
  201. printf("Reply was '%s'\n", buf);
  202. display(buf);
  203. }
  204. CLOSESOCKET(sock);
  205. return ret;
  206. }
  207. static char *trim(char *str)
  208. {
  209. char *ptr;
  210. while (isspace(*str))
  211. str++;
  212. ptr = strchr(str, '\0');
  213. while (ptr-- > str) {
  214. if (isspace(*ptr))
  215. *ptr = '\0';
  216. }
  217. return str;
  218. }
  219. int main(int argc, char *argv[])
  220. {
  221. char *command = "summary";
  222. char *host = "127.0.0.1";
  223. short int port = 4028;
  224. char *ptr;
  225. if (argc > 1)
  226. if (strcmp(argv[1], "-?") == 0
  227. || strcmp(argv[1], "-h") == 0
  228. || strcmp(argv[1], "--help") == 0) {
  229. fprintf(stderr, "usAge: %s [command [ip/host [port]]]\n", argv[0]);
  230. return 1;
  231. }
  232. if (argc > 1) {
  233. ptr = trim(argv[1]);
  234. if (strlen(ptr) > 0)
  235. command = ptr;
  236. }
  237. if (argc > 2) {
  238. ptr = trim(argv[2]);
  239. if (strlen(ptr) > 0)
  240. host = ptr;
  241. }
  242. if (argc > 3) {
  243. ptr = trim(argv[3]);
  244. if (strlen(ptr) > 0)
  245. port = atoi(ptr);
  246. }
  247. return callapi(command, host, port);
  248. }