diff --git a/CMakeLists.txt b/CMakeLists.txt index 157b4ef..75c634d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,11 @@ project(simple-dict-server C) SET(CMAKE_BUILD_TYPE "Release") add_definitions(-DLISTEN_ON_IPV6) +IF(CMAKE_SIZEOF_VOID_P EQUAL 8) + add_definitions("-DCPUBIT64") +ELSE() + add_definitions("-DCPUBIT32") +ENDIF() include_directories("/usr/local/include") link_directories("/usr/local/lib") @@ -14,6 +19,7 @@ add_executable(simple-dict-client client.c) add_executable(migrate migrate.c) add_executable(cfgwriter cfgwriter.c) +target_link_libraries(dict smd5) target_link_libraries(simple-dict-server dict spb pthread) target_link_libraries(simple-dict-client pthread) target_link_libraries(migrate spb) diff --git a/client.c b/client.c index b90aac7..8a9a985 100644 --- a/client.c +++ b/client.c @@ -92,7 +92,16 @@ int main(int argc,char *argv[]) { //usage: ./client host port fclose(fp); printf("Send count:%u\n", len); } else puts("Open file error!"); - } else { + } 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 { send(sockfd, buf, strlen(buf), 0); if(!strcmp(buf, "quit")) exit(EXIT_SUCCESS); } diff --git a/dict.c b/dict.c index 1edb80c..a78ecf7 100644 --- a/dict.c +++ b/dict.c @@ -1,10 +1,15 @@ #include #include +#include +#include #include "dict.h" static FILE *fp = NULL; static int lock = 0; static char* filepath; +static uint8_t* dict_md5; +#define _dict_md5_4 ((uint32_t*)dict_md5) +#define _dict_md5_2 ((uint64_t*)dict_md5) uint32_t last_nonnull(char* p, uint32_t max_size) { if(max_size > 1) while(!p[max_size - 1]) max_size--; @@ -16,7 +21,23 @@ int init_dict(char* file_path) { if(fp) { lock = LOCK_UN; filepath = file_path; - return 1; + size_t size = get_dict_size(); + uint8_t* dict_buff = (uint8_t*)malloc(size); + if(dict_buff) { + if(fread(dict_buff, size, 1, fp) == 1) { + if(dict_md5) free(dict_md5); + dict_md5 = md5(dict_buff, size); + free(dict_buff); + return 1; + } else { + free(dict_buff); + puts("Read dict error"); + return 0; + } + } else { + puts("Allocate memory error"); + return 0; + } } else { puts("Open dict error"); return 0; @@ -48,3 +69,17 @@ off_t get_dict_size() { if(stat(filepath, &statbuf)==0) return statbuf.st_size; else return -1; } + +int is_md5_equal(uint8_t* digest) { + #ifdef CPUBIT64 + uint64_t* digest2 = (uint64_t*)digest; + return (digest2[0] == _dict_md5_2[0]) && + (digest2[1] == _dict_md5_2[1]); + #else + uint32_t* digest4 = (uint32_t*)digest; + return (digest4[0] == _dict_md5_4[0]) && + (digest4[1] == _dict_md5_4[1]) && + (digest4[2] == _dict_md5_4[2]) && + (digest4[3] == _dict_md5_4[3]); + #endif +} diff --git a/dict.h b/dict.h index 737029e..ee116c1 100644 --- a/dict.h +++ b/dict.h @@ -20,5 +20,6 @@ int init_dict(char* file_path); FILE *open_dict(int lock_type); void close_dict(); off_t get_dict_size(); +int is_md5_equal(uint8_t* digest); #endif \ No newline at end of file diff --git a/server.c b/server.c index a1c617a..23a8f13 100644 --- a/server.c +++ b/server.c @@ -72,6 +72,7 @@ int s1_get(THREADTIMER *timer); int s2_set(THREADTIMER *timer); int s3_set_data(THREADTIMER *timer); int s4_del(THREADTIMER *timer); +int s5_md5(THREADTIMER *timer); int bind_server(uint16_t port, u_int try_times) { int fail_count = 0; @@ -168,6 +169,7 @@ int s0_init(THREADTIMER *timer) { if(!strcmp("get", timer->data)) timer->status = 1; else if(!strcmp(setpass, timer->data)) timer->status = 2; else if(!strcmp(delpass, timer->data)) timer->status = 4; + else if(!strcmp("md5", timer->data)) timer->status = 5; else if(!strcmp("cat", timer->data)) return send_all(timer); else if(!strcmp("quit", timer->data)) return 0; return send_data(timer->accept_fd, timer->data, timer->numbytes); @@ -280,6 +282,12 @@ int s4_del(THREADTIMER *timer) { return close_and_send(timer, "null", 4); } +int s5_md5(THREADTIMER *timer) { + timer->status = 0; + if(is_md5_equal(timer->data)) return send_data(timer->accept_fd, "null", 4); + else return send_data(timer->accept_fd, "nequ", 4); +} + int check_buffer(THREADTIMER *timer) { printf("Status: %d\n", timer->status); switch(timer->status) { @@ -289,6 +297,7 @@ int check_buffer(THREADTIMER *timer) { case 2: return s2_set(timer); break; case 3: return s3_set_data(timer); break; case 4: return s4_del(timer); break; + case 5: return s5_md5(timer); break; default: return -1; break; } } @@ -382,6 +391,7 @@ void handle_accept(void *p) { puts("Check buffer"); take_word(p, cfg->pwd); take_word(p, "cat"); + take_word(p, "md5"); take_word(p, setpass); take_word(p, delpass); if(timer_pointer_of(p)->numbytes > 0) chkbuf(p);