api-example.c 6.9 KB

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