some changes to the protocol

This commit is contained in:
Ruslan Kuchumov 2014-07-25 19:58:16 +00:00
parent c85ebb0c1e
commit 29515bfcd5

View File

@ -15,13 +15,13 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#define DEBUG_OUT 0 #define DEBUG_OUT 1
/* Protocol constants: */ /* Protocol constants: */
#define INT_LEN 10 #define INT_LEN 10
#define OK 1 #define OK 1
#define PASSWORD 2 #define PASSWORD 2
#define BIN_HEADER 3 #define BIN_MODE 3
#define ERROR 51 #define ERROR 51
#define UNKNOWN_COMMAND 52 #define UNKNOWN_COMMAND 52
#define WRONG_PASSWORD 53 #define WRONG_PASSWORD 53
@ -99,13 +99,15 @@ void net_send_header(const char *data, size_t len)
assert(srv_sd > 0); assert(srv_sd > 0);
#if DEBUG_OUT #if DEBUG_OUT
fprintf(stderr, "[C] Sending header (len = %zd): \n", len); fprintf(stderr, "Sending header (len = %zd): \n", len);
fprintf(stderr, "File created by %02X version %02X%02X\n", data[3], data[4], data[5]); fprintf(stderr, "File created by %02X version %02X%02X\n", data[3], data[4], data[5]);
fprintf(stderr, "File format revision: %02X%02X\n", data[6], data[7]); fprintf(stderr, "File format revision: %02X%02X\n", data[6], data[7]);
#endif #endif
if (write_block(srv_sd, BIN_MODE, NULL, 0) <= 0)
if (write_byte(srv_sd, BIN_HEADER) != 1) {
printf("Can't send BIN header\n");
return; return;
}
char ok; char ok;
if (read_byte(srv_sd, &ok) != 1) if (read_byte(srv_sd, &ok) != 1)
@ -158,9 +160,6 @@ void net_send_cc(const char *data, size_t len)
*/ */
ssize_t write_block(int fd, char command, const char *buf, size_t buf_len) ssize_t write_block(int fd, char command, const char *buf, size_t buf_len)
{ {
assert(buf != NULL);
assert(buf_len > 0);
#if DEBUG_OUT #if DEBUG_OUT
fprintf(stderr, "[C] "); fprintf(stderr, "[C] ");
#endif #endif
@ -424,16 +423,13 @@ int start_srv(const char *port, const char *pwd)
goto close_conn; goto close_conn;
char c; char c;
if (read_byte(sockfd, &c) != 1) size_t len = BUFFER_SIZE;
goto close_conn; char buf[BUFFER_SIZE];
#if DEBUG_OUT do {
fprintf(stderr, "[C] "); if (read_block(sockfd, &c, buf, &len) <= 0)
pr_command(c); goto close_conn;
fprintf(stderr, "\n"); } while (c != BIN_MODE);
#endif
if (c != BIN_HEADER)
goto close_conn;
#if DEBUG_OUT #if DEBUG_OUT
fprintf(stderr, "[S] OK\n"); fprintf(stderr, "[S] OK\n");
@ -460,7 +456,7 @@ int check_password(int fd, const char *pwd)
char c; char c;
int rc; int rc;
size_t len = BUFFER_SIZE; size_t len = BUFFER_SIZE;
char buf[BUFFER_SIZE] = {0}; char buf[BUFFER_SIZE];
while(1) while(1)
{ {
@ -584,37 +580,35 @@ ssize_t read_block(int fd, char *command, char *buf, size_t *buf_len)
#endif #endif
size_t len = atoi(len_str); size_t len = atoi(len_str);
if (len <= 0)
if (len > 0)
{ {
mprint("read_block(): Wrong block size\n"); size_t ign_bytes = 0;
return -1; if (len > *buf_len)
} {
ign_bytes = len - *buf_len;
mprint("read_block() warning: Buffer overflow, ignoring %d bytes\n",
ign_bytes);
len = *buf_len;
}
size_t ign_bytes = 0; if ((rc = readn(fd, buf, len)) < 0)
if (len > *buf_len) return -1;
{ else if ((size_t) rc != len)
ign_bytes = len - *buf_len; return 0;
mprint("read_block() warning: Buffer overflow, ignoring %d bytes\n", nread += rc;
ign_bytes); *buf_len = len;
len = *buf_len;
}
if ((rc = readn(fd, buf, len)) < 0) if ((rc = readn(fd, 0, ign_bytes)) < 0)
return -1; return -1;
else if ((size_t) rc != len) else if ((size_t) rc != ign_bytes)
return 0; return 0;
nread += rc; nread += rc;
*buf_len = len;
if ((rc = readn(fd, 0, ign_bytes)) < 0)
return -1;
else if ((size_t) rc != ign_bytes)
return 0;
nread += rc;
#if DEBUG_OUT #if DEBUG_OUT
fwrite(buf, sizeof(char), len, stderr); fwrite(buf, sizeof(char), len, stderr);
#endif #endif
}
char end[2] = {0}; char end[2] = {0};
if ((rc = readn(fd, end, sizeof(end))) < 0) if ((rc = readn(fd, end, sizeof(end))) < 0)
@ -648,8 +642,8 @@ void pr_command(char c)
case OK: case OK:
fprintf(stderr, "OK"); fprintf(stderr, "OK");
break; break;
case BIN_HEADER: case BIN_MODE:
fprintf(stderr, "BIN_HEADER"); fprintf(stderr, "BIN_MODE");
break; break;
case WRONG_PASSWORD: case WRONG_PASSWORD:
fprintf(stderr, "WRONG_PASSWORD"); fprintf(stderr, "WRONG_PASSWORD");
@ -718,8 +712,7 @@ ssize_t readn(int fd, void *vptr, size_t n)
ssize_t writen(int fd, const void *vptr, size_t n) ssize_t writen(int fd, const void *vptr, size_t n)
{ {
assert(vptr != NULL); assert((n > 0 && vptr != NULL) || (n == 0 && vptr == NULL));
assert(n > 0);
size_t nleft; size_t nleft;
ssize_t nwritten; ssize_t nwritten;