Should not crash when data port cannot be received during file transfer

This commit is contained in:
mysticbbs 2013-09-07 18:52:49 -04:00
parent c12aa847be
commit ada1c5dfd1
1 changed files with 27 additions and 13 deletions

View File

@ -11,10 +11,10 @@ Uses
Type Type
TFTPClient = Class(TTCPClient) TFTPClient = Class(TTCPClient)
DataPort : Word; DataPort : Word;
DataIP : String; DataIP : String;
DataSocket : TIOSocket; DataSocket : TIOSocket;
IsPassive : Boolean; IsPassive : Boolean;
Constructor Create (NetI: String); Override; Constructor Create (NetI: String); Override;
Function OpenDataSession : Boolean; Function OpenDataSession : Boolean;
@ -34,13 +34,16 @@ Uses
m_FileIO, m_FileIO,
m_Strings; m_Strings;
Const
ftpDefaultDataPort = 20;
Constructor TFTPClient.Create (NetI: String); Constructor TFTPClient.Create (NetI: String);
Begin Begin
Inherited Create(NetI); Inherited Create(NetI);
IsPassive := False; IsPassive := False;
DataIP := ''; DataIP := '';
DataPort := 10000; DataPort := ftpDefaultDataPort;
End; End;
Function TFTPClient.OpenDataSession : Boolean; Function TFTPClient.OpenDataSession : Boolean;
@ -151,7 +154,7 @@ Begin
End; End;
End Else Begin End Else Begin
IsPassive := False; IsPassive := False;
DataPort := 10000; // Calc and make configurable? DataPort := ftpDefaultDataPort;
Result := SendCommand('PORT ' + strReplace(Client.PeerIP, '.', ',') + ',' + strI2S(WordRec(DataPort).Hi) + ',' + strI2S(WordRec(DataPort).Lo)) = 200; Result := SendCommand('PORT ' + strReplace(Client.PeerIP, '.', ',') + ',' + strI2S(WordRec(DataPort).Hi) + ',' + strI2S(WordRec(DataPort).Lo)) = 200;
End; End;
End; End;
@ -159,8 +162,9 @@ End;
Function TFTPClient.SendFile (Passive: Boolean; FileName: String) : Boolean; Function TFTPClient.SendFile (Passive: Boolean; FileName: String) : Boolean;
Var Var
F : File; F : File;
Buffer : Array[1..8*1024] of Char; Buffer : Array[1..8 * 1024] of Char;
Res : LongInt; Res : LongInt;
OK : Boolean;
Begin Begin
Result := False; Result := False;
@ -174,9 +178,9 @@ Begin
Client.WriteLine ('STOR ' + JustFile(FileName)); Client.WriteLine ('STOR ' + JustFile(FileName));
OpenDataSession; OK := OpenDataSession;
If GetResponse = 150 Then Begin If OK and (GetResponse = 150) Then Begin
WriteLn ('DEBUG BEGIN SEND FILE'); WriteLn ('DEBUG BEGIN SEND FILE');
Assign (F, FileName); Assign (F, FileName);
@ -197,8 +201,14 @@ Begin
CloseDataSession; CloseDataSession;
Result := GetResponse = 226; Result := GetResponse = 226;
End Else End Else Begin
If IsPassive Then
WriteLn ('DEBUG unable to connect to FTP server for data session')
Else
WriteLn ('DEBUG unable to establish data session on port ', DataPort);
CloseDataSession; CloseDataSession;
End;
End; End;
Function TFTPClient.GetFile (Passive: Boolean; FileName: String) : Boolean; Function TFTPClient.GetFile (Passive: Boolean; FileName: String) : Boolean;
@ -206,6 +216,7 @@ Var
F : File; F : File;
Res : LongInt; Res : LongInt;
Buffer : Array[1..8*1024] of Char; Buffer : Array[1..8*1024] of Char;
OK : Boolean;
Begin Begin
Result := False; Result := False;
@ -215,9 +226,9 @@ Begin
Client.WriteLine('RETR ' + JustFile(FileName)); Client.WriteLine('RETR ' + JustFile(FileName));
OpenDataSession; OK := OpenDataSession;
If GetResponse = 150 Then Begin If OK And (GetResponse = 150) Then Begin
Assign (F, FileName); Assign (F, FileName);
If ioReWrite(F, 1, fmRWDW) Then Begin If ioReWrite(F, 1, fmRWDW) Then Begin
@ -236,8 +247,11 @@ Begin
CloseDataSession; CloseDataSession;
Result := GetResponse = 226; Result := GetResponse = 226;
End Else End Else Begin
WriteLn ('DEBUG unable to open data session, receive aborted');
CloseDataSession; CloseDataSession;
End;
End; End;
Function TFTPClient.ChangeDirectory (Str: String) : Boolean; Function TFTPClient.ChangeDirectory (Str: String) : Boolean;