/***************************************************************************/
/* */
/* ftplib.c - callable ftp access routines */
/* Copyright (C) 1996, 1997 Thomas Pfau, pfau@cnj.digex.net */
/* 73 Catherine Street, South Bound Brook, NJ, 08880 */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Library General Public */
/* License as published by the Free Software Foundation; either */
/* version 2 of the License, or (at your option) any later version. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
/* Library General Public License for more details. */
/* */
/* You should have received a copy of the GNU Library General Public */
/* License along with this progam; if not, write to the */
/* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */
/* Boston, MA 02111-1307, USA. */
/* */
/***************************************************************************/
#if defined(__unix__) || defined(__VMS)
#include <unistd.h>
#endif
#if defined(_WIN32)
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#if defined(__unix__)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#elif defined(VMS)
#include <types.h>
#include <socket.h>
#include <in.h>
#include <netdb.h>
#include <inet.h>
#elif defined(_WIN32)
#include <winsock.h>
#endif
#define BUILDING_LIBRARY
#include "ftplib.h"
#if defined(_WIN32)
#define SETSOCKOPT_OPTVAL_TYPE (const char *)
#else
#define SETSOCKOPT_OPTVAL_TYPE (void *)
#endif
#define FTPLIB_BUFSIZ 8192
#define ACCEPT_TIMEOUT 30
#define FTPLIB_CONTROL 0
#define FTPLIB_READ 1
#define FTPLIB_WRITE 2
struct NetBuf {
char *cput,*cget;
int handle;
int cavail,cleft;
char *buf;
int dir;
char response[256];
};
static char *version =
"ftplib Release 3 12/xx/97, copyright 1996, 1997 Thomas Pfau";
GLOBALDEF int ftplib_debug = 0;
#if defined(__unix__) || defined(VMS)
#define net_read read
#define net_write write
#define net_close close
#elif defined(_WIN32)
#define net_read(x,y,z) recv(x,y,z,0)
#define net_write(x,y,z) send(x,y,z,0)
#define net_close closesocket
#endif
#if defined(VMS)
/*
* VAX C does not supply a memccpy routine so I provide my own
*/
void *memccpy(void *dest, const void *src, int c, size_t n)
{
int i=0;
const unsigned char *ip=src;
unsigned char *op=dest;
while (i < n)
{
if ((*op++ = *ip++) == c)
break;
i++;
}
if (i == n)
return NULL;
return op;
}
#endif
/*
* read a line of text
*
* return -1 on error or bytecount
*/
static int readline(char *buf,int max,netbuf *ctl)
{
int x,retval = 0;
char *end,*bp=buf;
int eof = 0;
if ((ctl->dir != FTPLIB_CONTROL) && (ctl->dir != FTPLIB_READ))
return -1;
if (max == 0)
return 0;
do
{
if (ctl->cavail > 0)
{
x = (max >= ctl->cavail) ? ctl->cavail : max-1;
end = memccpy(bp,ctl->cget,'\n',x);
if (end != NULL)
x = end - bp;
retval += x;
bp += x;
*bp = '\0';
max -= x;
ctl->cget += x;
ctl->cavail -= x;
if (end != NULL)
{
bp -= 2;
if (strcmp(bp,"\r\n") == 0)
{
*bp++ = '\n';
*bp++ = '\0';
--retval;
}
break;
}
}
if (max == 1)
{
*buf = '\0';
break;
}
if (ctl->cput == ctl->cget)
{
ctl->cput = ctl->cget = ctl->buf;
ctl->cavail = 0;
ctl->cleft = FTPLIB_BUFSIZ;
}
if (eof)
{
if (retval == 0)
retval = -1;
break;
}
if ((x = net_read(ctl->handle,ctl->cput,ctl->cleft)) == -1)
{
perror("read");
retval = -1;
break;
}
if (x == 0)
eof = 1;
ctl->cleft -= x;
ctl->cavail += x;
ctl->cput += x;
}
while (1);
return retval;
}
/*
* write lines of text
*
* return -1 on error or bytecount
*/
static int writeline(char *buf, int len, netbuf *nData)
{
int x, nb=0, w;
char *ubp = buf, *nbp;
char lc=0;
if (nData->dir != FTPLIB_WRITE)
return -1;
nbp = nData->buf;
for (x=0; x < len; x++)
{
if ((*ubp == '\n') && (lc != '\r'))
{
if (nb == FTPLIB_BUFSIZ)
{
w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
if (w != FTPLIB_BUFSIZ)
{
printf("net_write(1) returned %d, errno = %d\n", w, errno);
return(-1);
}
nb = 0;
}
nbp[nb++] = '\r';
}
if (nb == FTPLIB_BUFSIZ)
{
w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
if (w != FTPLIB_BUFSIZ)
{
printf("net_write(2) returned %d, errno = %d\n", w, errno);
return(-1);
}
nb = 0;
}
nbp[nb++] = lc = *ubp++;
}
if (nb)
{
w = net_write(nData->handle, nbp, nb);
if (w != nb)
{
printf("net_write(3) returned %d, errno = %d\n", w, errno);
return(-1);
}
}
return len;
}
/*
* read a response from the server
*
* return 0 if first char doesn't match
* return 1 if first char matches
*/
static int readresp(char c, netbuf *nControl)
{
char match[5];
if (readline(nControl->response,256,nControl) == -1)
{
perror("Control socket read failed");
return 0;
}
if (ftplib_debug > 1)
fprintf(stderr,"%s",nControl->response);
if (nControl->response[3] == '-')
{
strncpy(match,nControl->response,3);
match[3] = ' ';
match[4] = '\0';
do
{
if (readline(nControl->response,256,nControl) == -1)
{
perror("Control socket read failed");
return 0;
}
if (ftplib_debug > 1)
fprintf(stderr,"%s",nControl->response);
}
while (strncmp(nControl->response,match,4));
}
if (nControl->response[0] == c)
return 1;
return 0;
}
/*
* FtpInit for stupid operating systems that require it (Windows NT)
*/
GLOBALDEF void FtpInit(void)
{
#if defined(_WIN32)
WORD wVersionRequested;
WSADATA wsadata;
int err;
wVersionRequested = MAKEWORD(1,1);
if ((err = WSAStartup(wVersionRequested,&wsadata)) != 0)
fprintf(stderr,"Network failed to start: %d\n",err);
#endif
}
/*
* FtpLastResponse - return a pointer to the last response received
*/
GLOBALDEF char *FtpLastResponse(netbuf *nControl)
{
if ((nControl) && (nControl->dir == FTPLIB_CONTROL))
return nControl->response;
return NULL;
}
/*
* FtpConnect - connect to remote server
*
* return 1 if connected, 0 if not
*/
GLOBALDEF int FtpConnect(const char *host, netbuf **nControl)
{
int sControl;
struct sockaddr_in sin;
struct hostent *phe;
struct servent *pse;
int on=1;
netbuf *ctrl;
char *lhost;
char *pnum;
memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
lhost = strdup(host);
pnum = strchr(lhost,':');
if (pnum == NULL)
{
#if defined(VMS)
sin.sin_port = htons(21);
#else
if ((pse = getservbyname("ftp","tcp")) == NULL)
{
perror("getservbyname");
return 0;
}
sin.sin_port = pse->s_port;
#endif
}
else
{
*pnum++ = '\0';
if (isdigit(*pnum))
sin.sin_port = htons(atoi(pnum));
else
{
pse = getservbyname(pnum,"tcp");
sin.sin_port = pse->s_port;
}
}
if ((sin.sin_addr.s_addr = inet_addr(lhost)) == -1)
{
if ((phe = gethostbyname(lhost)) == NULL)
{
perror("gethostbyname");
return 0;
}
memcpy((char *)&sin.sin_addr, phe->h_addr, phe->h_length);
}
free(lhost);
sControl = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sControl == -1)
{
perror("socket");
return 0;
}
if (setsockopt(sControl,SOL_SOCKET,SO_REUSEADDR,
SETSOCKOPT_OPTVAL_TYPE &on, sizeof(on)) == -1)
{
perror("setsockopt");
net_close(sControl);
return 0;
}
评论0