Skip to content

Commit ed4b8c3

Browse files
committed
- add first version of timer based NoOp support (timing seems to be not accurate yet) and debug traces from branch https://github.com/ashkulz/NppFTP/tree/mkerost
1 parent 2dc4ac8 commit ed4b8c3

23 files changed

+519
-170
lines changed

UTCP/src/ftp_c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// =================================================================
2020
// Ultimate TCP/IP v4.2
2121
// This software along with its related components, documentation and files ("The Libraries")
22-
// is © 1994-2007 The Code Project (1612916 Ontario Limited) and use of The Libraries is
22+
// is © 1994-2007 The Code Project (1612916 Ontario Limited) and use of The Libraries is
2323
// governed by a software license agreement ("Agreement"). Copies of the Agreement are
2424
// available at The Code Project (www.codeproject.com), as part of the package you downloaded
2525
// to obtain this file, or directly from our office. For a copy of the license governing
@@ -2978,7 +2978,7 @@ void CUT_FTPClient::GetInfoInUNIXFormat( CUT_DIRINFOA * di){
29782978
strncpy(di->mod, &m_szBuf[0], sizeof(di->mod)-1);
29792979
di->mod[sizeof(di->mod)-1] = '\0';
29802980

2981-
//directory attrib
2981+
//directory attribute
29822982
if(m_szBuf[0]=='d' || m_szBuf[0] =='D')
29832983
di->isDir = TRUE;
29842984
else if (m_szBuf[0]=='l' || m_szBuf[0] =='L')

src/FTPCache.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@ int FTPCache::Clear() {
123123
return 0;
124124
}
125125

126+
// should return 0 on success
126127
int FTPCache::GetExternalPathFromLocal(const TCHAR * localpath, char * extbuf, int extsize) const {
127128
TCHAR expanded[MAX_PATH];
128129
BOOL res = PathSearchAndQualify(localpath, expanded, MAX_PATH);
129130
if (res == FALSE) {
131+
OutErr("[GetExternalPathFromLocal] PathSearchAndQualify res is false");
130132
return -1;
131133
}
132134

@@ -138,8 +140,10 @@ int FTPCache::GetExternalPathFromLocal(const TCHAR * localpath, char * extbuf, i
138140
}
139141
}
140142

141-
if (!m_cacheParent)
143+
if (!m_cacheParent) {
144+
OutErr("[GetExternalPathFromLocal] At root folder. End of search. No match was found.");
142145
return 1;
146+
}
143147

144148
return m_cacheParent->GetExternalPathFromLocal(localpath, extbuf, extsize);
145149
}
@@ -177,7 +181,7 @@ int FTPCache::ClearCurrentCache(bool permanent) {
177181

178182

179183
for(size_t i = 0; i < m_vCachePaths.size(); i++) {
180-
OutMsg("[Cache] Clearing cache in '%T'", m_vCachePaths[i].localpathExpanded);
184+
OutDebug("[FTPCache] Clearing cache in '%T'", m_vCachePaths[i].localpathExpanded);
181185
lstrcpy(dirPath, m_vCachePaths[i].localpathExpanded);
182186
int len = lstrlen(dirPath);
183187
dirPath[len+1] = 0;

src/FTPClientWrapper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ FTPClientWrapper::FTPClientWrapper(Client_Type type, const char * host, int port
3535
}
3636

3737
FTPClientWrapper::~FTPClientWrapper() {
38-
if (m_connected) //cannot simply call Disconnect as it's virtual
38+
if (m_connected){ //cannot simply call Disconnect as it's virtual
3939
OutErr("[FTPClientWrapper] connection active in destructor");
40+
}
4041

4142
SU::free(m_hostname);
4243
SU::free(m_username);

src/FTPClientWrapper.h

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ enum Connection_Mode {Mode_Passive = 0, Mode_Active = 1, Mode_ConnectionMax = 2}
3333
enum Transfer_Mode {Mode_Binary = 0, Mode_ASCII = 1, Mode_TransferMax = 2};
3434
enum AuthenticationMethods {Method_Password=0x01, Method_Key=0x02, Method_Interactive=0x04, Method_All=0x07};
3535

36+
37+
// =================================================================================================
38+
// FtpSSLWrapper
39+
// =================================================================================================
40+
3641
class FtpSSLWrapper : public CUT_FTPClient {
3742
public:
3843
FtpSSLWrapper();
@@ -46,6 +51,8 @@ class FtpSSLWrapper : public CUT_FTPClient {
4651

4752
virtual int SetCertificates(vX509 * x509Vect);
4853

54+
virtual DWORD LastAction();
55+
4956
virtual BOOL IsConnected();
5057
protected:
5158
virtual int GetResponseCode(CUT_WSClient *ws,LPSTR string = NULL,int maxlen = 0);
@@ -64,8 +71,14 @@ class FtpSSLWrapper : public CUT_FTPClient {
6471
ProgressMonitor* m_progmon;
6572
long m_currentTotal; //kinda hacky
6673
vX509* m_certificates;
74+
75+
DWORD m_lastAction;
6776
};
6877

78+
// =================================================================================================
79+
// FTPClientWrapper
80+
// =================================================================================================
81+
6982
class FTPClientWrapper {
7083
public:
7184
FTPClientWrapper(Client_Type type, const char * host, int port, const char * user, const char * password);
@@ -82,6 +95,8 @@ class FTPClientWrapper {
8295
virtual int Connect() = 0;
8396
virtual int Disconnect() = 0;
8497

98+
virtual int NoOp() = 0;
99+
85100
//Don't forget to call releasedir
86101
virtual int GetDir(const char * path, FTPFile** files) = 0;
87102
static int ReleaseDir(FTPFile* files, int size);
@@ -103,6 +118,8 @@ class FTPClientWrapper {
103118
virtual int ReceiveFile(HANDLE hFile, const char * ftpfile) = 0;
104119
virtual int DeleteFile(const char * path) = 0;
105120

121+
virtual DWORD LastAction() = 0;
122+
106123
virtual bool IsConnected();
107124
virtual int Abort();
108125
protected:
@@ -125,6 +142,10 @@ class FTPClientWrapper {
125142
vX509* m_certificates;
126143
};
127144

145+
// =================================================================================================
146+
// FTPClientWrapperSSH
147+
// =================================================================================================
148+
128149
class FTPClientWrapperSSH : public FTPClientWrapper {
129150
public:
130151
FTPClientWrapperSSH(const char * host, int port, const char * user, const char * password);
@@ -140,6 +161,8 @@ class FTPClientWrapperSSH : public FTPClientWrapper {
140161
virtual int Cwd(const char * path);
141162
virtual int Pwd(char* buf, size_t size);
142163

164+
virtual int NoOp();
165+
143166
//Modifying operations
144167
virtual int Rename(const char * from, const char * to);
145168
virtual int ChmodFile(const char * path, const char * mode);
@@ -154,6 +177,8 @@ class FTPClientWrapperSSH : public FTPClientWrapper {
154177
virtual int ReceiveFile(HANDLE hFile, const char * ftpfile);
155178
virtual int DeleteFile(const char * path);
156179

180+
virtual DWORD LastAction();
181+
157182
virtual bool IsConnected();
158183

159184
//Class specific operations
@@ -182,6 +207,10 @@ class FTPClientWrapperSSH : public FTPClientWrapper {
182207
unsigned int m_acceptedMethods;
183208
};
184209

210+
// =================================================================================================
211+
// FTPClientWrapperSSL
212+
// =================================================================================================
213+
185214
class FTPClientWrapperSSL : public FTPClientWrapper {
186215
public:
187216
FTPClientWrapperSSL(const char * host, int port, const char * user, const char * password);
@@ -196,6 +225,8 @@ class FTPClientWrapperSSL : public FTPClientWrapper {
196225
virtual int Connect();
197226
virtual int Disconnect();
198227

228+
virtual int NoOp();
229+
199230
virtual int GetDir(const char * path, FTPFile** files);
200231

201232
virtual int Cwd(const char * path);
@@ -215,6 +246,8 @@ class FTPClientWrapperSSL : public FTPClientWrapper {
215246
virtual int ReceiveFile(HANDLE hFile, const char * ftpfile);
216247
virtual int DeleteFile(const char * path);
217248

249+
virtual DWORD LastAction();
250+
218251
virtual bool IsConnected();
219252
virtual int Abort();
220253
virtual int OnReturn(int ret);
@@ -235,9 +268,12 @@ class FTPClientWrapperSSL : public FTPClientWrapper {
235268
FILETIME ConvertFiletime(int day, int month, int year, int hour, int minute);
236269
};
237270

238-
/////////////////////////////////////////////////////////
239-
/////Class extending some classes from CUT
240-
/////////////////////////////////////////////////////////
271+
272+
// =================================================================================================
273+
// MemoryDataSource
274+
// =================================================================================================
275+
276+
// Class extending some classes from CUT
241277

242278
class MemoryDataSource : public CUT_DataSource {
243279
protected:

src/FTPClientWrapperSSH.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ FTPClientWrapper* FTPClientWrapperSSH::Clone() {
6262
return wrapper;
6363
}
6464

65+
DWORD FTPClientWrapperSSH::LastAction() {
66+
67+
// SSH last action not needed to be followed, since SSH doesn't need timer to send NOOP command.
68+
return 0;
69+
}
70+
6571
int FTPClientWrapperSSH::Connect() {
6672
if (m_connected)
6773
return 0;
@@ -262,6 +268,10 @@ int FTPClientWrapperSSH::ReceiveFile(const TCHAR * localfile, const char * ftpfi
262268
return ReceiveFile(hFile, ftpfile);
263269
}
264270

271+
int FTPClientWrapperSSH::NoOp() {
272+
return 0;
273+
}
274+
265275
int FTPClientWrapperSSH::ReceiveFile(HANDLE hFile, const char * ftpfile) {
266276
ssize_t retcode = 0;
267277
int res = TRUE;
@@ -513,13 +523,13 @@ int FTPClientWrapperSSH::authenticate(ssh_session session) {
513523
OutErr("[SFTP] Error during authentication: %s", ssh_get_error(session));
514524
return -1;
515525
} else if (authres == SSH_AUTH_SUCCESS) {
516-
OutMsg("[SFTP] Authenticated without credentials.");
526+
OutDebug("[SFTP] Authenticated without credentials.");
517527
return 0;
518528
}
519529

520530
char * banner = ssh_get_issue_banner(session);
521531
if (banner) {
522-
OutMsg("[SFTP] Banner: %s\n", banner);
532+
OutDebug("[SFTP] Banner: %s\n", banner);
523533
ssh_string_free_char(banner);
524534
}
525535

@@ -561,7 +571,7 @@ int FTPClientWrapperSSH::authenticate(ssh_session session) {
561571
}
562572

563573
if (authres == SSH_AUTH_SUCCESS) {
564-
OutMsg("[SFTP] Successfully authenticated");
574+
OutDebug("[SFTP] Successfully authenticated");
565575
return 0;
566576
}
567577
retries++;
@@ -594,7 +604,7 @@ int FTPClientWrapperSSH::authenticate_key(ssh_session session) {
594604
ssh_key_free(privkey);
595605

596606
if (rc == SSH_AUTH_DENIED) {
597-
OutErr("[SFTP] Key authentication denied.");
607+
OutDebug("[SFTP] Key authentication denied.");
598608
}
599609

600610
return rc;
@@ -605,7 +615,7 @@ int FTPClientWrapperSSH::authenticate_password(ssh_session session) {
605615

606616
rc = ssh_userauth_password(session, NULL, m_password);
607617
if (rc == SSH_AUTH_DENIED) {
608-
OutMsg("[SFTP] Password authentication denied.");
618+
OutDebug("[SFTP] Password authentication denied.");
609619
}
610620

611621
return rc;
@@ -624,15 +634,15 @@ int FTPClientWrapperSSH::authenticate_kbinteractive(ssh_session session) {
624634
OutErr("[SFTP] Error creating interactive dialog");
625635
return SSH_AUTH_ERROR;
626636
} else if (res != 1 && res != 0) { //1: Gave answer, 0: No input required
627-
OutMsg("[SFTP] Keyboard interactive authentication cancelled.");
637+
OutDebug("[SFTP] Keyboard interactive authentication cancelled.");
628638
return SSH_AUTH_ERROR;
629639
}
630640
rc = ssh_userauth_kbdint(session, NULL, NULL);
631641
i++;
632642
}
633643

634644
if (rc == SSH_AUTH_DENIED) {
635-
OutMsg("[SFTP] Keyboard interactive authentication denied.");
645+
OutDebug("[SFTP] Keyboard interactive authentication denied.");
636646
}
637647

638648
return rc;
@@ -666,11 +676,11 @@ int FTPClientWrapperSSH::verify_knownhost(ssh_session session) {
666676

667677
switch(state){
668678
case SSH_SERVER_KNOWN_OK:
669-
OutMsg("[SFTP] Host key accepted");
679+
OutDebug("[SFTP] Host key accepted");
670680
result = 0;
671681
break; /* ok */
672682
case SSH_SERVER_FILE_NOT_FOUND:
673-
OutMsg("[SFTP] Creating known hosts file.");
683+
OutDebug("[SFTP] Creating known hosts file.");
674684
/* fallback to SSH_SERVER_NOT_KNOWN behavior */
675685
case SSH_SERVER_NOT_KNOWN: {
676686
SU::TSprintf(errMessage, 512, TEXT("The server is unknown. Do you trust the host key\r\n%s %s ?"), keytype, hashHex);
@@ -699,7 +709,7 @@ int FTPClientWrapperSSH::verify_knownhost(ssh_session session) {
699709
OutErr("[SFTP] The session will continue but the key will not be stored");
700710
result = 0; //return 0 even if an error occured
701711
} else {
702-
OutMsg("[SFTP] Host key written to file");
712+
OutDebug("[SFTP] Host key written to file");
703713
result = 0;
704714
}
705715
} else {

src/FTPClientWrapperSSL.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ int FTPClientWrapperSSL::SetTimeout(int timeout) {
7575
return ret;
7676
}
7777

78+
DWORD FTPClientWrapperSSL::LastAction() {
79+
return m_client.LastAction();
80+
}
81+
7882
int FTPClientWrapperSSL::Connect() {
7983
if (m_connected)
8084
return OnReturn(0);
@@ -88,13 +92,23 @@ int FTPClientWrapperSSL::Connect() {
8892
}
8993

9094
int FTPClientWrapperSSL::Disconnect() {
91-
if (!m_connected)
95+
96+
OutDebug("[FTPS] now disconnecting.");
97+
98+
if (!m_connected) {
99+
OutDebug("[FTPS] not connected. nothing will be done.");
92100
return OnReturn(0);
101+
}
93102

94103
int retcode = m_client.Close();
95104

96105
m_connected = false; //just set to disconnected state, ignore errors
97-
106+
107+
if (retcode == UTE_SUCCESS) {
108+
OutDebug("[FTPS] successfully disconnected.");
109+
} else {
110+
OutDebug("[FTPS] failed to disconnect.");
111+
}
98112
return OnReturn((retcode == UTE_SUCCESS)?0:-1);
99113
}
100114

@@ -233,6 +247,10 @@ int FTPClientWrapperSSL::Cwd(const char * path) {
233247
return OnReturn((retcode == UTE_SUCCESS)?0:-1);
234248
}
235249

250+
int FTPClientWrapperSSL::NoOp() {
251+
return m_client.NoOp();
252+
}
253+
236254
int FTPClientWrapperSSL::Pwd(char* buf, size_t size) {
237255
int retcode = m_client.GetCurDir(buf, size);
238256

@@ -488,6 +506,8 @@ int FtpSSLWrapper::Send(LPCSTR data, int len) {
488506
}
489507
OutClnt("-> %T", SU::Utf8ToTChar(datacpy));
490508
delete [] datacpy;
509+
510+
m_lastAction = GetTickCount();
491511

492512
return CUT_WSClient::Send(data, len);
493513
}
@@ -532,6 +552,14 @@ int FtpSSLWrapper::GetResponseCode(CUT_WSClient *ws,LPSTR string,int maxlen) {
532552
return res;
533553
}
534554

555+
DWORD FtpSSLWrapper::LastAction() {
556+
if (m_lastAction == 0) {
557+
return 0;
558+
} else {
559+
return (DWORD) ((GetTickCount() - m_lastAction) / 1000);
560+
}
561+
}
562+
535563
BOOL FtpSSLWrapper::ReceiveFileStatus(long bytesReceived) {
536564
BOOL res = CUT_FTPClient::ReceiveFileStatus(bytesReceived);
537565
if (res == FALSE)
@@ -603,7 +631,7 @@ int FtpSSLWrapper::OnSSLCertificate(const SSL * ssl, const X509* certificate, in
603631
}
604632

605633
if (verifyResult == X509_V_OK || previouslyAccepted) {
606-
OutMsg("[FTPS] Certificate valid.");
634+
OutDebug("[FTPS] Certificate valid.");
607635
} else {
608636
//X509_error_string(verifyResult);
609637
OutErr("[FTPS] Certificate invalid (%d): %s.", verifyResult, X509_verify_cert_error_string(verifyResult));
@@ -620,14 +648,14 @@ int FtpSSLWrapper::OnSSLCertificate(const SSL * ssl, const X509* certificate, in
620648
int ret = MessageBox(_MainOutputWindow, msgBuf, TEXT("FTP(E)S certificate verification"), MB_YESNO | MB_ICONWARNING);
621649
SU::FreeTChar(msgBuf);
622650
if (ret == IDYES) {
623-
OutMsg("[FTPS] Certificate accepted");
651+
OutDebug("[FTPS] Certificate accepted");
624652

625653
if (m_certificates) {
626654
SSL_get_peer_certificate(ssl); //increase reference counter
627655
m_certificates->push_back(certificate);
628656
}
629657
} else {
630-
OutMsg("[FTPS] Certificate rejected");
658+
OutDebug("[FTPS] Certificate rejected");
631659
return UTE_ERROR;
632660
}
633661
}

0 commit comments

Comments
 (0)