1
0
mirror of https://github.com/fumiama/simple-dict.git synced 2026-06-21 11:20:34 +08:00

update README

This commit is contained in:
fumiama
2021-12-12 15:05:14 +08:00
parent 53f46ac1d3
commit 8e9cb10297
3 changed files with 135 additions and 24 deletions

115
README.md
View File

@@ -28,13 +28,124 @@ Start server on localhost using the commands below.
chmod +x simple-dict-server simple-dict-client chmod +x simple-dict-server simple-dict-client
./simple-dict-server -d 7777 1 ./dict.sp ./cfg.sp # use -d to start as daemon ./simple-dict-server -d 7777 1 ./dict.sp ./cfg.sp # use -d to start as daemon
``` ```
`cfg.sp`is generated by `cfgwriter`.
Open another shell to connect to it. Open another shell to connect to it.
```bash ```bash
./simple-dict-client 127.0.0.1 7777 ./simple-dict-client 127.0.0.1 7777
``` ```
Now you have connected to the server. Type `fumiama` and press enter in `10` seconds to get the read/write access. You can modify the password in source code as you like. Please note that the server will only wait `10` seconds for a response after the last communication. The box below shows how to control the server to accompilsh basic add/del/find/edit operations. Now you have connected to the server. The default access passwords is in `client.c` and you can modify the password in source code as you like. Please note that the server will only wait `10` seconds for a response after the last communication. The box below shows how to control the server to accompilsh basic add/del/find/edit operations.
The raw data starts with an integer showing its size, then a char `$` follows, finally following all binary data in `./dict.sp`. | cmd | data | description |
| ---- | ---- | ---- |
| get: | key | get key value |
| cat: | filename | save raw dict.sp into filename |
| md5: | md5 str | compare whether md5 of dict.sp is what given in data |
| end | no data | end conversation |
| set: | key | set key |
| dat: | value to set | give value to the key |
| del: | key | del key |
The raw data starts with an integer showing its size, then a char `$` follows, finally following all binary data in `./dict.sp` encoded by `TEA`.
A cmd sequence example is as below
```bash
get:test
set:test
dat:测试
get:test
del:test
get:test
end
```
The client output is
```bash
break!
Get sockfd
Connected to server
Thread create succeeded
Enter command:get:test
Send data succeed.
recv ack: null
Enter command:set:test
Send data succeed.
recv ack: data
Enter command:dat:测试
Send data succeed.
recv ack: succ
Enter command:get:test
Send data succeed.
recv ack: 测试
Enter command:del:test
Send data succeed.
recv ack: succ
Enter command:get:test
Send data succeed.
recv ack: null
Enter command:end
Send data succeed.
```
The server output meanwhile is
```bash
Bind server success!
Listening....
Ready for accept, waitting...
Next thread is No.0
Accept client ::ffff:127.0.0.1:51296
Creating thread succeeded
Ready for accept, waitting...
Connected to the client.
Next thread is No.1
Creating timer thread succeeded
Wait sec: 2, max: 10
[normal] Get 34 bytes packet with cmd: 0, data: test
Close dict
Send data: null
Wait sec: 0, max: 10
Wait sec: 2, max: 10
Wait sec: 4, max: 10
[super] Get 34 bytes packet with data: test
Send data: data
Wait sec: 1, max: 10
Wait sec: 3, max: 10
[super] Get 34 bytes packet with data: 测试
Data copy to dict succ
Set data: dict[test]=测试
Close dict
Send data: succ
Wait sec: 0, max: 10
Wait sec: 2, max: 10
[normal] Get 34 bytes packet with cmd: 0, data: test
Close dict
Send data: 测试
Wait sec: 0, max: 10
Wait sec: 2, max: 10
[super] Get 34 bytes packet with data: test
Close dict
Send data: succ
Wait sec: 0, max: 10
Wait sec: 2, max: 10
[normal] Get 34 bytes packet with cmd: 0, data: test
Close dict
Send data: null
Wait sec: 0, max: 10
[normal] Get 34 bytes packet with cmd: 4, data: fill
Conversation end
Start killing.
Close accept.
Free data.
Close dict
Finish killing.
Wait sec: 0, max: 10
Call kill thread
Start killing.
Close dict
Finish killing.
Free timer
Finish calling kill thread
```
# Android Client for simple-dict-server # Android Client for simple-dict-server
There is also an [Android Client](https://github.com/fumiama/simple-dict-android) for simple-dict-server. Just install the apk file downloaded from release page and click `config` icon to set your server address using the format There is also an [Android Client](https://github.com/fumiama/simple-dict-android) for simple-dict-server. Just install the apk file downloaded from release page and click `config` icon to set your server address using the format

View File

@@ -33,7 +33,7 @@ void getMessage(void *p) {
int c = 0, offset = 0; int c = 0, offset = 0;
CMDPACKET* cp = (CMDPACKET*)bufr; CMDPACKET* cp = (CMDPACKET*)bufr;
while(offset >= CMDPACKET_HEAD_LEN || (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.\n", c); //printf("Recv %d bytes.\n", c);
if(recv_bin) { if(recv_bin) {
if(~recv_bin) { if(~recv_bin) {
recv_bin = -1; recv_bin = -1;
@@ -48,12 +48,12 @@ void getMessage(void *p) {
bufr[i] = 0; bufr[i] = 0;
off_t datalen; off_t datalen;
sscanf(bufr, "%d", &datalen); sscanf(bufr, "%d", &datalen);
printf("raw data len: %d\n", datalen); //printf("raw data len: %d\n", datalen);
char* data = malloc(datalen); char* data = malloc(datalen);
offset = c - ++i; offset = c - ++i;
if(offset > 0) { if(offset > 0) {
memcpy(data, bufr+i, offset); memcpy(data, bufr+i, offset);
printf("copy %d bytes data that had been received.\n", offset); //printf("copy %d bytes data that had been received.\n", offset);
} }
else offset = 0; else offset = 0;
if(datalen-offset == recv(sockfd, data+offset, datalen-offset, MSG_WAITALL)) { if(datalen-offset == recv(sockfd, data+offset, datalen-offset, MSG_WAITALL)) {
@@ -75,22 +75,22 @@ void getMessage(void *p) {
} }
} else { } else {
offset += c; offset += c;
printf("[handle] Get %zd bytes, total: %zd.\n", c, offset); //printf("[handle] Get %zd bytes, total: %zd.\n", c, 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) {
c = recv(sockfd, bufr+offset, CMDPACKET_HEAD_LEN+cp->datalen-offset, MSG_WAITALL); c = recv(sockfd, bufr+offset, CMDPACKET_HEAD_LEN+cp->datalen-offset, MSG_WAITALL);
if(c <= 0) break; if(c <= 0) break;
else { else {
offset += c; offset += c;
printf("[handle] Get %zd bytes, total: %zd.\n", c, offset); //printf("[handle] Get %zd bytes, total: %zd.\n", c, offset);
} }
} }
c = CMDPACKET_HEAD_LEN+cp->datalen; // 暂存 packet len c = CMDPACKET_HEAD_LEN+cp->datalen; // 暂存 packet len
if(offset < c) break; if(offset < c) break;
printf("[handle] Decrypt %zd bytes data...\n", cp->datalen); //printf("[handle] Decrypt %zd bytes data...\n", cp->datalen);
if(cmdpacket_decrypt(cp, 0, pwd)) { if(cmdpacket_decrypt(cp, 0, pwd)) {
cp->data[cp->datalen] = 0; cp->data[cp->datalen] = 0;
printf("[normal] Get %u bytes packet with 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);
@@ -103,7 +103,7 @@ void getMessage(void *p) {
memmove(bufr, bufr+c, offset); memmove(bufr, bufr+c, offset);
c = 0; c = 0;
} else offset = 0; } else offset = 0;
printf("offset after analyzing packet: %zd\n", offset); //printf("offset after analyzing packet: %zd\n", offset);
} }
} }
} }
@@ -115,11 +115,11 @@ 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: "); //printf("raw packet: ");
for(int i = 0; i < CMDPACKET_HEAD_LEN+p->datalen; i++) printf("%02x", ((uint8_t*)p)[i]); //for(int i = 0; i < CMDPACKET_HEAD_LEN+p->datalen; i++) printf("%02x", ((uint8_t*)p)[i]);
putchar('\n'); //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.");
} }

View File

@@ -138,10 +138,10 @@ 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); //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+"); //FILE* fp = fopen("raw_after_enc", "wb+");
//fwrite(encbuf, file_size, 1, fp); //fwrite(encbuf, file_size, 1, fp);
//fclose(fp); //fclose(fp);
@@ -198,7 +198,7 @@ static int s2_set(THREADTIMER *timer) {
static int s3_set_data(THREADTIMER *timer) { static int s3_set_data(THREADTIMER *timer) {
//timer->status = 0; //timer->status = 0;
uint32_t datasize = (timer->numbytes > (DICTDATSZ-1))?(DICTDATSZ-1):timer->numbytes; uint32_t datasize = (timer->numbytes > (DICTDATSZ-1))?(DICTDATSZ-1):timer->numbytes;
printf("Set data size: %u\n", datasize); //printf("Set data size: %u\n", datasize);
memcpy(d.data, timer->dat, datasize); memcpy(d.data, timer->dat, datasize);
puts("Data copy to dict succ"); puts("Data copy to dict succ");
if(!set_pb(get_dict_fp(timer->index), items_len, sizeof(DICT), &d)) { if(!set_pb(get_dict_fp(timer->index), items_len, sizeof(DICT), &d)) {
@@ -234,7 +234,7 @@ static int s4_del(THREADTIMER *timer) {
} }
} else { } else {
uint32_t cap = end - next; uint32_t cap = end - next;
printf("this: %u, next: %u, end: %u, cap: %u\n", this, next, end, cap); //printf("this: %u, next: %u, end: %u, cap: %u\n", this, next, end, cap);
char* data = malloc(cap); char* data = malloc(cap);
if(data) { if(data) {
fseek(fp, next, SEEK_SET); fseek(fp, next, SEEK_SET);
@@ -342,19 +342,19 @@ static void handle_accept(void *p) {
) { ) {
touch_timer(p); touch_timer(p);
offset += numbytes; offset += numbytes;
printf("[handle] Get %zd bytes, total: %zd.\n", numbytes, offset); //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);
if(numbytes <= 0) break; if(numbytes <= 0) break;
else { else {
offset += numbytes; offset += numbytes;
printf("[handle] Get %zd bytes, total: %zd.\n", numbytes, offset); //printf("[handle] Get %zd bytes, total: %zd.\n", numbytes, offset);
} }
} }
numbytes = CMDPACKET_HEAD_LEN+cp->datalen; // 暂存 packet len numbytes = CMDPACKET_HEAD_LEN+cp->datalen; // 暂存 packet len
if(offset < numbytes) break; if(offset < numbytes) break;
printf("[handle] Decrypt %zd bytes data...\n", cp->datalen); //printf("[handle] Decrypt %zd bytes data...\n", cp->datalen);
if(cp->cmd < 5 && cmdpacket_decrypt(cp, index, cfg->pwd)) { if(cp->cmd < 5 && 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;
@@ -406,7 +406,7 @@ static void handle_accept(void *p) {
memmove(buff, buff+numbytes, offset); memmove(buff, buff+numbytes, offset);
numbytes = 0; numbytes = 0;
} else offset = 0; } else offset = 0;
printf("Offset after analyzing packet: %zd\n", offset); //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");
@@ -458,7 +458,7 @@ static void accept_client() {
timer->touch = time(NULL); timer->touch = time(NULL);
timer->ptr = NULL; timer->ptr = NULL;
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");
} }