tests/server: comply with our code style

This commit is contained in:
Daniel Stenberg 2016-04-03 22:35:43 +02:00
parent a71012c03e
commit a332c4f769
9 changed files with 240 additions and 210 deletions

View File

@ -76,7 +76,7 @@ static char *printable(char *inbuf, size_t inlength)
return NULL; return NULL;
if(!inlength) { if(!inlength) {
sprintf(&outbuf[0], "%s", NOTHING_STR); snprintf(&outbuf[0], outsize, "%s", NOTHING_STR);
return outbuf; return outbuf;
} }
@ -98,7 +98,7 @@ static char *printable(char *inbuf, size_t inlength)
o++; o++;
} }
else { else {
sprintf(&outbuf[o], HEX_FMT_STR, inbuf[i]); snprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
o += HEX_STR_LEN; o += HEX_STR_LEN;
} }
@ -163,7 +163,7 @@ int main(int argc, char *argv[])
(use_cached_creds) ? "yes" : "no"); (use_cached_creds) ? "yes" : "no");
env = getenv("CURL_NTLM_AUTH_TESTNUM"); env = getenv("CURL_NTLM_AUTH_TESTNUM");
if (env) { if(env) {
char *endptr; char *endptr;
long lnum = strtol(env, &endptr, 10); long lnum = strtol(env, &endptr, 10);
if((endptr != env + strlen(env)) || (lnum < 1L)) { if((endptr != env + strlen(env)) || (lnum < 1L)) {
@ -171,13 +171,14 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
testnum = lnum; testnum = lnum;
} else { }
else {
logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM"); logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
exit(1); exit(1);
} }
env = getenv("CURL_NTLM_AUTH_SRCDIR"); env = getenv("CURL_NTLM_AUTH_SRCDIR");
if (env) { if(env) {
path = env; path = env;
} }
@ -230,7 +231,8 @@ int main(int argc, char *argv[])
} }
else { else {
size = 0; size = 0;
error = getpart(&type1_output, &size, "ntlm_auth_type1", "output", stream); error = getpart(&type1_output, &size, "ntlm_auth_type1", "output",
stream);
fclose(stream); fclose(stream);
if(error || size == 0) { if(error || size == 0) {
logmsg("getpart() type 1 output failed with error: %d", error); logmsg("getpart() type 1 output failed with error: %d", error);
@ -251,7 +253,8 @@ int main(int argc, char *argv[])
} }
else { else {
size = 0; size = 0;
error = getpart(&type3_output, &size, "ntlm_auth_type3", "output", stream); error = getpart(&type3_output, &size, "ntlm_auth_type3", "output",
stream);
fclose(stream); fclose(stream);
if(error || size == 0) { if(error || size == 0) {
logmsg("getpart() type 3 output failed with error: %d", error); logmsg("getpart() type 3 output failed with error: %d", error);

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -101,7 +101,7 @@ static int readline(char **buffer, size_t *bufsize, FILE *stream)
int bytestoread = curlx_uztosi(*bufsize - offset); int bytestoread = curlx_uztosi(*bufsize - offset);
if(!fgets(*buffer + offset, bytestoread, stream)) if(!fgets(*buffer + offset, bytestoread, stream))
return (offset != 0) ? GPE_OK : GPE_END_OF_FILE ; return (offset != 0) ? GPE_OK : GPE_END_OF_FILE;
length = offset + strlen(*buffer + offset); length = offset + strlen(*buffer + offset);
if(*(*buffer + length - 1) == '\n') if(*(*buffer + length - 1) == '\n')

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -126,7 +126,7 @@ int main(int argc, char *argv[])
sclose(s); sclose(s);
} }
if (rc == 0) { if(rc == 0) {
/* getaddrinfo() resolve */ /* getaddrinfo() resolve */
struct addrinfo *ai; struct addrinfo *ai;
struct addrinfo hints; struct addrinfo hints;
@ -138,7 +138,7 @@ int main(int argc, char *argv[])
/* Use parenthesis around functions to stop them from being replaced by /* Use parenthesis around functions to stop them from being replaced by
the macro in memdebug.h */ the macro in memdebug.h */
rc = (getaddrinfo)(host, "80", &hints, &ai); rc = (getaddrinfo)(host, "80", &hints, &ai);
if (rc == 0) if(rc == 0)
(freeaddrinfo)(ai); (freeaddrinfo)(ai);
} }

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -353,15 +353,15 @@ static int ProcessRequest(struct httprequest *req)
char *ptr; char *ptr;
if(!strcmp(prot_str, "HTTP")) { if(!strcmp(prot_str, "HTTP")) {
req->protocol = RPROT_HTTP; req->protocol = RPROT_HTTP;
} }
else if(!strcmp(prot_str, "RTSP")) { else if(!strcmp(prot_str, "RTSP")) {
req->protocol = RPROT_RTSP; req->protocol = RPROT_RTSP;
} }
else { else {
req->protocol = RPROT_NONE; req->protocol = RPROT_NONE;
logmsg("got unknown protocol %s", prot_str); logmsg("got unknown protocol %s", prot_str);
return 1; return 1;
} }
req->prot_version = prot_major*10 + prot_minor; req->prot_version = prot_major*10 + prot_minor;
@ -375,10 +375,10 @@ static int ProcessRequest(struct httprequest *req)
char *filename; char *filename;
if((strlen(doc) + strlen(request)) < 200) if((strlen(doc) + strlen(request)) < 200)
sprintf(logbuf, "Got request: %s %s %s/%d.%d", snprintf(logbuf, sizeof(logbuf), "Got request: %s %s %s/%d.%d",
request, doc, prot_str, prot_major, prot_minor); request, doc, prot_str, prot_major, prot_minor);
else else
sprintf(logbuf, "Got a *HUGE* request %s/%d.%d", snprintf(logbuf, sizeof(logbuf), "Got a *HUGE* request %s/%d.%d",
prot_str, prot_major, prot_minor); prot_str, prot_major, prot_minor);
logmsg("%s", logbuf); logmsg("%s", logbuf);
@ -409,8 +409,8 @@ static int ProcessRequest(struct httprequest *req)
else else
req->partno = 0; req->partno = 0;
sprintf(logbuf, "Requested test number %ld part %ld", snprintf(logbuf, sizeof(logbuf), "Requested test number %ld part %ld",
req->testno, req->partno); req->testno, req->partno);
logmsg("%s", logbuf); logmsg("%s", logbuf);
filename = test2file(req->testno); filename = test2file(req->testno);
@ -488,7 +488,7 @@ static int ProcessRequest(struct httprequest *req)
rtp_scratch[0] = '$'; rtp_scratch[0] = '$';
/* The channel follows and is one byte */ /* The channel follows and is one byte */
SET_RTP_PKT_CHN(rtp_scratch ,rtp_channel); SET_RTP_PKT_CHN(rtp_scratch, rtp_channel);
/* Length follows and is a two byte short in network order */ /* Length follows and is a two byte short in network order */
SET_RTP_PKT_LEN(rtp_scratch, rtp_size); SET_RTP_PKT_LEN(rtp_scratch, rtp_size);
@ -501,14 +501,18 @@ static int ProcessRequest(struct httprequest *req)
if(req->rtp_buffer == NULL) { if(req->rtp_buffer == NULL) {
req->rtp_buffer = rtp_scratch; req->rtp_buffer = rtp_scratch;
req->rtp_buffersize = rtp_size + 4; req->rtp_buffersize = rtp_size + 4;
} else { }
req->rtp_buffer = realloc(req->rtp_buffer, req->rtp_buffersize + rtp_size + 4); else {
memcpy(req->rtp_buffer + req->rtp_buffersize, rtp_scratch, rtp_size + 4); req->rtp_buffer = realloc(req->rtp_buffer,
req->rtp_buffersize +
rtp_size + 4);
memcpy(req->rtp_buffer + req->rtp_buffersize, rtp_scratch,
rtp_size + 4);
req->rtp_buffersize += rtp_size + 4; req->rtp_buffersize += rtp_size + 4;
free(rtp_scratch); free(rtp_scratch);
} }
logmsg("rtp_buffersize is %zu, rtp_size is %d.", req->rtp_buffersize, rtp_size); logmsg("rtp_buffersize is %zu, rtp_size is %d.",
req->rtp_buffersize, rtp_size);
} }
} }
else { else {
@ -529,8 +533,9 @@ static int ProcessRequest(struct httprequest *req)
else { else {
if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d", if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
doc, &prot_major, &prot_minor) == 3) { doc, &prot_major, &prot_minor) == 3) {
sprintf(logbuf, "Received a CONNECT %s HTTP/%d.%d request", snprintf(logbuf, sizeof(logbuf),
doc, prot_major, prot_minor); "Received a CONNECT %s HTTP/%d.%d request",
doc, prot_major, prot_minor);
logmsg("%s", logbuf); logmsg("%s", logbuf);
if(req->prot_version == 10) if(req->prot_version == 10)
@ -662,10 +667,11 @@ static int ProcessRequest(struct httprequest *req)
req->ntlm = TRUE; /* NTLM found */ req->ntlm = TRUE; /* NTLM found */
logmsg("Received NTLM type-1, sending back data %ld", req->partno); logmsg("Received NTLM type-1, sending back data %ld", req->partno);
} }
else if((req->partno >= 1000) && strstr(req->reqbuf, "Authorization: Basic")) { else if((req->partno >= 1000) &&
/* If the client is passing this Basic-header and the part number is already strstr(req->reqbuf, "Authorization: Basic")) {
>=1000, we add 1 to the part number. This allows simple Basic authentication /* If the client is passing this Basic-header and the part number is
negotiation to work in the test suite. */ already >=1000, we add 1 to the part number. This allows simple Basic
authentication negotiation to work in the test suite. */
req->partno += 1; req->partno += 1;
logmsg("Received Basic request, sending back data %ld", req->partno); logmsg("Received Basic request, sending back data %ld", req->partno);
} }
@ -723,15 +729,15 @@ static void storerequest(char *reqbuf, size_t totalsize)
size_t writeleft; size_t writeleft;
FILE *dump; FILE *dump;
if (reqbuf == NULL) if(reqbuf == NULL)
return; return;
if (totalsize == 0) if(totalsize == 0)
return; return;
do { do {
dump = fopen(REQUEST_DUMP, "ab"); dump = fopen(REQUEST_DUMP, "ab");
} while ((dump == NULL) && ((error = errno) == EINTR)); } while((dump == NULL) && ((error = errno) == EINTR));
if (dump == NULL) { if(dump == NULL) {
logmsg("Error opening file %s error: %d %s", logmsg("Error opening file %s error: %d %s",
REQUEST_DUMP, error, strerror(error)); REQUEST_DUMP, error, strerror(error));
logmsg("Failed to write request input to " REQUEST_DUMP); logmsg("Failed to write request input to " REQUEST_DUMP);
@ -746,7 +752,7 @@ static void storerequest(char *reqbuf, size_t totalsize)
goto storerequest_cleanup; goto storerequest_cleanup;
if(written > 0) if(written > 0)
writeleft -= written; writeleft -= written;
} while ((writeleft > 0) && ((error = errno) == EINTR)); } while((writeleft > 0) && ((error = errno) == EINTR));
if(writeleft == 0) if(writeleft == 0)
logmsg("Wrote request (%zu bytes) input to " REQUEST_DUMP, totalsize); logmsg("Wrote request (%zu bytes) input to " REQUEST_DUMP, totalsize);
@ -815,9 +821,9 @@ static int get_request(curl_socket_t sock, struct httprequest *req)
} }
else { else {
if(req->skip) if(req->skip)
/* we are instructed to not read the entire thing, so we make sure to only /* we are instructed to not read the entire thing, so we make sure to
read what we're supposed to and NOT read the enire thing the client only read what we're supposed to and NOT read the enire thing the
wants to send! */ client wants to send! */
got = sread(sock, reqbuf + req->offset, req->cl); got = sread(sock, reqbuf + req->offset, req->cl);
else else
got = sread(sock, reqbuf + req->offset, REQBUFSIZ-1 - req->offset); got = sread(sock, reqbuf + req->offset, REQBUFSIZ-1 - req->offset);
@ -908,7 +914,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
case RCMD_STREAM: case RCMD_STREAM:
#define STREAMTHIS "a string to stream 01234567890\n" #define STREAMTHIS "a string to stream 01234567890\n"
count = strlen(STREAMTHIS); count = strlen(STREAMTHIS);
for (;;) { for(;;) {
written = swrite(sock, STREAMTHIS, count); written = swrite(sock, STREAMTHIS, count);
if(got_exit_signal) if(got_exit_signal)
return -1; return -1;
@ -937,10 +943,12 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
case DOCNUMBER_WERULEZ: case DOCNUMBER_WERULEZ:
/* we got a "friends?" question, reply back that we sure are */ /* we got a "friends?" question, reply back that we sure are */
logmsg("Identifying ourselves as friends"); logmsg("Identifying ourselves as friends");
sprintf(msgbuf, "RTSP_SERVER WE ROOLZ: %ld\r\n", (long)getpid()); snprintf(msgbuf, sizeof(msgbuf), "RTSP_SERVER WE ROOLZ: %ld\r\n",
(long)getpid());
msglen = strlen(msgbuf); msglen = strlen(msgbuf);
sprintf(weare, "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s", snprintf(weare, sizeof(weare),
msglen, msgbuf); "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
msglen, msgbuf);
buffer = weare; buffer = weare;
break; break;
case DOCNUMBER_INTERNAL: case DOCNUMBER_INTERNAL:
@ -958,9 +966,10 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
default: default:
logmsg("Replying to with a 404"); logmsg("Replying to with a 404");
if(req->protocol == RPROT_HTTP) { if(req->protocol == RPROT_HTTP) {
buffer = doc404_HTTP; buffer = doc404_HTTP;
} else { }
buffer = doc404_RTSP; else {
buffer = doc404_RTSP;
} }
break; break;
} }
@ -971,7 +980,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
char *filename = test2file(req->testno); char *filename = test2file(req->testno);
if(0 != req->partno) if(0 != req->partno)
sprintf(partbuf, "data%ld", req->partno); snprintf(partbuf, sizeof(partbuf), "data%ld", req->partno);
stream=fopen(filename, "rb"); stream=fopen(filename, "rb");
if(!stream) { if(!stream) {
@ -1058,7 +1067,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
if(num > 200) if(num > 200)
num = 200; num = 200;
written = swrite(sock, buffer, num); written = swrite(sock, buffer, num);
if (written < 0) { if(written < 0) {
sendfailure = TRUE; sendfailure = TRUE;
break; break;
} }
@ -1082,7 +1091,8 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
size_t num = count; size_t num = count;
if(num > 200) if(num > 200)
num = 200; num = 200;
written = swrite(sock, req->rtp_buffer + (req->rtp_buffersize - count), num); written = swrite(sock, req->rtp_buffer + (req->rtp_buffersize - count),
num);
if(written < 0) { if(written < 0) {
sendfailure = TRUE; sendfailure = TRUE;
break; break;
@ -1108,7 +1118,8 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
} }
if(sendfailure) { if(sendfailure) {
logmsg("Sending response failed. Only (%zu bytes) of (%zu bytes) were sent", logmsg("Sending response failed. Only (%zu bytes) of "
"(%zu bytes) were sent",
responsesize-count, responsesize); responsesize-count, responsesize);
free(ptr); free(ptr);
free(cmd); free(cmd);
@ -1119,7 +1130,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
responsesize); responsesize);
free(ptr); free(ptr);
if(cmdsize > 0 ) { if(cmdsize > 0) {
char command[32]; char command[32];
int quarters; int quarters;
int num; int num;
@ -1278,7 +1289,7 @@ int main(int argc, char *argv[])
} }
flag = 1; flag = 1;
if (0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, if(0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(void *)&flag, sizeof(flag))) { (void *)&flag, sizeof(flag))) {
error = SOCKERRNO; error = SOCKERRNO;
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s", logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
@ -1331,12 +1342,12 @@ int main(int argc, char *argv[])
if(!wrotepidfile) if(!wrotepidfile)
goto server_cleanup; goto server_cleanup;
for (;;) { for(;;) {
msgsock = accept(sock, NULL, NULL); msgsock = accept(sock, NULL, NULL);
if(got_exit_signal) if(got_exit_signal)
break; break;
if (CURL_SOCKET_BAD == msgsock) { if(CURL_SOCKET_BAD == msgsock) {
error = SOCKERRNO; error = SOCKERRNO;
logmsg("MAJOR ERROR: accept() failed with error: (%d) %s", logmsg("MAJOR ERROR: accept() failed with error: (%d) %s",
error, strerror(error)); error, strerror(error));
@ -1360,7 +1371,7 @@ int main(int argc, char *argv[])
* response in many small segments to torture the clients more. * response in many small segments to torture the clients more.
*/ */
flag = 1; flag = 1;
if (setsockopt(msgsock, IPPROTO_TCP, TCP_NODELAY, if(setsockopt(msgsock, IPPROTO_TCP, TCP_NODELAY,
(void *)&flag, sizeof(flag)) == -1) { (void *)&flag, sizeof(flag)) == -1) {
logmsg("====> TCP_NODELAY failed"); logmsg("====> TCP_NODELAY failed");
} }
@ -1424,7 +1435,7 @@ int main(int argc, char *argv[])
clear_advisor_read_lock(SERVERLOGS_LOCK); clear_advisor_read_lock(SERVERLOGS_LOCK);
} }
if (req.testno == DOCNUMBER_QUIT) if(req.testno == DOCNUMBER_QUIT)
break; break;
} }

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -53,31 +53,34 @@
* *
* This program is a single-threaded process. * This program is a single-threaded process.
* *
* This program is intended to be highly portable and as such it must be kept as * This program is intended to be highly portable and as such it must be kept
* simple as possible, due to this the only signal handling mechanisms used will * as simple as possible, due to this the only signal handling mechanisms used
* be those of ANSI C, and used only in the most basic form which is good enough * will be those of ANSI C, and used only in the most basic form which is good
* for the purpose of this program. * enough for the purpose of this program.
* *
* For the above reason and the specific needs of this program signals SIGHUP, * For the above reason and the specific needs of this program signals SIGHUP,
* SIGPIPE and SIGALRM will be simply ignored on systems where this can be done. * SIGPIPE and SIGALRM will be simply ignored on systems where this can be
* If possible, signals SIGINT and SIGTERM will be handled by this program as an * done. If possible, signals SIGINT and SIGTERM will be handled by this
* indication to cleanup and finish execution as soon as possible. This will be * program as an indication to cleanup and finish execution as soon as
* achieved with a single signal handler 'exit_signal_handler' for both signals. * possible. This will be achieved with a single signal handler
* 'exit_signal_handler' for both signals.
* *
* The 'exit_signal_handler' upon the first SIGINT or SIGTERM received signal * The 'exit_signal_handler' upon the first SIGINT or SIGTERM received signal
* will just set to one the global var 'got_exit_signal' storing in global var * will just set to one the global var 'got_exit_signal' storing in global var
* 'exit_signal' the signal that triggered this change. * 'exit_signal' the signal that triggered this change.
* *
* Nothing fancy that could introduce problems is used, the program at certain * Nothing fancy that could introduce problems is used, the program at certain
* points in its normal flow checks if var 'got_exit_signal' is set and in case * points in its normal flow checks if var 'got_exit_signal' is set and in
* this is true it just makes its way out of loops and functions in structured * case this is true it just makes its way out of loops and functions in
* and well behaved manner to achieve proper program cleanup and termination. * structured and well behaved manner to achieve proper program cleanup and
* termination.
* *
* Even with the above mechanism implemented it is worthwile to note that other * Even with the above mechanism implemented it is worthwile to note that
* signals might still be received, or that there might be systems on which it * other signals might still be received, or that there might be systems on
* is not possible to trap and ignore some of the above signals. This implies * which it is not possible to trap and ignore some of the above signals.
* that for increased portability and reliability the program must be coded as * This implies that for increased portability and reliability the program
* if no signal was being ignored or handled at all. Enjoy it! * must be coded as if no signal was being ignored or handled at all. Enjoy
* it!
*/ */
#ifdef HAVE_SIGNAL_H #ifdef HAVE_SIGNAL_H
@ -469,23 +472,28 @@ static void lograw(unsigned char *buffer, ssize_t len)
unsigned char *ptr = buffer; unsigned char *ptr = buffer;
char *optr = data; char *optr = data;
ssize_t width=0; ssize_t width=0;
int left = sizeof(data);
for(i=0; i<len; i++) { for(i=0; i<len; i++) {
switch(ptr[i]) { switch(ptr[i]) {
case '\n': case '\n':
sprintf(optr, "\\n"); snprintf(optr, left, "\\n");
width += 2; width += 2;
optr += 2; optr += 2;
left-=2;
break; break;
case '\r': case '\r':
sprintf(optr, "\\r"); snprintf(optr, left, "\\r");
width += 2; width += 2;
optr += 2; optr += 2;
left-=2;
break; break;
default: default:
sprintf(optr, "%c", (ISGRAPH(ptr[i]) || ptr[i]==0x20) ?ptr[i]:'.'); snprintf(optr, left, "%c", (ISGRAPH(ptr[i]) ||
ptr[i]==0x20) ?ptr[i]:'.');
width++; width++;
optr++; optr++;
left--;
break; break;
} }
@ -493,6 +501,7 @@ static void lograw(unsigned char *buffer, ssize_t len)
logmsg("'%s'", data); logmsg("'%s'", data);
width = 0; width = 0;
optr = data; optr = data;
left = sizeof(data);
} }
} }
if(width) if(width)
@ -1038,7 +1047,7 @@ static bool juggle(curl_socket_t *sockfdp,
return FALSE; return FALSE;
logmsg("Received %c%c%c%c (on stdin)", logmsg("Received %c%c%c%c (on stdin)",
buffer[0], buffer[1], buffer[2], buffer[3] ); buffer[0], buffer[1], buffer[2], buffer[3]);
if(!memcmp("PING", buffer, 4)) { if(!memcmp("PING", buffer, 4)) {
/* send reply on stdout, just proving we are alive */ /* send reply on stdout, just proving we are alive */
@ -1049,7 +1058,7 @@ static bool juggle(curl_socket_t *sockfdp,
else if(!memcmp("PORT", buffer, 4)) { else if(!memcmp("PORT", buffer, 4)) {
/* Question asking us what PORT number we are listening to. /* Question asking us what PORT number we are listening to.
Replies to PORT with "IPv[num]/[port]" */ Replies to PORT with "IPv[num]/[port]" */
sprintf((char *)buffer, "%s/%hu\n", ipv_inuse, port); snprintf((char *)buffer, sizeof(buffer), "%s/%hu\n", ipv_inuse, port);
buffer_len = (ssize_t)strlen((char *)buffer); buffer_len = (ssize_t)strlen((char *)buffer);
snprintf(data, sizeof(data), "PORT\n%04zx\n", buffer_len); snprintf(data, sizeof(data), "PORT\n%04zx\n", buffer_len);
if(!write_stdout(data, 10)) if(!write_stdout(data, 10))
@ -1071,7 +1080,7 @@ static bool juggle(curl_socket_t *sockfdp,
buffer[5] = '\0'; buffer[5] = '\0';
buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16); buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
if (buffer_len > (ssize_t)sizeof(buffer)) { if(buffer_len > (ssize_t)sizeof(buffer)) {
logmsg("ERROR: Buffer size (%zu bytes) too small for data size " logmsg("ERROR: Buffer size (%zu bytes) too small for data size "
"(%zd bytes)", sizeof(buffer), buffer_len); "(%zd bytes)", sizeof(buffer), buffer_len);
return FALSE; return FALSE;
@ -1469,7 +1478,7 @@ int main(int argc, char *argv[])
me.sa4.sin_family = AF_INET; me.sa4.sin_family = AF_INET;
me.sa4.sin_port = htons(connectport); me.sa4.sin_port = htons(connectport);
me.sa4.sin_addr.s_addr = INADDR_ANY; me.sa4.sin_addr.s_addr = INADDR_ANY;
if (!addr) if(!addr)
addr = "127.0.0.1"; addr = "127.0.0.1";
Curl_inet_pton(AF_INET, addr, &me.sa4.sin_addr); Curl_inet_pton(AF_INET, addr, &me.sa4.sin_addr);
@ -1480,7 +1489,7 @@ int main(int argc, char *argv[])
memset(&me.sa6, 0, sizeof(me.sa6)); memset(&me.sa6, 0, sizeof(me.sa6));
me.sa6.sin6_family = AF_INET6; me.sa6.sin6_family = AF_INET6;
me.sa6.sin6_port = htons(connectport); me.sa6.sin6_port = htons(connectport);
if (!addr) if(!addr)
addr = "::1"; addr = "::1";
Curl_inet_pton(AF_INET6, addr, &me.sa6.sin6_addr); Curl_inet_pton(AF_INET6, addr, &me.sa6.sin6_addr);

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -491,11 +491,11 @@ static int ProcessRequest(struct httprequest *req)
/* get the number after it */ /* get the number after it */
if(ptr) { if(ptr) {
if((strlen(doc) + strlen(request)) < 400) if((strlen(doc) + strlen(request)) < 400)
sprintf(logbuf, "Got request: %s %s HTTP/%d.%d", snprintf(logbuf, sizeof(logbuf), "Got request: %s %s HTTP/%d.%d",
request, doc, prot_major, prot_minor); request, doc, prot_major, prot_minor);
else else
sprintf(logbuf, "Got a *HUGE* request HTTP/%d.%d", snprintf(logbuf, sizeof(logbuf), "Got a *HUGE* request HTTP/%d.%d",
prot_major, prot_minor); prot_major, prot_minor);
logmsg("%s", logbuf); logmsg("%s", logbuf);
if(!strncmp("/verifiedserver", ptr, 15)) { if(!strncmp("/verifiedserver", ptr, 15)) {
@ -527,8 +527,8 @@ static int ProcessRequest(struct httprequest *req)
if(req->testno) { if(req->testno) {
sprintf(logbuf, "Requested test number %ld part %ld", snprintf(logbuf, sizeof(logbuf), "Requested test number %ld part %ld",
req->testno, req->partno); req->testno, req->partno);
logmsg("%s", logbuf); logmsg("%s", logbuf);
/* find and parse <servercmd> for this test */ /* find and parse <servercmd> for this test */
@ -548,8 +548,9 @@ static int ProcessRequest(struct httprequest *req)
char *portp = NULL; char *portp = NULL;
unsigned long part=0; unsigned long part=0;
sprintf(logbuf, "Received a CONNECT %s HTTP/%d.%d request", snprintf(logbuf, sizeof(logbuf),
doc, prot_major, prot_minor); "Received a CONNECT %s HTTP/%d.%d request",
doc, prot_major, prot_minor);
logmsg("%s", logbuf); logmsg("%s", logbuf);
req->connect_request = TRUE; req->connect_request = TRUE;
@ -571,7 +572,7 @@ static int ProcessRequest(struct httprequest *req)
} }
if(*p != ']') if(*p != ']')
logmsg("Invalid CONNECT IPv6 address format"); logmsg("Invalid CONNECT IPv6 address format");
else if (*(p+1) != ':') else if(*(p+1) != ':')
logmsg("Invalid CONNECT IPv6 port format"); logmsg("Invalid CONNECT IPv6 port format");
else else
portp = p+1; portp = p+1;
@ -618,8 +619,9 @@ static int ProcessRequest(struct httprequest *req)
else else
req->partno = 0; req->partno = 0;
sprintf(logbuf, "Requested test number %ld part %ld (from host name)", snprintf(logbuf, sizeof(logbuf),
req->testno, req->partno); "Requested test number %ld part %ld (from host name)",
req->testno, req->partno);
logmsg("%s", logbuf); logmsg("%s", logbuf);
} }
@ -668,8 +670,9 @@ static int ProcessRequest(struct httprequest *req)
else else
req->partno = 0; req->partno = 0;
sprintf(logbuf, "Requested GOPHER test number %ld part %ld", snprintf(logbuf, sizeof(logbuf),
req->testno, req->partno); "Requested GOPHER test number %ld part %ld",
req->testno, req->partno);
logmsg("%s", logbuf); logmsg("%s", logbuf);
} }
} }
@ -843,15 +846,15 @@ static void storerequest(char *reqbuf, size_t totalsize)
FILE *dump; FILE *dump;
const char *dumpfile=is_proxy?REQUEST_PROXY_DUMP:REQUEST_DUMP; const char *dumpfile=is_proxy?REQUEST_PROXY_DUMP:REQUEST_DUMP;
if (reqbuf == NULL) if(reqbuf == NULL)
return; return;
if (totalsize == 0) if(totalsize == 0)
return; return;
do { do {
dump = fopen(dumpfile, "ab"); dump = fopen(dumpfile, "ab");
} while ((dump == NULL) && ((error = errno) == EINTR)); } while((dump == NULL) && ((error = errno) == EINTR));
if (dump == NULL) { if(dump == NULL) {
logmsg("[2] Error opening file %s error: %d %s", logmsg("[2] Error opening file %s error: %d %s",
dumpfile, error, strerror(error)); dumpfile, error, strerror(error));
logmsg("Failed to write request input "); logmsg("Failed to write request input ");
@ -866,7 +869,7 @@ static void storerequest(char *reqbuf, size_t totalsize)
goto storerequest_cleanup; goto storerequest_cleanup;
if(written > 0) if(written > 0)
writeleft -= written; writeleft -= written;
} while ((writeleft > 0) && ((error = errno) == EINTR)); } while((writeleft > 0) && ((error = errno) == EINTR));
if(writeleft == 0) if(writeleft == 0)
logmsg("Wrote request (%zu bytes) input to %s", totalsize, dumpfile); logmsg("Wrote request (%zu bytes) input to %s", totalsize, dumpfile);
@ -968,7 +971,7 @@ static int get_request(curl_socket_t sock, struct httprequest *req)
} }
else if(got < 0) { else if(got < 0) {
error = SOCKERRNO; error = SOCKERRNO;
if (EAGAIN == error || EWOULDBLOCK == error) { if(EAGAIN == error || EWOULDBLOCK == error) {
/* nothing to read at the moment */ /* nothing to read at the moment */
return 0; return 0;
} }
@ -1013,7 +1016,7 @@ static int get_request(curl_socket_t sock, struct httprequest *req)
reqbuf[req->offset] = '\0'; reqbuf[req->offset] = '\0';
/* at the end of a request dump it to an external file */ /* at the end of a request dump it to an external file */
if (fail || req->done_processing) if(fail || req->done_processing)
storerequest(reqbuf, req->pipelining ? req->checkindex : req->offset); storerequest(reqbuf, req->pipelining ? req->checkindex : req->offset);
if(got_exit_signal) if(got_exit_signal)
return -1; return -1;
@ -1047,7 +1050,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
case RCMD_STREAM: case RCMD_STREAM:
#define STREAMTHIS "a string to stream 01234567890\n" #define STREAMTHIS "a string to stream 01234567890\n"
count = strlen(STREAMTHIS); count = strlen(STREAMTHIS);
for (;;) { for(;;) {
written = swrite(sock, STREAMTHIS, count); written = swrite(sock, STREAMTHIS, count);
if(got_exit_signal) if(got_exit_signal)
return -1; return -1;
@ -1076,13 +1079,14 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
case DOCNUMBER_WERULEZ: case DOCNUMBER_WERULEZ:
/* we got a "friends?" question, reply back that we sure are */ /* we got a "friends?" question, reply back that we sure are */
logmsg("Identifying ourselves as friends"); logmsg("Identifying ourselves as friends");
sprintf(msgbuf, "WE ROOLZ: %ld\r\n", (long)getpid()); snprintf(msgbuf, sizeof(msgbuf), "WE ROOLZ: %ld\r\n", (long)getpid());
msglen = strlen(msgbuf); msglen = strlen(msgbuf);
if(use_gopher) if(use_gopher)
sprintf(weare, "%s", msgbuf); snprintf(weare, sizeof(weare), "%s", msgbuf);
else else
sprintf(weare, "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s", snprintf(weare, sizeof(weare),
msglen, msgbuf); "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
msglen, msgbuf);
buffer = weare; buffer = weare;
break; break;
case DOCNUMBER_404: case DOCNUMBER_404:
@ -1103,9 +1107,9 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
const char *section= req->connect_request?"connect":"data"; const char *section= req->connect_request?"connect":"data";
if(req->partno) if(req->partno)
sprintf(partbuf, "%s%ld", section, req->partno); snprintf(partbuf, sizeof(partbuf), "%s%ld", section, req->partno);
else else
sprintf(partbuf, "%s", section); snprintf(partbuf, sizeof(partbuf), "%s", section);
logmsg("Send response test%ld section <%s>", req->testno, partbuf); logmsg("Send response test%ld section <%s>", req->testno, partbuf);
@ -1191,7 +1195,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
if(num > 200) if(num > 200)
num = 200; num = 200;
written = swrite(sock, buffer, num); written = swrite(sock, buffer, num);
if (written < 0) { if(written < 0) {
sendfailure = TRUE; sendfailure = TRUE;
break; break;
} }
@ -1228,7 +1232,8 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
} }
if(sendfailure) { if(sendfailure) {
logmsg("Sending response failed. Only (%zu bytes) of (%zu bytes) were sent", logmsg("Sending response failed. Only (%zu bytes) of (%zu bytes) "
"were sent",
responsesize-count, responsesize); responsesize-count, responsesize);
free(ptr); free(ptr);
free(cmd); free(cmd);
@ -1239,7 +1244,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
responsesize, responsedump); responsesize, responsedump);
free(ptr); free(ptr);
if(cmdsize > 0 ) { if(cmdsize > 0) {
char command[32]; char command[32];
int quarters; int quarters;
int num; int num;
@ -1552,7 +1557,8 @@ static void http_connect(curl_socket_t *infdp,
/* connect to the server */ /* connect to the server */
serverfd[DATA] = connect_to(ipaddr, req2.connect_port); serverfd[DATA] = connect_to(ipaddr, req2.connect_port);
if(serverfd[DATA] != CURL_SOCKET_BAD) { if(serverfd[DATA] != CURL_SOCKET_BAD) {
/* secondary tunnel established, now we have two connections */ /* secondary tunnel established, now we have two
connections */
poll_client_rd[DATA] = TRUE; poll_client_rd[DATA] = TRUE;
poll_client_wr[DATA] = TRUE; poll_client_wr[DATA] = TRUE;
poll_server_rd[DATA] = TRUE; poll_server_rd[DATA] = TRUE;
@ -1874,7 +1880,7 @@ static int service_connection(curl_socket_t msgsock, struct httprequest *req,
while(!req->done_processing) { while(!req->done_processing) {
int rc = get_request(msgsock, req); int rc = get_request(msgsock, req);
if (rc <= 0) { if(rc <= 0) {
/* Nothing further to read now (possibly because the socket was closed */ /* Nothing further to read now (possibly because the socket was closed */
return rc; return rc;
} }
@ -2153,7 +2159,7 @@ int main(int argc, char *argv[])
/* socket server is not alive, now check if it was actually a socket. /* socket server is not alive, now check if it was actually a socket.
* Systems which have Unix sockets will also have lstat */ * Systems which have Unix sockets will also have lstat */
rc = lstat(unix_socket, &statbuf); rc = lstat(unix_socket, &statbuf);
if (0 != rc) { if(0 != rc) {
logmsg("Error binding socket, failed to stat %s: (%d) %s", logmsg("Error binding socket, failed to stat %s: (%d) %s",
unix_socket, errno, strerror(errno)); unix_socket, errno, strerror(errno));
goto sws_cleanup; goto sws_cleanup;
@ -2223,8 +2229,8 @@ int main(int argc, char *argv[])
curl_socket_t maxfd = (curl_socket_t)-1; curl_socket_t maxfd = (curl_socket_t)-1;
/* Clear out closed sockets */ /* Clear out closed sockets */
for (socket_idx = num_sockets - 1; socket_idx >= 1; --socket_idx) { for(socket_idx = num_sockets - 1; socket_idx >= 1; --socket_idx) {
if (CURL_SOCKET_BAD == all_sockets[socket_idx]) { if(CURL_SOCKET_BAD == all_sockets[socket_idx]) {
char* dst = (char *) (all_sockets + socket_idx); char* dst = (char *) (all_sockets + socket_idx);
char* src = (char *) (all_sockets + socket_idx + 1); char* src = (char *) (all_sockets + socket_idx + 1);
char* end = (char *) (all_sockets + num_sockets); char* end = (char *) (all_sockets + num_sockets);
@ -2240,7 +2246,7 @@ int main(int argc, char *argv[])
FD_ZERO(&input); FD_ZERO(&input);
FD_ZERO(&output); FD_ZERO(&output);
for (socket_idx = 0; socket_idx < num_sockets; ++socket_idx) { for(socket_idx = 0; socket_idx < num_sockets; ++socket_idx) {
/* Listen on all sockets */ /* Listen on all sockets */
FD_SET(all_sockets[socket_idx], &input); FD_SET(all_sockets[socket_idx], &input);
if(all_sockets[socket_idx] > maxfd) if(all_sockets[socket_idx] > maxfd)
@ -2251,7 +2257,7 @@ int main(int argc, char *argv[])
goto sws_cleanup; goto sws_cleanup;
rc = select((int)maxfd + 1, &input, &output, NULL, &timeout); rc = select((int)maxfd + 1, &input, &output, NULL, &timeout);
if (rc < 0) { if(rc < 0) {
error = SOCKERRNO; error = SOCKERRNO;
logmsg("select() failed with error: (%d) %s", logmsg("select() failed with error: (%d) %s",
error, strerror(error)); error, strerror(error));
@ -2261,26 +2267,26 @@ int main(int argc, char *argv[])
if(got_exit_signal) if(got_exit_signal)
goto sws_cleanup; goto sws_cleanup;
if (rc == 0) { if(rc == 0) {
/* Timed out - try again*/ /* Timed out - try again*/
continue; continue;
} }
/* Check if the listening socket is ready to accept */ /* Check if the listening socket is ready to accept */
if (FD_ISSET(all_sockets[0], &input)) { if(FD_ISSET(all_sockets[0], &input)) {
/* Service all queued connections */ /* Service all queued connections */
curl_socket_t msgsock; curl_socket_t msgsock;
do { do {
msgsock = accept_connection(sock); msgsock = accept_connection(sock);
logmsg("accept_connection %d returned %d", sock, msgsock); logmsg("accept_connection %d returned %d", sock, msgsock);
if (CURL_SOCKET_BAD == msgsock) if(CURL_SOCKET_BAD == msgsock)
goto sws_cleanup; goto sws_cleanup;
} while (msgsock > 0); } while(msgsock > 0);
} }
/* Service all connections that are ready */ /* Service all connections that are ready */
for (socket_idx = 1; socket_idx < num_sockets; ++socket_idx) { for(socket_idx = 1; socket_idx < num_sockets; ++socket_idx) {
if (FD_ISSET(all_sockets[socket_idx], &input)) { if(FD_ISSET(all_sockets[socket_idx], &input)) {
if(got_exit_signal) if(got_exit_signal)
goto sws_cleanup; goto sws_cleanup;
@ -2291,7 +2297,7 @@ int main(int argc, char *argv[])
if(got_exit_signal) if(got_exit_signal)
goto sws_cleanup; goto sws_cleanup;
if (rc < 0) { if(rc < 0) {
logmsg("====> Client disconnect %d", req.connmon); logmsg("====> Client disconnect %d", req.connmon);
if(req.connmon) { if(req.connmon) {
@ -2315,14 +2321,14 @@ int main(int argc, char *argv[])
if(!serverlogslocked) if(!serverlogslocked)
clear_advisor_read_lock(SERVERLOGS_LOCK); clear_advisor_read_lock(SERVERLOGS_LOCK);
if (req.testno == DOCNUMBER_QUIT) if(req.testno == DOCNUMBER_QUIT)
goto sws_cleanup; goto sws_cleanup;
} }
/* Reset the request, unless we're still in the middle of reading */ /* Reset the request, unless we're still in the middle of reading */
if (rc != 0) if(rc != 0)
init_httprequest(&req); init_httprequest(&req);
} while (rc > 0); } while(rc > 0);
} }
} }
@ -2332,7 +2338,7 @@ int main(int argc, char *argv[])
sws_cleanup: sws_cleanup:
for (socket_idx = 1; socket_idx < num_sockets; ++socket_idx) for(socket_idx = 1; socket_idx < num_sockets; ++socket_idx)
if((all_sockets[socket_idx] != sock) && if((all_sockets[socket_idx] != sock) &&
(all_sockets[socket_idx] != CURL_SOCKET_BAD)) (all_sockets[socket_idx] != CURL_SOCKET_BAD))
sclose(all_sockets[socket_idx]); sclose(all_sockets[socket_idx]);

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -41,7 +41,7 @@ int main(int argc, char **argv)
else { else {
rc = getpart(&part, &partlen, argv[1], argv[2], stdin); rc = getpart(&part, &partlen, argv[1], argv[2], stdin);
if(rc) if(rc)
return(rc); return rc;
for(i = 0; i < partlen; i++) for(i = 0; i < partlen; i++)
printf("%c", part[i]); printf("%c", part[i]);
free(part); free(part);

View File

@ -459,7 +459,7 @@ static int readit(struct testcase *test, struct tftphdr **dpp,
current = !current; /* "incr" current */ current = !current; /* "incr" current */
b = &bfs[current]; /* look at new buffer */ b = &bfs[current]; /* look at new buffer */
if (b->counter == BF_FREE) /* if it's empty */ if(b->counter == BF_FREE) /* if it's empty */
read_ahead(test, convert); /* fill it */ read_ahead(test, convert); /* fill it */
*dpp = &b->buf.hdr; /* set caller's ptr */ *dpp = &b->buf.hdr; /* set caller's ptr */
@ -468,7 +468,7 @@ static int readit(struct testcase *test, struct tftphdr **dpp,
/* /*
* fill the input buffer, doing ascii conversions if requested * fill the input buffer, doing ascii conversions if requested
* conversions are lf -> cr,lf and cr -> cr, nul * conversions are lf -> cr, lf and cr -> cr, nul
*/ */
static void read_ahead(struct testcase *test, static void read_ahead(struct testcase *test,
int convert /* if true, convert to ascii */) int convert /* if true, convert to ascii */)
@ -480,13 +480,13 @@ static void read_ahead(struct testcase *test,
struct tftphdr *dp; struct tftphdr *dp;
b = &bfs[nextone]; /* look at "next" buffer */ b = &bfs[nextone]; /* look at "next" buffer */
if (b->counter != BF_FREE) /* nop if not free */ if(b->counter != BF_FREE) /* nop if not free */
return; return;
nextone = !nextone; /* "incr" next buffer ptr */ nextone = !nextone; /* "incr" next buffer ptr */
dp = &b->buf.hdr; dp = &b->buf.hdr;
if (convert == 0) { if(convert == 0) {
/* The former file reading code did this: /* The former file reading code did this:
b->counter = read(fileno(file), dp->th_data, SEGSIZE); */ b->counter = read(fileno(file), dp->th_data, SEGSIZE); */
size_t copy_n = MIN(SEGSIZE, test->rcount); size_t copy_n = MIN(SEGSIZE, test->rcount);
@ -500,9 +500,9 @@ static void read_ahead(struct testcase *test,
} }
p = dp->th_data; p = dp->th_data;
for (i = 0 ; i < SEGSIZE; i++) { for(i = 0 ; i < SEGSIZE; i++) {
if (newline) { if(newline) {
if (prevchar == '\n') if(prevchar == '\n')
c = '\n'; /* lf to cr,lf */ c = '\n'; /* lf to cr,lf */
else else
c = '\0'; /* cr to cr,nul */ c = '\0'; /* cr to cr,nul */
@ -516,7 +516,7 @@ static void read_ahead(struct testcase *test,
} }
else else
break; break;
if (c == '\n' || c == '\r') { if(c == '\n' || c == '\r') {
prevchar = c; prevchar = c;
c = '\r'; c = '\r';
newline = 1; newline = 1;
@ -535,7 +535,7 @@ static int writeit(struct testcase *test, struct tftphdr * volatile *dpp,
{ {
bfs[current].counter = ct; /* set size of data to write */ bfs[current].counter = ct; /* set size of data to write */
current = !current; /* switch to other buffer */ current = !current; /* switch to other buffer */
if (bfs[current].counter != BF_FREE) /* if not free */ if(bfs[current].counter != BF_FREE) /* if not free */
write_behind(test, convert); /* flush it */ write_behind(test, convert); /* flush it */
bfs[current].counter = BF_ALLOC; /* mark as alloc'd */ bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
*dpp = &bfs[current].buf.hdr; *dpp = &bfs[current].buf.hdr;
@ -544,7 +544,7 @@ static int writeit(struct testcase *test, struct tftphdr * volatile *dpp,
/* /*
* Output a buffer to a file, converting from netascii if requested. * Output a buffer to a file, converting from netascii if requested.
* CR,NUL -> CR and CR,LF => LF. * CR, NUL -> CR and CR, LF => LF.
* Note spec is undefined if we get CR as last byte of file or a * Note spec is undefined if we get CR as last byte of file or a
* CR followed by anything else. In this case we leave it alone. * CR followed by anything else. In this case we leave it alone.
*/ */
@ -559,7 +559,7 @@ static ssize_t write_behind(struct testcase *test, int convert)
struct tftphdr *dp; struct tftphdr *dp;
b = &bfs[nextone]; b = &bfs[nextone];
if (b->counter < -1) /* anything to flush? */ if(b->counter < -1) /* anything to flush? */
return 0; /* just nop if nothing to do */ return 0; /* just nop if nothing to do */
if(!test->ofile) { if(!test->ofile) {
@ -582,21 +582,21 @@ static ssize_t write_behind(struct testcase *test, int convert)
nextone = !nextone; /* incr for next time */ nextone = !nextone; /* incr for next time */
writebuf = dp->th_data; writebuf = dp->th_data;
if (count <= 0) if(count <= 0)
return -1; /* nak logic? */ return -1; /* nak logic? */
if (convert == 0) if(convert == 0)
return write(test->ofile, writebuf, count); return write(test->ofile, writebuf, count);
p = writebuf; p = writebuf;
ct = count; ct = count;
while (ct--) { /* loop over the buffer */ while(ct--) { /* loop over the buffer */
c = *p++; /* pick up a character */ c = *p++; /* pick up a character */
if (prevchar == '\r') { /* if prev char was cr */ if(prevchar == '\r') { /* if prev char was cr */
if (c == '\n') /* if have cr,lf then just */ if(c == '\n') /* if have cr,lf then just */
lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */ lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */
else else
if (c == '\0') /* if have cr,nul then */ if(c == '\0') /* if have cr,nul then */
goto skipit; /* just skip over the putc */ goto skipit; /* just skip over the putc */
/* else just fall through and allow it */ /* else just fall through and allow it */
} }
@ -634,13 +634,13 @@ static int synchnet(curl_socket_t f /* socket to flush */)
srvr_sockaddr_union_t fromaddr; srvr_sockaddr_union_t fromaddr;
curl_socklen_t fromaddrlen; curl_socklen_t fromaddrlen;
for (;;) { for(;;) {
#if defined(HAVE_IOCTLSOCKET) #if defined(HAVE_IOCTLSOCKET)
(void) ioctlsocket(f, FIONREAD, &i); (void) ioctlsocket(f, FIONREAD, &i);
#else #else
(void) ioctl(f, FIONREAD, &i); (void) ioctl(f, FIONREAD, &i);
#endif #endif
if (i) { if(i) {
j++; j++;
#ifdef ENABLE_IPV6 #ifdef ENABLE_IPV6
if(!use_ipv6) if(!use_ipv6)
@ -773,7 +773,7 @@ int main(int argc, char **argv)
} }
flag = 1; flag = 1;
if (0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, if(0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(void *)&flag, sizeof(flag))) { (void *)&flag, sizeof(flag))) {
error = SOCKERRNO; error = SOCKERRNO;
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s", logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
@ -816,7 +816,7 @@ int main(int argc, char **argv)
logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port); logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port);
for (;;) { for(;;) {
fromlen = sizeof(from); fromlen = sizeof(from);
#ifdef ENABLE_IPV6 #ifdef ENABLE_IPV6
if(!use_ipv6) if(!use_ipv6)
@ -830,7 +830,7 @@ int main(int argc, char **argv)
&from.sa, &fromlen); &from.sa, &fromlen);
if(got_exit_signal) if(got_exit_signal)
break; break;
if (n < 0) { if(n < 0) {
logmsg("recvfrom"); logmsg("recvfrom");
result = 3; result = 3;
break; break;
@ -876,9 +876,9 @@ int main(int argc, char **argv)
tp = &buf.hdr; tp = &buf.hdr;
tp->th_opcode = ntohs(tp->th_opcode); tp->th_opcode = ntohs(tp->th_opcode);
if (tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) { if(tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
memset(&test, 0, sizeof(test)); memset(&test, 0, sizeof(test));
if (do_tftp(&test, tp, n) < 0) if(do_tftp(&test, tp, n) < 0)
break; break;
free(test.buffer); free(test.buffer);
} }
@ -974,8 +974,8 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
filename = cp; filename = cp;
do { do {
bool endofit = true; bool endofit = true;
while (cp < &buf.storage[size]) { while(cp < &buf.storage[size]) {
if (*cp == '\0') { if(*cp == '\0') {
endofit = false; endofit = false;
break; break;
} }
@ -1008,7 +1008,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
break; break;
} while(1); } while(1);
if (*cp) { if(*cp) {
nak(EBADOP); nak(EBADOP);
fclose(server); fclose(server);
return 3; return 3;
@ -1017,22 +1017,22 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
/* store input protocol */ /* store input protocol */
fprintf(server, "filename: %s\n", filename); fprintf(server, "filename: %s\n", filename);
for (cp = mode; cp && *cp; cp++) for(cp = mode; cp && *cp; cp++)
if(ISUPPER(*cp)) if(ISUPPER(*cp))
*cp = (char)tolower((int)*cp); *cp = (char)tolower((int)*cp);
/* store input protocol */ /* store input protocol */
fclose(server); fclose(server);
for (pf = formata; pf->f_mode; pf++) for(pf = formata; pf->f_mode; pf++)
if (strcmp(pf->f_mode, mode) == 0) if(strcmp(pf->f_mode, mode) == 0)
break; break;
if (!pf->f_mode) { if(!pf->f_mode) {
nak(EBADOP); nak(EBADOP);
return 2; return 2;
} }
ecode = validate_access(test, filename, tp->th_opcode); ecode = validate_access(test, filename, tp->th_opcode);
if (ecode) { if(ecode) {
nak(ecode); nak(ecode);
return 1; return 1;
} }
@ -1046,7 +1046,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
(const char*)&recvtimeout, sizeof(recvtimeout)); (const char*)&recvtimeout, sizeof(recvtimeout));
#endif #endif
if (tp->th_opcode == opcode_WRQ) if(tp->th_opcode == opcode_WRQ)
recvtftp(test, pf); recvtftp(test, pf);
else else
sendtftp(test, pf); sendtftp(test, pf);
@ -1141,7 +1141,8 @@ static int validate_access(struct testcase *test,
if(!strncmp("verifiedserver", filename, 14)) { if(!strncmp("verifiedserver", filename, 14)) {
char weare[128]; char weare[128];
size_t count = sprintf(weare, "WE ROOLZ: %ld\r\n", (long)getpid()); size_t count = snprintf(weare, sizeof(weare),
"WE ROOLZ: %ld\r\n", (long)getpid());
logmsg("Are-we-friendly question received"); logmsg("Are-we-friendly question received");
test->buffer = strdup(weare); test->buffer = strdup(weare);
@ -1183,7 +1184,7 @@ static int validate_access(struct testcase *test,
file = test2file(testno); file = test2file(testno);
if(0 != partno) if(0 != partno)
sprintf(partbuf, "data%ld", partno); snprintf(partbuf, sizeof(partbuf), "data%ld", partno);
if(file) { if(file) {
FILE *stream=fopen(file, "rb"); FILE *stream=fopen(file, "rb");
@ -1244,7 +1245,7 @@ static void sendtftp(struct testcase *test, struct formats *pf)
sap = &ackbuf.hdr; sap = &ackbuf.hdr;
do { do {
size = readit(test, &sdp, pf->f_convert); size = readit(test, &sdp, pf->f_convert);
if (size < 0) { if(size < 0) {
nak(errno + 100); nak(errno + 100);
return; return;
} }
@ -1261,12 +1262,12 @@ static void sendtftp(struct testcase *test, struct formats *pf)
} }
send_data: send_data:
if (swrite(peer, sdp, size + 4) != size + 4) { if(swrite(peer, sdp, size + 4) != size + 4) {
logmsg("write"); logmsg("write");
return; return;
} }
read_ahead(test, pf->f_convert); read_ahead(test, pf->f_convert);
for ( ; ; ) { for(;;) {
#ifdef HAVE_ALARM #ifdef HAVE_ALARM
alarm(rexmtval); /* read the ack */ alarm(rexmtval); /* read the ack */
#endif #endif
@ -1276,32 +1277,32 @@ static void sendtftp(struct testcase *test, struct formats *pf)
#endif #endif
if(got_exit_signal) if(got_exit_signal)
return; return;
if (n < 0) { if(n < 0) {
logmsg("read: fail"); logmsg("read: fail");
return; return;
} }
sap->th_opcode = ntohs((unsigned short)sap->th_opcode); sap->th_opcode = ntohs((unsigned short)sap->th_opcode);
sap->th_block = ntohs(sap->th_block); sap->th_block = ntohs(sap->th_block);
if (sap->th_opcode == opcode_ERROR) { if(sap->th_opcode == opcode_ERROR) {
logmsg("got ERROR"); logmsg("got ERROR");
return; return;
} }
if (sap->th_opcode == opcode_ACK) { if(sap->th_opcode == opcode_ACK) {
if (sap->th_block == sendblock) { if(sap->th_block == sendblock) {
break; break;
} }
/* Re-synchronize with the other side */ /* Re-synchronize with the other side */
(void) synchnet(peer); (void) synchnet(peer);
if (sap->th_block == (sendblock-1)) { if(sap->th_block == (sendblock-1)) {
goto send_data; goto send_data;
} }
} }
} }
sendblock++; sendblock++;
} while (size == SEGSIZE); } while(size == SEGSIZE);
} }
/* /*
@ -1330,12 +1331,12 @@ static void recvtftp(struct testcase *test, struct formats *pf)
(void) sigsetjmp(timeoutbuf, 1); (void) sigsetjmp(timeoutbuf, 1);
#endif #endif
send_ack: send_ack:
if (swrite(peer, &ackbuf.storage[0], 4) != 4) { if(swrite(peer, &ackbuf.storage[0], 4) != 4) {
logmsg("write: fail\n"); logmsg("write: fail\n");
goto abort; goto abort;
} }
write_behind(test, pf->f_convert); write_behind(test, pf->f_convert);
for ( ; ; ) { for(;;) {
#ifdef HAVE_ALARM #ifdef HAVE_ALARM
alarm(rexmtval); alarm(rexmtval);
#endif #endif
@ -1345,37 +1346,38 @@ send_ack:
#endif #endif
if(got_exit_signal) if(got_exit_signal)
goto abort; goto abort;
if (n < 0) { /* really? */ if(n < 0) { /* really? */
logmsg("read: fail\n"); logmsg("read: fail\n");
goto abort; goto abort;
} }
rdp->th_opcode = ntohs((unsigned short)rdp->th_opcode); rdp->th_opcode = ntohs((unsigned short)rdp->th_opcode);
rdp->th_block = ntohs(rdp->th_block); rdp->th_block = ntohs(rdp->th_block);
if (rdp->th_opcode == opcode_ERROR) if(rdp->th_opcode == opcode_ERROR)
goto abort; goto abort;
if (rdp->th_opcode == opcode_DATA) { if(rdp->th_opcode == opcode_DATA) {
if (rdp->th_block == recvblock) { if(rdp->th_block == recvblock) {
break; /* normal */ break; /* normal */
} }
/* Re-synchronize with the other side */ /* Re-synchronize with the other side */
(void) synchnet(peer); (void) synchnet(peer);
if (rdp->th_block == (recvblock-1)) if(rdp->th_block == (recvblock-1))
goto send_ack; /* rexmit */ goto send_ack; /* rexmit */
} }
} }
size = writeit(test, &rdp, (int)(n - 4), pf->f_convert); size = writeit(test, &rdp, (int)(n - 4), pf->f_convert);
if (size != (n-4)) { /* ahem */ if(size != (n-4)) { /* ahem */
if (size < 0) if(size < 0)
nak(errno + 100); nak(errno + 100);
else else
nak(ENOSPACE); nak(ENOSPACE);
goto abort; goto abort;
} }
} while (size == SEGSIZE); } while(size == SEGSIZE);
write_behind(test, pf->f_convert); write_behind(test, pf->f_convert);
rap->th_opcode = htons((unsigned short)opcode_ACK); /* send the "final" ack */ rap->th_opcode = htons((unsigned short)opcode_ACK); /* send the "final"
ack */
rap->th_block = htons(recvblock); rap->th_block = htons(recvblock);
(void) swrite(peer, &ackbuf.storage[0], 4); (void) swrite(peer, &ackbuf.storage[0], 4);
#if defined(HAVE_ALARM) && defined(SIGALRM) #if defined(HAVE_ALARM) && defined(SIGALRM)
@ -1389,9 +1391,9 @@ send_ack:
#endif #endif
if(got_exit_signal) if(got_exit_signal)
goto abort; goto abort;
if (n >= 4 && /* if read some data */ if(n >= 4 && /* if read some data */
rdp->th_opcode == opcode_DATA && /* and got a data block */ rdp->th_opcode == opcode_DATA && /* and got a data block */
recvblock == rdp->th_block) { /* then my last ack was lost */ recvblock == rdp->th_block) { /* then my last ack was lost */
(void) swrite(peer, &ackbuf.storage[0], 4); /* resend final ack */ (void) swrite(peer, &ackbuf.storage[0], 4); /* resend final ack */
} }
abort: abort:
@ -1411,10 +1413,10 @@ static void nak(int error)
tp = &buf.hdr; tp = &buf.hdr;
tp->th_opcode = htons((unsigned short)opcode_ERROR); tp->th_opcode = htons((unsigned short)opcode_ERROR);
tp->th_code = htons((unsigned short)error); tp->th_code = htons((unsigned short)error);
for (pe = errmsgs; pe->e_code >= 0; pe++) for(pe = errmsgs; pe->e_code >= 0; pe++)
if (pe->e_code == error) if(pe->e_code == error)
break; break;
if (pe->e_code < 0) { if(pe->e_code < 0) {
pe->e_msg = strerror(error - 100); pe->e_msg = strerror(error - 100);
tp->th_code = EUNDEF; /* set 'undef' errorcode */ tp->th_code = EUNDEF; /* set 'undef' errorcode */
} }
@ -1424,6 +1426,6 @@ static void nak(int error)
* report from glibc with FORTIFY_SOURCE */ * report from glibc with FORTIFY_SOURCE */
memcpy(tp->th_msg, pe->e_msg, length + 1); memcpy(tp->th_msg, pe->e_msg, length + 1);
length += 5; length += 5;
if (swrite(peer, &buf.storage[0], length) != length) if(swrite(peer, &buf.storage[0], length) != length)
logmsg("nak: fail\n"); logmsg("nak: fail\n");
} }

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -77,11 +77,11 @@ char *data_to_hex(char *data, size_t len)
if((data[i] >= 0x20) && (data[i] < 0x7f)) if((data[i] >= 0x20) && (data[i] < 0x7f))
*optr++ = *iptr++; *optr++ = *iptr++;
else { else {
sprintf(optr, "%%%02x", *iptr++); snprintf(optr, 4, "%%%02x", *iptr++);
optr+=3; optr+=3;
} }
} }
*optr=0; /* in case no sprintf() was used */ *optr=0; /* in case no sprintf was used */
return buf; return buf;
} }
@ -99,7 +99,7 @@ void logmsg(const char *msg, ...)
static time_t epoch_offset; static time_t epoch_offset;
static int known_offset; static int known_offset;
if (!serverlogfile) { if(!serverlogfile) {
fprintf(stderr, "Error: serverlogfile not set\n"); fprintf(stderr, "Error: serverlogfile not set\n");
return; return;
} }
@ -140,11 +140,11 @@ void win32_perror (const char *msg)
char buf[512]; char buf[512];
DWORD err = SOCKERRNO; DWORD err = SOCKERRNO;
if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
LANG_NEUTRAL, buf, sizeof(buf), NULL)) LANG_NEUTRAL, buf, sizeof(buf), NULL))
snprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err); snprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
if (msg) if(msg)
fprintf(stderr, "%s: ", msg); fprintf(stderr, "%s: ", msg);
fprintf(stderr, "%s\n", buf); fprintf(stderr, "%s\n", buf);
} }
#endif /* WIN32 */ #endif /* WIN32 */
@ -159,15 +159,14 @@ void win32_init(void)
err = WSAStartup(wVersionRequested, &wsaData); err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) { if(err != 0) {
perror("Winsock init failed"); perror("Winsock init failed");
logmsg("Error initialising winsock -- aborting"); logmsg("Error initialising winsock -- aborting");
exit(1); exit(1);
} }
if ( LOBYTE( wsaData.wVersion ) != USE_WINSOCK || if(LOBYTE(wsaData.wVersion) != USE_WINSOCK ||
HIBYTE( wsaData.wVersion ) != USE_WINSOCK ) { HIBYTE(wsaData.wVersion) != USE_WINSOCK) {
WSACleanup(); WSACleanup();
perror("Winsock init failed"); perror("Winsock init failed");
logmsg("No suitable winsock.dll found -- aborting"); logmsg("No suitable winsock.dll found -- aborting");