src/utilfuns/ftplib.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "ftplib.h"
Include dependency graph for ftplib.c:

Go to the source code of this file.

Classes

struct  NetBuf

Defines

#define ACCEPT_TIMEOUT   10
#define BUILDING_LIBRARY
#define FTPLIB_BUFSIZ   8192
#define FTPLIB_CONTROL   0
#define FTPLIB_DEFMODE   FTPLIB_PASSIVE
#define FTPLIB_READ   1
#define FTPLIB_WRITE   2
#define lllog(M)   fprintf(stderr, M);
#define SETSOCKOPT_OPTVAL_TYPE   (void *)

Functions

static int FtpAcceptConnection (netbuf *nData, netbuf *nControl)
GLOBALDEF int FtpAccess (const char *path, int typ, int mode, netbuf *nControl, netbuf **nData)
GLOBALDEF int FtpCDUp (netbuf *nControl)
GLOBALDEF int FtpChdir (const char *path, netbuf *nControl)
GLOBALDEF int FtpClose (netbuf *nData)
GLOBALDEF int FtpConnect (const char *host, netbuf **nControl)
GLOBALDEF int FtpDelete (const char *fnm, netbuf *nControl)
GLOBALDEF int FtpDir (const char *outputfile, const char *path, netbuf *nControl)
GLOBALDEF int FtpGet (const char *outputfile, const char *path, char mode, netbuf *nControl)
GLOBALDEF void FtpInit (void)
GLOBALDEF char * FtpLastResponse (netbuf *nControl)
GLOBALDEF int FtpLogin (const char *user, const char *pass, netbuf *nControl)
GLOBALDEF int FtpMkdir (const char *path, netbuf *nControl)
GLOBALDEF int FtpModDate (const char *path, char *dt, int max, netbuf *nControl)
GLOBALDEF int FtpNlst (const char *outputfile, const char *path, netbuf *nControl)
static int FtpOpenPort (netbuf *nControl, netbuf **nData, int mode, int dir)
GLOBALDEF int FtpOptions (int opt, long val, netbuf *nControl)
GLOBALDEF int FtpPut (const char *inputfile, const char *path, char mode, netbuf *nControl)
GLOBALDEF int FtpPwd (char *path, int max, netbuf *nControl)
GLOBALDEF void FtpQuit (netbuf *nControl)
GLOBALDEF int FtpRead (void *buf, int max, netbuf *nData)
GLOBALDEF int FtpRename (const char *src, const char *dst, netbuf *nControl)
GLOBALDEF int FtpRmdir (const char *path, netbuf *nControl)
static int FtpSendCmd (const char *cmd, char expresp, netbuf *nControl)
GLOBALDEF int FtpSite (const char *cmd, netbuf *nControl)
GLOBALDEF int FtpSize (const char *path, int *size, char mode, netbuf *nControl)
GLOBALDEF int FtpSysType (char *buf, int max, netbuf *nControl)
GLOBALDEF int FtpWrite (void *buf, int len, netbuf *nData)
static int FtpXfer (const char *localfile, const char *path, netbuf *nControl, int typ, int mode)
void * mymemccpy (void *dst, const void *src, int c, size_t n)
static int readline (char *buf, int max, netbuf *ctl)
static int readresp (char c, netbuf *nControl)
static int socket_wait (netbuf *ctl)
static int writeline (char *buf, int len, netbuf *nData)

Variables

GLOBALDEF int ftplib_debug = 0
static char * version

Define Documentation

#define ACCEPT_TIMEOUT   10

Definition at line 73 of file ftplib.c.

#define BUILDING_LIBRARY

Definition at line 63 of file ftplib.c.

#define FTPLIB_BUFSIZ   8192

Definition at line 72 of file ftplib.c.

#define FTPLIB_CONTROL   0

Definition at line 75 of file ftplib.c.

#define FTPLIB_DEFMODE   FTPLIB_PASSIVE

Definition at line 80 of file ftplib.c.

#define FTPLIB_READ   1

Definition at line 76 of file ftplib.c.

#define FTPLIB_WRITE   2

Definition at line 77 of file ftplib.c.

#define lllog (  )     fprintf(stderr, M);

Definition at line 88 of file ftplib.c.

#define SETSOCKOPT_OPTVAL_TYPE   (void *)

Definition at line 69 of file ftplib.c.


Function Documentation

static int FtpAcceptConnection ( netbuf nData,
netbuf nControl 
) [static]

Definition at line 840 of file ftplib.c.

