nx_storage_sdk  1.0
Storage SDK
ftplib.h
1 // Copyright 2003-present (see below). Licensed under LGPL 2.1: www.gnu.org/licenses/lgpl-3.0.html
2 
3 /***************************************************************************
4  ftplib.h - description
5  -------------------
6  begin : Son Jul 27 2003
7  copyright : (C) 2013 by magnus kulke
8  email : mkulke@gmail.com
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU Lesser General Public License as *
15  * published by the Free Software Foundation; either version 2.1 of the *
16  * License, or (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 /***************************************************************************
21  * Note: ftplib, on which ftplibpp was originally based upon used to be *
22  * licensed as GPL 2.0 software, as of Jan. 26th 2013 its author Thomas *
23  * Pfau allowed the distribution of ftplib via LGPL. Thus the license of *
24  * ftplibpp changed aswell. *
25  ***************************************************************************/
26 
27 #ifndef FTPLIB_H
28 #define FTPLIB_H
29 
30 #if defined(_WIN32)
31 #include <winsock2.h>
32 #include <windows.h>
33 #pragma warning(disable: 4996) //< Disable warnings "... deprecated API...".
34 #else
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netdb.h>
38 #include <arpa/inet.h>
39 #include <sys/ioctl.h>
40 #endif
41 
42 #ifndef _WIN32
43 #include <unistd.h>
44 #include <sys/time.h>
45 #endif
46 
47 #ifdef NOLFS
48 #define off64_t long
49 #endif
50 
51 #if defined(__APPLE__)
52 #define off64_t __darwin_off_t
53 #define fseeko64 fseeko
54 #define fopen64 fopen
55 #endif
56 
57 #ifndef NOSSL
58 #include <openssl/ssl.h>
59 #endif
60 
61 using namespace std;
62 
67 typedef int (*FtpCallbackXfer)(off64_t xfered, void *arg);
68 typedef int (*FtpCallbackIdle)(void *arg);
69 typedef void (*FtpCallbackLog)(char *str, void* arg, bool out);
70 
71 #ifndef NOSSL
72 typedef bool (*FtpCallbackCert)(void *arg, X509 *cert);
73 #endif
74 
75 struct ftphandle {
76  char *cput,*cget;
77  int handle;
78  int cavail,cleft;
79  char *buf;
80  int dir;
81  ftphandle *ctrl;
82  int cmode;
83  struct timeval idletime;
84  FtpCallbackXfer xfercb;
85  FtpCallbackIdle idlecb;
86  FtpCallbackLog logcb;
87  void *cbarg;
88  off64_t xfered;
89  off64_t cbbytes;
90  off64_t xfered1;
91  char response[256];
92 #ifndef NOSSL
93  SSL* ssl;
94  SSL_CTX* ctx;
95  BIO* sbio;
96  int tlsctrl;
97  int tlsdata;
98  FtpCallbackCert certcb;
99 #endif
100  off64_t offset;
101  bool correctpasv;
102 };
103 
104 
105 class ftplib {
106 public:
107 
108  enum accesstype
109  {
110  dir = 1,
111  dirverbose,
112  fileread,
113  filewrite,
114  filereadappend,
115  filewriteappend
116  };
117 
118  enum transfermode
119  {
120  ascii = 'A',
121  image = 'I'
122  };
123 
124  enum connmode
125  {
126  pasv = 1,
127  port
128  };
129 
130  enum fxpmethod
131  {
132  defaultfxp = 0,
133  alternativefxp
134  };
135 
136  enum dataencryption
137  {
138  unencrypted = 0,
139  secure
140  };
141 
142  ftplib();
143  ~ftplib();
144  char* LastResponse();
145  int Connect(const char *host);
146  int Login(const char *user, const char *pass);
147  int Site(const char *cmd);
148  int Raw(const char *cmd);
149  int SysType(char *buf, int max);
150  int Mkdir(const char *path);
151  int Chdir(const char *path);
152  int Cdup();
153  int Rmdir(const char *path);
154  int Pwd(char *path, int max);
155  int Nlst(const char *outputfile, const char *path);
156  int Dir(const char *outputfile, const char *path);
157  int Size(const char *path, int *size, transfermode mode);
158  int ModDate(const char *path, char *dt, int max);
159  int Get(const char *outputfile, const char *path, transfermode mode, off64_t offset = 0);
160  int Put(const char *inputfile, const char *path, transfermode mode, off64_t offset = 0);
161  int Rename(const char *src, const char *dst);
162  int Delete(const char *path);
163  int TestControlConnection();
164 #ifndef NOSSL
165  int SetDataEncryption(dataencryption enc);
166  int NegotiateEncryption();
167  void SetCallbackCertFunction(FtpCallbackCert pointer);
168 #endif
169  int Quit();
170  void SetCallbackIdleFunction(FtpCallbackIdle pointer);
171  void SetCallbackLogFunction(FtpCallbackLog pointer);
172  void SetCallbackXferFunction(FtpCallbackXfer pointer);
173  void SetCallbackArg(void *arg);
174  void SetCallbackBytes(off64_t bytes);
175  void SetCorrectPasv(bool b) { mp_ftphandle->correctpasv = b; };
176  void SetCallbackIdletime(int time);
177  void SetConnmode(connmode mode);
178  static int Fxp(ftplib* src, ftplib* dst, const char *pathSrc, const char *pathDst, transfermode mode, fxpmethod method);
179 
180  ftphandle* RawOpen(const char *path, accesstype type, transfermode mode);
181  int RawClose(ftphandle* handle);
182  int RawWrite(void* buf, int len, ftphandle* handle);
183  int RawRead(void* buf, int max, ftphandle* handle);
184 
185 private:
186  ftphandle* mp_ftphandle;
187 
188  int FtpXfer(const char *localfile, const char *path, ftphandle *nControl, accesstype type, transfermode mode);
189  int FtpOpenPasv(ftphandle *nControl, ftphandle **nData, transfermode mode, int dir, char *cmd);
190  int FtpSendCmd(const char *cmd, char expresp, ftphandle *nControl);
191  int FtpAcceptConnection(ftphandle *nData, ftphandle *nControl);
192  int FtpOpenPort(ftphandle *nControl, ftphandle **nData, transfermode mode, int dir, char *cmd);
193  int FtpRead(void *buf, int max, ftphandle *nData);
194  int FtpWrite(void *buf, int len, ftphandle *nData);
195  int FtpAccess(const char *path, accesstype type, transfermode mode, ftphandle *nControl, ftphandle **nData);
196  int FtpClose(ftphandle *nData);
197 
198  int socket_wait(ftphandle *ctl);
199  int readline(char *buf,int max,ftphandle *ctl);
200  int writeline(char *buf, int len, ftphandle *nData);
201  int readresp(char c, ftphandle *nControl);
202 
203  void ClearHandle();
204  int CorrectPasvResponse(unsigned char *v);
205 };
206 
207 #endif
Definition: ftplib.h:75
Definition: ftplib.h:105