mirror of
https://github.com/fumiama/simple-dict.git
synced 2026-06-05 02:00:25 +08:00
fix
This commit is contained in:
@@ -3,6 +3,7 @@ project(simple-dict-server C)
|
||||
SET(CMAKE_BUILD_TYPE "Release")
|
||||
|
||||
add_definitions(-DLISTEN_ON_IPV6)
|
||||
# add_definitions(-DDEBUG)
|
||||
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
add_definitions("-DCPUBIT64")
|
||||
ELSE()
|
||||
|
||||
42
client.c
42
client.c
@@ -33,7 +33,9 @@ void getMessage(void *p) {
|
||||
int c = 0, offset = 0;
|
||||
CMDPACKET* cp = (CMDPACKET*)bufr;
|
||||
while(offset >= CMDPACKET_HEAD_LEN || (c = recv(sockfd, bufr+offset, CMDPACKET_HEAD_LEN-offset, MSG_WAITALL)) > 0) {
|
||||
//printf("Recv %d bytes.\n", c);
|
||||
#ifdef DEBUG
|
||||
printf("Recv %d bytes.\n", c);
|
||||
#endif
|
||||
if(recv_bin) {
|
||||
if(~recv_bin) {
|
||||
recv_bin = -1;
|
||||
@@ -48,12 +50,16 @@ void getMessage(void *p) {
|
||||
bufr[i] = 0;
|
||||
off_t datalen;
|
||||
sscanf(bufr, "%d", &datalen);
|
||||
//printf("raw data len: %d\n", datalen);
|
||||
#ifdef DEBUG
|
||||
printf("raw data len: %d\n", datalen);
|
||||
#endif
|
||||
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);
|
||||
#ifdef DEBUG
|
||||
printf("copy %d bytes data that had been received.\n", offset);
|
||||
#endif
|
||||
}
|
||||
else offset = 0;
|
||||
if(datalen-offset == recv(sockfd, data+offset, datalen-offset, MSG_WAITALL)) {
|
||||
@@ -75,22 +81,30 @@ void getMessage(void *p) {
|
||||
}
|
||||
} else {
|
||||
offset += c;
|
||||
//printf("[handle] Get %zd bytes, total: %zd.\n", c, offset);
|
||||
#ifdef DEBUG
|
||||
printf("[handle] Get %zd bytes, total: %zd.\n", c, offset);
|
||||
#endif
|
||||
if(offset < CMDPACKET_HEAD_LEN) break;
|
||||
if(offset < CMDPACKET_HEAD_LEN+cp->datalen) {
|
||||
c = recv(sockfd, bufr+offset, CMDPACKET_HEAD_LEN+cp->datalen-offset, MSG_WAITALL);
|
||||
if(c <= 0) break;
|
||||
else {
|
||||
offset += c;
|
||||
//printf("[handle] Get %zd bytes, total: %zd.\n", c, offset);
|
||||
#ifdef DEBUG
|
||||
printf("[handle] Get %zd bytes, total: %zd.\n", c, offset);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
c = CMDPACKET_HEAD_LEN+cp->datalen; // 暂存 packet len
|
||||
if(offset < c) break;
|
||||
//printf("[handle] Decrypt %zd bytes data...\n", cp->datalen);
|
||||
#ifdef DEBUG
|
||||
printf("[handle] Decrypt %zd bytes data...\n", cp->datalen);
|
||||
#endif
|
||||
if(cmdpacket_decrypt(cp, 0, pwd)) {
|
||||
cp->data[cp->datalen] = 0;
|
||||
//printf("[normal] Get %u bytes packet with data: %s\n", offset, cp->data);
|
||||
#ifdef DEBUG
|
||||
printf("[normal] Get %u bytes packet with data: %s\n", offset, cp->data);
|
||||
#endif
|
||||
switch(cp->cmd) {
|
||||
case CMDACK:
|
||||
printf("recv ack: %s\n", cp->data);
|
||||
@@ -103,7 +117,9 @@ void getMessage(void *p) {
|
||||
memmove(bufr, bufr+c, offset);
|
||||
c = 0;
|
||||
} else offset = 0;
|
||||
//printf("offset after analyzing packet: %zd\n", offset);
|
||||
#ifdef DEBUG
|
||||
printf("offset after analyzing packet: %zd\n", offset);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,10 +131,12 @@ off_t file_size_of(const char* fname) {
|
||||
}
|
||||
|
||||
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("raw packet: ");
|
||||
//for(int i = 0; i < CMDPACKET_HEAD_LEN+p->datalen; i++) printf("%02x", ((uint8_t*)p)[i]);
|
||||
//putchar('\n');
|
||||
#ifdef DEBUG
|
||||
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');
|
||||
#endif
|
||||
if(!~send(accept_fd, (void*)p, CMDPACKET_HEAD_LEN+p->datalen, 0)) puts("Send data error.");
|
||||
else puts("Send data succeed.");
|
||||
}
|
||||
|
||||
13
crypto.c
13
crypto.c
@@ -3,8 +3,6 @@
|
||||
#include <time.h>
|
||||
#include "crypto.h"
|
||||
|
||||
//#define DEBUG
|
||||
|
||||
// TEA encoding sumtable
|
||||
static const uint32_t sumtable[0x10] = {
|
||||
0x9e3579b9,
|
||||
@@ -74,7 +72,11 @@ char* raw_decrypt(const char* buf, off_t* len, int index, const char pwd[64]) {
|
||||
((uint8_t*)tea)[15] = seqs[index];
|
||||
TEADAT* tout = tea_decrypt_native_endian(tea, sumtable, &tin);
|
||||
if(!tout) return NULL;
|
||||
else seqs[index]++;
|
||||
else if(tout->len <= 0) {
|
||||
free(tout->ptr);
|
||||
free(tout);
|
||||
return NULL;
|
||||
} else seqs[index]++;
|
||||
|
||||
*len = tout->len;
|
||||
char* decbuf = (char*)malloc(*len);
|
||||
@@ -149,6 +151,11 @@ int cmdpacket_decrypt(CMDPACKET* p, int index, const char pwd[64]) {
|
||||
|
||||
TEADAT* tout = tea_decrypt_native_endian(tea, sumtable, &tin);
|
||||
if(!tout) return 0;
|
||||
if(tout->len <= 0) {
|
||||
free(tout->ptr);
|
||||
free(tout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t* datamd5 = md5(tout->data, tout->len);
|
||||
#ifdef DEBUG
|
||||
|
||||
118
server.c
118
server.c
@@ -138,7 +138,9 @@ static int send_all(THREADTIMER *timer) {
|
||||
char* buf = (char*)malloc(file_size);
|
||||
if(buf) {
|
||||
if(fread(buf, file_size, 1, fp) == 1) {
|
||||
//printf("Get dict file size: %zu\n", file_size);
|
||||
#ifdef DEBUG
|
||||
printf("Get dict file size: %zu\n", file_size);
|
||||
#endif
|
||||
char* encbuf = raw_encrypt(buf, &file_size, timer->index, cfg->pwd);
|
||||
sprintf(timer->dat, "%zu$", file_size);
|
||||
//printf("Get encrypted file size: %s\n", timer->dat);
|
||||
@@ -198,9 +200,10 @@ static int s2_set(THREADTIMER *timer) {
|
||||
static int s3_set_data(THREADTIMER *timer) {
|
||||
//timer->status = 0;
|
||||
uint32_t datasize = (timer->numbytes > (DICTDATSZ-1))?(DICTDATSZ-1):timer->numbytes;
|
||||
//printf("Set data size: %u\n", datasize);
|
||||
#ifdef DEBUG
|
||||
printf("Set data size: %u\n", datasize);
|
||||
#endif
|
||||
memcpy(d.data, timer->dat, datasize);
|
||||
puts("Data copy to dict succ");
|
||||
if(!set_pb(get_dict_fp(timer->index), items_len, sizeof(DICT), &d)) {
|
||||
printf("Error set data: dict[%s]=%s\n", d.key, timer->dat);
|
||||
return close_and_send(timer, "erro", 4);
|
||||
@@ -234,7 +237,9 @@ static int s4_del(THREADTIMER *timer) {
|
||||
}
|
||||
} else {
|
||||
uint32_t cap = end - next;
|
||||
//printf("this: %u, next: %u, end: %u, cap: %u\n", this, next, end, cap);
|
||||
#ifdef DEBUG
|
||||
printf("this: %u, next: %u, end: %u, cap: %u\n", this, next, end, cap);
|
||||
#endif
|
||||
char* data = malloc(cap);
|
||||
if(data) {
|
||||
fseek(fp, next, SEEK_SET);
|
||||
@@ -342,63 +347,79 @@ static void handle_accept(void *p) {
|
||||
) {
|
||||
touch_timer(p);
|
||||
offset += numbytes;
|
||||
//printf("[handle] Get %zd bytes, total: %zd.\n", numbytes, offset);
|
||||
#ifdef DEBUG
|
||||
printf("[handle] Get %zd bytes, total: %zd.\n", numbytes, offset);
|
||||
#endif
|
||||
if(offset < CMDPACKET_HEAD_LEN) break;
|
||||
if(offset < CMDPACKET_HEAD_LEN+cp->datalen) {
|
||||
numbytes = recv(accept_fd, buff+offset, CMDPACKET_HEAD_LEN+cp->datalen-offset, MSG_WAITALL);
|
||||
if(numbytes <= 0) break;
|
||||
else {
|
||||
offset += numbytes;
|
||||
//printf("[handle] Get %zd bytes, total: %zd.\n", numbytes, offset);
|
||||
#ifdef DEBUG
|
||||
printf("[handle] Get %zd bytes, total: %zd.\n", numbytes, offset);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
numbytes = CMDPACKET_HEAD_LEN+cp->datalen; // 暂存 packet len
|
||||
if(offset < numbytes) break;
|
||||
//printf("[handle] Decrypt %zd bytes data...\n", cp->datalen);
|
||||
if(cp->cmd < 5 && cmdpacket_decrypt(cp, index, cfg->pwd)) {
|
||||
cp->data[cp->datalen] = 0;
|
||||
timer_pointer_of(p)->dat = (char*)cp->data;
|
||||
timer_pointer_of(p)->numbytes = cp->datalen;
|
||||
printf("[normal] Get %zd bytes packet with cmd: %d, data: %s\n", offset, cp->cmd, cp->data);
|
||||
switch(cp->cmd) {
|
||||
case CMDGET:
|
||||
//timer_pointer_of(p)->status = 1;
|
||||
if(!s1_get(timer_pointer_of(p))) goto CONV_END;
|
||||
#ifdef DEBUG
|
||||
printf("[handle] Decrypt %zd bytes data...\n", cp->datalen);
|
||||
#endif
|
||||
if(cp->cmd < 5) {
|
||||
if(cmdpacket_decrypt(cp, index, cfg->pwd)) {
|
||||
cp->data[cp->datalen] = 0;
|
||||
timer_pointer_of(p)->dat = (char*)cp->data;
|
||||
timer_pointer_of(p)->numbytes = cp->datalen;
|
||||
printf("[normal] Get %zd bytes packet with cmd: %d, data: %s\n", offset, cp->cmd, cp->data);
|
||||
switch(cp->cmd) {
|
||||
case CMDGET:
|
||||
//timer_pointer_of(p)->status = 1;
|
||||
if(!s1_get(timer_pointer_of(p))) goto CONV_END;
|
||||
break;
|
||||
case CMDCAT:
|
||||
if(!send_all(timer_pointer_of(p))) goto CONV_END;
|
||||
break;
|
||||
case CMDMD5:
|
||||
//timer_pointer_of(p)->status = 5;
|
||||
if(!s5_md5(timer_pointer_of(p))) goto CONV_END;
|
||||
break;
|
||||
case CMDACK: break;
|
||||
case CMDEND:
|
||||
default: goto CONV_END; break;
|
||||
}
|
||||
} else {
|
||||
puts("Decrypt normal data failed.");
|
||||
break;
|
||||
case CMDCAT:
|
||||
if(!send_all(timer_pointer_of(p))) goto CONV_END;
|
||||
break;
|
||||
case CMDMD5:
|
||||
//timer_pointer_of(p)->status = 5;
|
||||
if(!s5_md5(timer_pointer_of(p))) goto CONV_END;
|
||||
break;
|
||||
case CMDACK: break;
|
||||
case CMDEND:
|
||||
default: goto CONV_END; break;
|
||||
}
|
||||
} else if(cmdpacket_decrypt(cp, index, cfg->sps)) {
|
||||
cp->data[cp->datalen] = 0;
|
||||
timer_pointer_of(p)->dat = (char*)cp->data;
|
||||
timer_pointer_of(p)->numbytes = cp->datalen;
|
||||
printf("[super] Get %zd bytes packet with data: %s\n", offset, cp->data);
|
||||
switch(cp->cmd) {
|
||||
case CMDSET:
|
||||
//timer_pointer_of(p)->status = 2;
|
||||
if(!s2_set(timer_pointer_of(p))) goto CONV_END;
|
||||
} else if(cp->cmd < 8) {
|
||||
if(cmdpacket_decrypt(cp, index, cfg->sps)) {
|
||||
cp->data[cp->datalen] = 0;
|
||||
timer_pointer_of(p)->dat = (char*)cp->data;
|
||||
timer_pointer_of(p)->numbytes = cp->datalen;
|
||||
printf("[super] Get %zd bytes packet with data: %s\n", offset, cp->data);
|
||||
switch(cp->cmd) {
|
||||
case CMDSET:
|
||||
//timer_pointer_of(p)->status = 2;
|
||||
if(!s2_set(timer_pointer_of(p))) goto CONV_END;
|
||||
break;
|
||||
case CMDDEL:
|
||||
//timer_pointer_of(p)->status = 4;
|
||||
if(!s4_del(timer_pointer_of(p))) goto CONV_END;
|
||||
break;
|
||||
case CMDDAT:
|
||||
if(timer_pointer_of(p)->lock_type == DICT_LOCK_EX) {
|
||||
if(!s3_set_data(timer_pointer_of(p))) goto CONV_END;
|
||||
}
|
||||
break;
|
||||
default: goto CONV_END; break;
|
||||
}
|
||||
} else {
|
||||
puts("Decrypt super data failed.");
|
||||
break;
|
||||
case CMDDEL:
|
||||
//timer_pointer_of(p)->status = 4;
|
||||
if(!s4_del(timer_pointer_of(p))) goto CONV_END;
|
||||
break;
|
||||
case CMDDAT:
|
||||
if(timer_pointer_of(p)->lock_type == DICT_LOCK_EX) {
|
||||
if(!s3_set_data(timer_pointer_of(p))) goto CONV_END;
|
||||
}
|
||||
break;
|
||||
default: goto CONV_END; break;
|
||||
}
|
||||
} else {
|
||||
puts("Decrypt data failed.");
|
||||
puts("Invalid command.");
|
||||
break;
|
||||
}
|
||||
if(offset > numbytes) {
|
||||
@@ -406,7 +427,9 @@ static void handle_accept(void *p) {
|
||||
memmove(buff, buff+numbytes, offset);
|
||||
numbytes = 0;
|
||||
} else offset = 0;
|
||||
//printf("Offset after analyzing packet: %zd\n", offset);
|
||||
#ifdef DEBUG
|
||||
printf("Offset after analyzing packet: %zd\n", offset);
|
||||
#endif
|
||||
}
|
||||
CONV_END: puts("Conversation end\n");
|
||||
} else puts("Error allocating buffer");
|
||||
@@ -458,7 +481,6 @@ static void accept_client() {
|
||||
timer->touch = time(NULL);
|
||||
timer->ptr = NULL;
|
||||
reset_seq(p);
|
||||
//puts("Reset seq succeed");
|
||||
if (pthread_create(accept_threads + p, &attr, (void *)&handle_accept, timer)) puts("Error creating thread");
|
||||
else puts("Creating thread succeeded");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user