00841 {
00842     int sData;
00843     struct sockaddr addr;
00844     unsigned int l;
00845     int i;
00846     struct timeval tv;
00847     fd_set mask;
00848     int rv;
00849 
00850     FD_ZERO(&mask);
00851     FD_SET(nControl->handle, &mask);
00852     FD_SET(nData->handle, &mask);
00853     tv.tv_usec = 0;
00854     tv.tv_sec = ACCEPT_TIMEOUT;
00855     if (ftplib_debug > 1)
00856         printf("<<<<<<<<<<<<<<<<%d\n",ACCEPT_TIMEOUT);
00857     i = nControl->handle;
00858     if (i < nData->handle)
00859         i = nData->handle;
00860     i = select(i+1, &mask, NULL, NULL, &tv);
00861     if (i == -1)
00862       {
00863           strncpy(nControl->response, strerror(errno),
00864                   sizeof(nControl->response));
00865           net_close(nData->handle);
00866           nData->handle = 0;
00867           rv = 0;
00868       }
00869     else if (i == 0)
00870       {
00871           strcpy(nControl->response, "timed out waiting for connection");
00872           net_close(nData->handle);
00873           nData->handle = 0;
00874           rv = 0;
00875       }
00876     else
00877       {
00878           if (FD_ISSET(nData->handle, &mask))
00879             {
00880                 l = sizeof(addr);
00881                 sData = accept(nData->handle, &addr, &l);
00882                 i = errno;
00883                 net_close(nData->handle);
00884                 if (sData > 0)
00885                   {
00886                       rv = 1;
00887                       nData->handle = sData;
00888                   }
00889                 else
00890                   {
00891                       strncpy(nControl->response, strerror(i),
00892                               sizeof(nControl->response));
00893                       nData->handle = 0;
00894                       rv = 0;
00895                   }
00896             }
00897           else if (FD_ISSET(nControl->handle, &mask))
00898             {
00899                 net_close(nData->handle);
00900                 nData->handle = 0;
00901                 readresp('2', nControl);
00902                 rv = 0;
00903             }
00904       }
00905     return rv;  
00906 }

GLOBALDEF int FtpAccess ( const char *  path,
int  typ,
int  mode,
netbuf nControl,
netbuf **  nData 
)

Definition at line 913 of file ftplib.c.

00915 {
00916     char buf[256];
00917     int dir;
00918     if ((path == NULL) &&
00919         ((typ == FTPLIB_FILE_WRITE) || (typ == FTPLIB_FILE_READ)))
00920       {
00921           sprintf(nControl->response,
00922                   "Missing path argument for file transfer\n");
00923           return 0;
00924       }
00925     sprintf(buf, "TYPE %c", mode);
00926     if (!FtpSendCmd(buf, '2', nControl))
00927         return 0;
00928     switch (typ)
00929       {
00930         case FTPLIB_DIR:
00931             strcpy(buf,"NLST");
00932             dir = FTPLIB_READ;
00933             break;
00934         case FTPLIB_DIR_VERBOSE:
00935             strcpy(buf,"LIST");
00936             dir = FTPLIB_READ;
00937             break;
00938         case FTPLIB_FILE_READ:
00939             strcpy(buf,"RETR");
00940             dir = FTPLIB_READ;
00941             break;
00942         case FTPLIB_FILE_WRITE:
00943             strcpy(buf,"STOR");
00944             dir = FTPLIB_WRITE;
00945             break;
00946         default:
00947             sprintf(nControl->response, "Invalid open type %d\n", typ);
00948             return 0;
00949       }
00950     if (path != NULL)
00951       {
00952           int i = strlen(buf);
00953           buf[i++] = ' ';
00954           if ((strlen(path) + i) >= sizeof(buf))
00955               return 0;
00956           strcpy(&buf[i],path);
00957       }
00958     if (FtpOpenPort(nControl, nData, mode, dir) == -1)
00959         return 0;
00960     if (!FtpSendCmd(buf, '1', nControl))
00961       {
00962           FtpClose(*nData);
00963           *nData = NULL;
00964           return 0;
00965       }
00966     (*nData)->ctrl = nControl;
00967     nControl->data = *nData;
00968     if (nControl->cmode == FTPLIB_PORT)
00969       {
00970           if (!FtpAcceptConnection(*nData,nControl))
00971             {
00972                 FtpClose(*nData);
00973                 *nData = NULL;
00974                 nControl->data = NULL;
00975                 return 0;
00976             }
00977       }
00978     return 1;
00979 }

GLOBALDEF int FtpCDUp ( netbuf nControl  ) 

Definition at line 1161 of file ftplib.c.

01162 {
01163     if (!FtpSendCmd("CDUP",'2',nControl))
01164         return 0;
01165     return 1;
01166 }

GLOBALDEF int FtpChdir ( const char *  path,
netbuf nControl 
)

Definition at line 1144 of file ftplib.c.

01145 {
01146     char buf[256];
01147 
01148     if ((strlen(path) + 6) > sizeof(buf))
01149         return 0;
01150     sprintf(buf,"CWD %s",path);
01151     if (!FtpSendCmd(buf,'2',nControl))
01152         return 0;
01153     return 1;
01154 }

GLOBALDEF int FtpClose ( netbuf nData  ) 

Definition at line 1047 of file ftplib.c.

01048 {
01049     netbuf *ctrl;
01050     switch (nData->dir)
01051       {
01052         case FTPLIB_WRITE:
01053               /* potential problem - if buffer flush fails, how to notify user? */
01054             if (nData->buf != NULL)
01055                 writeline(NULL, 0, nData);
01056         case FTPLIB_READ:
01057             if (nData->buf)
01058                 free(nData->buf);
01059             shutdown(nData->handle,2);
01060             net_close(nData->handle);
01061             ctrl = nData->ctrl;
01062             free(nData);
01063             if (ctrl)
01064               {
01065                   ctrl->data = NULL;
01066                   return(readresp('2', ctrl));
01067               }
01068             return 1;
01069         case FTPLIB_CONTROL:
01070             if (nData->data)
01071               {
01072                   nData->ctrl = NULL;
01073                   FtpClose(nData);
01074               }
01075             net_close(nData->handle);
01076             free(nData);
01077             return 0;
01078       }
01079     return 1;
01080 }

GLOBALDEF int FtpConnect ( const char *  host,
netbuf **  nControl 
)

