int get_port_and_connect_to_it(t_client *client, int idx, char *arg) { char ip[16]; char *ptr; int port; int porth; port = 0; arg[idx] = '\0'; strncpy(ip, arg, 16); porth = atoi(arg + idx + 1) * 256; ptr = strtok(arg + idx + 1, ","); ptr = strtok(NULL, ","); if (ptr != NULL) port = porth + atoi(ptr); if (handle_data_connection(client, port, ip) != 0) printf("port fail\n"); else printf("port ok\n"); return (0); }
/* * This function is called whenever an uIP event occurs (e.g. when * a new connection is established, new data arrives, sent data is * acknowledged, data needs to be retransmitted, etc.), from the * uip_tcp_appcall() callback. */ void ftpd_appcall( void ) { u16_t i; //PRINT_Log("\r\nftpd_appcall\r\n"); /* * First we must check that it is the port we are listening on as * this function will be called for all events on all connections. */ if (uip_conn->lport == HTONS(FTPD_CONTROL_PORT)) { ftpd_appstate_t *s = (ftpd_appstate_t *)uip_conn->appdata; /* * If a new connection we need to check for a few possibilities. */ if (uip_connected()) { PRINT_Log("\r\nNew command connection from IP Address %d.%d.%d.%d:%d", uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr), uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr), ntohs(uip_conn->rport)); if (!driveAttached) { PRINT_Log("\r\nNo drive attached!"); // Initialise the ProtoSocket for the 421 message PSOCK_INIT(&s->ps, NULL, 0); // Set the flag to cause it to be sent s->resp421_flag = TRUE; } else { // find a new FTP server structure for (i = 0; i < NUM_FTP_DAEMONS; i++) { if (FTP_UNUSED == ftpd_state[i].state) { break; } } // Check if we found a free ftpd_state if (NUM_FTP_DAEMONS == i) { PRINT_Log("\r\nNo daemons available!"); // Initialise the ProtoSocket for the 421 message PSOCK_INIT(&s->ps, NULL, 0); // Set the flag to cause it to be sent s->resp421_flag = TRUE; } else { PRINT_Log("\r\nFound unused deamon %d", i); s->ftpd_state_ptr = &ftpd_state[i]; // Initialise the FTP Daemon state ftpd_state[i].state = LOGGED_OUT; ftpd_state[i].data_conn = NULL; //ftpd_state[i].clientip = uip_conn->ripaddr; memcpy(ftpd_state[i].clientip,uip_conn->ripaddr,sizeof(uip_ipaddr_t)); ftpd_state[i].clientport = FTPC_DATA_PORT; // Clear the command index down ftpd_state[i].cmd_index = CMD_INDEX_IDLE; // Reset the directory pointer //ftpd_state[i].pCurrent_dir = NULL; // Clear the current path strcpy(ftpd_state[i].Current_path, "/"); // Setup the PASV information ftpd_state[i].pasvflag = FALSE; // Initialise the ProtoSockets and ProtoThread used by the command parser PSOCK_INIT(&ftpd_state[i].cmdpsin, ftpd_state[i].cmdinputbuffer, CMD_BUFFER_SIZE); PT_INIT(&ftpd_state[i].cmdpt); // Clear the 421 response flag s->resp421_flag = FALSE; } } } else if (uip_closed() || uip_aborted() || uip_timedout()) { PRINT_Log("\r\nCommand connection closing"); if (s->ftpd_state_ptr) { PRINT_Log(" for daemon %d", s->ftpd_state_ptr->pasvport - PASV_PORT_OFFSET); // Free up the FTP daemon s->ftpd_state_ptr->state = FTP_UNUSED; s->ftpd_state_ptr = NULL; } } if (s->resp421_flag) { send_421_response(s); } else if (s->ftpd_state_ptr) { handle_cmd_input(s->ftpd_state_ptr); handle_cmd_output(s->ftpd_state_ptr); } } else { //PRINT_Log("\r\nSearch though all the FTPD Structures and see if this one of our data connections."); /* * Search though all the FTPD Structures and see if this one of our data connections. */ u16_t i; for (i = 0; i < NUM_FTP_DAEMONS; i++) { if (ftpd_state[i].data_conn == uip_conn) { if (uip_connected()) { PRINT_Log("\r\nNew active data connection for daemon %d to IP Address %d.%d.%d.%d:%d", i, uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr), uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr), ntohs(uip_conn->rport)); PSOCK_INIT(&ftpd_state[i].dataps, ftpd_state[i].datainputbuffer, DATA_BUFFER_SIZE); // Move the state on as required if (WAITING_FOR_CONNECT == ftpd_state[i].state) { ftpd_state[i].state = CONNECT_OK; } } else if (uip_closed() || uip_aborted() || uip_timedout()) { PRINT_Log("\r\nData connection closing for daemon %d", i); if (uip_closed() && (TRANSFER_RECV == ftpd_state[i].state)) { // There may be data in this last packet handle_data_connection(&ftpd_state[i]); // Closing of the connection in a RECV state indicates end of file #if 1 CloseFile(&ftpd_state[i]); #endif //close the file in here PRINT_Log("\r\nFinished file receive for daemon %d", i); ftpd_state[i].state = TRANSFER_DONE; } else if((TRANSFER_LIST == ftpd_state[i].state) || (TRANSFER_SEND == ftpd_state[i].state) || (TRANSFER_RECV == ftpd_state[i].state)) { PRINT_Log("\r\nAborting data transfer for daemon %d", i); ftpd_state[i].state = TRANSFER_ABORT; } ftpd_state[i].data_conn = NULL; } if (ftpd_state[i].data_conn) { handle_data_connection(&ftpd_state[i]); } // Leave the loop early break; } else { // Check for a new connection in Passive mode if(ftpd_state[i].pasvflag && uip_connected() && (ntohs(uip_conn->lport) == ftpd_state[i].pasvport)) { PRINT_Log("\r\nNew passive data connection for daemon %d from IP Address %d.%d.%d.%d:%d", i, uip_ipaddr1(uip_conn->ripaddr), uip_ipaddr2(uip_conn->ripaddr), uip_ipaddr3(uip_conn->ripaddr), uip_ipaddr4(uip_conn->ripaddr), ntohs(uip_conn->rport)); ftpd_state[i].data_conn = uip_conn; PSOCK_INIT(&ftpd_state[i].dataps, ftpd_state[i].datainputbuffer, DATA_BUFFER_SIZE); // Move the state on as required if (WAITING_FOR_CONNECT == ftpd_state[i].state) { ftpd_state[i].state = CONNECT_OK; } handle_data_connection(&ftpd_state[i]); // Leave the loop early break; } } } } }