update ftpserver

This commit is contained in:
ambrumf 2023-10-17 01:25:16 +08:00
parent 4295885cee
commit 1f57b5e6b8
1 changed files with 15 additions and 10 deletions

View File

@ -65,10 +65,7 @@ int CheckPassword(int it) {
} }
return 0; return 0;
} }
void StartDataConnection(int data_fd,struct sockaddr_in client_addr)
{
}
void *CreateDataSockt(void *arg) { void *CreateDataSockt(void *arg) {
int *data = arg; int *data = arg;
@ -102,7 +99,6 @@ void *CreateDataSockt(void *arg) {
fd_map[cmd_fd] = data_fd; fd_map[cmd_fd] = data_fd;
StartDataConnection(data_fd, client_addr);
} }
close(fd); close(fd);
@ -113,6 +109,7 @@ char* GetRespond(char* buf, int it,int *ret,int cmd_fd) {
char * cmd = malloc(100); char * cmd = malloc(100);
char * data = malloc(100); char * data = malloc(100);
char* respond = malloc(100); char* respond = malloc(100);
//debug
printf("%s\n", buf); printf("%s\n", buf);
sscanf(buf, "%s %s", cmd, data); sscanf(buf, "%s %s", cmd, data);
if(strcmp(cmd,"USER")==0){ if(strcmp(cmd,"USER")==0){
@ -157,18 +154,20 @@ char* GetRespond(char* buf, int it,int *ret,int cmd_fd) {
sprintf(respond, "550 failed to open file\n"); sprintf(respond, "550 failed to open file\n");
} }
fseek(file, 0, SEEK_END); long file_size = 5000;
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char* start = "150 starting data transfer\n";
write(cmd_fd, start, strlen(start));
char* buff = (char*)malloc(file_size); char* buff = (char*)malloc(file_size);
fread(buf, 1, file_size, file); fread(buff, 1, file_size, file);
int size = write(fd_map[cmd_fd], buff, strlen(buff)); int size = write(fd_map[cmd_fd], buff, strlen(buff));
if (size < 0) { if (size < 0) {
printf("failed to send message\n"); printf("failed to send message\n");
} }
char* end = "226 Operation successfull";
write(cmd_fd, end, strlen(end));
fclose(file); fclose(file);
free(buff); free(buff);
free(dir); free(dir);
@ -180,13 +179,16 @@ char* GetRespond(char* buf, int it,int *ret,int cmd_fd) {
if (file == NULL) { if (file == NULL) {
sprintf(respond, "550 failed to open file\n"); sprintf(respond, "550 failed to open file\n");
} }
char* start = "150 starting data transfer\n";
write(cmd_fd, start, strlen(start));
char* buff = (char*)malloc(5000); char* buff = (char*)malloc(5000);
int size = read(fd_map[cmd_fd], buff, 5000); int size = read(fd_map[cmd_fd], buff, 5000);
while (size < 0) { while (size < 0) {
size = read(fd_map[cmd_fd], buff, 5000); size = read(fd_map[cmd_fd], buff, 5000);
} }
char* end = "226 Operation successfull";
write(cmd_fd, end, strlen(end));
fwrite(buff, 1, strlen(buff), file); fwrite(buff, 1, strlen(buff), file);
fclose(file); fclose(file);
@ -197,6 +199,9 @@ char* GetRespond(char* buf, int it,int *ret,int cmd_fd) {
sprintf(respond, "221 connection close"); sprintf(respond, "221 connection close");
*ret = 221; *ret = 221;
} }
else if (strcmp(cmd, "TYPE") == 0) {
sprintf(respond, "200 Type set to I");
}
else { else {
respond = "500 syntax error\n"; respond = "500 syntax error\n";
} }