Definition at line 437 of file ftplib.c.

00438 {
00439     int sControl, stat, flags, oldflags;
00440     struct sockaddr_in sin;
00441     struct hostent *phe;
00442     struct servent *pse;
00443     int on=1;
00444     netbuf *ctrl;
00445     char *lhost;
00446     char *pnum;
00447     struct timeval tv;
00448     fd_set wr;
00449 
00450     memset(&sin,0,sizeof(sin));
00451     sin.sin_family = AF_INET;
00452     lhost = strdup(host);
00453     pnum = strchr(lhost,':');
00454     if (pnum == NULL)
00455       {
00456 #if defined(VMS) || defined(ANDROID)
00457           sin.sin_port = htons(21);
00458 #else
00459           if ((pse = getservbyname("ftp","tcp")) == NULL)
00460             {
00461                 perror("getservbyname");
00462                 return 0;
00463             }
00464           sin.sin_port = pse->s_port;
00465 #endif
00466       }
00467     else
00468       {
00469           *pnum++ = '\0';
00470           if (isdigit(*pnum))
00471               sin.sin_port = htons(atoi(pnum));
00472           else
00473             {
00474                 pse = getservbyname(pnum,"tcp");
00475                 sin.sin_port = pse->s_port;
00476             }
00477       }
00478     if ((sin.sin_addr.s_addr = inet_addr(lhost)) == -1)
00479       {
00480           if ((phe = gethostbyname(lhost)) == NULL)
00481             {
00482                 perror("gethostbyname");
00483                 return 0;
00484             }
00485 
00486           memcpy((char *)&sin.sin_addr, phe->h_addr, phe->h_length);
00487 
00488       }
00489     free(lhost);
00490 
00491     sControl = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
00492     if (sControl == -1)
00493       {
00494           perror("socket");
00495           return 0;
00496       }
00497 
00498     if ( setsockopt(sControl,SOL_SOCKET,SO_REUSEADDR,
00499                    SETSOCKOPT_OPTVAL_TYPE &on, sizeof(on)) == -1)
00500       {
00501           perror("setsockopt");
00502           net_close(sControl);
00503           return 0;
00504       }
00505 
00506 #if defined(_WIN32)
00507     if (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1)
00508     {
00509         perror("connect");
00510         net_close(sControl);
00511         return 0;
00512     }
00513 #else
00514      //set nonblocking for connection timeout
00515     flags = fcntl( sControl, F_GETFL,0);
00516     oldflags=flags;
00517     fcntl( sControl, F_SETFL, O_NONBLOCK|flags);
00518 
00519     stat=connect( sControl, (struct sockaddr *)&sin, sizeof(sin));
00520     if (stat < 0) 
00521       { 
00522           if (errno != EWOULDBLOCK && errno != EINPROGRESS) 
00523             { 
00524                 perror("connect");
00525                 net_close(sControl);
00526                 return 0;
00527             } 
00528       } 
00529 
00530     FD_ZERO(&wr); 
00531     FD_SET( sControl, &wr); 
00532 
00533     tv.tv_sec = ACCEPT_TIMEOUT;
00534     tv.tv_usec = 0; 
00535 
00536     stat = select(sControl+1, 0, &wr, 0, &tv);
00537 
00538     if (stat < 1)
00539       { 
00540             // time out has expired, 
00541             // or an error has ocurred
00542           perror("timeout");
00543           net_close(sControl);
00544           return 0;
00545       } 
00546 
00547     if (ftplib_debug > 1)
00548         printf("connected\n");
00549 
00550       //set original flags
00551     fcntl( sControl, F_SETFL, oldflags);
00552 #endif
00553 
00554     ctrl = calloc(1,sizeof(netbuf));
00555     if (ctrl == NULL)
00556       {
00557           perror("calloc");
00558           net_close(sControl);
00559           return 0;
00560       }
00561     ctrl->buf = malloc(FTPLIB_BUFSIZ);
00562     if (ctrl->buf == NULL)
00563       {
00564           perror("calloc");
00565           net_close(sControl);
00566           free(ctrl);
00567           return 0;
00568       }
00569     ctrl->handle = sControl;
00570     ctrl->dir = FTPLIB_CONTROL;
00571     ctrl->ctrl = NULL;
00572     ctrl->cmode = FTPLIB_DEFMODE;
00573     ctrl->idlecb = NULL;
00574     ctrl->writercb = NULL;
00575     ctrl->idletime.tv_sec = ctrl->idletime.tv_usec = 0;
00576     ctrl->idlearg = NULL;
00577     ctrl->writerarg = NULL;
00578     ctrl->xfered = 0;
00579     ctrl->xfered1 = 0;
00580     ctrl->cbbytes = 0;
00581     if (readresp('2', ctrl) == 0)
00582       {
00583           net_close(sControl);
00584           free(ctrl->buf);
00585           free(ctrl);
00586           return 0;
00587       }
00588     *nControl = ctrl;
00589     return 1;
00590 }

GLOBALDEF int FtpDelete ( const char *  fnm,
netbuf nControl 
)

Definition at line 1390 of file ftplib.c.

01391 {
01392     char cmd[256];
01393 
01394     if ((strlen(fnm) + 7) > sizeof(cmd))
01395         return 0;
01396     sprintf(cmd,"DELE %s",fnm);
01397     if (!FtpSendCmd(cmd,'2', nControl))
01398         return 0;
01399     return 1;
01400 }

