when attempt ls command ftp, hit 425 in port section always, can tell me why is? user section works, , if can port work, can focus on stor , retr.
/*ftp server*/ #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <stdio.h> #include <stdlib.h> /*for getting file size using stat()*/ #include<sys/stat.h> /*for sendfile()*/ #include<sys/sendfile.h> /*for o_rdonly*/ #include<fcntl.h> int active=0; int sock1, sock2; char buf[100], command[5], filename[20]; struct sockaddr_in remoteaddr, remoteaddr_data; int main(int argc,char *argv[]) { struct sockaddr_in server, client; struct stat obj; struct sockaddr_in local_data_addr_act; int s, s_data, s_data_act; s = socket(af_inet, sock_stream, 0); s_data = socket(af_inet, sock_stream, 0); s_data_act = socket(af_inet, sock_stream, 0); int k, i, size, len, c; int filehandle; sock1 = socket(af_inet, sock_stream, 0); if(sock1 == -1) { printf("socket creation failed"); exit(1); } server.sin_port = htons(atoi(argv[1])); server.sin_addr.s_addr = 0; k = bind(sock1,(struct sockaddr*)&server,sizeof(server)); if(k == -1) { printf("binding error"); exit(1); } k = listen(sock1,1); if(k == -1) { printf("listen failed"); exit(1); } len = sizeof(client); sock2 = accept(sock1,(struct sockaddr*)&client, &len); write(sock2,"220\r\n", 6); = 1; while(1) { recv(sock2, buf, 100, 0); printf("%s\n", buf); sscanf(buf, "%s", command); printf("%s\n", command); if(!strcmp(command, "list")) { system("ls >tmp.txt"); file *fin=fopen("tmp.txt","r"); sprintf(buf, "125\r\n"); send(sock2, buf, strlen(buf), 0); char temp_buf[10]; while (!feof(fin)) { fgets(temp_buf, 98, fin); sprintf(buf, "%s", temp_buf); if (!active)send(sock1, buf, strlen(buf), 0); else send(s_data_act, buf, strlen(buf), 0); } fclose(fin); sprintf(buf, "250\r\n"); send(sock2, buf, strlen(buf), 0); if(!active) close(sock1); else close(s_data_act); sprintf(buf, "226\r\n"); send(sock2, buf, strlen(buf), 0); } else if(!strcmp(command,"user")) { sprintf(buf, "230\r\n"); send(sock2, buf, strlen(buf), 0); } else if(!strcmp(command,"syst")) { write(sock2, "502\r\n", 6); } else if(!strcmp(command,"port")) { unsigned char act_port[2]; int act_ip[4], port_dec; char ip_decimal[40]; active=1; sscanf(command, "port %d,%d,%d,%d,%d,%d",&act_ip[0],&act_ip[1],&act_ip[2],&act_ip[3], &act_port[0], &act_port[1]); local_data_addr_act.sin_family=af_inet; sprintf(ip_decimal, "%d.%d.%d.%d", act_ip[0], act_ip[1], act_ip[2], act_ip[3]); local_data_addr_act.sin_addr.s_addr=inet_addr(ip_decimal); port_dec=act_port[0]; port_dec=port_dec<<8; port_dec=port_dec+act_port[1]; local_data_addr_act.sin_port=htons(port_dec); if (connect(s_data_act,(struct sockaddr*)&local_data_addr_act, (int)sizeof(struct sockaddr))!=0) { printf("%s%d\n",inet_ntoa(local_data_addr_act.sin_addr),ntohs(local_data_addr_act.sin_port)); sprintf(buf, "425\r\n"); send(sock2, buf, strlen(buf), 0); close(s_data_act); } else { sprintf(buf, "200\r\n"); send(sock2, buf, strlen(buf), 0); } } else if(!strcmp(command,"retr")) { sscanf(buf, "%s%s", filename, filename); stat(filename, &obj); filehandle = open(filename, o_rdonly); size = obj.st_size; if(filehandle == -1) size = 0; send(sock2, &size, sizeof(int), 0); if(size) sendfile(sock2, filehandle, null, size); } else if(!strcmp(command, "stor")) { int c = 0, len; char *f; sscanf(buf+strlen(command), "%s", filename); recv(sock2, &size, sizeof(int), 0); = 1; while(1) { filehandle = open(filename, o_creat | o_excl | o_wronly, 0666); if(filehandle == -1) { sprintf(filename + strlen(filename), "%d", i); } else break; } f = malloc(size); recv(sock2, f, size, 0); c = write(filehandle, f, size); close(filehandle); send(sock2, &c, sizeof(int), 0); } else if(!strcmp(command, "pwd")) { system("pwd>temp.txt"); = 0; file*f = fopen("temp.txt","r"); while(!feof(f)) buf[i++] = fgetc(f); buf[i-1] = '\0'; fclose(f); send(sock2, buf, 100, 0); } else if(!strcmp(command, "cd")) { if(chdir(buf+3) == 0) c = 1; else c = 0; send(sock2, &c, sizeof(int), 0); } else if(!strcmp(command, "bye") || !strcmp(command, "quit")) { printf("ftp server quitting..\n"); = 1; send(sock2, &i, sizeof(int), 0); exit(0); } } return 0; }
this code full of errors , mistakes. whatever tried implement code, not remotely close conforming ftp server implementation.
you creating many sockets, , not using of them. , creating of them @ wrong time under wrong conditions.
a complete lack of error handling on socket/file function calls.
not handling socket reads/writes correctly, in particular not accounting fact tcp streaming transport not provide guarantee of 1-to-1 relationship between writes , reads. must must must account this!
writing incorrect data client @ times - sending null terminators not expected, sending data buffers if null terminated when not, sending file sizes binary integers when not expected, etc.
assuming received data null terminated when not.
sending passive-mode data wrong socket (and not implementing pasv
command begin with).
not implementing port
, stor
, , retr
commands correctly (mismanaging sockets).
not implementing pwd
, cd
, , bye
/quit
commands correctly (sending invalid replies).
and, although not technically error, should not using system()
. there better native ways data needed. list
, can use opendir()
instead , write entries of directory needed. pwd
, can use getcwd()
.
you need scrap code , start over. there many things wrong try fix. numerous examples available online how implement socket i/o correctly, , how line-based i/o sockets (since ftp line-based protocol). , please read rfc 959 proper rules of handling ftp command/responses , data transfer management.