1
{==============================================================================|
2
| Project : Ararat Synapse | 001.001.001 |
3
|==============================================================================|
4
| Content: Trivial FTP (TFTP) client and server |
5
|==============================================================================|
6
| Copyright (c)1999-2010, Lukas Gebauer |
7
| All rights reserved. |
9
| Redistribution and use in source and binary forms, with or without |
10
| modification, are permitted provided that the following conditions are met: |
12
| Redistributions of source code must retain the above copyright notice, this |
13
| list of conditions and the following disclaimer. |
15
| Redistributions in binary form must reproduce the above copyright notice, |
16
| this list of conditions and the following disclaimer in the documentation |
17
| and/or other materials provided with the distribution. |
19
| Neither the name of Lukas Gebauer nor the names of its contributors may |
20
| be used to endorse or promote products derived from this software without |
21
| specific prior written permission. |
23
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
24
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
27
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
29
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
30
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
34
|==============================================================================|
35
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
36
| Portions created by Lukas Gebauer are Copyright (c)2003-2010. |
37
| All Rights Reserved. |
38
|==============================================================================|
40
|==============================================================================|
41
| History: see HISTORY.HTM from distribution package |
42
| (Found at URL: http://www.ararat.cz/synapse/) |
43
|==============================================================================}
45
{: @abstract(TFTP client and server protocol)
57
{$WARN IMPLICIT_STRING_CAST OFF}
58
{$WARN IMPLICIT_STRING_CAST_LOSS OFF}
79
{:@abstract(Implementation of TFTP client and server)
80
Note: Are you missing properties for specify server address and port? Look to
81
parent @link(TSynaClient) too!}
82
TTFTPSend = class(TSynaClient)
84
FSock: TUDPBlockSocket;
90
function SendPacket(Cmd: word; Serial: word; const Value: string): Boolean;
91
function RecvPacket(Serial: word; var Value: string): Boolean;
94
destructor Destroy; override;
96
{:Upload @link(data) as file to TFTP server.}
97
function SendFile(const Filename: string): Boolean;
99
{:Download file from TFTP server to @link(data).}
100
function RecvFile(const Filename: string): Boolean;
102
{:Acts as TFTP server and wait for client request. When some request
103
incoming within Timeout, result is @true and parametres is filled with
104
information from request. You must handle this request, validate it, and
105
call @link(ReplyError), @link(ReplyRecv) or @link(ReplySend) for send reply
107
function WaitForRequest(var Req: word; var filename: string): Boolean;
109
{:send error to TFTP client, when you acts as TFTP server.}
110
procedure ReplyError(Error: word; Description: string);
112
{:Accept uploaded file from TFTP client to @link(data), when you acts as
114
function ReplyRecv: Boolean;
116
{:Accept download request file from TFTP client and send content of
117
@link(data), when you acts as TFTP server.}
118
function ReplySend: Boolean;
120
{:Code of TFTP error.}
121
property ErrorCode: integer read FErrorCode;
123
{:Human readable decription of TFTP error. (if is sended by remote side)}
124
property ErrorString: string read FErrorString;
126
{:MemoryStream with datas for sending or receiving}
127
property Data: TMemoryStream read FData;
129
{:Address of TFTP remote side.}
130
property RequestIP: string read FRequestIP write FRequestIP;
132
{:Port of TFTP remote side.}
133
property RequestPort: string read FRequestPort write FRequestPort;
138
constructor TTFTPSend.Create;
141
FSock := TUDPBlockSocket.Create;
143
FTargetPort := cTFTPProtocol;
144
FData := TMemoryStream.Create;
149
destructor TTFTPSend.Destroy;
156
function TTFTPSend.SendPacket(Cmd: word; Serial: word; const Value: string): Boolean;
164
s := CodeInt(Cmd) + CodeInt(Serial) + Value
166
s := CodeInt(Cmd) + Value;
168
s := FSock.RecvPacket(FTimeout);
169
if FSock.LastError = 0 then
170
if length(s) >= 4 then
172
sh := CodeInt(4) + CodeInt(Serial);
173
if Pos(sh, s) = 1 then
178
FErrorCode := DecodeInt(s, 3);
180
FErrorString := SeparateLeft(s, #0);
185
function TTFTPSend.RecvPacket(Serial: word; var Value: string): Boolean;
194
s := FSock.RecvPacket(FTimeout);
195
if FSock.LastError = 0 then
196
if length(s) >= 4 then
197
if DecodeInt(s, 1) = 3 then
199
ser := DecodeInt(s, 3);
204
S := CodeInt(4) + CodeInt(ser);
206
Result := FSock.LastError = 0;
210
S := CodeInt(5) + CodeInt(5) + 'Unexcepted serial#' + #0;
214
if DecodeInt(s, 1) = 5 then
216
FErrorCode := DecodeInt(s, 3);
218
FErrorString := SeparateLeft(s, #0);
222
function TTFTPSend.SendFile(const Filename: string): Boolean;
232
FSock.Connect(FTargetHost, FTargetPort);
234
if FSock.LastError = 0 then
236
s := Filename + #0 + 'octet' + #0;
237
if not Sendpacket(2, 0, s) then
241
n1 := FData.Size div 512;
242
n2 := FData.Size mod 512;
245
s := ReadStrFromStream(FData, 512);
246
// SetLength(s, 512);
247
// FData.Read(pointer(s)^, 512);
248
if not Sendpacket(3, ser, s) then
252
s := ReadStrFromStream(FData, n2);
254
// FData.Read(pointer(s)^, n2);
255
if not Sendpacket(3, ser, s) then
264
function TTFTPSend.RecvFile(const Filename: string): Boolean;
273
FSock.Connect(FTargetHost, FTargetPort);
275
if FSock.LastError = 0 then
277
s := CodeInt(1) + Filename + #0 + 'octet' + #0;
279
if FSock.LastError <> 0 then
284
if not RecvPacket(ser, s) then
287
WriteStrToStream(FData, s);
288
// FData.Write(pointer(s)^, length(s));
289
until length(s) <> 512;
298
function TTFTPSend.WaitForRequest(var Req: word; var filename: string): Boolean;
306
FSock.Bind('0.0.0.0', FTargetPort);
307
if FSock.LastError = 0 then
309
s := FSock.RecvPacket(FTimeout);
310
if FSock.LastError = 0 then
311
if Length(s) >= 4 then
313
FRequestIP := FSock.GetRemoteSinIP;
314
FRequestPort := IntToStr(FSock.GetRemoteSinPort);
315
Req := DecodeInt(s, 1);
317
filename := Trim(SeparateLeft(s, #0));
318
s := SeparateRight(s, #0);
319
s := SeparateLeft(s, #0);
320
Result := lowercase(trim(s)) = 'octet';
325
procedure TTFTPSend.ReplyError(Error: word; Description: string);
330
FSock.Connect(FRequestIP, FRequestPort);
331
s := CodeInt(5) + CodeInt(Error) + Description + #0;
336
function TTFTPSend.ReplyRecv: Boolean;
345
FSock.Connect(FRequestIP, FRequestPort);
347
s := CodeInt(4) + CodeInt(0);
352
if not RecvPacket(ser, s) then
355
WriteStrToStream(FData, s);
356
// FData.Write(pointer(s)^, length(s));
357
until length(s) <> 512;
365
function TTFTPSend.ReplySend: Boolean;
375
FSock.Connect(FRequestIP, FRequestPort);
379
n1 := FData.Size div 512;
380
n2 := FData.Size mod 512;
383
s := ReadStrFromStream(FData, 512);
384
// SetLength(s, 512);
385
// FData.Read(pointer(s)^, 512);
386
if not Sendpacket(3, ser, s) then
390
s := ReadStrFromStream(FData, n2);
392
// FData.Read(pointer(s)^, n2);
393
if not Sendpacket(3, ser, s) then
401
{==============================================================================}
1
{==============================================================================|
2
| Project : Ararat Synapse | 001.001.001 |
3
|==============================================================================|
4
| Content: Trivial FTP (TFTP) client and server |
5
|==============================================================================|
6
| Copyright (c)1999-2010, Lukas Gebauer |
7
| All rights reserved. |
9
| Redistribution and use in source and binary forms, with or without |
10
| modification, are permitted provided that the following conditions are met: |
12
| Redistributions of source code must retain the above copyright notice, this |
13
| list of conditions and the following disclaimer. |
15
| Redistributions in binary form must reproduce the above copyright notice, |
16
| this list of conditions and the following disclaimer in the documentation |
17
| and/or other materials provided with the distribution. |
19
| Neither the name of Lukas Gebauer nor the names of its contributors may |
20
| be used to endorse or promote products derived from this software without |
21
| specific prior written permission. |
23
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
24
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
27
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
29
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
30
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
34
|==============================================================================|
35
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
36
| Portions created by Lukas Gebauer are Copyright (c)2003-2010. |
37
| All Rights Reserved. |
38
|==============================================================================|
40
|==============================================================================|
41
| History: see HISTORY.HTM from distribution package |
42
| (Found at URL: http://www.ararat.cz/synapse/) |
43
|==============================================================================}
45
{: @abstract(TFTP client and server protocol)
57
{$WARN IMPLICIT_STRING_CAST OFF}
58
{$WARN IMPLICIT_STRING_CAST_LOSS OFF}
79
{:@abstract(Implementation of TFTP client and server)
80
Note: Are you missing properties for specify server address and port? Look to
81
parent @link(TSynaClient) too!}
82
TTFTPSend = class(TSynaClient)
84
FSock: TUDPBlockSocket;
90
function SendPacket(Cmd: word; Serial: word; const Value: string): Boolean;
91
function RecvPacket(Serial: word; var Value: string): Boolean;
94
destructor Destroy; override;
96
{:Upload @link(data) as file to TFTP server.}
97
function SendFile(const Filename: string): Boolean;
99
{:Download file from TFTP server to @link(data).}
100
function RecvFile(const Filename: string): Boolean;
102
{:Acts as TFTP server and wait for client request. When some request
103
incoming within Timeout, result is @true and parametres is filled with
104
information from request. You must handle this request, validate it, and
105
call @link(ReplyError), @link(ReplyRecv) or @link(ReplySend) for send reply
107
function WaitForRequest(var Req: word; var filename: string): Boolean;
109
{:send error to TFTP client, when you acts as TFTP server.}
110
procedure ReplyError(Error: word; Description: string);
112
{:Accept uploaded file from TFTP client to @link(data), when you acts as
114
function ReplyRecv: Boolean;
116
{:Accept download request file from TFTP client and send content of
117
@link(data), when you acts as TFTP server.}
118
function ReplySend: Boolean;
120
{:Code of TFTP error.}
121
property ErrorCode: integer read FErrorCode;
123
{:Human readable decription of TFTP error. (if is sended by remote side)}
124
property ErrorString: string read FErrorString;
126
{:MemoryStream with datas for sending or receiving}
127
property Data: TMemoryStream read FData;
129
{:Address of TFTP remote side.}
130
property RequestIP: string read FRequestIP write FRequestIP;
132
{:Port of TFTP remote side.}
133
property RequestPort: string read FRequestPort write FRequestPort;
138
constructor TTFTPSend.Create;
141
FSock := TUDPBlockSocket.Create;
143
FTargetPort := cTFTPProtocol;
144
FData := TMemoryStream.Create;
149
destructor TTFTPSend.Destroy;
156
function TTFTPSend.SendPacket(Cmd: word; Serial: word; const Value: string): Boolean;
164
s := CodeInt(Cmd) + CodeInt(Serial) + Value
166
s := CodeInt(Cmd) + Value;
168
s := FSock.RecvPacket(FTimeout);
169
if FSock.LastError = 0 then
170
if length(s) >= 4 then
172
sh := CodeInt(4) + CodeInt(Serial);
173
if Pos(sh, s) = 1 then
178
FErrorCode := DecodeInt(s, 3);
180
FErrorString := SeparateLeft(s, #0);
185
function TTFTPSend.RecvPacket(Serial: word; var Value: string): Boolean;
194
s := FSock.RecvPacket(FTimeout);
195
if FSock.LastError = 0 then
196
if length(s) >= 4 then
197
if DecodeInt(s, 1) = 3 then
199
ser := DecodeInt(s, 3);
204
S := CodeInt(4) + CodeInt(ser);
206
Result := FSock.LastError = 0;
210
S := CodeInt(5) + CodeInt(5) + 'Unexcepted serial#' + #0;
214
if DecodeInt(s, 1) = 5 then
216
FErrorCode := DecodeInt(s, 3);
218
FErrorString := SeparateLeft(s, #0);
222
function TTFTPSend.SendFile(const Filename: string): Boolean;
232
FSock.Connect(FTargetHost, FTargetPort);
234
if FSock.LastError = 0 then
236
s := Filename + #0 + 'octet' + #0;
237
if not Sendpacket(2, 0, s) then
241
n1 := FData.Size div 512;
242
n2 := FData.Size mod 512;
245
s := ReadStrFromStream(FData, 512);
246
// SetLength(s, 512);
247
// FData.Read(pointer(s)^, 512);
248
if not Sendpacket(3, ser, s) then
252
s := ReadStrFromStream(FData, n2);
254
// FData.Read(pointer(s)^, n2);
255
if not Sendpacket(3, ser, s) then
264
function TTFTPSend.RecvFile(const Filename: string): Boolean;
273
FSock.Connect(FTargetHost, FTargetPort);
275
if FSock.LastError = 0 then
277
s := CodeInt(1) + Filename + #0 + 'octet' + #0;
279
if FSock.LastError <> 0 then
284
if not RecvPacket(ser, s) then
287
WriteStrToStream(FData, s);
288
// FData.Write(pointer(s)^, length(s));
289
until length(s) <> 512;
298
function TTFTPSend.WaitForRequest(var Req: word; var filename: string): Boolean;
306
FSock.Bind('0.0.0.0', FTargetPort);
307
if FSock.LastError = 0 then
309
s := FSock.RecvPacket(FTimeout);
310
if FSock.LastError = 0 then
311
if Length(s) >= 4 then
313
FRequestIP := FSock.GetRemoteSinIP;
314
FRequestPort := IntToStr(FSock.GetRemoteSinPort);
315
Req := DecodeInt(s, 1);
317
filename := Trim(SeparateLeft(s, #0));
318
s := SeparateRight(s, #0);
319
s := SeparateLeft(s, #0);
320
Result := lowercase(trim(s)) = 'octet';
325
procedure TTFTPSend.ReplyError(Error: word; Description: string);
330
FSock.Connect(FRequestIP, FRequestPort);
331
s := CodeInt(5) + CodeInt(Error) + Description + #0;
336
function TTFTPSend.ReplyRecv: Boolean;
345
FSock.Connect(FRequestIP, FRequestPort);
347
s := CodeInt(4) + CodeInt(0);
352
if not RecvPacket(ser, s) then
355
WriteStrToStream(FData, s);
356
// FData.Write(pointer(s)^, length(s));
357
until length(s) <> 512;
365
function TTFTPSend.ReplySend: Boolean;
375
FSock.Connect(FRequestIP, FRequestPort);
379
n1 := FData.Size div 512;
380
n2 := FData.Size mod 512;
383
s := ReadStrFromStream(FData, 512);
384
// SetLength(s, 512);
385
// FData.Read(pointer(s)^, 512);
386
if not Sendpacket(3, ser, s) then
390
s := ReadStrFromStream(FData, n2);
392
// FData.Read(pointer(s)^, n2);
393
if not Sendpacket(3, ser, s) then
401
{==============================================================================}