GLOBALDEF int FtpDir ( const char *  outputfile,
const char *  path,
netbuf nControl 
)

Definition at line 1289 of file ftplib.c.

01290 {
01291     return FtpXfer(outputfile, path, nControl, FTPLIB_DIR_VERBOSE, FTPLIB_ASCII);
01292 }

GLOBALDEF int FtpGet ( const char *  outputfile,
const char *  path,
char  mode,
netbuf nControl 
)

Definition at line 1347 of file ftplib.c.

01349 {
01350     return FtpXfer(outputfile, path, nControl, FTPLIB_FILE_READ, mode);
01351 }

GLOBALDEF void FtpInit ( void   ) 

Definition at line 409 of file ftplib.c.

00410 {
00411 #if defined(_WIN32)
00412     WORD wVersionRequested;
00413     WSADATA wsadata;
00414     int err;
00415     wVersionRequested = MAKEWORD(1,1);
00416     if ((err = WSAStartup(wVersionRequested,&wsadata)) != 0)
00417         if (ftplib_debug > 1)
00418             fprintf(stderr,"Network failed to start: %d\n",err);
00419 #endif
00420 }

GLOBALDEF char* FtpLastResponse ( netbuf nControl  ) 

Definition at line 425 of file ftplib.c.

00426 {
00427     if ((nControl) && (nControl->dir == FTPLIB_CONTROL))
00428         return nControl->response;
00429     return NULL;
00430 }

GLOBALDEF int FtpLogin ( const char *  user,
const char *  pass,
netbuf nControl 
)

Definition at line 668 of file ftplib.c.

00669 {
00670     char tempbuf[64];
00671 
00672     if (((strlen(user) + 7) > sizeof(tempbuf)) ||
00673         ((strlen(pass) + 7) > sizeof(tempbuf)))
00674         return 0;
00675     sprintf(tempbuf,"USER %s",user);
00676     if (!FtpSendCmd(tempbuf,'3',nControl))
00677       {
00678           if (nControl->response[0] == '2')
00679               return 1;
00680           return 0;
00681       }
00682     sprintf(tempbuf,"PASS %s",pass);
00683     return FtpSendCmd(tempbuf,'2',nControl);
00684 }

GLOBALDEF int FtpMkdir ( const char *  path,
netbuf nControl 
)

Definition at line 1127 of file ftplib.c.

01128 {
01129     char buf[256];
01130 
01131     if ((strlen(path) + 6) > sizeof(buf))
01132         return 0;
01133     sprintf(buf,"MKD %s",path);
01134     if (!FtpSendCmd(buf,'2', nControl))
01135         return 0;
01136     return 1;
01137 }

GLOBALDEF int FtpModDate ( const char *  path,
char *  dt,
int  max,
netbuf nControl 
)

Definition at line 1327 of file ftplib.c.

01328 {
01329     char buf[256];
01330     int rv = 1;
01331 
01332     if ((strlen(path) + 7) > sizeof(buf))
01333         return 0;
01334     sprintf(buf,"MDTM %s",path);
01335     if (!FtpSendCmd(buf,'2',nControl))
01336         rv = 0;
01337     else
01338         strncpy(dt, &nControl->response[4], max);
01339     return rv;
01340 }

GLOBALDEF int FtpNlst ( const char *  outputfile,
const char *  path,
netbuf nControl 
)

Definition at line 1278 of file ftplib.c.

01280 {
01281     return FtpXfer(outputfile, path, nControl, FTPLIB_DIR, FTPLIB_ASCII);
01282 }

static int FtpOpenPort ( netbuf nControl,
netbuf **  nData,
int  mode,
int  dir 
) [static]

Definition at line 691 of file ftplib.c.

