Skip to content

Commit

Permalink
Use forward declaration for ssl.
Browse files Browse the repository at this point in the history
  • Loading branch information
wadealer authored and mkulke committed Apr 28, 2019
1 parent 848150e commit 6be689f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 31 deletions.
27 changes: 23 additions & 4 deletions ftplib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
#define _LARGEFILE64_SOURCE
#endif

#ifndef NOSSL
#include <openssl/ssl.h>
#endif

#include "ftplib.h"

#ifndef NOSSL
#include <openssl/ssl.h>
#endif

#if defined(_WIN32)
#include <windows.h>
#include <winsock.h>
Expand Down Expand Up @@ -1527,9 +1535,13 @@ int ftplib::Fxp(ftplib* src, ftplib* dst, const char *pathSrc, const char *pathD
return retval;
}

#ifndef NOSSL

int ftplib::SetDataEncryption(dataencryption enc)
{
#ifdef NOSSL
(void)enc;
return 0;
#else
if (!mp_ftphandle->tlsctrl) return 0;
if (!FtpSendCmd("PBSZ 0",'2',mp_ftphandle)) return 0;
switch(enc)
Expand All @@ -1546,10 +1558,14 @@ int ftplib::SetDataEncryption(dataencryption enc)
return 0;
}
return 1;
#endif
}

int ftplib::NegotiateEncryption()
{
#ifdef NOSSL
return 0;
#else
int ret;

if (!FtpSendCmd("AUTH TLS",'2',mp_ftphandle)) return 0;
Expand All @@ -1569,14 +1585,17 @@ int ftplib::NegotiateEncryption()
if (ret < 1) return 0;

return 1;
#endif
}

void ftplib::SetCallbackCertFunction(FtpCallbackCert pointer)
{
mp_ftphandle->certcb = pointer;
}

#ifdef NOSSL
(void)pointer;
#else
mp_ftphandle->certcb = pointer;
#endif
}

void ftplib::SetCallbackIdleFunction(FtpCallbackIdle pointer)
{
Expand Down
40 changes: 24 additions & 16 deletions ftplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,36 @@
#define fopen64 fopen
#endif

#ifndef NOSSL
#include <openssl/ssl.h>
#endif
//SSL
typedef struct ssl_st SSL;
typedef struct ssl_ctx_st SSL_CTX;
typedef struct bio_st BIO;
typedef struct x509_st X509;

#include <sys/types.h>

#ifndef _FTPLIB_SSL_CLIENT_METHOD_
#define _FTPLIB_SSL_CLIENT_METHOD_ TLSv1_2_client_method
#endif//_FTPLIB_SSL_CLIENT_METHOD_
#endif

using namespace std;

//SSL
typedef struct ssl_st SSL;
typedef struct ssl_ctx_st SSL_CTX;
typedef struct bio_st BIO;
typedef struct x509_st X509;

/**
*@author mkulke
*/

typedef int (*FtpCallbackXfer)(off64_t xfered, void *arg);
typedef int (*FtpCallbackIdle)(void *arg);
typedef void (*FtpCallbackLog)(char *str, void* arg, bool out);

#ifndef NOSSL
//SSL
typedef bool (*FtpCallbackCert)(void *arg, X509 *cert);
#endif


struct ftphandle {
char *cput,*cget;
Expand All @@ -92,14 +101,14 @@ struct ftphandle {
off64_t cbbytes;
off64_t xfered1;
char response[256];
#ifndef NOSSL
//SSL
SSL* ssl;
SSL_CTX* ctx;
BIO* sbio;
int tlsctrl;
int tlsdata;
FtpCallbackCert certcb;
#endif

off64_t offset;
bool correctpasv;
};
Expand Down Expand Up @@ -136,8 +145,8 @@ class ftplib {
enum fxpmethod
{
defaultfxp = 0,
alternativefxp
};
alternativefxp
};

enum dataencryption
{
Expand Down Expand Up @@ -166,11 +175,6 @@ class ftplib {
int Put(const char *inputfile, const char *path, transfermode mode, off64_t offset = 0);
int Rename(const char *src, const char *dst);
int Delete(const char *path);
#ifndef NOSSL
int SetDataEncryption(dataencryption enc);
int NegotiateEncryption();
void SetCallbackCertFunction(FtpCallbackCert pointer);
#endif
int Quit();
void SetCallbackIdleFunction(FtpCallbackIdle pointer);
void SetCallbackLogFunction(FtpCallbackLog pointer);
Expand All @@ -185,6 +189,10 @@ class ftplib {
int RawClose(ftphandle* handle);
int RawWrite(void* buf, int len, ftphandle* handle);
int RawRead(void* buf, int max, ftphandle* handle);
// SSL
int SetDataEncryption(dataencryption enc);
int NegotiateEncryption();
void SetCallbackCertFunction(FtpCallbackCert pointer);

private:
ftphandle* mp_ftphandle;
Expand Down
23 changes: 14 additions & 9 deletions sample/Readme.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
Sample Project
===
# Sample Project

g++ -I.. -c sample.cpp
g++ -L.. -o sample sample.o -lftp++
```
g++ -I.. -c sample.cpp
g++ -L.. -o sample sample.o -lftp++
```

OSX:

DYLD_LIBRARY_PATH=.. ./sample
## MacOs

LINUX:
```
DYLD_LIBRARY_PATH=.. ./sample
```

LD_LIBRARY_PATH=.. ./sample
## Linux

```
LD_LIBRARY_PATH=.. ./sample
```
4 changes: 2 additions & 2 deletions sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

int main(void)
{
ftplib *ftp = new ftplib::ftplib();
ftplib *ftp = new ftplib();
ftp->Connect("ftp.gwdg.de:21");
ftp->Login("anonymous", "");
ftp->Dir(NULL, "/ftp/pub");
ftp->Dir(NULL, "/pub/linux/apache");
ftp->Quit();
return 0;
}

0 comments on commit 6be689f

Please sign in to comment.