ftp server PORT and LIST command implementation


I'll try to keep this simple. Basically, whenever I call "ls" from the ftp command prompt, It always returns a 425, as seen in the relevant if statement in the code below:

EDIT: It apparently is due to overflow, but I can't see where it might be overflowing. Can anyone help me find it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 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); 
          }



        }

However, I have no idea why it is doing this. What should I do to fix this problem?

The LIST command:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

    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);
    

The server returns:
1
2
3
4
5
6
7
8
PORT 127,0,0,1,217,89

PORT
255.255.255.2550
LIST
27,0,0,1,217,89

LIST

sock1 and sock2 are my sockets. Again, can anyone help me with this/point out errors that are obvious to you, but I'm blind to?

If you need more info, or think the error is somewhere else, please ask, and I'll be all to happy to provide it.
Topic archived. No new replies allowed.