00692 {
00693     int sData;
00694     union {
00695         struct sockaddr sa;
00696         struct sockaddr_in in;
00697     } sin;
00698     struct linger lng = { 0, 0 };
00699     unsigned int l;
00700     int on=1;
00701     netbuf *ctrl;
00702     char *cp;
00703     unsigned int v[6];
00704     char buf[256];
00705 
00706     if (nControl->dir != FTPLIB_CONTROL)
00707         return -1;
00708     if ((dir != FTPLIB_READ) && (dir != FTPLIB_WRITE))
00709       {
00710           sprintf(nControl->response, "Invalid direction %d\n", dir);
00711           return -1;
00712       }
00713     if ((mode != FTPLIB_ASCII) && (mode != FTPLIB_IMAGE))
00714       {
00715           sprintf(nControl->response, "Invalid mode %c\n", mode);
00716           return -1;
00717       }
00718     l = sizeof(sin);
00719     if (nControl->cmode == FTPLIB_PASSIVE)
00720       {
00721           memset(&sin, 0, l);
00722           sin.in.sin_family = AF_INET;
00723           if (!FtpSendCmd("PASV",'2',nControl))
00724               return -1;
00725           cp = strchr(nControl->response,'(');
00726           if (cp == NULL)
00727               return -1;
00728           cp++;
00729           sscanf(cp,"%u,%u,%u,%u,%u,%u",&v[2],&v[3],&v[4],&v[5],&v[0],&v[1]);
00730           sin.sa.sa_data[2] = v[2];
00731           sin.sa.sa_data[3] = v[3];
00732           sin.sa.sa_data[4] = v[4];
00733           sin.sa.sa_data[5] = v[5];
00734           sin.sa.sa_data[0] = v[0];
00735           sin.sa.sa_data[1] = v[1];
00736       }
00737     else
00738       {
00739           if (getsockname(nControl->handle, &sin.sa, &l) < 0)
00740             {
00741                 perror("getsockname");
00742                 return 0;
00743             }
00744       }
00745     sData = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
00746     if (sData == -1)
00747       {
00748           perror("socket");
00749           return -1;
00750       }
00751     if (setsockopt(sData,SOL_SOCKET,SO_REUSEADDR,
00752                    SETSOCKOPT_OPTVAL_TYPE &on,sizeof(on)) == -1)
00753       {
00754           perror("setsockopt");
00755           net_close(sData);
00756           return -1;
00757       }
00758     if (setsockopt(sData,SOL_SOCKET,SO_LINGER,
00759                    SETSOCKOPT_OPTVAL_TYPE &lng,sizeof(lng)) == -1)
00760       {
00761           perror("setsockopt");
00762           net_close(sData);
00763           return -1;
00764       }
00765     if (nControl->cmode == FTPLIB_PASSIVE)
00766       {
00767           if (connect(sData, &sin.sa, sizeof(sin.sa)) == -1)
00768             {
00769                 perror("connect");
00770                 net_close(sData);
00771                 return -1;
00772             }
00773       }
00774     else
00775       {
00776           sin.in.sin_port = 0;
00777           if (bind(sData, &sin.sa, sizeof(sin)) == -1)
00778             {
00779                 perror("bind");
00780                 net_close(sData);
00781                 return 0;
00782             }
00783           if (listen(sData, 1) < 0)
00784             {
00785                 perror("listen");
00786                 net_close(sData);
00787                 return 0;
00788             }
00789           if (getsockname(sData, &sin.sa, &l) < 0)
00790               return 0;
00791           sprintf(buf, "PORT %d,%d,%d,%d,%d,%d",
00792                   (unsigned char) sin.sa.sa_data[2],
00793                   (unsigned char) sin.sa.sa_data[3],
00794                   (unsigned char) sin.sa.sa_data[4],
00795                   (unsigned char) sin.sa.sa_data[5],
00796                   (unsigned char) sin.sa.sa_data[0],
00797                   (unsigned char) sin.sa.sa_data[1]);
00798           if (!FtpSendCmd(buf,'2',nControl))
00799             {
00800                 net_close(sData);
00801                 return 0;
00802             }
00803       }
00804     ctrl = calloc(1,sizeof(netbuf));
00805     if (ctrl == NULL)
00806       {
00807           perror("calloc");
00808           net_close(sData);
00809           return -1;
00810       }
00811     if ((mode == 'A') && ((ctrl->buf = malloc(FTPLIB_BUFSIZ)) == NULL))
00812       {
00813           perror("calloc");
00814           net_close(sData);
00815           free(ctrl);
00816           return -1;
00817       }
00818     ctrl->handle = sData;
00819     ctrl->dir = dir;
00820     ctrl->idletime = nControl->idletime;
00821     ctrl->idlearg = nControl->idlearg;
00822     ctrl->writerarg = nControl->writerarg;
00823     ctrl->xfered = 0;
00824     ctrl->xfered1 = 0;
00825     ctrl->cbbytes = nControl->cbbytes;
00826     if (ctrl->idletime.tv_sec || ctrl->idletime.tv_usec || ctrl->cbbytes)
00827         ctrl->idlecb = nControl->idlecb;
00828     else
00829         ctrl->idlecb = NULL;
00830     ctrl->writercb = nControl->writercb;
00831     *nData = ctrl;
00832     return 1;
00833 }

GLOBALDEF int FtpOptions ( int  opt,
long  val,
netbuf nControl 
)

Definition at line 597 of file ftplib.c.

00598 {
00599     int v,rv=0;
00600     switch (opt)
00601       {
00602         case FTPLIB_CONNMODE:
00603             v = (int) val;
00604             if ((v == FTPLIB_PASSIVE) || (v == FTPLIB_PORT))
00605               {
00606                   nControl->cmode = v;
00607                   rv = 1;
00608               }
00609             break;
00610         case FTPLIB_CALLBACK:
00611             nControl->idlecb = (FtpCallback) val;
00612             rv = 1;
00613             break;
00614         case FTPLIB_CALLBACK_WRITER:
00615             nControl->writercb = (FtpCallbackWriter) val;
00616             rv = 1;
00617             break;
00618         case FTPLIB_IDLETIME:
00619             v = (int) val;
00620             rv = 1;
00621             nControl->idletime.tv_sec = v / 1000;
00622             nControl->idletime.tv_usec = (v % 1000) * 1000;
00623             break;
00624         case FTPLIB_CALLBACKARG:
00625             rv = 1;
00626             nControl->idlearg = (void *) val;
00627             break;
00628         case FTPLIB_CALLBACK_WRITERARG:
00629             rv = 1;
00630             nControl->writerarg = (void *) val;
00631             break;
00632         case FTPLIB_CALLBACKBYTES:
00633             rv = 1;
00634             nControl->cbbytes = (int) val;
00635             break;
00636       }
00637     return rv;
00638 }

GLOBALDEF int FtpPut ( const char *  inputfile,
const char *  path,
char  mode,
netbuf nControl 
)

Definition at line 1358 of file ftplib.c.

01360 {
01361     return FtpXfer(inputfile, path, nControl, FTPLIB_FILE_WRITE, mode);
01362 }

