From b445baa7437ab08872d2e96b41d97979aa2b2859 Mon Sep 17 00:00:00 2001 From: alopex9527 <1710066191@qq.com> Date: Thu, 18 May 2023 22:23:08 +0800 Subject: [PATCH 1/3] alopex modify --- checkMD5.c | 5 +++++ command.c | 9 ++++++-- fileSystem.c | 1 + ftpserver.c | 2 ++ gets.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++------ puts.c | 42 ++++++++++++++++++------------------ threadPool.c | 26 ++++++++++++++++------- tools.c | 28 ++++++++++++++++++++---- 8 files changed, 130 insertions(+), 43 deletions(-) diff --git a/checkMD5.c b/checkMD5.c index 83d6684..fb491a1 100644 --- a/checkMD5.c +++ b/checkMD5.c @@ -21,6 +21,7 @@ int checkMD5(const unsigned char *MD5, MYSQL* mysql){ } if(flag == 1){ + LOGRECORD(INFO,"MD5 exist in vfsystem"); // 存在该md5则返回0 return 0; } @@ -42,6 +43,10 @@ int recvandcheck(int netfd, MYSQL* mysql, unsigned char *MD5){ recvn(netfd, &train.length, sizeof(train.length)); recvn(netfd, train.data, train.length); memcpy(MD5, train.data, train.length); + for(int i=0; i<16; i++){ + printf("%02x", MD5[i]); + } + printf("\n\n"); LOGRECORD(INFO,"get MD5 finish"); int ret = checkMD5(MD5, mysql); diff --git a/command.c b/command.c index dacfd99..798d1d5 100644 --- a/command.c +++ b/command.c @@ -52,12 +52,17 @@ int execCommand(int netfd, struct userProcess_s *user, MYSQL* mysql){ if(ret == 0){ // 文件表存在该md5,则秒传 printf("秒传\n"); - ret = insertSql(train.data, mysql, user, MD5); + // 接收文件名 + train_t filename; + bzero(&filename,sizeof(filename)); + recvn(netfd, &filename.length ,sizeof(int)); + recvn(netfd, filename.data, train.length); + ret = insertSql(filename.data, mysql, user, MD5); LOGRECORD(INFO,"insertSql in execCommand !"); }else{ // 文件表不存在该md5,则按类型接收 LOGRECORD(INFO,"file not exist"); - ret = recvFile(netfd, mysql, user); + ret = recvFile(netfd, MD5, mysql, user); } send(netfd, &ret, sizeof(int), MSG_NOSIGNAL); break; diff --git a/fileSystem.c b/fileSystem.c index 8658f75..9ada37d 100644 --- a/fileSystem.c +++ b/fileSystem.c @@ -150,6 +150,7 @@ int lsCurrDir(int netfd, MYSQL* mysql, userProcess_t* puser){ train2.length = strlen(output); memcpy(train2.data, output, train2.length); send(netfd, &train2, sizeof(train2.length)+train2.length, MSG_NOSIGNAL);//发送数据给客户端send + printf("%s\n",train2.data); } train_t train3; diff --git a/ftpserver.c b/ftpserver.c index f083db7..0b76274 100644 --- a/ftpserver.c +++ b/ftpserver.c @@ -7,6 +7,8 @@ int main(int argc, char** argv) // tcpInit 返回一个socketfd int sockfd = tcpInit(argv[1]); ERROR_CHECK(sockfd, -1, "tcpInit"); + int reuse = 1; + setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(int)); // threadPool 线程池存放子线程的tid、锁、条件变量、任务队列 threadPool_t *threadPool = calloc(1,sizeof(threadPool_t)); bzero(threadPool,sizeof(threadPool_t)); diff --git a/gets.c b/gets.c index e082f52..4e99ea2 100644 --- a/gets.c +++ b/gets.c @@ -1,5 +1,44 @@ #include "head.h" +#define int8_t char +#define uint8_t unsigned char +#define int32_t int +#define uint32_t unsigned int +#define int16_t short +#define uint16_t unsigned short + + +void hb_hex2ascii(uint8_t *hex, char *str, uint32_t len) +{ + uint8_t value = 0; + uint32_t i; + int lower = 1; + + if (str != NULL) { + + for (i = 0; i < len; i++) { + if (lower == 1) { + value = 0; + value |= (*hex & 0xF0) >> 4; + value |= (*hex & 0x0F) << 4; + hex++; + lower = 0; + } else { + value = (value >> 4) & 0x0F; + lower = 1; + } + + if ((value & 0x0F) <= 9) { + *str++ = '0' + (value & 0x0F); + } else { + *str++ = 'a' + (value & 0x0F) - 10; + } + } + } +} + + + // 小文件的接收 void recvMinFile(int sockfd, int fd, train_t train){ @@ -41,7 +80,7 @@ void recvAgain(int sockfd, int fd, off_t filesize, off_t offset){ -int recvFile(int sockfd, MYSQL* mysql, userProcess_t* usr){ +int recvFile(int sockfd, unsigned char *MD5, MYSQL* mysql, userProcess_t* usr){ train_t train1, train2, train3, train4; // step1:接收文件名长度和文件名 @@ -53,8 +92,17 @@ int recvFile(int sockfd, MYSQL* mysql, userProcess_t* usr){ recvn(sockfd, train1.data, train1.length); } // 打开文件,若不存在则新建文件 - char newPath[4096] = {0}; - sprintf(newPath, "%s/%s", ".", train1.data); + char newPath[33] = {0}; + //memcpy(newPath,MD5,16); + for(int i=0; i<16; i++){ + printf("%02x",MD5[i]); + } + printf("\n"); + + hb_hex2ascii(MD5, newPath, 32); + newPath[32]='\0'; + + printf("newPath = %s\n", newPath); int fd = open(newPath, O_RDWR|O_CREAT, 0666); ERROR_CHECK(fd, -1, "open"); @@ -112,15 +160,13 @@ int recvFile(int sockfd, MYSQL* mysql, userProcess_t* usr){ // 写入文件表 - int insRet = insertSql(train1.data, mysql, usr, mdFromRecver); + int insRet = insertSql(train1.data, mysql, usr, newPath); if(insRet == 0){ LOGRECORD(INFO,"insertSql success "); }else{ LOGRECORD(INFO,"insertSql faild ! "); } - LOGRECORD(INFO,"recvFile"); - - + LOGRECORD(INFO,"recvFile finish"); printf("此接收文件无错误\n"); return 0; diff --git a/puts.c b/puts.c index 4140769..7e0e5ec 100644 --- a/puts.c +++ b/puts.c @@ -42,9 +42,10 @@ void transAgain(int netfd, int fd, off_t fileSize, off_t offset){ int getPath(char* path,int currid, MYSQL* mysql){ + LOGRECORD(INFO,"getPath start"); char sqlCommand[1024]; bzero(sqlCommand,sizeof(sqlCommand)); - sprintf(sqlCommand,"select path from usr where id = %d",currid); + sprintf(sqlCommand,"select path from vfsystem where id = %d",currid); int ret = mysql_query(mysql, sqlCommand); if(ret != 0){ LOGRECORD(ERROR,"mysql_query"); @@ -56,7 +57,7 @@ int getPath(char* path,int currid, MYSQL* mysql){ LOGRECORD(ERROR,"mysql_fetch_row"); return -1; }else{ - memcpy(path,row[0],strlen(row[0])); + strcpy(path,row[0]); } return 0; @@ -67,14 +68,24 @@ int getPath(char* path,int currid, MYSQL* mysql){ int sendFile(int netfd, const char *fileName, MYSQL* mysql, userProcess_t* usr){ char sqlCommand[1024]; bzero(sqlCommand,sizeof(sqlCommand)); - char path[128] = {0}; + char path[128]; bzero(path, sizeof(path)); getPath(path, usr->curid, mysql); - sprintf(sqlCommand, "select MD5 from usr where user = %s AND tomb = 0 AND path = %s AND name = %s", usr->username, path, fileName); + LOGRECORD(INFO,"getPath finish"); + sprintf(sqlCommand, "select MD5 from vfsystem " + "where user = '%s' and tomb = 0 and preid = %d and name = '%s'", + usr->username, usr->curid, fileName); int ret = mysql_query(mysql, sqlCommand); ERROR_CHECK(ret, -1, "mysql_query"); MYSQL_RES *result = mysql_store_result(mysql); MYSQL_ROW row = mysql_fetch_row(result); + if(row == 0){ + LOGRECORD(ERROR,"MD5 IS NOT EXISTS"); + return -1; + } + char realfile[128] = {0}; + strcpy(realfile,row[0]); + LOGRECORD(INFO,"sendfile start"); train_t train1, train2, train3, train4; @@ -85,7 +96,7 @@ int sendFile(int netfd, const char *fileName, MYSQL* mysql, userProcess_t* usr){ send(netfd, &train1, sizeof(train1.length)+train1.length, MSG_NOSIGNAL); // step2:发送文件的总大小 - int fd = open(row[0], O_RDWR); + int fd = open(realfile, O_RDWR); ERROR_CHECK(fd, -1, "open"); bzero(&train2, sizeof(train2)); struct stat statbuf; @@ -97,23 +108,10 @@ int sendFile(int netfd, const char *fileName, MYSQL* mysql, userProcess_t* usr){ send(netfd, &train2, sizeof(train2.length)+train2.length, MSG_NOSIGNAL); // step3:发送文件的md5码 - MD5_CTX ctx; - MD5_Init(&ctx); - char buf[4096]; - while(1){ - bzero(buf, sizeof(buf)); - ssize_t sret = read(fd, buf, sizeof(buf)); - if(sret == 0){ - break; - } - MD5_Update(&ctx, buf, sret); - } - unsigned char mdFromSender[16]; // 将要保存md5码16个字节的二进制数据 - MD5_Final(mdFromSender, &ctx); - train3.length = sizeof(mdFromSender); - for(int i=0; ilength,sizeof(int)); int ret = recv(netfd, &usrName->length, sizeof(int),MSG_WAITALL); + printf("usrName->length = %d\n", usrName->length); if(ret == 0){ LOGRECORD(ERROR,"client close !"); goto end; } + //ret = recvn(netfd, usrName->data, usrName->length); ret = recv(netfd, usrName->data, usrName->length,MSG_WAITALL); + printf("usrName->data=%s\n", usrName->data); if(ret == 0){ LOGRECORD(ERROR,"client close !"); goto end; } + // 接受用户密码 bzero(usrPassword,sizeof(train_t)); ret = recv(netfd, &usrPassword->length, sizeof(int),MSG_WAITALL); + printf("usrPassword->length = %d\n", usrPassword->length); if(ret == 0){ LOGRECORD(ERROR,"client close !"); goto end; } - ret = recv(netfd, usrPassword->data, usrName->length,MSG_WAITALL); + ret = recv(netfd, usrPassword->data, usrPassword->length,MSG_WAITALL); + printf("usrPassword->data=%s\n", usrPassword->data); if(ret == 0){ LOGRECORD(ERROR,"client close !"); goto end; @@ -225,7 +235,7 @@ void *threadFunc(void* arg){ LOGRECORD(ERROR,"copy initid"); int id = atoi(row[0]); memcpy(&usr.initid,&id,sizeof(int)); - printf("initid :%d\n",usr.initid); + //printf("initid :%d \n",usr.initid); memcpy(&usr.curid,&usr.initid,sizeof(int)); LOGRECORD(INFO,"usr initiaze finish!"); mysql_free_result(result); diff --git a/tools.c b/tools.c index 0a75334..a1b726b 100644 --- a/tools.c +++ b/tools.c @@ -12,7 +12,8 @@ int recvn(int sockfd, void *buf, int length){ -int insertSql(const char* filename, MYSQL* usrsql, userProcess_t *usr, unsigned char *md5){ +int insertSql(const char* filename, MYSQL* usrsql, userProcess_t *usr, char *md5){ + int pre_id = 0; char path[128] = {0}; char sqlCommand[1024] = {0}; @@ -24,9 +25,27 @@ int insertSql(const char* filename, MYSQL* usrsql, userProcess_t *usr, unsigned } MYSQL_RES *result = mysql_store_result(usrsql); MYSQL_ROW row = mysql_fetch_row(result); + int row0 = atoi(row[0]); + memcpy(&pre_id, &row0, sizeof(int)); + strcpy(path,row[1]); + LOGRECORD(INFO,"get preid and path"); mysql_free_result(result); - memcpy(&pre_id, &row[0], sizeof(int)); - memcpy(path, &row[1], sizeof(int)); + + //bzero(sqlCommand,sizeof(sqlCommand)); + //sprintf(sqlCommand, "select id from vfsystem where path = '%s' and preid = %d and name = '%s'", + // path, pre_id,filename); + //printf("%s\n", sqlCommand); + //ret = mysql_query(usrsql, sqlCommand); + //printf("%d\n", ret); + // + //result = mysql_store_result(usrsql); + //row = mysql_fetch_row(result); + //printf("in tools.c :%s\n",row[0]); + //if(row != NULL){ + // LOGRECORD(ERROR,"filename already exist in currpath"); + // mysql_free_result(result); + // return -1; + //} bzero(sqlCommand, sizeof(sqlCommand)); sprintf(sqlCommand, "insert into vfsystem (name,type,preid,path,user,tomb,md5)" @@ -34,9 +53,10 @@ int insertSql(const char* filename, MYSQL* usrsql, userProcess_t *usr, unsigned ret = mysql_query(usrsql, sqlCommand); if(ret != 0){ LOGRECORD(ERROR,"mysql_query failed !"); + return -1; } LOGRECORD(INFO,"mysql_query success"); - return ret; + return 0; } -- Gitee From e934aee761e55cebaeb61832306a891c95a65ef4 Mon Sep 17 00:00:00 2001 From: alopex9527 <1710066191@qq.com> Date: Thu, 18 May 2023 22:23:28 +0800 Subject: [PATCH 2/3] alopex modify --- head.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/head.h b/head.h index f6ee444..bec7873 100644 --- a/head.h +++ b/head.h @@ -34,7 +34,6 @@ enum{ERROR,WARNING,INFO,DEBUG}; char strTime[128];\ strftime(strTime,sizeof(strTime),"%Y-%m-%d %H:%M:%S",localtime(&currTime));\ if(errorLevel == ERROR ){\ - printf("Hello world\n");\ fprintf(fp,"[%s]-[%s]-[%s]-[%d]-[%s] :> " formation "\n",strTime,"ERROR",__FILE__,__LINE__,__FUNCTION__);\ }\ else if(errorLevel == WARNING ){\ @@ -73,7 +72,7 @@ int recvandcheck(int netfd, MYSQL* mysql, unsigned char *MD5); int sendFile(int netfd, const char* fileName, MYSQL* mysql, userProcess_t* usr); // 下载文件 -int recvFile(int sockfd, MYSQL* usrsql, userProcess_t* usr); +int recvFile(int sockfd,unsigned char *MD5, MYSQL* usrsql, userProcess_t* usr); // 执行命令 int execCommand(int netfd, struct userProcess_s *user, MYSQL* mysql); @@ -91,7 +90,7 @@ int recvn(int sockfd, void *buf, int length); char* merge(char* aPath, char* cPath); // 修改文件表 -int insertSql(const char* filename, MYSQL* usrsql, userProcess_t *usr, unsigned char *md5); +int insertSql(const char* filename, MYSQL* usrsql, userProcess_t *usr, char *md5); #endif -- Gitee From c8f51422766982b318aaae72d2e88433e35cc1eb Mon Sep 17 00:00:00 2001 From: alopex9527 <1710066191@qq.com> Date: Thu, 18 May 2023 22:23:47 +0800 Subject: [PATCH 3/3] alopex midify --- client/gets_file.c | 4 ++-- client/password_check.c | 4 ++-- client/puts_file.c | 14 +++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/client/gets_file.c b/client/gets_file.c index 0ffc4a5..bc791b7 100644 --- a/client/gets_file.c +++ b/client/gets_file.c @@ -46,8 +46,8 @@ int gets_file(int sockfd) bzero(&train1,sizeof(train1)); recvn(sockfd, &train1.length, sizeof(train1.length)); recvn(sockfd, train1.data, train1.length); - //printf("train.length = %d\n", train1.length); - //printf("train1.data = %s\n", train1.data); + printf("train.length = %d\n", train1.length); + printf("train1.data = %s\n", train1.data); if(train1.length == 0){ recvn(sockfd, &train1.length, sizeof(train1.length)); recvn(sockfd, train1.data, train1.length); diff --git a/client/password_check.c b/client/password_check.c index ec7febd..a2c4a68 100644 --- a/client/password_check.c +++ b/client/password_check.c @@ -9,12 +9,12 @@ int password_check(int sockfd, char * username, int name_len, char * password, i bzero(&train1,sizeof(train1)); strncpy(train.data,username,(size_t)name_len); train.length = name_len; - send(sockfd,&train,sizeof(train),0); + send(sockfd,&train,sizeof(train.length)+train.length, MSG_NOSIGNAL); printf("len = %d, data = %s\n",train.length,train.data); strncpy(train1.data,password,(size_t)passwd_len); train1.length = passwd_len; - send(sockfd,&train1,sizeof(train1),0); + send(sockfd,&train1,sizeof(train1.length)+train1.length,MSG_NOSIGNAL); printf("len = %d, data = %s\n",train1.length,train1.data); int error_num; diff --git a/client/puts_file.c b/client/puts_file.c index 8e9400a..abba1b2 100644 --- a/client/puts_file.c +++ b/client/puts_file.c @@ -72,12 +72,12 @@ int puts_file(int netfd, const char *fileName) lseek(fd, 0, SEEK_SET); //判断是否是秒传 - int num; - recvn(netfd,&num,sizeof(int)); - if(num == 0) - { - printf("send file done!\n"); - }else{ + //int num; + //recvn(netfd,&num,sizeof(int)); + //if(num == 0) + //{ + // printf("send file done!\n"); + //}else{ bzero(&train1, sizeof(train1)); train1.length = strlen(fileName); memcpy(train1.data, fileName, train1.length); @@ -124,7 +124,7 @@ int puts_file(int netfd, const char *fileName) printf("send file done!\n"); - } + //} //bzero(&train1, sizeof(train1)); //train1.length = strlen(fileName); //int i=0; -- Gitee