1 | {
|
---|
2 | TFTP supports five types of packets, all of which have been mentioned
|
---|
3 | above:
|
---|
4 | opcode operation
|
---|
5 |
|
---|
6 | 1 Read request (RRQ)
|
---|
7 | 2 Write request (WRQ)
|
---|
8 | 3 Data (DATA)
|
---|
9 | 4 Acknowledgment (ACK)
|
---|
10 | 5 Error (ERROR)
|
---|
11 |
|
---|
12 |
|
---|
13 | Error Codes
|
---|
14 | Value Meaning
|
---|
15 |
|
---|
16 | 0 Not defined, see error message (if any).
|
---|
17 | 1 File not found.
|
---|
18 | 2 Access violation.
|
---|
19 | 3 Disk full or allocation exceeded.
|
---|
20 | 4 Illegal TFTP operation.
|
---|
21 | 5 Unknown transfer ID.
|
---|
22 | 6 File already exists.
|
---|
23 | 7 No such user.
|
---|
24 |
|
---|
25 |
|
---|
26 | }
|
---|
27 |
|
---|
28 | unit TFTPDaemonThread;
|
---|
29 |
|
---|
30 | interface
|
---|
31 |
|
---|
32 | uses Classes, SysUtils, FTPTSend;
|
---|
33 |
|
---|
34 | type
|
---|
35 | TTFTPDaemonThread = class(TThread)
|
---|
36 | private
|
---|
37 | { Private declarations }
|
---|
38 | TFTPDaemon:TTFTPSend;
|
---|
39 | FIPAdress:String;
|
---|
40 | FPort:String;
|
---|
41 | FLogMessage:String;
|
---|
42 | procedure UpdateLog;
|
---|
43 | protected
|
---|
44 | procedure Execute; override;
|
---|
45 | public
|
---|
46 | constructor Create(IPAdress,Port:String);
|
---|
47 | end;
|
---|
48 |
|
---|
49 | implementation
|
---|
50 |
|
---|
51 | uses MainBox;
|
---|
52 |
|
---|
53 | constructor TTFTPDaemonThread.Create(IPAdress,Port:String);
|
---|
54 | begin
|
---|
55 | FIPAdress := IPAdress;
|
---|
56 | FPort := Port;
|
---|
57 | inherited Create(False);
|
---|
58 | end;
|
---|
59 |
|
---|
60 | procedure TTFTPDaemonThread.UpdateLOG;
|
---|
61 | begin
|
---|
62 | MainForm.Log.Lines.Add(FLogMessage);
|
---|
63 | end;
|
---|
64 |
|
---|
65 | procedure TTFTPDaemonThread.Execute;
|
---|
66 | var RequestType:Word;
|
---|
67 | FileName:String;
|
---|
68 | begin
|
---|
69 | TFTPDaemon := TTFTPSend.Create;
|
---|
70 | FLogMessage := 'ServerThread created on Port ' + FPort;
|
---|
71 | Synchronize(UpdateLog);
|
---|
72 | TFTPDaemon.TargetHost := FIPAdress;
|
---|
73 | TFTPDaemon.TargetPort := FPort;
|
---|
74 | try
|
---|
75 | while not terminated do
|
---|
76 | begin
|
---|
77 | if TFTPDaemon.WaitForRequest(RequestType,FileName)
|
---|
78 | then
|
---|
79 | begin
|
---|
80 | // Fill the Log-Memo whith Infos about the request
|
---|
81 | case RequestType of
|
---|
82 | 1:FLogMessage := 'Read-Request from '
|
---|
83 | + TFTPDaemon.RequestIP + ':' + TFTPDaemon.RequestPort;
|
---|
84 | 2:FLogMessage := 'Write-Request from '
|
---|
85 | + TFTPDaemon.RequestIP + ':' + TFTPDaemon.RequestPort;
|
---|
86 | end;
|
---|
87 | Synchronize(UpdateLog);
|
---|
88 | FLogMessage := 'File: ' + Filename;
|
---|
89 | Synchronize(UpdateLog);
|
---|
90 |
|
---|
91 | // Process the Request
|
---|
92 | case RequestType of
|
---|
93 | 1:begin // Read request (RRQ)
|
---|
94 | if FileExists(MainForm.PathEdit.Text + FileName)
|
---|
95 | then
|
---|
96 | begin
|
---|
97 | TFTPDaemon.Data.LoadFromFile(MainForm.PathEdit.Text + FileName);
|
---|
98 | if TFTPDaemon.ReplySend
|
---|
99 | then
|
---|
100 | begin
|
---|
101 | FLogMessage := '"' + MainForm.PathEdit.Text + FileName + '" successfully sent.';
|
---|
102 | Synchronize(UpdateLog);
|
---|
103 | end;
|
---|
104 | end
|
---|
105 | else TFTPDaemon.ReplyError(1,'File not Found');
|
---|
106 | end;
|
---|
107 | 2:begin // Write request (WRQ)
|
---|
108 | if not FileExists(MainForm.PathEdit.Text + FileName)
|
---|
109 | then
|
---|
110 | begin
|
---|
111 | if TFTPDaemon.ReplyRecv
|
---|
112 | then
|
---|
113 | begin
|
---|
114 | TFTPDaemon.Data.SaveToFile(MainForm.PathEdit.Text + FileName);
|
---|
115 | FLogMessage := 'File sucessfully stored to ' + MainForm.PathEdit.Text + FileName;
|
---|
116 | Synchronize(UpdateLog);
|
---|
117 | end;
|
---|
118 | end
|
---|
119 | else TFTPDaemon.ReplyError(6,'File already exists');
|
---|
120 | end;
|
---|
121 | end;
|
---|
122 | end;
|
---|
123 | end;
|
---|
124 | finally
|
---|
125 | TFTPDaemon.Free;
|
---|
126 | end;
|
---|
127 | end;
|
---|
128 |
|
---|
129 | end.
|
---|