GLOBALDEF int FtpPwd ( char *  path,
int  max,
netbuf nControl 
)

Definition at line 1190 of file ftplib.c.

01191 {
01192     int l = max;
01193     char *b = path;
01194     char *s;
01195     if (!FtpSendCmd("PWD",'2',nControl))
01196         return 0;
01197     s = strchr(nControl->response, '"');
01198     if (s == NULL)
01199         return 0;
01200     s++;
01201     while ((--l) && (*s) && (*s != '"'))
01202         *b++ = *s++;
01203     *b++ = '\0';
01204     return 1;
01205 }

GLOBALDEF void FtpQuit ( netbuf nControl  ) 

Definition at line 1407 of file ftplib.c.

01408 {
01409     if (nControl->dir != FTPLIB_CONTROL)
01410         return;
01411     FtpSendCmd("QUIT",'2',nControl);
01412     net_close(nControl->handle);
01413     free(nControl->buf);
01414     free(nControl);
01415 }

GLOBALDEF int FtpRead ( void *  buf,
int  max,
netbuf nData 
)

Definition at line 984 of file ftplib.c.

00985 {
00986     int i;
00987     if (nData->dir != FTPLIB_READ)
00988         return 0;
00989     if (nData->buf)
00990         i = readline(buf, max, nData);
00991     else
00992       {
00993           i = socket_wait(nData);
00994           if (i != 1)
00995               return 0;
00996           i = net_read(nData->handle, buf, max);
00997       }
00998     if (i == -1)
00999         return 0;
01000     nData->xfered += i;
01001     if (nData->idlecb && nData->cbbytes)
01002       {
01003           nData->xfered1 += i;
01004           if (nData->xfered1 > nData->cbbytes)
01005             {
01006                 if (nData->idlecb(nData, nData->xfered, nData->idlearg) == 0)
01007                     return 0;
01008                 nData->xfered1 = 0;
01009             }
01010       }
01011     return i;
01012 }

GLOBALDEF int FtpRename ( const char *  src,
const char *  dst,
netbuf nControl 
)

Definition at line 1369 of file ftplib.c.

01370 {
01371     char cmd[256];
01372 
01373     if (((strlen(src) + 7) > sizeof(cmd)) ||
01374         ((strlen(dst) + 7) > sizeof(cmd)))
01375         return 0;
01376     sprintf(cmd,"RNFR %s",src);
01377     if (!FtpSendCmd(cmd,'3',nControl))
01378         return 0;
01379     sprintf(cmd,"RNTO %s",dst);
01380     if (!FtpSendCmd(cmd,'2',nControl))
01381         return 0;
01382     return 1;
01383 }

GLOBALDEF int FtpRmdir ( const char *  path,
netbuf nControl 
)

Definition at line 1173 of file ftplib.c.

01174 {
01175     char buf[256];
01176 
01177     if ((strlen(path) + 6) > sizeof(buf))
01178         return 0;
01179     sprintf(buf,"RMD %s",path);
01180     if (!FtpSendCmd(buf,'2',nControl))
01181         return 0;
01182     return 1;
01183 }

static int FtpSendCmd ( const char *  cmd,
char  expresp,
netbuf nControl 
) [static]

Definition at line 645 of file ftplib.c.

00646 {
00647     char buf[256];
00648     if (nControl->dir != FTPLIB_CONTROL)
00649         return 0;
00650     if (ftplib_debug > 2)
00651         fprintf(stderr,"%s\n",cmd);
00652     if ((strlen(cmd) + 3) > sizeof(buf))
00653         return 0;
00654     sprintf(buf,"%s\r\n",cmd);
00655     if (net_write(nControl->handle,buf,strlen(buf)) <= 0)
00656       {
00657           perror("write");
00658           return 0;
00659       }
00660     return readresp(expresp, nControl);
00661 }

GLOBALDEF int FtpSite ( const char *  cmd,
netbuf nControl 
)

Definition at line 1087 of file ftplib.c.

01088 {
01089     char buf[256];
01090     
01091     if ((strlen(cmd) + 7) > sizeof(buf))
01092         return 0;
01093     sprintf(buf,"SITE %s",cmd);
01094     if (!FtpSendCmd(buf,'2',nControl))
01095         return 0;
01096     return 1;
01097 }

GLOBALDEF int FtpSize ( const char *  path,
int *  size,
char  mode,
netbuf nControl 
)

Definition at line 1299 of file ftplib.c.

01300 {
01301     char cmd[256];
01302     int resp,sz,rv=1;
01303 
01304     if ((strlen(path) + 7) > sizeof(cmd))
01305         return 0;
01306     sprintf(cmd, "TYPE %c", mode);
01307     if (!FtpSendCmd(cmd, '2', nControl))
01308         return 0;
01309     sprintf(cmd,"SIZE %s",path);
01310     if (!FtpSendCmd(cmd,'2',nControl))
01311         rv = 0;
01312     else
01313       {
01314           if (sscanf(nControl->response, "%d %d", &resp, &sz) == 2)
01315               *size = sz;
01316           else
01317               rv = 0;
01318       }   
01319     return rv;
01320 }

GLOBALDEF int FtpSysType ( char *  buf,
int  max,
netbuf nControl 
)

Definition at line 1108 of file ftplib.c.

01109 {
01110     int l = max;
01111     char *b = buf;
01112     char *s;
01113     if (!FtpSendCmd("SYST",'2',nControl))
01114         return 0;
01115     s = &nControl->response[4];
01116     while ((--l) && (*s != ' '))
01117         *b++ = *s++;
01118     *b++ = '\0';
01119     return 1;
01120 }

