mirror of
https://github.com/fumiama/simple-dict.git
synced 2026-06-30 00:40:25 +08:00
增加md5校验
This commit is contained in:
@@ -3,6 +3,11 @@ project(simple-dict-server C)
|
|||||||
SET(CMAKE_BUILD_TYPE "Release")
|
SET(CMAKE_BUILD_TYPE "Release")
|
||||||
|
|
||||||
add_definitions(-DLISTEN_ON_IPV6)
|
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")
|
include_directories("/usr/local/include")
|
||||||
link_directories("/usr/local/lib")
|
link_directories("/usr/local/lib")
|
||||||
@@ -14,6 +19,7 @@ add_executable(simple-dict-client client.c)
|
|||||||
add_executable(migrate migrate.c)
|
add_executable(migrate migrate.c)
|
||||||
add_executable(cfgwriter cfgwriter.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-server dict spb pthread)
|
||||||
target_link_libraries(simple-dict-client pthread)
|
target_link_libraries(simple-dict-client pthread)
|
||||||
target_link_libraries(migrate spb)
|
target_link_libraries(migrate spb)
|
||||||
|
|||||||
11
client.c
11
client.c
@@ -92,7 +92,16 @@ int main(int argc,char *argv[]) { //usage: ./client host port
|
|||||||
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 {
|
} 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);
|
send(sockfd, buf, strlen(buf), 0);
|
||||||
if(!strcmp(buf, "quit")) exit(EXIT_SUCCESS);
|
if(!strcmp(buf, "quit")) exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|||||||
35
dict.c
35
dict.c
@@ -1,10 +1,15 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <simplemd5.h>
|
||||||
#include "dict.h"
|
#include "dict.h"
|
||||||
|
|
||||||
static FILE *fp = NULL;
|
static FILE *fp = NULL;
|
||||||
static int lock = 0;
|
static int lock = 0;
|
||||||
static char* filepath;
|
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) {
|
uint32_t last_nonnull(char* p, uint32_t max_size) {
|
||||||
if(max_size > 1) while(!p[max_size - 1]) 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) {
|
if(fp) {
|
||||||
lock = LOCK_UN;
|
lock = LOCK_UN;
|
||||||
filepath = file_path;
|
filepath = file_path;
|
||||||
|
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;
|
return 1;
|
||||||
|
} else {
|
||||||
|
free(dict_buff);
|
||||||
|
puts("Read dict error");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
puts("Allocate memory error");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
puts("Open dict error");
|
puts("Open dict error");
|
||||||
return 0;
|
return 0;
|
||||||
@@ -48,3 +69,17 @@ off_t get_dict_size() {
|
|||||||
if(stat(filepath, &statbuf)==0) return statbuf.st_size;
|
if(stat(filepath, &statbuf)==0) return statbuf.st_size;
|
||||||
else return -1;
|
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
|
||||||
|
}
|
||||||
|
|||||||
1
dict.h
1
dict.h
@@ -20,5 +20,6 @@ int init_dict(char* file_path);
|
|||||||
FILE *open_dict(int lock_type);
|
FILE *open_dict(int lock_type);
|
||||||
void close_dict();
|
void close_dict();
|
||||||
off_t get_dict_size();
|
off_t get_dict_size();
|
||||||
|
int is_md5_equal(uint8_t* digest);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
10
server.c
10
server.c
@@ -72,6 +72,7 @@ int s1_get(THREADTIMER *timer);
|
|||||||
int s2_set(THREADTIMER *timer);
|
int s2_set(THREADTIMER *timer);
|
||||||
int s3_set_data(THREADTIMER *timer);
|
int s3_set_data(THREADTIMER *timer);
|
||||||
int s4_del(THREADTIMER *timer);
|
int s4_del(THREADTIMER *timer);
|
||||||
|
int s5_md5(THREADTIMER *timer);
|
||||||
|
|
||||||
int bind_server(uint16_t port, u_int try_times) {
|
int bind_server(uint16_t port, u_int try_times) {
|
||||||
int fail_count = 0;
|
int fail_count = 0;
|
||||||
@@ -168,6 +169,7 @@ int s0_init(THREADTIMER *timer) {
|
|||||||
if(!strcmp("get", timer->data)) timer->status = 1;
|
if(!strcmp("get", timer->data)) timer->status = 1;
|
||||||
else if(!strcmp(setpass, timer->data)) timer->status = 2;
|
else if(!strcmp(setpass, timer->data)) timer->status = 2;
|
||||||
else if(!strcmp(delpass, timer->data)) timer->status = 4;
|
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("cat", timer->data)) return send_all(timer);
|
||||||
else if(!strcmp("quit", timer->data)) return 0;
|
else if(!strcmp("quit", timer->data)) return 0;
|
||||||
return send_data(timer->accept_fd, timer->data, timer->numbytes);
|
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);
|
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) {
|
int check_buffer(THREADTIMER *timer) {
|
||||||
printf("Status: %d\n", timer->status);
|
printf("Status: %d\n", timer->status);
|
||||||
switch(timer->status) {
|
switch(timer->status) {
|
||||||
@@ -289,6 +297,7 @@ int check_buffer(THREADTIMER *timer) {
|
|||||||
case 2: return s2_set(timer); break;
|
case 2: return s2_set(timer); break;
|
||||||
case 3: return s3_set_data(timer); break;
|
case 3: return s3_set_data(timer); break;
|
||||||
case 4: return s4_del(timer); break;
|
case 4: return s4_del(timer); break;
|
||||||
|
case 5: return s5_md5(timer); break;
|
||||||
default: return -1; break;
|
default: return -1; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -382,6 +391,7 @@ void handle_accept(void *p) {
|
|||||||
puts("Check buffer");
|
puts("Check buffer");
|
||||||
take_word(p, cfg->pwd);
|
take_word(p, cfg->pwd);
|
||||||
take_word(p, "cat");
|
take_word(p, "cat");
|
||||||
|
take_word(p, "md5");
|
||||||
take_word(p, setpass);
|
take_word(p, setpass);
|
||||||
take_word(p, delpass);
|
take_word(p, delpass);
|
||||||
if(timer_pointer_of(p)->numbytes > 0) chkbuf(p);
|
if(timer_pointer_of(p)->numbytes > 0) chkbuf(p);
|
||||||
|
|||||||
Reference in New Issue
Block a user