예제 #1
0
int  main(int argc,char **argv)
{
    printf("ftp>");
    char command[MAXSIZE];
    char*cmd;
    scanf("%s",command);
    cmd=command;
    while(*(cmd)==' ')
        cmd++;

    if(strncmp(cmd,"login",5)==0)
    {
        login();
        if(login_yes==1)
        {
            while(1)
            {
            comm:            
                sleep(1);
                printf("ftp>");
                zeromery(command,1024);
                scanf("%s",command);
                cmd=command;
                while(*(cmd)==' ')
                cmd++;
                if(strncmp(cmd,"pasv",4)==0)
                {
                    ftp_list(control_sockfd);
                }
                if(strncmp(cmd,"port",4)==0)
                {
                    ftp_list(control_sockfd);
                }
                if(strncmp(cmd,"list",4)==0)
                {
                    ftp_pwd(control_sockfd);
                    ftp_list(control_sockfd);
                }
                if(strncmp(cmd,"pwd",3)==0)
                {
                    ftp_pwd(control_sockfd);
                }
                if(strncmp(cmd,"mkdir",5)==0)
                {
                    char path[60];
                    zeromery(path,60);
                    printf("创建的路径名: ");
                    scanf("%s",path);
                    printf("s\n",path);
                    ftp_creat_mkd(path,control_sockfd);
                }
                if(strncmp(cmd,"back",4)==0)
                {
                    ftp_back(control_sockfd);

                    ftp_pwd(control_sockfd);
                }
                if(strncmp(cmd,"cd",2)==0)
                {
                    int i;
                    char path[60];
                    zeromery(path,60);
                    printf("要到的路径:");
                    scanf("%s",path);
                    printf("%s\n",path);
                    ftp_changdir(path,control_sockfd);
                }
                if(strncmp(cmd,"get",3)==0)
                {
                    ftp_pwd(control_sockfd);    
                    ftp_download(control_sockfd);
                }
                if(strncmp(cmd,"up",3)==0)
                {
                    ftp_pwd(control_sockfd);
                    ftp_up(control_sockfd);        
                }
                if(strncmp(cmd,"quit",4)==0)
                {
                    printf("bye^_^\n");
                    close(control_sockfd);
                    break;
                }
                printf("支持 list,pwd,mkdir,back,cd,up,get\n");    
        }
        
    }
    else if(login_yes==0)
    { 
        int i;//不成功登录下最多还有两次机会,如果不能在两次登录,则,关闭链接。
        printf("Can not login vsftpd");
        for(i=2;i>0;i--)
        {
            printf("你还有 %d 登录机会\n",i);            
            login();
            if(login_yes==1)
            {
                goto comm;
            }
        }
        if(i==0)
        {
            printf("你不能在登录!\n");
            close(control_sockfd);
        }
            
    }
    else if (strncmp(cmd,"quit",4)==0)
    {
        ftp_quit(control_sockfd);
        close(control_sockfd);
        
    }
    }
    return 0;
}
예제 #2
0
int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        print_usage(argv[0]);
        return EXIT_FAILURE;
    }

    URL url;
    url_init(&url);
    int error = url_from_string(&url, argv[1]);
    if (error)
    {
        fprintf(stderr, "Could not sucessfully parse FTP url (error code: %d)\n", error);
        url_destroy(&url);
        return EXIT_FAILURE;
    }

    error = url_host_to_ip(&url);
    if (error)
    {
        fprintf(stderr, "Could not get an IPv4 IP address from hostname %s (error code: %d)\n", url.host, error);
        return EXIT_FAILURE;
    }

    FTP ftp;
    error = ftp_connect(&ftp, url.ip, url.port ? url.port : 21);
    if (error)
    {
        fprintf(stderr, "Could not connect to ftp at IP %s, port %d\n", url.ip, url.port ? url.port : 21);
        return EXIT_FAILURE;
    }

    const char* user = strlen(url.user) ? url.user : "******";
    const char* pass = strlen(url.password) ? url.password : "******";

    error = ftp_login(&ftp, user, pass);
    if (error)
    {
        fprintf(stderr, "Could not login with user %s and pass %s\n", user, pass);
        return EXIT_FAILURE;
    }

    char path[1024] = "";
    for (int i = 0; i < url.num_parts - 1; ++i) {
        strcat(path, url.parts[i]);
        strcat(path, "/");
    }

    if (path[0] != 0) {
        error = ftp_cwd(&ftp, path);
        if (error) {
            perror("ftp_cwd");
            return error;
        }
    }

    error = ftp_pasv(&ftp);
    if (error) {
        perror("ftp_pasv");
        return error;
    }

    const char* file_name = url.num_parts ? url.parts[url.num_parts - 1] : "";

    error = ftp_retr(&ftp, file_name);
    if (error) {
        perror("ftp_retr");
        return error;
    }

    error = ftp_download(&ftp, file_name);
    if (error) {
        perror("ftp_download");
        return error;
    }

    error = ftp_disconnect(&ftp);
    if (error) {
        perror("ftp_disconnect");
        return error;
    }

    url_destroy(&url);

    return EXIT_SUCCESS;
}
예제 #3
0
int cmd_testtools_ls2sd(struct command_context *ctx) {

	remoteShell_packet_t * message_send;
	remoteShell_packet_t * message_reply;
	char remote_path[REMOTE_MESSAGE_SIZE];

	message_send = (remoteShell_packet_t *) calloc(sizeof(remoteShell_packet_t), sizeof(char));
	message_reply = (remoteShell_packet_t *) calloc(sizeof(remoteShell_packet_t), sizeof(char));
	unsigned int now;

	char * out_buffer = calloc(10240, 1);

	if (!(message_send && message_reply)) {
		printf("Houston: calloc error.\n");
		goto err;
	}

	if (!(out_buffer)) {
		printf("Houston: malloc error.\n");
		goto err;
	}

	memset(remote_path, 0, REMOTE_MESSAGE_SIZE);

	int start = 0; // 0 means from start.
	int file_count = 0;
	int lastfilecount = 0;

	do {
		message_send->returned = csp_hton16(start);
		memcpy(message_reply, message_send, sizeof(remoteShell_packet_t));

		sprintf(remote_path, "/sd/");
		memcpy(message_send->command_text, remote_path, REMOTE_MESSAGE_SIZE);

		int packet_status = csp_transaction(CSP_PRIO_NORM, NODE_OBC, OBC_PORT_FTP_LIST, 6000,
		                                    message_send , sizeof(remoteShell_packet_t), message_reply, sizeof(remoteShell_packet_t));

		message_reply->returned = csp_ntoh16(message_reply->returned);
		file_count = message_reply->returned;

		printf("status = %d, ", packet_status);

		if (packet_status == sizeof(remoteShell_packet_t)) {
			now = (unsigned)time(NULL);
			if (message_reply->returned < 0) {
				printf("returned value = %d, ls failed. Re-formatting may required.\n", message_reply->returned);
				goto err;
			}
			memcpy(remote_path, message_reply->command_text, REMOTE_MESSAGE_SIZE);
			printf("%d files, path in sd is %s\n", message_reply->returned, remote_path);
			printf("successed listing files.\n");
		} else {
			printf("failed!\nNETWORK ERROR\n");
			goto err;
		}

		/* Then download list file. */

		char local_path[PATH_MAX]; // no need to malloc since it is part of remote_path
		memset(local_path, 0, PATH_MAX);
		getcwd(local_path, sizeof(local_path));
		strcat(local_path, "/");
		strcat(local_path, my_basename_1(remote_path));

		printf("local path is: %s\n", local_path);

		int download_status = ftp_download(NODE_OBC, OBC_PORT_FTP, local_path, ftp_backend, ftp_chunk_size, 0, 0, remote_path, &ftp_size);

		if (download_status != 0) {
			ftp_done(0);
			goto err;
		}

		if (ftp_status_reply() != 0) {
			ftp_done(0);
			goto err;
		}
		if (ftp_crc() != 0) {
			ftp_done(0);
			goto err;
		}

		ftp_done(1);

		usleep(100 * 1000); // 100 ms
		char remove_command[50];
		memset(remove_command, 0, 50);
		sprintf(remove_command, "<rm|%s>", remote_path);
		int remove_status = -1;
		send_remote_shell_command(remove_command, NULL, &remove_status);
		printf("remove status: %d\n", remove_status);


		int decompress_result = decompress_file_to_buffer(local_path, out_buffer, (uint32_t) 10240);

		if (decompress_result < 0) {
			printf("Decompress of %s failed. Maybe try to decompress this file in shell?\n", local_path);
			goto err;
		}

		char * pc_start;
		char * pc_end;
		int found_timestamp = 0;


		pc_start = strchr(out_buffer, parse_start);
		pc_end = strchr(out_buffer, parse_end);

		if (!(pc_start && pc_end))
			printf("timestamp not found. ugh.\n");
		else {
			found_timestamp = 1;
			pc_start += sizeof(char);
		}

		if (found_timestamp) {
			char list_time[32];
			memset(&list_time, 0, sizeof(list_time));
			memcpy(&list_time, pc_start, (pc_end - pc_start));

			printf("current time:%u, list time:%s\nTime difference: %d seconds.\n",
			       now , list_time, (now - (unsigned)atoi(list_time)));

			printf("%s\n", pc_end + sizeof(char));
		} else {
			printf("current time:%u, list time: unavailable\n", (unsigned)time(NULL));
			printf("%s\n", out_buffer);
		}

		char * last_legal_line = NULL;
		char * second_last_line = NULL;
		last_legal_line = strrchr(out_buffer, '\n');
		if (!last_legal_line) {
			printf("cannot find new line what happend?\n");
			break;
		}
		int i;
		for (i = 1; i < 32; ++i) {
			if (*(last_legal_line - i) == '\n' ) {
				second_last_line = last_legal_line - i;
				break;
			}
		}

		if (!second_last_line) {
			printf("cannot find new line what happend? quitting..\n");
			goto err;
		}

		char line[32] = {0};

		memcpy(line, second_last_line + 1, last_legal_line - second_last_line);
		sscanf (line, "%d\t", &lastfilecount);
		if (lastfilecount == file_count)
			printf("Finished listing.\n");
		else
			printf("downloading next listing file...\n");
		start = lastfilecount;
		memset(out_buffer, 0, 10240);

	}
	while (lastfilecount < file_count);

	/* cleaning starts. */
	if (out_buffer)
		free(out_buffer);
	if (message_send)
		free(message_send);
	if (message_reply)
		free(message_reply);

	return 0;

	/* Exception handlers */
err:
	if (out_buffer)
		free(out_buffer);
	if (message_send)
		free(message_send);
	if (message_reply)
		free(message_reply);
	return CMD_ERROR_FAIL;
}