GLOBALDEF int FtpWrite ( void *  buf,
int  len,
netbuf nData 
)

Definition at line 1017 of file ftplib.c.

01018 {
01019     int i;
01020     if (nData->dir != FTPLIB_WRITE)
01021         return 0;
01022     if (nData->buf)
01023         i = writeline(buf, len, nData);
01024     else
01025       {
01026           socket_wait(nData);
01027           i = net_write(nData->handle, buf, len);
01028       }
01029     if (i == -1)
01030         return 0;
01031     nData->xfered += i;
01032     if (nData->idlecb && nData->cbbytes)
01033       {
01034           nData->xfered1 += i;
01035           if (nData->xfered1 > nData->cbbytes)
01036             {
01037                 nData->idlecb(nData, nData->xfered, nData->idlearg);
01038                 nData->xfered1 = 0;
01039             }
01040       }
01041     return i;
01042 }

static int FtpXfer ( const char *  localfile,
const char *  path,
netbuf nControl,
int  typ,
int  mode 
) [static]

Definition at line 1212 of file ftplib.c.

01214 {
01215     int l,c;
01216     char *dbuf;
01217     FILE *local = NULL;
01218     netbuf *nData;
01219     int rv=1;
01220     int writeResult = 0;
01221 
01222     if (localfile != NULL)
01223       {
01224           char ac[4] = "w";
01225           if (typ == FTPLIB_FILE_WRITE)
01226               ac[0] = 'r';
01227           if (mode == FTPLIB_IMAGE)
01228               ac[1] = 'b';
01229           local = fopen(localfile, ac);
01230           if (local == NULL)
01231             {
01232                 strncpy(nControl->response, strerror(errno),
01233                         sizeof(nControl->response));
01234                 return 0;
01235             }
01236       }
01237     if (local == NULL)
01238         local = (typ == FTPLIB_FILE_WRITE) ? stdin : stdout;
01239     if (!FtpAccess(path, typ, mode, nControl, &nData))
01240         return 0;
01241     dbuf = malloc(FTPLIB_BUFSIZ);
01242     if (typ == FTPLIB_FILE_WRITE)
01243       {
01244           while ((l = fread(dbuf, 1, FTPLIB_BUFSIZ, local)) > 0)
01245               if ((c = FtpWrite(dbuf, l, nData)) < l)
01246                 {
01247                     if (ftplib_debug > 1)
01248                         printf("short write: passed %d, wrote %d\n", l, c);
01249                     rv = 0;
01250                     break;
01251                 }
01252       }
01253     else
01254       {
01255           while ((l = FtpRead(dbuf, FTPLIB_BUFSIZ, nData)) > 0) {
01256               writeResult = (nData->writercb) ? nData->writercb(nData, dbuf, l, nData->writerarg) : fwrite(dbuf, 1, l, local);
01257               if (writeResult <= 0)
01258                 {
01259                     perror("localstore write");
01260                     rv = 0;
01261                     break;
01262                 }
01263           }
01264       }
01265     free(dbuf);
01266     fflush(local);
01267     if (localfile != NULL)
01268         fclose(local);
01269     FtpClose(nData);
01270     return rv;
01271 }

void* mymemccpy ( void *  dst,
const void *  src,
int  c,
size_t  n 
)

Definition at line 90 of file ftplib.c.

00091 {
00092     char*        q     = dst;
00093     const char*  p     = src;
00094     const char*  p_end = p + n;
00095     char         ch    = ~(char)c;  /* ensure ch != c */
00096 
00097     for (;;) {
00098         if (ch == c || p >= p_end) break;
00099         *q++ = ch = *p++;
00100 
00101         if (ch == c || p >= p_end) break;
00102         *q++ = ch = *p++;
00103 
00104         if (ch == c || p >= p_end) break;
00105         *q++ = ch = *p++;
00106 
00107         if (ch == c || p >= p_end) break;
00108         *q++ = ch = *p++;
00109     }
00110 
00111     if (p >= p_end && ch != c)
00112         return NULL;
00113 
00114     return q;
00115 }

static int readline ( char *  buf,
int  max,
netbuf ctl 
) [static]

Definition at line 233 of file ftplib.c.

00234 {
00235     int x,retval = 0;
00236     char *end,*bp=buf;
00237     int eof = 0;
00238 
00239     if ((ctl->dir != FTPLIB_CONTROL) && (ctl->dir != FTPLIB_READ))
00240         return -1;
00241     if (max == 0)
00242         return 0;
00243     do
00244       {
00245           if (ctl->cavail > 0)
00246             {
00247                 x = (max >= ctl->cavail) ? ctl->cavail : max-1;
00248                 end = mymemccpy(bp,ctl->cget,'\n',x);
00249                 if (end != NULL) {
00250                     x = end - bp;
00251              }
00252                 retval += x;
00253                 bp += x;
00254                 *bp = '\0';
00255                 max -= x;
00256                 ctl->cget += x;
00257                 ctl->cavail -= x;
00258                 if (end != NULL)
00259                   {
00260                       bp -= 2;
00261                       if (strcmp(bp,"\r\n") == 0)
00262                         {
00263                             *bp++ = '\n';
00264                             *bp++ = '\0';
00265                             --retval;
00266                         }
00267                       break;
00268                   }
00269             }
00270           if (max == 1)
00271             {
00272                 *buf = '\0';
00273                 break;
00274             }
00275           if (ctl->cput == ctl->cget)
00276             {
00277                 ctl->cput = ctl->cget = ctl->buf;
00278                 ctl->cavail = 0;
00279                 ctl->cleft = FTPLIB_BUFSIZ;
00280             }
00281           if (eof)
00282             {
00283                 if (retval == 0)
00284                     retval = -1;
00285                 break;
00286             }
00287           if (!socket_wait(ctl))
00288               return retval;
00289           if ((x = net_read(ctl->handle,ctl->cput,ctl->cleft)) == -1)
00290             {
00291                 perror("read");
00292                 retval = -1;
00293                 break;
00294             }
00295           if (x == 0) //< ctl->cleft)
00296               eof = 1;
00297           ctl->cleft -= x;
00298           ctl->cavail += x;
00299           ctl->cput += x;
00300       }
00301     while (1);
00302     return retval;
00303 }

