-
Notifications
You must be signed in to change notification settings - Fork 0
/
FtpClient.cs
333 lines (286 loc) · 9.99 KB
/
FtpClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System;
using Microsoft.SPOT;
using System.Net.Sockets;
using System.Net;
using Microsoft.SPOT.Net.NetworkInformation;
using System.Text;
using System.Collections;
using System.IO;
using System.Threading;
namespace Redecode.Archimede
{
//-- For Raw FTP command see http://www.nsftools.com/tips/RawFTP.htm
public class FtpClient
{
public string IP;
public int Port;
public string Username;
public string Password;
Socket SocketCommands;
Socket SocketData;
int Timeout = 5000;
public FtpClient()
{
}
public FtpClient(string ip, int port)
{
IP = ip;
Port = port;
}
public FtpClient(string ip, int port, string username, string password)
{
IP = ip;
Port = port;
Username = username;
Password = password;
}
public bool Connect(int timeout = 5000)
{
Timeout = timeout;
try
{
string rString = "";
Ethernet.Connect();
SocketCommands = SocketStable.Connect(IP, Port, Timeout);
SocketData = null;
//int passivePort = -1; // Used with socketB when using the data connection
if (SocketCommands == null)
{
return false;
}
rString = readTextFromSocket(SocketCommands);
sendTextToSocket(SocketCommands, "USER " + Username + "\r\n");
rString = readTextFromSocket(SocketCommands);
sendTextToSocket(SocketCommands, "PASS " + Password + "\r\n");
rString = readTextFromSocket(SocketCommands);
return true;
}
catch (Exception ex)
{
return false;
}
}
public void Disconnect()
{
string rString;
try
{
// All done so send the QUIT command to log out
sendTextToSocket(SocketCommands, "QUIT\r\n");
rString = readTextFromSocket(SocketCommands);
}
catch
{
}
if (SocketCommands != null)
{
SocketCommands.Close();
}
if (SocketData != null)
{
SocketData.Close();
}
}
public string[] ListDirectory()
{
string rString;
sendTextToSocket(SocketCommands, "NLST\r\n");
using (SocketData = SocketPassiveMode())
{
rString = readTextFromSocket(SocketData); // READING FROM SOCKET B NOW!
SocketData.Close();
}
string[] list = rString.Split('\n');
for (int i = 0; i < list.Length; i++)
{
list[i] = list[i].Trim();
}
return list;
//throw new Exception("Not implemented");
}
public bool DownloadFile(string remote_path, string local_path)
{
try
{
byte[] fileContent = ReadBinaryFile(remote_path);
if (fileContent == null)
{
return false;
}
using (FileStream FileHandle = new FileStream(local_path, FileMode.OpenOrCreate))
{
FileHandle.Write(fileContent, 0, fileContent.Length);
FileHandle.Close();
}
return true;
}
catch
{
return false;
}
}
public string ReadTextFile(string remote_path)
{
try
{
string data;
string rString = "";
sendTextToSocket(SocketCommands, "TYPE A\r\n"); // Not sure if necessary, but FileZilla client does this
rString = readTextFromSocket(SocketCommands);
using (SocketData = SocketPassiveMode())
{
sendTextToSocket(SocketCommands, "RETR " + remote_path + "\r\n");
rString = readTextFromSocket(SocketCommands);
data = readTextFromSocket(SocketData);
SocketData.Close();
}
return data;
}
catch
{
return null;
}
}
public byte[] ReadBinaryFile(string remote_path)
{
try
{
string rString = "";
byte[] data;
sendTextToSocket(SocketCommands, "TYPE I\r\n"); // Not sure if necessary, but FileZilla client does this
rString = readTextFromSocket(SocketCommands);
using (SocketData = SocketPassiveMode())
{
sendTextToSocket(SocketCommands, "RETR " + remote_path + "\r\n");
rString = readTextFromSocket(SocketCommands);
data = readBytesFromSocket(SocketData);
SocketData.Close();
}
return data;
}
catch
{
return null;
}
}
public bool UploadFile(string local_path, string remote_path)
{
try
{
byte[] fileContent = File.ReadAllBytes(local_path);
WriteBinaryFile(remote_path, fileContent);
return true;
}
catch
{
return false;
}
}
public bool WriteTextFile(string remote_path, string data)
{
try
{
string rString;
sendTextToSocket(SocketCommands, "TYPE A\r\n"); // Not sure if necessary, but FileZilla client does this
rString = readTextFromSocket(SocketCommands);
using (SocketData = SocketPassiveMode())
{
sendTextToSocket(SocketCommands, "STOR " + remote_path + ".txt\r\n");
rString = readTextFromSocket(SocketCommands);
sendTextToSocket(SocketData, data);
SocketData.Close();
}
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool WriteBinaryFile(string remote_path, byte[] data)
{
try
{
string rString;
sendTextToSocket(SocketCommands, "TYPE I\r\n"); // Not sure if necessary, but FileZilla client does this
rString = readTextFromSocket(SocketCommands);
using (SocketData = SocketPassiveMode())
{
sendTextToSocket(SocketCommands, "STOR " + remote_path + "\r\n");
rString = readTextFromSocket(SocketCommands);
sendBytesToSocket(SocketData, data);
SocketData.Close();
}
return true;
}
catch
{
return false;
}
}
private static string readTextFromSocket(Socket sock)
{
return new string(Encoding.UTF8.GetChars(readBytesFromSocket(sock)));
}
private static byte[] readBytesFromSocket(Socket sock)
{
//string receivedString = "";
int bytesAvailToRead = 0;
byte[] buffer = null;
var receivedBytes = new MemoryStream();
int waitCount = 0;
// The socket did not always have bytes immediately
// available to read, so I wait a bit until it is
// ready. This could be changed/shortened. Almost no
// testing was done here. to optimize.
while (waitCount < 10 && sock.Available == 0)
{
System.Threading.Thread.Sleep(100);
waitCount++;
}
while (sock.Available > 0)
{
if (sock.Poll(1000, SelectMode.SelectRead))
{
bytesAvailToRead = sock.Available;
buffer = new byte[bytesAvailToRead];
sock.Receive(buffer, bytesAvailToRead, SocketFlags.None);
receivedBytes.Write(buffer, 0, buffer.Length);
}
}
return receivedBytes.ToArray();
}
private static void sendTextToSocket(Socket sock, string data)
{
sendBytesToSocket(sock, Encoding.UTF8.GetBytes(data));
}
private static void sendBytesToSocket(Socket sock, byte[] data)
{
if (sock.Poll(1000, SelectMode.SelectWrite))
{
int numSent = sock.Send(data);
}
}
private Socket SocketPassiveMode()
{
string rString;
sendTextToSocket(SocketCommands, "PASV\r\n");
rString = readTextFromSocket(SocketCommands);
int passivePort = parseForPasvPort(rString);
return SocketStable.Connect(IP, passivePort, Timeout);
}
private static int parseForPasvPort(string s)
{
// Probably only works for FileZilla servers.
// There are suggestions online for how to read/parse
// for a passive port number online. For example,
// here: <a href="http://cr.yp.to/ftp/retr.html" target="_blank" rel="nofollow">http://cr.yp.to/ftp/retr.html</a>
int i, j;
i = s.IndexOf('(');
j = s.IndexOf(')');
string ip = s.Substring(i + 1, j - i - 1);
string[] pieces = ip.Split(',');
return System.Convert.ToInt32(pieces[pieces.Length - 2]) * 256 + System.Convert.ToInt32(pieces[pieces.Length - 1]);
}
}
}