/*
* Copyright (c) 1985, 1989 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* From: @(#)ftp.c 5.38 (Berkeley) 4/22/91
*/
char ftp_rcsid[] =
"$Id: ftp.c,v 1.25 1999/12/13 20:33:20 dholland Exp $";
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/ftp.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <pwd.h>
#include <stdarg.h>
#include "ftp_var.h"
#include "version.h"
int data = -1;
static struct sockaddr_in hisctladdr;
static struct sockaddr_in data_addr;
static struct sockaddr_in myctladdr;
static sigjmp_buf recvabort;
static sigjmp_buf sendabort;
static int ptabflg = 0;
static int abrtflag = 0;
void lostpeer(int);
extern int connected;
extern int ftp_passive;
extern int ftp_verbose;
extern int ftp_sendport;
extern int ftp_debug;
static int initconn(void);
static void ptransfer(const char *direction, long bytes,
const struct timeval *t0,
const struct timeval *t1);
static void tvsub(struct timeval *tdiff,
const struct timeval *t1,
const struct timeval *t0);
static void abort_remote(FILE *din);
FILE *cin, *cout;
static FILE *dataconn(const char *);
char *
hookup(char *host, int port)
{
register struct hostent *hp = 0;
int s, tos;
socklen_t len;
static char hostnamebuf[256];
memset(&hisctladdr, 0, sizeof(hisctladdr));
if (inet_aton(host, &hisctladdr.sin_addr)) {
hisctladdr.sin_family = AF_INET;
strncpy(hostnamebuf, host, sizeof(hostnamebuf));
hostnamebuf[sizeof(hostnamebuf)-1]=0;
}
else {
hp = gethostbyname(host);
if (hp == NULL) {
fprintf(stderr, "ftp: %s: ", host);
herror((char *)NULL);
code = -1;
return((char *) 0);
}
hisctladdr.sin_family = hp->h_addrtype;
if (hp->h_length > (int)sizeof(hisctladdr.sin_addr)) {
hp->h_length = sizeof(hisctladdr.sin_addr);
}
memcpy(&hisctladdr.sin_addr, hp->h_addr_list[0], hp->h_length);
(void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
hostnamebuf[sizeof(hostnamebuf)-1] = 0;
}
hostname = hostnamebuf;
s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
if (s < 0) {
perror("ftp: socket");
code = -1;
return (0);
}
hisctladdr.sin_port = port;
while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
if (hp && hp->h_addr_list[1]) {
int oerrno = errno;
fprintf(stderr, "ftp: connect to address %s: ",
inet_ntoa(hisctladdr.sin_addr));
errno = oerrno;
perror((char *) 0);
hp->h_addr_list++;
memcpy(&hisctladdr.sin_addr, hp->h_addr_list[0],
hp->h_length);
fprintf(stdout, "Trying %s...\n",
inet_ntoa(hisctladdr.sin_addr));
(void) close(s);
s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
if (s < 0) {
perror("ftp: socket");
code = -1;
return (0);
}
continue;
}
perror("ftp: connect");
code = -1;
goto bad;
}
len = sizeof (myctladdr);
if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
perror("ftp: getsockname");
code = -1;
goto bad;
}
#ifdef IP_TOS
tos = IPTOS_LOWDELAY;
if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
perror("ftp: setsockopt TOS (ignored)");
#endif
cin = fdopen(s, "r");
cout = fdopen(s, "w");
if (cin == NULL || cout == NULL) {
fprintf(stderr, "ftp: fdopen failed.\n");
if (cin)
(void) fclose(cin);
if (cout)
(void) fclose(cout);
code = -1;
goto bad;
}
if (ftp_verbose)
printf("Connected to %s.\n", hostname);
if (getreply(0) > 2) { /* read startup message from server */
if (cin)
(void) fclose(cin);
if (cout)
(void) fclose(cout);
code = -1;
goto bad;
}
#ifdef SO_OOBINLINE
{
int on = 1;
if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
< 0 && ftp_debug) {
perror("ftp: setsockopt");
}
}
#endif /* SO_OOBINLINE */
return (hostname);
bad:
(void) close(s);
return ((char *)0);
}
int
dologin(const char *luser, const char *pass)
{
int n;
n = command("USER %s", luser);
if (n == CONTINUE) {
n = command("PASS %s", pass);
}
if (n == CONTINUE) {
fprintf(stderr, "Server asked account!!\n");
return 1;
}
if (n != COMPLETE) {
fprintf(stderr, "Login failed.\n");
return 1;
}
return 0;
}
static void
cmdabort(int ignore)
{
(void)ignore;
printf("\n");
fflush(stdout);
abrtflag++;
}
int
command(const char *fmt, ...)
{
va_list ap;
int r;
void (*oldintr)(int);
abrtflag = 0;
if (ftp_debug) {
printf("---> ");
va_start(ap, fmt);
if (strncmp("PASS ", fmt, 5) == 0)
printf("PASS XXXX");
else
vfprintf(stdout, fmt, ap);
va_end(ap);
printf("\n");
(void) fflush(stdout);
}
if (cout == NULL) {
perror ("No control connection for command");
code = -1;
return (0);
}
oldintr = signal(SIGINT, cmdabort);
va_start(ap, fmt);
vfprintf(cout, fmt, ap);
va_end(ap);
fprintf(cout, "\r\n");
(void) fflush(cout);
cpend = 1;
r = getreply(!strcmp(fmt, "QUIT"));
if (abrtflag && oldintr != SIG_IGN)
(*oldintr)(SIGINT);
(void) signal(SIGINT, oldintr);
return(r);
}
char reply_string[BUFSIZ]; /* last line of previous reply */
char lreply[BUFSIZ];
#include <ctype.h>
int
getreply(int expecteof)
{
register int c, n;
register int dig;
register char *cp, *lcp;
int originalcode = 0, continuation = 0;
void (*oldintr)(int);
int pflag = 0;
size_t px = 0;
size_t psize = sizeof(pasv);
lcp = lreply;
oldintr = signal(SIGINT, cmdabort);
for (;;) {
dig = n = code = 0;
cp = reply_string;
while ((c = getc(cin)) != '\n') {
if (c == IAC) { /* handle telnet commands */
switch (c = getc(cin)) {
case WILL:
case WONT:
c = getc(cin);
fprintf(cout, "%c%c%c", IAC, DONT, c);
(void) fflush(cout);
break;
case DO:
case DONT:
c = getc(cin);
fprintf(cout, "%c%c%c", IAC, WONT, c);
(void) fflush(cout);
break;
default:
break;
}
continue;
}
dig++;
if (c == EOF) {
if (expecteof) {
(void) signal(SIGINT,oldintr);
code = 221;
return (0);
}
lostpeer(0);
i
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共10个文件
c:4个
h:3个
makefile:1个
ftplib is a set of routines that implement the FTP protocol. They allow applications to create and access remote files through function calls instead of needing to fork and exec an interactive ftp client program.
资源推荐
资源详情
资源评论
收起资源包目录
ftplib.tgz (10个子文件)
ftplib
version.h 105B
ftplib.h 3KB
example.c 1007B
ftp.ml 2KB
ftplib.c 10KB
caml.c 4KB
README 250B
ftp_var.h 4KB
Makefile 926B
ftp.c 22KB
共 10 条
- 1
资源评论
- 燕子的心情2016-06-12还可以,基本上能用
- realdada2013-01-15还可以,基本上能用
- makill_kill2014-10-03不错,写的还详细
- 此间的青年2013-12-16不错,还在想怎么做文件夹传输功能
圈地自萌程序媛
- 粉丝: 14
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功