static int readresp ( char  c,
netbuf nControl 
) [static]

Definition at line 374 of file ftplib.c.

00375 {
00376     char match[5];
00377     if (readline(nControl->response,256,nControl) == -1)
00378       {
00379           perror("Control socket read failed");
00380           return 0;
00381       }
00382     if (ftplib_debug > 1)
00383         fprintf(stderr,"%s",nControl->response);
00384     if (nControl->response[3] == '-')
00385       {
00386           strncpy(match,nControl->response,3);
00387           match[3] = ' ';
00388           match[4] = '\0';
00389           do
00390             {
00391                 if (readline(nControl->response,256,nControl) == -1)
00392                   {
00393                       perror("Control socket read failed");
00394                       return 0;
00395                   }
00396                 if (ftplib_debug > 1)
00397                     fprintf(stderr,"%s",nControl->response);
00398             }
00399           while (strncmp(nControl->response,match,4));
00400       }
00401     if (nControl->response[0] == c)
00402         return 1;
00403     return 0;
00404 }

static int socket_wait ( netbuf ctl  )  [static]

Definition at line 194 of file ftplib.c.

00195 {
00196     fd_set fd,*rfd = NULL,*wfd = NULL;
00197     struct timeval tv;
00198     int rv = 0;
00199     if ((ctl->dir == FTPLIB_CONTROL) || (ctl->idlecb == NULL))
00200         return 1;
00201     if (ctl->dir == FTPLIB_WRITE)
00202         wfd = &fd;
00203     else
00204         rfd = &fd;
00205     FD_ZERO(&fd);
00206     do
00207       {
00208           FD_SET(ctl->handle,&fd);
00209           tv = ctl->idletime;
00210           rv = select(ctl->handle+1, rfd, wfd, NULL, &tv);
00211           if (rv == -1)
00212             {
00213                 rv = 0;
00214                 strncpy(ctl->ctrl->response, strerror(errno),
00215                         sizeof(ctl->ctrl->response));
00216                 break;
00217             }
00218           else if (rv > 0)
00219             {
00220                 rv = 1;
00221                 break;
00222             }
00223       }
00224     while ((rv = ctl->idlecb(ctl, ctl->xfered, ctl->idlearg)));
00225     return rv;
00226 }

static int writeline ( char *  buf,
int  len,
netbuf nData 
) [static]

Definition at line 310 of file ftplib.c.

00311 {
00312     int x, nb=0, w;
00313     char *ubp = buf, *nbp;
00314     char lc=0;
00315 
00316     if (nData->dir != FTPLIB_WRITE)
00317         return -1;
00318     nbp = nData->buf;
00319     for (x=0; x < len; x++)
00320       {
00321           if ((*ubp == '\n') && (lc != '\r'))
00322             {
00323                 if (nb == FTPLIB_BUFSIZ)
00324                   {
00325                       if (!socket_wait(nData))
00326                           return x;
00327                       w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
00328                       if (w != FTPLIB_BUFSIZ)
00329                         {
00330                             if (ftplib_debug > 1)
00331                                 printf("net_write(1) returned %d, errno = %d\n", w, errno);
00332                             return(-1);
00333                         }
00334                       nb = 0;
00335                   }
00336                 nbp[nb++] = '\r';
00337             }
00338           if (nb == FTPLIB_BUFSIZ)
00339             {
00340                 if (!socket_wait(nData))
00341                     return x;
00342                 w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ);
00343                 if (w != FTPLIB_BUFSIZ)
00344                   {
00345                         if (ftplib_debug > 1)
00346                             printf("net_write(2) returned %d, errno = %d\n", w, errno);
00347                       return(-1);
00348                   }
00349                 nb = 0;
00350             }
00351           nbp[nb++] = lc = *ubp++;
00352       }
00353     if (nb)
00354       {
00355           if (!socket_wait(nData))
00356               return x;
00357           w = net_write(nData->handle, nbp, nb);
00358           if (w != nb)
00359             {
00360                 if (ftplib_debug > 1)
00361                     printf("net_write(3) returned %d, errno = %d\n", w, errno);
00362                 return(-1);
00363             }
00364       }
00365     return len;
00366 }


Variable Documentation

GLOBALDEF int ftplib_debug = 0

Definition at line 141 of file ftplib.c.

char* version [static]
Initial value:
"ftplib Release 3.1-1 9/16/00, copyright 1996-2000 Thomas Pfau"

Definition at line 138 of file ftplib.c.


Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1