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