diff --git a/cfgwriter.c b/cfgwriter.c index 143cc8b..1cff83f 100644 --- a/cfgwriter.c +++ b/cfgwriter.c @@ -3,17 +3,17 @@ #include #include "config.h" -CONFIG cfg; +config_t cfg; int main() { printf("Enter a password: "); scanf("%s", cfg.pwd); printf("Enter a set password: "); scanf("%s", cfg.sps); - uint32_t* types_len = align_struct(sizeof(CONFIG), 2, cfg.pwd, cfg.sps); + uint32_t* types_len = align_struct(sizeof(config_t), 2, cfg.pwd, cfg.sps); FILE* fp = fopen("cfg.sp", "wb"); if(fp) { - set_pb(fp, types_len, sizeof(CONFIG), &cfg); + set_pb(fp, types_len, sizeof(config_t), &cfg); fclose(fp); puts("Config is saved to cfg.sp."); fp = NULL; @@ -21,8 +21,8 @@ int main() { fp = fopen("cfg.sp", "rb"); if(fp) { SIMPLE_PB* spb = get_pb(fp); - memset(&cfg, 0, sizeof(CONFIG)); - memcpy(&cfg, spb->target, sizeof(CONFIG)); + memset(&cfg, 0, sizeof(config_t)); + memcpy(&cfg, spb->target, sizeof(config_t)); printf("set pwd: %s, sps: %s\n", cfg.pwd, cfg.sps); } else perror("[SPB]"); } else perror("[SPB]"); diff --git a/client.c b/client.c index c2f4270..e84fe0e 100644 --- a/client.c +++ b/client.c @@ -12,7 +12,9 @@ #include #include #include +#include #include "crypto.h" +#include "config.h" #if !__APPLE__ #include @@ -27,8 +29,7 @@ static struct sockaddr_in their_addr; static pthread_t thread; static uint32_t file_size; static int recv_bin = 0; -static char pwd[64] = "fumiama"; -static char sps[64] = "minamoto"; +static config_t conf; void getMessage(void *p) { int c = 0, offset = 0; @@ -68,7 +69,7 @@ void getMessage(void *p) { //fwrite(data, datalen, 1, fp); //fclose(fp); off_t tmp = datalen; - char* newdata = raw_decrypt(data, &tmp, 0, pwd); + char* newdata = raw_decrypt(data, &tmp, 0, conf.pwd); if(newdata) { printf("raw data len after decode: %d\n", (int)tmp); FILE* fp = fopen(savepath, "wb+"); @@ -102,7 +103,7 @@ void getMessage(void *p) { #ifdef DEBUG printf("[handle] Decrypt %d bytes data...\n", (int)cp->datalen); #endif - if(cmdpacket_decrypt(cp, 0, pwd)) { + if(cmdpacket_decrypt(cp, 0, conf.pwd)) { cp->data[cp->datalen] = 0; #ifdef DEBUG printf("[normal] Get %u bytes packet with data: %s\n", offset, cp->data); @@ -138,14 +139,32 @@ void send_cmd(int accept_fd, cmdpacket_t p) { else puts("Send data succeed."); } -int main(int argc,char *argv[]) { //usage: ./client host port +int main(int argc, char *argv[]) { // usage: ./client cfg.sp host port + if(argc != 4) { + puts("usage: cfg.sp host port"); + return 0; + } + FILE* fp = fopen(argv[1], "rb"); + if(fp == NULL) { + fprintf(stderr, "Error opening config file: %s", argv[1]); + perror("fopen"); + return 1; + } + uint8_t buf[8+sizeof(config_t)]; + SIMPLE_PB* spb = read_pb_into(fp, (SIMPLE_PB*)buf); + if(!spb) { + fprintf(stderr, "Error reading config file: %s\n", argv[1]); + return 2; + } + conf = *(config_t*)(spb->target); + fclose(fp); ssize_t numbytes; puts("break!"); while((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1); puts("Get sockfd"); their_addr.sin_family = AF_INET; - their_addr.sin_port = htons(atoi(argv[2])); - their_addr.sin_addr.s_addr=inet_addr(argv[1]); + their_addr.sin_port = htons(atoi(argv[3])); + their_addr.sin_addr.s_addr=inet_addr(argv[2]); bzero(&(their_addr.sin_zero), 8); while(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)) == -1); puts("Connected to server"); @@ -193,21 +212,21 @@ int main(int argc,char *argv[]) { //usage: ./client host port p->cmd = CMDSET; p->datalen = strlen(buf+4); memcpy(p->data, buf+4, p->datalen); - cmdpacket_encrypt(p, 0, sps); + cmdpacket_encrypt(p, 0, conf.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); + cmdpacket_encrypt(p, 0, conf.sps); send_cmd(sockfd, p); free(p); } else if(!strcmp(buf, "get")) { p->cmd = CMDGET; p->datalen = strlen(buf+4); memcpy(p->data, buf+4, p->datalen); - cmdpacket_encrypt(p, 0, pwd); + cmdpacket_encrypt(p, 0, conf.pwd); send_cmd(sockfd, p); free(p); } else if(!strcmp(buf, "cat")) { @@ -215,14 +234,14 @@ int main(int argc,char *argv[]) { //usage: ./client host port p->datalen = 4; memcpy(p->data, "fill", p->datalen); recv_bin = 1; - cmdpacket_encrypt(p, 0, pwd); + cmdpacket_encrypt(p, 0, conf.pwd); send_cmd(sockfd, p); free(p); } else if(!strcmp(buf, "del")) { p->cmd = CMDDEL; p->datalen = strlen(buf+4); memcpy(p->data, buf+4, p->datalen); - cmdpacket_encrypt(p, 0, sps); + cmdpacket_encrypt(p, 0, conf.sps); send_cmd(sockfd, p); free(p); } else if(!strcmp(buf, "md5")) { @@ -234,7 +253,7 @@ int main(int argc,char *argv[]) { //usage: ./client host port printf("Read md5:"); for(int i = 0; i < 16; i++) printf("%02x", (uint8_t)(p->data[i])); putchar('\n'); - cmdpacket_encrypt(p, 0, pwd); + cmdpacket_encrypt(p, 0, conf.pwd); send_cmd(sockfd, p); free(p); } @@ -242,7 +261,7 @@ int main(int argc,char *argv[]) { //usage: ./client host port p->cmd = CMDEND; p->datalen = 4; memcpy(p->data, "fill", p->datalen); - cmdpacket_encrypt(p, 0, pwd); + cmdpacket_encrypt(p, 0, conf.pwd); send_cmd(sockfd, p); free(p); exit(EXIT_SUCCESS); diff --git a/config.h b/config.h index 56dd421..8496a57 100644 --- a/config.h +++ b/config.h @@ -1,10 +1,10 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ -struct CONFIG { +struct config_t { char pwd[64]; //password char sps[64]; //set password }; -typedef struct CONFIG CONFIG; +typedef struct config_t config_t; #endif \ No newline at end of file diff --git a/dict.h b/dict.h index aacd790..30ebd65 100644 --- a/dict.h +++ b/dict.h @@ -106,7 +106,7 @@ static inline FILE* open_dict(uint32_t index, int isro) { } static inline int require_shared_lock() { - if(pthread_rwlock_tryrdlock(&mu)) { + if(pthread_rwlock_rdlock(&mu)) { perror("Open dict: Readlock busy"); return 1; } diff --git a/migrate.c b/migrate.c index 65d80de..64d0663 100644 --- a/migrate.c +++ b/migrate.c @@ -5,11 +5,11 @@ #include "old_dict.h" DICTBLK dict; -DICT d; +dict_t d; int main(int argc, char** argv) { if(argc == 3) { - uint32_t* items_len = align_struct(sizeof(DICT), 2, d.key, d.data); + uint32_t* items_len = align_struct(sizeof(dict_t), 2, d.key, d.data); FILE* old = fopen(argv[1], "rb"); FILE* new = fopen(argv[2], "wb"); if(old && new) { @@ -18,10 +18,10 @@ int main(int argc, char** argv) { dict.key[ks] = 0; uint8_t ds = dict.datasize; dict.data[ds] = 0; - memset(&d, 0, sizeof(DICT)); + memset(&d, 0, sizeof(dict_t)); memcpy(d.key, dict.key, ks); memcpy(d.data, dict.data, ds); - set_pb(new, items_len, sizeof(DICT), &d); + set_pb(new, items_len, sizeof(dict_t), &d); } fclose(old); fclose(new); diff --git a/server.c b/server.c index 953033b..3e5f3f9 100644 --- a/server.c +++ b/server.c @@ -46,7 +46,7 @@ static thread_timer_t timers[THREADCNT]; static dict_t setdicts[THREADCNT]; static uint32_t* items_len; -static CONFIG cfg; +static config_t cfg; static pthread_attr_t attr; #define DICTPOOLSZ (((uint32_t)-1)>>((sizeof(uint32_t)*8-DICTPOOLBIT))) @@ -827,13 +827,13 @@ int main(int argc, char *argv[]) { return 7; } if(~((int)fp)) { - uint8_t buf[8+sizeof(CONFIG)]; + uint8_t buf[8+sizeof(config_t)]; SIMPLE_PB* spb = read_pb_into(fp, (SIMPLE_PB*)buf); if(!spb) { fprintf(stderr, "Error reading config file: %s\n", argv[as_daemon?4:3]); return 8; } - cfg = *(CONFIG*)(spb->target); + cfg = *(config_t*)(spb->target); fclose(fp); } items_len = align_struct(sizeof(dict_t), 2, setdicts[0].key, setdicts[0].data); diff --git a/server.h b/server.h index 7e0c2c1..7ea25db 100644 --- a/server.h +++ b/server.h @@ -3,8 +3,8 @@ #include -#define THREADCNT 16 -#define MAXWAITSEC 4 +#define THREADCNT 32 +#define MAXWAITSEC 8 // DICTPOOLBIT must be lower than 4*8 = 32 #define DICTPOOLBIT 16