char * skod_hc_fingerprint(ftp_t *ftp) { char *line = NULL; const char **ptr = NULL; unsigned long sum = 0; char ssum[MAX_STR]; static char patr[MAX_STR]; int size = sizeof(ftp_commands) / sizeof(ftp_commands[0]); int c = 1; bzero(patr, MAX_STR); bzero(ssum, MAX_STR); ftp_close(ftp); /* It's more safe to make connection to each request * This way we will get the fingerprint 100% */ for ( ptr = ftp_commands; *ptr; ptr++ ) { ftp_mkcon(ftp); fprintf(ftp->FD, "%s\r\n", *ptr); line = ftp_getline(ftp); sum = skod_checksum(line); sprintf(ssum, "%lu", sum); strcat(patr, ssum); if ( c == size ) break; strcat(patr,":"); c++; ftp_close(ftp); } return (char *)patr; }
int main() { ftp_init(); struct ftp_connect client; memcpy(client.hostname, "192.168.0.107", sizeof(client.hostname)); client.control_port = 21; memcpy(client.username, "odin", sizeof(client.username)); memcpy(client.password, "odin", sizeof(client.password)); client.mode = 0; struct file_description file; memcpy(file.remote_dir, "/test/X00/192.168.0.188/20121128/09", sizeof(file.remote_dir)); memcpy(file.file_name, "a.txt", sizeof(file.file_name)); int size = 1024 * 1024; char *ptr = (char *)malloc(size * sizeof(char)); file.file_buffer = ptr; file.file_size = size; if (ftp_open(&client) == FTP_OK) { ftp_upload(&client, &file); } ftp_close(&client); free(ptr); ptr = NULL; ftp_release(); return 0; }
int main(int argc, char **argv) { skod_t skod; ftp_t ftp; skod_init(&skod); skod_parse_cla(argc, argv, &skod); signal(SIGINT, signal_handler); /* Init FTP*/ ftp.user = skod.user; ftp.password = skod.password; ftp.server = skod.server; ftp.port = skod.port; ftp.alarm_sec = 1; /* High risk */ ftp_mkcon(&ftp); /* --dest, -e*/ if ( skod.dest != NULL ) ftp_cwd(&ftp, skod.dest); else if ( skod.dest == NULL && flag == 4 ) die("You need to pass --dest (destination folder) with --upload."); switch(flag) { case 1: ftp_list(&ftp, skod.path, 1); break; case 2: ftp_remove(&ftp, skod.path); break; case 3: ftp_download_single(&ftp, skod.path); break; case 4: ftp_upload_single(&ftp, skod.path); break; case 5: ftp_cat(&ftp, skod.path); break; case 6: printf("%d\n", ftp_size(&ftp, skod.path)); break; case 7: printf("%s\n", ftp_pwd(&ftp)); break; case 8: ftp_delete(&ftp, skod.path); break; case 10: ftp_mdtm(&ftp, skod.path); break; case 99: skod_scan(&skod, &ftp); break; } ftp_close(&ftp); return 0; }
/* Parse OS/STAT*/ void skod_parse_stat(skod_t *skod, ftp_t *ftp) { char *line = NULL; char buffer[MAX_STR]; const char **p = NULL; if ( ftp->logged ) { fprintf(ftp->FD, "STAT\r\n"); while (( fgets(buffer, sizeof(buffer), ftp->FD)) != NULL ) { for ( p = ftp_prod; *p;p++ ) { if ( strstr(buffer, *p)) print(3, "%s", buffer); } } print(0, " "); } ftp_close(ftp); ftp_mkcon(ftp); /* Parse OS */ fprintf(ftp->FD, "SYST\r\n"); line = ftp_getline(ftp); if (( strstr(line, "UNIX"))) skod->os = SKOD_OS_NIX; if (( strstr(line, "NT"))) skod->os = SKOD_OS_NT; }
static void reply_ALRM_handler(int signum) { ftp_err(_("Tired of waiting for reply, timeout after %u seconds\n"), ftp->reply_timeout); ftp_close(); if(gvJmpBufSet) { ftp_trace("jumping to gvRestartJmp\n"); ftp_longjmp(gvRestartJmp, 1); } else exit_yafc(); }
void ftp_quit(void) { #ifdef HAVE_LIBSSH if (ftp_connected() && !ftp->session) #else if (ftp_connected()) #endif { ftp_reply_timeout(10); ftp_set_tmp_verbosity(vbCommand); ftp_cmd("QUIT"); } ftp_close(); }
static int ftp_quit(FTP ftp) { /* * int status; * ftp_command(ftp, "QUIT", NULL, &status); * ftp_close(ftp); * if (status != 221) * return -1; */ ftp_command(ftp, "QUIT", NULL, NULL); ftp_close(ftp); return 0; }
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); }
int ftp_dir(FTP_CON * con, const char *file) /* display directory */ { char command[256], buffer[8192]; if (file == NULL || *file == '\0') strcpy(command, "LIST"); else sprintf(command, "LIST %s", file); if (ftp_data(con, command, "") >= 0) return con->err_no; while (ftp_receive(con->data, buffer, sizeof(buffer))) printf("%s", buffer); return ftp_close(con); }
int ftp_reopen(void) { if(ftp && ftp->url) { url_t *u = url_clone(ftp->url); int r; url_setdirectory(u, ftp->curdir); r = ftp_open_url(u, false); if(r == 0) { r = ftp_login(u->username, gvAnonPasswd); } else ftp_close(); url_destroy(u); return r; } return -1; }
int ftp_get(FTP_CON * con, const char *local_name, const char *remote_name) /* get file */ { int fh; int status; char buff[8192]; char str[256]; int count, i; long total = 0; DWORD start, stop; if (ftp_open_read(con, remote_name) >= 0) return con->err_no; if ((fh = open(local_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0644)) == -1) return FTP_FILE_ERROR; start = ss_millitime(); while ((count = ftp_receive(con->data, buff, sizeof(buff))) > 0) { total += write(fh, buff, count); i = 0; if (ftp_debug_func != NULL) { printf("%c\r", bars[(i++) % 4]); fflush(stdout); } } close(fh); stop = ss_millitime(); status = ftp_close(con); if (ftp_debug_func != NULL) { sprintf(str, "%ld bytes received in %1.2f seconds (%1.2lf kB/sec).", total, (stop - start) / 1000.0, total / 1024.0 / ((stop - start) / 1000.0)); ftp_debug_func(str); } return status; }
int ftp_put(FTP_CON * con, const char *local_name, const char *remote_name) /* put file */ { int fh; int status; char buff[8193]; char str[256]; int count, i = 0; long total = 0; DWORD start, stop; if (ftp_open_write(con, remote_name) >= 0) return con->err_no; if ((fh = open(local_name, O_BINARY)) == -1) return FTP_FILE_ERROR; start = ss_millitime(); while ((count = read(fh, buff, 8192)) > 0) { total += ftp_send(con->data, buff, count); if (ftp_debug_func != NULL) { printf("%c\r", bars[(i++) % 4]); fflush(stdout); } } close(fh); stop = ss_millitime(); status = ftp_close(con); if (ftp_debug_func != NULL) { sprintf(str, "%ld bytes sent in %1.2f seconds (%1.2lf kB/sec).", total, (stop - start) / 1000.0, total / 1024.0 / ((stop - start) / 1000.0)); ftp_debug_func(str); } return status; }
static int ftp_login(FTP ftp) { int sock, status; sock = openSocket(ftp->host, "ftp", 21); if (sock < 0) goto open_err; if (ftppass_hostnamegen && !strcmp(ftp->user, "anonymous")) { size_t n = strlen(ftp->pass); if (n > 0 && ftp->pass[n - 1] == '@') { #ifdef INET6 struct sockaddr_storage sockname; #else struct sockaddr_in sockname; #endif socklen_t socknamelen = sizeof(sockname); if (!getsockname(sock, (struct sockaddr *)&sockname, &socknamelen)) { struct hostent *sockent; Str tmp = Strnew_charp(ftp->pass); #ifdef INET6 char hostbuf[NI_MAXHOST]; if (getnameinfo((struct sockaddr *)&sockname, socknamelen, hostbuf, sizeof hostbuf, NULL, 0, NI_NAMEREQD) == 0) Strcat_charp(tmp, hostbuf); else if (getnameinfo((struct sockaddr *)&sockname, socknamelen, hostbuf, sizeof hostbuf, NULL, 0, NI_NUMERICHOST) == 0) Strcat_m_charp(tmp, "[", hostbuf, "]", NULL); else Strcat_charp(tmp, "unknown"); #else if ((sockent = gethostbyaddr((char *)&sockname.sin_addr, sizeof(sockname.sin_addr), sockname.sin_family))) Strcat_charp(tmp, sockent->h_name); else Strcat_m_charp(tmp, "[", inet_ntoa(sockname.sin_addr), "]", NULL); #endif ftp->pass = tmp->ptr; } } } ftp->rf = newInputStream(sock); ftp->wf = fdopen(dup(sock), "wb"); if (!ftp->rf || !ftp->wf) goto open_err; IStype(ftp->rf) |= IST_UNCLOSE; ftp_command(ftp, NULL, NULL, &status); if (status != 220) goto open_err; if (fmInitialized) { message(Sprintf("Sending FTP username (%s) to remote server.", ftp->user)->ptr, 0, 0); refresh(); } ftp_command(ftp, "USER", ftp->user, &status); /* * Some ftp daemons(e.g. publicfile) return code 230 for user command. */ if (status == 230) goto succeed; if (status != 331) goto open_err; if (fmInitialized) { message("Sending FTP password to remote server.", 0, 0); refresh(); } ftp_command(ftp, "PASS", ftp->pass, &status); if (status != 230) goto open_err; succeed: return TRUE; open_err: ftp_close(ftp); return FALSE; }
InputStream openFTPStream(ParsedURL *pu, URLFile *uf) { Str tmp; int status; char *user = NULL; char *pass = NULL; Str uname = NULL; Str pwd = NULL; int add_auth_cookie_flag = FALSE; char *realpathname = NULL; if (!pu->host) return NULL; if (pu->user == NULL && pu->pass == NULL) { if (find_auth_user_passwd(pu, NULL, &uname, &pwd, 0)) { if (uname) user = uname->ptr; if (pwd) pass = pwd->ptr; } } if (user) /* do nothing */ ; else if (pu->user) user = pu->user; else user = "******"; if (current_ftp.host) { if (!strcmp(current_ftp.host, pu->host) && current_ftp.port == pu->port && !strcmp(current_ftp.user, user)) { ftp_command(¤t_ftp, "NOOP", NULL, &status); if (status != 200) ftp_close(¤t_ftp); else goto ftp_read; } else ftp_quit(¤t_ftp); } if (pass) /* do nothing */ ; else if (pu->pass) pass = pu->pass; else if (pu->user) { pwd = NULL; find_auth_user_passwd(pu, NULL, &uname, &pwd, 0); if (pwd == NULL) { if (fmInitialized) { term_raw(); pwd = Strnew_charp(inputLine("Password: "******"Password: "******"Password: "******"anonymous"); #else tmp = Strnew_charp("anonymous"); #endif /* __MINGW32_VERSION */ Strcat_char(tmp, '@'); pass = tmp->ptr; } if (!current_ftp.host) { current_ftp.host = allocStr(pu->host, -1); current_ftp.port = pu->port; current_ftp.user = allocStr(user, -1); current_ftp.pass = allocStr(pass, -1); if (!ftp_login(¤t_ftp)) return NULL; } if (add_auth_cookie_flag) add_auth_user_passwd(pu, NULL, uname, pwd, 0); ftp_read: ftp_command(¤t_ftp, "TYPE", "I", &status); if (ftp_pasv(¤t_ftp) < 0) { ftp_quit(¤t_ftp); return NULL; } if (pu->file == NULL || *pu->file == '\0' || pu->file[strlen(pu->file) - 1] == '/') goto ftp_dir; realpathname = file_unquote(pu->file); if (*realpathname == '/' && *(realpathname + 1) == '~') realpathname++; /* Get file */ uf->modtime = ftp_modtime(¤t_ftp, realpathname); ftp_command(¤t_ftp, "RETR", realpathname, &status); if (status == 125 || status == 150) return newFileStream(current_ftp.data, (void (*)())closeFTPdata); ftp_dir: pu->scheme = SCM_FTPDIR; return NULL; }
void closeFTP(void) { ftp_close(¤t_ftp); }
/*------------------------------------------------------------------*/ INT md_file_wclose(INT handle, INT type, INT data_fmt, char *destination) /******************************************************************** Routine: external md_file_wclose Purpose: Close a data file used for replay for the given data format Input: INT data_fmt : YBOS or MIDAS Output: none Function value: status : from lower function *******************************************************************/ { INT status; status = SS_SUCCESS; switch (type) { case LOG_TYPE_TAPE: /* writing EOF mark on tape Fonly */ status = ss_tape_write_eof(handle); ss_tape_close(handle); break; case LOG_TYPE_DISK: /* close file */ if (handle != 0) #ifdef OS_WINNT CloseHandle((HANDLE) handle); #else close(handle); #endif break; case LOG_TYPE_FTP: #ifdef HAVE_FTPLIB { char *p, filename[256]; int i; ftp_close(ftp_con); /* destination should have the form: host, port, user, password, directory, run%05d.mid, file_mode, command, ... */ p = destination; for (i=0 ; i<5 && p != NULL ; i++) { p = strchr(p, ','); if (*p == ',') p++; } if (p != NULL) { strlcpy(filename, p, sizeof(filename)); if (strchr(filename, ',')) *strchr(filename, ',') = 0; } else strlcpy(filename, destination, sizeof(filename)); /* if filename starts with a '.' rename it */ if (filename[0] == '.') ftp_move(ftp_con, filename, filename+1); ftp_bye(ftp_con); } #endif break; } if (status != SS_SUCCESS) return status; return (MD_SUCCESS); }
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"); } }
static void ftp_destructor_ftpbuf(zend_resource *rsrc) { ftpbuf_t *ftp = (ftpbuf_t *)rsrc->ptr; ftp_close(ftp); }
/* reads one line from server into ftp->reply * returns 0 on success or -1 on failure */ static int ftp_gets(void) { int c, i=0; ftp->reply[0] = 0; if(!sock_connected(ftp->ctrl)) { ftp_err(_("No control connection\n")); return -1; } while(true) { c = sock_get(ftp->ctrl); if(c == EOF) { ftp_err(_("Server has closed control connection\n")); ftp_close(); return -1; } else if(c == 255/*IAC*/) { /* handle telnet commands */ switch(c = sock_get(ftp->ctrl)) { case 251/*WILL*/: case 252/*WONT*/: c = sock_get(ftp->ctrl); sock_printf(ftp->ctrl, "%c%c%c", 255/*IAC*/, 254/*DONT*/, c); sock_flush(ftp->ctrl); break; case 253/*DO*/: case 254/*DONT*/: c = sock_get(ftp->ctrl); sock_printf(ftp->ctrl, "%c%c%c", 255/*IAC*/, 252/*WONT*/, c); sock_flush(ftp->ctrl); break; default: break; } continue; } else if(c == '\r') { c = sock_get(ftp->ctrl); if(c == 0) c = '\r'; else if(c == '\n') { ftp->reply[i++] = (char)c; break; } else if(c == EOF) /* do nothing */ ; else { /* telnet protocol violation, hmpf... */ sock_unget(ftp->ctrl, c); continue; } } else if(c == '\n') break; if(i < MAXREPLY) ftp->reply[i++] = (char)c; } if(i >= MAXREPLY) { ftp_err(_("Reply too long (truncated)\n")); i = MAXREPLY; } ftp->reply[i] = '\0'; ftp->fullcode = atoi(ftp->reply); #ifdef SECFTP { int r = 0; switch(ftp->fullcode) { /* handle protected responses 6xx */ case 631: r = sec_read_msg(ftp->reply, prot_safe); break; case 632: r = sec_read_msg(ftp->reply, prot_private); break; case 633: r = sec_read_msg(ftp->reply, prot_confidential); break; } if(r == -1) { ftp->fullcode = 0; ftp->code = vbNone; return 0; } else ftp->fullcode = atoi(ftp->reply); } #endif strip_trailing_chars(ftp->reply, "\n\r"); ftp->code = ftp->fullcode / 100; return ftp->fullcode; }
int ftp_open_url(url_t *urlp, bool reset_vars) { bool use_proxy; int i; if(reset_vars) ftp_reset_vars(); /* don't assume server is in ascii mode initially even if RFC says so */ ftp->prev_type = '?'; #ifdef HAVE_POSIX_SIGSETJMP if(sigsetjmp(open_timeout_jmp, 1)) #else if(setjmp(open_timeout_jmp)) #endif { ftp_close(); ftp_err(_("Connection timeout after %u seconds\n"), ftp->open_timeout); return 1; } ftp_set_signal(SIGALRM, ftp_open_handler); alarm(ftp->open_timeout); use_proxy = (proxy_type(urlp) != 0); ftp_err(_("Looking up %s... "), use_proxy ? gvProxyUrl->hostname : urlp->hostname); /* Set the default port (22) for SSH if no port is specified. We * need to do this here, 'cause host_lookup() sets it to 21 * (default port for vanilla FTP) */ if(urlp->protocol) { if(strcmp(urlp->protocol, "sftp") == 0) url_setprotocol(urlp, "ssh"); if(strcmp(urlp->protocol, "ssh") == 0 && urlp->port == -1) urlp->port = 22; /* default SSH port */ } ftp->host = host_create(use_proxy ? gvProxyUrl : urlp); if(!host_lookup(ftp->host)) { herror(host_getname(ftp->host)); alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); return -1; } /* keep the value in urlp->port urlp->port = ntohs(ftp->host->port); and set it to 21 if it is -1 */ if(urlp->port == -1) { urlp->port = 21; } fprintf(stderr, "\r "); i = strlen(use_proxy ? gvProxyUrl->hostname : urlp->hostname); while(i--) fprintf(stderr, " "); fprintf(stderr, "\r"); ftp_trace("\n"); #ifdef HAVE_LIBSSH if(urlp->protocol && strcmp(urlp->protocol, "ssh") == 0) { int ret = ssh_open_url(urlp); alarm(0); return ret; } #endif if(urlp->protocol && strcmp(urlp->protocol, "ftp") != 0) { ftp_err(_("Sorry, don't know how to handle your '%s' protocol\n" "trying 'ftp' instead...\n"), urlp->protocol); url_setprotocol(urlp, 0); } if(use_proxy) { ftp_err(_("Connecting to proxy %s at port %d...\n"), host_getoname(ftp->host), urlp->port); } else { ftp_err(_("Connecting to %s at port %d...\n"), host_getoname(ftp->host), urlp->port); } ftp->ctrl = sock_create(); if (ftp->ctrl == 0) { ftp_err(_("Unable to create socket.\n")); alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); return -1; } if(!sock_connect_host(ftp->ctrl, ftp->host)) { alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); return -1; } sock_lowdelay(ftp->ctrl); char* ip = host_getip(ftp->host); ftp_err(_("Connected to %s ([%s]:%d).\n"), host_getoname(ftp->host), ip, urlp->port); free(ip); /* read startup message from server */ ftp_set_tmp_verbosity(vbCommand); ftp_read_reply(); if(ftp->fullcode == 120) { ftp_set_tmp_verbosity(vbCommand); ftp_read_reply(); } alarm(0); ftp_set_signal(SIGALRM, SIG_IGN); if(!sock_connected(ftp->ctrl)) { ftp_close(); return 1; } ftp->connected = (ftp->fullcode == 220); if(ftp->connected) { void (*tracefunq)(const char *fmt, ...); url_destroy(ftp->url); ftp->url = url_clone(urlp); tracefunq = (ftp->verbosity == vbDebug ? ftp_err : ftp_trace); char* remote_addr = printable_address(sock_remote_addr(ftp->ctrl)), *local_addr = printable_address(sock_local_addr(ftp->ctrl)); tracefunq("remote address: %s\n", remote_addr); tracefunq("local address: %s\n", local_addr); free(remote_addr); free(local_addr); return 0; } else { ftp_close(); return 1; } }