Cleanup/remove included inet functions)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
diff --git a/api-example.c b/api-example.c
index 9cb0416..4c76135 100755
--- a/api-example.c
+++ b/api-example.c
@@ -40,8 +40,6 @@
#ifdef WIN32
#include <winsock2.h>
- #include "inet_ntop.h"
- #include "inet_pton.h"
#define SOCKETTYPE SOCKET
#define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
@@ -138,6 +136,8 @@
#endif
#endif
+#define RECVSIZE 65500
+
static const char SEPARATOR = '|';
static const char COMMA = ',';
static const char EQ = '=';
@@ -187,12 +187,12 @@ void display(char *buf)
int callapi(char *command, char *host, short int port)
{
- char buf[BUFSIZ];
+ char buf[RECVSIZE+1];
struct hostent *ip;
struct sockaddr_in serv;
SOCKETTYPE sock;
int ret = 0;
- int n;
+ int n, p;
SOCKETINIT;
@@ -220,8 +220,23 @@ int callapi(char *command, char *host, short int port)
ret = 1;
}
else {
- n = recv(sock, buf, BUFSIZ, 0);
- buf[n] = '\0';
+ p = 0;
+ buf[0] = '\0';
+ while (p < RECVSIZE) {
+ n = recv(sock, &buf[p], RECVSIZE - p , 0);
+
+ if (SOCKETFAIL(n)) {
+ printf("Recv failed: %s\n", SOCKERRMSG);
+ ret = 1;
+ break;
+ }
+
+ if (n == 0)
+ break;
+
+ p += n;
+ buf[p] = '\0';
+ }
printf("Reply was '%s'\n", buf);
@@ -256,6 +271,14 @@ int main(int argc, char *argv[])
short int port = 4028;
char *ptr;
+ if (argc > 1)
+ if (strcmp(argv[1], "-?") == 0
+ || strcmp(argv[1], "-h") == 0
+ || strcmp(argv[1], "--help") == 0) {
+ fprintf(stderr, "usAge: %s [command [ip/host [port]]]\n", argv[0]);
+ return 1;
+ }
+
if (argc > 1) {
ptr = trim(argv[1]);
if (strlen(ptr) > 0)
diff --git a/api.c b/api.c
index bf9c41c..b7c7b78 100644
--- a/api.c
+++ b/api.c
@@ -30,6 +30,7 @@
#define SOCKETTYPE int
#define SOCKETFAIL(a) ((a) < 0)
#define INVSOCK -1
+ #define INVINETADDR -1
#define CLOSESOCKET close
#define SOCKERRMSG strerror(errno)
@@ -37,12 +38,11 @@
#ifdef WIN32
#include <winsock2.h>
- #include "inet_ntop.h"
- #include "inet_pton.h"
#define SOCKETTYPE SOCKET
#define SOCKETFAIL(a) ((a) == SOCKET_ERROR)
#define INVSOCK INVALID_SOCKET
+ #define INVINETADDR INADDR_NONE
#define CLOSESOCKET closesocket
static char WSAbuf[1024];
@@ -706,7 +706,7 @@ void api(void)
const char *localaddr = "127.0.0.1";
SOCKETTYPE c;
int n, bound;
- char connectaddr[32];
+ char *connectaddr;
char *binderror;
time_t bindstart;
short int port = opt_api_port;
@@ -718,14 +718,14 @@ void api(void)
bool did;
int i;
+ /* This should be done first to ensure curl has already called WSAStartup() in windows */
+ sleep(opt_log_interval);
+
if (!opt_api_listen) {
applog(LOG_WARNING, "API not running%s", UNAVAILABLE);
return;
}
- /* This should be done first to ensure curl has already called WSAStartup() in windows */
- sleep(opt_log_interval);
-
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVSOCK) {
applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
@@ -737,7 +737,8 @@ void api(void)
serv.sin_family = AF_INET;
if (!opt_api_network) {
- if (inet_pton(AF_INET, localaddr, &(serv.sin_addr)) == 0) {
+ serv.sin_addr.s_addr = inet_addr(localaddr);
+ if (serv.sin_addr.s_addr == INVINETADDR) {
applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
return;
}
@@ -791,12 +792,12 @@ void api(void)
if (opt_api_network)
addrok = true;
else {
- inet_ntop(AF_INET, &(cli.sin_addr), &(connectaddr[0]), sizeof(connectaddr)-1);
+ connectaddr = inet_ntoa(cli.sin_addr);
addrok = (strcmp(connectaddr, localaddr) == 0);
}
if (opt_debug) {
- inet_ntop(AF_INET, &(cli.sin_addr), &(connectaddr[0]), sizeof(connectaddr)-1);
+ connectaddr = inet_ntoa(cli.sin_addr);
applog(LOG_DEBUG, "DBG: connection from %s - %s", connectaddr, addrok ? "Accepted" : "Ignored");
}