コード例 #1
0
ファイル: ftp.c プロジェクト: snimmagadda/http
int
ftp_connect(struct url *url)
{
	char	buf[MAX_LINE];
	int	r = -1;

	if (url->port[0] == '\0')
		(void)strlcpy(url->port, "21", sizeof url->port);

	if ((ctrl_sock = tcp_connect(url->host, url->port)) == -1)
		return -1;

	if (proxy && proxy_connect(ctrl_sock, url) == -1)
		goto error;

	/* greeting */
	if ((r = ftp_readline(ctrl_sock, buf, sizeof buf)) != P_OK)
		goto error;

	log_info("Connected to %s\n", url->host);
	if ((r = ftp_auth(NULL, NULL)) != P_OK)
		goto error;

	return ctrl_sock;
error:
	if (r == -1 && errno != EINTR)
		warnx("Can't connect or login to host `%s'", url->host);

	ftp_command(ctrl_sock, "QUIT\r\n");
	if (close(ctrl_sock) == -1 && errno != EINTR)
		err(1, "ftp_connect: close");

	return -1;
}
コード例 #2
0
ファイル: main.c プロジェクト: cf981378640/libmftp
void libmftp_tls_test(char *host, unsigned int port, char *user, char *pw, char *workingdirectory)
{
	ftp_connection *c = ftp_open(host, port, ftp_security_always);
	if (!c) {
		printf("No TLS connection!\n");
		return;
	}

	if (ftp_auth(c, user, pw, ftp_btrue) != FTP_OK) {
		printf("Could not authenticate. Error: %i\n", c->error);
	}

	if (ftp_item_exists(c, "httpdocs", NULL)) {
		printf("exists!°!11e3degvrbnm\n");
	}

	ftp_close(c);
}
コード例 #3
0
ファイル: main.c プロジェクト: cf981378640/libmftp
void libmftp_main_test(char *host, unsigned int port, char *user, char *pw, char *workingdirectory)
{
	int success = 0;
	ftp_file *f = NULL, *g = NULL;
	ftp_content_listing *cl = NULL, *cl2 = NULL;
	ftp_date d;
	char *buf = NULL;
	ftp_connection *c = ftp_open(host, port, tls == 0 ? ftp_security_none : ftp_security_always);

	/*c->_current_features->use_mlsd = ftp_bfalse;*/

	if (!c) {
		printf("Could not connect. Error: %i\n",ftp_error);
		if (ftp_error == FTP_ESECURITY) {
			printf("This server does not support a TLS connection. FTP_ESECURITY\n");
		}
		goto end;
	}

	if (ftp_auth(c, user, pw, ftp_btrue) != FTP_OK) {
		printf("Could not authenticate. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_change_cur_directory(c, workingdirectory) != FTP_OK) {
		printf("Could not cwd. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_reload_cur_directory(c) != FTP_OK) {
		printf("Could not reload wd. Error: %i\n", c->error);
		goto end;
	}

	if (strcmp(workingdirectory, c->cur_directory) != 0) {
		printf("Warning: Working dir (%s) does not match ftp dir (%s)! Continue? (y/n)\n",workingdirectory,c->cur_directory);
		if (getchar() != 'y') {
			goto end;
		}
	}

	char *test = "This is a test string that will be written to the server.";
	size_t test_len = strlen(test);

	//TEST UPLOAD 1

	f = ftp_fopen(c, "testfile.test", FTP_WRITE, 0);
	if (!f) {
		printf("Could not fopen to write. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_fwrites(test, f) != test_len) {
		printf("Could not write test string. Error: %i\n", *(f->error));
		goto end;
	}

	ftp_fclose(f);
	f = NULL;

	//TEST CONTENT LISTING

	int entry_count;
	cl = ftp_contents_of_directory(c, &entry_count);

	if (!cl) {
		printf("Could not get content listing. Error: %i\n", c->error);
		goto end;
	}

	if (entry_count != 1 || cl->next) {
		printf("More than 1 entry in content listing.\n");
		goto end;
	}

	if (!ftp_item_exists_in_content_listing(cl, "testfile.test", &cl2)) {
		printf("Could not find previously generated file in content listing.\n");
		goto end;
	}

	if (!cl2) {
		printf("Did not get a pointer to the content listing entry.\n");
		goto end;
	}

	if (!cl2->facts.given.modify) {
		printf("Warning: Remote host does not provide modification date information! Continue? (y/n)\n");
		if (getchar() != 'y') {
			goto end;
		}
	} else {
		time_t cur = time(NULL);
		struct tm *curtm = localtime(&cur);
		if (curtm->tm_year + 1900 != cl2->facts.modify.year) {
			printf("Warning: Modification year of remote file (%i) does not equal local year (%i). Continue? (y/n)\n",cl2->facts.modify.year,curtm->tm_year + 1900);
			if (getchar() != 'y') {
				goto end;
			}
		}
	}

	//TEST SIZE

	size_t srv_size;
	if (ftp_size(c, "testfile.test", &srv_size) != FTP_OK) {
		printf("Could not get file size. Error: %i\n", c->error);
		goto end;
	}

	if (srv_size != test_len) {
		printf("Remote file size (%lu) differs from local file size (%lu).\n",srv_size,test_len);
		goto end;
	}

	//TEST READ

	f = ftp_fopen(c, "testfile.test", FTP_READ, 0);
	if (!f) {
		printf("Could not fopen to read. Error: %i\n", c->error);
		goto end;
	}

	buf = malloc(srv_size + 1);

	if (ftp_fread(buf, 1, srv_size, f) != srv_size) {
		printf("Could not read file. Error: %i\n", *(f->error));
		goto end;
	}

	ftp_fclose(f);
	f = NULL;

	*(buf+srv_size) = '\0';
	if (strcmp(test, buf) != 0) {
		printf("Remote file content differs from local file content.\n");
		goto end;
	}

	//TEST UPLOAD 2 (SIMULTANEOUS)

	f = ftp_fopen(c, "testfile1.txt", FTP_WRITE, 0);
	if (!f) {
		printf("Could not fopen to write 2 (1). Error: %i\n", c->error);
		goto end;
	}
	g = ftp_fopen(c, "testfile2.txt", FTP_WRITE, 0);
	if (!g) {
		printf("Could not fopen to write 2 (2). Error: %i\n", c->error);
		goto end;
	}
	if (ftp_fwrites(test, f) != test_len) {
		printf("Could not write test string 2 (1). Error: %i\n", *(f->error));
		goto end;
	}
	if (ftp_fwrites(test, g) != test_len) {
		printf("Could not write test string 2 (2). Error: %i\n", *(g->error));
		goto end;
	}
	ftp_fclose(f);
	ftp_fclose(g);
	f = g = NULL;

	//TEST SIZE 2

	size_t srv_size2;
	if (ftp_size(c, "testfile1.txt", &srv_size) != FTP_OK) {
		printf("Could not get file size 2 (1). Error: %i\n", c->error);
		goto end;
	}
	if (ftp_size(c, "testfile2.txt", &srv_size2) != FTP_OK) {
		printf("Could not get file size 2 (2). Error: %i\n", c->error);
		goto end;
	}
	if (srv_size != test_len || srv_size2 != test_len) {
		printf("Remote file size 2 differs from local file size.\n");
		goto end;
	}

	//TEST FOLDERS

	if (ftp_create_folder(c, "testfolder") != FTP_OK) {
		printf("Could not create folder. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_move(c, "testfile1.txt", "testfolder/testfile1.txt")) {
		printf("Could not move file. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_change_cur_directory(c, "testfolder") != FTP_OK) {
		printf("Could not cwd 2. Error: %i\n", c->error);
		goto end;
	}

	if (!ftp_item_exists(c, "testfile1.txt", NULL)) {
		printf("File does not exist where it was moved to.");
		goto end;
	}

	//TEST DELETE

	if (ftp_delete(c, "testfile1.txt", ftp_bfalse) != FTP_OK) {
		printf("Could not delete file. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_item_exists(c, "testfile1.txt", NULL)) {
		printf("Deleted file still exists.");
		goto end;
	}

	if (ftp_change_cur_directory(c, workingdirectory) != FTP_OK) {
		printf("Could not cwd 3. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_delete(c, "testfolder", ftp_btrue) != FTP_OK) {
		printf("Could not delete folder. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_item_exists(c, "testfolder", NULL)) {
		printf("Deleted folder still exists.");
		goto end;
	}

	if (ftp_delete(c, "testfile.test", ftp_bfalse) != FTP_OK || ftp_delete(c, "testfile2.txt", ftp_bfalse) != FTP_OK) {
		printf("Could not clean up. Error: %i\n", c->error);
		goto end;
	}

	success = 1;
end:
	if (cl) ftp_free(cl);
	if (f) ftp_fclose(f);
	if (g) ftp_fclose(g);
	if (c) ftp_close(c);
	if (buf) free(buf);
	if (!success) {
		printf("Test was NOT successful. :-(\n");
	} else {
		printf("Test was successful! :-)\n");
	}
}