mirror of
https://github.com/fumiama/simple-dict.git
synced 2026-06-21 19:48:03 +08:00
finish
This commit is contained in:
164
client.c
164
client.c
@@ -19,68 +19,91 @@
|
|||||||
struct sf_hdtr hdtr;
|
struct sf_hdtr hdtr;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int sockfd;
|
static int sockfd;
|
||||||
char buf[BUFSIZ];
|
static char buf[BUFSIZ];
|
||||||
char bufr[BUFSIZ];
|
static char bufr[BUFSIZ];
|
||||||
struct sockaddr_in their_addr;
|
static struct sockaddr_in their_addr;
|
||||||
pthread_t thread;
|
static pthread_t thread;
|
||||||
uint32_t file_size;
|
static uint32_t file_size;
|
||||||
int recv_bin = 0;
|
static int recv_bin = 0;
|
||||||
|
static char pwd[64] = "testpwd";
|
||||||
|
static char sps[64] = "testsps";
|
||||||
|
|
||||||
void getMessage(void *p) {
|
void getMessage(void *p) {
|
||||||
int c = 0, offset = 0;
|
int c = 0, offset = 0;
|
||||||
CMDPACKET* cp = bufr;
|
CMDPACKET* cp = (CMDPACKET*)bufr;
|
||||||
while((c = recv(sockfd, bufr+offset, CMDPACKET_HEAD_LEN-offset, MSG_WAITALL)) > 0) {
|
while(offset >= CMDPACKET_HEAD_LEN || (c = recv(sockfd, bufr+offset, CMDPACKET_HEAD_LEN-offset, MSG_WAITALL)) > 0) {
|
||||||
printf("Recv %d bytes: ", c);
|
printf("Recv %d bytes.\n", c);
|
||||||
if(recv_bin) {
|
if(recv_bin) {
|
||||||
recv_bin = 0;
|
if(~recv_bin) {
|
||||||
int i = 0;
|
recv_bin = -1;
|
||||||
bufr[0] = 0;
|
int l = strlen(buf+4);
|
||||||
while(bufr[i] != '$') recv(sockfd, bufr+i, 1, MSG_WAITALL);
|
char savepath[l+1];
|
||||||
bufr[i] = 0;
|
memcpy(savepath, buf+4, l+1);
|
||||||
off_t datalen;
|
printf("Save path: ");
|
||||||
sscanf(bufr, "%d", &datalen);
|
puts(savepath);
|
||||||
printf("raw data len: %d\n", datalen);
|
int i = 0;
|
||||||
char* data = malloc(datalen);
|
while(bufr[i] != '$') i++;
|
||||||
if(datalen == recv(sockfd, data, datalen, MSG_WAITALL)) {
|
while(bufr[i] != '$') recv(sockfd, bufr+i++, 1, MSG_WAITALL);
|
||||||
raw_decrypt(data, &datalen, 0, "testpwd");
|
bufr[i] = 0;
|
||||||
printf("raw data len after decode: %d\n", datalen);
|
off_t datalen;
|
||||||
FILE* fp = fopen("rawdata.bin", "w+");
|
sscanf(bufr, "%d", &datalen);
|
||||||
fwrite(data, datalen, 1, fp);
|
printf("raw data len: %d\n", datalen);
|
||||||
fclose(fp);
|
char* data = malloc(datalen);
|
||||||
|
offset = c - ++i;
|
||||||
|
if(offset > 0) {
|
||||||
|
memcpy(data, bufr+i, offset);
|
||||||
|
printf("copy %d bytes data that had been received.\n", offset);
|
||||||
|
}
|
||||||
|
else offset = 0;
|
||||||
|
if(datalen-offset == recv(sockfd, data+offset, datalen-offset, MSG_WAITALL)) {
|
||||||
|
//FILE* fp = fopen("raw_before_dec", "wb+");
|
||||||
|
//fwrite(data, datalen, 1, fp);
|
||||||
|
//fclose(fp);
|
||||||
|
char* newdata = raw_decrypt(data, &datalen, 0, pwd);
|
||||||
|
if(newdata) {
|
||||||
|
printf("raw data len after decode: %d\n", datalen);
|
||||||
|
FILE* fp = fopen(savepath, "wb+");
|
||||||
|
fwrite(newdata, datalen, 1, fp);
|
||||||
|
fclose(fp);
|
||||||
|
free(newdata);
|
||||||
|
puts("recv raw data succeed.");
|
||||||
|
} else puts("decode raw data error.");
|
||||||
|
} else puts("recv raw data error.");
|
||||||
free(data);
|
free(data);
|
||||||
puts("recv raw data succeed.");
|
recv_bin = offset = 0;
|
||||||
} else {
|
|
||||||
puts("recv raw data error.");
|
|
||||||
free(data);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
offset += c;
|
offset += c;
|
||||||
if(offset < CMDPACKET_HEAD_LEN) {
|
printf("[handle] Get %zd bytes, total: %zd.\n", c, offset);
|
||||||
puts("recv head error.");
|
if(offset < CMDPACKET_HEAD_LEN) break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
c = recv(sockfd, bufr+offset, CMDPACKET_HEAD_LEN+cp->datalen-offset, MSG_WAITALL);
|
|
||||||
if(c <= 0) {
|
|
||||||
puts("on recv body error.");
|
|
||||||
break;
|
|
||||||
} else offset += c;
|
|
||||||
if(offset < CMDPACKET_HEAD_LEN+cp->datalen) {
|
if(offset < CMDPACKET_HEAD_LEN+cp->datalen) {
|
||||||
puts("after recv body error.");
|
c = recv(sockfd, bufr+offset, CMDPACKET_HEAD_LEN+cp->datalen-offset, MSG_WAITALL);
|
||||||
break;
|
if(c <= 0) break;
|
||||||
|
else {
|
||||||
|
offset += c;
|
||||||
|
printf("[handle] Get %zd bytes, total: %zd.\n", c, offset);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(cmdpacket_decrypt(cp, index, "testpwd")) {
|
c = CMDPACKET_HEAD_LEN+cp->datalen; // 暂存 packet len
|
||||||
|
if(offset < c) break;
|
||||||
|
printf("[handle] Decrypt %zd bytes data...\n", cp->datalen);
|
||||||
|
if(cmdpacket_decrypt(cp, 0, pwd)) {
|
||||||
cp->data[cp->datalen] = 0;
|
cp->data[cp->datalen] = 0;
|
||||||
printf("[normal] Get %u bytes data: %s\n", offset, cp->data);
|
printf("[normal] Get %u bytes packet with data: %s\n", offset, cp->data);
|
||||||
switch(cp->cmd) {
|
switch(cp->cmd) {
|
||||||
case CMDACK:
|
case CMDACK:
|
||||||
printf("recv ack: %s\n", cp->data);
|
printf("recv ack: %s\n", cp->data);
|
||||||
break;
|
break;
|
||||||
case CMDEND:
|
default: break;
|
||||||
default: return; break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(offset > c) {
|
||||||
|
offset -= c;
|
||||||
|
memmove(bufr, bufr+c, offset);
|
||||||
|
c = 0;
|
||||||
|
} else offset = 0;
|
||||||
|
printf("offset after analyzing packet: %zd\n", offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,6 +116,9 @@ off_t file_size_of(const char* fname) {
|
|||||||
|
|
||||||
void send_cmd(int accept_fd, CMDPACKET* p) {
|
void send_cmd(int accept_fd, CMDPACKET* p) {
|
||||||
printf("send %d bytes encrypted data with %d bytes head.\n", p->datalen, CMDPACKET_HEAD_LEN);
|
printf("send %d bytes encrypted data with %d bytes head.\n", p->datalen, CMDPACKET_HEAD_LEN);
|
||||||
|
printf("raw packet: ");
|
||||||
|
for(int i = 0; i < CMDPACKET_HEAD_LEN+p->datalen; i++) printf("%02x", ((uint8_t*)p)[i]);
|
||||||
|
putchar('\n');
|
||||||
if(!~send(accept_fd, (void*)p, CMDPACKET_HEAD_LEN+p->datalen, 0)) puts("Send data error");
|
if(!~send(accept_fd, (void*)p, CMDPACKET_HEAD_LEN+p->datalen, 0)) puts("Send data error");
|
||||||
else puts("Send data succeed.");
|
else puts("Send data succeed.");
|
||||||
}
|
}
|
||||||
@@ -106,7 +132,6 @@ int main(int argc,char *argv[]) { //usage: ./client host port
|
|||||||
their_addr.sin_port = htons(atoi(argv[2]));
|
their_addr.sin_port = htons(atoi(argv[2]));
|
||||||
their_addr.sin_addr.s_addr=inet_addr(argv[1]);
|
their_addr.sin_addr.s_addr=inet_addr(argv[1]);
|
||||||
bzero(&(their_addr.sin_zero), 8);
|
bzero(&(their_addr.sin_zero), 8);
|
||||||
|
|
||||||
while(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)) == -1);
|
while(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)) == -1);
|
||||||
puts("Connected to server");
|
puts("Connected to server");
|
||||||
if(!pthread_create(&thread, NULL, (void*)&getMessage, NULL)) {
|
if(!pthread_create(&thread, NULL, (void*)&getMessage, NULL)) {
|
||||||
@@ -143,57 +168,66 @@ int main(int argc,char *argv[]) { //usage: ./client host port
|
|||||||
else puts("Send file error.");
|
else puts("Send file error.");
|
||||||
#endif
|
#endif
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
printf("Send count:%u\n", len);
|
printf("Send count: %u\n", len);
|
||||||
} else puts("Open file error!");
|
} else puts("Open file error!");
|
||||||
} else if(!strcmp(buf, "2md5")) {
|
|
||||||
uint8_t md5_vals[16];
|
|
||||||
printf("Enter md5 string:");
|
|
||||||
for(int i = 0; i < 16; i++) scanf("%02x", &md5_vals[i]);
|
|
||||||
printf("Read md5:");
|
|
||||||
for(int i = 0; i < 16; i++) printf("%02x", (uint8_t)(md5_vals[i]));
|
|
||||||
putchar('\n');
|
|
||||||
send(sockfd, md5_vals, 16, 0);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
buf[3] = 0;
|
buf[3] = 0;
|
||||||
|
CMDPACKET* p = malloc(CMDPACKET_LEN_MAX);
|
||||||
if(!strcmp(buf, "set")) {
|
if(!strcmp(buf, "set")) {
|
||||||
CMDPACKET* p = malloc(CMDPACKET_HEAD_LEN+strlen(buf+4));
|
|
||||||
p->cmd = CMDSET;
|
p->cmd = CMDSET;
|
||||||
p->datalen = strlen(buf+4);
|
p->datalen = strlen(buf+4);
|
||||||
memcpy(p->data, buf+4, p->datalen);
|
memcpy(p->data, buf+4, p->datalen);
|
||||||
cmdpacket_encrypt(p, 0, "testsps");
|
cmdpacket_encrypt(p, 0, sps);
|
||||||
|
send_cmd(sockfd, p);
|
||||||
|
free(p);
|
||||||
|
} else if(!strcmp(buf, "dat")) {
|
||||||
|
p->cmd = CMDDAT;
|
||||||
|
p->datalen = strlen(buf+4);
|
||||||
|
memcpy(p->data, buf+4, p->datalen);
|
||||||
|
cmdpacket_encrypt(p, 0, sps);
|
||||||
send_cmd(sockfd, p);
|
send_cmd(sockfd, p);
|
||||||
free(p);
|
free(p);
|
||||||
} else if(!strcmp(buf, "get")) {
|
} else if(!strcmp(buf, "get")) {
|
||||||
CMDPACKET* p = malloc(CMDPACKET_HEAD_LEN+strlen(buf+4));
|
|
||||||
p->cmd = CMDGET;
|
p->cmd = CMDGET;
|
||||||
p->datalen = strlen(buf+4);
|
p->datalen = strlen(buf+4);
|
||||||
memcpy(p->data, buf+4, p->datalen);
|
memcpy(p->data, buf+4, p->datalen);
|
||||||
cmdpacket_encrypt(p, 0, "testpwd");
|
cmdpacket_encrypt(p, 0, pwd);
|
||||||
send_cmd(sockfd, p);
|
send_cmd(sockfd, p);
|
||||||
free(p);
|
free(p);
|
||||||
} else if(!strcmp(buf, "cat")) {
|
} else if(!strcmp(buf, "cat")) {
|
||||||
CMDPACKET* p = malloc(CMDPACKET_HEAD_LEN+strlen(buf+4));
|
|
||||||
p->cmd = CMDCAT;
|
p->cmd = CMDCAT;
|
||||||
p->datalen = 4;
|
p->datalen = 4;
|
||||||
memcpy(p->data, "fill", p->datalen);
|
memcpy(p->data, "fill", p->datalen);
|
||||||
cmdpacket_encrypt(p, 0, "testpwd");
|
recv_bin = 1;
|
||||||
|
cmdpacket_encrypt(p, 0, pwd);
|
||||||
send_cmd(sockfd, p);
|
send_cmd(sockfd, p);
|
||||||
free(p);
|
free(p);
|
||||||
} else if(!strcmp(buf, "del")) {
|
} else if(!strcmp(buf, "del")) {
|
||||||
CMDPACKET* p = malloc(CMDPACKET_HEAD_LEN+strlen(buf+4));
|
|
||||||
p->cmd = CMDDEL;
|
p->cmd = CMDDEL;
|
||||||
p->datalen = strlen(buf+4);
|
p->datalen = strlen(buf+4);
|
||||||
memcpy(p->data, buf+4, p->datalen);
|
memcpy(p->data, buf+4, p->datalen);
|
||||||
cmdpacket_encrypt(p, 0, "testsps");
|
cmdpacket_encrypt(p, 0, sps);
|
||||||
send_cmd(sockfd, p);
|
send_cmd(sockfd, p);
|
||||||
free(p);
|
free(p);
|
||||||
|
} else if(!strcmp(buf, "md5")) {
|
||||||
|
if(strlen(buf+4) != 32) puts("md5 len mismatch.");
|
||||||
|
else {
|
||||||
|
p->cmd = CMDMD5;
|
||||||
|
p->datalen = 16;
|
||||||
|
for(int i = 0; i < 16; i++) sscanf(buf+4+i*2, "%02x", &p->data[i]);
|
||||||
|
printf("Read md5:");
|
||||||
|
for(int i = 0; i < 16; i++) printf("%02x", (uint8_t)(p->data[i]));
|
||||||
|
putchar('\n');
|
||||||
|
cmdpacket_encrypt(p, 0, pwd);
|
||||||
|
send_cmd(sockfd, p);
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
} else if(!strcmp(buf, "end")) {
|
} else if(!strcmp(buf, "end")) {
|
||||||
CMDPACKET* p = malloc(CMDPACKET_HEAD_LEN+strlen(buf+4));
|
|
||||||
p->cmd = CMDEND;
|
p->cmd = CMDEND;
|
||||||
p->datalen = 4;
|
p->datalen = 4;
|
||||||
memcpy(p->data, "fill", p->datalen);
|
memcpy(p->data, "fill", p->datalen);
|
||||||
cmdpacket_encrypt(p, 0, "testpwd");
|
cmdpacket_encrypt(p, 0, pwd);
|
||||||
send_cmd(sockfd, p);
|
send_cmd(sockfd, p);
|
||||||
free(p);
|
free(p);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
|||||||
49
crypto.c
49
crypto.c
@@ -3,6 +3,8 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
|
||||||
|
//#define DEBUG
|
||||||
|
|
||||||
// TEA encoding sumtable
|
// TEA encoding sumtable
|
||||||
static const uint32_t sumtable[0x10] = {
|
static const uint32_t sumtable[0x10] = {
|
||||||
0x9e3579b9,
|
0x9e3579b9,
|
||||||
@@ -69,8 +71,10 @@ char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
|||||||
|
|
||||||
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
||||||
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
||||||
((uint8_t*)tea)[15] = seqs[index]++;
|
((uint8_t*)tea)[15] = seqs[index];
|
||||||
TEADAT* tout = tea_decrypt_native_endian(tea, sumtable, &tin);
|
TEADAT* tout = tea_decrypt_native_endian(tea, sumtable, &tin);
|
||||||
|
if(!tout) return NULL;
|
||||||
|
else seqs[index]++;
|
||||||
|
|
||||||
*len = tout->len;
|
*len = tout->len;
|
||||||
char* decbuf = (char*)malloc(*len);
|
char* decbuf = (char*)malloc(*len);
|
||||||
@@ -84,18 +88,40 @@ char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
|||||||
void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
||||||
TEADAT tin = {p->datalen, p->data};
|
TEADAT tin = {p->datalen, p->data};
|
||||||
TEA tea[4];
|
TEA tea[4];
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("encrypt len: %d, data: ", p->datalen);
|
||||||
|
for(int i = 0; i < p->datalen; i++) printf("%02x", p->data[i]);
|
||||||
|
putchar('\n');
|
||||||
|
#endif
|
||||||
|
|
||||||
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
||||||
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
||||||
((uint8_t*)tea)[15] = seqs[index]++;
|
((uint8_t*)tea)[15] = seqs[index]++;
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("encrypt tea: ");
|
||||||
|
for(int i = 0; i < 16; i++) printf("%02x", ((uint8_t*)tea)[i]);
|
||||||
|
putchar('\n');
|
||||||
|
#endif
|
||||||
|
|
||||||
TEADAT* tout = tea_encrypt_native_endian(tea, sumtable, &tin);
|
TEADAT* tout = tea_encrypt_native_endian(tea, sumtable, &tin);
|
||||||
|
|
||||||
uint8_t* datamd5 = md5(p->data, p->datalen);
|
uint8_t* datamd5 = md5(p->data, p->datalen);
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("encrypt md5: ");
|
||||||
|
for(int i = 0; i < 16; i++) printf("%02x", datamd5[i]);
|
||||||
|
putchar('\n');
|
||||||
|
#endif
|
||||||
memcpy(p->md5, datamd5, 16);
|
memcpy(p->md5, datamd5, 16);
|
||||||
free(datamd5);
|
free(datamd5);
|
||||||
|
|
||||||
p->datalen = tout->len;
|
p->datalen = tout->len;
|
||||||
memcpy(p->data, tout->data, p->datalen);
|
memcpy(p->data, tout->data, p->datalen);
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("encrypted data len: %d, data: ", p->datalen);
|
||||||
|
for(int i = 0; i < p->datalen; i++) printf("%02x", p->data[i]);
|
||||||
|
putchar('\n');
|
||||||
|
#endif
|
||||||
free(tout->ptr);
|
free(tout->ptr);
|
||||||
free(tout);
|
free(tout);
|
||||||
|
|
||||||
@@ -105,13 +131,34 @@ void cmdpacket_encrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
|||||||
int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
||||||
TEADAT tin = {p->datalen, p->data};
|
TEADAT tin = {p->datalen, p->data};
|
||||||
TEA tea[4];
|
TEA tea[4];
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("decrypt len: %d, data: ", p->datalen);
|
||||||
|
for(int i = 0; i < p->datalen; i++) printf("%02x", p->data[i]);
|
||||||
|
putchar('\n');
|
||||||
|
#endif
|
||||||
|
|
||||||
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
((uint64_t*)tea)[0] = ((uint64_t*)pwd)[0];
|
||||||
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
((uint64_t*)tea)[1] = ((uint64_t*)pwd)[1];
|
||||||
((uint8_t*)tea)[15] = seqs[index];
|
((uint8_t*)tea)[15] = seqs[index];
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("decrypt tea: ");
|
||||||
|
for(int i = 0; i < 16; i++) printf("%02x", ((uint8_t*)tea)[i]);
|
||||||
|
putchar('\n');
|
||||||
|
#endif
|
||||||
|
|
||||||
TEADAT* tout = tea_decrypt_native_endian(tea, sumtable, &tin);
|
TEADAT* tout = tea_decrypt_native_endian(tea, sumtable, &tin);
|
||||||
|
if(!tout) return 0;
|
||||||
|
|
||||||
uint8_t* datamd5 = md5(tout->data, tout->len);
|
uint8_t* datamd5 = md5(tout->data, tout->len);
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("decrypt md5: ");
|
||||||
|
for(int i = 0; i < 16; i++) printf("%02x", datamd5[i]);
|
||||||
|
putchar('\n');
|
||||||
|
printf("decrypted data len: %d, data: ", tout->len);
|
||||||
|
for(int i = 0; i < tout->len; i++) printf("%02x", tout->data[i]);
|
||||||
|
putchar('\n');
|
||||||
|
#endif
|
||||||
if(is_md5_equal(datamd5, p->md5)) {
|
if(is_md5_equal(datamd5, p->md5)) {
|
||||||
seqs[index]++;
|
seqs[index]++;
|
||||||
p->datalen = tout->len;
|
p->datalen = tout->len;
|
||||||
|
|||||||
59
server.c
59
server.c
@@ -138,11 +138,15 @@ static int send_all(THREADTIMER *timer) {
|
|||||||
char* buf = (char*)malloc(file_size);
|
char* buf = (char*)malloc(file_size);
|
||||||
if(buf) {
|
if(buf) {
|
||||||
if(fread(buf, file_size, 1, fp) == 1) {
|
if(fread(buf, file_size, 1, fp) == 1) {
|
||||||
|
printf("Get dict file size: %zu\n", file_size);
|
||||||
char* encbuf = raw_encrypt(buf, &file_size, timer->index, cfg->pwd);
|
char* encbuf = raw_encrypt(buf, &file_size, timer->index, cfg->pwd);
|
||||||
sprintf(timer->dat, "%zu$", file_size);
|
sprintf(timer->dat, "%zu$", file_size);
|
||||||
printf("Get encrypted file size: %s\n", timer->dat);
|
printf("Get encrypted file size: %s\n", timer->dat);
|
||||||
|
//FILE* fp = fopen("raw_after_enc", "wb+");
|
||||||
|
//fwrite(encbuf, file_size, 1, fp);
|
||||||
|
//fclose(fp);
|
||||||
if(send(timer->accept_fd, timer->dat, strlen(timer->dat), 0) > 0) {
|
if(send(timer->accept_fd, timer->dat, strlen(timer->dat), 0) > 0) {
|
||||||
re = send(timer->accept_fd, encbuf, file_size, 0) > 0;
|
re = send(timer->accept_fd, encbuf, file_size, 0);
|
||||||
printf("Send %u bytes.\n", re);
|
printf("Send %u bytes.\n", re);
|
||||||
close_dict(DICT_LOCK_SH, timer->index);
|
close_dict(DICT_LOCK_SH, timer->index);
|
||||||
} else re = 0;
|
} else re = 0;
|
||||||
@@ -275,14 +279,15 @@ static void accept_timer(void *p) {
|
|||||||
uint32_t index = timer->index;
|
uint32_t index = timer->index;
|
||||||
while(accept_threads[index] && !pthread_kill(accept_threads[index], 0)) {
|
while(accept_threads[index] && !pthread_kill(accept_threads[index], 0)) {
|
||||||
sleep(MAXWAITSEC / 4);
|
sleep(MAXWAITSEC / 4);
|
||||||
puts("Check accept status");
|
time_t waitsec = time(NULL) - timer->touch;
|
||||||
if(time(NULL) - timer->touch > MAXWAITSEC) break;
|
printf("Wait sec: %u, max: %u\n", waitsec, MAXWAITSEC);
|
||||||
|
if(waitsec > MAXWAITSEC) break;
|
||||||
}
|
}
|
||||||
puts("Call kill thread");
|
puts("Call kill thread");
|
||||||
kill_thread(timer);
|
kill_thread(timer);
|
||||||
puts("Free timer");
|
puts("Free timer");
|
||||||
free(timer);
|
free(timer);
|
||||||
puts("Finish calling kill thread");
|
puts("Finish calling kill thread\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void kill_thread(THREADTIMER* timer) {
|
static void kill_thread(THREADTIMER* timer) {
|
||||||
@@ -316,7 +321,7 @@ static void handle_pipe(int signo) {
|
|||||||
static void handle_accept(void *p) {
|
static void handle_accept(void *p) {
|
||||||
int accept_fd = timer_pointer_of(p)->accept_fd;
|
int accept_fd = timer_pointer_of(p)->accept_fd;
|
||||||
if(accept_fd > 0) {
|
if(accept_fd > 0) {
|
||||||
puts("Connected to the client.");
|
puts("\nConnected to the client.");
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) puts("Error creating timer thread");
|
if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) puts("Error creating timer thread");
|
||||||
else puts("Creating timer thread succeeded");
|
else puts("Creating timer thread succeeded");
|
||||||
@@ -328,24 +333,33 @@ static void handle_accept(void *p) {
|
|||||||
timer_pointer_of(p)->ptr = buff;
|
timer_pointer_of(p)->ptr = buff;
|
||||||
CMDPACKET* cp = (CMDPACKET*)buff;
|
CMDPACKET* cp = (CMDPACKET*)buff;
|
||||||
ssize_t numbytes = 0, offset = 0;
|
ssize_t numbytes = 0, offset = 0;
|
||||||
while(accept_threads[index] && (numbytes = recv(accept_fd, buff+offset, CMDPACKET_HEAD_LEN-offset, MSG_WAITALL)) > 0) {
|
while(
|
||||||
|
accept_threads[index]
|
||||||
|
&& (
|
||||||
|
offset >= CMDPACKET_HEAD_LEN
|
||||||
|
|| (numbytes = recv(accept_fd, buff+offset, CMDPACKET_HEAD_LEN-offset, MSG_WAITALL)) > 0
|
||||||
|
)
|
||||||
|
) {
|
||||||
touch_timer(p);
|
touch_timer(p);
|
||||||
offset += numbytes;
|
offset += numbytes;
|
||||||
printf("[normal] Get %zd bytes head.\n", numbytes);
|
printf("[handle] Get %zd bytes, total: %zd.\n", numbytes, offset);
|
||||||
if(offset < CMDPACKET_HEAD_LEN) break;
|
if(offset < CMDPACKET_HEAD_LEN) break;
|
||||||
if(offset < CMDPACKET_HEAD_LEN+cp->datalen) {
|
if(offset < CMDPACKET_HEAD_LEN+cp->datalen) {
|
||||||
numbytes = recv(accept_fd, buff+offset, CMDPACKET_HEAD_LEN+cp->datalen-offset, MSG_WAITALL);
|
numbytes = recv(accept_fd, buff+offset, CMDPACKET_HEAD_LEN+cp->datalen-offset, MSG_WAITALL);
|
||||||
printf("[normal] Get %zd bytes body.\n", numbytes);
|
if(numbytes <= 0) break;
|
||||||
|
else {
|
||||||
|
offset += numbytes;
|
||||||
|
printf("[handle] Get %zd bytes, total: %zd.\n", numbytes, offset);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(numbytes <= 0) break;
|
numbytes = CMDPACKET_HEAD_LEN+cp->datalen; // 暂存 packet len
|
||||||
else offset += numbytes;
|
if(offset < numbytes) break;
|
||||||
if(offset < CMDPACKET_HEAD_LEN+cp->datalen) break;
|
printf("[handle] Decrypt %zd bytes data...\n", cp->datalen);
|
||||||
printf("[normal] Decrypt %zd bytes data...\n", cp->datalen);
|
if(cp->cmd < 5 && cmdpacket_decrypt(cp, index, cfg->pwd)) {
|
||||||
if(cmdpacket_decrypt(cp, index, cfg->pwd)) {
|
|
||||||
cp->data[cp->datalen] = 0;
|
cp->data[cp->datalen] = 0;
|
||||||
timer_pointer_of(p)->dat = (char*)cp->data;
|
timer_pointer_of(p)->dat = (char*)cp->data;
|
||||||
timer_pointer_of(p)->numbytes = cp->datalen;
|
timer_pointer_of(p)->numbytes = cp->datalen;
|
||||||
printf("[normal] Get %zd bytes data: %s\n", offset, cp->data);
|
printf("[normal] Get %zd bytes packet with cmd: %d, data: %s\n", offset, cp->cmd, cp->data);
|
||||||
switch(cp->cmd) {
|
switch(cp->cmd) {
|
||||||
case CMDGET:
|
case CMDGET:
|
||||||
//timer_pointer_of(p)->status = 1;
|
//timer_pointer_of(p)->status = 1;
|
||||||
@@ -366,7 +380,7 @@ static void handle_accept(void *p) {
|
|||||||
cp->data[cp->datalen] = 0;
|
cp->data[cp->datalen] = 0;
|
||||||
timer_pointer_of(p)->dat = (char*)cp->data;
|
timer_pointer_of(p)->dat = (char*)cp->data;
|
||||||
timer_pointer_of(p)->numbytes = cp->datalen;
|
timer_pointer_of(p)->numbytes = cp->datalen;
|
||||||
printf("[super] Get %zd bytes data: %s\n", offset, cp->data);
|
printf("[super] Get %zd bytes packet with data: %s\n", offset, cp->data);
|
||||||
switch(cp->cmd) {
|
switch(cp->cmd) {
|
||||||
case CMDSET:
|
case CMDSET:
|
||||||
//timer_pointer_of(p)->status = 2;
|
//timer_pointer_of(p)->status = 2;
|
||||||
@@ -384,13 +398,15 @@ static void handle_accept(void *p) {
|
|||||||
default: goto CONV_END; break;
|
default: goto CONV_END; break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
puts("decrypt data failed.");
|
puts("Decrypt data failed.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(offset > CMDPACKET_HEAD_LEN+cp->datalen) {
|
if(offset > numbytes) {
|
||||||
offset -= CMDPACKET_HEAD_LEN+cp->datalen;
|
offset -= numbytes;
|
||||||
memmove(buff, buff+CMDPACKET_HEAD_LEN+cp->datalen, offset);
|
memmove(buff, buff+numbytes, offset);
|
||||||
|
numbytes = 0;
|
||||||
} else offset = 0;
|
} else offset = 0;
|
||||||
|
printf("Offset after analyzing packet: %zd\n", offset);
|
||||||
}
|
}
|
||||||
CONV_END: puts("Conversation end\n");
|
CONV_END: puts("Conversation end\n");
|
||||||
} else puts("Error allocating buffer");
|
} else puts("Error allocating buffer");
|
||||||
@@ -418,7 +434,7 @@ static void accept_client() {
|
|||||||
int p = 0;
|
int p = 0;
|
||||||
while(p < THREADCNT && accept_threads[p] && !pthread_kill(accept_threads[p], 0)) p++;
|
while(p < THREADCNT && accept_threads[p] && !pthread_kill(accept_threads[p], 0)) p++;
|
||||||
if(p < THREADCNT) {
|
if(p < THREADCNT) {
|
||||||
printf("Run on thread No.%d\n", p);
|
printf("Next thread is No.%d\n", p);
|
||||||
THREADTIMER *timer = malloc(sizeof(THREADTIMER));
|
THREADTIMER *timer = malloc(sizeof(THREADTIMER));
|
||||||
if(timer) {
|
if(timer) {
|
||||||
timer->accept_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len);
|
timer->accept_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len);
|
||||||
@@ -441,9 +457,8 @@ static void accept_client() {
|
|||||||
timer->index = p;
|
timer->index = p;
|
||||||
timer->touch = time(NULL);
|
timer->touch = time(NULL);
|
||||||
timer->ptr = NULL;
|
timer->ptr = NULL;
|
||||||
puts("reset seq...");
|
|
||||||
reset_seq(p);
|
reset_seq(p);
|
||||||
puts("reset seq succeed");
|
puts("Reset seq succeed");
|
||||||
if (pthread_create(accept_threads + p, &attr, (void *)&handle_accept, timer)) puts("Error creating thread");
|
if (pthread_create(accept_threads + p, &attr, (void *)&handle_accept, timer)) puts("Error creating thread");
|
||||||
else puts("Creating thread succeeded");
|
else puts("Creating thread succeeded");
|
||||||
}
|
}
|
||||||
|
|||||||
2
server.h
2
server.h
@@ -4,7 +4,7 @@
|
|||||||
#define THREADCNT 16
|
#define THREADCNT 16
|
||||||
#define MAXWAITSEC 10
|
#define MAXWAITSEC 10
|
||||||
|
|
||||||
enum SERVERCMD {CMDGET, CMDSET, CMDDEL, CMDCAT, CMDMD5, CMDDAT, CMDACK, CMDEND};
|
enum SERVERCMD {CMDGET, CMDCAT, CMDMD5, CMDACK, CMDEND, CMDSET, CMDDEL, CMDDAT};
|
||||||
|
|
||||||
struct CMDPACKET {
|
struct CMDPACKET {
|
||||||
uint8_t cmd;
|
uint8_t cmd;
|
||||||
|
|||||||
Reference in New Issue
Block a user