-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftpUtilities.c
290 lines (234 loc) · 6.6 KB
/
ftpUtilities.c
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
#include "ftpUtilities.h"
// loadBar code based on
// http://www.rosshemsley.co.uk/2011/02/creating-a-progress-bar-in-c-or-any-other-console-app/
// Process has done x out of n rounds,
// and we want a bar of width w and resolution r.
static inline void loadBar(int x, int n, int r, int w, int doneSize, int totalSize) {
// Only update r times.
if ( x % (n/r) != 0 ) return;
// Calculuate the ratio of complete-to-incomplete.
float ratio = x/(float)n;
int c = ratio * w;
// Show the percentage complete.
printf("%3d%%[", (int)(ratio*100));
// Show the load bar.
int y;
for (y = 0; y < c; y++)
printf("=");
for (y = c; y < w; y++)
printf(" ");
// ANSI Control codes to go back to the
// previous line and clear it.
printf("] %d/%d B", doneSize, totalSize);
printf("\r"); // Move to the first column
fflush(stdout);
if(x == n) {
printf("\n");
}
}
struct hostent* getHostInfo(char* hostname) {
struct hostent *h;
if ((h=gethostbyname(hostname)) == NULL) {
return NULL;
}
return h;
}
int openTCPandConnectServer(char* hostname, unsigned int port) {
//Get hostent struct for the hostname provided and retrieve the host address
struct hostent* h = getHostInfo(hostname);
if(h == NULL) {
printf("Could not resolve hostname\n");
return -1;
}
char* host = inet_ntoa(*((struct in_addr *)h->h_addr));
struct addrinfo server_addr, *res;
int sockfd, status;
// first, load up address structs with getaddrinfo():
memset(&server_addr, 0, sizeof server_addr);
server_addr.ai_family = AF_INET;
server_addr.ai_socktype = SOCK_STREAM;
char* portStr = itoa(port);
if((status = getaddrinfo(host, portStr, &server_addr, &res)) != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
//create socket
if((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
perror("socket()");
return -1;
}
// connect
if((connect(sockfd, res->ai_addr, res->ai_addrlen) < 0)) {
perror("connect()");
return -1;
}
return sockfd;
}
int openDataPort(char* hostname, int receivedPort) {
int dataSocketFD = openTCPandConnectServer(hostname, receivedPort);
return(dataSocketFD);
}
int getResponse(int sockfd, char* response, int verbose) {
int ind = 0, totalSize = 0;
char line[MAX_LINE_SIZE];
int isMultiLine = 0;
int returnCode = 0;
//Read a line
ind = readLine(sockfd, line);
//Copy said line to the response variable
memcpy(response+totalSize, line, ind);
//Calculate the line "return code"
returnCode = getReturnCode(line);
//Update total response size with line size
totalSize += ind;
//Get line identifier(i.e.: "220-" or "220 ")
char ident[4];
getLineIdentifier(line, ident);
//A multiline FTP response is identified by
//[code]-
//[code]- something
//[code]- something
//[code]- something
//[code]
//So we need to read from the server while we dont get a return code the same as the first line's followed
//by an empty space
if(ident[3] == '-')
isMultiLine = 1;
if(isMultiLine) {
int lineReturnCode;
char lineIdent[4];
do {
ind = readLine(sockfd, line);
memcpy(response+totalSize, line, ind);
lineReturnCode = getReturnCode(line);
totalSize += ind;
getLineIdentifier(line, lineIdent);
} while(lineReturnCode != returnCode || lineIdent[3] != ' ');
}
response[totalSize] = '\0';
if(verbose)
printf("%s", response);
return returnCode;
}
int readLine(int sockfd, char* line) {
int lineSize = 0;
char buf[1];
int r = 0;
//An FTP line always ends on a LF+CR sequence, that is, line feed (newline) and carriage return
//So we keep on reading until we find this sequence
do {
r = recv(sockfd, buf, 1, 0);
if(r == 0) {
fprintf(stderr, "Host closed connection!\n");
exit(1);
}
if(r == -1) {
perror("recv()");
exit(1);
}
line[lineSize] = buf[0];
lineSize++;
}
while(lineSize < 2 || line[lineSize - 1] != '\n' || line[lineSize - 2] != '\r');
return lineSize;
}
int communicate(int sockfd, char* com, int verbose) {
int bytes = send(sockfd, com, strlen(com), 0);
if(bytes != strlen(com)) {
fprintf(stderr, "Error, problem communicating with server\n");
return -1;
}
if(verbose)
printf(">%s", com);
return 0;
}
int checkIfServerReady(int commandSocketFD, int verbose) {
char response[MAX_SIZE];
int code = 0;
code = getResponse(commandSocketFD, response, verbose);
if(code != 220) {
printf("Failed with code %d\n", code);
return -1;
}
return 0;
}
int setUsername(int commandSocketFD, char* username, int verbose) {
char response[MAX_SIZE];
char login[MAX_SIZE];
int code = 0;
sprintf(login, "user %s\n", username);
communicate(commandSocketFD, login, verbose);
code = getResponse(commandSocketFD, response, verbose);
if(code != 331) {
printf("Failed with code %d\n", code);
return -1;
}
return 0;
}
int setPassword(int commandSocketFD, char* password, int verbose) {
char response[MAX_SIZE];
char pass[MAX_SIZE];
int code = 0;
sprintf(pass, "pass %s\n", password);
communicate(commandSocketFD, pass, verbose);
code = getResponse(commandSocketFD, response, verbose);
if(code != 230) {
printf("Failed with code %d\n", code);
return -1;
}
return 0;
}
int setPassiveMode(int commandSocketFD, int verbose) {
char response[MAX_SIZE];
int code = 0;
communicate(commandSocketFD, "pasv\n", verbose);
code = getResponse(commandSocketFD, response, verbose);
if(code != 227) {
printf("Failed with code %d\n", code);
return -1;
}
int receivedPort = 0;
if(calculatePasvPort(&receivedPort, response) != 0) {
return -1;
}
return receivedPort;
}
int retrieveFile(int commandSocketFD, char* fileUrl, int verbose) {
char response[MAX_SIZE];
char command[MAX_SIZE];
int code = 0;
sprintf(command, "retr %s\n", fileUrl);
communicate(commandSocketFD, command, verbose);
code = getResponse(commandSocketFD, response, verbose);
int size = getFileSize(response);
if(code != 150) {
printf("Failed with code %d\n", code);
return -1;
}
return size;
}
int downloadFile(int dataSocketFD, char* filename, int fileSize) {
if(receiveFile(dataSocketFD, filename, fileSize) != 0) {
return(-1);
}
return 0;
}
int receiveFile(int sockfd, char* filename, int fileSize) {
FILE* f=fopen(filename, "wb");
if(f == NULL) {
perror("receiveFile()");
}
char buf[1024];
int read;
int sizeReceived = 0;
printf("Receiving file...\n");
loadBar(0, fileSize, fileSize, 50, 0, fileSize);
while ((read = recv(sockfd, buf, 1024, 0))) {
fwrite(buf, read, 1, f);
sizeReceived += read;
loadBar(sizeReceived, fileSize, fileSize, 50, sizeReceived, fileSize);
}
printf("File received!\n");
fclose(f);
return 0;
}